File: impute_ui.c

package info (click to toggle)
ggobi 2.1.9~20091212-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,340 kB
  • ctags: 5,083
  • sloc: ansic: 57,242; xml: 30,604; cpp: 833; makefile: 355; java: 225; perl: 201; sh: 122; python: 23
file content (438 lines) | stat: -rw-r--r-- 16,035 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
                                    /* impute_ui.c *//*-- should be called missing_ui.c --*/
/*
 * ggobi
 * Copyright (C) AT&T, Duncan Temple Lang, Dianne Cook 1999-2005
 *
 * ggobi is free software; you may use, redistribute, and/or modify it
 * under the terms of the Common Public License, which is distributed
 * with the source code and displayed on the ggobi web site, 
 * www.ggobi.org.  For more information, contact the authors:
 *
 *   Deborah F. Swayne   dfs@research.att.com
 *   Di Cook             dicook@iastate.edu
 *   Duncan Temple Lang  duncan@wald.ucdavis.edu
 *   Andreas Buja        andreas.buja@wharton.upenn.edu
*/

#include <stdlib.h>
#include <string.h>

#include <gtk/gtk.h>
#include "vars.h"
#include "externs.h"

/*-- called when closed from the close button --*/
static void
close_btn_cb (GtkWidget * w, ggobid * gg)
{
  gtk_widget_hide (gg->impute.window);
}

/*-- called when closed from the window manager --*/
static void
close_wmgr_cb (GtkWidget * w, GdkEvent * event, ggobid * gg)
{
  gtk_widget_hide (gg->impute.window);
}

/* Random */
static void
set_random_cb (GtkToggleButton * w, ggobid * gg)
{
  gg->impute.type = IMP_RANDOM;
}

/* Fixed */
static void
set_fixed_cb (GtkToggleButton * w, ggobid * gg)
{
  gg->impute.type = IMP_FIXED;
}

/* Below */
static void
set_fixed_below_cb (GtkToggleButton * w, ggobid * gg)
{
  gg->impute.type = IMP_BELOW;
}

/* Above */
static void
set_fixed_above_cb (GtkToggleButton * w, ggobid * gg)
{
  gg->impute.type = IMP_ABOVE;
}

/* Mean */
static void
set_mean_cb (GtkToggleButton * w, ggobid * gg)
{
  gg->impute.type = IMP_MEAN;
}

/* Median */
static void
set_median_cb (GtkToggleButton * w, ggobid * gg)
{
  gg->impute.type = IMP_MEDIAN;
}

static void
rescale_cb (GtkButton * button, ggobid * gg)
{
  GtkWidget *tv = get_tree_view_from_object (G_OBJECT (gg->impute.window));
  GGobiData *d = (GGobiData *) g_object_get_data (G_OBJECT (tv), "datad");

  limits_set (d, true, true, gg->lims_use_visible);
  vartable_limits_set (d);
  vartable_stats_set (d);

  tform_to_world (d, gg);
  displays_tailpipe (FULL, gg);
}
static void
group_cb (GtkToggleButton * button, ggobid * gg)
{
  gg->impute.bgroup_p = button->active;
}
static void
show_missings_cb (GtkToggleButton * button, ggobid * gg)
{
  GtkWidget *tv = get_tree_view_from_object (G_OBJECT (gg->impute.window));
  GGobiData *d = (GGobiData *) g_object_get_data (G_OBJECT (tv), "datad");

  d->missings_show_p = button->active;
  displays_tailpipe (FULL, gg);
}

static gboolean
impute_fixed_cb (ImputeType impute_type, gfloat * val, ggobid * gg)
{
  GtkWidget *w;
  gchar *val_str;
  gboolean ok = true;

  if (impute_type == IMP_ABOVE || impute_type == IMP_BELOW) {

    if (impute_type == IMP_ABOVE) {
      w = widget_find_by_name (gg->impute.window, "IMPUTE:entry_above");
      val_str = gtk_editable_get_chars (GTK_EDITABLE (w), 0, -1);
    }
    else { // if (impute_type == IMP_BELOW) {
      w = widget_find_by_name (gg->impute.window, "IMPUTE:entry_below");
      val_str = gtk_editable_get_chars (GTK_EDITABLE (w), 0, -1);
    }

    if (strlen (val_str) == 0) {
      gchar *message =
        g_strdup_printf
        ("You selected '%% over or under' but didn't specify a percentage.\n");
      quick_message (message, false);
      g_free (message);
      ok = false;
      return ok;
    }

    *val = (gfloat) atof (val_str);
    g_free (val_str);
    if (*val < 0 || *val > 100) {
      gchar *message =
        g_strdup_printf
        ("You specified %f%%; please specify a percentage between 0 and 100.\n",
         *val);
      quick_message (message, false);
      g_free (message);
      ok = false;
      return ok;
    }
  }
  else if (impute_type == IMP_FIXED) {
    w = widget_find_by_name (gg->impute.window, "IMPUTE:entry_val");
    val_str = gtk_editable_get_chars (GTK_EDITABLE (w), 0, -1);
    if (strlen (val_str) == 0) {
      quick_message
        ("You've selected 'Specify' but haven't specified a value.\n", false);
      ok = false;
      return ok;
    }
    else {
      *val = (gfloat) atof (val_str);
      g_free (val_str);
    }
  }

  return ok;
}

