- Integrated the file import into the main interface (not as a plugin anymore)
[mldemos:mldemos.git] / _AlgorithmsPlugins / SEDS / interfaceSEDSDynamic.cpp
1 /*********************************************************************
2 MLDemos: A User-Friendly visualization toolkit for machine learning
3 Copyright (C) 2010  Basilio Noris
4 Contact: mldemos@b4silio.com
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License,
8 version 3 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *********************************************************************/
19 #include "interfaceSEDSDynamic.h"
20 #include "drawUtils.h"
21 #include <QPixmap>
22 #include <QBitmap>
23 #include <QPainter>
24
25 using namespace std;
26
27 DynamicSEDS::DynamicSEDS()
28 {
29         params = new Ui::ParametersSEDS();
30         params->setupUi(widget = new QWidget());
31         connect(params->sedsConstraintCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(OptionsChanged()));
32 }
33
34 void DynamicSEDS::OptionsChanged()
35 {
36         int constraintCriterion = params->sedsConstraintCombo->currentIndex();
37         //params->minorIterationCount->setEnabled(constraintCriterion);
38 }
39
40 void DynamicSEDS::SetParams(Dynamical *dynamical)
41 {
42         if(!dynamical) return;
43
44         int clusters = params->sedsCount->value();
45         bool bPrior = params->sedsCheckPrior->isChecked();
46         bool bMu = params->sedsCheckMu->isChecked();
47         bool bSigma = params->sedsCheckSigma->isChecked();
48         int objectiveType = params->sedsObjectiveCombo->currentIndex();
49         int maxIteration = params->iterationCount->value();
50         int constraintCriterion = params->sedsConstraintCombo->currentIndex();
51
52         ((DynamicalSEDS *)dynamical)->SetParams(clusters, bPrior, bMu, bSigma, objectiveType, maxIteration, constraintCriterion);
53
54         //float penalty = params->sedsPenaltySpin->value();
55         //int maxMinorIteration = params->minorIterationCount->value();
56         //((DynamicalSEDS *)dynamical)->SetParams(clusters, penalty, bPrior, bMu, bSigma, objectiveType, maxIteration);
57         //((DynamicalSEDS *)dynamical)->SetParams(clusters, penalty, bPrior, bMu, bSigma, objectiveType, maxIteration, maxMinorIteration, constraintCriterion);
58 }
59
60 Dynamical *DynamicSEDS::GetDynamical()
61 {
62         DynamicalSEDS *dynamical = new DynamicalSEDS();
63         SetParams(dynamical);
64         return dynamical;
65 }
66
67 void DynamicSEDS::DrawInfo(Canvas *canvas, QPainter &painter, Dynamical *dynamical)
68 {
69         if(!canvas || !dynamical) return;
70         painter.setRenderHint(QPainter::Antialiasing);
71
72         float resize = ((DynamicalSEDS*)dynamical)->resizeFactor;
73
74     Gmm *gmm = ((DynamicalSEDS*)dynamical)->gmm;
75     int xIndex = canvas->xIndex;
76     int yIndex = canvas->yIndex;
77     int dim = gmm->dim;
78     float mean[2];
79     float sigma[3];
80     painter.setBrush(Qt::NoBrush);
81     FOR(i, gmm->nstates)
82     {
83         float* bigSigma = new float[dim*dim];
84         float* bigMean = new float[dim];
85         gmm->getCovariance(i, bigSigma, false);
86         sigma[0] = bigSigma[xIndex*dim + xIndex];
87         sigma[1] = bigSigma[yIndex*dim + xIndex];
88         sigma[2] = bigSigma[yIndex*dim + yIndex];
89         gmm->getMean(i, bigMean);
90         mean[0] = bigMean[xIndex];
91         mean[1] = bigMean[yIndex];
92         delete [] bigSigma;
93         delete [] bigMean;
94
95         fvec endpoint = ((DynamicalSEDS*)dynamical)->endpoint;
96         FOR(j,2) mean[j] = mean[j]/resize + endpoint[j];
97         FOR(j,3) sigma[j] /= resize*resize;
98
99         painter.setPen(QPen(Qt::black, 1));
100         DrawEllipse(mean, sigma, 1, &painter, canvas);
101         painter.setPen(QPen(Qt::black, 0.5));
102         DrawEllipse(mean, sigma, 2, &painter, canvas);
103         QPointF point = canvas->toCanvasCoords(mean[0],mean[1]);
104         painter.setPen(QPen(Qt::black, 4));
105         painter.drawEllipse(point, 2, 2);
106         painter.setPen(QPen(Qt::white, 2));
107         painter.drawEllipse(point, 2, 2);
108     }
109 }
110
111 void DynamicSEDS::SaveOptions(QSettings &settings)
112 {
113         settings.setValue("sedsCount", params->sedsCount->value());
114         //settings.setValue("sedsPenalty", params->sedsPenaltySpin->value());
115         settings.setValue("sedsObjective", params->sedsObjectiveCombo->currentIndex());
116         settings.setValue("sedsPrior", params->sedsCheckPrior->isChecked());
117         settings.setValue("sedsMu", params->sedsCheckMu->isChecked());
118         settings.setValue("sedsSigma", params->sedsCheckSigma->isChecked());
119         settings.setValue("sedsConstraintCombo", params->sedsConstraintCombo->currentIndex());
120         settings.setValue("iterationCount", params->iterationCount->value());
121         //settings.setValue("minorIterationCount", params->minorIterationCount->value());
122 }
123
124 bool DynamicSEDS::LoadOptions(QSettings &settings)
125 {
126         if(settings.contains("sedsCount")) params->sedsCount->setValue(settings.value("sedsCount").toInt());
127         //if(settings.contains("sedsPenalty")) params->sedsPenaltySpin->setValue(settings.value("sedsPenalty").toFloat());
128         if(settings.contains("sedsObjective")) params->sedsObjectiveCombo->setCurrentIndex(settings.value("sedsObjective").toInt());
129         if(settings.contains("sedsPrior")) params->sedsCheckPrior->setChecked(settings.value("sedsPrior").toBool());
130         if(settings.contains("sedsMu")) params->sedsCheckMu->setChecked(settings.value("sedsMu").toBool());
131         if(settings.contains("sedsSigma")) params->sedsCheckSigma->setChecked(settings.value("sedsSigma").toBool());
132         if(settings.contains("sedsConstraintCombo")) params->sedsConstraintCombo->setCurrentIndex(settings.value("sedsConstraintCombo").toInt());
133         if(settings.contains("iterationCount")) params->iterationCount->setValue(settings.value("iterationCount").toInt());
134         //if(settings.contains("minorIterationCount")) params->minorIterationCount->setValue(settings.value("minorIterationCount").toInt());
135         return true;
136 }
137
138 void DynamicSEDS::SaveParams(QTextStream &file)
139 {
140         file << "dynamicalOptions" << ":" << "sedsCount" << " " << params->sedsCount->value() << "\n";
141         //file << "dynamicalOptions" << ":" << "sedsPenalty" << " " << params->sedsPenaltySpin->value() << "\n";
142         file << "dynamicalOptions" << ":" << "sedsObjective" << " " << params->sedsObjectiveCombo->currentIndex() << "\n";
143         file << "dynamicalOptions" << ":" << "sedsPrior" << " " << params->sedsCheckPrior->isChecked() << "\n";
144         file << "dynamicalOptions" << ":" << "sedsMu" << " " << params->sedsCheckMu->isChecked() << "\n";
145         file << "dynamicalOptions" << ":" << "sedsSigma" << " " << params->sedsCheckSigma->isChecked() << "\n";
146         file << "dynamicalOptions" << ":" << "sedsConstraintCombo" << " " << params->sedsConstraintCombo->currentIndex() << "\n";
147         file << "dynamicalOptions" << ":" << "iterationCount" << " " << params->iterationCount->value() << "\n";
148         //file << "dynamicalOptions" << ":" << "minorIterationCount" << " " << params->minorIterationCount->value() << "\n";
149 }
150
151 bool DynamicSEDS::LoadParams(QString name, float value)
152 {
153         if(name.endsWith("sedsCount")) params->sedsCount->setValue((int)value);
154         //if(name.endsWith("sedsPenalty")) params->sedsPenaltySpin->setValue(value);
155         if(name.endsWith("sedsObjective")) params->sedsObjectiveCombo->setCurrentIndex((int)value);
156         if(name.endsWith("sedsPrior")) params->sedsCheckPrior->setChecked((int)value);
157         if(name.endsWith("sedsMu")) params->sedsCheckMu->setChecked((int)value);
158         if(name.endsWith("sedsSigma")) params->sedsCheckSigma->setChecked((int)value);
159         if(name.endsWith("sedsConstraintCombo")) params->sedsConstraintCombo->setCurrentIndex((int)value);
160         if(name.endsWith("iterationCount")) params->iterationCount->setValue((int)value);
161         //if(name.endsWith("minorIterationCount")) params->minorIterationCount->setValue((int)value);
162         return true;
163 }
164
165 Q_EXPORT_PLUGIN2(mld_SEDS, DynamicSEDS)