- Introduced SVG export, which has some issues with dynamical systems (too many paths...
[mldemos:mldemos.git] / _AlgorithmsPlugins / KNN / interfaceKNNRegress.cpp
1 /*********************************************************************\r
2 MLDemos: A User-Friendly visualization toolkit for machine learning\r
3 Copyright (C) 2010  Basilio Noris\r
4 Contact: mldemos@b4silio.com\r
5 \r
6 This library is free software; you can redistribute it and/or\r
7 modify it under the terms of the GNU Lesser General Public License,\r
8 version 3 as published by the Free Software Foundation.\r
9 \r
10 This library is distributed in the hope that it will be useful, but\r
11 WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
13 Lesser General Public License for more details.\r
14 \r
15 You should have received a copy of the GNU Lesser General Public\r
16 License along with this library; if not, write to the Free\r
17 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
18 *********************************************************************/\r
19 #include "interfaceKNNRegress.h"\r
20 #include <QPixmap>\r
21 #include <QBitmap>\r
22 #include <QPainter>\r
23 #include <datasetManager.h>\r
24 #include <canvas.h>\r
25 \r
26 using namespace std;\r
27 \r
28 RegrKNN::RegrKNN()\r
29 {\r
30         params = new Ui::ParametersKNNRegress();\r
31         params->setupUi(widget = new QWidget());\r
32 }\r
33 \r
34 void RegrKNN::SetParams(Regressor *regressor)\r
35 {\r
36         if(!regressor) return;\r
37         int k = params->knnKspin->value();\r
38         int metricType = params->knnNormCombo->currentIndex();\r
39         int metricP = params->knnNormSpin->value();\r
40 \r
41         ((RegressorKNN *)regressor)->SetParams(k, metricType, metricP);\r
42 }\r
43 \r
44 Regressor *RegrKNN::GetRegressor()\r
45 {\r
46         RegressorKNN *regressor = new RegressorKNN();\r
47         SetParams(regressor);\r
48         return regressor;\r
49 }\r
50 \r
51 void RegrKNN::DrawModel(Canvas *canvas, QPainter &painter, Regressor *regressor)\r
52 {\r
53         if(!regressor || !canvas) return;\r
54         int w = canvas->width();\r
55         int h = canvas->height();\r
56         painter.setRenderHint(QPainter::Antialiasing, true);\r
57 \r
58         fvec sample;\r
59         sample.resize(2,0);\r
60         int steps = w;\r
61         QPointF oldPoint(-FLT_MAX,-FLT_MAX);\r
62         QPointF oldPointUp(-FLT_MAX,-FLT_MAX);\r
63         QPointF oldPointDown(-FLT_MAX,-FLT_MAX);\r
64         FOR(x, steps)\r
65         {\r
66                 sample = canvas->toSampleCoords(x,0);\r
67                 fvec res = regressor->Test(sample);\r
68                 if(res[0] != res[0] || res[1] != res[1]) continue;\r
69                 QPointF point = canvas->toCanvasCoords(sample[0], res[0]);\r
70                 QPointF pointUp = canvas->toCanvasCoords(sample[0],res[0] + res[1]);\r
71                 pointUp.setX(0);\r
72                 pointUp.setY(pointUp.y() - point.y());\r
73                 QPointF pointDown = -pointUp;\r
74                 if(x)\r
75                 {\r
76                         painter.setPen(QPen(Qt::black, 1));\r
77                         painter.drawLine(point, oldPoint);\r
78                         painter.setPen(QPen(Qt::black, 0.5));\r
79                         painter.drawLine(pointUp, oldPointUp);\r
80                         painter.drawLine(pointDown, oldPointDown);\r
81                 }\r
82                 oldPoint = point;\r
83                 oldPointUp = pointUp;\r
84                 oldPointDown = pointDown;\r
85         }\r
86 }\r
87 \r
88 void RegrKNN::SaveOptions(QSettings &settings)\r
89 {\r
90         settings.setValue("knnK", params->knnKspin->value());\r
91         settings.setValue("knnNorm", params->knnNormCombo->currentIndex());\r
92         settings.setValue("knnPower", params->knnNormSpin->value());\r
93 }\r
94 \r
95 bool RegrKNN::LoadOptions(QSettings &settings)\r
96 {\r
97         if(settings.contains("knnK")) params->knnKspin->setValue(settings.value("knnK").toFloat());\r
98         if(settings.contains("knnNorm")) params->knnNormCombo->setCurrentIndex(settings.value("knnNorm").toInt());\r
99         if(settings.contains("knnPower")) params->knnNormSpin->setValue(settings.value("knnPower").toFloat());\r
100         return true;\r
101 }\r
102 \r
103 void RegrKNN::SaveParams(std::ofstream &file)\r
104 {\r
105         file << "regressionOptions" << ":" << "knnK" << " " << params->knnKspin->value() << std::endl;\r
106         file << "regressionOptions" << ":" << "knnNorm" << " " << params->knnNormCombo->currentIndex() << std::endl;\r
107         file << "regressionOptions" << ":" << "knnPower" << " " << params->knnNormSpin->value() << std::endl;\r
108 }\r
109 \r
110 bool RegrKNN::LoadParams(char *line, float value)\r
111 {\r
112         if(endsWith(line,"knnK")) params->knnKspin->setValue((int)value);\r
113         if(endsWith(line,"knnNorm")) params->knnNormCombo->setCurrentIndex((int)value);\r
114         if(endsWith(line,"knnPower")) params->knnNormSpin->setValue((int)value);\r
115         return true;\r
116 }\r