1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
import pytest
pytest.importorskip("hypothesis")
import hypothesis.strategies as st
from hypothesis import given
import xarray as xr
import xarray.testing.strategies as xrst
def _slice_size(s: slice, dim_size: int) -> int:
"""Compute the size of a slice applied to a dimension."""
return len(range(*s.indices(dim_size)))
@given(
st.data(),
xrst.variables(dims=xrst.dimension_sizes(min_dims=1, max_dims=4, min_side=1)),
)
def test_basic_indexing(data, var):
"""Test that basic indexers produce expected output shape."""
idxr = data.draw(xrst.basic_indexers(sizes=var.sizes))
result = var.isel(idxr)
expected_shape = tuple(
_slice_size(idxr[d], var.sizes[d]) if d in idxr else var.sizes[d]
for d in result.dims
)
assert result.shape == expected_shape
@given(
st.data(),
xrst.variables(dims=xrst.dimension_sizes(min_dims=1, max_dims=4, min_side=1)),
)
def test_outer_indexing(data, var):
"""Test that outer array indexers produce expected output shape."""
idxr = data.draw(xrst.outer_array_indexers(sizes=var.sizes, min_dims=1))
result = var.isel(idxr)
expected_shape = tuple(
len(idxr[d]) if d in idxr else var.sizes[d] for d in result.dims
)
assert result.shape == expected_shape
@given(
st.data(),
xrst.variables(dims=xrst.dimension_sizes(min_dims=2, max_dims=4, min_side=1)),
)
def test_vectorized_indexing(data, var):
"""Test that vectorized indexers produce expected output shape."""
da = xr.DataArray(var)
idxr = data.draw(xrst.vectorized_indexers(sizes=var.sizes))
result = da.isel(idxr)
# TODO: this logic works because the dims in idxr don't overlap with da.dims
# Compute expected shape from result dims
# Non-indexed dims keep their original size, indexed dims get broadcast size
broadcast_result = xr.broadcast(*idxr.values())
broadcast_sizes = dict(
zip(broadcast_result[0].dims, broadcast_result[0].shape, strict=True)
)
expected_shape = tuple(
var.sizes[d] if d in var.sizes else broadcast_sizes[d] for d in result.dims
)
assert result.shape == expected_shape
|