dialog-analysis-tool-sign-test.c 12.2 KB
Newer Older
1 2 3 4 5
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * dialog-analysis-tool-sign-test.c:
 *
 * Authors:
6
  *  Andreas J. Guelzow  <aguelzow@pyrshep.ca>
7
 *
8
 * (C) Copyright 2009-2010 by Andreas J. Guelzow  <aguelzow@pyrshep.ca>
9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
22 23 24 25 26 27
 */

#include <gnumeric-config.h>
#include <glib/gi18n-lib.h>
#include <gnumeric.h>
#include "dialogs.h"
28 29 30
#include "tools/analysis-sign-test.h"
#include "tools/analysis-signed-rank-test.h"
#include "tools/analysis-tools.h"
31 32 33 34 35 36 37 38

#include <workbook.h>
#include <workbook-control.h>
#include <wbc-gtk.h>
#include <workbook-view.h>
#include <gui-util.h>
#include <parse-util.h>
#include <gnm-format.h>
39 40
#include <dialogs/tool-dialogs.h>
#include <dialogs/dao-gui-utils.h>
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <sheet.h>
#include <expr.h>
#include <number-match.h>
#include <ranges.h>
#include <selection.h>
#include <value.h>
#include <commands.h>
#include "help.h"

#include <widgets/gnm-dao.h>
#include <widgets/gnumeric-expr-entry.h>

#include <string.h>
#include <gtk/gtk.h>

56 57
#define SIGN_TEST_KEY_ONE      "analysistools-sign-test-one-dialog"
#define SIGN_TEST_KEY_TWO      "analysistools-sign-test-two-dialog"
58 59 60 61 62 63 64 65 66

static char const * const grouped_by_group[] = {
	"grouped_by_row",
	"grouped_by_col",
	"grouped_by_area",
	NULL
};

typedef struct {
67
	GnmGenericToolState base;
68 69 70 71 72
	GtkWidget *alpha_entry;
	GtkWidget *median_entry;
} SignTestToolState;

/**
73
 * sign_test_tool_update_common_sensitivity_cb:
74 75 76 77 78
 * @dummy:
 * @state:
 *
 * Update the dialog widgets sensitivity
 **/
79 80
static gboolean
sign_test_tool_update_common_sensitivity_cb (SignTestToolState *state)
81 82 83 84 85 86 87 88 89 90 91 92
{
	gnm_float alpha;
	gnm_float median;
	gboolean err;

	/* Checking Median*/
	err = entry_to_float
		(GTK_ENTRY (state->median_entry), &median, FALSE);
	if (err) {
		gtk_label_set_text (GTK_LABEL (state->base.warning),
				    _("The predicted median should be a number."));
		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
93
		return FALSE;
94 95 96 97 98 99 100 101 102 103
	}

	/* Checking Alpha*/
	alpha = gtk_spin_button_get_value
		(GTK_SPIN_BUTTON (state->alpha_entry));
	if (!(alpha > 0 && alpha < 1)) {
		gtk_label_set_text (GTK_LABEL (state->base.warning),
				    _("The alpha value should "
				      "be a number between 0 and 1."));
		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
104
		return FALSE;
105 106 107 108 109 110 111 112
	}

	/* Checking Output Page */
	if (!gnm_dao_is_ready (GNM_DAO (state->base.gdao))) {
		gtk_label_set_text (GTK_LABEL (state->base.warning),
				    _("The output specification "
				      "is invalid."));
		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
113
		return FALSE;
114 115
	}

116
	return TRUE;
117 118 119 120
}


/**
121
 * sign_test_two_tool_update_sensitivity_cb:
122
 * @dummy:
123 124
 * @state:
 *
125
 * Update the dialog widgets sensitivity
126 127
 **/
static void
128
sign_test_two_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
129
					  SignTestToolState *state)
