Skip to content

Commit

Permalink
Merge branch 'main' into math
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller authored Feb 15, 2025
2 parents 88c0a0e + 763aa04 commit 5293ade
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/index.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
toc: false
format-links: false
---

![](MIKE-IO-Logo-Pos-RGB.svg)
Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/dataarray.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Other methods that also return a DataArray:
* [`interp_like`](`mikeio.DataArray.interp_like`) - Spatio (temporal) interpolation (see example [Dfsu interpolation](../examples/dfsu/spatial_interpolation.qmd)
* [`interp_time()`](`mikeio.DataArray.interp_time`) - Temporal interpolation (see example [Time interpolation](../examples/Time-interpolation.qmd))
* [`dropna()`](`mikeio.DataArray.dropna`) - Remove time steps where all items are NaN
* [`fillna()`](`mikeio.DataArray.fillna`) - Fill missing values with a constant value
* [`squeeze()`](`mikeio.DataArray.squeeze`) - Remove axes of length 1

### Conversion:
Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/dataset.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Other methods that also return a Dataset:
* [`interp_like`](`mikeio.Dataset.interp_like`) - Spatio (temporal) interpolation (see [Dfsu interpolation notebook](../examples/dfsu/spatial_interpolation.qmd)
* [`interp_time()`](`mikeio.Dataset.interp_time`) - Temporal interpolation (see [Time interpolation notebook](../examples/Time-interpolation.qmd))
* [`dropna()`](`mikeio.Dataset.dropna`) - Remove time steps where all items are NaN
* [`fillna()`](`mikeio.Dataset.fillna`) - Fill missing values with a constant value
* [`squeeze()`](`mikeio.Dataset.squeeze`) - Remove axes of length 1

### Conversion:
Expand Down
28 changes: 28 additions & 0 deletions mikeio/dataset/_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,34 @@ def to_numpy(self) -> np.ndarray:
def _has_time_axis(self) -> bool:
return self.dims[0][0] == "t"

def fillna(self, value: float = 0.0) -> "DataArray":
"""Fill NA/NaN value.
Parameters
----------
value: float, optional
Value used to fill missing values. Default is 0.0.
Examples
--------
```{python}
import numpy as np
import mikeio
da = mikeio.DataArray([np.nan, 1.0])
da
```
```{python}
da.fillna(0.0)
```
"""
da = self.copy()
x = da.values
x[np.isnan(x)] = value
return da

def dropna(self) -> "DataArray":
"""Remove time steps where values are NaN."""
if not self._has_time_axis:
Expand Down
13 changes: 13 additions & 0 deletions mikeio/dataset/_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,19 @@ def copy(self) -> "Dataset":
"""Returns a copy of this dataset."""
return deepcopy(self)

def fillna(self, value: float = 0.0) -> "Dataset":
"""Fill NA/NaN value.
Parameters
----------
value: float, optional
Value used to fill missing values. Default is 0.0.
"""
res = {name: da.fillna(value=value) for name, da in self._data_vars.items()}

return Dataset(data=res, validate=False)

def dropna(self) -> "Dataset":
"""Remove time steps where all items are NaN."""
if not self[0]._has_time_axis: # type: ignore
Expand Down
3 changes: 3 additions & 0 deletions mikeio/pfs/_pfsdocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ def _parse_line(self, line: str, level: int = 0) -> tuple[str, int]:
def _parse_param(self, value: str) -> str:
if len(value) == 0:
return "[]"
if value[0] == "|" and value[-1] == "|":
if value.count("|") == 2:
return self._parse_token(value)
if "MULTIPOLYGON" in value:
return value
if "," in value:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1679,3 +1679,23 @@ def test_read_write_single_timestep_preserves_dt(tmp_path):

dfs2 = mikeio.open(outfn)
assert dfs2.timestep == pytest.approx(10800.0)


def test_fillna() -> None:
ds = mikeio.Dataset(
{
"foo": mikeio.DataArray(np.array([np.nan, 1.0])),
"bar": mikeio.DataArray(np.array([2.0, np.nan])),
"baz": mikeio.DataArray(np.array([2.0, 3.0])),
}
)
assert np.isnan(ds["foo"].to_numpy()[0])
assert np.isnan(ds["bar"].to_numpy()[-1])

ds_filled = ds.fillna()

assert ds_filled["foo"].to_numpy()[0] == pytest.approx(0.0)
assert ds_filled["bar"].to_numpy()[-1] == pytest.approx(0.0)

# original dataset is not modified
assert np.isnan(ds["foo"].to_numpy()[0])
9 changes: 7 additions & 2 deletions tests/test_pfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_pfssection_insert_pfssection(d1) -> None:

for j in range(10):
dj = dict(val=j, lst=[0.3, 0.7])
key = f"FILE_{j+1}"
key = f"FILE_{j + 1}"
sct[key] = mikeio.PfsSection(dj)

assert sct.FILE_6.val == 5
Expand All @@ -226,7 +226,7 @@ def test_pfssection_find_replace(d1) -> None:

for j in range(10):
dj = dict(val=j, lst=[0.3, 0.7])
key = f"FILE_{j+1}"
key = f"FILE_{j + 1}"
sct[key] = mikeio.PfsSection(dj)

assert sct.FILE_6.lst == [0.3, 0.7]
Expand Down Expand Up @@ -1250,3 +1250,8 @@ def test_ignores_comments_in_quotes() -> None:
"""
pfs = mikeio.PfsDocument.from_text(text)
assert pfs.SymbologyModule.SymbologyTreeView == "//"


def test_filenames_may_contain_comma() -> None:
pfs = mikeio.read_pfs("tests/testdata/pfs/tidal.21t")
assert pfs.m21_tideph.Setup.File_1.mesh_file == "|.\\b,athy.mesh|"
26 changes: 26 additions & 0 deletions tests/testdata/pfs/tidal.21t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[m21_tideph]
CLSID = 'TidePredictionHeights.dll'
TypeName = 'm21_tideph'
CREATEDTIME = '2018-01-25T08:27:01'
MODIFIEDTIME = '2018-01-25T08:28:25'
NOTES = ''
[Setup]
Name = 'Esbjerg1989Global_Area'
Prediction_type = 1
constituent_file_name = |..\..\..\..\..\..\..\..\Program Files (x86)\DHI\MIKE Zero\2024\Application Data\Tide_Constituents\global_tide_constituents_height_0.25deg.dfs2|
start_date = 1989, 1, 1
end_date = 1990, 1, 4
timestep = 1.0
number_of_files = 1
[File_1]
format = 3
mesh_file = |.\b,athy.mesh|
file_name = |.\Esbjerg1989PredictedGlobal.dfsu|
description = 'Predicted tide level'
EndSect // File_1

logfilelocation = |.\|
EndSect // Setup

EndSect // m21_tideph

0 comments on commit 5293ade

Please sign in to comment.