Skip to content

Commit

Permalink
update rename labels (#302)
Browse files Browse the repository at this point in the history
* update rename labels

* pre-commit hook

* v2024.11.5
  • Loading branch information
ValentinaHutter authored Nov 29, 2024
1 parent 5f6d095 commit 9e828bb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
22 changes: 18 additions & 4 deletions openeo_processes_dask/process_implementations/cubes/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,44 @@ def rename_labels(
raise DimensionNotAvailable(
f"Provided dimension ({dimension}) not found in data.dims: {data_rename.dims}"
)
if source:
if len(source) > 0:
if len(source) != len(target):
raise Exception(
f"LabelMismatch - The number of labels in the parameters `source` and `target` don't match."
)

time = False
if dimension in data.openeo.temporal_dims:
time = True

source_labels = data_rename[dimension].values
if time:
source_labels = np.array(source_labels, dtype="datetime64[s]")
elif np.issubdtype(source_labels.dtype, np.datetime64):
source_labels = source_labels.astype("datetime64[s]")
time = True
if isinstance(source_labels, np.ndarray):
source_labels = source_labels.tolist()
if isinstance(target, np.ndarray):
target = target.tolist()

if time:
source = np.array(source, dtype="datetime64[s]")
if isinstance(source, np.ndarray):
if np.issubdtype(source.dtype, np.datetime64):
source = source.astype("datetime64[s]")
source = source.tolist()
target_values = []

for label in source_labels:
if label in target:
raise Exception(f"LabelExists - A label with the specified name exists.")
if source:
if len(source) > 0:
if label in source:
target_values.append(target[source.index(label)])
else:
target_values.append(label)

if not source:
if len(source) == 0:
if len(source_labels) == len(target):
data_rename[dimension] = target
elif len(target) < len(source_labels):
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.11.4"
version = "2024.11.5"
description = "Python implementations of many OpenEO processes, dask-friendly by default."
authors = ["Lukas Weidenholzer <[email protected]>", "Sean Hoyal <[email protected]>", "Valentina Hutter <[email protected]>"]
maintainers = ["EODC Staff <[email protected]>"]
Expand Down
31 changes: 24 additions & 7 deletions tests/test_dimensions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import numpy as np
import pytest

from openeo_processes_dask.process_implementations.cubes.general import (
add_dimension,
drop_dimension,
rename_dimension,
rename_labels,
trim_cube,
)
from openeo_processes_dask.process_implementations.cubes.general import *
from openeo_processes_dask.process_implementations.exceptions import (
DimensionLabelCountMismatch,
DimensionNotAvailable,
Expand Down Expand Up @@ -127,6 +121,29 @@ def test_rename_labels(temporal_interval, bounding_box, random_raster_data):
)


@pytest.mark.parametrize("size", [(30, 30, 2, 4)])
@pytest.mark.parametrize("dtype", [np.float32])
def test_rename_labels_time(temporal_interval, bounding_box, random_raster_data):
input_cube = create_fake_rastercube(
data=random_raster_data,
spatial_extent=bounding_box,
temporal_extent=temporal_interval,
bands=["B02", "B03", "B04", "B08"],
backend="dask",
)

t_labels = dimension_labels(input_cube, dimension="t")
output_cube = rename_labels(
input_cube, dimension="t", source=t_labels, target=["first_date", "second_date"]
)
assert "first_date" in output_cube["t"].values

output_cube_2 = rename_labels(
input_cube, dimension="t", source=[t_labels[-1]], target=["second_date"]
)
assert "second_date" in output_cube_2["t"].values


@pytest.mark.parametrize("size", [(30, 30, 20, 4)])
@pytest.mark.parametrize("dtype", [np.float32])
def test_trim_cube(temporal_interval, bounding_box, random_raster_data):
Expand Down

0 comments on commit 9e828bb

Please sign in to comment.