Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array apply #276

Merged
merged 10 commits into from
Sep 13, 2024
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
Loading