static void
impute_cb (GtkWidget * w, ggobid * gg)
{
  gboolean redraw = true;
  GtkWidget *tree_view =
    get_tree_view_from_object (G_OBJECT (gg->impute.window));
  GGobiData *d =
    (GGobiData *) g_object_get_data (G_OBJECT (tree_view), "datad");
  gint *vars; // = (gint *) g_malloc (d->ncols * sizeof(gint));
  gint nvars;
  gfloat val = 0.0;  // compiler pacification

  vars = get_selections_from_tree_view (tree_view, &nvars);

  switch (gg->impute.type) {
  case IMP_RANDOM:
    impute_random (d, nvars, vars, gg);
    break;
  case IMP_FIXED:
  case IMP_BELOW:
  case IMP_ABOVE:
    if (impute_fixed_cb (gg->impute.type, &val, gg))
      redraw = impute_fixed (gg->impute.type, val, nvars, vars, d, gg);
    break;
  case IMP_MEAN:
  case IMP_MEDIAN:
    redraw = impute_mean_or_median (gg->impute.type, nvars, vars, d, gg);
    break;
  }

  if (redraw) {
    tform_to_world (d, gg);
    displays_tailpipe (FULL, gg);
  }

  g_free (vars);
}

/*------------------------------------------------------------------*/

