| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import math |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import assume, given, note, strategies as st |
| 16 |
from hypothesis.extra._array_helpers import NDIM_MAX |
| 17 |
|
| 18 |
from tests.common.debug import assert_all_examples, find_any |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.parametrize( |
| 22 |
"condition", |
| 23 |
[ |
| 24 |
lambda ix: Ellipsis in ix, |
| 25 |
lambda ix: Ellipsis not in ix, |
| 26 |
lambda ix: None in ix, |
| 27 |
lambda ix: None not in ix, |
| 28 |
], |
| 29 |
) |
| 30 |
def test_generate_optional_indices(xp, xps, condition): |
| 31 |
"""Strategy can generate indices with optional values.""" |
| 32 |
strat = ( |
| 33 |
xps.array_shapes(min_dims=1, max_dims=32) |
| 34 |
.flatmap(lambda s: xps.indices(s, allow_newaxis=True)) |
| 35 |
.map(lambda idx: idx if isinstance(idx, tuple) else (idx,)) |
| 36 |
) |
| 37 |
find_any(strat, condition) |
| 38 |
|
| 39 |
|
| 40 |
def test_cannot_generate_newaxis_when_disabled(xp, xps): |
| 41 |
"""Strategy does not generate newaxis when disabled (i.e. the default).""" |
| 42 |
assert_all_examples( |
| 43 |
xps.indices((3, 3, 3)), lambda idx: idx == ... or None not in idx |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def test_generate_indices_for_0d_shape(xp, xps): |
| 48 |
"""Strategy only generates empty tuples or Ellipsis as indices for an empty |
| 49 |
shape.""" |
| 50 |
assert_all_examples( |
| 51 |
xps.indices(shape=(), allow_ellipsis=True), |
| 52 |
lambda idx: idx in [(), Ellipsis, (Ellipsis,)], |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
def test_generate_tuples_and_non_tuples_for_1d_shape(xp, xps): |
| 57 |
"""Strategy can generate tuple and non-tuple indices with a 1-dimensional shape.""" |
| 58 |
strat = xps.indices(shape=(1,), allow_ellipsis=True) |
| 59 |
find_any(strat, lambda ix: isinstance(ix, tuple)) |
| 60 |
find_any(strat, lambda ix: not isinstance(ix, tuple)) |
| 61 |
|
| 62 |
|
| 63 |
def test_generate_long_ellipsis(xp, xps): |
| 64 |
"""Strategy can replace runs of slice(None) with Ellipsis. |
| 65 |
|
| 66 |
We specifically test if [0,...,0] is generated alongside [0,:,:,:,0] |
| 67 |
""" |
| 68 |
strat = xps.indices(shape=(1, 0, 0, 0, 1), max_dims=3, allow_ellipsis=True) |
| 69 |
find_any(strat, lambda ix: len(ix) == 3 and ix[1] == Ellipsis) |
| 70 |
find_any( |
| 71 |
strat, |
| 72 |
lambda ix: len(ix) == 5 |
| 73 |
and all(isinstance(key, slice) and key == slice(None) for key in ix[1:3]), |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
def test_indices_replaces_whole_axis_slices_with_ellipsis(xp, xps): |
| 78 |
# `slice(None)` (aka `:`) is the only valid index for an axis of size |
| 79 |
# zero, so if all dimensions are 0 then a `...` will replace all the |
| 80 |
# slices because we generate `...` for entire contiguous runs of `:` |
| 81 |
assert_all_examples( |
| 82 |
xps.indices(shape=(0, 0, 0, 0, 0), max_dims=5).filter( |
| 83 |
lambda idx: isinstance(idx, tuple) and Ellipsis in idx |
| 84 |
), |
| 85 |
lambda idx: slice(None) not in idx, |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
def test_efficiently_generate_indexers(xp, xps): |
| 90 |
"""Generation is not too slow.""" |
| 91 |
find_any(xps.indices((3, 3, 3, 3, 3))) |
| 92 |
|
| 93 |
|
| 94 |
@given(allow_newaxis=st.booleans(), allow_ellipsis=st.booleans(), data=st.data()) |
| 95 |
def test_generate_valid_indices(xp, xps, allow_newaxis, allow_ellipsis, data): |
| 96 |
"""Strategy generates valid indices.""" |
| 97 |
shape = data.draw( |
| 98 |
xps.array_shapes(min_dims=1, max_side=4) |
| 99 |
| xps.array_shapes(min_dims=1, min_side=0, max_side=10), |
| 100 |
label="shape", |
| 101 |
) |
| 102 |
min_dims = data.draw( |
| 103 |
st.integers(0, len(shape) if not allow_newaxis else len(shape) + 2), |
| 104 |
label="min_dims", |
| 105 |
) |
| 106 |
max_dims = data.draw( |
| 107 |
st.none() |
| 108 |
| st.integers(min_dims, len(shape) if not allow_newaxis else NDIM_MAX), |
| 109 |
label="max_dims", |
| 110 |
) |
| 111 |
indexer = data.draw( |
| 112 |
xps.indices( |
| 113 |
shape, |
| 114 |
min_dims=min_dims, |
| 115 |
max_dims=max_dims, |
| 116 |
allow_newaxis=allow_newaxis, |
| 117 |
allow_ellipsis=allow_ellipsis, |
| 118 |
), |
| 119 |
label="indexer", |
| 120 |
) |
| 121 |
|
| 122 |
_indexer = indexer if isinstance(indexer, tuple) else (indexer,) |
| 123 |
# Check that disallowed things are indeed absent |
| 124 |
if not allow_ellipsis: |
| 125 |
assert Ellipsis not in _indexer |
| 126 |
if not allow_newaxis: |
| 127 |
assert None not in _indexer # i.e. xp.newaxis |
| 128 |
# Check index is composed of valid objects |
| 129 |
for i in _indexer: |
| 130 |
assert isinstance(i, (int, slice)) or i is None or i == Ellipsis |
| 131 |
# Check indexer does not flat index |
| 132 |
nonexpanding_indexer = [i for i in _indexer if i is not None] |
| 133 |
if Ellipsis in _indexer: |
| 134 |
assert sum(i == Ellipsis for i in _indexer) == 1 |
| 135 |
# Note ellipsis can index 0 axes |
| 136 |
assert len(nonexpanding_indexer) <= len(shape) + 1 |
| 137 |
else: |
| 138 |
assert len(nonexpanding_indexer) == len(shape) |
| 139 |
|
| 140 |
if 0 in shape: |
| 141 |
# If there's a zero in the shape, the array will have no elements. |
| 142 |
array = xp.zeros(shape) |
| 143 |
assert array.size == 0 # sanity check |
| 144 |
elif math.prod(shape) <= 10**5: |
| 145 |
# If it's small enough to instantiate, do so with distinct elements. |
| 146 |
array = xp.reshape(xp.arange(math.prod(shape)), shape) |
| 147 |
else: |
| 148 |
# We can't cheat on this one, so just try another. |
| 149 |
assume(False) |
| 150 |
# Finally, check that we can use our indexer without error |
| 151 |
note(f"{array=}") |
| 152 |
array[indexer] |