Skip to content

Commit 9a9f9db

Browse files
Issue #1797 fix api generate index array (#1798)
Fixes #1797 # Description This PR changes the following: - Fix so that public API method ``GridData.generate_index_array`` returns the same as it did in iMOD Python 1.0, throw DeprecationWarning urging users to update to ``GridData.generate_isactive_svat_array``. - Further improve new name method to ``GridData.generate_isactive_svat_array`` and add it to public API docs - Refactor methods in GridData somewhat, to reduce code duplication. - Start renaming the unfortunately chosen variable name ``index`` to the more descriptive ``isactive``, as the variable is in fact NOT an index. In a further PR we can rename this variable throughout the complete MetaSWAP module, which will affect a lot of files. I think that is worthy a separate PR. # Checklist <!--- Before requesting review, please go through this checklist: --> - [x] Links to correct issue - [x] Update changelog, if changes affect users - [x] PR title starts with ``Issue #nr``, e.g. ``Issue #737`` - [ ] Unit tests were added - [ ] **If feature added**: Added/extended example - [ ] **If feature added**: Added feature to API documentation - [ ] **If pixi.lock was changed**: Ran `pixi run generate-sbom` and committed changes
1 parent 3f42f65 commit 9a9f9db

5 files changed

Lines changed: 47 additions & 30 deletions

File tree

docs/api/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Changed
6464
:class:`imod.mf6.Evapotranspiration` are now optional variables. If provided,
6565
now require ``"segment"`` dimension when ``proportion_depth`` and
6666
``proportion_rate``.
67+
- :meth:`imod.msw.GridData.generate_index_array` is now deprecated, use
68+
:meth:`imod.msw.GridData.generate_isactive_svat_arrays` instead.
6769

6870

6971
[1.0.0] - 2025-11-11

docs/api/msw.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Grid packages
2828
GridData.from_imod5_data
2929
GridData.get_regrid_methods
3030
GridData.generate_index_array
31+
GridData.generate_isactive_svat_arrays
3132
GridData.write
3233
Infiltration
3334
Infiltration.regrid_like

imod/msw/grid_data.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import numpy as np
24
import xarray as xr
35

