Skip to content

Commit

Permalink
chore: apply ruff updates (#2411)
Browse files Browse the repository at this point in the history
Ruff 0.9 changed some defaults, rerun the formatter.
  • Loading branch information
wpbonelli authored Jan 9, 2025
1 parent 2de949f commit d67e351
Show file tree
Hide file tree
Showing 52 changed files with 383 additions and 409 deletions.
6 changes: 3 additions & 3 deletions autotest/regression/test_lgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def test_simplelgr(function_tmpdir, example_data_path):

# get the namefiles of the parent and child
namefiles = lgr.get_namefiles()
assert (
len(namefiles) == 2
), f"get_namefiles returned {len(namefiles)} items instead of 2"
assert len(namefiles) == 2, (
f"get_namefiles returned {len(namefiles)} items instead of 2"
)

tpth = dirname(namefiles[0])
assert tpth == model_ws2, f"dir path is {tpth} not {model_ws2}"
Expand Down
3 changes: 1 addition & 2 deletions autotest/regression/test_mf6.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,7 @@ def test_np002(function_tmpdir, example_data_path):
"checker threshold of 1e-06" in summary
)
assert (
"sto package: specific yield values above "
"checker threshold of 0.5" in summary
"sto package: specific yield values above checker threshold of 0.5" in summary
)
assert "Not a number" in summary
model.remove_package("chd_2")
Expand Down
6 changes: 3 additions & 3 deletions autotest/regression/test_mfnwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ def test_run_mfnwt_model(function_tmpdir, namfile):
assert compare_heads(fn0, fn1, outfile=fsum), "head comparison failure"

fsum = function_tmpdir / f"{base_name}.budget.out"
assert compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
), "budget comparison failure"
assert compare_budget(fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum), (
"budget comparison failure"
)
6 changes: 3 additions & 3 deletions autotest/regression/test_modflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ def test_gage(function_tmpdir, example_data_path):
for f in files:
pth0 = join(ws, f)
pth1 = join(model_ws2, f)
assert filecmp.cmp(
pth0, pth1
), f'new and original gage file "{f}" are not binary equal.'
assert filecmp.cmp(pth0, pth1), (
f'new and original gage file "{f}" are not binary equal.'
)


__example_data_path = get_example_data_path()
Expand Down
6 changes: 3 additions & 3 deletions autotest/regression/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ def test_str_fixed_free(function_tmpdir, example_data_path):
# compare the fixed and free format head files
fn1 = function_tmpdir / "str.nam"
fn2 = function_tmpdir / "str.nam"
assert compare_heads(
fn1, fn2, verbose=True
), "fixed and free format input output head files are different"
assert compare_heads(fn1, fn2, verbose=True), (
"fixed and free format input output head files are different"
)
6 changes: 3 additions & 3 deletions autotest/regression/test_wel.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ def test_binary_well(function_tmpdir):
assert compare_heads(fn0, fn1, outfile=fsum), "head comparison failure"

fsum = os.path.join(function_tmpdir, f"{os.path.splitext(mfnam)[0]}.budget.out")
assert compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
), "budget comparison failure"
assert compare_budget(fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum), (
"budget comparison failure"
)
12 changes: 6 additions & 6 deletions autotest/test_binaryfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,16 @@ def test_binaryfile_read(function_tmpdir, freyberg_model_path):
h0 = h.get_data(totim=times[0])
h1 = h.get_data(kstpkper=kstpkper[0])
h2 = h.get_data(idx=0)
assert np.array_equal(
h0, h1
), "binary head read using totim != head read using kstpkper"
assert np.array_equal(h0, h1), (
"binary head read using totim != head read using kstpkper"
)
assert np.array_equal(h0, h2), "binary head read using totim != head read using idx"

ts = h.get_ts((0, 7, 5))
expected = 26.00697135925293
assert np.isclose(
ts[0, 1], expected
), f"time series value ({ts[0, 1]}) != {expected}"
assert np.isclose(ts[0, 1], expected), (
f"time series value ({ts[0, 1]}) != {expected}"
)
h.close()

# Check error when reading empty file
Expand Down
36 changes: 18 additions & 18 deletions autotest/test_binarygrid_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def test_mfgrddis_modelgrid(mfgrd_test_path):
assert isinstance(modelgrid, StructuredGrid), "invalid grid type"

