Isis 3 Programmer Reference
PlotWindowBestFitDialog.cpp
1#include "PlotWindowBestFitDialog.h"
2
3#include <iostream>
4
5#include <qwt_plot_spectrogram.h>
6#include <qwt_text.h>
7
8#include <QCheckBox>
9#include <QColorDialog>
10#include <QComboBox>
11#include <QGridLayout>
12#include <QLabel>
13#include <QLineEdit>
14#include <QPushButton>
15#include <QVariant>
16
17#include "CubePlotCurve.h"
18#include "IString.h"
19#include "ScatterPlotData.h"
20
21namespace Isis {
33 PlotWindow *windowWithCurves, QWidget *parent) : QDialog(parent) {
34 m_plotWindowWithCurves = windowWithCurves;
35 QGridLayout *optionsLayout = new QGridLayout;
36
37 int row = 0;
38 QLabel *titleLabel = new QLabel("Curve To Fit: ");
40// m_curvesCombo->setInsertPolicy(QComboBox::InsertAlphabetically);
41 optionsLayout->addWidget(titleLabel, row, 0);
42 optionsLayout->addWidget(m_curvesCombo, row, 1);
43 connect(m_curvesCombo, SIGNAL(currentIndexChanged(int)),
44 this, SLOT(refreshWidgetStates()));
45 row++;
46
47 QLabel *equationTitleLabel = new QLabel("Equation from Curve: ");
48 m_equationLabel = new QLabel;
49 optionsLayout->addWidget(equationTitleLabel, row, 0);
50 optionsLayout->addWidget(m_equationLabel, row, 1);
51 row++;
52
53 QLabel *correlationTitleLabel = new QLabel("Correlation Coefficient (r): ");
54 m_correlationLabel = new QLabel;
55 optionsLayout->addWidget(correlationTitleLabel, row, 0);
56 optionsLayout->addWidget(m_correlationLabel, row, 1);
57 row++;
58
59 QLabel *determinationTitleLabel = new QLabel(
60 "Coefficient of Determination (r<sup>2</sup>): ");
61 m_determinationLabel = new QLabel;
62 optionsLayout->addWidget(determinationTitleLabel, row, 0);
63 optionsLayout->addWidget(m_determinationLabel, row, 1);
64 row++;
65
66 QHBoxLayout *applyButtonsLayout = new QHBoxLayout;
67 applyButtonsLayout->addStretch();
68
69 m_okayButton = new QPushButton("&Ok");
70 m_okayButton->setIcon(QIcon::fromTheme("dialog-ok"));
71 connect(m_okayButton, SIGNAL(clicked()),
72 this, SLOT(createBestFitLine()));
73 connect(m_okayButton, SIGNAL(clicked()),
74 this, SLOT(close()));
75 applyButtonsLayout->addWidget(m_okayButton);
76
77 QPushButton *cancel = new QPushButton("&Cancel");
78 cancel->setIcon(QIcon::fromTheme("dialog-cancel"));
79 connect(cancel, SIGNAL(clicked()),
80 this, SLOT(close()));
81 applyButtonsLayout->addWidget(cancel);
82
83 QWidget *optionsHolder = new QWidget;
84 optionsHolder->setLayout(optionsLayout);
85
86 QWidget *applyButtonsHolder = new QWidget;
87 applyButtonsHolder->setLayout(applyButtonsLayout);
88
89 QVBoxLayout *mainLayout = new QVBoxLayout;
90 mainLayout->addWidget(optionsHolder);
91 mainLayout->addWidget(applyButtonsHolder);
92
93 setLayout(mainLayout);
94
97 }
98
99
100 PlotWindowBestFitDialog::~PlotWindowBestFitDialog() {
101 }
102
103
112
114 CubePlotCurve *selected = selectedCurve();
115 double a = Null;
116 double b = Null;
117 try {
118 m_curveMultivariateStats->LinearRegression(a, b);
119 }
120 catch (IException &) {
121 }
122
123 if (!IsSpecial(a) && !IsSpecial(b)) {
124 CubePlotCurve *newCurve = new CubePlotCurve(selected->xUnits(),
125 selected->yUnits());
126
127 int dataSize = selected->dataSize();
128 QVector<QPointF> data(dataSize);
129
130 for (int i = 0; i < dataSize; i++) {
131 data[i].setX(selected->sample(i).x());
132 data[i].setY(a + b * data[i].x());
133 }
134
135 newCurve->setData(new QwtPointSeriesData(data));
136 newCurve->setColor(selected->color());
137
138 newCurve->setMarkerSymbol(QwtSymbol::NoSymbol);
139
140 QPen pen(newCurve->pen());
141 pen.setStyle(Qt::SolidLine);
142 newCurve->setPen(pen);
143
144 newCurve->setTitle(selected->title().text() + " Best Fit");
145 newCurve->copySource(*selected);
146
147 m_plotWindowWithCurves->add(newCurve);
148 }
149 }
152 QwtPlotSpectrogram *selected = selectedSpectrogram();
153 double a = Null;
154 double b = Null;
155
156 try {
157 m_curveMultivariateStats->LinearRegression(a, b);
158 }
159 catch (IException &) {
160 }
161
162 ScatterPlotData *scatterData =
163 dynamic_cast<ScatterPlotData *>(selected->data());
164
165 if (scatterData && !IsSpecial(a) && !IsSpecial(b)) {
166 CubePlotCurve *newCurve = new CubePlotCurve(
167 m_plotWindowWithCurves->xAxisUnits(),
168 m_plotWindowWithCurves->yAxisUnits());
169
170 QVector<double> rawXValues = scatterData->discreteXValues();
171
172 int dataSize = rawXValues.size();
173 QVector<QPointF> data(dataSize);
174
175 for (int i = 0; i < dataSize; i++) {
176 data[i].setX(rawXValues[i]);
177 data[i].setY(a + b * data[i].x());
178 }
179
180 newCurve->setData(new QwtPointSeriesData(data));
181 newCurve->setColor(Qt::red);
182
183 newCurve->setMarkerSymbol(QwtSymbol::NoSymbol);
184
185 QPen pen(newCurve->pen());
186 pen.setStyle(Qt::SolidLine);
187 newCurve->setPen(pen);
188
189 newCurve->setTitle(selected->title().text() + " Best Fit");
190
191 m_plotWindowWithCurves->add(newCurve);
192 }
193 }
194 }
195
196
203 m_curvesCombo->clear();
204
206 QList<QwtPlotSpectrogram *> spectrograms =
207 m_plotWindowWithCurves->plotSpectrograms();
208
209 foreach (QwtPlotSpectrogram *spectrogram, spectrograms) {
210 if (dynamic_cast<ScatterPlotData *>(spectrogram->data())) {
211 m_curvesCombo->addItem( spectrogram->title().text(), qVariantFromValue(spectrogram) );
212 }
213 }
214
215 QList<CubePlotCurve *> curves = m_plotWindowWithCurves->plotCurves();
216
217 foreach (CubePlotCurve *curve, curves) {
218 m_curvesCombo->addItem( curve->title().text(), qVariantFromValue(curve) );
219 }
220 }
221 }
222
223
230 bool canDeriveEquation = false;
231
232 if (selectedCurve()) {
233 CubePlotCurve *selected = selectedCurve();
235
236 int dataSize = selected->dataSize();
237 for (int dataPoint = 0; dataPoint < dataSize; dataPoint++) {
238 double x = selected->sample(dataPoint).x();
239 double y = selected->sample(dataPoint).y();
240 m_curveMultivariateStats->AddData(&x, &y, 1);
241 }
242 }
243 else if (selectedSpectrogram()) {
244 QwtPlotSpectrogram *selected = selectedSpectrogram();
246
247 ScatterPlotData *scatterData =
248 dynamic_cast<ScatterPlotData *>(selected->data());
249
250 if (scatterData) {
251 for (int i = 0; i < scatterData->numberOfBins(); i++) {
252 QPair<double, double> pointXY = scatterData->binXY(i);
253
254 int binValue = scatterData->binCount(i);
255 m_curveMultivariateStats->AddData(pointXY.first, pointXY.second,
256 binValue);
257 }
258 }
259 }
260
262 m_curveMultivariateStats->ValidPixels() > 1) {
263 double a = Null;
264 double b = Null;
265
266 try {
267 m_curveMultivariateStats->LinearRegression(a, b);
268 }
269 catch (IException &) {
270 }
271
272 if (!IsSpecial(a) && !IsSpecial(b)) {
273 canDeriveEquation = true;
274 m_equationLabel->setText(
275 IString("y = " + IString(b) + "x + " + IString(a)).ToQt());
276
277 double correlation = m_curveMultivariateStats->Correlation();
278
279 if (!IsSpecial(correlation)) {
280 m_correlationLabel->setText(IString(correlation).ToQt());
281 m_determinationLabel->setText(
282 IString(correlation * correlation).ToQt());
283 }
284 else {
285 m_correlationLabel->setText("Undefined");
286 m_determinationLabel->setText("Undefined");
287 }
288 }
289 }
290
291 m_okayButton->setEnabled(canDeriveEquation);
292
293 if (!canDeriveEquation) {
294 m_equationLabel->setText("N/A");
295 m_correlationLabel->setText("N/A");
296 m_determinationLabel->setText("N/A");
297 }
298 }
299
300
308 CubePlotCurve *selected = NULL;
309
311 selected = m_curvesCombo->itemData(
312 m_curvesCombo->currentIndex()).value<CubePlotCurve *>();
313
314 QList<CubePlotCurve *> curves = m_plotWindowWithCurves->plotCurves();
315
316 if (selected != NULL && curves.indexOf(selected) == -1) {
317 m_curvesCombo->removeItem(m_curvesCombo->currentIndex());
318 selected = selectedCurve();
319 }
320 }
321
322 return selected;
323 }
324
325
334 QwtPlotSpectrogram *selected = NULL;
335
337 selected = m_curvesCombo->itemData(
338 m_curvesCombo->currentIndex()).value<QwtPlotSpectrogram *>();
339
340 QList<QwtPlotSpectrogram *> spectrograms =
341 m_plotWindowWithCurves->plotSpectrograms();
342
343 if (selected != NULL && spectrograms.indexOf(selected) == -1) {
344 m_curvesCombo->removeItem(m_curvesCombo->currentIndex());
345 selected = selectedSpectrogram();
346 }
347 }
348
349 return selected;
350 }
351}
This is a plot curve with information relating it to a particular cube or region of a cube.
Isis exception class.
Definition IException.h:91
Adds specific functionality to C++ strings.
Definition IString.h:165
QString ToQt() const
Retuns the object string as a QString.
Definition IString.cpp:869
Container of multivariate statistics.
QColor color() const
This method returns the color of the curve.
Definition PlotCurve.cpp:55
Units yUnits() const
Get the units of the y-axis double data.
Definition PlotCurve.cpp:85
Units xUnits() const
Get the units of the x-axis double data.
Definition PlotCurve.cpp:75
QPointer< QLabel > m_equationLabel
A label populated with the resulting equation from a best fit.
QPointer< PlotWindow > m_plotWindowWithCurves
The plot window we're creating a best for line for.
QPointer< QLabel > m_correlationLabel
A label populated with the resulting correlation from a best fit.
PlotWindowBestFitDialog(PlotWindow *windowWithCurves, QWidget *parent)
Create a PlotWindowBestFitDialog.
QPointer< QPushButton > m_okayButton
The ok button which the user clicks to create the best fit curve.
QPointer< QComboBox > m_curvesCombo
A combo box for the user to select a curve/spectrogram to best fit.
void createBestFitLine()
This is called when the user wants the best fit line.
QScopedPointer< MultivariateStatistics > m_curveMultivariateStats
The MV stats which is doing our regression calculations.
void readCurvesFromWindow()
This populates the curve selection combo with all of the available information inside of the PlotWind...
QwtPlotSpectrogram * selectedSpectrogram()
If a spectrogram (scatter plot) is selected, this returns it.
void refreshWidgetStates()
This updates all of the widgets in this window's visibility and text data based on what the user has ...
QPointer< QLabel > m_determinationLabel
A label populated with the resulting determination from a best fit.
CubePlotCurve * selectedCurve()
If a curve is selected, this returns it.
This is the QwtRasterData for a scatter plot.
QPair< double, double > binXY(int binIndex) const
Get the center X/Y Dn values for the bin at index.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
const double Null
Value for an Isis Null pixel.
bool IsSpecial(const double d)
Returns if the input pixel is special.