Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions qeframeworkSup/project/widgets/QECorrelation/QECorrelation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ void QECorrelation::setup ()

QObject::connect (this->uiForm->Time_Select_Button, SIGNAL (clicked (bool)),
this, SLOT (timeSelectButtonClick (bool)));
QObject::connect (this->uiForm->Show_Regression_CheckBox, SIGNAL (stateChanged (int)),
this, SLOT (plotRegressionStateChanged (int)));


QObject::connect (this->uiForm->Number_Samples_Edit, SIGNAL (valueChanged (const double)),
this, SLOT (numberSamplesEditChange (const double)));
Expand Down Expand Up @@ -688,10 +691,10 @@ void QECorrelation::setReadOut (const QString status)

//------------------------------------------------------------------------------
// Refer to http://en.wikipedia.org/wiki/Correlation_and_dependence
// and to https://en.wikipedia.org/wiki/Simple_linear_regression
//
double QECorrelation::calculateCorrelationCoefficient ()
void QECorrelation::calculateCorrelationAndRegression ()
{
double result;
int number;
double sumX;
double sumY ;
Expand All @@ -709,7 +712,12 @@ double QECorrelation::calculateCorrelationCoefficient ()
double sdY;

number = this->xData.count ();
if (number <= 0) return 0.0;
if (number <= 0){
this->correlationCoef = 0.0;
this->regressionSlope = std::nan("0");
this->regressionIntersept = std::nan("0");
return ;
}

// Sum x, x^2, y, y^2 and xy.
//
Expand Down Expand Up @@ -753,12 +761,19 @@ double QECorrelation::calculateCorrelationCoefficient ()
sdX = sqrt (varX);
sdY = sqrt (varY);

result = (meanXY - meanX * meanY) / (sdX * sdY);
this->correlationCoef = (meanXY - meanX * meanY) / (sdX * sdY);
} else {
result = 0.0;
this->correlationCoef = 0.0;
}

return result;
if (varX != 0.0){
this->regressionSlope = (meanXY - meanX * meanY) / varX;
this->regressionIntersept = meanY - regressionSlope*meanX;
}else{
this->regressionSlope = std::nan("0");
this->regressionIntersept = std::nan("0");
}
return;
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -825,6 +840,16 @@ void QECorrelation::reDrawPlane ()
pen.setStyle (Qt::DashLine);
this->plotArea->setGridPen (pen);

if (this->uiForm->Show_Regression_CheckBox->checkState() == Qt::Checked){
if (this->regressionSlope!=this->regressionSlope){ // is nan
}else{
pen.setColor (QColor ("red"));
this->plotArea->setCurveStyle (QwtPlotCurve::Lines);
this->plotArea->setCurvePen (pen);
this->plotArea->plotCurveData ({TX_Min, TX_Max}, {this->regressionSlope*TX_Min+this->regressionIntersept, this->regressionSlope*TX_Max+this->regressionIntersept});
}
}

pen.setColor (QColor ("blue"));
pen.setStyle (Qt::SolidLine);

Expand Down Expand Up @@ -882,13 +907,19 @@ void QECorrelation::updateDataArrays ()
maximumPeriod = samplePeriod * maximumPoints;
currentPeriod = samplePeriod * number;

correlation = this->calculateCorrelationCoefficient ();
this->calculateCorrelationAndRegression ();

this->uiForm->Number_Points_Label->setText (QString ("%1").arg (number));
this->uiForm->Maximum_Sample_Label->setText (QEUtilities::intervalToString (maximumPeriod, 0, false));
this->uiForm->Ongoing_Sample_Label->setText (QEUtilities::intervalToString (currentPeriod, 0, false));
this->uiForm->Correlation_Value_Label->setText (QString ("%1").arg (correlation, 0, 'f', 4));

this->uiForm->Correlation_Value_Label->setText (QString ("%1").arg (this->correlationCoef, 0, 'f', 4));
if (this->regressionSlope!=this->regressionSlope){ // is nan
this->uiForm->Regression_Slope_Value_Label->setText (QString ("---"));
this->uiForm->Regression_Intercept_Value_Label->setText (QString ("---"));
}else{
this->uiForm->Regression_Slope_Value_Label->setText (QString ("%1").arg (this->regressionSlope, 0, 'f', 4));
this->uiForm->Regression_Intercept_Value_Label->setText (QString ("%1").arg (this->regressionIntersept, 0, 'f', 4));
}
this->replotIsRequired = true;
}