void
impute_window_open (ggobid * gg)
{
  GtkWidget *frame0, *vb;
  GtkWidget *btn, *tgl, *notebook;
  GtkWidget *vbox, *hb;
  GtkWidget *table, *entry, *radio;
  gint row = 0;

  /*-- if used before we have data, bail out --*/
  if (gg->d == NULL || g_slist_length (gg->d) == 0)
    /**/ return;

  if (gg->impute.window == NULL) {

    gg->impute.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (gg->impute.window), "Missing Values");
    g_signal_connect (G_OBJECT (gg->impute.window),
                      "delete_event", G_CALLBACK (close_wmgr_cb), gg);

    gtk_container_set_border_width (GTK_CONTAINER (gg->impute.window), 5);

    vbox = gtk_vbox_new (false, 2);
    gtk_container_add (GTK_CONTAINER (gg->impute.window), vbox);

    /*-- Add a toggle button, show missings or not --*/
    tgl = gtk_check_button_new_with_mnemonic ("Sh_ow missing values");
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (tgl), on);
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), tgl,
                          "Draw the missing values when plotting displays; if there are multiple datasets, this applies only to the current dataset",
                          NULL);
    g_signal_connect (G_OBJECT (tgl), "toggled",
                      G_CALLBACK (show_missings_cb), (gpointer) gg);
    gtk_box_pack_start (GTK_BOX (vbox), tgl, false, false, 2);


    /*-- add a button to generate a new datad --*/
    btn = gtk_button_new_with_mnemonic ("_Add missings as new dataset");
    g_signal_connect (G_OBJECT (btn),
                      "clicked", G_CALLBACK (missings_datad_cb),
                      (gpointer) gg);
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), btn,
                          "Generate a new dataset from the 1's and 0's representing missingness",
                          NULL);
    gtk_box_pack_start (GTK_BOX (vbox), btn, false, false, 2);

    /*-- add a frame to contain the "imputation" widgets --*/
    frame0 = gtk_frame_new ("Assign or impute values");
    gtk_container_set_border_width (GTK_CONTAINER (frame0), 2);
    gtk_box_pack_start (GTK_BOX (vbox), frame0, true, true, 2);

    vb = gtk_vbox_new (false, 2);
    /*-- this has the effect of setting an internal border inside the frame --*/
    gtk_container_set_border_width (GTK_CONTAINER (vb), 5);
    gtk_container_add (GTK_CONTAINER (frame0), vb);

    /* Create a notebook, set the position of the tabs */
    notebook = create_variable_notebook (vb,
                                         GTK_SELECTION_MULTIPLE, all_vartypes,
                                         all_datatypes, G_CALLBACK (NULL),
                                         NULL, gg);
    row = 0;
    table = gtk_table_new (6, 2, false);
    gtk_box_pack_start (GTK_BOX (vb), table, false, false, 2);

    /* Random */
    radio = gtk_radio_button_new_with_mnemonic (NULL, "_Random");
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), radio,
                          "Assign to each missing value one of the existing variable values chosen at random",
                          NULL);
    g_signal_connect (G_OBJECT (radio), "toggled",
                      G_CALLBACK (set_random_cb), (gpointer) gg);
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radio), true);
    gtk_table_attach (GTK_TABLE (table), radio, 0, 1, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    tgl =
      gtk_check_button_new_with_mnemonic ("Condition on symbol and _color");
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), tgl,
                          "Condition the random imputation on the symbol and color; these groups can be seen in the case clusters window",
                          NULL);
    g_signal_connect (G_OBJECT (tgl), "toggled", G_CALLBACK (group_cb),
                      (gpointer) gg);
    gtk_table_attach (GTK_TABLE (table), tgl, 1, 2, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    row++;



    /* Mean */
    radio =
      gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group
                                          (GTK_RADIO_BUTTON (radio)),
                                          "Variable _mean");
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), radio,
                          "Assign the variable mean to each missing value",
                          NULL);
    g_signal_connect (G_OBJECT (radio), "toggled", G_CALLBACK (set_mean_cb),
                      (gpointer) gg);
    gtk_table_attach (GTK_TABLE (table), radio, 0, 1, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    row++;

    /* Median */
    radio =
      gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group
                                          (GTK_RADIO_BUTTON (radio)),
                                          "Variable m_edian");
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), radio,
                          "Assign the variable median to each missing value",
                          NULL);
    g_signal_connect (G_OBJECT (radio), "toggled", G_CALLBACK (set_median_cb),
                      (gpointer) gg);
    gtk_table_attach (GTK_TABLE (table), radio, 0, 1, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    row++;


    /* Fixed */
    radio =
      gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group
                                          (GTK_RADIO_BUTTON (radio)),
                                          "_Fixed");
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), radio,
                          "Assign a fixed value to each missing variable value",
                          NULL);
    g_signal_connect (G_OBJECT (radio), "toggled", G_CALLBACK (set_fixed_cb),
                      (gpointer) gg);
    gtk_table_attach (GTK_TABLE (table), radio, 0, 1, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    entry = gtk_entry_new ();
    gtk_widget_set_name (entry, "IMPUTE:entry_val");
    gtk_entry_set_text (GTK_ENTRY (entry), "0");
    gtk_table_attach (GTK_TABLE (table), entry, 1, 2, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    row++;

    /* Pctage below min */
    radio =
      gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group
                                          (GTK_RADIO_BUTTON (radio)),
                                          "Percent _below min");
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), radio,
                          "Assign a fixed value which is some percentage below the minimum value for the variable",
                          NULL);
    g_signal_connect (G_OBJECT (radio), "toggled",
                      G_CALLBACK (set_fixed_below_cb), (gpointer) gg);
    gtk_table_attach (GTK_TABLE (table), radio, 0, 1, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    entry = gtk_entry_new ();
    gtk_entry_set_text (GTK_ENTRY (entry), "10");
    gtk_widget_set_name (entry, "IMPUTE:entry_below");
    gtk_table_attach (GTK_TABLE (table), entry, 1, 2, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    row++;

    /* Pctage above min */
    radio =
      gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group
                                          (GTK_RADIO_BUTTON (radio)),
                                          "Percent ab_ove min");
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), radio,
                          "Assign a fixed value which is some percentage above the minimum value for the variable",
                          NULL);
    g_signal_connect (G_OBJECT (radio), "toggled",
                      G_CALLBACK (set_fixed_above_cb), (gpointer) gg);
    gtk_table_attach (GTK_TABLE (table), radio, 0, 1, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    entry = gtk_entry_new ();
    gtk_entry_set_text (GTK_ENTRY (entry), "10");
    gtk_widget_set_name (entry, "IMPUTE:entry_above");
    gtk_table_attach (GTK_TABLE (table), entry, 1, 2, row, row + 1,
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      (GtkAttachOptions) (GTK_SHRINK | GTK_FILL | GTK_EXPAND),
                      1, 1);
    row++;

   /*-- hbox to hold a few buttons --*/
    hb = gtk_hbox_new (true, 2);

    gtk_box_pack_start (GTK_BOX (vb), hb, false, false, 2);

    btn = gtk_button_new_with_mnemonic ("_Impute");
    g_signal_connect (G_OBJECT (btn),
                      "clicked", G_CALLBACK (impute_cb), (gpointer) gg);
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), btn,
                          "Impute or assign values to missings", NULL);
    gtk_box_pack_start (GTK_BOX (hb), btn, true, true, 2);

    btn = gtk_button_new_with_mnemonic ("Re_scale");
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), btn,
                          "Rescale the data after imputing", NULL);
    g_signal_connect (G_OBJECT (btn), "clicked",
                      G_CALLBACK (rescale_cb), (gpointer) gg);
    gtk_box_pack_start (GTK_BOX (hb), btn, true, true, 2);



    /*-- add a close button --*/
    hb = gtk_hbox_new (false, 2);
    gtk_box_pack_start (GTK_BOX (vbox), hb, false, false, 1);

    btn = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
    gtk_tooltips_set_tip (GTK_TOOLTIPS (gg->tips), btn,
                          "Close the window", NULL);
    gtk_box_pack_start (GTK_BOX (hb), btn, true, false, 2);
    g_signal_connect (G_OBJECT (btn), "clicked",
                      G_CALLBACK (close_btn_cb), gg);

    g_object_set_data (G_OBJECT (gg->impute.window), "notebook", notebook);
  }

  gtk_widget_show_all (gg->impute.window);
  gdk_window_raise (gg->impute.window->window);
}