lc = modelgrid.plot()
assert isinstance(
lc, matplotlib.collections.LineCollection
), f"could not plot grid object created from {fn}"
assert isinstance(lc, matplotlib.collections.LineCollection), (
f"could not plot grid object created from {fn}"
)
plt.close()

extents = modelgrid.extent
Expand All @@ -54,9 +54,9 @@ def test_mfgrddis_modelgrid(mfgrd_test_path):
maxvertex = max([max(sublist[1:]) for sublist in iverts])
assert maxvertex + 1 == nvert, f"nvert ({maxvertex + 1}) does not equal {nvert}"
verts = modelgrid.verts
assert (
nvert == verts.shape[0]
), f"number of vertex (x, y) pairs ({verts.shape[0]}) does not equal {nvert}"
assert nvert == verts.shape[0], (
f"number of vertex (x, y) pairs ({verts.shape[0]}) does not equal {nvert}"
)


def test_mfgrddisv_MfGrdFile(mfgrd_test_path):
Expand Down Expand Up @@ -87,9 +87,9 @@ def test_mfgrddisv_modelgrid(mfgrd_test_path):
assert mg.ncpl == ncpl, f"ncpl ({mg.ncpl}) does not equal {ncpl}"

lc = mg.plot()
assert isinstance(
lc, matplotlib.collections.LineCollection
), f"could not plot grid object created from {fn}"
assert isinstance(lc, matplotlib.collections.LineCollection), (
f"could not plot grid object created from {fn}"
)
plt.close("all")

extents = mg.extent
Expand All @@ -102,9 +102,9 @@ def test_mfgrddisv_modelgrid(mfgrd_test_path):
maxvertex = max([max(sublist[1:]) for sublist in iverts])
assert maxvertex + 1 == nvert, f"nvert ({maxvertex + 1}) does not equal {nvert}"
verts = mg.verts
assert (
nvert == verts.shape[0]
), f"number of vertex (x, y) pairs ({verts.shape[0]}) does not equal {nvert}"
assert nvert == verts.shape[0], (
f"number of vertex (x, y) pairs ({verts.shape[0]}) does not equal {nvert}"
)

cellxy = np.column_stack(mg.xyzcellcenters[:2])
errmsg = f"shape of flow.disv centroids {cellxy.shape} not equal to (218, 2)."
Expand Down Expand Up @@ -141,9 +141,9 @@ def test_mfgrddisu_modelgrid(mfgrd_test_path):
assert isinstance(mg, UnstructuredGrid), f"invalid grid type ({type(mg)})"

lc = mg.plot()
assert isinstance(
lc, matplotlib.collections.LineCollection
), f"could not plot grid object created from {fn}"
assert isinstance(lc, matplotlib.collections.LineCollection), (
f"could not plot grid object created from {fn}"
)
plt.close("all")

extents = mg.extent
Expand All @@ -156,6 +156,6 @@ def test_mfgrddisu_modelgrid(mfgrd_test_path):
maxvertex = max([max(sublist[1:]) for sublist in iverts])
assert maxvertex + 1 == nvert, f"nvert ({maxvertex + 1}) does not equal {nvert}"
verts = mg.verts
assert (
nvert == verts.shape[0]
), f"number of vertex (x, y) pairs ({verts.shape[0]}) does not equal {nvert}"
assert nvert == verts.shape[0], (
f"number of vertex (x, y) pairs ({verts.shape[0]}) does not equal {nvert}"
)
12 changes: 6 additions & 6 deletions autotest/test_cbc_full3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ def load_mf6(path, ws_out):
def cbc_eval_size(cbcobj, nnodes, shape3d):
cbc_pth = cbcobj.filename

assert (
cbcobj.nnodes == nnodes
), f"{cbc_pth} nnodes ({cbcobj.nnodes}) does not equal {nnodes}"
assert cbcobj.nnodes == nnodes, (
f"{cbc_pth} nnodes ({cbcobj.nnodes}) does not equal {nnodes}"
)
a = np.squeeze(np.ones(cbcobj.shape, dtype=float))
b = np.squeeze(np.ones(shape3d, dtype=float))
assert (
a.shape == b.shape
), f"{cbc_pth} shape {cbcobj.shape} does not conform to {shape3d}"
assert a.shape == b.shape, (
f"{cbc_pth} shape {cbcobj.shape} does not conform to {shape3d}"
)