130 131 132 133 134 135 136 137
{
        GnmValue *input_range;
 	gint w, h;

	/* Checking first input range*/
        input_range = gnm_expr_entry_parse_as_value
		(GNM_EXPR_ENTRY (state->base.input_entry),
		 state->base.sheet);
138
	if (input_range == NULL || !VALUE_IS_CELLRANGE (input_range)) {
139
		gtk_label_set_text (GTK_LABEL (state->base.warning),
Morten Welinder's avatar
Morten Welinder committed
140
				    (state->base.input_entry_2 == NULL)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
				    ? _("The input range is invalid.")
				    : _("The first input range is invalid."));
		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
		value_release (input_range);
		return;
	} else {
		GnmRange r;
		range_init_rangeref (&r, &(input_range->v_range.cell));
		w = range_width (&r);
		h = range_height (&r);
		value_release (input_range);
	}

	/* Checking second input range*/
	if (state->base.input_entry_2 != NULL) {
		input_range = gnm_expr_entry_parse_as_value
			(GNM_EXPR_ENTRY (state->base.input_entry_2),
			 state->base.sheet);
159
		if (input_range == NULL || !VALUE_IS_CELLRANGE (input_range)) {
160 161 162 163 164 165 166 167 168
			gtk_label_set_text (GTK_LABEL (state->base.warning),
					    _("The second input range is invalid."));
			gtk_widget_set_sensitive (state->base.ok_button, FALSE);
			value_release (input_range);
			return;
		} else {
			GnmRange r;
			range_init_rangeref (&r, &(input_range->v_range.cell));
			value_release (input_range);
Morten Welinder's avatar
Morten Welinder committed
169
			if (w != range_width (&r) ||
170
			    h != range_height (&r)) {
Morten Welinder's avatar
Morten Welinder committed
171
				gtk_label_set_text
172 173
					(GTK_LABEL (state->base.warning),
					 _("The input ranges do not have the same shape."));
Morten Welinder's avatar
Morten Welinder committed
174
				gtk_widget_set_sensitive
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
					(state->base.ok_button, FALSE);
			return;

			}
		}
	}

	if (sign_test_tool_update_common_sensitivity_cb (state)) {
		gtk_label_set_text (GTK_LABEL (state->base.warning), "");
		gtk_widget_set_sensitive (state->base.ok_button, TRUE);
	}
}

static void
sign_test_two_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
190 191 192 193
			      SignTestToolState *state)
{
	data_analysis_output_t  *dao;
	GtkWidget *w;
194 195
	analysis_tools_data_sign_test_two_t *data;
	analysis_tool_engine engine;
196

197
	data = g_new0 (analysis_tools_data_sign_test_two_t, 1);
198
	dao  = parse_output ((GnmGenericToolState *)state, NULL);
199

200 201 202 203 204 205
	data->base.range_1 = gnm_expr_entry_parse_as_value
		(GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);

	data->base.range_2 = gnm_expr_entry_parse_as_value
		(GNM_EXPR_ENTRY (state->base.input_entry_2), state->base.sheet);

Morten Welinder's avatar
Morten Welinder committed
206
	w = go_gtk_builder_get_widget (state->base.gui, "labels_button");
Morten Welinder's avatar
Morten Welinder committed
207
        data->base.labels = gtk_toggle_button_get_active
208
		(GTK_TOGGLE_BUTTON (w));
Morten Welinder's avatar
Morten Welinder committed
209

210
	entry_to_float
211
		(GTK_ENTRY (state->median_entry), &data->median, FALSE);
212 213

	data->base.alpha = gtk_spin_button_get_value
214 215
		(GTK_SPIN_BUTTON (state->alpha_entry));

Morten Welinder's avatar
Morten Welinder committed
216
	w =  go_gtk_builder_get_widget (state->base.gui, "signtest");
217
	engine =  gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w))
Morten Welinder's avatar
Morten Welinder committed
218
		? analysis_tool_sign_test_two_engine
219 220
		: analysis_tool_signed_rank_test_two_engine;

Morten Welinder's avatar
Morten Welinder committed
221
	if (!cmd_analysis_tool (GNM_WBC (state->base.wbcg),
222
				state->base.sheet,
223
				dao, data, engine, TRUE))
224 225 226 227 228
		gtk_widget_destroy (state->base.dialog);

	return;
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/**
 * dialog_sign_test_two_tool:
 *
 **/
