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

clip: throw exception is min > max #208

Merged
merged 4 commits into from
Feb 12, 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
4 changes: 4 additions & 0 deletions openeo_processes_dask/process_implementations/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ class LabelMismatch(OpenEOException):

class KernelDimensionsUneven(OpenEOException):
pass


class MinMaxSwapped(OpenEOException):
pass
5 changes: 5 additions & 0 deletions openeo_processes_dask/process_implementations/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_is_dask_array,
)
from openeo_processes_dask.process_implementations.exceptions import (
MinMaxSwapped,
OpenEOException,
QuantilesParameterConflict,
QuantilesParameterMissing,
Expand Down Expand Up @@ -263,6 +264,10 @@ def extrema(data, ignore_nodata=True, axis=None, keepdims=False):


def clip(x, min, max):
if min > max:
raise MinMaxSwapped(
"The minimum value should be lower than or equal to the maximum value."
)
# Cannot use positional arguments to pass min and max into np.clip, this will not work with dask.
return np.clip(x, min, max)

Expand Down
3 changes: 3 additions & 0 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
import pytest

from openeo_processes_dask.process_implementations.exceptions import MinMaxSwapped
from openeo_processes_dask.process_implementations.math import *


Expand Down Expand Up @@ -86,3 +87,5 @@ def test_clip():
result_dask = clip(dask_array, min=0, max=2)
assert np.array_equal(result_np, result_dask.compute(), equal_nan=True)
assert np.array_equal(result_np, np.array([2, 0]))
with pytest.raises(MinMaxSwapped):
clip(dask_array, 10, 0)
Loading