-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53a4ac9
commit a1f1b0f
Showing
3 changed files
with
180 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
from contextlib import contextmanager, suppress | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from array_api_compat import array_namespace, at, is_dask_array, is_jax_array, is_writeable_array | ||
from ._helpers import import_, all_libraries | ||
|
||
|
||
@contextmanager | ||
def assert_copy(x, copy: bool | None): | ||
# dask arrays are writeable, but writing to them will hot-swap the | ||
# dask graph inside the collection so that anything that references | ||
# the original graph, i.e. the input collection, won't be mutated. | ||
if copy is False and (not is_writeable_array(x) or is_dask_array(x)): | ||
with pytest.raises((TypeError, ValueError)): | ||
yield | ||
return | ||
|
||
xp = array_namespace(x) | ||
x_orig = xp.asarray(x, copy=True) | ||
yield | ||
|
||
expect_copy = ( | ||
copy if copy is not None else (not is_writeable_array(x) or is_dask_array(x)) | ||
) | ||
np.testing.assert_array_equal((x == x_orig).all(), expect_copy) | ||
|
||
|
||
@pytest.fixture(params=all_libraries + ["np_readonly"]) | ||
def x(request): | ||
library = request.param | ||
if library == "np_readonly": | ||
x = np.asarray([10, 20, 30]) | ||
x.flags.writeable = False | ||
else: | ||
lib = import_(library) | ||
x = lib.asarray([10, 20, 30]) | ||
return x | ||
|
||
|
||
@pytest.mark.parametrize("copy", [True, False, None]) | ||
@pytest.mark.parametrize( | ||
"op,arg,expect", | ||
[ | ||
("apply", np.negative, [10, -20, -30]), | ||
("set", 40, [10, 40, 40]), | ||
("add", 40, [10, 60, 70]), | ||
("subtract", 100, [10, -80, -70]), | ||
("multiply", 2, [10, 40, 60]), | ||
("divide", 3, [10, 6, 10]), | ||
("power", 2, [10, 400, 900]), | ||
("min", 25, [10, 20, 25]), | ||
("max", 25, [10, 25, 30]), | ||
], | ||
) | ||
def test_operations(x, copy, op, arg, expect): | ||
with assert_copy(x, copy): | ||
y = getattr(at(x, slice(1, None)), op)(arg, copy=copy) | ||
assert isinstance(y, type(x)) | ||
np.testing.assert_equal(y, expect) | ||
|
||
|
||
@pytest.mark.parametrize("copy", [True, False, None]) | ||
def test_get(x, copy): | ||
with assert_copy(x, copy): | ||
y = at(x, slice(2)).get(copy=copy) | ||
assert isinstance(y, type(x)) | ||
np.testing.assert_array_equal(y, [10, 20]) | ||
# Let assert_copy test that y is a view or copy | ||
with suppress((TypeError, ValueError)): | ||
y[0] = 40 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"idx", | ||
[ | ||
[0, 1], | ||
((0, 1), ), | ||
np.array([0, 1], dtype="i1"), | ||
np.array([0, 1], dtype="u1"), | ||
[True, True, False], | ||
(True, True, False), | ||
np.array([True, True, False]), | ||
], | ||
) | ||
@pytest.mark.parametrize("wrap_index", [True, False]) | ||
def test_get_fancy_indices(x, idx, wrap_index): | ||
"""get() with a fancy index always returns a copy""" | ||
if not wrap_index and is_jax_array(x) and isinstance(idx, (list, tuple)): | ||
pytest.skip("JAX fancy indices must always be arrays") | ||
|
||
if wrap_index: | ||
xp = array_namespace(x) | ||
idx = xp.asarray(idx) | ||
|
||
with assert_copy(x, True): | ||
y = at(x, [0, 1]).get() | ||
assert isinstance(y, type(x)) | ||
np.testing.assert_array_equal(y, [10, 20]) | ||
# Let assert_copy test that y is a view or copy | ||
with suppress((TypeError, ValueError)): | ||
y[0] = 40 | ||
|
||
with assert_copy(x, True): | ||
y = at(x, [0, 1]).get(copy=None) | ||
assert isinstance(y, type(x)) | ||
np.testing.assert_array_equal(y, [10, 20]) | ||
# Let assert_copy test that y is a view or copy | ||
with suppress((TypeError, ValueError)): | ||
y[0] = 40 | ||
|
||
with pytest.raises(ValueError, match="fancy index"): | ||
at(x, [0, 1]).get(copy=False) | ||
|
||
|
||
@pytest.mark.parametrize("copy", [True, False, None]) | ||
def test_variant_index_syntax(x, copy): | ||
with assert_copy(x, copy): | ||
y = at(x)[:2].set(40, copy=copy) | ||
assert isinstance(y, type(x)) | ||
np.testing.assert_array_equal(y, [40, 40, 30]) | ||
with pytest.raises(ValueError): | ||
at(x, 1)[2] | ||
with pytest.raises(ValueError): | ||
at(x)[1][2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters