Skip to content

Commit

Permalink
Add DataCube.filter_labels()
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Nov 28, 2023
1 parent 128d261 commit 4b66d8e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `DataCube.filter_labels()`

### Changed

### Removed
Expand Down
24 changes: 23 additions & 1 deletion openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import typing
import warnings
from builtins import staticmethod
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union, Callable

import numpy as np
import shapely.geometry
Expand Down Expand Up @@ -475,6 +475,28 @@ def filter_bands(self, bands: Union[List[Union[str, int]], str]) -> DataCube:
)
return cube

@openeo_process
def filter_labels(
self, condition: Union[PGNode, Callable], dimension: str, context: Optional[dict] = None
) -> DataCube:
"""
Filters the dimension labels in the data cube for the given dimension.
Only the dimension labels that match the specified condition are preserved,
all other labels with their corresponding data get removed.
:param condition: the "child callback" which will be given a single label value (number or string)
and returns a boolean expressing if the label should be preserved.
Also see :ref:`callbackfunctions`.
:param dimension: The name of the dimension to filter on.
.. versionadded:: 0.27.0
"""
condition = build_child_callback(condition, parent_parameters=["value"])
return self.process(
process_id="filter_labels",
arguments=dict_no_none(data=THIS, condition=condition, dimension=dimension, context=context),
)

band_filter = legacy_alias(filter_bands, "band_filter", since="0.1.0")

def band(self, band: Union[str, int]) -> DataCube:
Expand Down
10 changes: 9 additions & 1 deletion openeo/rest/vectorcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,17 @@ def filter_labels(
self, condition: Union[PGNode, Callable], dimension: str, context: Optional[dict] = None
) -> VectorCube:
"""
Filters the dimension labels in the data cube for the given dimension.
Only the dimension labels that match the specified condition are preserved,
all other labels with their corresponding data get removed.
:param condition: the "child callback" which will be given a single label value (number or string)
and returns a boolean expressing if the label should be preserved.
Also see :ref:`callbackfunctions`.
:param dimension: The name of the dimension to filter on.
.. versionadded:: 0.22.0
"""
# TODO #459 docs
condition = build_child_callback(condition, parent_parameters=["value"])
return self.process(
process_id="filter_labels",
Expand Down
23 changes: 23 additions & 0 deletions tests/rest/datacube/test_datacube100.py
Original file line number Diff line number Diff line change
Expand Up @@ -2223,6 +2223,29 @@ def test_filter_spatial_callback(con100):
}


def test_filter_labels_callback(con100):
cube = con100.load_collection("S2")
cube = cube.filter_labels(condition=lambda t: t.text_contains("-02-2"), dimension="t")
assert get_download_graph(cube, drop_save_result=True, drop_load_collection=True) == {
"filterlabels1": {
"process_id": "filter_labels",
"arguments": {
"data": {"from_node": "loadcollection1"},
"condition": {
"process_graph": {
"textcontains1": {
"process_id": "text_contains",
"arguments": {"data": {"from_parameter": "value"}, "pattern": "-02-2"},
"result": True,
}
}
},
"dimension": "t",
},
},
}


def test_custom_process_kwargs_datacube(con100: Connection):
img = con100.load_collection("S2")
res = img.process(process_id="foo", data=img, bar=123)
Expand Down

0 comments on commit 4b66d8e

Please sign in to comment.