Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expand/reorg/stub tests
Browse files Browse the repository at this point in the history
wpbonelli committed Aug 15, 2023
1 parent 5272fd2 commit 1f9ee55
Showing 8 changed files with 1,018 additions and 796 deletions.
99 changes: 99 additions & 0 deletions autotest/test_cross_section_line_representations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import numpy as np
import pytest
from modflow_devtools.markers import requires_pkg

import flopy


def structured_square_grid(side: int = 10, thick: int = 10):
"""
Creates a basic 1-layer structured grid with the given thickness and number of cells per side
Parameters
----------
side : The number of cells per side
thick : The thickness of the grid's single layer
Returns
-------
A single-layer StructuredGrid of the given size and thickness
"""

from flopy.discretization.structuredgrid import StructuredGrid

delr = np.ones(side)
delc = np.ones(side)
top = np.ones((side, side)) * thick
botm = np.ones((side, side)) * (top - thick).reshape(1, side, side)
return StructuredGrid(delr=delr, delc=delc, top=top, botm=botm)


@requires_pkg("shapely")
@pytest.mark.parametrize(
"line",
[(), [], (()), [[]], (0, 0), [0, 0], [[0, 0]]],
)
def test_cross_section_invalid_lines_raise_error(line):
grid = structured_square_grid(side=10)
with pytest.raises(ValueError):
flopy.plot.PlotCrossSection(modelgrid=grid, line={"line": line})


@requires_pkg("shapely")
@pytest.mark.parametrize(
"line",
[
# diagonal
[(0, 0), (10, 10)],
([0, 0], [10, 10]),
# horizontal
([0, 5.5], [10, 5.5]),
[(0, 5.5), (10, 5.5)],
# vertical
[(5.5, 0), (5.5, 10)],
([5.5, 0], [5.5, 10]),
# multiple segments
[(0, 0), (4, 6), (10, 10)],
([0, 0], [4, 6], [10, 10]),
],
)
def test_cross_section_valid_line_representations(line):
from shapely.geometry import LineString as SLS

from flopy.utils.geometry import LineString as FLS

grid = structured_square_grid(side=10)

fls = FLS(line)
sls = SLS(line)

# use raw, flopy.utils.geometry and shapely.geometry representations
lxc = flopy.plot.PlotCrossSection(modelgrid=grid, line={"line": line})
fxc = flopy.plot.PlotCrossSection(modelgrid=grid, line={"line": fls})
sxc = flopy.plot.PlotCrossSection(modelgrid=grid, line={"line": sls})

# make sure parsed points are identical for all line representations
assert np.allclose(lxc.pts, fxc.pts) and np.allclose(lxc.pts, sxc.pts)
assert (
set(lxc.xypts.keys()) == set(fxc.xypts.keys()) == set(sxc.xypts.keys())
)
for k in lxc.xypts.keys():
assert np.allclose(lxc.xypts[k], fxc.xypts[k]) and np.allclose(
lxc.xypts[k], sxc.xypts[k]
)


@pytest.mark.parametrize(
"line",
[
0,
[0],
[0, 0],
(0, 0),
[(0, 0)],
([0, 0]),
],
)
@requires_pkg("shapely", "geojson")
def test_cross_section_invalid_line_representations_fail(line):
grid = structured_square_grid(side=10)
with pytest.raises(ValueError):
flopy.plot.PlotCrossSection(modelgrid=grid, line={"line": line})
90 changes: 90 additions & 0 deletions autotest/test_model_dot_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import os

import pytest
from flaky import flaky
from matplotlib import pyplot as plt
from matplotlib import rcParams
from matplotlib.collections import (
LineCollection,
PatchCollection,
PathCollection,
QuadMesh,
)

import flopy
from flopy.mf6 import MFSimulation


@pytest.mark.mf6
def test_vertex_model_dot_plot(example_data_path):
rcParams["figure.max_open_warning"] = 36

# load up the vertex example problem
sim = MFSimulation.load(
sim_ws=example_data_path / "mf6" / "test003_gwftri_disv"
)
disv_ml = sim.get_model("gwf_1")
ax = disv_ml.plot()
assert isinstance(ax, list)
assert len(ax) == 36


# occasional _tkinter.TclError: Can't find a usable tk.tcl (or init.tcl)
# similar: https://github.com/microsoft/azure-pipelines-tasks/issues/16426
@flaky
def test_model_dot_plot(function_tmpdir, example_data_path):
loadpth = example_data_path / "mf2005_test"
ml = flopy.modflow.Modflow.load(
"ibs2k.nam", "mf2k", model_ws=loadpth, check=False
)
ax = ml.plot()
assert isinstance(ax, list), "ml.plot() ax is is not a list"
assert len(ax) == 18, f"number of axes ({len(ax)}) is not equal to 18"


def test_dataset_dot_plot(function_tmpdir, example_data_path):
loadpth = example_data_path / "mf2005_test"
ml = flopy.modflow.Modflow.load(
"ibs2k.nam", "mf2k", model_ws=loadpth, check=False
)

# plot specific dataset
ax = ml.bcf6.hy.plot()
assert isinstance(ax, list), "ml.bcf6.hy.plot() ax is is not a list"
assert len(ax) == 2, f"number of hy axes ({len(ax)}) is not equal to 2"


def test_dataset_dot_plot_nlay_ne_plottable(
function_tmpdir, example_data_path
):
import matplotlib.pyplot as plt

loadpth = example_data_path / "mf2005_test"
ml = flopy.modflow.Modflow.load(
"ibs2k.nam", "mf2k", model_ws=loadpth, check=False
)
# special case where nlay != plottable
ax = ml.bcf6.vcont.plot()
assert isinstance(
ax, plt.Axes
), "ml.bcf6.vcont.plot() ax is is not of type plt.Axes"


def test_model_dot_plot_export(function_tmpdir, example_data_path):
loadpth = example_data_path / "mf2005_test"
ml = flopy.modflow.Modflow.load(
"ibs2k.nam", "mf2k", model_ws=loadpth, check=False
)

fh = os.path.join(function_tmpdir, "ibs2k")
ml.plot(mflay=0, filename_base=fh, file_extension="png")
files = [f for f in os.listdir(function_tmpdir) if f.endswith(".png")]
if len(files) < 10:
raise AssertionError(
"ml.plot did not properly export all supported data types"
)

for f in files:
t = f.split("_")
if len(t) < 3:
raise AssertionError("Plot filenames not written correctly")
Loading

0 comments on commit 1f9ee55

Please sign in to comment.