def cbc_eval_data(cbcobj, shape3d):
Expand Down
18 changes: 9 additions & 9 deletions autotest/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ def test_compare_budget_and_heads(comparison_model_1):
assert success, "could not run the new MODFLOW-2005 model"

# compare the files
assert compare_heads(
fn0, fn1, outfile=fhsum
), "head comparison failure (pathlib.Path)"
assert compare_heads(
str(fn0), str(fn1), outfile=fhsum
), "head comparison failure (str path)"
assert compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fbsum
), "budget comparison failure (pathlib.Path)"
assert compare_heads(fn0, fn1, outfile=fhsum), (
"head comparison failure (pathlib.Path)"
)
assert compare_heads(str(fn0), str(fn1), outfile=fhsum), (
"head comparison failure (str path)"
)
assert compare_budget(fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fbsum), (
"budget comparison failure (pathlib.Path)"
)
assert compare_budget(
str(fn0), str(fn1), max_incpd=0.1, max_cumpd=0.1, outfile=str(fbsum)
), "budget comparison failure (str path)"
Expand Down
24 changes: 12 additions & 12 deletions autotest/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ def test_shapefile(function_tmpdir, namfile):
fnc = model.export(fnc_name)

s = Reader(fnc_name)
assert (
s.numRecords == model.nrow * model.ncol
), f"wrong number of records in shapefile {fnc_name}"
assert s.numRecords == model.nrow * model.ncol, (
f"wrong number of records in shapefile {fnc_name}"
)


@requires_pkg("pyshp", name_map={"pyshp": "shapefile"})
Expand Down Expand Up @@ -896,9 +896,9 @@ def test_export_mf6_shp(function_tmpdir):
riv6spdarrays = dict(riv6.stress_period_data.masked_4D_arrays_itr())
rivspdarrays = dict(riv.stress_period_data.masked_4D_arrays_itr())
for k, v in rivspdarrays.items():
assert (
np.abs(np.nansum(v) - np.nansum(riv6spdarrays[k])) < 1e-6
), f"variable {k} is not equal"
assert np.abs(np.nansum(v) - np.nansum(riv6spdarrays[k])) < 1e-6, (
f"variable {k} is not equal"
)
pass

m.export(function_tmpdir / "mfnwt.shp")
Expand Down Expand Up @@ -1579,9 +1579,9 @@ def test_vtk_vertex(function_tmpdir, example_data_path):
hk = gwf.npf.k.array
hk[gwf.modelgrid.idomain == 0] = np.nan

assert np.allclose(
np.ravel(hk), hk2, equal_nan=True
), "Field data not properly written"
assert np.allclose(np.ravel(hk), hk2, equal_nan=True), (
"Field data not properly written"
)


@requires_exe("mf2005")
Expand Down Expand Up @@ -1652,9 +1652,9 @@ def test_vtk_pathline(function_tmpdir, example_data_path):

assert len(totim) == 12054, "Array size is incorrect"
assert np.abs(np.max(totim) - maxtime) < 100, "time values are incorrect"
assert len(np.unique(pid)) == len(
plines
), "number of particles are incorrect for modpath VTK"
assert len(np.unique(pid)) == len(plines), (
"number of particles are incorrect for modpath VTK"
)


def grid2disvgrid(nrow, ncol):
Expand Down
18 changes: 9 additions & 9 deletions autotest/test_formattedfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ def test_formattedfile_read(function_tmpdir, example_data_path):
h0 = h.get_data(totim=times[0])
h1 = h.get_data(kstpkper=kstpkper[0])
h2 = h.get_data(idx=0)
assert np.array_equal(
h0, h1
), "formatted head read using totim != head read using kstpkper"
assert np.array_equal(
h0, h2
), "formatted head read using totim != head read using idx"
assert np.array_equal(h0, h1), (
"formatted head read using totim != head read using kstpkper"
)
assert np.array_equal(h0, h2), (
"formatted head read using totim != head read using idx"
)

