You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

callbacks.cpp 10 kB

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

A Python package for graph kernels, graph edit distances and graph pre-image problem.