From 8e732fac22182f2501d8ef55d16aa08ad2912aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Mon, 28 Jun 2021 13:51:40 -0400 Subject: [PATCH 1/5] Use hydrological year and GLUE50 to compute yearly avg. values --- gwhat/gwrecharge/gwrecharge_plot_results.py | 39 ++++++++++++--------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index a66d6667..5031c7bf 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -1426,23 +1426,29 @@ def __init__(self, setp={}): self.setp['ylabel size'] = 16 def __plot__(self, glue_df): - avg_yrly = { - 'evapo': np.nanmean(glue_df['yearly budget']['evapo']), - 'runoff': np.nanmean(glue_df['yearly budget']['runoff']), - 'recharge': np.nanmean(glue_df['yearly budget']['recharge']), - 'precip': np.nanmean(glue_df['yearly budget']['precip'])} + glue50_avg_yrly = { + 'evapo': np.nanmean( + glue_df['hydrol yearly budget']['evapo'][:, 2]), + 'runoff': np.nanmean( + glue_df['hydrol yearly budget']['runoff'][:, 2]), + 'recharge': np.nanmean( + glue_df['hydrol yearly budget']['recharge'][:, 2]), + 'precip': np.nanmean(glue_df['hydrol yearly budget']['precip']) + } # Plot the results. offset = mpl.transforms.ScaledTranslation( 0, 3/72, self.figure.dpi_scale_trans) self.bar_handles = [] self.notes = [] - for i, varname in enumerate(avg_yrly.keys()): - l, = self.ax0.bar(i+1, avg_yrly[varname], 0.65, align='center', - color=COLORS[varname]) + for i, varname in enumerate(glue50_avg_yrly.keys()): + l, = self.ax0.bar( + i + 1, glue50_avg_yrly[varname], 0.65, align='center', + color=COLORS[varname]) self.bar_handles.append(l) self.notes.append(self.ax0.text( - i+1, avg_yrly[varname], "%d" % avg_yrly[varname], + i + 1, glue50_avg_yrly[varname], + "%d" % glue50_avg_yrly[varname], ha='center', va='bottom', transform=self.ax0.transData + offset, fontsize=self.setp['notes size'])) @@ -1452,8 +1458,8 @@ def __plot__(self, glue_df): self.setp['ymin'] = 0 if 'ymax' not in self.setp.keys(): self.setp['ymax'] = np.max([ - avg_yrly['recharge'], avg_yrly['evapo'], - avg_yrly['runoff'], avg_yrly['precip'] + glue50_avg_yrly['recharge'], glue50_avg_yrly['evapo'], + glue50_avg_yrly['runoff'], glue50_avg_yrly['precip'] ]) + 100 if 'yscl' not in self.setp.keys(): self.setp['yscl'] = 250 @@ -1535,7 +1541,7 @@ def __init__(self, setp={}): def __plot__(self, glue_df): """Plot the results.""" - avg_mly = { + glue50_avg_mly = { 'evapo': np.nanmean( glue_df['monthly budget']['evapo'][:, :, 2], axis=0), 'runoff': np.nanmean( @@ -1547,9 +1553,9 @@ def __plot__(self, glue_df): # Plot the results. months = np.arange(1, 13) - for i, varname in enumerate(avg_mly.keys()): + for i, varname in enumerate(glue50_avg_mly.keys()): hl, = self.ax0.plot( - months, avg_mly[varname], marker='o', mec='white', + months, glue50_avg_mly[varname], marker='o', mec='white', clip_on=False, lw=2, color=COLORS[varname], zorder=3) self.lg_handles.append(hl) @@ -1557,7 +1563,8 @@ def __plot__(self, glue_df): if 'ymin' not in self.setp.keys(): self.setp['ymin'] = 0 if 'ymax' not in self.setp.keys(): - self.setp['ymax'] = np.max([x for x in avg_mly.values()]) + 10 + self.setp['ymax'] = np.max( + [x for x in glue50_avg_mly.values()]) + 10 if 'yscl' not in self.setp.keys(): self.setp['yscl'] = 50 if 'yscl minor' not in self.setp.keys(): @@ -1630,8 +1637,6 @@ def setup_legend(self): legend.draw_frame(False) -# %% ---- if __name__ == '__main__' - if __name__ == '__main__': from gwhat.gwrecharge.gwrecharge_calc2 import load_glue_from_npy from gwhat.gwrecharge.glue import GLUEDataFrame From 148083e65fc677f7b9bb1c9177869667b8dcfcce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Tue, 29 Jun 2021 15:52:23 -0400 Subject: [PATCH 2/5] Specify GLUE50 in figure tabs --- gwhat/gwrecharge/gwrecharge_plot_results.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index 5031c7bf..31efffb0 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -101,9 +101,9 @@ def setup_stack(self): self.stack = QTabWidget() self.stack.addTab(fig_rechg_glue, 'Recharge') - self.stack.addTab(fig_watbudg_glue, 'Yearly Budget') - self.stack.addTab(fig_avg_yearly_budg, 'Yearly Avg. Budget') - self.stack.addTab(fig_avg_monthly_budg, 'Monthly Avg. Budget') + self.stack.addTab(fig_watbudg_glue, 'Yearly Budget (GLUE50)') + self.stack.addTab(fig_avg_yearly_budg, 'Yearly Avg. Budget (GLUE50)') + self.stack.addTab(fig_avg_monthly_budg, 'Monthly Avg. Budget (GLUE50)') self.stack.currentChanged.connect(self.plot_results) def set_gluedf(self, gluedf): From f6bf890566db7c8e8a62d153d2539340786cb08b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Tue, 22 Feb 2022 15:00:51 -0500 Subject: [PATCH 3/5] Minor codestyle change --- gwhat/gwrecharge/gwrecharge_plot_results.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gwhat/gwrecharge/gwrecharge_plot_results.py b/gwhat/gwrecharge/gwrecharge_plot_results.py index 7c57b0b9..b2b2a5ed 100644 --- a/gwhat/gwrecharge/gwrecharge_plot_results.py +++ b/gwhat/gwrecharge/gwrecharge_plot_results.py @@ -1450,7 +1450,7 @@ def __plot__(self, glue_df): self.bar_handles.append(l) self.notes.append(self.ax0.text( i + 1, glue50_avg_yrly[varname], - "%d" % glue50_avg_yrly[varname], + '{:0.0f}'.format(glue50_avg_yrly[varname]), ha='center', va='bottom', transform=self.ax0.transData + offset, fontsize=self.setp['notes size'])) From f34e8cd96bbb5a22749247d28f9d7d5a8aa23e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Tue, 22 Feb 2022 15:01:18 -0500 Subject: [PATCH 4/5] Add test_hydrological_budget_calculs --- .../gwrecharge/tests/test_gwrecharge_plot.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/gwhat/gwrecharge/tests/test_gwrecharge_plot.py b/gwhat/gwrecharge/tests/test_gwrecharge_plot.py index 1f1ae84e..fc70b70a 100644 --- a/gwhat/gwrecharge/tests/test_gwrecharge_plot.py +++ b/gwhat/gwrecharge/tests/test_gwrecharge_plot.py @@ -11,6 +11,7 @@ import os.path as osp # ---- Third party imports +import numpy as np import pytest from PyQt5.QtGui import QImage from PyQt5.QtCore import Qt @@ -62,6 +63,36 @@ def test_figstackmanager(figstackmanager, project): figstackmanager.stack.setCurrentIndex(index) +def test_hydrological_budget_calculs(figstackmanager, project): + """ + Test that the yearly values of the hydrological budget are calculated + as expected + """ + wldset = project.get_wldset('3040002_15min') + glue_df = wldset.get_glue_at(-1) + figstackmanager.set_gluedf(glue_df) + + # Assert the average yearly recharge values are calculated as expected in + # FigYearlyRechgGLUE. + figstackmanager.stack.setCurrentIndex(0) + figcanvas = figstackmanager.figmanagers[0].figcanvas + assert '(GLUE 5) 363 mm/y' in figcanvas.txt_yearly_avg.get_text() + assert '(GLUE 25) 415 mm/y' in figcanvas.txt_yearly_avg.get_text() + assert '(GLUE 50) 484 mm/y' in figcanvas.txt_yearly_avg.get_text() + assert '(GLUE 75) 578 mm/y' in figcanvas.txt_yearly_avg.get_text() + assert '(GLUE 95) 709 mm/y' in figcanvas.txt_yearly_avg.get_text() + + # Assert the values are calculated as expected of the average + # yearly water budget in FigAvgYearlyBudget. + figstackmanager.stack.setCurrentIndex(2) + figcanvas = figstackmanager.figmanagers[2].figcanvas + + assert figcanvas.notes[0].get_text() == '421' + assert figcanvas.notes[1].get_text() == '226' + assert figcanvas.notes[2].get_text() == '484' + assert figcanvas.notes[3].get_text() == '1131' + + def test_copy_to_clipboard(qtbot, figstackmanager, project): """ Test that copying figures to the clipboard works as expected. From f3e677c2b15400471975f143b6a92df9b69240a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Tue, 22 Feb 2022 15:04:08 -0500 Subject: [PATCH 5/5] Make sure a new project is used for each test in test_gwrecharge_plot --- gwhat/gwrecharge/tests/test_gwrecharge_plot.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/gwhat/gwrecharge/tests/test_gwrecharge_plot.py b/gwhat/gwrecharge/tests/test_gwrecharge_plot.py index fc70b70a..9507e6c1 100644 --- a/gwhat/gwrecharge/tests/test_gwrecharge_plot.py +++ b/gwhat/gwrecharge/tests/test_gwrecharge_plot.py @@ -9,6 +9,7 @@ # ---- Standard library imports import os.path as osp +from shutil import copyfile # ---- Third party imports import numpy as np @@ -28,11 +29,13 @@ # ---- Pytest Fixtures # ============================================================================= @pytest.fixture -def project(): - projectfile = osp.join( +def project(tmp_path): + fsrc = osp.join( __rootdir__, 'gwrecharge', 'tests', 'test_gwrecharge_project.gwt') - project = ProjetReader(projectfile) - return project + fdst = osp.join( + tmp_path, 'test_gwrecharge_project.gwt') + copyfile(fsrc, fdst) + return ProjetReader(fdst) @pytest.fixture()