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

fix: support axis keyword in array_contains #152

Merged
merged 4 commits into from
Aug 18, 2023
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
11 changes: 5 additions & 6 deletions openeo_processes_dask/process_implementations/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def array_concat(array1: ArrayLike, array2: ArrayLike) -> ArrayLike:
return concat


def array_contains(data: ArrayLike, value: Any) -> bool:
def array_contains(data: ArrayLike, value: Any, axis=None) -> bool:
# TODO: Contrary to the process spec, our implementation does interpret temporal strings before checking them here
# This is somewhat implicit in how we currently parse parameters, so cannot be easily changed.

Expand All @@ -135,15 +135,14 @@ def array_contains(data: ArrayLike, value: Any) -> bool:
for dtype in valid_dtypes:
if np.issubdtype(type(value), dtype):
value_is_valid = True
if not value_is_valid:
if len(np.shape(data)) != 1 and axis is None:
return False

if len(np.shape(data)) != 1:
if not value_is_valid:
return False
if pd.isnull(value):
return np.isnan(data).any()
return np.isnan(data).any(axis=axis)
else:
return np.isin(data, value).any()
return np.isin(data, value).any(axis=axis)


def array_find(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ def test_array_contains(data, value, expected):
assert dask_result == expected or dask_result.compute() == expected


def test_array_contains_axis():
data = np.array([[4, 5, 6], [5, 7, 9]])

result_0 = array_contains(data, 5, axis=0)
np.testing.assert_array_equal(result_0, np.array([True, True, False]))

result_1 = array_contains(data, 5, axis=1)
np.testing.assert_array_equal(result_1, np.array([True, True]))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not have an assert, yet.


def test_array_contains_object_dtype():
assert not array_contains([{"a": "b"}, {"c": "d"}], {"a": "b"})
assert not array_contains(np.array([{"a": "b"}, {"c": "d"}]), {"a": "b"})
Expand Down Expand Up @@ -397,3 +407,19 @@ def test_reduce_dimension(
)
assert output_cube.dims == ("x", "y", "t")
xr.testing.assert_equal(output_cube, xr.ones_like(output_cube))

input_cube[0, 0, 0, 0] = 99999
_process = partial(
process_registry["array_contains"].implementation,
data=ParameterReference(from_parameter="data"),
value=99999,
)
output_cube = reduce_dimension(data=input_cube, reducer=_process, dimension="bands")
general_output_checks(
input_cube=input_cube,
output_cube=output_cube,
verify_attrs=False,
verify_crs=True,
)
assert output_cube[0, 0, 0].data.compute().item() is True
assert not output_cube[slice(1, None), :, :].data.compute().any()