Skip to content

Commit 637b3bc

Browse files
authored
add compare check for example outputs (#269)
1 parent 08c9eb0 commit 637b3bc

File tree

12 files changed

+152
-36
lines changed

12 files changed

+152
-36
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ repos:
55
rev: v3.2.0
66
hooks:
77
- id: trailing-whitespace
8+
exclude: ^test/__compare__
89
- id: end-of-file-fixer
10+
exclude: ^test/__compare__
911
- id: check-yaml
1012
- id: check-added-large-files
1113
- id: check-toml

docs/examples/twri.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def plot_head(head, workspace):
173173
tdis = flopy4.mf6.simulation.Tdis.from_time(time)
174174

175175
# Create workspace
176-
workspace = Path(__file__).parent / "twri"
176+
workspace = Path(__file__).parent / "twri" / "list_stresspkg"
177177
workspace.mkdir(parents=True, exist_ok=True)
178178

179179
# Create simulation
@@ -260,7 +260,7 @@ def plot_head(head, workspace):
260260
gwf.rcha = [rcha]
261261

262262
# create new workspace
263-
workspace = Path(__file__).parent / "twri2"
263+
workspace = Path(__file__).parent / "twri" / "array_stresspkg"
264264
workspace.mkdir(parents=True, exist_ok=True)
265265
sim.workspace = workspace
266266

pixi.lock

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
7.91 KB
Binary file not shown.
6.49 KB
Binary file not shown.
928 Bytes
Binary file not shown.
98.8 KB
Binary file not shown.
33 KB
Binary file not shown.
5.4 KB
Binary file not shown.

test/test_examples.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import sys
44

5+
import numpy as np
56
import pytest
67
from conftest import EXAMPLES_PATH
78
from modflow_devtools.misc import run_cmd
@@ -19,8 +20,73 @@ def pytest_generate_tests(metafunc):
1920
metafunc.parametrize("example_script", scripts.values(), ids=scripts.keys())
2021

2122

23+
def compare_hds(compare_fpth, check_path):
24+
from flopy.utils import HeadFile
25+
26+
if compare_fpth.suffix == ".hds":
27+
hds_compare = HeadFile(compare_fpth, precision="double").get_data()
28+
elif compare_fpth.suffix == ".npy":
29+
hds_compare = np.load(compare_fpth)
30+
31+
# check *.hds files
32+
for f in check_path.rglob("*.hds"):
33+
hds = HeadFile(f, precision="double")
34+
assert np.allclose(hds_compare, hds.get_data())
35+
36+
37+
def compare_grb(compare_fpth, check_path):
38+
from flopy.mf6.utils import MfGrdFile
39+
40+
grb_compare = MfGrdFile(compare_fpth)
41+
42+
# check *.grb files
43+
for f in check_path.rglob("*.grb"):
44+
grb = MfGrdFile(f)
45+
np.testing.assert_equal(grb_compare._datadict, grb._datadict)
46+
47+
48+
def compare_bud(compare_fpth, check_path):
49+
from flopy.utils.binaryfile import CellBudgetFile
50+
51+
bud_compare = CellBudgetFile(compare_fpth)
52+
53+
# check *.bud and *.cbc files
54+
pattern = ["*.bud", "*.cbc"]
55+
files = [f for p in pattern for f in check_path.rglob(p)]
56+
for f in files:
57+
bud = CellBudgetFile(f)
58+
names = bud_compare.get_unique_record_names()
59+
for n in names:
60+
bud_cmp = bud_compare.get_data(text=n)
61+
bud_chk = bud.get_data(text=n)
62+
if isinstance(bud_cmp[0], np.rec.recarray):
63+
assert len(bud_cmp) == len(bud_chk)
64+
for ra_cmp, ra in zip(bud_cmp, bud_chk):
65+
assert len(ra_cmp) == len(ra)
66+
for i, rec in enumerate(ra_cmp):
67+
assert all(np.allclose(x, y) for x, y in zip(rec, ra[i]))
68+
else:
69+
assert np.allclose(bud_compare.get_data(text=n), bud.get_data(text=n))
70+
71+
72+
def compare(example_script):
73+
from pathlib import Path
74+
75+
check_path = Path(f"{example_script.parent}/{example_script.stem}")
76+
compare_path = Path(f"{example_script.parent.parent.parent}/test/__compare__/test_examples")
77+
78+
for f in compare_path.glob(f"{example_script.stem}.*"):
79+
if f.suffix == ".bud" or f.suffix == ".cbc":
80+
compare_bud(f, check_path)
81+
if f.suffix == ".hds" or f.match("*hds.npy"):
82+
compare_hds(f, check_path)
83+
if f.suffix == ".grb":
84+
compare_grb(f, check_path)
85+
86+
2287
@pytest.mark.slow
2388
def test_scripts(example_script):
2489
args = [sys.executable, example_script]
2590
stdout, stderr, retcode = run_cmd(*args, verbose=True)
2691
assert not retcode, stdout + stderr
92+
compare(example_script)

0 commit comments

Comments
 (0)