Skip to content

Commit

Permalink
Merge branch 'Open-EO:main' into fix_resample_spatial
Browse files Browse the repository at this point in the history
  • Loading branch information
clausmichele authored Feb 28, 2024
2 parents 9df6f9c + 9fcafef commit f363715
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 10 deletions.
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 @@ -43,6 +44,8 @@ def array_element(
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 @@ -55,14 +58,20 @@ def array_element(
)

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()
(index,) = np.where(dim_labels == label)
if len(index) == 0:
index = None
else:
index = index[0]

try:
if index is not None:
element = np.take(data, index, axis=axis)
return element
else:
raise IndexError
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", "context"]
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def apply_kernel(
)

def convolve(data, kernel, mode="constant", cval=0, fill_value=0):
dims = ("y", "x")
dims = data.openeo.spatial_dims
convolved = lambda data: scipy.ndimage.convolve(
data, kernel, mode=mode, cval=cval
)
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openeo-processes-dask"
version = "2024.2.2"
version = "2024.2.4"
description = "Python implementations of many OpenEO processes, dask-friendly by default."
authors = ["Lukas Weidenholzer <lukas.weidenholzer@eodc.eu>", "Sean Hoyal <sean.hoyal@eodc.eu>", "Valentina Hutter <valentina.hutter@eodc.eu>"]
maintainers = ["EODC Staff <support@eodc.eu>"]
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

0 comments on commit f363715

Please sign in to comment.