Skip to content

Commit 9e828bb

Browse files
update rename labels (#302)
* update rename labels * pre-commit hook * v2024.11.5
1 parent 5f6d095 commit 9e828bb

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

openeo_processes_dask/process_implementations/cubes/general.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,30 +158,44 @@ def rename_labels(
158158
raise DimensionNotAvailable(
159159
f"Provided dimension ({dimension}) not found in data.dims: {data_rename.dims}"
160160
)
161-
if source:
161+
if len(source) > 0:
162162
if len(source) != len(target):
163163
raise Exception(
164164
f"LabelMismatch - The number of labels in the parameters `source` and `target` don't match."
165165
)
166166

167+
time = False
168+
if dimension in data.openeo.temporal_dims:
169+
time = True
170+
167171
source_labels = data_rename[dimension].values
172+
if time:
173+
source_labels = np.array(source_labels, dtype="datetime64[s]")
174+
elif np.issubdtype(source_labels.dtype, np.datetime64):
175+
source_labels = source_labels.astype("datetime64[s]")
176+
time = True
168177
if isinstance(source_labels, np.ndarray):
169178
source_labels = source_labels.tolist()
170179
if isinstance(target, np.ndarray):
171180
target = target.tolist()
172-
181+
if time:
182+
source = np.array(source, dtype="datetime64[s]")
183+
if isinstance(source, np.ndarray):
184+
if np.issubdtype(source.dtype, np.datetime64):
185+
source = source.astype("datetime64[s]")
186+
source = source.tolist()
173187
target_values = []
174188

175189
for label in source_labels:
176190
if label in target:
177191
raise Exception(f"LabelExists - A label with the specified name exists.")
178-
if source:
192+
if len(source) > 0:
179193
if label in source:
180194
target_values.append(target[source.index(label)])
181195
else:
182196
target_values.append(label)
183197

184-
if not source:
198+
if len(source) == 0:
185199
if len(source_labels) == len(target):
186200
data_rename[dimension] = target
187201
elif len(target) < len(source_labels):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "openeo-processes-dask"
3-
version = "2024.11.4"
3+
version = "2024.11.5"
44
description = "Python implementations of many OpenEO processes, dask-friendly by default."
55
authors = ["Lukas Weidenholzer <lukas.weidenholzer@eodc.eu>", "Sean Hoyal <sean.hoyal@eodc.eu>", "Valentina Hutter <valentina.hutter@eodc.eu>"]
66
maintainers = ["EODC Staff <support@eodc.eu>"]

tests/test_dimensions.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from openeo_processes_dask.process_implementations.cubes.general import (
5-
add_dimension,
6-
drop_dimension,
7-
rename_dimension,
8-
rename_labels,
9-
trim_cube,
10-
)
4+
from openeo_processes_dask.process_implementations.cubes.general import *
115
from openeo_processes_dask.process_implementations.exceptions import (
126
DimensionLabelCountMismatch,
137
DimensionNotAvailable,
@@ -127,6 +121,29 @@ def test_rename_labels(temporal_interval, bounding_box, random_raster_data):
127121
)
128122

129123

124+
@pytest.mark.parametrize("size", [(30, 30, 2, 4)])
125+
@pytest.mark.parametrize("dtype", [np.float32])
126+
def test_rename_labels_time(temporal_interval, bounding_box, random_raster_data):
127+
input_cube = create_fake_rastercube(
128+
data=random_raster_data,
129+
spatial_extent=bounding_box,
130+
temporal_extent=temporal_interval,
131+
bands=["B02", "B03", "B04", "B08"],
132+
backend="dask",
133+
)
134+
135+
t_labels = dimension_labels(input_cube, dimension="t")
136+
output_cube = rename_labels(
137+
input_cube, dimension="t", source=t_labels, target=["first_date", "second_date"]
138+
)
139+
assert "first_date" in output_cube["t"].values
140+
141+
output_cube_2 = rename_labels(
142+
input_cube, dimension="t", source=[t_labels[-1]], target=["second_date"]
143+
)
144+
assert "second_date" in output_cube_2["t"].values
145+
146+
130147
@pytest.mark.parametrize("size", [(30, 30, 20, 4)])
131148
@pytest.mark.parametrize("dtype", [np.float32])
132149
def test_trim_cube(temporal_interval, bounding_box, random_raster_data):

0 commit comments

Comments
 (0)