@@ -83,15 +85,14 @@ def __init__(
8385

8486
self._pkgcheck()
8587

86-
def generate_index_array(self) -> np.ndarray:
88+
def _generate_isactive_array(self) -> xr.DataArray:
8789
"""
88-
Generate index array to be used on other packages.
90+
Generate a 1D array of active cells to be used on other packages.
8991
9092
Returns
9193
-------
9294
np.ndarray
93-
Index array and svat grid.
94-
The index array is a 1D array with the index of the active cells.
95+
A 1D array with which cells are active.
9596
"""
9697
area = self.dataset["area"]
9798
active = self.dataset["active"]
@@ -101,36 +102,47 @@ def generate_index_array(self) -> np.ndarray:
101102
# https://github.com/dask/dask/issues/11753
102103
isactive.load()
103104

104-
index = isactive.values.ravel()
105-
106-
return index
105+
return isactive
107106

108-
def generate_index_svat_array(self) -> tuple[np.ndarray, xr.DataArray]:
107+
def generate_isactive_svat_arrays(self) -> tuple[np.ndarray, xr.DataArray]:
109108
"""
110109
Generate index array and svat grid to be used on other packages.
111110
112111
Returns
113112
-------
114113
tuple[np.ndarray, xr.DataArray]
115-
Index array and svat grid.
116-
The index array is a 1D array with the index of the active cells.
114+
isactive array and svat grid.
115+
The isactive array is a 1D array with which cells are active.
117116
The svat grid is a 2D array with the SVAT numbers for each cell.
118117
"""
119-
index = self.generate_index_array()
118+
isactive = self._generate_isactive_array()
119+
isactive_1d = isactive.values.ravel()
120120

121-
area = self.dataset["area"]
122-
active = self.dataset["active"]
123-
isactive = area.where(active).notnull()
121+
svat = xr.full_like(isactive, fill_value=0, dtype=np.int64).rename("svat")
122+
svat.data[isactive.data] = np.arange(1, isactive_1d.sum() + 1)
124123

125-
svat = xr.full_like(area, fill_value=0, dtype=np.int64).rename("svat")
126-
# Load into memory to avoid dask issue
127-
# https://github.com/dask/dask/issues/11753
128-
isactive.load()
129-
svat.load()
124+
return isactive_1d, svat
130125

131-
svat.data[isactive.data] = np.arange(1, index.sum() + 1)
126+
def generate_index_array(self) -> tuple[np.ndarray, xr.DataArray]:
127+
"""
128+
This method is kept for backward compatibility, but will be removed in
129+
future versions and will thus throw a deprecation warning. Use
130+
:meth:`imod.msw.GridData.generate_isactive_svat_arrays` instead.
131+
132+
Generate index array and svat grid to be used on other packages.
132133
133-
return index, svat
134+
Returns
135+
-------
136+
tuple[np.ndarray, xr.DataArray]
137+
isactive array and svat grid.
138+
The isactive array is a 1D array with which cells are active.
139+
The svat grid is a 2D array with the SVAT numbers for each cell.
140+
"""
141+
warnings.warn(
142+
"Method 'generate_index_array' is deprecated and will be removed in the future, use 'generate_isactive_svat_arrays' instead.",
143+
DeprecationWarning,
144+
)
145+
return self.generate_isactive_svat_arrays()
134146

135147
def _pkgcheck(self):
136148
super()._pkgcheck()

imod/msw/model.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def write(
360360
# Get index and svat
361361
grid_key = self.get_pkgkey(GridData)
362362
grid_pkg = cast(GridData, self[grid_key])
363-
index, svat = grid_pkg.generate_index_svat_array()
363+
index, svat = grid_pkg.generate_isactive_svat_arrays()
364364

365365
# write package contents
366366
for pkgname in self:
@@ -565,11 +565,13 @@ def split(
565565

566566
for submodel_name, submodel in partitioned_submodels.items():
567567
partition_info = submodel_to_partition[submodel_name]
568-
sliced_grid_pkg = clip_by_grid(grid_pkg, partition_info.active_domain)
569-
sliced_index = sliced_grid_pkg.generate_index_array()
568+
sliced_grid_pkg = cast(
569+
GridData, clip_by_grid(grid_pkg, partition_info.active_domain)
570+
)
571+
sliced_isactive = sliced_grid_pkg._generate_isactive_array().values
570572

571573
# Add package to model if it has data in the active domain.
572-
if bool(sliced_index.any()):
574+
if bool(sliced_isactive.any()):
573575
is_in_active_domain[submodel_name] = True
574576
submodel[grid_key] = sliced_grid_pkg
575577
else:

imod/tests/test_msw/test_grid_data.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_write(
6464
xr.full_like(like, True, dtype=bool),
6565
)
6666

67-
index, svat = grid_data.generate_index_svat_array()
67+
index, svat = grid_data.generate_isactive_svat_arrays()
6868

6969
with tempfile.TemporaryDirectory() as output_dir:
7070
output_dir = Path(output_dir)
@@ -293,12 +293,12 @@ def case_grid_data_two_subunits__dask(
293293

294294

295295
@parametrize_with_cases("grid_data_dict", cases=".", has_tag="two_subunit")
296-
def test_generate_index_svat_array(
296+
def test_generate_isactive_svat_arrays(
297297
grid_data_dict: dict[str, xr.DataArray], coords_two_subunit: dict
298298
):
299299
grid_data = GridData(**grid_data_dict)
300300

301-
index, svat = grid_data.generate_index_svat_array()
301+
index, svat = grid_data.generate_isactive_svat_arrays()
302302

303303
index_expected = [
304304
False,
@@ -346,7 +346,7 @@ def test_generate_index_svat_array(
346346
def test_simple_model(fixed_format_parser, grid_data_dict: dict[str, xr.DataArray]):
347347
grid_data = GridData(**grid_data_dict)
348348

349-
index, svat = grid_data.generate_index_svat_array()
349+
index, svat = grid_data.generate_isactive_svat_arrays()
350350

351351
with tempfile.TemporaryDirectory() as output_dir:
352352
output_dir = Path(output_dir)
@@ -388,7 +388,7 @@ def test_simple_model_1_subunit(
388388
):
389389
grid_data = GridData(**grid_data_dict)
390390

391-
index, svat = grid_data.generate_index_svat_array()
391+
index, svat = grid_data.generate_isactive_svat_arrays()
392392

393393
with tempfile.TemporaryDirectory() as output_dir:
394394
output_dir = Path(output_dir)

0 commit comments

Comments
 (0)