diff --git a/CHANGELOG.md b/CHANGELOG.md index c5ef1387f..081942007 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Support `rename_labels` on temporal dimension ([#274](https://github.com/Open-EO/openeo-python-client/issues/274)) + ## [0.9.2] - 2022-01-14 diff --git a/openeo/metadata.py b/openeo/metadata.py index cc0aed088..37603e9d3 100644 --- a/openeo/metadata.py +++ b/openeo/metadata.py @@ -38,7 +38,8 @@ def rename_labels(self, target, source) -> 'Dimension': @param source: Source labels, or empty list @return: A new dimension with modified labels, or the same if no change is applied. """ - raise MetadataException("Trying to rename labels of dimension %s of type %s, which is not supported." % (self.name,self.type)) + # In general, we don't have/manage label info here, so do nothing. + return Dimension(type=self.type, name=self.name) class SpatialDimension(Dimension): @@ -153,23 +154,21 @@ def append_band(self, band: Band) -> 'BandDimension': ) def rename_labels(self, target, source) -> 'Dimension': - if not source or len(source) == 0: - source = None - elif len(target) != len(source): - raise ValueError('In rename_labels, the number of labels in target should equal length of source, or the number of original labels in the dimension. Received target labels: %s and source: %s' % (str(target),str(source))) - if source: + if len(target) != len(source): + raise ValueError( + 'In rename_labels, `target` and `source` should have same number of labels, ' + 'but got: `target` {t} and `source` {s}'.format(t=target, s=source) + ) new_bands = self.bands.copy() - for old_name,new_name in zip(source,target): + for old_name, new_name in zip(source, target): band_index = self.band_index(old_name) the_band = new_bands[band_index] new_bands[band_index] = Band(new_name, the_band.common_name, the_band.wavelength_um, the_band.aliases, the_band.gsd) else: - new_bands = [] - for new_name in target: - new_bands.append(Band(name=new_name,common_name=None, wavelength_um=None)) - return BandDimension(self.name, new_bands) + new_bands = [Band(name=n, common_name=None, wavelength_um=None) for n in target] + return BandDimension(name=self.name, bands=new_bands) class CollectionMetadata: diff --git a/openeo/rest/datacube.py b/openeo/rest/datacube.py index 11cbc5017..a363d35ea 100644 --- a/openeo/rest/datacube.py +++ b/openeo/rest/datacube.py @@ -1206,7 +1206,7 @@ def rename_labels(self, dimension: str, target: list, source: list = None) -> 'D target=target, source=source ), - metadata=self.metadata.rename_labels(dimension,target,source) + metadata=self.metadata.rename_labels(dimension, target, source) ) def linear_scale_range(self, input_min, input_max, output_min, output_max) -> 'DataCube': diff --git a/tests/rest/datacube/test_datacube100.py b/tests/rest/datacube/test_datacube100.py index b57fe08a1..f2120f2c8 100644 --- a/tests/rest/datacube/test_datacube100.py +++ b/tests/rest/datacube/test_datacube100.py @@ -1268,6 +1268,46 @@ def test_dimension_labels_invalid(con100): assert cube.flat_graph()["dimensionlabels1"]["arguments"]["dimension"] == "unv6lidd" +def test_rename_labels_bands(con100): + cube = con100.load_collection("S2").rename_labels("bands", target=["blue", "green"], source=["B02", "B03"]) + assert cube.flat_graph() == { + "loadcollection1": { + "process_id": "load_collection", + "arguments": {"id": "S2", "spatial_extent": None, "temporal_extent": None}, + }, + "renamelabels1": { + "process_id": "rename_labels", + "arguments": { + "data": {"from_node": "loadcollection1"}, + "dimension": "bands", + "target": ["blue", "green"], + "source": ["B02", "B03"], + }, + "result": True + }, + } + + +def test_rename_labels_temporal(con100): + """https://github.com/Open-EO/openeo-python-client/issues/274""" + cube = con100.load_collection("S2").rename_labels("t", target=["2019", "2020", "2021"]) + assert cube.flat_graph() == { + "loadcollection1": { + "process_id": "load_collection", + "arguments": {"id": "S2", "spatial_extent": None, "temporal_extent": None}, + }, + "renamelabels1": { + "process_id": "rename_labels", + "arguments": { + "data": {"from_node": "loadcollection1"}, + "dimension": "t", + "target": ["2019", "2020", "2021"], + }, + "result": True + }, + } + + def test_fit_curve_callback(con100: Connection): from openeo.processes import array_element def model(x, parameters): diff --git a/tests/test_metadata.py b/tests/test_metadata.py index f1cff7824..34461ac96 100644 --- a/tests/test_metadata.py +++ b/tests/test_metadata.py @@ -126,6 +126,15 @@ def test_band_dimension_rename_labels_with_source(): assert newdim.band_names == ['B02','2','B04'] +def test_band_dimension_rename_labels_with_source_mismatch(): + b02 = Band("B02", "blue", 0.490) + b03 = Band("B03", "green", 0.560) + bdim = BandDimension(name="bs", bands=[b02, b03]) + metadata = CollectionMetadata({}, dimensions=[bdim]) + with pytest.raises(ValueError, match="should have same number of labels, but got"): + _ = metadata.rename_labels("bs", target=['2', "3"], source=['B03']) + + def assert_same_dimensions(dims1: List[Dimension], dims2: List[Dimension]): assert sorted(dims1, key=lambda d: d.name) == sorted(dims2, key=lambda d: d.name)