int
dialog_sign_test_two_tool (WBCGtk *wbcg, Sheet *sheet, signtest_type type)
{
	char const * plugins[] = { "Gnumeric_fnstat",
				   "Gnumeric_fnlogical",
				   "Gnumeric_fnmath",
				   "Gnumeric_fninfo",
				   NULL};
        SignTestToolState *state;
	GtkWidget *w;

	if ((wbcg == NULL) ||
	    gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
		return 1;

	/* Only pop up one copy per workbook */
Morten Welinder's avatar
Morten Welinder committed
249
	if (gnm_dialog_raise_if_exists (wbcg, SIGN_TEST_KEY_TWO))
250 251 252 253 254
		return 0;

	state = g_new0 (SignTestToolState, 1);

	if (dialog_tool_init (&state->base, wbcg, sheet,
255
			      GNUMERIC_HELP_LINK_SIGN_TEST_TWO,
256
			      "res:ui/sign-test-two.ui", "Sign-Test",
257
			      _("Could not create the Sign Test Tool dialog."),
Morten Welinder's avatar
Morten Welinder committed
258 259 260 261
			      SIGN_TEST_KEY_TWO,
			      G_CALLBACK (sign_test_two_tool_ok_clicked_cb),
			      NULL,
			      G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
262
			      GNM_EE_SINGLE_RANGE))
263 264
	{
		g_free(state);
265
		return 0;
266
	}
267

Morten Welinder's avatar
Morten Welinder committed
268

Morten Welinder's avatar
Morten Welinder committed
269
	state->alpha_entry = tool_setup_update
270
		(&state->base, "alpha-entry",
Morten Welinder's avatar
Morten Welinder committed
271
		 G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
272
		 state);
273
	float_to_entry (GTK_ENTRY (state->alpha_entry), 0.05);
274

Morten Welinder's avatar
Morten Welinder committed
275
	state->median_entry = tool_setup_update
276 277
		(&state->base, "median-entry",
		 G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
Morten Welinder's avatar
Morten Welinder committed
278
		 state);
279
	int_to_entry (GTK_ENTRY (state->median_entry), 0);
280

Morten Welinder's avatar
Morten Welinder committed
281
	w =  go_gtk_builder_get_widget (state->base.gui,
Morten Welinder's avatar
Morten Welinder committed
282
				   (type == SIGNTEST) ? "signtest"
283
				   : "signedranktest");
284 285 286 287
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);

	gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
	sign_test_two_tool_update_sensitivity_cb (NULL, state);
288
	tool_load_selection ((GnmGenericToolState *)state, TRUE);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

	return 0;

}

/************************************************************************************/

/**
 * sign_test_tool_ok_clicked_cb:
 * @button:
 * @state:
 *
 * Retrieve the information from the dialog and call the sign_test_tool.
 * Note that we assume that the ok_button is only active if the entry fields
 * contain sensible data.
 **/
305
static void
306
sign_test_tool_ok_clicked_cb (G_GNUC_UNUSED GtkWidget *button,
307 308 309 310
			      SignTestToolState *state)
{
	data_analysis_output_t  *dao;
	GtkWidget *w;
311 312
	analysis_tools_data_sign_test_t *data;
	analysis_tool_engine engine;
313

314
	data = g_new0 (analysis_tools_data_sign_test_t, 1);
315
	dao  = parse_output ((GnmGenericToolState *)state, NULL);
316

317 318
	data->base.input = gnm_expr_entry_parse_as_list (
		GNM_EXPR_ENTRY (state->base.input_entry), state->base.sheet);
319
	data->base.group_by = gnm_gui_group_value (state->base.gui, grouped_by_group);
Morten Welinder's avatar
Morten Welinder committed
320

Morten Welinder's avatar
Morten Welinder committed
321
	w = go_gtk_builder_get_widget (state->base.gui, "labels_button");
Morten Welinder's avatar
Morten Welinder committed
322
        data->base.labels = gtk_toggle_button_get_active
323
		(GTK_TOGGLE_BUTTON (w));
Morten Welinder's avatar
Morten Welinder committed
324

325
	entry_to_float
326
		(GTK_ENTRY (state->median_entry), &data->median, FALSE);
327
	data->alpha = gtk_spin_button_get_value
328 329
		(GTK_SPIN_BUTTON (state->alpha_entry));

Morten Welinder's avatar
Morten Welinder committed
330
	w =  go_gtk_builder_get_widget (state->base.gui, "signtest");
331
	engine =  gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w))
Morten Welinder's avatar
Morten Welinder committed
332
		? analysis_tool_sign_test_engine
333 334
		: analysis_tool_signed_rank_test_engine;

Morten Welinder's avatar
Morten Welinder committed
335
	if (!cmd_analysis_tool (GNM_WBC (state->base.wbcg),
336 337
				state->base.sheet,
				dao, data, engine, TRUE))
