-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSWATGraphscript.py
More file actions
732 lines (697 loc) · 32.8 KB
/
Copy pathSWATGraphscript.py
File metadata and controls
732 lines (697 loc) · 32.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#import arcpy
from PyQt6.QtCore import QObject, QSettings, Qt, QSize, QRect, QCoreApplication, QMetaObject
from PyQt6.QtGui import QFont, QFontDatabase
from PyQt6.QtWidgets import QApplication, QFileDialog, QMessageBox, QTableWidgetItem, QSizePolicy, QWidget, \
QGridLayout, QPushButton, QDialog, QComboBox, QLabel, QVBoxLayout, QTextBrowser, QTableWidget, QAbstractItemView
import sys
import os
import csv
from datetime import datetime, timedelta
import math
import numpy as np
from numpy.polynomial import Polynomial
import locale
# import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
import mpl_toolkits.axes_grid1.axes_size as Size
# from matplotlib.figure import Figure
from matplotlib.backends.backend_qtagg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
import traceback
#arcpy.SetupDebugger()
# basic matplotlib colours
colours = ['b', 'g', 'r', 'm', 'y', 'c', 'k']
class Ui_GraphDlg(object):
def setupUi(self, GraphDlg):
GraphDlg.setObjectName("GraphDlg")
GraphDlg.resize(1153, 590)
# icon = QtGui.QIcon()
# icon.addPixmap(QtGui.QPixmap(":/plugins/QSWATPlus/SWATPlus32.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
# GraphDlg.setWindowIcon(icon)
GraphDlg.setSizeGripEnabled(True)
self.gridLayout = QGridLayout(GraphDlg)
self.gridLayout.setObjectName("gridLayout")
self.widget = QWidget(GraphDlg)
sizePolicy = QSizePolicy()
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy)
self.widget.setMinimumSize(QSize(201, 251))
self.widget.setFocusPolicy(Qt.FocusPolicy.WheelFocus)
self.widget.setObjectName("widget")
self.newFile = QPushButton(self.widget)
self.newFile.setGeometry(QRect(20, 140, 81, 41))
self.newFile.setObjectName("newFile")
self.closeForm = QPushButton(self.widget)
self.closeForm.setGeometry(QRect(120, 150, 75, 23))
self.closeForm.setObjectName("closeForm")
self.lineOrBar = QComboBox(self.widget)
self.lineOrBar.setGeometry(QRect(20, 90, 90, 20))
self.lineOrBar.setMinimumSize(QSize(90, 0))
self.lineOrBar.setMaxVisibleItems(2)
self.lineOrBar.setObjectName("lineOrBar")
self.chartLabel = QLabel(self.widget)
self.chartLabel.setGeometry(QRect(20, 70, 82, 16))
self.chartLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.chartLabel.setObjectName("chartLabel")
self.updateButton = QPushButton(self.widget)
self.updateButton.setGeometry(QRect(120, 90, 75, 23))
self.updateButton.setObjectName("updateButton")
self.plotType = QComboBox(self.widget)
self.plotType.setGeometry(QRect(20, 30, 171, 22))
self.plotType.setObjectName("plotType")
self.label_2 = QLabel(self.widget)
self.label_2.setGeometry(QRect(26, 10, 161, 20))
#self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.widget, 3, 2, 2, 1)
self.graph = QWidget(GraphDlg)
sizePolicy = QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.graph.sizePolicy().hasHeightForWidth())
self.graph.setSizePolicy(sizePolicy)
self.graph.setObjectName("graph")
self.graphvl = QVBoxLayout(self.graph)
self.graphvl.setObjectName("graphvl")
self.gridLayout.addWidget(self.graph, 2, 1, 1, 2)
self.coeffs = QTextBrowser(GraphDlg)
sizePolicy = QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.coeffs.sizePolicy().hasHeightForWidth())
self.coeffs.setSizePolicy(sizePolicy)
self.coeffs.setObjectName("coeffs")
self.gridLayout.addWidget(self.coeffs, 4, 1, 1, 1)
self.table = QTableWidget(GraphDlg)
sizePolicy = QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.table.sizePolicy().hasHeightForWidth())
self.table.setSizePolicy(sizePolicy)
self.table.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
self.table.setAlternatingRowColors(True)
self.table.setObjectName("table")
self.table.setColumnCount(0)
self.table.setRowCount(0)
self.table.verticalHeader().setVisible(False)
self.gridLayout.addWidget(self.table, 3, 1, 1, 1)
self.retranslateUi(GraphDlg)
QMetaObject.connectSlotsByName(GraphDlg)
def retranslateUi(self, GraphDlg):
_translate = QCoreApplication.translate
GraphDlg.setWindowTitle(_translate("GraphDlg", "SWATGraph"))
self.newFile.setText(_translate("GraphDlg", "New File\n"
"to Plot"))
self.closeForm.setText(_translate("GraphDlg", "Close"))
self.chartLabel.setText(_translate("GraphDlg", "Chart Type"))
self.updateButton.setText(_translate("GraphDlg", "Update"))
self.label_2.setText(_translate("GraphDlg", "Plot type"))
class GraphDialog(QDialog, Ui_GraphDlg):
"""Set up dialog from designer."""
def __init__(self, parent=None):
"""Constructor."""
super(GraphDialog, self).__init__(parent)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
class SWATGraph(QObject):
"""Display SWAT result data as line graph, bar chart, flow duration curve, scatter plot or box plot."""
def __init__(self, csvFile, plotType, fontSize=None):
"""Initialise class variables."""
QObject.__init__(self)
self._dlg = GraphDialog()
self._dlg.setWindowFlags(self._dlg.windowFlags() & ~Qt.WindowType.WindowContextHelpButtonHint)
## csv file of results
self.csvFile = csvFile
## Plot type: 1 for line graph or bar chart, 2 flor flow duration curve, 3 for scatter plot, 4 for box plot
self.plotType = plotType
## canvas for displaying matplotlib figure
self.canvas = None
## matplotlib tool bar
self.toolbar = None
## matplotlib axes
self.ax1 = None
## matplotlib figure
self.fig = None
## font size for plotting
self.fontSize = 9 if fontSize is None else fontSize
def run(self):
"""Initialise form and run on initial csv file."""
print('Running main')
self._dlg.plotType.clear()
self._dlg.plotType.addItem('Line graph/bar chart')
self._dlg.plotType.addItem('Flow/load duration curve')
self._dlg.plotType.addItem('Scatter plot')
self._dlg.plotType.addItem('Box plot')
if self.plotType in range(1,5):
self._dlg.plotType.setCurrentIndex(self.plotType - 1)
if self.plotType == 3: # scatter plot
# increase the height from default 600 to 900
size = self._dlg.size()
newHeight = int(size.height() * 1.5)
self._dlg.resize(size.width(), newHeight)
self._dlg.chartLabel.setVisible(self.plotType==1)
self._dlg.lineOrBar.setVisible(self.plotType==1)
self._dlg.lineOrBar.clear()
self._dlg.lineOrBar.addItem('Line graph')
self._dlg.lineOrBar.addItem('Bar chart')
self._dlg.lineOrBar.setCurrentIndex(0)
self._dlg.plotType.currentIndexChanged.connect(self.updateGraph)
self._dlg.newFile.clicked.connect(self.getCsv)
self._dlg.updateButton.clicked.connect(self.updateGraph)
self._dlg.closeForm.clicked.connect(self.closeFun)
#self.setUbuntuFont()
print('Reading csv')
self.readCsv()
print('csv read. Opening dialog')
self._dlg.exec()
def addmpl(self):
"""Add graph defined in self.fig."""
self.canvas = FigureCanvas(self.fig)
# graphvl is the QVBoxLayout instance added to the graph widget.
# Needed to make self.fig expand to fill graph widget.
self._dlg.graphvl.addWidget(self.canvas)
self.canvas.draw()
self.toolbar = NavigationToolbar(self.canvas,
self._dlg.graph, coordinates=True)
self._dlg.graphvl.addWidget(self.toolbar)
def rmmpl(self):
"""Remove current graph if any."""
try:
if self.canvas is not None:
self._dlg.graphvl.removeWidget(self.canvas)
self.canvas.close()
if self.toolbar is not None:
self._dlg.graphvl.removeWidget(self.toolbar)
self.toolbar.close()
self.ax1 = None
return
except Exception:
# no problem = may not have been a graph
return
@staticmethod
def trans(msg):
"""Translate message."""
return QApplication.translate("SWATGraph", msg)
@staticmethod
def error(msg):
"""Report msg as an error."""
msgbox = QMessageBox()
msgbox.setWindowTitle('SWATGraph')
msgbox.setIcon(QMessageBox.Icon.Critical)
msgbox.setText(SWATGraph.trans(msg))
msgbox.exec()
return
def getCsv(self):
"""Ask user for csv file."""
settings = QSettings()
if settings.contains('/QSWATPlus/LastInputPath'):
path = str(settings.value('/QSWATPlus/LastInputPath'))
else:
path = ''
filtr = self.trans('CSV files (*.csv);;All files (*.*)')
csvFile, _ = QFileDialog.getOpenFileName(None, 'Open csv file', path, filtr)
if csvFile is not None and csvFile != '':
settings.setValue('/QSWATPlus/LastInputPath', os.path.dirname(str(csvFile)))
self.csvFile = csvFile
self.readCsv()
def readCsv(self):
"""Read current csv file (if any)."""
# csvFile may be none if run from command line
if not self.csvFile or self.csvFile == '':
return
if not os.path.exists(self.csvFile):
self.error('Error: Cannot find csv file {0}'.format(self.csvFile))
return
"""Read csv file into table; create statistics (coefficients); draw graph."""
# clear graph
self.rmmpl()
# clear table
self._dlg.table.clear()
for i in range(self._dlg.table.columnCount()-1, -1, -1):
self._dlg.table.removeColumn(i)
self._dlg.table.setColumnCount(0)
self._dlg.table.setRowCount(0)
row = 0
numCols = 0
with open(self.csvFile, 'r', newline='') as csvFil:
reader = csv.reader(csvFil)
for line in reader:
try:
# use headers in first line
if row == 0:
numCols = len(line)
for i in range(numCols):
self._dlg.table.insertColumn(i)
self._dlg.table.setHorizontalHeaderLabels(line)
else:
self._dlg.table.insertRow(row-1)
for i in range(numCols):
try:
val = line[i].strip()
except Exception:
self.error('Error: could not read file {0} at line {1} column {2}: {3}'.format(self.csvFile, row+1, i+1, traceback.format_exc()))
return
item = QTableWidgetItem(val)
self._dlg.table.setItem(row-1, i, item)
row = row + 1
except Exception:
self.error('Error: could not read file {0} at line {1}: {2}'.format(self.csvFile, row+1, traceback.format_exc()))
return
if row == 1:
self.error('There is no data to plot in {0}'.format(self.csvFile))
return
# columns are too narrow for headings
self._dlg.table.resizeColumnsToContents()
# rows are too widely spaced vertically
self._dlg.table.resizeRowsToContents()
self.writeStats()
self.updateGraph()
@staticmethod
def makeFloat(s):
"""Parse string s as float and return; return nan on failure."""
try:
return float(s)
except Exception:
return float('nan')
def updateGraph(self):
"""Redraw graph according to current plotType and lineOrBar setting."""
self.plotType = self._dlg.plotType.currentIndex() + 1
self._dlg.chartLabel.setVisible(self.plotType==1)
self._dlg.lineOrBar.setVisible(self.plotType==1)
style = 'bar' if self._dlg.lineOrBar.currentText() == 'Bar chart' else 'line'
self.drawGraph(style)
@staticmethod
def shiftDates(dates, shift):
"""Add shift (number of days) to each date in dates."""
delta = timedelta(days=shift)
return [x + delta for x in dates]
@staticmethod
def getDateFormat(date):
"""
Return date strptime format string from example date, plus basic width for drawing bar charts.
Basic width is how matplotlib divides the date axis: number of days in the time unit
Assumes date has one of 3 formats:
yyyy: annual: return %Y and 12
yyyy/m or yyyy/mm: monthly: return %Y/%m and 30
yyyyddd: daily: return %Y%j and 24
"""
if date.find('/') > 0:
return '%Y/%m', 30
length = len(date)
if length == 4:
return '%Y', 365
if length == 7:
return '%Y%j', 1
SWATGraph.error('Cannot parse date {0}'.format(date))
return '', 1
def drawGraph(self, style):
"""Draw graph as line or bar chart according to style."""
# preserve title and xlabel if they exist
# in order to replace them when updating graph
try:
title = self.ax1.get_title()
except Exception:
title = ''
self.rmmpl()
self.fig, self.ax1 = plt.subplots()
mpl.rcParams['font.size'] = self.fontSize
self.fig.subplots_adjust(left=0.05)
self.fig.subplots_adjust(right=0.95)
if self.plotType not in {3,4} : # no legend with scatter or box plot, otherwise make space below
self.fig.subplots_adjust(bottom=0.3)
tkw = dict(size=4, width=1.5)
plots = []
if self.plotType == 1: # line graph or bar chart
colToTwin, twins = self.makeYAxes()
#print('colToTwin: {0}'.format(colToTwin))
#print('Twins: {0}'.format(twins.keys()))
numPlots = self._dlg.table.columnCount() - 1
rng = range(self._dlg.table.rowCount())
fmt, widthBase = self.getDateFormat(str(self._dlg.table.item(0, 0).text()).strip())
if fmt == '':
# could not parse
return
xVals = [datetime.strptime(str(self._dlg.table.item(i, 0).text()).strip(), fmt) for i in rng]
for col in range(1, numPlots+1):
yVals = [self.makeFloat(self._dlg.table.item(i, col).text()) for i in rng]
colour = self.getColour(col)
h = self._dlg.table.horizontalHeaderItem(col).text()
indx = colToTwin.get(col, -1)
if indx < 0: # axis on left
if not 'observed' in h:
self.ax1.set_ylabel(h.split('-')[3])
self.ax1.yaxis.label.set_color(colour)
self.ax1.yaxis.label.set_fontsize(self.fontSize)
self.ax1.tick_params(axis='y', colors=colour, **tkw)
self.ax1.tick_params(axis='x', labelsize=self.fontSize, **tkw)
else:
twins[indx].set_ylabel(h.split('-')[3])
twins[indx].yaxis.label.set_color(colour)
twins[indx].yaxis.label.set_fontsize(self.fontSize)
twins[indx].tick_params(axis='y', colors=colour, **tkw)
if style == 'line':
if indx < 0: # axis on left
p, = self.ax1.plot(xVals, yVals, colour, label=h)
else:
p, = twins[indx].plot(xVals, yVals, colour, label=h)
#print('Ylim for index {0}: {1}'.format(indx, twins[indx].get_ylim()))
else:
# width of bars in days.
# adding 1 to divisor gives space of size width between each date's group
width = float(widthBase) / (numPlots+1)
mid = numPlots / 2
shift = width * (col - 1 - mid)
xValsShifted = xVals if shift == 0 else self.shiftDates(xVals, shift)
if indx < 0: # axis on left
p = self.ax1.bar(xValsShifted, yVals, width, color=colour, linewidth=0, label=h)
else:
p = twins[indx].bar(xValsShifted, yVals, width, color=colour, linewidth=0, label=h)
plots.append(p)
elif self.plotType == 2: # flow duration curve
colToTwin, twins = self.makeYAxes()
numPlots = self._dlg.table.columnCount() - 1
timeLen = self._dlg.table.rowCount()
rng = range(timeLen)
exceedence = np.arange(1.0, timeLen + 1) / timeLen
exceedence *= 100
for col in range(1, numPlots+1):
yVals = sorted([self.makeFloat(self._dlg.table.item(i, col).text()) for i in rng], reverse=True)
colour = self.getColour(col)
h = self._dlg.table.horizontalHeaderItem(col).text()
indx = colToTwin.get(col, -1)
if indx < 0: # axis on left
p, = self.ax1.plot(exceedence, yVals, colour, label=h)
if not 'observed' in h:
self.ax1.set_ylabel(h.split('-')[3])
self.ax1.yaxis.label.set_color(colour)
self.ax1.tick_params(axis='y', colors=colour, **tkw)
self.ax1.tick_params(axis='x', **tkw)
else:
p, = twins[indx].plot(exceedence, yVals, colour, label=h)
twins[indx].set_ylabel(h.split('-')[3])
twins[indx].yaxis.label.set_color(colour)
twins[indx].tick_params(axis='y', colors=colour, **tkw)
plots.append(p)
elif self.plotType == 3: # scatter plot
numPlots = self._dlg.table.columnCount()
if numPlots >= 3:
rowCount = self._dlg.table.rowCount()
rng = range(rowCount)
xVals = np.array([self.makeFloat(self._dlg.table.item(i, 1).text()) for i in rng])
yVals = np.array([self.makeFloat(self._dlg.table.item(i, 2).text()) for i in rng])
self.ax1.scatter(xVals, yVals, marker=".")
# Fit linear regression via least squares with numpy.polyfit
# It returns intercept (a) and slope (b)
# deg=1 means linear fit (i.e. polynomial of degree 1)
# use convert to retain unscaled domain
a, b = Polynomial.fit(xVals, yVals, deg=1).convert().coef
# Create sequence of 2 numbers from min to max: only need 2 for straight line
xseq = np.linspace(np.nanmin(xVals), np.nanmax(xVals), num=2)
#print('Minimum {0:.2F} and maximum {1:.2F}; intercept {2:.2F}, slope {3:.2F}'.format(np.amin(xVals), np.amax(xVals), a, b))
# Plot regression line
self.ax1.plot(xseq, a + b * xseq, color="k", lw=1);
elif self.plotType == 4: # box plot
variables, colToIndex, count = self.organiseBoxSubplots()
#print('variables: {0}'.format(variables))
#print('colToIndex: {0}'.format(colToIndex))
#print('count: {0}'.format(count))
numPlots = len(count)
rng = range(self._dlg.table.rowCount())
gs_kw = dict(width_ratios=[count[index] for index in count], height_ratios=[1])
self.fig, axs = plt.subplots(1, numPlots, gridspec_kw=gs_kw)
self.fig.subplots_adjust(left=0.05)
self.fig.subplots_adjust(right=0.95)
llDict = dict() # list of list of values for each index
labelsDict = dict() # list of labels for each index
for col in range(1, self._dlg.table.columnCount()):
if col not in colToIndex:
# allow for observed in first column error
continue
index = colToIndex[col]
vals = [self.makeFloat(self._dlg.table.item(i, col).text()) for i in rng]
val2 = sorted([v for v in vals if not math.isnan(v)])
minn = val2[0]
maxx = val2[-1]
median = SWATGraph.percentile(val2, 0.5)
Q1 = SWATGraph.percentile(val2, 0.25)
Q3 = SWATGraph.percentile(val2, 0.75)
msg = ('{0}: min: {1:.2F}, max: {2:.2F}, median: {3:.2F}, Q1: {4:.2F}, Q3: {5:.2F}'.format(self._dlg.table.horizontalHeaderItem(col).text(), minn, maxx, median, Q1, Q3))
self._dlg.coeffs.append(SWATGraph.trans(msg))
ll = llDict.setdefault(index, [])
ll.append(vals)
labels = labelsDict.setdefault(index, [])
labels.append(self._dlg.table.horizontalHeaderItem(col).text())
for indx, ll in llDict.items():
# convert inputs to numpy 2D array
npArray = np.asarray(ll).T
# axs is not an array if only 1 subplot
ax = axs[indx] if len(count) > 1 else axs
p = ax.boxplot(npArray, tick_labels=labelsDict[indx])
ax.grid(True)
#SWATGraph.colourBoxplot(p, self.getColour(indx))
# cannot get this to give other than a very spread layout with narrow boxplots
#plt.tight_layout(w_pad=-0.5)
# reinstate title and labels
if title != '':
self.ax1.set_title(title)
if self.plotType == 1: # line or bar
self.ax1.set_xlabel('Date', fontsize=self.fontSize)
elif self.plotType == 2: # flow duration
self.ax1.set_xlabel('Exceedance (%)', fontsize=self.fontSize)
elif self.plotType == 3 and self._dlg.table.columnCount() >= 3: # scatter plot
self.ax1.set_xlabel(self._dlg.table.horizontalHeaderItem(1).text(), fontsize=self.fontSize)
self.ax1.set_ylabel(self._dlg.table.horizontalHeaderItem(2).text(), fontsize=self.fontSize)
if self.plotType != 4:
self.ax1.grid(True)
if self.plotType in {1,2}: # line or bar, or flow duration
legendCols = min(7, self._dlg.table.columnCount() - 1)
self.ax1.legend(handles=plots, bbox_to_anchor=(1.0 + 0.07 * len(twins), -0.3), ncol=legendCols, fontsize='small')
self.addmpl()
def makeYAxes(self):
"""Make extra y-axes on right if more than one variable is used."""
variables = []
colToTwin = dict() # map of column number in table to twin index used for extra y-axis
twins = dict() # map of twin index to twinx
for col in range(1, self._dlg.table.columnCount()):
h = self._dlg.table.horizontalHeaderItem(col).text()
if 'observed' in h:
# assume matches previous variable
continue
var = h.split('-')[3]
try:
indx = variables.index(var)
if indx > 0: # indx zero means we use first y-axis, on left
colToTwin[col] = indx
except: # var not in variables
variables.append(var)
if len(variables) > 1:
colToTwin[col] = len(variables) - 1
if len(variables) > 1:
numAxes = len(variables) - 1
adj = 0.95 - 0.05 * numAxes
#print('Adjustment: {0}'.format(adj))
self.fig.subplots_adjust(right=adj)
for i in range(1, numAxes + 1):
twins[i] = self.ax1.twinx()
if i > 1:
# offset the second and later right spines
# spines.right syntax only in matplotlib >= 3.4.0
twins[i].spines['right'].set_position(("axes", 1 + 0.1 * (i - 1)))
twins[i].set_ylabel(var)
return colToTwin, twins
def organiseBoxSubplots(self):
"""Make a collection of indexes of box subplots by collecting boxplots sharing the same variable."""
variables = dict() # map of variable name to index
colToIndex = dict() # map of column number to index
count = dict() # count of boxes for each index
nextIndex = 0
for col in range(1, self._dlg.table.columnCount()):
h = self._dlg.table.horizontalHeaderItem(col).text()
if 'observed' in h:
# assume it belongs with previous column in table
if col == 1:
SWATGraph.error('Observed values must immediately follow a column assumed to have the same type of values. Ignoring the observed column.')
continue
else:
index = colToIndex[col - 1]
colToIndex[col] = index
variables['observed'] = index
count[index] += 1
else:
var = h.split('-')[3]
index = variables.get(var, -1)
if index < 0:
variables[var] = nextIndex
colToIndex[col] = nextIndex
count[nextIndex] = 1
nextIndex += 1
else:
colToIndex[col] = index
count[index] += 1
return variables, colToIndex, count
@staticmethod
def percentile(N, percent):
"""
Find the percentile of a sorted list of values.
N - is a list of values. Note N MUST BE already sorted.
percent - a float value from 0.0 to 1.0.
return - the percentile of the values
"""
if not N:
return None
k = (len(N)-1) * percent
f = math.floor(k)
c = math.ceil(k)
if f == c:
return N[int(k)]
d0 = N[int(f)] * (c-k)
d1 = N[int(c)] * (k-f)
return d0+d1
@staticmethod
def make_heights_equal(fig, rect, ax1, ax2, pad):
# pad in inches
divider = HBoxDivider(
fig, rect,
horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)],
vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)])
ax1.set_axes_locator(divider.new_locator(0))
ax2.set_axes_locator(divider.new_locator(2))
def getColour(self, col):
"""Colour to use for coloumn in table."""
# column indexess run 1 to n, since first is date
# cannot imagine more than 7 (numColours), but use shades of grey if necessary
numColours = len(colours)
return colours[col-1] if col <= len(colours) else str(float((col - numColours)/(self._dlg.table.columnCount() - numColours)))
@staticmethod
def colourBoxplot(p, colour):
"""Colour component lines of boxplot."""
for l in p['boxes']:
l.set(color=colour)
for l in p['medians']:
l.set(color=colour)
for l in p['whiskers']:
l.set(color=colour)
for l in p['caps']:
l.set(color=colour)
def closeFun(self):
"""Close dialog."""
self._dlg.close()
def writeStats(self):
"""Write Pearson and Nash coefficients."""
numCols = self._dlg.table.columnCount()
numRows = self._dlg.table.rowCount()
self._dlg.coeffs.clear()
for i in range(1, numCols):
for j in range(i+1, numCols):
self.pearson(i, j, numRows)
for i in range(1, numCols - 1):
# only compute NSE for pairs where second is observed
h = self._dlg.table.horizontalHeaderItem(i+1).text()
if 'observed' in h:
self.nash(i+1, i, numRows)
def multiSums(self, idx1, idx2, N):
"""Return various sums for two series, only including points where both are numbers, plus count of such values."""
s1 = 0
s2 = 0
s11 = 0
s22 = 0
s12 = 0
count = 0
for i in range(N):
val1 = self.makeFloat(self._dlg.table.item(i, idx1).text())
val2 = self.makeFloat(self._dlg.table.item(i, idx2).text())
# ignore missing values
if not (math.isnan(val1) or math.isnan(val2)):
s1 += val1
s2 += val2
s11 += val1 * val1
s22 += val2 * val2
s12 += val1 * val2
count = count + 1
return (s1, s2, s11, s22, s12, count)
def sum1(self, idx1, idx2, N):
"""Return sum for series1, only including points where both are numbers, plus count of such values."""
s1 = 0
count = 0
for i in range(N):
val1 = self.makeFloat(self._dlg.table.item(i, idx1).text())
val2 = self.makeFloat(self._dlg.table.item(i, idx2).text())
# ignore missing values
if not (math.isnan(val1) or math.isnan(val2)):
s1 += val1
count = count + 1
return (s1, count)
def pearson(self, idx1, idx2, N):
"""Calculate and display R2 and Pearson correlation coefficients for pair of plots."""
s1, s2, s11, s22, s12, count = self.multiSums(idx1, idx2, N)
if count == 0: return
sqx = (count * s11) - (s1 * s1)
sqy = (count * s22) - (s2 * s2)
sxy = (count * s12) - (s1 * s2)
deno = math.sqrt(sqx * sqy)
if deno == 0: return
rho = sxy / deno
if count < N:
extra = ' (using {0!s} of {1!s} values)'.format(count , N)
else:
extra = ''
msg = 'Series1: ' + self._dlg.table.horizontalHeaderItem(idx1).text() + \
' Series2: ' + self._dlg.table.horizontalHeaderItem(idx2).text() + ' R2 = {0:.2f} (Pearson Correlation Coefficient = {1:.2f}){2}'.format(rho * rho, rho, extra)
self._dlg.coeffs.append(SWATGraph.trans(msg))
def nash(self, idx1, idx2, N):
"""Calculate and display Nash-Sutcliffe efficiency coefficients for pair of plots."""
s1, count = self.sum1(idx1, idx2, N)
if count == 0: return
mean = s1 / count
num = 0
deno = 0
for i in range(N):
val1 = self.makeFloat(self._dlg.table.item(i, idx1).text())
val2 = self.makeFloat(self._dlg.table.item(i, idx2).text())
# ignore missing values
if not (math.isnan(val1) or math.isnan(val2)):
diff12 = val1 - val2
diff1m = val1 - mean
num += diff12 * diff12
deno += diff1m * diff1m
if deno == 0: return
result = 1 - (num / deno)
if count < N:
extra = ' (using {0!s} of {1!s} values)'.format(count , N)
else:
extra = ''
msg = 'Series1: ' + self._dlg.table.horizontalHeaderItem(idx1).text() + \
' Series2: ' + self._dlg.table.horizontalHeaderItem(idx2).text() + ' Nash-Sutcliffe Efficiency Coefficient = {0:.2f}{1}'.format(result, extra)
self._dlg.coeffs.append(SWATGraph.trans(msg))
# def setUbuntuFont(self):
# """Set Ubuntu font."""
# QFontDatabase.addApplicationFont(":/fonts/Ubuntu-R.ttf")
# ufont = QFont("Ubuntu", 10, 1)
# QApplication.setFont(ufont)
def script_tool(csvFile, plotType):
"""Script code goes below"""
## main program
main = SWATGraph(csvFile, plotType, fontSize=9)
#print('SWATGraph initialised')
main.run()
if __name__ == "__main__":
app = QApplication(sys.argv)
if len(sys.argv) < 3:
SWATGraph.error('You need to provide a path to a csv data file and a plot type (integer 1-4)')
exit()
csvFile = sys.argv[1]
plotType = int(sys.argv[2])
#csvFile = arcpy.GetParameterAsText(0)
#plotType = int(arcpy.GetParameterAsText(1))
script_tool(csvFile, plotType)
# arcpy.SetParameterAsText(2, "Result")
exit()