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

labelled arrays #155

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions openeo_processes_dask/process_implementations/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pandas as pd
import xarray as xr
from numpy.typing import ArrayLike
from openeo_pg_parser_networkx.pg_schema import DateTime
from xarray.core.duck_array_ops import isnull, notnull

from openeo_processes_dask.process_implementations.cubes.utils import _is_dask_array
Expand Down Expand Up @@ -42,6 +43,8 @@
label: Optional[str] = None,
return_nodata: Optional[bool] = False,
axis=None,
context=None,
dim_labels=None,
):
if index is None and label is None:
raise ArrayElementParameterMissing(
Expand All @@ -54,14 +57,20 @@
)

if label is not None:
raise NotImplementedError(
"labelled arrays are currently not implemented. Please use index instead."
)
if isinstance(label, DateTime):
label = label.to_numpy()

Check warning on line 61 in openeo_processes_dask/process_implementations/arrays.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/arrays.py#L61

Added line #L61 was not covered by tests
(index,) = np.where(dim_labels == label)
if len(index) == 0:
index = None

Check warning on line 64 in openeo_processes_dask/process_implementations/arrays.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/arrays.py#L64

Added line #L64 was not covered by tests
else:
index = index[0]

try:
if index is not None:
element = np.take(data, index, axis=axis)
return element
else:
raise IndexError

Check warning on line 73 in openeo_processes_dask/process_implementations/arrays.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/arrays.py#L73

Added line #L73 was not covered by tests
except IndexError:
if return_nodata:
logger.warning(
Expand Down
8 changes: 7 additions & 1 deletion openeo_processes_dask/process_implementations/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ def wrapper(
else:
resolved_kwargs[k] = arg

special_args = ["axis", "keepdims", "source_transposed_axis"]
special_args = [
"axis",
"keepdims",
"source_transposed_axis",
"context",
"dim_labels",
]
# Remove 'axis' and keepdims parameter if not expected in function signature.
for arg in special_args:
if arg not in inspect.signature(f).parameters:
Expand Down
7 changes: 4 additions & 3 deletions openeo_processes_dask/process_implementations/cubes/reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ def reduce_dimension(
f"Provided dimension ({dimension}) not found in data.dims: {data.dims}"
)

positional_parameters = {"data": 0}
named_parameters = {"context": context}
dim_labels = data[dimension].values

positional_parameters = {"data": 0}
reduced_data = data.reduce(
reducer,
dim=dimension,
keep_attrs=True,
positional_parameters=positional_parameters,
named_parameters=named_parameters,
context=context,
dim_labels=dim_labels,
)

# Preset
Expand Down
18 changes: 18 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ def test_array_element(

xr.testing.assert_equal(output_cube, input_cube.isel({"bands": 1}, drop=True))

# Use a label
_process = partial(
process_registry["array_element"].implementation,
label="B02",
data=ParameterReference(from_parameter="data"),
)

output_cube = reduce_dimension(data=input_cube, reducer=_process, dimension="bands")

general_output_checks(
input_cube=input_cube,
output_cube=output_cube,
verify_attrs=False,
verify_crs=True,
)

xr.testing.assert_equal(output_cube, input_cube.loc[{"bands": "B02"}].drop("bands"))

# When the index is out of range, we expect an ArrayElementNotAvailable exception to be thrown
_process_not_available = partial(
process_registry["array_element"].implementation,
Expand Down