callbacks.cpp
Go to the documentation of this file.
1 #include <gtk/gtk.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <list>
7 #include "callbacks.h"
8 #include "interface.h"
9 #include "../../svm.h"
10 using namespace std;
11 
12 #define DEFAULT_PARAM "-t 2 -c 100"
13 #define XLEN 500
14 #define YLEN 500
15 
16 GdkColor colors[] =
17 {
18  {0, 0, 0, 0},
19  {0, 0, 120 << 8, 120 << 8},
20  {0, 120 << 8, 120 << 8, 0},
21  {0, 120 << 8, 0, 120 << 8},
22  {0, 0, 200 << 8, 200 << 8},
23  {0, 200 << 8, 200 << 8, 0},
24  {0, 200 << 8, 0, 200 << 8},
25 };
26 
27 GdkGC *gc;
28 GdkPixmap *pixmap;
29 extern "C" GtkWidget *draw_main;
30 GtkWidget *draw_main;
31 extern "C" GtkWidget *entry_option;
32 GtkWidget *entry_option;
33 
34 typedef struct
35 {
36  double x, y;
37  signed char value;
38 } point;
39 
40 list<point> point_list;
41 int current_value = 1;
42 
43 extern "C" void svm_toy_initialize()
44 {
45  gboolean success[7];
46 
47  gdk_colormap_alloc_colors(
48  gdk_colormap_get_system(),
49  colors,
50  7,
51  FALSE,
52  TRUE,
53  success);
54 
55  gc = gdk_gc_new(draw_main->window);
56  pixmap = gdk_pixmap_new(draw_main->window, XLEN, YLEN, -1);
57  gdk_gc_set_foreground(gc, &colors[0]);
58  gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, XLEN, YLEN);
59  gtk_entry_set_text(GTK_ENTRY(entry_option), DEFAULT_PARAM);
60 }
61 
62 void redraw_area(GtkWidget* widget, int x, int y, int w, int h)
63 {
64  gdk_draw_pixmap(widget->window,
65  gc,
66  pixmap,
67  x, y, x, y, w, h);
68 }
69 
70 void draw_point(const point& p)
71 {
72  gdk_gc_set_foreground(gc, &colors[p.value + 3]);
73  gdk_draw_rectangle(pixmap, gc, TRUE, int(p.x * XLEN), int(p.y * YLEN), 4, 4);
74  gdk_draw_rectangle(draw_main->window, gc, TRUE, int(p.x * XLEN), int(p.y * YLEN), 4, 4);
75 }
76 
78 {
79  for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
80  draw_point(*p);
81 }
82 
83 void clear_all()
84 {
85  point_list.clear();
86  gdk_gc_set_foreground(gc, &colors[0]);
87  gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, XLEN, YLEN);
88  redraw_area(draw_main, 0, 0, XLEN, YLEN);
89 }
90 
91 void
92 on_button_change_clicked(GtkButton *button,
93  gpointer user_data)
94 {
95  ++current_value;
96  if (current_value > 3) current_value = 1;
97 }
98 
99 void
100 on_button_run_clicked(GtkButton *button,
101  gpointer user_data)
102 {
103  // guard
104  if (point_list.empty()) return;
105 
107  int i, j;
108 
109  // default values
110  param.svm_type = C_SVC;
111  param.kernel_type = RBF;
112  param.degree = 3;
113  param.gamma = 0;
114  param.coef0 = 0;
115  param.nu = 0.5;
116  param.cache_size = 100;
117  param.C = 1;
118  param.eps = 1e-3;
119  param.p = 0.1;
120  param.shrinking = 1;
121  param.probability = 0;
122  param.nr_weight = 0;
123  param.weight_label = NULL;
124  param.weight = NULL;
125 
126  // parse options
127  const char *p = gtk_entry_get_text(GTK_ENTRY(entry_option));
128 
129  while (1)
130  {
131  while (*p && *p != '-')
132  p++;
133 
134  if (*p == '\0')
135  break;
136 
137  p++;
138  switch (*p++)
139  {
140  case 's':
141  param.svm_type = atoi(p);
142  break;
143  case 't':
144  param.kernel_type = atoi(p);
145  break;
146  case 'd':
147  param.degree = atoi(p);
148  break;
149  case 'g':
150  param.gamma = atof(p);
151  break;
152  case 'r':
153  param.coef0 = atof(p);
154  break;
155  case 'n':
156  param.nu = atof(p);
157  break;
158  case 'm':
159  param.cache_size = atof(p);
160  break;
161  case 'c':
162  param.C = atof(p);
163  break;
164  case 'e':
165  param.eps = atof(p);
166  break;
167  case 'p':
168  param.p = atof(p);
169  break;
170  case 'h':
171  param.shrinking = atoi(p);
172  break;
173  case 'b':
174  param.probability = atoi(p);
175  break;
176  case 'w':
177  ++param.nr_weight;
178  param.weight_label = (int *)realloc(param.weight_label, sizeof(int) * param.nr_weight);
179  param.weight = (double *)realloc(param.weight, sizeof(double) * param.nr_weight);
180  param.weight_label[param.nr_weight - 1] = atoi(p);
181  while (*p && !isspace(*p)) ++p;
182  param.weight[param.nr_weight - 1] = atof(p);
183  break;
184  }
185  }
186 
187  // build problem
189 
190  prob.l = point_list.size();
191  prob.y = new double[prob.l];
192 
193  if (param.kernel_type == PRECOMPUTED)
194  {
195  }
196  else if (param.svm_type == EPSILON_SVR ||
197  param.svm_type == NU_SVR)
198  {
199  if (param.gamma == 0) param.gamma = 1;
200  svm_node *x_space = new svm_node[2 * prob.l];
201  prob.x = new svm_node *[prob.l];
202 
203  i = 0;
204  for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
205  {
206  x_space[2 * i].index = 1;
207  x_space[2 * i].value = q->x;
208  x_space[2 * i + 1].index = -1;
209  prob.x[i] = &x_space[2 * i];
210  prob.y[i] = q->y;
211  }
212 
213  // build model & classify
214  svm_model *model = svm_train(&prob, &param);
215  svm_node x[2];
216  x[0].index = 1;
217  x[1].index = -1;
218  int *j = new int[XLEN];
219 
220  for (i = 0; i < XLEN; i++)
221  {
222  x[0].value = (double) i / XLEN;
223  j[i] = (int)(YLEN * svm_predict(model, x));
224  }
225 
226  gdk_gc_set_foreground(gc, &colors[0]);
227  gdk_draw_line(pixmap, gc, 0, 0, 0, YLEN - 1);
228  gdk_draw_line(draw_main->window, gc, 0, 0, 0, YLEN - 1);
229 
230  int p = (int)(param.p * YLEN);
231  for (i = 1; i < XLEN; i++)
232  {
233  gdk_gc_set_foreground(gc, &colors[0]);
234  gdk_draw_line(pixmap, gc, i, 0, i, YLEN - 1);
235  gdk_draw_line(draw_main->window, gc, i, 0, i, YLEN - 1);
236 
237  gdk_gc_set_foreground(gc, &colors[5]);
238  gdk_draw_line(pixmap, gc, i - 1, j[i - 1], i, j[i]);
239  gdk_draw_line(draw_main->window, gc, i - 1, j[i - 1], i, j[i]);
240 
241  if (param.svm_type == EPSILON_SVR)
242  {
243  gdk_gc_set_foreground(gc, &colors[2]);
244  gdk_draw_line(pixmap, gc, i - 1, j[i - 1] + p, i, j[i] + p);
245  gdk_draw_line(draw_main->window, gc, i - 1, j[i - 1] + p, i, j[i] + p);
246 
247  gdk_gc_set_foreground(gc, &colors[2]);
248  gdk_draw_line(pixmap, gc, i - 1, j[i - 1] - p, i, j[i] - p);
249  gdk_draw_line(draw_main->window, gc, i - 1, j[i - 1] - p, i, j[i] - p);
250  }
251  }
252 
254  delete[] j;
255  delete[] x_space;
256  delete[] prob.x;
257  delete[] prob.y;
258  }
259  else
260  {
261  if (param.gamma == 0) param.gamma = 0.5;
262  svm_node *x_space = new svm_node[3 * prob.l];
263  prob.x = new svm_node *[prob.l];
264 
265  i = 0;
266  for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
267  {
268  x_space[3 * i].index = 1;
269  x_space[3 * i].value = q->x;
270  x_space[3 * i + 1].index = 2;
271  x_space[3 * i + 1].value = q->y;
272  x_space[3 * i + 2].index = -1;
273  prob.x[i] = &x_space[3 * i];
274  prob.y[i] = q->value;
275  }
276 
277  // build model & classify
278  svm_model *model = svm_train(&prob, &param);
279  svm_node x[3];
280  x[0].index = 1;
281  x[1].index = 2;
282  x[2].index = -1;
283 
284  for (i = 0; i < XLEN; i++)
285  for (j = 0; j < YLEN; j++)
286  {
287  x[0].value = (double) i / XLEN;
288  x[1].value = (double) j / YLEN;
289  double d = svm_predict(model, x);
290  if (param.svm_type == ONE_CLASS && d < 0) d = 2;
291  gdk_gc_set_foreground(gc, &colors[(int)d]);
292  gdk_draw_point(pixmap, gc, i, j);
293  gdk_draw_point(draw_main->window, gc, i, j);
294  }
295 
297  delete[] x_space;
298  delete[] prob.x;
299  delete[] prob.y;
300  }
301  free(param.weight_label);
302  free(param.weight);
303  draw_all_points();
304 }
305 
306 void
307 on_button_clear_clicked(GtkButton *button,
308  gpointer user_data)
309 {
310  clear_all();
311 }
312 
313 void
314 on_window1_destroy(GtkObject *object,
315  gpointer user_data)
316 {
317  gtk_exit(0);
318 }
319 
320 gboolean
322  GdkEventButton *event,
323  gpointer user_data)
324 {
325  point p = {(double)event->x / XLEN, (double)event->y / YLEN, current_value};
326  point_list.push_back(p);
327  draw_point(p);
328  return FALSE;
329 }
330 
331 gboolean
332 on_draw_main_expose_event(GtkWidget *widget,
333  GdkEventExpose *event,
334  gpointer user_data)
335 {
336  redraw_area(widget,
337  event->area.x, event->area.y,
338  event->area.width, event->area.height);
339  return FALSE;
340 }
341 
342 GtkWidget *fileselection;
343 static enum { SAVE, LOAD } fileselection_flag;
344 
346 {
348  gtk_signal_connect_object(
349  GTK_OBJECT(GTK_FILE_SELECTION(fileselection)->ok_button),
350  "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
351  (GtkObject *) fileselection);
352 
353  gtk_signal_connect_object(GTK_OBJECT
354  (GTK_FILE_SELECTION(fileselection)->cancel_button),
355  "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
356  (GtkObject *) fileselection);
357 
358  gtk_widget_show(fileselection);
359 }
360 
361 void
362 on_button_save_clicked(GtkButton *button,
363  gpointer user_data)
364 {
367 }
368 
369 
370 void
371 on_button_load_clicked(GtkButton *button,
372  gpointer user_data)
373 {
376 }
377 
378 void
379 on_filesel_ok_clicked(GtkButton *button,
380  gpointer user_data)
381 {
382  gtk_widget_hide(fileselection);
383  const char *filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileselection));
384 
385  if (fileselection_flag == SAVE)
386  {
387  FILE *fp = fopen(filename, "w");
388 
389  const char *p = gtk_entry_get_text(GTK_ENTRY(entry_option));
390  const char* svm_type_str = strstr(p, "-s ");
391  int svm_type = C_SVC;
392  if (svm_type_str != NULL)
393  sscanf(svm_type_str, "-s %d", &svm_type);
394 
395  if (fp)
396  {
397  if (svm_type == EPSILON_SVR || svm_type == NU_SVR)
398  {
399  for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
400  fprintf(fp, "%f 1:%f\n", p->y, p->x);
401  }
402  else
403  {
404  for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
405  fprintf(fp, "%d 1:%f 2:%f\n", p->value, p->x, p->y);
406  }
407  fclose(fp);
408  }
409 
410  }
411  else if (fileselection_flag == LOAD)
412  {
413  FILE *fp = fopen(filename, "r");
414  if (fp)
415  {
416  clear_all();
417  char buf[4096];
418  while (fgets(buf, sizeof(buf), fp))
419  {
420  int v;
421  double x, y;
422  if (sscanf(buf, "%d%*d:%lf%*d:%lf", &v, &x, &y) == 3)
423  {
424  point p = {x, y, v};
425  point_list.push_back(p);
426  }
427  else if (sscanf(buf, "%lf%*d:%lf", &y, &x) == 2)
428  {
429  point p = {x, y, current_value};
430  point_list.push_back(p);
431  }
432  else
433  break;
434  }
435  fclose(fp);
436  draw_all_points();
437  }
438  }
439 }
440 
441 void
442 on_fileselection_destroy(GtkObject *object,
443  gpointer user_data)
444 {
445 }
446 
447 void
448 on_filesel_cancel_clicked(GtkButton *button,
449  gpointer user_data)
450 {
451 }
d
GtkWidget * fileselection
Definition: callbacks.cpp:342
void svm_toy_initialize()
Definition: callbacks.cpp:43
void show_fileselection()
Definition: callbacks.cpp:345
#define DEFAULT_PARAM
Definition: callbacks.cpp:12
struct svm_problem prob
Definition: svmtrain.c:60
struct svm_parameter param
Definition: svmtrain.c:59
gboolean on_draw_main_button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
Definition: callbacks.cpp:321
GdkGC * gc
Definition: callbacks.cpp:27
def svm_train(arg1, arg2=None, arg3=None)
Definition: svmutil.py:77
Definition: svm.h:25
void on_button_load_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:371
double value
Definition: svm.h:15
int nr_weight
Definition: svm.h:40
void on_fileselection_destroy(GtkObject *object, gpointer user_data)
Definition: callbacks.cpp:442
int * weight_label
Definition: svm.h:41
Definition: svm.h:25
void on_button_clear_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:307
Definition: svm.h:52
GdkPixmap * pixmap
Definition: callbacks.cpp:28
double p
Definition: svm.h:44
void on_filesel_ok_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:379
static enum @0 fileselection_flag
def svm_predict(y, x, m, options="")
Definition: svmutil.py:164
struct svm_node * x
Definition: svm-predict.c:12
double cache_size
Definition: svm.h:37
void on_button_change_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:92
void redraw_area(GtkWidget *widget, int x, int y, int w, int h)
Definition: callbacks.cpp:62
double y
Definition: callbacks.cpp:36
double eps
Definition: svm.h:38
int shrinking
Definition: svm.h:45
void clear_all()
Definition: callbacks.cpp:83
struct svm_node ** x
Definition: svm.h:22
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
Definition: svm.cpp:3013
GtkWidget * draw_main
Definition: callbacks.cpp:29
GtkWidget * create_fileselection(void)
Definition: interface.c:131
double x
Definition: callbacks.cpp:36
void draw_point(const point &p)
Definition: callbacks.cpp:70
void on_button_run_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:100
Definition: svm.h:26
int index
Definition: svm.h:14
#define YLEN
Definition: callbacks.cpp:14
list< point > point_list
Definition: callbacks.cpp:40
GtkWidget * entry_option
Definition: callbacks.cpp:31
int current_value
Definition: callbacks.cpp:41
void on_button_save_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:362
void on_filesel_cancel_clicked(GtkButton *button, gpointer user_data)
Definition: callbacks.cpp:448
signed char value
Definition: callbacks.cpp:37
int probability
Definition: svm.h:46
int degree
Definition: svm.h:32
Definition: svm.h:25
Definition: svm.h:12
double * y
Definition: svm.h:21
double gamma
Definition: svm.h:33
int l
Definition: svm.h:20
double * weight
Definition: svm.h:42
GdkColor colors[]
Definition: callbacks.cpp:16
void draw_all_points()
Definition: callbacks.cpp:77
double C
Definition: svm.h:39
#define XLEN
Definition: callbacks.cpp:13
int svm_type
Definition: svm.h:30
double nu
Definition: svm.h:43
struct svm_model * model
Definition: svmtrain.c:61
gboolean on_draw_main_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
Definition: callbacks.cpp:332
double coef0
Definition: svm.h:34
int kernel_type
Definition: svm.h:31
struct svm_node * x_space
Definition: svmtrain.c:62
void on_window1_destroy(GtkObject *object, gpointer user_data)
Definition: callbacks.cpp:314


ml_classifiers
Author(s): Scott Niekum , Joshua Whitley
autogenerated on Tue May 14 2019 02:28:35