-
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 e428fa6
Showing
3 changed files
with
221 additions
and
31 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,158 @@ | ||
from __future__ import annotations | ||
|
||
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_torch_namespace, | ||
is_pydata_sparse_array, | ||
is_writeable_array, | ||
) | ||
from ._helpers import import_, all_libraries | ||
|
||
|
||
def assert_array_equal(a, b): | ||
if is_pydata_sparse_array(a): | ||
a = a.todense() | ||
elif is_dask_array(a): | ||
a = a.compute() | ||
np.testing.assert_array_equal(a, b) | ||
|
||
|
||
@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 | ||
|
||
if is_dask_array(x): | ||
expect_copy = True | ||
elif copy is None: | ||
expect_copy = not is_writeable_array(x) | ||
else: | ||
expect_copy = copy | ||
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)) | ||
assert_array_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)) | ||
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"), | ||
lambda xp: xp.asarray([0, 1], dtype="i1"), | ||
lambda xp: xp.asarray([0, 1], dtype="u1"), | ||
[True, True, False], | ||
(True, True, False), | ||
np.array([True, True, False]), | ||
lambda xp: xp.asarray([True, True, False]), | ||
], | ||
) | ||
@pytest.mark.parametrize("tuple_index", [True, False]) | ||
def test_get_fancy_indices(x, idx, tuple_index): | ||
"""get() with a fancy index always returns a copy""" | ||
if callable(idx): | ||
xp = array_namespace(x) | ||
idx = idx(xp) | ||
|
||
if is_jax_array(x) and isinstance(idx, (list, tuple)): | ||
pytest.skip("JAX fancy indices must always be arrays") | ||
if is_pydata_sparse_array(x) and is_pydata_sparse_array(idx): | ||
pytest.skip("sparse fancy indices can't be sparse themselves") | ||
if is_dask_array(x) and isinstance(idx, tuple): | ||
pytest.skip("dask does not support tuples; only lists or arrays") | ||
if isinstance(idx, tuple) and not tuple_index: | ||
pytest.skip("tuple indices must always be wrapped in a tuple") | ||
|
||
if tuple_index: | ||
idx = (idx,) | ||
|
||
with assert_copy(x, True): | ||
y = at(x, idx).get() | ||
assert isinstance(y, type(x)) | ||
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, idx).get(copy=None) | ||
assert isinstance(y, type(x)) | ||
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, idx).get(copy=False) | ||
|
||
|
||
def test_variant_index_syntax(x): | ||
y = at(x)[:2].set(40) | ||
assert isinstance(y, type(x)) | ||
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