Skip to content

Commit

Permalink
add array_interpolate
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinaHutter committed Sep 18, 2024
1 parent e5f8089 commit e7fc372
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions openeo_processes_dask/process_implementations/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"array_find",
"array_labels",
"array_apply",
"array_interpolate_linear",
"first",
"last",
"order",
Expand Down Expand Up @@ -234,6 +235,17 @@ def array_apply(
)


def array_interpolate_linear(data: ArrayLike):
x = np.arange(len(data))
valid = np.isfinite(data)
if len(x[valid]) < 2:
return data
data[~valid] = np.interp(
x[~valid], x[valid], data[valid], left=np.nan, right=np.nan
)
return data


def first(
data: ArrayLike,
ignore_nodata: Optional[bool] = True,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ def test_array_apply(process_registry):
assert (output_cube == [2, 3, 4, 5, 6, 7]).all()


@pytest.mark.parametrize(
"data, expected",
[
([np.nan, 1, np.nan, 6, np.nan, -8], [np.nan, 1, 3.5, 6, -1, -8]),
([np.nan, 1, np.nan, np.nan], [np.nan, 1, np.nan, np.nan]),
],
)
def test_array_interpolate_linear(data, expected):
assert np.array_equal(
array_interpolate_linear(data),
expected,
equal_nan=True,
)


def test_first():
assert first(np.array([1, 0, 3, 2])) == 1
assert pd.isnull(first(np.array([np.nan, 2, 3]), ignore_nodata=False))
Expand Down

0 comments on commit e7fc372

Please sign in to comment.