Skip to content

Commit

Permalink
ci(plot): add plot option to conftest (MODFLOW-USGS#2087)
Browse files Browse the repository at this point in the history
* ci(plot): add plot option to conftest

* ruff
  • Loading branch information
langevin-usgs authored Dec 3, 2024
1 parent 099d735 commit 7f1180f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 31 deletions.
11 changes: 11 additions & 0 deletions autotest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def original_regression(request) -> bool:
return request.config.getoption("--original-regression")


@pytest.fixture
def plot(request) -> bool:
return request.config.getoption("--plot")


@pytest.fixture(scope="session")
def markers(pytestconfig) -> str:
return pytestconfig.getoption("-m")
Expand All @@ -124,6 +129,12 @@ def pytest_addoption(parser):
default=False,
help="include netcdf test cases",
)
parser.addoption(
"--plot",
action="store_true",
default=False,
help="make plots of model output",
)


def pytest_collection_modifyitems(config, items):
Expand Down
8 changes: 8 additions & 0 deletions autotest/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def __init__(
api_func: Optional[Callable] = None,
build: Optional[Callable] = None,
check: Optional[Callable] = None,
plot: Optional[Callable] = None,
compare: Optional[str] = "auto",
parallel=False,
ncpus=1,
Expand All @@ -244,6 +245,7 @@ def __init__(
self.targets = targets
self.build = build
self.check = check
self.plot = plot
self.parallel = parallel
self.ncpus = [ncpus] if isinstance(ncpus, int) else ncpus
self.api_func = api_func
Expand Down Expand Up @@ -753,3 +755,9 @@ def run(self):
if self.verbose:
print("Checking outputs")
self.check(self)

# plot results, if enabled
if self.plot:
if self.verbose:
print("Plotting outputs")
self.plot(self)
64 changes: 33 additions & 31 deletions autotest/test_chf_dfw_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ def build_models(idx, test):
return sim, None


def make_plot(test):
print("making plots...")
def plot_output(idx, test):
import matplotlib.pyplot as plt

name = test.name
Expand Down Expand Up @@ -436,49 +435,51 @@ def make_plot(test):
fname = ws / "loop_network_flow.png"
plt.savefig(fname)

# read grb and get locations of the cell center
fpth = test.workspace / f"{name}.disv1d.grb"
grb = flopy.mf6.utils.MfGrdFile(fpth)
cellx = grb._datadict["CELLX"]
celly = grb._datadict["CELLY"]

fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim(-100, 6100)
ax.set_ylim(-100, 6100)
pmv = flopy.plot.PlotMapView(model=chf, ax=ax)
pmv.plot_grid()

# plot and label vertices
vertices = chf.disv1d.vertices.get_data()
ax.plot(vertices["xv"], vertices["yv"], "bo")
for iv, x, y in vertices:
ax.text(x, y, f"{iv + 1}")
cell1d = chf.disv1d.cell1d.get_data()
print(cell1d.dtype)
for row in cell1d:
ic = row["icell1d"]
ncvert = row["ncvert"]
if ncvert == 2:
n0 = row["icvert_0"]
n1 = row["icvert_1"]
x0 = vertices["xv"][n0]
x1 = vertices["xv"][n1]
y0 = vertices["yv"][n0]
y1 = vertices["yv"][n1]
xm = 0.5 * (x0 + x1)
ym = 0.5 * (y0 + y1)
elif ncvert == 3:
n0 = row["icvert_1"]
xm = vertices["xv"][n0] + 150
ym = vertices["yv"][n0] + 150
ax.text(xm, ym, f"{ic + 1}", color="red")
# raise Exception()
fname = ws / "grid.png"
ax.text(
x,
y,
f"{iv + 1}",
color="blue",
horizontalalignment="left",
verticalalignment="bottom",
)

# plot and label cell centers
ax.plot(cellx, celly, marker="o", mfc="none", mec="red", linewidth=0.0)
for icell, (x, y) in enumerate(zip(cellx, celly)):
ax.text(
x,
y,
f"{icell + 1}",
color="red",
horizontalalignment="right",
verticalalignment="top",
)

fname = ws / "loop_network_grid.png"
plt.savefig(fname)

return


def check_output(idx, test):
print("evaluating model...")

makeplot = False
if makeplot:
make_plot(test)

# read the observation output
name = cases[idx]
fpth = test.workspace / f"{name}.obs.csv"
Expand Down Expand Up @@ -532,12 +533,13 @@ def check_output(idx, test):

@pytest.mark.developmode
@pytest.mark.parametrize("idx, name", enumerate(cases))
def test_mf6model(idx, name, function_tmpdir, targets):
def test_mf6model(idx, name, function_tmpdir, targets, plot):
test = TestFramework(
name=name,
workspace=function_tmpdir,
build=lambda t: build_models(idx, t),
check=lambda t: check_output(idx, t),
plot=lambda t: plot_output(idx, t) if plot else None,
targets=targets,
)
test.run()

0 comments on commit 7f1180f

Please sign in to comment.