|
@@ -61,10 +61,11 @@ except ImportError:
|
|
import wx.lib.filebrowsebutton as filebrowse
|
|
import wx.lib.filebrowsebutton as filebrowse
|
|
|
|
|
|
from gui_core.widgets import GNotebook
|
|
from gui_core.widgets import GNotebook
|
|
-from gui_core.wrap import TextCtrl, Button, StaticText
|
|
|
|
|
|
+from gui_core.wrap import CheckBox, TextCtrl, Button, StaticText
|
|
|
|
|
|
ALPHA = 0.5
|
|
ALPHA = 0.5
|
|
COLORS = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
|
|
COLORS = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
|
|
|
|
+LINEAR_REG_LINE_COLOR = (0.56, 0.00, 1.00)
|
|
|
|
|
|
|
|
|
|
def check_version(*version):
|
|
def check_version(*version):
|
|
@@ -204,6 +205,11 @@ class TplotFrame(wx.Frame):
|
|
|
|
|
|
self.coorval.SetToolTip(_("Coordinates can be obtained for example"
|
|
self.coorval.SetToolTip(_("Coordinates can be obtained for example"
|
|
" by right-clicking on Map Display."))
|
|
" by right-clicking on Map Display."))
|
|
|
|
+ self.linRegRaster = CheckBox(
|
|
|
|
+ parent=self.controlPanelRaster, id=wx.ID_ANY,
|
|
|
|
+ label=_('Show simple linear regression line'),
|
|
|
|
+ )
|
|
|
|
+
|
|
self.controlPanelSizerRaster = wx.BoxSizer(wx.VERTICAL)
|
|
self.controlPanelSizerRaster = wx.BoxSizer(wx.VERTICAL)
|
|
# self.controlPanelSizer.Add(wx.StaticText(self.panel, id=wx.ID_ANY,
|
|
# self.controlPanelSizer.Add(wx.StaticText(self.panel, id=wx.ID_ANY,
|
|
# label=_("Select space time raster dataset(s):")),
|
|
# label=_("Select space time raster dataset(s):")),
|
|
@@ -214,6 +220,7 @@ class TplotFrame(wx.Frame):
|
|
|
|
|
|
self.controlPanelSizerRaster.Add(self.coor, flag=wx.EXPAND)
|
|
self.controlPanelSizerRaster.Add(self.coor, flag=wx.EXPAND)
|
|
self.controlPanelSizerRaster.Add(self.coorval, flag=wx.EXPAND)
|
|
self.controlPanelSizerRaster.Add(self.coorval, flag=wx.EXPAND)
|
|
|
|
+ self.controlPanelSizerRaster.Add(self.linRegRaster, flag=wx.EXPAND)
|
|
|
|
|
|
self.controlPanelRaster.SetSizer(self.controlPanelSizerRaster)
|
|
self.controlPanelRaster.SetSizer(self.controlPanelSizerRaster)
|
|
self.controlPanelSizerRaster.Fit(self)
|
|
self.controlPanelSizerRaster.Fit(self)
|
|
@@ -253,6 +260,10 @@ class TplotFrame(wx.Frame):
|
|
self.catsLabel = StaticText(parent=self.controlPanelVector,
|
|
self.catsLabel = StaticText(parent=self.controlPanelVector,
|
|
id=wx.ID_ANY,
|
|
id=wx.ID_ANY,
|
|
label=_('Select category of vector(s)'))
|
|
label=_('Select category of vector(s)'))
|
|
|
|
+ self.linRegVector = CheckBox(
|
|
|
|
+ parent=self.controlPanelVector, id=wx.ID_ANY,
|
|
|
|
+ label=_('Show simple linear regression line'),
|
|
|
|
+ )
|
|
|
|
|
|
self.controlPanelSizerVector = wx.BoxSizer(wx.VERTICAL)
|
|
self.controlPanelSizerVector = wx.BoxSizer(wx.VERTICAL)
|
|
# self.controlPanelSizer.Add(wx.StaticText(self.panel, id=wx.ID_ANY,
|
|
# self.controlPanelSizer.Add(wx.StaticText(self.panel, id=wx.ID_ANY,
|
|
@@ -267,6 +278,7 @@ class TplotFrame(wx.Frame):
|
|
|
|
|
|
self.controlPanelSizerVector.Add(self.catsLabel, flag=wx.EXPAND)
|
|
self.controlPanelSizerVector.Add(self.catsLabel, flag=wx.EXPAND)
|
|
self.controlPanelSizerVector.Add(self.cats, flag=wx.EXPAND)
|
|
self.controlPanelSizerVector.Add(self.cats, flag=wx.EXPAND)
|
|
|
|
+ self.controlPanelSizerVector.Add(self.linRegVector, flag=wx.EXPAND)
|
|
|
|
|
|
self.controlPanelVector.SetSizer(self.controlPanelSizerVector)
|
|
self.controlPanelVector.SetSizer(self.controlPanelSizerVector)
|
|
self.controlPanelSizerVector.Fit(self)
|
|
self.controlPanelSizerVector.Fit(self)
|
|
@@ -659,6 +671,61 @@ class TplotFrame(wx.Frame):
|
|
writer.writerow(head)
|
|
writer.writerow(head)
|
|
writer.writerows(zipped)
|
|
writer.writerows(zipped)
|
|
|
|
|
|
|
|
+ def _calcSimpleLinReg(self, x, y, returnFormula=False):
|
|
|
|
+ """Calculate simple linear regression model
|
|
|
|
+ y = a + b*x (y is dependent variable, a is intercept, b is slope,
|
|
|
|
+ x is explanatory variable)
|
|
|
|
+
|
|
|
|
+ param numpy.array x: explanatory variable
|
|
|
|
+ param numpy.array y: dependent variable
|
|
|
|
+ param returnFormula bool: return calculated simple linear
|
|
|
|
+ regression formula too
|
|
|
|
+
|
|
|
|
+ return tuple or function:
|
|
|
|
+
|
|
|
|
+ tuple: (simple linear regression function model for dependent
|
|
|
|
+ variable, calculated simple linear regression formula model)
|
|
|
|
+
|
|
|
|
+ function: simple linear regression model function for dependent
|
|
|
|
+ variable
|
|
|
|
+ """
|
|
|
|
+ def predict(x1):
|
|
|
|
+ return a + b * x1
|
|
|
|
+ b = ((len(x) * np.sum(x*y) - np.sum(x) * np.sum(y)) /
|
|
|
|
+ (len(x) * np.sum(x*x) - np.sum(x) * np.sum(x)))
|
|
|
|
+ a = (np.sum(y) - b *np.sum(x)) / len(x)
|
|
|
|
+ if returnFormula:
|
|
|
|
+ return predict, "y = {a:.5f} + {b:.5f}*x".format(a=a, b=b)
|
|
|
|
+ return predict
|
|
|
|
+
|
|
|
|
+ def _drawSimpleLinRegLine(self, xdata, ydata):
|
|
|
|
+ """Draw simple regression line
|
|
|
|
+
|
|
|
|
+ :param list xdata: x axis data
|
|
|
|
+ :param list xdata: y axis data
|
|
|
|
+
|
|
|
|
+ return None
|
|
|
|
+ """
|
|
|
|
+ predict, regFormula = self._calcSimpleLinReg(
|
|
|
|
+ x=np.array(xdata), y=np.array(ydata),
|
|
|
|
+ returnFormula=True)
|
|
|
|
+
|
|
|
|
+ r2 = "r\u00B2 = {:.5f}".format(
|
|
|
|
+ np.corrcoef(np.array(xdata), np.array(ydata))[0, 1]**2)
|
|
|
|
+ self.plots.append(
|
|
|
|
+ self.axes2d.plot(
|
|
|
|
+ xdata,
|
|
|
|
+ predict(x1=np.array(xdata)),
|
|
|
|
+ color=LINEAR_REG_LINE_COLOR,
|
|
|
|
+ label="{reg}, {r2}".format(reg=regFormula, r2=r2))[0])
|
|
|
|
+
|
|
|
|
+ print(regFormula)
|
|
|
|
+ import platform
|
|
|
|
+ if platform.system() == 'Windows':
|
|
|
|
+ print(' ='.join(['r2'] + r2.split('=')[1:]))
|
|
|
|
+ else:
|
|
|
|
+ print(r2)
|
|
|
|
+
|
|
def drawR(self):
|
|
def drawR(self):
|
|
ycsv = []
|
|
ycsv = []
|
|
xcsv = []
|
|
xcsv = []
|
|
@@ -688,6 +755,10 @@ class TplotFrame(wx.Frame):
|
|
self.plots.append(self.axes2d.plot(xdata, ydata, marker='o',
|
|
self.plots.append(self.axes2d.plot(xdata, ydata, marker='o',
|
|
color=color,
|
|
color=color,
|
|
label=self.plotNameListR[i])[0])
|
|
label=self.plotNameListR[i])[0])
|
|
|
|
+
|
|
|
|
+ if self.linRegRaster.IsChecked():
|
|
|
|
+ self._drawSimpleLinRegLine(xdata=xdata, ydata=ydata)
|
|
|
|
+
|
|
if self.csvpath:
|
|
if self.csvpath:
|
|
ycsv.append(ydata)
|
|
ycsv.append(ydata)
|
|
|
|
|
|
@@ -740,6 +811,10 @@ class TplotFrame(wx.Frame):
|
|
marker='o',
|
|
marker='o',
|
|
color=color,
|
|
color=color,
|
|
label=labelname)[0])
|
|
label=labelname)[0])
|
|
|
|
+
|
|
|
|
+ if self.linRegVector.IsChecked():
|
|
|
|
+ self._drawSimpleLinRegLine(xdata=xdata, ydata=ydata)
|
|
|
|
+
|
|
if self.csvpath:
|
|
if self.csvpath:
|
|
ycsv.append(ydata)
|
|
ycsv.append(ydata)
|
|
|
|
|
|
@@ -780,6 +855,10 @@ class TplotFrame(wx.Frame):
|
|
|
|
|
|
self.plots.append(self.axes2d.plot(xdata, ydata, marker='o',
|
|
self.plots.append(self.axes2d.plot(xdata, ydata, marker='o',
|
|
color=color, label=name)[0])
|
|
color=color, label=name)[0])
|
|
|
|
+
|
|
|
|
+ if self.linRegVector.IsChecked():
|
|
|
|
+ self._drawSimpleLinRegLine(xdata=xdata, ydata=ydata)
|
|
|
|
+
|
|
if self.csvpath:
|
|
if self.csvpath:
|
|
ycsv.append(ydata)
|
|
ycsv.append(ydata)
|
|
|
|
|