Skip to content

Commit

Permalink
chore: switch to ruff, ignore known test warnings, numpy compat fixes (
Browse files Browse the repository at this point in the history
…modflowpy#2124)

* switch to ruff from black, isort, flake8, pylint, keep (mostly) the same settings
* apply some suggested linting/formatting rules, ignore others in pyproject.toml
* run ruff check --select NPY201 as suggested here, replace deprecated syntax
    * np.float_ -> np.float64
    * np.NaN -> np.nan
* ignore well-known python-dateutil and pandas warnings
  • Loading branch information
wpbonelli authored Mar 14, 2024
1 parent 5ec612a commit b142b08
Show file tree
Hide file tree
Showing 92 changed files with 375 additions and 375 deletions.
2 changes: 1 addition & 1 deletion .docs/Notebooks/modflow_postprocessing_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
grid, os.path.join(workspace, "heads5_rot.tif"), hdslayer, nodata=nodata
)

results = np.loadtxt(os.path.join(workspace, f"heads5_rot.asc"), skiprows=6)
results = np.loadtxt(os.path.join(workspace, "heads5_rot.asc"), skiprows=6)
results[results == nodata] = np.nan
plt.imshow(results)
plt.colorbar()
Expand Down
2 changes: 1 addition & 1 deletion .docs/Notebooks/modpath7_unstructured_lateral_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@
sim.set_sim_path(workspace)
sim.write_simulation()
success, buff = sim.run_simulation(silent=True, report=True)
assert success, f"Failed to run MF6 simulation."
assert success, "Failed to run MF6 simulation."
for line in buff:
print(line)

Expand Down
5 changes: 4 additions & 1 deletion .docs/Notebooks/plot_cross_section_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@
# Plot a shapefile of
shp = os.path.join(loadpth, "gis", "bedrock_outcrop_hole_rotate14")
patch_collection = mapview.plot_shapefile(
shp, edgecolor="green", linewidths=2, alpha=0.5 # facecolor='none',
shp,
edgecolor="green",
linewidths=2,
alpha=0.5, # facecolor='none',
)
# Plot a shapefile of a cross-section line
shp = os.path.join(loadpth, "gis", "cross_section_rotate14")
Expand Down
12 changes: 9 additions & 3 deletions .docs/Notebooks/plot_map_view_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,10 @@
# Plot a shapefile of
shp = os.path.join(loadpth, "gis", "bedrock_outcrop_hole")
patch_collection = mapview.plot_shapefile(
shp, edgecolor="green", linewidths=2, alpha=0.5 # facecolor='none',
shp,
edgecolor="green",
linewidths=2,
alpha=0.5, # facecolor='none',
)
# Plot a shapefile of a cross-section line
shp = os.path.join(loadpth, "gis", "cross_section")
Expand Down Expand Up @@ -488,7 +491,10 @@
# Plot a shapefile of
shp = os.path.join(loadpth, "gis", "bedrock_outcrop_hole_rotate14")
patch_collection = mapview.plot_shapefile(
shp, edgecolor="green", linewidths=2, alpha=0.5 # facecolor='none',
shp,
edgecolor="green",
linewidths=2,
alpha=0.5, # facecolor='none',
)
# Plot a shapefile of a cross-section line
shp = os.path.join(loadpth, "gis", "cross_section_rotate14")
Expand Down Expand Up @@ -1024,7 +1030,7 @@ def load_iverts(fname):
plt.colorbar(quadmesh, shrink=0.75)

# change the font type to comic sans
styles.set_font_type(family="fantasy", fontname="Comic Sans MS"),
(styles.set_font_type(family="fantasy", fontname="Comic Sans MS"),)

