Skip to content

Commit 6c88f6a

Browse files
committed
Add plot_results.py to generate simple matplotlib plot
1 parent 7b88ee2 commit 6c88f6a

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ __pycache__
55
.args.txt
66

77
# Some distribution files
8+
dist/*.tgz
89
dist/neuralhydrology_demo1/app/

dist/neuralhydrology_demo1/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,13 @@ options:
6969
7070
Note: You can also put arguments in .args.txt file
7171
```
72+
73+
## Plotting Results
74+
75+
The `plot_results.py` script is provided to generated simple plots of a trained model test results.
76+
The script requires matplotlib and xarray packages.
77+
The results for each run are written to a file named `test_results.nc` under the basin folder.
78+
An example of the path is
79+
`.../experiments/03164000/runs/run_0703_025058/test/model_epoch050/test_results.nc`
80+
so the corresponding command is
81+
`python3 plot_results.py .../experiments/03164000/runs/run_0703_025058/test/model_epoch050/test_results.nc`

neuralhydrology/package_demo1.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1-
"""Script to build Demo1 install package in dist directory"""
1+
"""Script to build Demo1 install package in dist directory
2+
3+
4+
To create tar file, be sure to remote any pyc files first.
5+
Because I cannot figure out how to do that in tar, do this:
6+
7+
cd dist
8+
rm -rf neuralhydrology_demo1/app/demo1/__pycache__/ && tar zcf neuralhydrology_demo1.tgz neuralhydrology_demo1/
9+
"""
210

311
import pathlib
412
import shutil
13+
import tarfile
514

615
source_dir = pathlib.Path(__file__).parent
716
dist_dir = source_dir.parent / 'dist/neuralhydrology_demo1'
817
app_dir = dist_dir / 'app'
918

19+
# Delete any existing tgz and app files
20+
tgz_file = source_dir / 'dist/neuralhydrology_demo1.tgz'
21+
tgz_file.unlink(missing_ok=True)
22+
shutil.rmtree(app_dir)
23+
1024
# Copy source files to app dir
1125
app_dir.mkdir(parents=True, exist_ok=True)
1226

13-
from_path = source_dir / 'demo1.py'
14-
shutil.copy2(from_path, app_dir)
27+
filenames = ['demo1.py', 'plot_results.py']
28+
for filename in filenames:
29+
from_path = source_dir / filename
30+
shutil.copy2(from_path, app_dir)
1531

1632
source_sub_dir = source_dir / 'demo1'
1733
app_sub_dir = app_dir / 'demo1'
@@ -20,3 +36,9 @@
2036
for filename in filenames:
2137
from_path = source_sub_dir / filename
2238
shutil.copy2(from_path, app_sub_dir)
39+
40+
# Generate tgz file
41+
tgz_path = source_dir.parent / 'dist/neuralhydrology_demo1.tgz'
42+
with tarfile.open(tgz_path, 'w:gz') as tar:
43+
tar.add(dist_dir)
44+
print(f'Wrote {tgz_path}')

neuralhydrology/plot_results.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Plot model run results (observed & simulated)
2+
3+
Requires matplotlib and xarray packages
4+
"""
5+
6+
import argparse
7+
8+
import matplotlib.pyplot as plt
9+
import xarray as xr
10+
11+
if __name__ == '__main__':
12+
parser = argparse.ArgumentParser(
13+
description='Plot NueralHydrology results file. Requires xarray & matplotlib')
14+
parser.add_argument('results_file', help='test_results.nc file for basin/model')
15+
args = parser.parse_args()
16+
17+
ds = xr.open_dataset(args.results_file)
18+
print(ds)
19+
20+
qobs = ds['QObs(mm/d)_obs']
21+
qsim = ds['QObs(mm/d)_sim']
22+
23+
px = 1/plt.rcParams['figure.dpi'] # inches per pixel
24+
fig, ax = plt.subplots(figsize=(800*px, 600*px))
25+
ax.plot(qobs['date'], qobs, label='Observed')
26+
ax.plot(qsim['date'], qsim, label='Simulated')
27+
ax.set_ylabel("Discharge (mm/d)")
28+
ax.set_title(f"Basin {ds.attrs['basin']}/{ds.attrs['run']} - NSE {ds.attrs['NSE']:.3f}")
29+
ax.legend()
30+
plt.show()

0 commit comments

Comments
 (0)