Skip to content

Commit

Permalink
Fix #274 support rename_labels on temporal dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Mar 9, 2022
1 parent 8481263 commit 0ceb689
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 10 additions & 11 deletions openeo/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
40 changes: 40 additions & 0 deletions tests/rest/datacube/test_datacube100.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 0ceb689

Please sign in to comment.