Skip to content
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 .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ jobs:
python=${{matrix.python-version}}
conda
- name: Install nightly xarray
run: |
python -m pip install --upgrade --pre -i https://pypi.anaconda.org/scientific-python-nightly-wheels/simple xarray
- name: Install xarray-array-testing
run: |
python -m pip install --no-deps -e .
Expand Down
73 changes: 73 additions & 0 deletions xarray_array_testing/reduction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contextlib import nullcontext

import hypothesis.strategies as st
import numpy as np
import pytest
import xarray.testing.strategies as xrst
from hypothesis import given
Expand All @@ -24,4 +25,76 @@ def test_variable_numerical_reduce(self, op, data):
# compute using xp.<OP>(array)
expected = getattr(self.xp, op)(variable.data)

assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}"
self.assert_equal(actual, expected)

@pytest.mark.parametrize("op", ["all", "any"])
@given(st.data())
def test_variable_boolean_reduce(self, op, data):
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))

with self.expected_errors(op, variable=variable):
# compute using xr.Variable.<OP>()
actual = getattr(variable, op)().data
# compute using xp.<OP>(array)
expected = getattr(self.xp, op)(variable.data)

assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}"
self.assert_equal(actual, expected)

@pytest.mark.parametrize("op", ["max", "min"])
@given(st.data())
def test_variable_order_reduce(self, op, data):
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))

with self.expected_errors(op, variable=variable):
# compute using xr.Variable.<OP>()
actual = getattr(variable, op)().data
# compute using xp.<OP>(array)
expected = getattr(self.xp, op)(variable.data)

assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}"
self.assert_equal(actual, expected)

@pytest.mark.parametrize("op", ["argmax", "argmin"])
@given(st.data())
def test_variable_order_reduce_index(self, op, data):
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))

with self.expected_errors(op, variable=variable):
# compute using xr.Variable.<OP>()
actual = {k: v.item() for k, v in getattr(variable, op)(dim=...).items()}

# compute using xp.<OP>(array)
index = getattr(self.xp, op)(variable.data)
unraveled = np.unravel_index(index, variable.shape)
expected = dict(zip(variable.dims, unraveled))

self.assert_equal(actual, expected)

@pytest.mark.parametrize(
"op",
[
"cumsum",
pytest.param(
"cumprod",
marks=pytest.mark.skip(reason="not yet included in the array api"),
),
],
)
@given(st.data())
def test_variable_cumulative_reduce(self, op, data):
array_api_names = {"cumsum": "cumulative_sum", "cumprod": "cumulative_prod"}
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))

with self.expected_errors(op, variable=variable):
# compute using xr.Variable.<OP>()
actual = getattr(variable, op)().data
# compute using xp.<OP>(array)
# Variable implements n-d cumulative ops by iterating over dims
expected = variable.data
for axis in range(variable.ndim):
expected = getattr(self.xp, array_api_names[op])(expected, axis=axis)

assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}"
self.assert_equal(actual, expected)