Skip to content

Commit

Permalink
ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrakenhoff committed Nov 18, 2024
1 parent ecaa33b commit af19633
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 89 deletions.
13 changes: 10 additions & 3 deletions docs/examples/09_schoonhoven.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
")"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion nlmod/dims/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down
2 changes: 1 addition & 1 deletion nlmod/gwf/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion nlmod/gwf/surface_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
13 changes: 6 additions & 7 deletions nlmod/read/ahn.py
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down Expand Up @@ -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)


Expand Down
5 changes: 2 additions & 3 deletions nlmod/read/bro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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
----------
Expand All @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion nlmod/read/knmi_data_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
2 changes: 1 addition & 1 deletion nlmod/sim/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions nlmod/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 6 additions & 6 deletions tests/test_001_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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"))

Expand Down Expand Up @@ -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"))

Expand Down
3 changes: 2 additions & 1 deletion tests/test_005_external_data.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
69 changes: 46 additions & 23 deletions tests/test_006_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)
Loading

0 comments on commit af19633

Please sign in to comment.