Skip to content

vectorized indexing tests #31

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 1 commit into from
May 11, 2025
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
33 changes: 33 additions & 0 deletions xarray_array_testing/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hypothesis.extra.numpy as npst
import hypothesis.strategies as st
import xarray as xr
import xarray.testing.strategies as xrst
from hypothesis import given

Expand Down Expand Up @@ -51,6 +52,24 @@ def orthogonal_indexers(draw, sizes, indexer_types):
return {dim: draw(indexer) for dim, indexer in concrete_indexers.items()}


@st.composite
def vectorized_indexers(draw, sizes):
max_size = max(sizes.values())
shape = draw(st.integers(min_value=1, max_value=max_size))
dtypes = npst.integer_dtypes()

indexers = {
dim: npst.arrays(
dtypes, shape, elements={"min_value": -size, "max_value": size - 1}
)
for dim, size in sizes.items()
}

return {
dim: xr.Variable("points", draw(indexer)) for dim, indexer in indexers.items()
}


class IndexingTests(DuckArrayTestMixin):
@property
def orthogonal_indexer_types(self):
Expand Down Expand Up @@ -78,3 +97,17 @@ def test_variable_isel_orthogonal(self, data):

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

@given(st.data())
def test_variable_isel_vectorized(self, data):
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))
idx = data.draw(vectorized_indexers(variable.sizes))

with self.expected_errors("isel_vectorized", variable=variable):
actual = variable.isel(idx).data

raw_indexers = {dim: idx.get(dim, slice(None)) for dim in variable.dims}
expected = variable.data[*raw_indexers.values()]

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