Skip to content

Parametrize over different reduction operations #5

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

Merged
merged 2 commits into from
Aug 22, 2024
Merged
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: 8 additions & 14 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 pytest
import xarray.testing.strategies as xrst
from hypothesis import given

Expand All @@ -12,22 +13,15 @@ class ReductionTests(DuckArrayTestMixin):
def expected_errors(op, **parameters):
return nullcontext()

@pytest.mark.parametrize("op", ["mean", "sum", "prod", "std", "var"])

Choose a reason for hiding this comment

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

One of "min" or "max" would be good for checking that dtype is preserved. I'm not sure "std" adds much over "var".

Copy link
Member Author

Choose a reason for hiding this comment

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

These were really just chosen in order to cover some known bugs with cubed. Specifically that cubed doesn't yet implement std or var, and the array API standard only requires mean to work with floats, not integers.

Eventually we would want a test suite that covers everything, this is just meant to be a proof-of-principle for discussing overall design.

@given(st.data())
def test_variable_mean(self, data):
def test_variable_mean(self, op, data):
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))

with self.expected_errors("mean", variable=variable):
actual = variable.mean().data
expected = self.xp.mean(variable.data)

self.assert_equal(actual, expected)

@given(st.data())
def test_variable_prod(self, data):
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))

with self.expected_errors("prod", variable=variable):
actual = variable.prod().data
expected = self.xp.prod(variable.data)
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)

self.assert_equal(actual, expected)