- Introduced SVG export, which has some issues with dynamical systems (too many paths...
[mldemos:mldemos.git] / _AlgorithmsPlugins / KPCA / pluginKPCA.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 "pluginKPCA.h"\r
20 #include "classifierKPCA.h"\r
21 #include <QPixmap>\r
22 #include <QBitmap>\r
23 #include <QPainter>\r
24 #include <QDebug>\r
25 \r
26 using namespace std;\r
27 \r
28 ClassKPCA::ClassKPCA()\r
29 {\r
30         params = new Ui::ParametersKPCA();\r
31         params->setupUi(widget = new QWidget());\r
32         connect(params->kernelTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(ChangeOptions()));\r
33 }\r
34 \r
35 \r
36 void ClassKPCA::ChangeOptions()\r
37 {\r
38         switch(params->kernelTypeCombo->currentIndex())\r
39         {\r
40         case 0: // linear\r
41                 params->kernelDegSpin->setEnabled(false);\r
42                 params->kernelWidthSpin->setEnabled(false);\r
43                 break;\r
44         case 1: // poly\r
45                 params->kernelDegSpin->setEnabled(true);\r
46                 params->kernelWidthSpin->setEnabled(false);\r
47                 break;\r
48         case 2: // rbf\r
49                 params->kernelDegSpin->setEnabled(false);\r
50                 params->kernelWidthSpin->setEnabled(true);\r
51                 break;\r
52         }\r
53 }\r
54 \r
55 \r
56 void ClassKPCA::SetParams(Classifier *classifier)\r
57 {\r
58         if(!classifier) return;\r
59         int kernelType = params->kernelTypeCombo->currentIndex();\r
60         float kernelGamma = params->kernelWidthSpin->value();\r
61         int kernelDegree = params->kernelDegSpin->value();\r
62         ((ClassifierKPCA *)classifier)->SetParams(kernelType, kernelDegree, kernelGamma);\r
63 }\r
64 \r
65 Classifier *ClassKPCA::GetClassifier()\r
66 {\r
67         ClassifierKPCA *classifier = new ClassifierKPCA();\r
68         SetParams(classifier);\r
69         return classifier;\r
70 }\r
71 \r
72 void ClassKPCA::DrawModel(Canvas *canvas, QPainter &painter, Classifier *classifier)\r
73 {\r
74         if(!classifier || !canvas) return;\r
75         int w = canvas->width();\r
76         int h = canvas->height();\r
77         painter.setRenderHint(QPainter::Antialiasing);\r
78 \r
79         ClassifierKPCA *kpca = (ClassifierKPCA*)classifier;\r
80 \r
81         vector<fvec> results = kpca->GetResults();\r
82         ivec labels = kpca->GetLabels();\r
83 \r
84         vector<fvec> samples = kpca->GetSamples();\r
85         painter.setPen(QPen(Qt::black, 0.2));\r
86         FOR(i, samples.size())\r
87         {\r
88                 QPointF pt1 = canvas->toCanvasCoords(samples[i]);\r
89                 QPointF pt2 = canvas->toCanvasCoords(results[i]);\r
90                 painter.drawLine(pt1, pt2);\r
91         }\r
92 \r
93         FOR(i, results.size())\r
94         {\r
95                 QPointF point = canvas->toCanvasCoords(results[i]);\r
96                 Canvas::drawSample(painter, point, 6, labels[i] == 1);\r
97         }\r
98 }\r
99 \r
100 void ClassKPCA::SaveOptions(QSettings &settings)\r
101 {\r
102         settings.setValue("kernelDeg", params->kernelDegSpin->value());\r
103         settings.setValue("kernelType", params->kernelTypeCombo->currentIndex());\r
104         settings.setValue("kernelWidth", params->kernelWidthSpin->value());\r
105 }\r
106 \r
107 bool ClassKPCA::LoadOptions(QSettings &settings)\r
108 {\r
109         if(settings.contains("kernelDeg")) params->kernelDegSpin->setValue(settings.value("kernelDeg").toFloat());\r
110         if(settings.contains("kernelType")) params->kernelTypeCombo->setCurrentIndex(settings.value("kernelType").toInt());\r
111         if(settings.contains("kernelWidth")) params->kernelWidthSpin->setValue(settings.value("kernelWidth").toFloat());\r
112         return true;\r
113 }\r
114 \r
115 void ClassKPCA::SaveParams(std::ofstream &file)\r
116 {\r
117         file << "classificationOptions" << ":" << "kernelDeg" << " " << params->kernelDegSpin->value() << std::endl;\r
118         file << "classificationOptions" << ":" << "kernelType" << " " << params->kernelTypeCombo->currentIndex() << std::endl;\r
119         file << "classificationOptions" << ":" << "kernelWidth" << " " << params->kernelWidthSpin->value() << std::endl;\r
120 }\r
121 \r
122 bool ClassKPCA::LoadParams(char *line, float value)\r
123 {\r
124         if(endsWith(line,"kernelDeg")) params->kernelDegSpin->setValue((int)value);\r
125         if(endsWith(line,"kernelType")) params->kernelTypeCombo->setCurrentIndex((int)value);\r
126         if(endsWith(line,"kernelWidth")) params->kernelWidthSpin->setValue(value);\r
127         return true;\r
128 }\r
129 \r
130 \r
131 Q_EXPORT_PLUGIN2(mld_KPCA, ClassKPCA)\r