|
| 1 | +from contextlib import contextmanager, suppress |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +from array_api_compat import array_namespace, at, is_dask_array, is_jax_array, is_writeable_array |
| 7 | +from ._helpers import import_, all_libraries |
| 8 | + |
| 9 | + |
| 10 | +@contextmanager |
| 11 | +def assert_copy(x, copy: bool | None): |
| 12 | + # dask arrays are writeable, but writing to them will hot-swap the |
| 13 | + # dask graph inside the collection so that anything that references |
| 14 | + # the original graph, i.e. the input collection, won't be mutated. |
| 15 | + if copy is False and (not is_writeable_array(x) or is_dask_array(x)): |
| 16 | + with pytest.raises((TypeError, ValueError)): |
| 17 | + yield |
| 18 | + return |
| 19 | + |
| 20 | + xp = array_namespace(x) |
| 21 | + x_orig = xp.asarray(x, copy=True) |
| 22 | + yield |
| 23 | + |
| 24 | + expect_copy = ( |
| 25 | + copy if copy is not None else (not is_writeable_array(x) or is_dask_array(x)) |
| 26 | + ) |
| 27 | + np.testing.assert_array_equal((x == x_orig).all(), expect_copy) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(params=all_libraries + ["np_readonly"]) |
| 31 | +def x(request): |
| 32 | + library = request.param |
| 33 | + if library == "np_readonly": |
| 34 | + x = np.asarray([10, 20, 30]) |
| 35 | + x.flags.writeable = False |
| 36 | + else: |
| 37 | + lib = import_(library) |
| 38 | + x = lib.asarray([10, 20, 30]) |
| 39 | + return x |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("copy", [True, False, None]) |
| 43 | +@pytest.mark.parametrize( |
| 44 | + "op,arg,expect", |
| 45 | + [ |
| 46 | + ("apply", np.negative, [10, -20, 30]), |
| 47 | + ("set", 40, [10, 40, 30]), |
| 48 | + ("add", 40, [10, 60, 30]), |
| 49 | + ("subtract", 100, [10, -80, 30]), |
| 50 | + ("multiply", 2, [10, 40, 30]), |
| 51 | + ("divide", 3, [10, 6, 30]), |
| 52 | + ("power", 2, [10, 400, 30]), |
| 53 | + ("min", 15, [10, 15, 30]), |
| 54 | + ("min", 25, [10, 20, 30]), |
| 55 | + ("max", 15, [10, 20, 30]), |
| 56 | + ("max", 25, [10, 25, 30]), |
| 57 | + ], |
| 58 | +) |
| 59 | +def test_operations(x, copy, op, arg, expect): |
| 60 | + with assert_copy(x, copy): |
| 61 | + y = getattr(at(x, 1), op)(arg, copy=copy) |
| 62 | + assert isinstance(y, type(x)) |
| 63 | + np.testing.assert_equal(y, expect) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("copy", [True, False, None]) |
| 67 | +def test_get(x, copy): |
| 68 | + with assert_copy(x, copy): |
| 69 | + y = at(x, slice(2)).get(copy=copy) |
| 70 | + assert isinstance(y, type(x)) |
| 71 | + np.testing.assert_array_equal(y, [10, 20]) |
| 72 | + # Let assert_copy test that y is a view or copy |
| 73 | + with suppress((TypeError, ValueError)): |
| 74 | + y[0] = 40 |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "idx", |
| 79 | + [ |
| 80 | + [0, 1], |
| 81 | + ((0, 1), ), |
| 82 | + np.array([0, 1], dtype="i1"), |
| 83 | + np.array([0, 1], dtype="u1"), |
| 84 | + [True, True, False], |
| 85 | + (True, True, False), |
| 86 | + np.array([True, True, False]), |
| 87 | + ], |
| 88 | +) |
| 89 | +@pytest.mark.parametrize("wrap_index", [True, False]) |
| 90 | +def test_get_fancy_indices(x, idx, wrap_index): |
| 91 | + """get() with a fancy index always returns a copy""" |
| 92 | + if not wrap_index and is_jax_array(x) and isinstance(idx, (list, tuple)): |
| 93 | + pytest.skip("JAX fancy indices must always be arrays") |
| 94 | + |
| 95 | + if wrap_index: |
| 96 | + xp = array_namespace(x) |
| 97 | + idx = xp.asarray(idx) |
| 98 | + |
| 99 | + with assert_copy(x, True): |
| 100 | + y = at(x, [0, 1]).get() |
| 101 | + assert isinstance(y, type(x)) |
| 102 | + np.testing.assert_array_equal(y, [10, 20]) |
| 103 | + # Let assert_copy test that y is a view or copy |
| 104 | + with suppress((TypeError, ValueError)): |
| 105 | + y[0] = 40 |
| 106 | + |
| 107 | + with assert_copy(x, True): |
| 108 | + y = at(x, [0, 1]).get(copy=None) |
| 109 | + assert isinstance(y, type(x)) |
| 110 | + np.testing.assert_array_equal(y, [10, 20]) |
| 111 | + # Let assert_copy test that y is a view or copy |
| 112 | + with suppress((TypeError, ValueError)): |
| 113 | + y[0] = 40 |
| 114 | + |
| 115 | + with pytest.raises(ValueError, match="fancy index"): |
| 116 | + at(x, [0, 1]).get(copy=False) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize("copy", [True, False, None]) |
| 120 | +def test_variant_index_syntax(x, copy): |
| 121 | + with assert_copy(x, copy): |
| 122 | + y = at(x)[:2].set(40, copy=copy) |
| 123 | + assert isinstance(y, type(x)) |
| 124 | + np.testing.assert_array_equal(y, [40, 40, 30]) |
| 125 | + with pytest.raises(ValueError): |
| 126 | + at(x, 1)[2] |
| 127 | + with pytest.raises(ValueError): |
| 128 | + at(x)[1][2] |
0 commit comments