338 339 340 341 342
		gtk_widget_destroy (state->base.dialog);

	return;
}

343
/**
344 345 346
 * sign_test_tool_update_sensitivity_cb:
 * @dummy:
 * @state:
347
 *
348 349 350 351 352 353 354 355 356 357 358 359 360 361
 * Update the dialog widgets sensitivity
 **/
static void
sign_test_tool_update_sensitivity_cb (G_GNUC_UNUSED GtkWidget *dummy,
				      SignTestToolState *state)
{
        GSList *input_range;

	/* Checking first input range*/
        input_range = gnm_expr_entry_parse_as_list
		(GNM_EXPR_ENTRY (state->base.input_entry),
		 state->base.sheet);
	if (input_range == NULL) {
		gtk_label_set_text (GTK_LABEL (state->base.warning),
Morten Welinder's avatar
Morten Welinder committed
362
				    (state->base.input_entry_2 == NULL)
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
				    ? _("The input range is invalid.")
				    : _("The first input range is invalid."));
		gtk_widget_set_sensitive (state->base.ok_button, FALSE);
		return;
	} else
		range_list_destroy (input_range);

	if (sign_test_tool_update_common_sensitivity_cb (state)) {
		gtk_label_set_text (GTK_LABEL (state->base.warning), "");
		gtk_widget_set_sensitive (state->base.ok_button, TRUE);
	}

}

/**
 * dialog_sign_test_tool:
379 380 381
 *
 **/
int
382
dialog_sign_test_tool (WBCGtk *wbcg, Sheet *sheet, signtest_type type)
383 384 385 386
{
	char const * plugins[] = { "Gnumeric_fnstat",
				   "Gnumeric_fnlogical",
				   "Gnumeric_fnmath",
387
				   "Gnumeric_fninfo",
388
				   NULL};
389
        SignTestToolState *state;
390
	GtkWidget *w;
391 392 393 394 395 396

	if ((wbcg == NULL) ||
	    gnm_check_for_plugins_missing (plugins, wbcg_toplevel (wbcg)))
		return 1;

	/* Only pop up one copy per workbook */
Morten Welinder's avatar
Morten Welinder committed
397
	if (gnm_dialog_raise_if_exists (wbcg, SIGN_TEST_KEY_ONE))
398 399 400 401 402 403
		return 0;

	state = g_new0 (SignTestToolState, 1);

	if (dialog_tool_init (&state->base, wbcg, sheet,
			      GNUMERIC_HELP_LINK_SIGN_TEST,
404
			      "res:ui/sign-test.ui", "Sign-Test",
405
			      _("Could not create the Sign Test Tool dialog."),
Morten Welinder's avatar
Morten Welinder committed
406 407 408 409
			      SIGN_TEST_KEY_ONE,
			      G_CALLBACK (sign_test_tool_ok_clicked_cb),
			      NULL,
			      G_CALLBACK (sign_test_tool_update_sensitivity_cb),
410
			      0))
411 412
	{
		g_free(state);
413
		return 0;
414
	}
415

Morten Welinder's avatar
Morten Welinder committed
416

Morten Welinder's avatar
Morten Welinder committed
417
	state->alpha_entry = tool_setup_update
418
		(&state->base, "alpha-entry",
Morten Welinder's avatar
Morten Welinder committed
419
		 G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
420
		 state);
Morten Welinder's avatar
Morten Welinder committed
421
	state->median_entry = tool_setup_update
422 423
		(&state->base, "median-entry",
		 G_CALLBACK (sign_test_two_tool_update_sensitivity_cb),
Morten Welinder's avatar
Morten Welinder committed
424
		 state);
425

426
	int_to_entry (GTK_ENTRY (state->median_entry), 0);
427 428
	float_to_entry (GTK_ENTRY (state->alpha_entry), 0.05);

Morten Welinder's avatar
Morten Welinder committed
429
	w =  go_gtk_builder_get_widget (state->base.gui,
Morten Welinder's avatar
Morten Welinder committed
430
				   (type == SIGNTEST) ? "signtest"
431
				   : "signedranktest");
432
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
433 434 435

	gnm_dao_set_put (GNM_DAO (state->base.gdao), TRUE, TRUE);
	sign_test_tool_update_sensitivity_cb (NULL, state);
436
	tool_load_selection ((GnmGenericToolState *)state, TRUE);
437

438
	return 0;
439

440
}