ts = h.get_ts((0, 7, 5))
expected = 944.487
assert np.isclose(
ts[0, 1], expected, 1e-6
), f"time series value ({ts[0, 1]}) != {expected}"
assert np.isclose(ts[0, 1], expected, 1e-6), (
f"time series value ({ts[0, 1]}) != {expected}"
)
h.close()

# Check error when reading empty file
Expand Down
48 changes: 24 additions & 24 deletions autotest/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ def test_structured_from_gridspec(example_data_path, spc_file):
assert isinstance(modelgrid, StructuredGrid)

lc = modelgrid.plot()
assert isinstance(
lc, matplotlib.collections.LineCollection
), f"could not plot grid object created from {fn}"
assert isinstance(lc, matplotlib.collections.LineCollection), (
f"could not plot grid object created from {fn}"
)
plt.close()

extents = modelgrid.extent
Expand All @@ -442,9 +442,9 @@ def test_structured_from_gridspec(example_data_path, spc_file):
8000 + 8000 * np.sin(theta), # ymax
)
errmsg = f"extents {extents} of {fn} does not equal {rotated_extents}"
assert all(
np.isclose(x, x0) for x, x0 in zip(modelgrid.extent, rotated_extents)
), errmsg
assert all(np.isclose(x, x0) for x, x0 in zip(modelgrid.extent, rotated_extents)), (
errmsg
)

ncpl = modelgrid.ncol * modelgrid.nrow
assert modelgrid.ncpl == ncpl, f"ncpl ({modelgrid.ncpl}) does not equal {ncpl}"
Expand All @@ -454,9 +454,9 @@ def test_structured_from_gridspec(example_data_path, spc_file):
maxvertex = max([max(sublist[1:]) for sublist in iverts])
assert maxvertex + 1 == nvert, f"nvert ({maxvertex + 1}) does not equal {nvert}"
verts = modelgrid.verts
assert (
nvert == verts.shape[0]
), f"number of vertex (x, y) pairs ({verts.shape[0]}) does not equal {nvert}"
assert nvert == verts.shape[0], (
f"number of vertex (x, y) pairs ({verts.shape[0]}) does not equal {nvert}"
)


@requires_pkg("shapely")
Expand Down Expand Up @@ -1149,9 +1149,9 @@ def test_voronoi_grid(request, function_tmpdir, grid_info):
plt.savefig(function_tmpdir / f"{name}.png")

assert ncpl == gridprops["ncpl"] or almost_right
assert (
len(invalid_cells) == 0
), f"The following cells do not have 3 or more vertices.\n{invalid_cells}"
assert len(invalid_cells) == 0, (
f"The following cells do not have 3 or more vertices.\n{invalid_cells}"
)


@pytest.fixture
Expand Down Expand Up @@ -1257,31 +1257,31 @@ def test_structured_ncb_thickness():
grid = GridCases.structured_cbd_small()
thickness = grid.cell_thickness

assert thickness.shape[0] == grid.nlay + np.count_nonzero(
grid.laycbd
), "grid cell_thickness attribute returns incorrect shape"
assert thickness.shape[0] == grid.nlay + np.count_nonzero(grid.laycbd), (
"grid cell_thickness attribute returns incorrect shape"
)

thickness = grid.remove_confining_beds(grid.cell_thickness)
assert thickness.shape == grid.shape, "quasi3d confining beds not properly removed"

sat_thick = grid.saturated_thickness(grid.cell_thickness)
assert (
sat_thick.shape == grid.shape
), "saturated_thickness confining beds not removed"
assert sat_thick.shape == grid.shape, (
"saturated_thickness confining beds not removed"
)

assert (
sat_thick[1, 0, 0] == 20
), "saturated_thickness is not properly indexing confining beds"
assert sat_thick[1, 0, 0] == 20, (
"saturated_thickness is not properly indexing confining beds"
)


@pytest.mark.parametrize(
"grid", [GridCases.unstructured_small(), GridCases.unstructured_medium()]
)
def test_unstructured_iverts(grid):
iverts = grid.iverts
assert not any(
None in l for l in iverts
), "None type should not be returned in iverts list"
assert not any(None in l for l in iverts), (
"None type should not be returned in iverts list"
)


@pytest.mark.parametrize(
Expand Down
Loading

0 comments on commit d67e351

Please sign in to comment.