# use styles to add a heading, xlabel, ylabel, and remove tick marks
styles.heading(
Expand Down
2 changes: 1 addition & 1 deletion .docs/Notebooks/swi2package_example3.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def MergeData(ndim, zdata, tb):
found.fill(False)
for idx, layer in enumerate(zdata):
for jdx, z in enumerate(layer):
if found[jdx] == True:
if found[jdx] is True:
continue
t0 = tb[idx][0] - sv
t1 = tb[idx][1] + sv
Expand Down
28 changes: 4 additions & 24 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,11 @@ jobs:
pip install .
pip install ".[lint]"
- name: Run isort
run: |
echo "if isort check fails update isort using"
echo " pip install isort --upgrade"
echo "and run"
echo " isort ./flopy"
echo "and then commit the changes."
isort --check --diff ./flopy
- name: Run black
run: |
echo "if black check fails update black using"
echo " pip install black --upgrade"
echo "and run"
echo " black ./flopy"
echo "and then commit the changes."
black --check --diff ./flopy
- name: Run flake8
run: |
flake8 --count --show-source --exit-zero ./flopy
- name: Lint
run: ruff check .

- name: Run pylint
run: |
pylint --jobs=2 --errors-only --exit-zero ./flopy
- name: Check format
run: ruff format . --check

- name: Check CITATION.cff
run: |
Expand Down
7 changes: 6 additions & 1 deletion autotest/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ markers =
meta: tests run by other tests
mf6: tests for MODFLOW 6 support
regression: tests comparing multiple versions
slow: tests not completing in a few seconds
slow: tests not completing in a few seconds
filterwarnings =
# from python-dateutil, used by arrow, jupyter_client, matplotlib, pandas
ignore:datetime.datetime.utcfromtimestamp
# from pandas, see https://github.com/pandas-dev/pandas/issues/54466
ignore:\n.*Pyarrow
4 changes: 2 additions & 2 deletions autotest/regression/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def get_mf6_examples_path() -> Path:
__mf6_examples_lock.acquire()
try:
if __mf6_examples_path.is_dir() and any(__mf6_examples_path.glob("*")):
print(f"Example models already exist")
print("Example models already exist")
else:
__mf6_examples_path.mkdir(exist_ok=True)
print(f"Downloading example models")
print("Downloading example models")
download_and_unzip(
url="https://github.com/MODFLOW-USGS/modflow6-examples/releases/download/current/modflow6-examples.zip",
path=str(__mf6_examples_path),
Expand Down
26 changes: 13 additions & 13 deletions autotest/regression/test_mf6.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_np001(function_tmpdir, example_data_path):
)
except FlopyException:
ex = True
assert ex == True
assert ex is True

kwargs = {}
kwargs["xul"] = 20.5
Expand Down Expand Up @@ -855,9 +855,9 @@ def test_np002(function_tmpdir, example_data_path):
sim.simulation_data.max_columns_of_data = 22

name = sim.name_file
assert name.continue_.get_data() == None
assert name.nocheck.get_data() == True
assert name.memory_print_option.get_data() == None
assert name.continue_.get_data() is None
assert name.nocheck.get_data() is True
assert name.memory_print_option.get_data() is None

tdis_rc = [(6.0, 2, 1.0), (6.0, 3, 1.0)]
tdis_package = ModflowTdis(
Expand Down Expand Up @@ -1885,7 +1885,7 @@ def test005_create_tests_advgw_tidal(function_tmpdir, example_data_path):
assert model.name_file.filename == "new_name.nam"
package_type_dict = {}
for package in model.packagelist:
if not package.package_type in package_type_dict:
if package.package_type not in package_type_dict:
filename = os.path.split(package.filename)[1]
assert filename == f"new_name.{package.package_type}"
package_type_dict[package.package_type] = 1
Expand All @@ -1901,7 +1901,7 @@ def test005_create_tests_advgw_tidal(function_tmpdir, example_data_path):
sim.rename_all_packages("all_files_same_name")
package_type_dict = {}
for package in model.packagelist:
if not package.package_type in package_type_dict:
if package.package_type not in package_type_dict:
filename = os.path.split(package.filename)[1]
assert filename == f"all_files_same_name.{package.package_type}"
package_type_dict[package.package_type] = 1
Expand Down Expand Up @@ -2220,9 +2220,9 @@ def test035_create_tests_fhb(function_tmpdir, example_data_path):
)
time = model.modeltime
assert (
time.steady_state[0] == False
and time.steady_state[1] == False
and time.steady_state[2] == False
time.steady_state[0] is False
and time.steady_state[1] is False
and time.steady_state[2] is False
)
wel_period = {0: [((0, 1, 0), "flow")]}
wel_package = ModflowGwfwel(
Expand Down Expand Up @@ -3771,10 +3771,10 @@ def test005_advgw_tidal(function_tmpdir, example_data_path):
model = sim.get_model(model_name)
time = model.modeltime
assert (
time.steady_state[0] == True
and time.steady_state[1] == False
and time.steady_state[2] == False
and time.steady_state[3] == False
time.steady_state[0] is True
and time.steady_state[1] is False
and time.steady_state[2] is False
and time.steady_state[3] is False
)
ghb = model.get_package("ghb")
obs = ghb.obs
Expand Down
13 changes: 6 additions & 7 deletions autotest/test_copy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test copying of flopy objects.
"""
"""Test copying of flopy objects."""

import copy
import inspect
Expand Down Expand Up @@ -64,10 +63,10 @@ def model_is_copy(m1, m2):
continue
elif k not in m2.__dict__:
return False
elif type(v) == bool:
elif isinstance(v, bool):
if not v == v2:
return False
elif type(v) in [str, int, float, dict, list]:
elif isinstance(v, (str, int, float, dict, list)):
if v != v2:
return False
continue
Expand Down Expand Up @@ -118,13 +117,13 @@ def package_is_copy(pk1, pk2):
return False
elif k not in pk2.__dict__:
return False
elif type(v) == bool:
elif isinstance(v, bool):
if not v == v2:
return False
elif type(v) in [str, int, float, dict]:
elif isinstance(v, (str, int, float, dict)):
if v != v2:
return False
elif type(v) == list:
elif isinstance(v, list):
for item, item2 in zip(v, v2):
if not isinstance(item, MFPackage):
if item != item2:
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def test_shapefile_ibound(function_tmpdir, example_data_path):
field_names = [item[0] for item in shape.fields][1:]
ib_idx = field_names.index("ibound_1")
txt = f"should be int instead of {type(shape.record(0)[ib_idx])}"
assert type(shape.record(0)[ib_idx]) == int, txt
assert isinstance(shape.record(0)[ib_idx], int), txt
shape.close()


Expand Down
1 change: 1 addition & 0 deletions autotest/test_get_modflow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test get-modflow utility."""

import os
import platform
import sys
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ def test_voronoi_grid(request, function_tmpdir, grid_info):
# ensure proper number of cells
almost_right = ncpl == 538 and gridprops["ncpl"] == 535
if almost_right:
warn(f"off-by-3")
warn("off-by-3")

# ensure that all cells have 3 or more points
invalid_cells = [i for i, ivts in enumerate(vor.iverts) if len(ivts) < 3]
Expand Down
44 changes: 33 additions & 11 deletions autotest/test_gridintersect.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def test_rect_grid_3d_point_outside():

@requires_pkg("shapely")
def test_rect_grid_3d_point_inside():
botm = np.concatenate([np.ones(4), 0.5 * np.ones(4), np.zeros(4)]).reshape(3, 2, 2)
botm = np.concatenate([np.ones(4), 0.5 * np.ones(4), np.zeros(4)]).reshape(
3, 2, 2
)
gr = get_rect_grid(top=np.ones(4), botm=botm)
ix = GridIntersect(gr, method="structured")
result = ix.intersect(Point(2.0, 2.0, 0.2))
Expand Down Expand Up @@ -454,7 +456,9 @@ def test_rect_grid_linestring_in_and_out_of_cell():
def test_rect_grid_linestring_in_and_out_of_cell2():
gr = get_rect_grid()
ix = GridIntersect(gr, method="structured")
result = ix.intersect(LineString([(5, 15), (5.0, 9), (15.0, 5.0), (5.0, 1.0)]))
result = ix.intersect(
LineString([(5, 15), (5.0, 9), (15.0, 5.0), (5.0, 1.0)])
)
assert len(result) == 3
# assert result.cellids[0] == (1, 0)
# assert result.cellids[1] == (1, 1)
Expand Down Expand Up @@ -751,7 +755,9 @@ def test_rect_grid_polygon_outside():
def test_rect_grid_polygon_in_2cells():
gr = get_rect_grid()
ix = GridIntersect(gr, method="structured")
result = ix.intersect(Polygon([(2.5, 5.0), (7.5, 5.0), (7.5, 15.0), (2.5, 15.0)]))
result = ix.intersect(
Polygon([(2.5, 5.0), (7.5, 5.0), (7.5, 15.0), (2.5, 15.0)])
)
assert len(result) == 2
assert result.areas.sum() == 50.0

Expand Down Expand Up @@ -788,7 +794,9 @@ def test_rect_grid_polygon_running_along_boundary():
def test_rect_grid_polygon_on_inner_boundary():
gr = get_rect_grid()
ix = GridIntersect(gr, method="structured")
result = ix.intersect(Polygon([(5.0, 10.0), (15.0, 10.0), (15.0, 5.0), (5.0, 5.0)]))
result = ix.intersect(
Polygon([(5.0, 10.0), (15.0, 10.0), (15.0, 5.0), (5.0, 5.0)])
)
assert len(result) == 2
assert result.areas.sum() == 50.0

Expand Down Expand Up @@ -969,7 +977,9 @@ def test_rect_grid_polygon_outside_shapely(rtree):
def test_rect_grid_polygon_in_2cells_shapely(rtree):
gr = get_rect_grid()
ix = GridIntersect(gr, method="vertex", rtree=rtree)
result = ix.intersect(Polygon([(2.5, 5.0), (7.5, 5.0), (7.5, 15.0), (2.5, 15.0)]))
result = ix.intersect(
Polygon([(2.5, 5.0), (7.5, 5.0), (7.5, 15.0), (2.5, 15.0)])
)
assert len(result) == 2
assert result.areas.sum() == 50.0

Expand All @@ -990,7 +1000,9 @@ def test_rect_grid_polygon_on_outer_boundary_shapely(rtree):
def test_rect_grid_polygon_on_inner_boundary_shapely(rtree):
gr = get_rect_grid()
ix = GridIntersect(gr, method="vertex", rtree=rtree)
result = ix.intersect(Polygon([(5.0, 10.0), (15.0, 10.0), (15.0, 5.0), (5.0, 5.0)]))
result = ix.intersect(
Polygon([(5.0, 10.0), (15.0, 10.0), (15.0, 5.0), (5.0, 5.0)])
)
assert len(result) == 2
assert result.areas.sum() == 50.0

Expand Down Expand Up @@ -1073,7 +1085,9 @@ def test_tri_grid_polygon_in_2cells(rtree):
if gr == -1:
return
ix = GridIntersect(gr, rtree=rtree)
result = ix.intersect(Polygon([(2.5, 5.0), (5.0, 5.0), (5.0, 15.0), (2.5, 15.0)]))
result = ix.intersect(
Polygon([(2.5, 5.0), (5.0, 5.0), (5.0, 15.0), (2.5, 15.0)])
)
assert len(result) == 2
assert result.areas.sum() == 25.0

Expand All @@ -1098,7 +1112,9 @@ def test_tri_grid_polygon_on_inner_boundary(rtree):
if gr == -1:
return
ix = GridIntersect(gr, rtree=rtree)
result = ix.intersect(Polygon([(5.0, 10.0), (15.0, 10.0), (15.0, 5.0), (5.0, 5.0)]))
result = ix.intersect(
Polygon([(5.0, 10.0), (15.0, 10.0), (15.0, 5.0), (5.0, 5.0)])
)
assert len(result) == 4
assert result.areas.sum() == 50.0

Expand Down Expand Up @@ -1374,7 +1390,9 @@ def test_rasters(example_data_path):
if (np.max(data) - 2608.557) > 1e-4:
raise AssertionError

data = rio.resample_to_grid(ml.modelgrid, band=rio.bands[0], method="nearest")
data = rio.resample_to_grid(
ml.modelgrid, band=rio.bands[0], method="nearest"
)
if data.size != 5913:
raise AssertionError
if abs(np.min(data) - 1942.1735) > 1e-4:
Expand Down Expand Up @@ -1423,11 +1441,15 @@ def test_raster_sampling_methods(example_data_path):
}

for method, value in methods.items():
data = rio.resample_to_grid(ml.modelgrid, band=rio.bands[0], method=method)
data = rio.resample_to_grid(
ml.modelgrid, band=rio.bands[0], method=method
)

print(data[30, 37])
if np.abs(data[30, 37] - value) > 1e-05:
raise AssertionError(f"{method} resampling returning incorrect values")
raise AssertionError(
f"{method} resampling returning incorrect values"
)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit b142b08

Please sign in to comment.