Skip to content

Commit

Permalink
Array apply (#276)
Browse files Browse the repository at this point in the history
* update openeo-processes

* bump 2023.9.1

* bump 2023.9.0

* add array_apply process

* run pre-commit hook
  • Loading branch information
ValentinaHutter authored Sep 13, 2024
1 parent ac070ca commit 4bd5081
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions openeo_processes_dask/process_implementations/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"array_contains",
"array_find",
"array_labels",
"array_apply",
"first",
"last",
"order",
Expand Down Expand Up @@ -213,6 +214,22 @@ def array_labels(data: ArrayLike) -> ArrayLike:
return np.arange(len(data))


def array_apply(
data: ArrayLike, process: Callable, context: Optional[Any] = None
) -> ArrayLike:
if not context:
context = {}
positional_parameters = {"x": 0}
named_parameters = {"x": data, "context": context}
if callable(process):
process_to_apply = np.vectorize(process)
return process_to_apply(
data,
positional_parameters=positional_parameters,
named_parameters=named_parameters,
)


def first(
data: ArrayLike,
ignore_nodata: Optional[bool] = True,
Expand Down
12 changes: 12 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ArrayElementNotAvailable,
TooManyDimensions,
)
from openeo_processes_dask.process_implementations.math import add
from tests.general_checks import general_output_checks
from tests.mockdata import create_fake_rastercube

Expand Down Expand Up @@ -251,6 +252,17 @@ def test_array_labels():
array_labels(np.array([[1, 0, 3, 2], [5, 0, 6, 4]]))


def test_array_apply(process_registry):
_process = partial(
process_registry["add"].implementation,
y=1,
x=ParameterReference(from_parameter="x"),
)

output_cube = array_apply(data=np.array([1, 2, 3, 4, 5, 6]), process=_process)
assert (output_cube == [2, 3, 4, 5, 6, 7]).all()


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 4bd5081

Please sign in to comment.