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