2 * dialog-analysis-tool-chi-squared.c:
5 * Andreas J. Guelzow <aguelzow@taliesin.ca>
7 * (C) Copyright 2008 by Andreas J. Guelzow <aguelzow@pyrshep.ca>
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.
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.
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/>.
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
26 #include <dialogs/dialogs.h>
27 #include <tools/analysis-chi-squared.h>
28 #include <tools/analysis-tools.h>
31 #include <workbook-control.h>
33 #include <workbook-view.h>
35 #include <parse-util.h>
36 #include <gnm-format.h>
37 #include <dialogs/tool-dialogs.h>
38 #include <dialogs/dao-gui-utils.h>
41 #include <number-match.h>
43 #include <selection.h>
46 #include <dialogs/help.h>
48 #include <widgets/gnm-dao.h>
49 #include <widgets/gnm-expr-entry.h>
53 #define CHI_SQUARED_I_KEY "analysistools-chi-square-independence-dialog"
56 GnmGenericToolState base
;
57 GtkWidget
*alpha_entry
;
59 } ChiSquaredIToolState
;
62 * chi_squared_tool_ok_clicked_cb:
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.
71 chi_squared_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget
*button
,
72 ChiSquaredIToolState
*state
)
74 data_analysis_output_t
*dao
;
76 analysis_tools_data_chi_squared_t
*data
;
78 data
= g_new0 (analysis_tools_data_chi_squared_t
, 1);
79 dao
= parse_output ((GnmGenericToolState
*)state
, NULL
);
81 data
->input
= gnm_expr_entry_parse_as_value
82 (GNM_EXPR_ENTRY (state
->base
.input_entry
),
85 data
->wbc
= GNM_WBC (state
->base
.wbcg
);
87 data
->labels
= gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (state
->label
));
89 data
->alpha
= gtk_spin_button_get_value
90 (GTK_SPIN_BUTTON (state
->alpha_entry
));
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
));
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);
99 data
->n_c
--, data
->n_r
--;
102 if (!cmd_analysis_tool (data
->wbc
, state
->base
.sheet
,
103 dao
, data
, analysis_tool_chi_squared_engine
,
105 gtk_widget_destroy (state
->base
.dialog
);
111 calc_min_size (GnmValue
*input_range
)
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);
116 return ((row
< col
) ? row
: col
);
120 * chi_squared_tool_update_sensitivity_cb:
123 * Update the dialog widgets sensitivity.
124 * We cannot use tool_update_sensitivity_cb
125 * since we are also considering whether in fact
129 chi_squared_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget
*dummy
,
130 ChiSquaredIToolState
*state
)
133 GnmValue
*input_range
;
135 /* Checking Input Range */
136 input_range
= gnm_expr_entry_parse_as_value
137 (GNM_EXPR_ENTRY (state
->base
.input_entry
),
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
);
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
);
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
);
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
);
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 "
173 gtk_widget_set_sensitive (state
->base
.ok_button
, FALSE
);
177 gtk_label_set_text (GTK_LABEL (state
->base
.warning
), "");
178 gtk_widget_set_sensitive (state
->base
.ok_button
, TRUE
);
184 * dialog_chi_squared_tool:
189 * Show the dialog (guru).
193 dialog_chi_square_tool (WBCGtk
*wbcg
, Sheet
*sheet
, gboolean independence
)
195 ChiSquaredIToolState
*state
;
197 char const * plugins
[] = { "Gnumeric_fnstat",
202 if ((wbcg
== NULL
) ||
203 gnm_check_for_plugins_missing (plugins
, wbcg_toplevel (wbcg
)))
206 /* Only pop up one copy per workbook */
207 if (gnm_dialog_raise_if_exists (wbcg
, CHI_SQUARED_I_KEY
))
210 state
= g_new0 (ChiSquaredIToolState
, 1);
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 "
218 G_CALLBACK (chi_squared_tool_ok_clicked_cb
),
220 G_CALLBACK (chi_squared_tool_update_sensitivity_cb
),
221 GNM_EE_SINGLE_RANGE
))
228 type
="test-of-independence";
230 type
="test-of-homogeneity";
231 gtk_toggle_button_set_active
232 (GTK_TOGGLE_BUTTON (go_gtk_builder_get_widget (state
->base
.gui
,
236 state
->label
= tool_setup_update
237 (&state
->base
, "labels_button",
238 G_CALLBACK (chi_squared_tool_update_sensitivity_cb
),
241 state
->alpha_entry
= tool_setup_update
242 (&state
->base
, "alpha-entry",
243 G_CALLBACK (chi_squared_tool_update_sensitivity_cb
),
245 float_to_entry (GTK_ENTRY (state
->alpha_entry
), 0.05);
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
);