Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update rename labels #302

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <lukas.weidenholzer@eodc.eu>", "Sean Hoyal <sean.hoyal@eodc.eu>", "Valentina Hutter <valentina.hutter@eodc.eu>"]
maintainers = ["EODC Staff <support@eodc.eu>"]
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