USGS

Isis 3.0 Object Programmers' Reference

Home

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 "ScatterPlotData.h"
20 
21 namespace 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  QwtSymbol newSymbol(newCurve->markerSymbol());
139  newSymbol.setStyle(QwtSymbol::NoSymbol);
140  newCurve->setMarkerSymbol(newSymbol);
141 
142  QPen pen(newCurve->pen());
143  pen.setStyle(Qt::SolidLine);
144  newCurve->setPen(pen);
145 
146  newCurve->setTitle(selected->title().text() + " Best Fit");
147  newCurve->copySource(*selected);
148 
149  m_plotWindowWithCurves->add(newCurve);
150  }
151  }
154  QwtPlotSpectrogram *selected = selectedSpectrogram();
155  double a = Null;
156  double b = Null;
157 
158  try {
159  m_curveMultivariateStats->LinearRegression(a, b);
160  }
161  catch (IException &) {
162  }
163 
164  ScatterPlotData *scatterData =
165  dynamic_cast<ScatterPlotData *>(selected->data());
166 
167  if (scatterData && !IsSpecial(a) && !IsSpecial(b)) {
168  CubePlotCurve *newCurve = new CubePlotCurve(
169  m_plotWindowWithCurves->xAxisUnits(),
170  m_plotWindowWithCurves->yAxisUnits());
171 
172  QVector<double> rawXValues = scatterData->discreteXValues();
173 
174  int dataSize = rawXValues.size();
175  QVector<QPointF> data(dataSize);
176 
177  for (int i = 0; i < dataSize; i++) {
178  data[i].setX(rawXValues[i]);
179  data[i].setY(a + b * data[i].x());
180  }
181 
182  newCurve->setData(new QwtPointSeriesData(data));
183  newCurve->setColor(Qt::red);
184 
185  QwtSymbol newSymbol(newCurve->markerSymbol());
186  newSymbol.setStyle(QwtSymbol::NoSymbol);
187  newCurve->setMarkerSymbol(newSymbol);
188 
189  QPen pen(newCurve->pen());
190  pen.setStyle(Qt::SolidLine);
191  newCurve->setPen(pen);
192 
193  newCurve->setTitle(selected->title().text() + " Best Fit");
194 
195  m_plotWindowWithCurves->add(newCurve);
196  }
197  }
198  }
199 
200 
207  m_curvesCombo->clear();
208 
210  QList<QwtPlotSpectrogram *> spectrograms =
211  m_plotWindowWithCurves->plotSpectrograms();
212 
213  foreach (QwtPlotSpectrogram *spectrogram, spectrograms) {
214  if (dynamic_cast<ScatterPlotData *>(spectrogram->data())) {
215  m_curvesCombo->addItem( spectrogram->title().text(), qVariantFromValue(spectrogram) );
216  }
217  }
218 
219  QList<CubePlotCurve *> curves = m_plotWindowWithCurves->plotCurves();
220 
221  foreach (CubePlotCurve *curve, curves) {
222  m_curvesCombo->addItem( curve->title().text(), qVariantFromValue(curve) );
223  }
224  }
225  }
226 
227 
234  bool canDeriveEquation = false;
235 
236  if (selectedCurve()) {
237  CubePlotCurve *selected = selectedCurve();
239 
240  int dataSize = selected->dataSize();
241  for (int dataPoint = 0; dataPoint < dataSize; dataPoint++) {
242  double x = selected->sample(dataPoint).x();
243  double y = selected->sample(dataPoint).y();
244  m_curveMultivariateStats->AddData(&x, &y, 1);
245  }
246  }
247  else if (selectedSpectrogram()) {
248  QwtPlotSpectrogram *selected = selectedSpectrogram();
250 
251  ScatterPlotData *scatterData =
252  dynamic_cast<ScatterPlotData *>(selected->data());
253 
254  if (scatterData) {
255  for (int i = 0; i < scatterData->numberOfBins(); i++) {
256  QPair<double, double> pointXY = scatterData->binXY(i);
257 
258  int binValue = scatterData->binCount(i);
259  m_curveMultivariateStats->AddData(pointXY.first, pointXY.second,
260  binValue);
261  }
262  }
263  }
264 
266  m_curveMultivariateStats->ValidPixels() > 1) {
267  double a = Null;
268  double b = Null;
269 
270  try {
271  m_curveMultivariateStats->LinearRegression(a, b);
272  }
273  catch (IException &) {
274  }
275 
276  if (!IsSpecial(a) && !IsSpecial(b)) {
277  canDeriveEquation = true;
278  m_equationLabel->setText(
279  IString("y = " + IString(b) + "x + " + IString(a)).ToQt());
280 
281  double correlation = m_curveMultivariateStats->Correlation();
282 
283  if (!IsSpecial(correlation)) {
284  m_correlationLabel->setText(IString(correlation).ToQt());
285  m_determinationLabel->setText(
286  IString(correlation * correlation).ToQt());
287  }
288  else {
289  m_correlationLabel->setText("Undefined");
290  m_determinationLabel->setText("Undefined");
291  }
292  }
293  }
294 
295  m_okayButton->setEnabled(canDeriveEquation);
296 
297  if (!canDeriveEquation) {
298  m_equationLabel->setText("N/A");
299  m_correlationLabel->setText("N/A");
300  m_determinationLabel->setText("N/A");
301  }
302  }
303 
304 
312  CubePlotCurve *selected = NULL;
313 
315  selected = m_curvesCombo->itemData(
316  m_curvesCombo->currentIndex()).value<CubePlotCurve *>();
317 
318  QList<CubePlotCurve *> curves = m_plotWindowWithCurves->plotCurves();
319 
320  if (selected != NULL && curves.indexOf(selected) == -1) {
321  m_curvesCombo->removeItem(m_curvesCombo->currentIndex());
322  selected = selectedCurve();
323  }
324  }
325 
326  return selected;
327  }
328 
329 
338  QwtPlotSpectrogram *selected = NULL;
339 
341  selected = m_curvesCombo->itemData(
342  m_curvesCombo->currentIndex()).value<QwtPlotSpectrogram *>();
343 
344  QList<QwtPlotSpectrogram *> spectrograms =
345  m_plotWindowWithCurves->plotSpectrograms();
346 
347  if (selected != NULL && spectrograms.indexOf(selected) == -1) {
348  m_curvesCombo->removeItem(m_curvesCombo->currentIndex());
349  selected = selectedSpectrogram();
350  }
351  }
352 
353  return selected;
354  }
355 }