qt/svm-toy.cpp
Go to the documentation of this file.
1 #include <QtGui>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <list>
7 #include "../../svm.h"
8 using namespace std;
9 
10 #define DEFAULT_PARAM "-t 2 -c 100"
11 #define XLEN 500
12 #define YLEN 500
13 
14 QRgb colors[] =
15 {
16  qRgb(0, 0, 0),
17  qRgb(0, 120, 120),
18  qRgb(120, 120, 0),
19  qRgb(120, 0, 120),
20  qRgb(0, 200, 200),
21  qRgb(200, 200, 0),
22  qRgb(200, 0, 200)
23 };
24 
25 class SvmToyWindow : public QWidget
26 {
27 
28  Q_OBJECT
29 
30 public:
31  SvmToyWindow();
32  ~SvmToyWindow();
33 protected:
34  virtual void mousePressEvent(QMouseEvent*);
35  virtual void paintEvent(QPaintEvent*);
36 
37 private:
38  QPixmap buffer;
39  QPixmap icon1;
40  QPixmap icon2;
41  QPixmap icon3;
42  QPushButton button_change_icon;
43  QPushButton button_run;
44  QPushButton button_clear;
45  QPushButton button_save;
46  QPushButton button_load;
47  QLineEdit input_line;
48  QPainter buffer_painter;
49  struct point
50  {
51  double x, y;
52  signed char value;
53  };
54  list<point> point_list;
56  const QPixmap& choose_icon(int v)
57  {
58  if (v == 1) return icon1;
59  else if (v == 2) return icon2;
60  else return icon3;
61  }
62  void clear_all()
63  {
64  point_list.clear();
65  buffer.fill(Qt::black);
66  repaint();
67  }
68  void draw_point(const point& p)
69  {
70  const QPixmap& icon = choose_icon(p.value);
71  buffer_painter.drawPixmap((int)(p.x * XLEN), (int)(p.y * YLEN), icon);
72  repaint();
73  }
75  {
76  for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
77  draw_point(*p);
78  }
79 private slots:
81  {
82  ++current_value;
83  if (current_value > 3) current_value = 1;
84  button_change_icon.setIcon(choose_icon(current_value));
85  }
87  {
88  // guard
89  if (point_list.empty()) return;
90 
92  int i, j;
93 
94  // default values
95  param.svm_type = C_SVC;
96  param.kernel_type = RBF;
97  param.degree = 3;
98  param.gamma = 0;
99  param.coef0 = 0;
100  param.nu = 0.5;
101  param.cache_size = 100;
102  param.C = 1;
103  param.eps = 1e-3;
104  param.p = 0.1;
105  param.shrinking = 1;
106  param.probability = 0;
107  param.nr_weight = 0;
108  param.weight_label = NULL;
109  param.weight = NULL;
110 
111  // parse options
112  const char *p = input_line.text().toAscii().constData();
113 
114  while (1)
115  {
116  while (*p && *p != '-')
117  p++;
118 
119  if (*p == '\0')
120  break;
121 
122  p++;
123  switch (*p++)
124  {
125  case 's':
126  param.svm_type = atoi(p);
127  break;
128  case 't':
129  param.kernel_type = atoi(p);
130  break;
131  case 'd':
132  param.degree = atoi(p);
133  break;
134  case 'g':
135  param.gamma = atof(p);
136  break;
137  case 'r':
138  param.coef0 = atof(p);
139  break;
140  case 'n':
141  param.nu = atof(p);
142  break;
143  case 'm':
144  param.cache_size = atof(p);
145  break;
146  case 'c':
147  param.C = atof(p);
148  break;
149  case 'e':
150  param.eps = atof(p);
151  break;
152  case 'p':
153  param.p = atof(p);
154  break;
155  case 'h':
156  param.shrinking = atoi(p);
157  break;
158  case 'b':
159  param.probability = atoi(p);
160  break;
161  case 'w':
162  ++param.nr_weight;
163  param.weight_label = (int *)realloc(param.weight_label, sizeof(int) * param.nr_weight);
164  param.weight = (double *)realloc(param.weight, sizeof(double) * param.nr_weight);
165  param.weight_label[param.nr_weight - 1] = atoi(p);
166  while (*p && !isspace(*p)) ++p;
167  param.weight[param.nr_weight - 1] = atof(p);
168  break;
169  }
170  }
171 
172  // build problem
174 
175  prob.l = point_list.size();
176  prob.y = new double[prob.l];
177 
178  if (param.kernel_type == PRECOMPUTED)
179  {
180  }
181  else if (param.svm_type == EPSILON_SVR ||
182  param.svm_type == NU_SVR)
183  {
184  if (param.gamma == 0) param.gamma = 1;
185  svm_node *x_space = new svm_node[2 * prob.l];
186  prob.x = new svm_node *[prob.l];
187 
188  i = 0;
189  for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
190  {
191  x_space[2 * i].index = 1;
192  x_space[2 * i].value = q->x;
193  x_space[2 * i + 1].index = -1;
194  prob.x[i] = &x_space[2 * i];
195  prob.y[i] = q->y;
196  }
197 
198  // build model & classify
199  svm_model *model = svm_train(&prob, &param);
200  svm_node x[2];
201  x[0].index = 1;
202  x[1].index = -1;
203  int *j = new int[XLEN];
204 
205  for (i = 0; i < XLEN; i++)
206  {
207  x[0].value = (double) i / XLEN;
208  j[i] = (int)(YLEN * svm_predict(model, x));
209  }
210 
211  buffer_painter.setPen(colors[0]);
212  buffer_painter.drawLine(0, 0, 0, YLEN - 1);
213 
214  int p = (int)(param.p * YLEN);
215  for (i = 1; i < XLEN; i++)
216  {
217  buffer_painter.setPen(colors[0]);
218  buffer_painter.drawLine(i, 0, i, YLEN - 1);
219 
220  buffer_painter.setPen(colors[5]);
221  buffer_painter.drawLine(i - 1, j[i - 1], i, j[i]);
222 
223  if (param.svm_type == EPSILON_SVR)
224  {
225  buffer_painter.setPen(colors[2]);
226  buffer_painter.drawLine(i - 1, j[i - 1] + p, i, j[i] + p);
227 
228  buffer_painter.setPen(colors[2]);
229  buffer_painter.drawLine(i - 1, j[i - 1] - p, i, j[i] - p);
230  }
231  }
232 
234  delete[] j;
235  delete[] x_space;
236  delete[] prob.x;
237  delete[] prob.y;
238  }
239  else
240  {
241  if (param.gamma == 0) param.gamma = 0.5;
242  svm_node *x_space = new svm_node[3 * prob.l];
243  prob.x = new svm_node *[prob.l];
244 
245  i = 0;
246  for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
247  {
248  x_space[3 * i].index = 1;
249  x_space[3 * i].value = q->x;
250  x_space[3 * i + 1].index = 2;
251  x_space[3 * i + 1].value = q->y;
252  x_space[3 * i + 2].index = -1;
253  prob.x[i] = &x_space[3 * i];
254  prob.y[i] = q->value;
255  }
256 
257  // build model & classify
258  svm_model *model = svm_train(&prob, &param);
259  svm_node x[3];
260  x[0].index = 1;
261  x[1].index = 2;
262  x[2].index = -1;
263 
264  for (i = 0; i < XLEN; i++)
265  for (j = 0; j < YLEN ; j++)
266  {
267  x[0].value = (double) i / XLEN;
268  x[1].value = (double) j / YLEN;
269  double d = svm_predict(model, x);
270  if (param.svm_type == ONE_CLASS && d < 0) d = 2;
271  buffer_painter.setPen(colors[(int)d]);
272  buffer_painter.drawPoint(i, j);
273  }
274 
276  delete[] x_space;
277  delete[] prob.x;
278  delete[] prob.y;
279  }
280  free(param.weight_label);
281  free(param.weight);
282  draw_all_points();
283  }
285  {
286  clear_all();
287  }
289  {
290  QString filename = QFileDialog::getSaveFileName();
291  if (!filename.isNull())
292  {
293  FILE *fp = fopen(filename.toAscii().constData(), "w");
294 
295  const char *p = input_line.text().toAscii().constData();
296  const char* svm_type_str = strstr(p, "-s ");
297  int svm_type = C_SVC;
298  if (svm_type_str != NULL)
299  sscanf(svm_type_str, "-s %d", &svm_type);
300 
301  if (fp)
302  {
303  if (svm_type == EPSILON_SVR || svm_type == NU_SVR)
304  {
305  for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
306  fprintf(fp, "%f 1:%f\n", p->y, p->x);
307  }
308  else
309  {
310  for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
311  fprintf(fp, "%d 1:%f 2:%f\n", p->value, p->x, p->y);
312  }
313  fclose(fp);
314  }
315  }
316  }
318  {
319  QString filename = QFileDialog::getOpenFileName();
320  if (!filename.isNull())
321  {
322  FILE *fp = fopen(filename.toAscii().constData(), "r");
323  if (fp)
324  {
325  clear_all();
326  char buf[4096];
327  while (fgets(buf, sizeof(buf), fp))
328  {
329  int v;
330  double x, y;
331  if (sscanf(buf, "%d%*d:%lf%*d:%lf", &v, &x, &y) == 3)
332  {
333  point p = {x, y, v};
334  point_list.push_back(p);
335  }
336  else if (sscanf(buf, "%lf%*d:%lf", &y, &x) == 2)
337  {
338  point p = {x, y, current_value};
339  point_list.push_back(p);
340  }
341  else
342  break;
343  }
344  fclose(fp);
345  draw_all_points();
346  }
347  }
348 
349  }
350 };
351 
352 #include "svm-toy.moc"
353 
355  : button_change_icon(this)
356  , button_run("Run", this)
357  , button_clear("Clear", this)
358  , button_save("Save", this)
359  , button_load("Load", this)
360  , input_line(this)
361  , current_value(1)
362 {
363  buffer = QPixmap(XLEN, YLEN);
364  buffer.fill(Qt::black);
365 
366  buffer_painter.begin(&buffer);
367 
368  QObject::connect(&button_change_icon, SIGNAL(clicked()), this,
370  QObject::connect(&button_run, SIGNAL(clicked()), this,
371  SLOT(button_run_clicked()));
372  QObject::connect(&button_clear, SIGNAL(clicked()), this,
373  SLOT(button_clear_clicked()));
374  QObject::connect(&button_save, SIGNAL(clicked()), this,
375  SLOT(button_save_clicked()));
376  QObject::connect(&button_load, SIGNAL(clicked()), this,
377  SLOT(button_load_clicked()));
378  QObject::connect(&input_line, SIGNAL(returnPressed()), this,
379  SLOT(button_run_clicked()));
380 
381  // don't blank the window before repainting
382  setAttribute(Qt::WA_NoBackground);
383 
384  icon1 = QPixmap(4, 4);
385  icon2 = QPixmap(4, 4);
386  icon3 = QPixmap(4, 4);
387 
388 
389  QPainter painter;
390  painter.begin(&icon1);
391  painter.fillRect(0, 0, 4, 4, QBrush(colors[4]));
392  painter.end();
393 
394  painter.begin(&icon2);
395  painter.fillRect(0, 0, 4, 4, QBrush(colors[5]));
396  painter.end();
397 
398  painter.begin(&icon3);
399  painter.fillRect(0, 0, 4, 4, QBrush(colors[6]));
400  painter.end();
401 
402  button_change_icon.setGeometry(0, YLEN, 50, 25);
403  button_run.setGeometry(50, YLEN, 50, 25);
404  button_clear.setGeometry(100, YLEN, 50, 25);
405  button_save.setGeometry(150, YLEN, 50, 25);
406  button_load.setGeometry(200, YLEN, 50, 25);
407  input_line.setGeometry(250, YLEN, 250, 25);
408 
409  input_line.setText(DEFAULT_PARAM);
410  button_change_icon.setIcon(icon1);
411 }
412 
414 {
415  buffer_painter.end();
416 }
417 
418 void SvmToyWindow::mousePressEvent(QMouseEvent* event)
419 {
420  point p = {(double)event->x() / XLEN, (double)event->y() / YLEN, current_value};
421  point_list.push_back(p);
422  draw_point(p);
423 }
424 
425 void SvmToyWindow::paintEvent(QPaintEvent*)
426 {
427  // copy the image from the buffer pixmap to the window
428  QPainter p(this);
429  p.drawPixmap(0, 0, buffer);
430 }
431 
432 int main(int argc, char* argv[])
433 {
434  QApplication myapp(argc, argv);
435 
436  SvmToyWindow* mywidget = new SvmToyWindow();
437  mywidget->setGeometry(100, 100, XLEN, YLEN + 25);
438 
439  mywidget->show();
440  return myapp.exec();
441 }
d
QPainter buffer_painter
Definition: qt/svm-toy.cpp:48
struct svm_problem prob
Definition: svmtrain.c:60
const QPixmap & choose_icon(int v)
Definition: qt/svm-toy.cpp:56
virtual void mousePressEvent(QMouseEvent *)
Definition: qt/svm-toy.cpp:418
struct svm_parameter param
Definition: svmtrain.c:59
def svm_train(arg1, arg2=None, arg3=None)
Definition: svmutil.py:77
void button_save_clicked()
Definition: qt/svm-toy.cpp:288
QPushButton button_run
Definition: qt/svm-toy.cpp:43
Definition: svm.h:25
#define YLEN
Definition: qt/svm-toy.cpp:12
double value
Definition: svm.h:15
int nr_weight
Definition: svm.h:40
#define XLEN
Definition: qt/svm-toy.cpp:11
virtual void paintEvent(QPaintEvent *)
Definition: qt/svm-toy.cpp:425
QPushButton button_change_icon
Definition: qt/svm-toy.cpp:42
int * weight_label
Definition: svm.h:41
Definition: svm.h:25
QPushButton button_load
Definition: qt/svm-toy.cpp:46
int main(int argc, char *argv[])
Definition: qt/svm-toy.cpp:432
void button_change_icon_clicked()
Definition: qt/svm-toy.cpp:80
void button_run_clicked()
Definition: qt/svm-toy.cpp:86
Definition: svm.h:52
QPixmap icon1
Definition: qt/svm-toy.cpp:39
double p
Definition: svm.h:44
void draw_all_points()
Definition: qt/svm-toy.cpp:74
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
QLineEdit input_line
Definition: qt/svm-toy.cpp:47
QPushButton button_save
Definition: qt/svm-toy.cpp:45
QRgb colors[]
Definition: qt/svm-toy.cpp:14
QPushButton button_clear
Definition: qt/svm-toy.cpp:44
void draw_point(const point &p)
Definition: qt/svm-toy.cpp:68
signed char value
Definition: qt/svm-toy.cpp:52
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
QPixmap icon3
Definition: qt/svm-toy.cpp:41
#define DEFAULT_PARAM
Definition: qt/svm-toy.cpp:10
void draw_point(const point &p)
Definition: callbacks.cpp:70
Definition: svm.h:26
int index
Definition: svm.h:14
int current_value
Definition: callbacks.cpp:41
void button_clear_clicked()
Definition: qt/svm-toy.cpp:284
QPixmap icon2
Definition: qt/svm-toy.cpp:40
int probability
Definition: svm.h:46
int degree
Definition: svm.h:32
Definition: svm.h:25
Definition: svm.h:12
void button_load_clicked()
Definition: qt/svm-toy.cpp:317
double * y
Definition: svm.h:21
double gamma
Definition: svm.h:33
int l
Definition: svm.h:20
double * weight
Definition: svm.h:42
void draw_all_points()
Definition: callbacks.cpp:77
double C
Definition: svm.h:39
int svm_type
Definition: svm.h:30
double nu
Definition: svm.h:43
struct svm_model * model
Definition: svmtrain.c:61
double coef0
Definition: svm.h:34
QPixmap buffer
Definition: qt/svm-toy.cpp:38
list< point > point_list
Definition: qt/svm-toy.cpp:54
void clear_all()
Definition: qt/svm-toy.cpp:62
int kernel_type
Definition: svm.h:31
struct svm_node * x_space
Definition: svmtrain.c:62


ml_classifiers
Author(s): Scott Niekum , Joshua Whitley
autogenerated on Sun Dec 15 2019 03:53:50