Expand Down Expand Up @@ -1360,6 +1391,12 @@ void QECorrelation::timeSelectButtonClick (bool)
}
}

//------------------------------------------------------------------------------
//
void QECorrelation::plotRegressionStateChanged (bool){
this->replotIsRequired = true;
}

//------------------------------------------------------------------------------
//
void QECorrelation::setArchiveData (const QObject* userData, const bool isOkay,
Expand Down
7 changes: 6 additions & 1 deletion qeframeworkSup/project/widgets/QECorrelation/QECorrelation.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ class QE_FRAMEWORK_LIBRARY_SHARED_EXPORT QECorrelation : public QEAbstractDynami
double yMin;
double yMax;

double regressionSlope;
double regressionIntersept;
double correlationCoef;

enum PlotModes { pmDots = 1, pmLines };
enum ScaleModes { smDynamic, smManual, smData };

Expand All @@ -194,7 +198,7 @@ class QE_FRAMEWORK_LIBRARY_SHARED_EXPORT QECorrelation : public QEAbstractDynami

void setup ();
void setReadOut (const QString status);
double calculateCorrelationCoefficient ();
void calculateCorrelationAndRegression ();
void updateDataArrays ();
void reDrawPlane ();
void runSelectNameDialog (const int instance);
Expand Down Expand Up @@ -234,6 +238,7 @@ private slots:
void manualScaleClick (bool);
void plotModeSelect (bool);
void timeSelectButtonClick (bool);
void plotRegressionStateChanged (bool);
void numberSamplesEditChange (const double);
void sampleIntervalEditChange (const double);
};
Expand Down
115 changes: 110 additions & 5 deletions qeframeworkSup/project/widgets/QECorrelation/QECorrelation.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>672</width>
<height>536</height>
<height>562</height>
</rect>
</property>
<property name="minimumSize">
Expand Down Expand Up @@ -65,13 +65,13 @@
<property name="minimumSize">
<size>
<width>0</width>
<height>136</height>
<height>162</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>136</height>
<height>162</height>
</size>
</property>
<property name="font">
Expand Down Expand Up @@ -563,7 +563,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>92</y>
<y>118</y>
<width>660</width>
<height>20</height>
</rect>
Expand Down Expand Up @@ -659,7 +659,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>112</y>
<y>138</y>
<width>660</width>
<height>20</height>
</rect>
Expand Down Expand Up @@ -815,6 +815,111 @@
<normaloff>:/qe/stripchart/save_file.png</normaloff>:/qe/stripchart/save_file.png</iconset>
</property>
</widget>
<widget class="QLabel" name="Regression_Slope_Value_Label">
<property name="geometry">
<rect>
<x>120</x>
<y>95</y>
<width>84</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QLabel { background-color: rgb(224,224,224); } QLabel { color: rgb(0,0,0); }</string>
</property>
<property name="text">
<string>+99.7852</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="indent">
<number>6</number>
</property>
</widget>
<widget class="QLabel" name="Label7">
<property name="geometry">
<rect>
<x>4</x>
<y>95</y>
<width>108</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>8</pointsize>
</font>
</property>
<property name="styleSheet">
<string>QLabel { color: rgb(0,0,0); }</string>
</property>
<property name="text">
<string>Regression slope: </string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="Label8">
<property name="geometry">
<rect>
<x>216</x>
<y>95</y>
<width>108</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>8</pointsize>
</font>
</property>
<property name="styleSheet">
<string>QLabel { color: rgb(0,0,0); }</string>
</property>
<property name="text">
<string>Intercept: </string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="Regression_Intercept_Value_Label">
<property name="geometry">
<rect>
<x>292</x>
<y>95</y>
<width>84</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QLabel { background-color: rgb(224,224,224); } QLabel { color: rgb(0,0,0); }</string>
</property>
<property name="text">
<string>-1.2345</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="indent">
<number>6</number>
</property>
</widget>
<widget class="QCheckBox" name="Show_Regression_CheckBox">
<property name="geometry">
<rect>
<x>480</x>
<y>7</y>
<width>100</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string>Plot regression</string>
</property>
</widget>
</widget>
</item>
<item>
Expand Down