1 /*
2  * dialog-analysis-tool-chi-squared.c:
3  *
4  * Authors:
5   *  Andreas J. Guelzow  <aguelzow@taliesin.ca>
6  *
7  * (C) Copyright 2008 by Andreas J. Guelzow  <aguelzow@pyrshep.ca>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, see <https://www.gnu.org/licenses/>.
21  */
22 
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
25 #include <gnumeric.h>
26 #include <dialogs/dialogs.h>
27 #include <tools/analysis-chi-squared.h>
28 #include <tools/analysis-tools.h>
29 
30 #include <workbook.h>
31 #include <workbook-control.h>
32 #include <wbc-gtk.h>
33 #include <workbook-view.h>
34 #include <gui-util.h>
35 #include <parse-util.h>
36 #include <gnm-format.h>
37 #include <dialogs/tool-dialogs.h>
38 #include <dialogs/dao-gui-utils.h>
39 #include <sheet.h>
40 #include <expr.h>
41 #include <number-match.h>
42 #include <ranges.h>
43 #include <selection.h>
44 #include <value.h>
45 #include <commands.h>
46 #include <dialogs/help.h>
47 
48 #include <widgets/gnm-dao.h>
49 #include <widgets/gnm-expr-entry.h>
50 
51 #include <string.h>
52 
53 #define CHI_SQUARED_I_KEY      "analysistools-chi-square-independence-dialog"
54 
55 typedef struct {
56 	GnmGenericToolState base;
57 	GtkWidget *alpha_entry;
58 	GtkWidget *label;
59 } ChiSquaredIToolState;
60 
61 /**
62  * chi_squared_tool_ok_clicked_cb:
63  * @button:
64  * @state:
65  *
66  * Retrieve the information from the dialog and call the fourier_tool.
67  * Note that we assume that the ok_button is only active if the entry fields
68  * contain sensible data.
69  **/
70 static void
chi_squared_tool_ok_clicked_cb(G_GNUC_UNUSED GtkWidget * button,ChiSquaredIToolState * state)71 chi_squared_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
72 				 ChiSquaredIToolState *state)
73 {
74 	data_analysis_output_t  *dao;
75 	GtkWidget *w;
76 	analysis_tools_data_chi_squared_t *data;
77 
78 	data = g_new0 (analysis_tools_data_chi_squared_t, 1);
79 	dao  = parse_output ((GnmGenericToolState *)state, NULL);
80 
81 	data->input = gnm_expr_entry_parse_as_value
82 		(GNM_EXPR_ENTRY (state->base.input_entry),
83 		 state->base.sheet);
84 
85 	data->wbc = GNM_WBC (state->base.wbcg);
86 
87         data->labels = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->label));
88 
89 	data->alpha = gtk_spin_button_get_value
90 		(GTK_SPIN_BUTTON (state->alpha_entry));
91 
92 	w = go_gtk_builder_get_widget (state->base.gui, "test-of-independence");
93 	data->independence = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w));
94 
95 	data->n_c = (data->input->v_range.cell.b.col - data->input->v_range.cell.a.col + 1);
96 	data->n_r = (data->input->v_range.cell.b.row - data->input->v_range.cell.a.row + 1);
97 
98 	if (data->labels)
99 		data->n_c--, data->n_r--;
100 
101 
102 	if (!cmd_analysis_tool (data->wbc, state->base.sheet,
103 				dao, data, analysis_tool_chi_squared_engine,
104 				TRUE))
105 		gtk_widget_destroy (state->base.dialog);
106 
107 	return;
108 }
109 
110 static int
calc_min_size(GnmValue * input_range)111 calc_min_size (GnmValue *input_range)
112 {
113 	int row = (input_range->v_range.cell.b.row - input_range->v_range.cell.a.row + 1);
114 	int col = (input_range->v_range.cell.b.col - input_range->v_range.cell.a.col + 1);
115 
116 	return ((row < col) ? row : col);
117 }
118 
119 /**
120  * chi_squared_tool_update_sensitivity_cb:
121  * @state:
122  *
123  * Update the dialog widgets sensitivity.
124  * We cannot use tool_update_sensitivity_cb
125  * since we are also considering whether in fact
126  * an alpha is given.
127  **/
128 static void
chi_squared_tool_update_sensitivity_cb(G_GNUC_UNUSED GtkWidget * dummy,ChiSquaredIToolState * state)129 chi_squared_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
130 					ChiSquaredIToolState  *state)
131 {
132 	gnm_float alpha;
133         GnmValue *input_range;
134 
135 	/* Checking Input Range */
136         input_range = gnm_expr_entry_parse_as_value
137 		(GNM_EXPR_ENTRY (state->base.input_entry),
138 		 state->base.sheet);
139 	if (input_range == NULL) {
140 		gtk_label_set_text (GTK_LABEL (state->base.warning),
141 				    _("The input range is invalid."));
142 		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
143 		return;
144 	} else {
145 		int min_size = calc_min_size (input_range);
146 		gboolean label = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state->label));
147 		value_release (input_range);
148 
149 		if (min_size < (label ? 3 : 2)) {
150 			gtk_label_set_text (GTK_LABEL (state->base.warning),
151 					    _("The input range is too small."));
152 			gtk_widget_set_sensitive (state->base.ok_button, FALSE);
153 			return;
154 		}
155 	}
156 
157 	/* Checking Alpha*/
158 	alpha = gtk_spin_button_get_value
159 		(GTK_SPIN_BUTTON (state->alpha_entry));
160 	if (!(alpha > 0 && alpha < 1)) {
161 		gtk_label_set_text (GTK_LABEL (state->base.warning),
162 				    _("The alpha value should "
163 				      "be a number between 0 and 1."));
164 		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
165 		return;
166 	}
167 
168 	/* Checking Output Page */
169 	if (!gnm_dao_is_ready (GNM_DAO (state->base.gdao))) {
170 		gtk_label_set_text (GTK_LABEL (state->base.warning),
171 				    _("The output specification "
172 				      "is invalid."));
173 		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
174 		return;
175 	}
176 
177 	gtk_label_set_text (GTK_LABEL (state->base.warning), "");
178 	gtk_widget_set_sensitive (state->base.ok_button, TRUE);
179 
180 }
181 
182 
183 /**
184  * dialog_chi_squared_tool:
185  * @wbcg:
186  * @sheet:
187  * @independence
188  *
189  * Show the dialog (guru).
190  *
191  **/
192 int
dialog_chi_square_tool(WBCGtk * wbcg,Sheet * sheet,gboolean independence)193 dialog_chi_square_tool (WBCGtk *wbcg, Sheet *sheet, gboolean independence)
194 {
195         ChiSquaredIToolState *state;
196 	char const *type;
197 	char const * plugins[] = { "Gnumeric_fnstat",
198 				   "Gnumeric_fnlookup",
199 				   "Gnumeric_fnmath",
200 				   NULL};
201 
202 	if ((wbcg == NULL) ||
203 	    gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
204 		return 1;
205 
206 	/* Only pop up one copy per workbook */
207 	if (gnm_dialog_raise_if_exists (wbcg, CHI_SQUARED_I_KEY))
208 		return 0;
209 
210 	state = g_new0 (ChiSquaredIToolState, 1);
211 
212 	if (dialog_tool_init (&state->base, wbcg, sheet,
213 			      GNUMERIC_HELP_LINK_CHI_SQUARED,
214 			      "res:ui/chi-squared.ui", "Chi-Squared Tests",
215 			      _("Could not create the Chi Squared Tests "
216 				"tool dialog."),
217 			      CHI_SQUARED_I_KEY,
218 			      G_CALLBACK (chi_squared_tool_ok_clicked_cb),
219 			      NULL,
220 			      G_CALLBACK (chi_squared_tool_update_sensitivity_cb),
221 			      GNM_EE_SINGLE_RANGE))
222 	{
223 		g_free(state);
224 		return 0;
225 	}
226 
227 	if (independence)
228 		type ="test-of-independence";
229 	else
230 		type ="test-of-homogeneity";
231 	gtk_toggle_button_set_active
232 		(GTK_TOGGLE_BUTTON (go_gtk_builder_get_widget (state->base.gui,
233 							  type)),
234 		 TRUE);
235 
236 	state->label = tool_setup_update
237 		(&state->base, "labels_button",
238 		 G_CALLBACK (chi_squared_tool_update_sensitivity_cb),
239 		 state);
240 
241 	state->alpha_entry = tool_setup_update
242 		(&state->base, "alpha-entry",
243 		 G_CALLBACK (chi_squared_tool_update_sensitivity_cb),
244 		 state);
245 	float_to_entry (GTK_ENTRY (state->alpha_entry), 0.05);
246 
247 	gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
248 	chi_squared_tool_update_sensitivity_cb (NULL, state);
249 	tool_load_selection ((GnmGenericToolState *)state, TRUE);
250 
251         return 0;
252 }
253