From af19633067a470957cc6b76f0a41502f30fc3455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dav=C3=ADd=20Brakenhoff?= Date: Mon, 18 Nov 2024 14:07:10 +0100 Subject: [PATCH] ruff --- docs/examples/09_schoonhoven.ipynb | 13 ++++-- nlmod/dims/__init__.py | 2 +- nlmod/gwf/output.py | 2 +- nlmod/gwf/surface_water.py | 1 - nlmod/read/ahn.py | 13 +++--- nlmod/read/bro.py | 5 +-- nlmod/read/knmi_data_platform.py | 2 +- nlmod/sim/sim.py | 2 +- nlmod/util.py | 9 +--- tests/test_001_model.py | 12 +++--- tests/test_005_external_data.py | 3 +- tests/test_006_caching.py | 69 ++++++++++++++++++++---------- tests/test_007_run_notebooks.py | 37 ++++++++-------- tests/test_016_time.py | 21 ++++----- tests/test_021_nhi.py | 2 +- tests/test_027_util.py | 6 ++- tests/test_028_rws_bathymetry.py | 2 +- 17 files changed, 112 insertions(+), 89 deletions(-) diff --git a/docs/examples/09_schoonhoven.ipynb b/docs/examples/09_schoonhoven.ipynb index 245913f2..e71ffb3e 100644 --- a/docs/examples/09_schoonhoven.ipynb +++ b/docs/examples/09_schoonhoven.ipynb @@ -414,11 +414,18 @@ "# lakes.loc[lakes['identificatie'].isin(ids_oude_haven), 'INFLOW'] = 'inflow_lake'\n", "\n", "# add outlet to Oude Haven, water flows from Oude Haven to Grote Gracht.\n", - "lakes.loc[lakes['name'] == \"oudehaven\", \"lakeout\"] = \"grotegracht\"\n", - "lakes.loc[lakes['name'] == \"oudehaven\", \"outlet_invert\"] = 1.0 # overstort hoogte\n", + "lakes.loc[lakes[\"name\"] == \"oudehaven\", \"lakeout\"] = \"grotegracht\"\n", + "lakes.loc[lakes[\"name\"] == \"oudehaven\", \"outlet_invert\"] = 1.0 # overstort hoogte\n", "\n", "# add lake to groundwaterflow model\n", - "lak = nlmod.gwf.lake_from_gdf(gwf, lakes, ds, boundname_column=\"name\", rainfall=lak_rainfall, evaporation=lak_evaporation)" + "lak = nlmod.gwf.lake_from_gdf(\n", + " gwf,\n", + " lakes,\n", + " ds,\n", + " boundname_column=\"name\",\n", + " rainfall=lak_rainfall,\n", + " evaporation=lak_evaporation,\n", + ")" ] }, { diff --git a/nlmod/dims/__init__.py b/nlmod/dims/__init__.py index 14c2d185..6f194053 100644 --- a/nlmod/dims/__init__.py +++ b/nlmod/dims/__init__.py @@ -1,4 +1,4 @@ -# ruff: noqa: F401 F403 +# ruff: noqa: F401 F403 I001 from . import base, grid, layers, resample, time from .attributes_encodings import * from .base import * diff --git a/nlmod/gwf/output.py b/nlmod/gwf/output.py index 1e6e4072..b81c0452 100644 --- a/nlmod/gwf/output.py +++ b/nlmod/gwf/output.py @@ -6,7 +6,7 @@ import pandas as pd import xarray as xr -from ..dims.grid import get_row_col_from_xy, get_icell2d_from_xy +from ..dims.grid import get_icell2d_from_xy, get_row_col_from_xy from ..mfoutput.mfoutput import ( _get_budget_da, _get_flopy_data_object, diff --git a/nlmod/gwf/surface_water.py b/nlmod/gwf/surface_water.py index 529ca68e..61efef5e 100644 --- a/nlmod/gwf/surface_water.py +++ b/nlmod/gwf/surface_water.py @@ -876,7 +876,6 @@ def add_min_ahn_to_gdf( A GeoDataFrame with surface water features, with an added column containing the minimum surface level height near the features. """ - gdf = zonal_statistics( gdf, ahn, columns=column, buffer=buffer, statistics=statistic, **kwargs ) diff --git a/nlmod/read/ahn.py b/nlmod/read/ahn.py index 3b4ae5e8..155afaf0 100644 --- a/nlmod/read/ahn.py +++ b/nlmod/read/ahn.py @@ -1,26 +1,26 @@ import datetime as dt import logging import os -import requests -from requests.exceptions import HTTPError import geopandas as gpd import matplotlib.pyplot as plt import numpy as np import pandas as pd -import shapely import rasterio +import requests import rioxarray -from rioxarray.merge import merge_arrays +import shapely import xarray as xr from rasterio import merge from rasterio.io import MemoryFile +from requests.exceptions import HTTPError +from rioxarray.merge import merge_arrays from tqdm import tqdm -from .. import cache, NLMOD_DATADIR +from .. import NLMOD_DATADIR, cache from ..dims.grid import get_extent from ..dims.resample import structured_da_to_ds -from ..util import get_ds_empty, extent_to_polygon +from ..util import extent_to_polygon, get_ds_empty from .webservices import arcrest, wcs logger = logging.getLogger(__name__) @@ -570,7 +570,6 @@ def get_ahn5(extent, identifier="AHN5_5M_M", **kwargs): xr.DataArray DataArray of the AHN """ - return _get_ahn_ellipsis(extent, identifier, **kwargs) diff --git a/nlmod/read/bro.py b/nlmod/read/bro.py index aedef0b4..701acffd 100644 --- a/nlmod/read/bro.py +++ b/nlmod/read/bro.py @@ -270,7 +270,7 @@ def _get_bro_metadata(extent, max_dx=10000, max_dy=10000, cachedir=None): @cache.cache_pickle def _get_bro_metadata_oc(xmin, xmax, ymin, ymax): - """get observation collection with only metadata in extent. + """Get observation collection with only metadata in extent. Parameters ---------- @@ -297,7 +297,7 @@ def _get_bro_metadata_oc(xmin, xmax, ymin, ymax): @cache.cache_pickle def _get_bro_measurement(oc_meta): - """add measurements to BRO ObsCollection with only metadata. + """Add measurements to BRO ObsCollection with only metadata. Parameters ---------- @@ -309,7 +309,6 @@ def _get_bro_measurement(oc_meta): ObsCollection collection with measurements """ - obs_list = [] for _, row in oc_meta.iterrows(): o = hpd.GroundwaterObs.from_bro(row["monitoring_well"], tube_nr=row["tube_nr"]) diff --git a/nlmod/read/knmi_data_platform.py b/nlmod/read/knmi_data_platform.py index a2b57413..a871714a 100644 --- a/nlmod/read/knmi_data_platform.py +++ b/nlmod/read/knmi_data_platform.py @@ -10,7 +10,7 @@ import requests import xarray as xr from numpy import arange, array, ndarray -from pandas import Timedelta, Timestamp, read_html +from pandas import Timedelta, Timestamp from tqdm import tqdm logger = logging.getLogger(__name__) diff --git a/nlmod/sim/sim.py b/nlmod/sim/sim.py index 0983c1a5..6480dbca 100644 --- a/nlmod/sim/sim.py +++ b/nlmod/sim/sim.py @@ -106,7 +106,7 @@ def get_tdis_perioddata(ds, nstp="nstp", tsmult="tsmult"): if len(ds["time"]) > 1: perlen.extend(np.diff(ds["time"]) / deltat) elif ds.time.dtype.kind in ["i", "f"]: - perlen = [ds['time'][0]] + perlen = [ds["time"][0]] perlen.extent(np.diff(ds["time"].values)) nstp = util._get_value_from_ds_datavar(ds, "nstp", nstp, return_da=False) diff --git a/nlmod/util.py b/nlmod/util.py index aba4f6ff..90fe4531 100644 --- a/nlmod/util.py +++ b/nlmod/util.py @@ -4,12 +4,12 @@ import re import sys import warnings +from functools import partial from pathlib import Path from typing import Dict, Optional -from functools import partial -import numpy as np import geopandas as gpd +import numpy as np import requests import xarray as xr from colorama import Back, Fore, Style @@ -1184,16 +1184,11 @@ def zonal_statistics( Add the result to the orignal GeoDataFrame if True. Otherwise return a GeoDataFrame with only the statistics. The default is True. - Raises - ------ - - DESCRIPTION. Returns ------- gpd.GeoDataFrame A GeoDataFrame containing the the statistics in some of its columns. - """ if isinstance(statistics, str): statistics = [statistics] diff --git a/tests/test_001_model.py b/tests/test_001_model.py index 3f0e4c1d..13e2a66e 100644 --- a/tests/test_001_model.py +++ b/tests/test_001_model.py @@ -75,7 +75,7 @@ def test_get_ds_variable_delrc(): ) -@pytest.mark.slow() +@pytest.mark.slow def test_create_small_model_grid_only(tmpdir, model_name="test"): extent = [98700.0, 99000.0, 489500.0, 489700.0] # extent, nrow, ncol = nlmod.read.regis.fit_extent_to_regis(extent, 100, 100) @@ -117,7 +117,7 @@ def test_create_small_model_grid_only(tmpdir, model_name="test"): ds.to_netcdf(os.path.join(tst_model_dir, "small_model.nc")) -@pytest.mark.slow() +@pytest.mark.slow def test_create_sea_model_grid_only(tmpdir, model_name="test"): extent = [95000.0, 105000.0, 494000.0, 500000.0] # extent, nrow, ncol = nlmod.read.regis.fit_extent_to_regis(extent, 100, 100) @@ -143,7 +143,7 @@ def test_create_sea_model_grid_only(tmpdir, model_name="test"): ds.to_netcdf(os.path.join(tst_model_dir, "basic_sea_model.nc")) -@pytest.mark.slow() +@pytest.mark.slow def test_create_sea_model_grid_only_delr_delc_50(tmpdir, model_name="test"): ds = get_ds_time_transient(tmpdir) extent = [95000.0, 105000.0, 494000.0, 500000.0] @@ -160,7 +160,7 @@ def test_create_sea_model_grid_only_delr_delc_50(tmpdir, model_name="test"): ds.to_netcdf(os.path.join(tst_model_dir, "sea_model_grid_50.nc")) -@pytest.mark.slow() +@pytest.mark.slow def test_create_sea_model(tmpdir): ds = xr.open_dataset( os.path.join(tst_model_dir, "basic_sea_model.nc"), mask_and_scale=False @@ -210,7 +210,7 @@ def test_create_sea_model(tmpdir): _ = nlmod.sim.write_and_run(sim, ds) -@pytest.mark.slow() +@pytest.mark.slow def test_create_sea_model_perlen_list(tmpdir): ds = xr.open_dataset(os.path.join(tst_model_dir, "basic_sea_model.nc")) @@ -280,7 +280,7 @@ def test_create_sea_model_perlen_list(tmpdir): nlmod.sim.write_and_run(sim, ds) -@pytest.mark.slow() +@pytest.mark.slow def test_create_sea_model_perlen_14(tmpdir): ds = xr.open_dataset(os.path.join(tst_model_dir, "basic_sea_model.nc")) diff --git a/tests/test_005_external_data.py b/tests/test_005_external_data.py index cea4d197..f185c237 100644 --- a/tests/test_005_external_data.py +++ b/tests/test_005_external_data.py @@ -1,10 +1,11 @@ +import logging import os + import pandas as pd import pytest import test_001_model import xarray as xr from shapely.geometry import LineString -import logging import nlmod diff --git a/tests/test_006_caching.py b/tests/test_006_caching.py index 5bdfb3e0..214186b3 100644 --- a/tests/test_006_caching.py +++ b/tests/test_006_caching.py @@ -10,59 +10,80 @@ def test_cache_ahn_data_array(): cache_name = "ahn4.nc" with tempfile.TemporaryDirectory() as tmpdir: - assert not os.path.exists(os.path.join(tmpdir, cache_name)), "Cache should not exist yet1" + assert not os.path.exists( + os.path.join(tmpdir, cache_name) + ), "Cache should not exist yet1" ahn_no_cache = nlmod.read.ahn.get_ahn4(extent) - assert not os.path.exists(os.path.join(tmpdir, cache_name)), "Cache should not exist yet2" + assert not os.path.exists( + os.path.join(tmpdir, cache_name) + ), "Cache should not exist yet2" - ahn_cached = nlmod.read.ahn.get_ahn4(extent, cachedir=tmpdir, cachename=cache_name) - assert os.path.exists(os.path.join(tmpdir, cache_name)), "Cache should have existed by now" + ahn_cached = nlmod.read.ahn.get_ahn4( + extent, cachedir=tmpdir, cachename=cache_name + ) + assert os.path.exists( + os.path.join(tmpdir, cache_name) + ), "Cache should have existed by now" assert ahn_cached.equals(ahn_no_cache) modification_time1 = os.path.getmtime(os.path.join(tmpdir, cache_name)) # Check if the cache is used. If not, cache is rewritten and modification time changes - ahn_cache = nlmod.read.ahn.get_ahn4(extent, cachedir=tmpdir, cachename=cache_name) + ahn_cache = nlmod.read.ahn.get_ahn4( + extent, cachedir=tmpdir, cachename=cache_name + ) assert ahn_cache.equals(ahn_no_cache) modification_time2 = os.path.getmtime(os.path.join(tmpdir, cache_name)) assert modification_time1 == modification_time2, "Cache should not be rewritten" # Different extent should not lead to using the cache extent = [119_800, 120_000, 441_900, 442_000] - ahn_cache = nlmod.read.ahn.get_ahn4(extent, cachedir=tmpdir, cachename=cache_name) + ahn_cache = nlmod.read.ahn.get_ahn4( + extent, cachedir=tmpdir, cachename=cache_name + ) modification_time3 = os.path.getmtime(os.path.join(tmpdir, cache_name)) - assert modification_time1 != modification_time3, "Cache should have been rewritten" + assert ( + modification_time1 != modification_time3 + ), "Cache should have been rewritten" def test_cache_northsea_data_array(): """Test caching of AHN data array. Does have dataset as argument.""" from nlmod.read.rws import get_northsea + ds1 = nlmod.get_ds( [119_700, 120_000, 441_900, 442_000], - delr=100., - delc=100., - top=0., - botm=[-1., -2.], - kh=10., - kv=1., + delr=100.0, + delc=100.0, + top=0.0, + botm=[-1.0, -2.0], + kh=10.0, + kv=1.0, ) ds2 = nlmod.get_ds( [119_800, 120_000, 441_900, 444_000], - delr=100., - delc=100., - top=0., - botm=[-1., -3.], - kh=10., - kv=1., + delr=100.0, + delc=100.0, + top=0.0, + botm=[-1.0, -3.0], + kh=10.0, + kv=1.0, ) cache_name = "northsea.nc" with tempfile.TemporaryDirectory() as tmpdir: - assert not os.path.exists(os.path.join(tmpdir, cache_name)), "Cache should not exist yet1" + assert not os.path.exists( + os.path.join(tmpdir, cache_name) + ), "Cache should not exist yet1" out1_no_cache = get_northsea(ds1) - assert not os.path.exists(os.path.join(tmpdir, cache_name)), "Cache should not exist yet2" + assert not os.path.exists( + os.path.join(tmpdir, cache_name) + ), "Cache should not exist yet2" out1_cached = get_northsea(ds1, cachedir=tmpdir, cachename=cache_name) - assert os.path.exists(os.path.join(tmpdir, cache_name)), "Cache should exist by now" + assert os.path.exists( + os.path.join(tmpdir, cache_name) + ), "Cache should exist by now" assert out1_cached.equals(out1_no_cache) modification_time1 = os.path.getmtime(os.path.join(tmpdir, cache_name)) @@ -82,5 +103,7 @@ def test_cache_northsea_data_array(): # Different extent should not lead to using the cache out2_cache = get_northsea(ds2, cachedir=tmpdir, cachename=cache_name) modification_time3 = os.path.getmtime(os.path.join(tmpdir, cache_name)) - assert modification_time1 != modification_time3, "Cache should have been rewritten" + assert ( + modification_time1 != modification_time3 + ), "Cache should have been rewritten" assert not out2_cache.equals(out1_no_cache) diff --git a/tests/test_007_run_notebooks.py b/tests/test_007_run_notebooks.py index 8b27e8b8..93e1dab1 100644 --- a/tests/test_007_run_notebooks.py +++ b/tests/test_007_run_notebooks.py @@ -1,4 +1,5 @@ """run notebooks in the examples directory.""" + # ruff: noqa: D103 import os @@ -20,91 +21,91 @@ def _run_notebook(nbdir, fname): return out -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_00_model_from_scratch(): _run_notebook(nbdir, "00_model_from_scratch.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_01_basic_model(): _run_notebook(nbdir, "01_basic_model.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_02_surface_water(): _run_notebook(nbdir, "02_surface_water.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_03_local_grid_refinement(): _run_notebook(nbdir, "03_local_grid_refinement.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_04_modifying_layermodels(): _run_notebook(nbdir, "04_modifying_layermodels.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_05_caching(): _run_notebook(nbdir, "05_caching.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_06_gridding_vector_data(): _run_notebook(nbdir, "06_gridding_vector_data.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_07_resampling(): _run_notebook(nbdir, "07_resampling.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_08_gis(): _run_notebook(nbdir, "08_gis.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_09_schoonhoven(): _run_notebook(nbdir, "09_schoonhoven.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_10_modpath(): _run_notebook(nbdir, "10_modpath.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_11_grid_rotation(): _run_notebook(nbdir, "11_grid_rotation.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_12_layer_generation(): _run_notebook(nbdir, "12_layer_generation.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_13_plot_methods(): _run_notebook(nbdir, "13_plot_methods.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_14_stromingen_example(): _run_notebook(nbdir, "14_stromingen_example.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_15_geotop(): _run_notebook(nbdir, "15_geotop.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_16_groundwater_transport(): _run_notebook(nbdir, "16_groundwater_transport.ipynb") -@pytest.mark.notebooks() +@pytest.mark.notebooks def test_run_notebook_17_unsaturated_zone_flow(): _run_notebook(nbdir, "17_unsaturated_zone_flow.ipynb") diff --git a/tests/test_016_time.py b/tests/test_016_time.py index 9436ddce..242bb574 100644 --- a/tests/test_016_time.py +++ b/tests/test_016_time.py @@ -1,14 +1,13 @@ -import numpy as np -import xarray as xr -import datetime as dt -import pandas as pd import cftime -import nlmod +import numpy as np +import pandas as pd import pytest - +import xarray as xr from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime, OutOfBoundsTimedelta +import nlmod + def test_estimate_nstp(): forcing = [0.0, 10.0] + 100 * [0.0] @@ -45,7 +44,6 @@ def test_time_options(): """Attempt to list all the variations of start, time and perlen caling the nlmod.dims.set_ds_time functions """ - ds = nlmod.get_ds([0, 1000, 2000, 3000]) # start_time str and time int @@ -94,11 +92,10 @@ def test_time_options(): def test_time_out_of_bounds(): - """related to this issue: https://github.com/gwmod/nlmod/issues/374 + """Related to this issue: https://github.com/gwmod/nlmod/issues/374 pandas timestamps can only do computations with dates between the years 1678 and 2262. """ - ds = nlmod.get_ds([0, 1000, 2000, 3000]) cftime_ind = xr.date_range("1000-01-02", "9999-01-01", freq="100YS") @@ -149,7 +146,7 @@ def test_time_out_of_bounds(): # start numpy datetime and perlen list of int with pytest.raises(OutOfBoundsDatetime): nlmod.dims.set_ds_time( - ds, start=np.datetime64("1000-01-01"), perlen=[10, 100, 24] + ds, start=np.datetime64("1000-01-01"), perlen=[10, 100, 24] ) # start numpy datetime and time list of timestamps @@ -168,8 +165,8 @@ def test_time_out_of_bounds(): # start timestamp and perlen list of int with pytest.raises(OutOfBoundsDatetime): nlmod.dims.set_ds_time( - ds, start=pd.Timestamp("1000-01-01"), perlen=[10, 100, 24] - ) + ds, start=pd.Timestamp("1000-01-01"), perlen=[10, 100, 24] + ) # start timestamp and time CFTimeIndex _ = nlmod.dims.set_ds_time(ds, start=pd.Timestamp("1000-01-01"), time=cftime_ind) diff --git a/tests/test_021_nhi.py b/tests/test_021_nhi.py index 8be1c5be..378ad93d 100644 --- a/tests/test_021_nhi.py +++ b/tests/test_021_nhi.py @@ -12,7 +12,7 @@ tmpdir = tempfile.gettempdir() -@pytest.mark.slow() +@pytest.mark.slow def test_buisdrainage(): model_ws = os.path.join(tmpdir, "buidrain") ds = nlmod.get_ds([110_000, 130_000, 435_000, 445_000], model_ws=model_ws) diff --git a/tests/test_027_util.py b/tests/test_027_util.py index ca48f184..98be5ba6 100644 --- a/tests/test_027_util.py +++ b/tests/test_027_util.py @@ -1,8 +1,10 @@ import os -import nlmod import tempfile -import rioxarray + import geopandas as gpd +import rioxarray + +import nlmod model_ws = os.path.join(tempfile.gettempdir(), "test_util") extent = [98000.0, 99000.0, 489000.0, 490000.0] diff --git a/tests/test_028_rws_bathymetry.py b/tests/test_028_rws_bathymetry.py index c8f0076e..42b1aa94 100644 --- a/tests/test_028_rws_bathymetry.py +++ b/tests/test_028_rws_bathymetry.py @@ -1,4 +1,5 @@ import pytest + import nlmod @@ -10,4 +11,3 @@ def test_bathymetry(resolution): ymax = ymin + 2 * resolution extent = [xmin, xmax, ymin, ymax] nlmod.read.rws.get_bathymetry(extent, resolution=f"{resolution}m") -