library
stringclasses 1
value | test_file
stringclasses 785
values | test_function
stringlengths 1
295
| before
stringlengths 0
448k
| after
stringlengths 0
487k
| context_before
stringclasses 947
values | context_after
stringlengths 0
16.3k
| commit_before
stringclasses 1
value | commit_after
stringclasses 1
value | change_type
stringclasses 3
values |
---|---|---|---|---|---|---|---|---|---|
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_index_no_array_to_index
|
def test_index_no_array_to_index(self):
# No non-scalar arrays.
a = np.array([[[1]]])
assert_raises(TypeError, lambda: a[a:a:a])
# Conversely, using scalars doesn't raise in NumPy, e.g.
#
# >>> i = np.int64(1)
# >>> a[i:i:i]
# array([], shape=(0, 1, 1), dtype=int64)
#
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_empty_tuple_index
|
def test_empty_tuple_index(self):
# Empty tuple index creates a view
a = np.array([1, 2, 3])
assert_equal(a[()], a)
assert_(a[()].tensor._base is a.tensor)
a = np.array(0)
assert_(isinstance(a[()], np.int_))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_same_kind_index_casting
|
def test_same_kind_index_casting(self):
# Indexes should be cast with same-kind and not safe, even if that
# is somewhat unsafe. So test various different code paths.
index = np.arange(5)
u_index = index.astype(np.uint8) # i.e. cast to default uint indexing dtype
arr = np.arange(10)
assert_array_equal(arr[index], arr[u_index])
arr[u_index] = np.arange(5)
assert_array_equal(arr, np.arange(10))
arr = np.arange(10).reshape(5, 2)
assert_array_equal(arr[index], arr[u_index])
arr[u_index] = np.arange(5)[:, None]
assert_array_equal(arr, np.arange(5)[:, None].repeat(2, axis=1))
arr = np.arange(25).reshape(5, 5)
assert_array_equal(arr[u_index, u_index], arr[index, index])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_empty_fancy_index
|
def test_empty_fancy_index(self):
# Empty list index creates an empty array
# with the same dtype (but with weird shape)
a = np.array([1, 2, 3])
assert_equal(a[[]], [])
assert_equal(a[[]].dtype, a.dtype)
b = np.array([], dtype=np.intp)
assert_equal(a[[]], [])
assert_equal(a[[]].dtype, a.dtype)
b = np.array([])
assert_raises(IndexError, a.__getitem__, b)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_ellipsis_index_2
|
def test_ellipsis_index_2(self):
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
assert_(a[...] is not a)
assert_equal(a[...], a)
# `a[...]` was `a` in numpy <1.9.
assert_(a[...].base is a)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_single_int_index
|
def test_single_int_index(self):
# Single integer index selects one row
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
assert_equal(a[0], [1, 2, 3])
assert_equal(a[-1], [7, 8, 9])
# Index out of bounds produces IndexError
assert_raises(IndexError, a.__getitem__, 1 << 30)
# Index overflow produces IndexError
# Note torch raises RuntimeError here
assert_raises((IndexError, RuntimeError), a.__getitem__, 1 << 64)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_single_bool_index
|
def test_single_bool_index(self):
# Single boolean index
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
assert_equal(a[np.array(True)], a[None])
assert_equal(a[np.array(False)], a[None][0:0])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_boolean_shape_mismatch
|
def test_boolean_shape_mismatch(self):
arr = np.ones((5, 4, 3))
index = np.array([True])
assert_raises(IndexError, arr.__getitem__, index)
index = np.array([False] * 6)
assert_raises(IndexError, arr.__getitem__, index)
index = np.zeros((4, 4), dtype=bool)
assert_raises(IndexError, arr.__getitem__, index)
assert_raises(IndexError, arr.__getitem__, (slice(None), index))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_boolean_assignment_value_mismatch
|
def test_boolean_assignment_value_mismatch(self):
# A boolean assignment should fail when the shape of the values
# cannot be broadcast to the subscription. (see also gh-3458)
a = np.arange(4)
def f(a, v):
a[a > -1] = v
assert_raises((RuntimeError, ValueError, TypeError), f, a, [])
assert_raises((RuntimeError, ValueError, TypeError), f, a, [1, 2, 3])
assert_raises((RuntimeError, ValueError, TypeError), f, a[:1], [1, 2, 3])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
f
|
def f(a, v):
a[a > -1] = v
assert_raises((RuntimeError, ValueError, TypeError), f, a, [])
assert_raises((RuntimeError, ValueError, TypeError), f, a, [1, 2, 3])
assert_raises((RuntimeError, ValueError, TypeError), f, a[:1], [1, 2, 3])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_boolean_indexing_twodim
|
def test_boolean_indexing_twodim(self):
# Indexing a 2-dimensional array with
# 2-dimensional boolean array
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([[True, False, True], [False, True, False], [True, False, True]])
assert_equal(a[b], [1, 3, 5, 7, 9])
assert_equal(a[b[1]], [[4, 5, 6]])
assert_equal(a[b[0]], a[b[2]])
# boolean assignment
a[b] = 0
assert_equal(a, [[0, 2, 0], [4, 0, 6], [0, 8, 0]])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_small_regressions
|
def test_small_regressions(self):
# Reference count of intp for index checks
a = np.array([0])
if HAS_REFCOUNT:
refcount = sys.getrefcount(np.dtype(np.intp))
# item setting always checks indices in separate function:
a[np.array([0], dtype=np.intp)] = 1
a[np.array([0], dtype=np.uint8)] = 1
assert_raises(IndexError, a.__setitem__, np.array([1], dtype=np.intp), 1)
assert_raises(IndexError, a.__setitem__, np.array([1], dtype=np.uint8), 1)
if HAS_REFCOUNT:
assert_equal(sys.getrefcount(np.dtype(np.intp)), refcount)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_broken_sequence_not_nd_index
|
def test_broken_sequence_not_nd_index(self):
# See https://github.com/numpy/numpy/issues/5063
# If we have an object which claims to be a sequence, but fails
# on item getting, this should not be converted to an nd-index (tuple)
# If this object happens to be a valid index otherwise, it should work
# This object here is very dubious and probably bad though:
class SequenceLike:
def __index__(self):
return 0
def __len__(self):
return 1
def __getitem__(self, item):
raise IndexError("Not possible")
arr = np.arange(10)
assert_array_equal(arr[SequenceLike()], arr[SequenceLike(),])
# also test that field indexing does not segfault
# for a similar reason, by indexing a structured array
arr = np.zeros((1,), dtype=[("f1", "i8"), ("f2", "i8")])
assert_array_equal(arr[SequenceLike()], arr[SequenceLike(),])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
__index__
|
def __index__(self):
return 0
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class SequenceLike:
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
__len__
|
def __len__(self):
return 1
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class SequenceLike:
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
__getitem__
|
def __getitem__(self, item):
raise IndexError("Not possible")
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class SequenceLike:
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_indexing_array_weird_strides
|
def test_indexing_array_weird_strides(self):
# See also gh-6221
# the shapes used here come from the issue and create the correct
# size for the iterator buffering size.
x = np.ones(10)
x2 = np.ones((10, 2))
ind = np.arange(10)[:, None, None, None]
ind = np.broadcast_to(ind, (10, 55, 4, 4))
# single advanced index case
assert_array_equal(x[ind], x[ind.copy()])
# higher dimensional advanced index
zind = np.zeros(4, dtype=np.intp)
assert_array_equal(x2[ind, zind], x2[ind.copy(), zind])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_too_many_advanced_indices
|
def test_too_many_advanced_indices(self, index, num, original_ndim):
# These are limitations based on the number of arguments we can process.
# For `num=32` (and all boolean cases), the result is actually define;
# but the use of NpyIter (NPY_MAXARGS) limits it for technical reasons.
if not (isinstance(index, np.ndarray) and original_ndim < num):
# unskipped cases fail because of assigning too many indices
raise SkipTest("torch does not limit dims to 32")
arr = np.ones((1,) * original_ndim)
with pytest.raises(IndexError):
arr[(index,) * num]
with pytest.raises(IndexError):
arr[(index,) * num] = 1.0
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_boolean_indexing_list
|
def test_boolean_indexing_list(self):
# Regression test for #13715. It's a use-after-free bug which the
# test won't directly catch, but it will show up in valgrind.
a = np.array([1, 2, 3])
b = [True, False, True]
# Two variants of the test because the first takes a fast path
assert_equal(a[b], [1, 3])
assert_equal(a[None, b], [[1, 3]])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_reverse_strides_and_subspace_bufferinit
|
def test_reverse_strides_and_subspace_bufferinit(self):
# This tests that the strides are not reversed for simple and
# subspace fancy indexing.
a = np.ones(5)
b = np.zeros(5, dtype=np.intp)[::-1]
c = np.arange(5)[::-1]
a[b] = c
# If the strides are not reversed, the 0 in the arange comes last.
assert_equal(a[0], 0)
# This also tests that the subspace buffer is initialized:
a = np.ones((5, 2))
c = np.arange(10).reshape(5, 2)[::-1]
a[b, :] = c
assert_equal(a[0], [0, 1])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_reversed_strides_result_allocation
|
def test_reversed_strides_result_allocation(self):
# Test a bug when calculating the output strides for a result array
# when the subspace size was 1 (and test other cases as well)
a = np.arange(10)[:, None]
i = np.arange(10)[::-1]
assert_array_equal(a[i], a[i.copy("C")])
a = np.arange(20).reshape(-1, 2)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_too_many_fancy_indices_special_case
|
def test_too_many_fancy_indices_special_case(self):
# Just documents behaviour, this is a small limitation.
a = np.ones((1,) * 32) # 32 is NPY_MAXDIMS
assert_raises(IndexError, a.__getitem__, (np.array([0]),) * 32)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_scalar_array_bool
|
def test_scalar_array_bool(self):
# NumPy bools can be used as boolean index (python ones as of yet not)
a = np.array(1)
assert_equal(a[np.bool_(True)], a[np.array(True)])
assert_equal(a[np.bool_(False)], a[np.array(False)])
# After deprecating bools as integers:
# a = np.array([0,1,2])
# assert_equal(a[True, :], a[None, :])
# assert_equal(a[:, True], a[:, None])
#
# assert_(not np.may_share_memory(a, a[True, :]))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_everything_returns_views
|
def test_everything_returns_views(self):
# Before `...` would return a itself.
a = np.arange(5)
assert_(a is not a[()])
assert_(a is not a[...])
assert_(a is not a[:])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_broaderrors_indexing
|
def test_broaderrors_indexing(self):
a = np.zeros((5, 5))
assert_raises(IndexError, a.__getitem__, ([0, 1], [0, 1, 2]))
assert_raises(IndexError, a.__setitem__, ([0, 1], [0, 1, 2]), 0)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_trivial_fancy_out_of_bounds
|
def test_trivial_fancy_out_of_bounds(self):
a = np.zeros(5)
ind = np.ones(20, dtype=np.intp)
ind[-1] = 10
assert_raises(IndexError, a.__getitem__, ind)
assert_raises((IndexError, RuntimeError), a.__setitem__, ind, 0)
ind = np.ones(20, dtype=np.intp)
ind[0] = 11
assert_raises(IndexError, a.__getitem__, ind)
assert_raises((IndexError, RuntimeError), a.__setitem__, ind, 0)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_trivial_fancy_not_possible
|
def test_trivial_fancy_not_possible(self):
# Test that the fast path for trivial assignment is not incorrectly
# used when the index is not contiguous or 1D, see also gh-11467.
a = np.arange(6)
idx = np.arange(6, dtype=np.intp).reshape(2, 1, 3)[:, :, 0]
assert_array_equal(a[idx], idx)
# this case must not go into the fast path, note that idx is
# a non-contiuguous none 1D array here.
a[idx] = -1
res = np.arange(6)
res[0] = -1
res[3] = -1
assert_array_equal(a, res)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_nontuple_ndindex
|
def test_nontuple_ndindex(self):
a = np.arange(25).reshape((5, 5))
assert_equal(a[[0, 1]], np.array([a[0], a[1]]))
assert_equal(a[[0, 1], [0, 1]], np.array([0, 6]))
raise SkipTest(
"torch happily consumes non-tuple sequences with multi-axis "
"indices (i.e. slices) as an index, whereas NumPy invalidates "
"them, assumedly to keep things simple. This invalidation "
"behaviour is just too niche to bother emulating."
)
assert_raises(IndexError, a.__getitem__, [slice(None)])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
assign
|
def assign(self, a, ind, val):
a[ind] = val
return a
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestBroadcastedAssignments(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_prepending_ones
|
def test_prepending_ones(self):
a = np.zeros((3, 2))
a[...] = np.ones((1, 3, 2))
# Fancy with subspace with and without transpose
a[[0, 1, 2], :] = np.ones((1, 3, 2))
a[:, [0, 1]] = np.ones((1, 3, 2))
# Fancy without subspace (with broadcasting)
a[[[0], [1], [2]], [0, 1]] = np.ones((1, 3, 2))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestBroadcastedAssignments(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_prepend_not_one
|
def test_prepend_not_one(self):
assign = self.assign
s_ = np.s_
a = np.zeros(5)
# Too large and not only ones.
try:
assign(a, s_[...], np.ones((2, 1)))
except Exception as e:
self.assertTrue(isinstance(e, (ValueError, RuntimeError)))
assert_raises(
(ValueError, RuntimeError), assign, a, s_[[1, 2, 3],], np.ones((2, 1))
)
assert_raises(
(ValueError, RuntimeError), assign, a, s_[[[1], [2]],], np.ones((2, 2, 1))
)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestBroadcastedAssignments(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_broadcast_error_reports_correct_shape
|
def test_broadcast_error_reports_correct_shape(self, index):
values = np.zeros((100, 100)) # will never broadcast below
arr = np.zeros((3, 4, 5, 6, 7))
with pytest.raises((ValueError, RuntimeError)) as e:
arr[index] = values
shape = arr[index].shape
r_inner_shape = "".join(f"{side}, ?" for side in shape[:-1]) + str(shape[-1])
assert re.search(rf"[\(\[]{r_inner_shape}[\]\)]$", str(e.value))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestBroadcastedAssignments(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_index_is_larger
|
def test_index_is_larger(self):
# Simple case of fancy index broadcasting of the index.
a = np.zeros((5, 5))
a[[[0], [1], [2]], [0, 1, 2]] = [2, 3, 4]
assert_((a[:3, :3] == [2, 3, 4]).all())
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestBroadcastedAssignments(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_broadcast_subspace
|
def test_broadcast_subspace(self):
a = np.zeros((100, 100))
v = np.arange(100)[:, None]
b = np.arange(100)[::-1]
a[b] = v
assert_((a[::-1] == v).all())
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@instantiate_parametrized_tests
class TestBroadcastedAssignments(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_boolean_index_cast_assign
|
def test_boolean_index_cast_assign(self):
# Setup the boolean index and float arrays.
shape = (8, 63)
bool_index = np.zeros(shape).astype(bool)
bool_index[0, 1] = True
zero_array = np.zeros(shape)
# Assigning float is fine.
zero_array[bool_index] = np.array([1])
assert_equal(zero_array[0, 1], 1)
# Fancy indexing works, although we get a cast warning.
assert_warns(
np.ComplexWarning, zero_array.__setitem__, ([0], [1]), np.array([2 + 1j])
)
assert_equal(zero_array[0, 1], 2) # No complex part
# Cast complex to float, throwing away the imaginary portion.
assert_warns(
np.ComplexWarning, zero_array.__setitem__, bool_index, np.array([1j])
)
assert_equal(zero_array[0, 1], 0)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestFancyIndexingCast(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
setupUp
|
def setupUp(self):
self.a = np.arange(np.prod([3, 1, 5, 6])).reshape(3, 1, 5, 6)
self.b = np.empty((3, 0, 5, 6))
self.complex_indices = [
"skip",
Ellipsis,
0,
# Boolean indices, up to 3-d for some special cases of eating up
# dimensions, also need to test all False
np.array([True, False, False]),
np.array([[True, False], [False, True]]),
np.array([[[False, False], [False, False]]]),
# Some slices:
slice(-5, 5, 2),
slice(1, 1, 100),
slice(4, -1, -2),
slice(None, None, -3),
# Some Fancy indexes:
np.empty((0, 1, 1), dtype=np.intp), # empty and can be broadcast
np.array([0, 1, -2]),
np.array([[2], [0], [1]]),
np.array([[0, -1], [0, 1]], dtype=np.dtype("intp")),
np.array([2, -1], dtype=np.int8),
np.zeros([1] * 31, dtype=int), # trigger too large array.
np.array([0.0, 1.0]),
] # invalid datatype
# Some simpler indices that still cover a bit more
self.simple_indices = [Ellipsis, None, -1, [1], np.array([True]), "skip"]
# Very simple ones to fill the rest:
self.fill_indices = [slice(None, None), 0]
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@xfail # (reason="XXX: requires broadcast() and broadcast_to()")
class TestMultiIndexingAutomated(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_non_integer_sequence_multiplication
|
def test_non_integer_sequence_multiplication(self):
# NumPy scalar sequence multiply should not work with non-integers
def mult(a, b):
return a * b
assert_raises(TypeError, mult, [1], np.float64(3))
# following should be OK
mult([1], np.int_(3))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestFloatNonIntegerArgument(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
mult
|
def mult(a, b):
return a * b
assert_raises(TypeError, mult, [1], np.float64(3))
# following should be OK
mult([1], np.int_(3))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_reduce_axis_float_index
|
def test_reduce_axis_float_index(self):
d = np.zeros((3, 3, 3))
assert_raises(TypeError, np.min, d, 0.5)
assert_raises(TypeError, np.min, d, (0.5, 1))
assert_raises(TypeError, np.min, d, (1, 2.2))
assert_raises(TypeError, np.min, d, (0.2, 1.2))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestFloatNonIntegerArgument(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_bool_as_int_argument_errors
|
def test_bool_as_int_argument_errors(self):
a = np.array([[[1]]])
assert_raises(TypeError, np.reshape, a, (True, -1))
# Note that operator.index(np.array(True)) does not work, a boolean
# array is thus also deprecated, but not with the same message:
# assert_warns(DeprecationWarning, operator.index, np.True_)
assert_raises(TypeError, np.take, args=(a, [0], False))
raise SkipTest("torch consumes boolean tensors as ints, no bother raising here")
assert_raises(TypeError, np.reshape, a, (np.bool_(True), -1))
assert_raises(TypeError, operator.index, np.array(True))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestBooleanIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_boolean_indexing_weirdness
|
def test_boolean_indexing_weirdness(self):
# Weird boolean indexing things
a = np.ones((2, 3, 4))
assert a[False, True, ...].shape == (0, 2, 3, 4)
assert a[True, [0, 1], True, True, [1], [[2]]].shape == (1, 2)
assert_raises(IndexError, lambda: a[False, [0, 1], ...])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestBooleanIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_boolean_indexing_fast_path
|
def test_boolean_indexing_fast_path(self):
# These used to either give the wrong error, or incorrectly give no
# error.
a = np.ones((3, 3))
# This used to incorrectly work (and give an array of shape (0,))
idx1 = np.array([[False] * 9])
with pytest.raises(IndexError):
a[idx1]
# This used to incorrectly give a ValueError: operands could not be broadcast together
idx2 = np.array([[False] * 8 + [True]])
with pytest.raises(IndexError):
a[idx2]
# This is the same as it used to be. The above two should work like this.
idx3 = np.array([[False] * 10])
with pytest.raises(IndexError):
a[idx3]
# This used to give ValueError: non-broadcastable operand
a = np.ones((1, 1, 2))
idx = np.array([[[True], [False]]])
with pytest.raises(IndexError):
a[idx]
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestBooleanIndexing(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_array_to_index_error
|
def test_array_to_index_error(self):
# so no exception is expected. The raising is effectively tested above.
a = np.array([[[1]]])
assert_raises((TypeError, RuntimeError), np.take, a, [0], a)
raise SkipTest(
"Multi-dimensional tensors are indexable just as long as they only "
"contain a single element, no bother raising here"
)
assert_raises(TypeError, operator.index, np.array([1]))
raise SkipTest("torch consumes tensors as ints, no bother raising here")
assert_raises(TypeError, np.reshape, a, (a, -1))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestArrayToIndexDeprecation(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_basic
|
def test_basic(self):
a = np.arange(10)
assert_raises(IndexError, a.__getitem__, [0.5, 1.5])
assert_raises(IndexError, a.__getitem__, (["1", "2"],))
# The following is valid
a.__getitem__([])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestNonIntegerArrayLike(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_basic
|
def test_basic(self):
a = np.arange(10)
assert_raises(IndexError, a.__getitem__, [0.5, 1.5])
assert_raises(IndexError, a.__getitem__, (["1", "2"],))
# The following is valid
a.__getitem__([])
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestNonIntegerArrayLike(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
_get_multi_index
|
def _get_multi_index(self, arr, indices):
"""Mimic multi dimensional indexing.
Parameters
----------
arr : ndarray
Array to be indexed.
indices : tuple of index objects
Returns
-------
out : ndarray
An array equivalent to the indexing operation (but always a copy).
`arr[indices]` should be identical.
no_copy : bool
Whether the indexing operation requires a copy. If this is `True`,
`np.may_share_memory(arr, arr[indices])` should be `True` (with
some exceptions for scalars and possibly 0-d arrays).
Notes
-----
While the function may mostly match the errors of normal indexing this
is generally not the case.
"""
in_indices = list(indices)
indices = []
# if False, this is a fancy or boolean index
no_copy = True
# number of fancy/scalar indexes that are not consecutive
num_fancy = 0
# number of dimensions indexed by a "fancy" index
fancy_dim = 0
# NOTE: This is a funny twist (and probably OK to change).
# The boolean array has illegal indexes, but this is
# allowed if the broadcast fancy-indices are 0-sized.
# This variable is to catch that case.
error_unless_broadcast_to_empty = False
# We need to handle Ellipsis and make arrays from indices, also
# check if this is fancy indexing (set no_copy).
ndim = 0
ellipsis_pos = None # define here mostly to replace all but first.
for i, indx in enumerate(in_indices):
if indx is None:
continue
if isinstance(indx, np.ndarray) and indx.dtype == bool:
no_copy = False
if indx.ndim == 0:
raise IndexError
# boolean indices can have higher dimensions
ndim += indx.ndim
fancy_dim += indx.ndim
continue
if indx is Ellipsis:
if ellipsis_pos is None:
ellipsis_pos = i
continue # do not increment ndim counter
raise IndexError
if isinstance(indx, slice):
ndim += 1
continue
if not isinstance(indx, np.ndarray):
# This could be open for changes in numpy.
# numpy should maybe raise an error if casting to intp
# is not safe. It rejects np.array([1., 2.]) but not
# [1., 2.] as index (same for ie. np.take).
# (Note the importance of empty lists if changing this here)
try:
indx = np.array(indx, dtype=np.intp)
except ValueError:
raise IndexError from None
in_indices[i] = indx
elif indx.dtype.kind != "b" and indx.dtype.kind != "i":
raise IndexError(
"arrays used as indices must be of integer (or boolean) type"
)
if indx.ndim != 0:
no_copy = False
ndim += 1
fancy_dim += 1
if arr.ndim - ndim < 0:
# we can't take more dimensions then we have, not even for 0-d
# arrays. since a[()] makes sense, but not a[(),]. We will
# raise an error later on, unless a broadcasting error occurs
# first.
raise IndexError
if ndim == 0 and None not in in_indices:
# Well we have no indexes or one Ellipsis. This is legal.
return arr.copy(), no_copy
if ellipsis_pos is not None:
in_indices[ellipsis_pos : ellipsis_pos + 1] = [slice(None, None)] * (
arr.ndim - ndim
)
for ax, indx in enumerate(in_indices):
if isinstance(indx, slice):
# convert to an index array
indx = np.arange(*indx.indices(arr.shape[ax]))
indices.append(["s", indx])
continue
elif indx is None:
# this is like taking a slice with one element from a new axis:
indices.append(["n", np.array([0], dtype=np.intp)])
arr = arr.reshape(arr.shape[:ax] + (1,) + arr.shape[ax:])
continue
if isinstance(indx, np.ndarray) and indx.dtype == bool:
if indx.shape != arr.shape[ax : ax + indx.ndim]:
raise IndexError
try:
flat_indx = np.ravel_multi_index(
np.nonzero(indx), arr.shape[ax : ax + indx.ndim], mode="raise"
)
except Exception:
error_unless_broadcast_to_empty = True
# fill with 0s instead, and raise error later
flat_indx = np.array([0] * indx.sum(), dtype=np.intp)
# concatenate axis into a single one:
if indx.ndim != 0:
arr = arr.reshape(
arr.shape[:ax]
+ (np.prod(arr.shape[ax : ax + indx.ndim]),)
+ arr.shape[ax + indx.ndim :]
)
indx = flat_indx
else:
# This could be changed, a 0-d boolean index can
# make sense (even outside the 0-d indexed array case)
# Note that originally this is could be interpreted as
# integer in the full integer special case.
raise IndexError
else:
# If the index is a singleton, the bounds check is done
# before the broadcasting. This used to be different in <1.9
if indx.ndim == 0:
if indx >= arr.shape[ax] or indx < -arr.shape[ax]:
raise IndexError
if indx.ndim == 0:
# The index is a scalar. This used to be two fold, but if
# fancy indexing was active, the check was done later,
# possibly after broadcasting it away (1.7. or earlier).
# Now it is always done.
if indx >= arr.shape[ax] or indx < -arr.shape[ax]:
raise IndexError
if len(indices) > 0 and indices[-1][0] == "f" and ax != ellipsis_pos:
# NOTE: There could still have been a 0-sized Ellipsis
# between them. Checked that with ellipsis_pos.
indices[-1].append(indx)
else:
# We have a fancy index that is not after an existing one.
# NOTE: A 0-d array triggers this as well, while one may
# expect it to not trigger it, since a scalar would not be
# considered fancy indexing.
num_fancy += 1
indices.append(["f", indx])
if num_fancy > 1 and not no_copy:
# We have to flush the fancy indexes left
new_indices = indices[:]
axes = list(range(arr.ndim))
fancy_axes = []
new_indices.insert(0, ["f"])
ni = 0
ai = 0
for indx in indices:
ni += 1
if indx[0] == "f":
new_indices[0].extend(indx[1:])
del new_indices[ni]
ni -= 1
for ax in range(ai, ai + len(indx[1:])):
fancy_axes.append(ax)
axes.remove(ax)
ai += len(indx) - 1 # axis we are at
indices = new_indices
# and now we need to transpose arr:
arr = arr.transpose(*(fancy_axes + axes))
# We only have one 'f' index now and arr is transposed accordingly.
# Now handle newaxis by reshaping...
ax = 0
for indx in indices:
if indx[0] == "f":
if len(indx) == 1:
continue
# First of all, reshape arr to combine fancy axes into one:
orig_shape = arr.shape
orig_slice = orig_shape[ax : ax + len(indx[1:])]
arr = arr.reshape(
arr.shape[:ax]
+ (np.prod(orig_slice).astype(int),)
+ arr.shape[ax + len(indx[1:]) :]
)
# Check if broadcasting works
res = np.broadcast(*indx[1:])
# unfortunately the indices might be out of bounds. So check
# that first, and use mode='wrap' then. However only if
# there are any indices...
if res.size != 0:
if error_unless_broadcast_to_empty:
raise IndexError
for _indx, _size in zip(indx[1:], orig_slice):
if _indx.size == 0:
continue
if np.any(_indx >= _size) or np.any(_indx < -_size):
raise IndexError
if len(indx[1:]) == len(orig_slice):
if np.prod(orig_slice) == 0:
# Work around for a crash or IndexError with 'wrap'
# in some 0-sized cases.
try:
mi = np.ravel_multi_index(
indx[1:], orig_slice, mode="raise"
)
except Exception as exc:
# This happens with 0-sized orig_slice (sometimes?)
# here it is a ValueError, but indexing gives a:
raise IndexError("invalid index into 0-sized") from exc
else:
mi = np.ravel_multi_index(indx[1:], orig_slice, mode="wrap")
else:
# Maybe never happens...
raise ValueError
arr = arr.take(mi.ravel(), axis=ax)
try:
arr = arr.reshape(arr.shape[:ax] + mi.shape + arr.shape[ax + 1 :])
except ValueError:
# too many dimensions, probably
raise IndexError from None
ax += mi.ndim
continue
# If we are here, we have a 1D array for take:
arr = arr.take(indx[1], axis=ax)
ax += 1
return arr, no_copy
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@xfail # (reason="XXX: requires broadcast() and broadcast_to()")
class TestMultiIndexingAutomated(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
_check_multi_index
|
def _check_multi_index(self, arr, index):
"""Check a multi index item getting and simple setting.
Parameters
----------
arr : ndarray
Array to be indexed, must be a reshaped arange.
index : tuple of indexing objects
Index being tested.
"""
# Test item getting
try:
mimic_get, no_copy = self._get_multi_index(arr, index)
except Exception as e:
if HAS_REFCOUNT:
prev_refcount = sys.getrefcount(arr)
assert_raises(type(e), arr.__getitem__, index)
assert_raises(type(e), arr.__setitem__, index, 0)
if HAS_REFCOUNT:
assert_equal(prev_refcount, sys.getrefcount(arr))
return
self._compare_index_result(arr, index, mimic_get, no_copy)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@xfail # (reason="XXX: requires broadcast() and broadcast_to()")
class TestMultiIndexingAutomated(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
_check_single_index
|
def _check_single_index(self, arr, index):
"""Check a single index item getting and simple setting.
Parameters
----------
arr : ndarray
Array to be indexed, must be an arange.
index : indexing object
Index being tested. Must be a single index and not a tuple
of indexing objects (see also `_check_multi_index`).
"""
try:
mimic_get, no_copy = self._get_multi_index(arr, (index,))
except Exception as e:
if HAS_REFCOUNT:
prev_refcount = sys.getrefcount(arr)
assert_raises(type(e), arr.__getitem__, index)
assert_raises(type(e), arr.__setitem__, index, 0)
if HAS_REFCOUNT:
assert_equal(prev_refcount, sys.getrefcount(arr))
return
self._compare_index_result(arr, index, mimic_get, no_copy)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@xfail # (reason="XXX: requires broadcast() and broadcast_to()")
class TestMultiIndexingAutomated(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
_compare_index_result
|
def _compare_index_result(self, arr, index, mimic_get, no_copy):
"""Compare mimicked result to indexing result."""
raise SkipTest("torch does not support subclassing")
arr = arr.copy()
indexed_arr = arr[index]
assert_array_equal(indexed_arr, mimic_get)
# Check if we got a view, unless its a 0-sized or 0-d array.
# (then its not a view, and that does not matter)
if indexed_arr.size != 0 and indexed_arr.ndim != 0:
assert_(np.may_share_memory(indexed_arr, arr) == no_copy)
# Check reference count of the original array
if HAS_REFCOUNT:
if no_copy:
# refcount increases by one:
assert_equal(sys.getrefcount(arr), 3)
else:
assert_equal(sys.getrefcount(arr), 2)
# Test non-broadcast setitem:
b = arr.copy()
b[index] = mimic_get + 1000
if b.size == 0:
return # nothing to compare here...
if no_copy and indexed_arr.ndim != 0:
# change indexed_arr in-place to manipulate original:
indexed_arr += 1000
assert_array_equal(arr, b)
return
# Use the fact that the array is originally an arange:
arr.flat[indexed_arr.ravel()] += 1000
assert_array_equal(arr, b)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@xfail # (reason="XXX: requires broadcast() and broadcast_to()")
class TestMultiIndexingAutomated(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_boolean
|
def test_boolean(self):
a = np.array(5)
assert_equal(a[np.array(True)], 5)
a[np.array(True)] = 1
assert_equal(a, 1)
# NOTE: This is different from normal broadcasting, as
# arr[boolean_array] works like in a multi index. Which means
# it is aligned to the left. This is probably correct for
# consistency with arr[boolean_array,] also no broadcasting
# is done at all
self._check_multi_index(self.a, (np.zeros_like(self.a, dtype=bool),))
self._check_multi_index(self.a, (np.zeros_like(self.a, dtype=bool)[..., 0],))
self._check_multi_index(self.a, (np.zeros_like(self.a, dtype=bool)[None, ...],))
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@xfail # (reason="XXX: requires broadcast() and broadcast_to()")
class TestMultiIndexingAutomated(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_multidim
|
def test_multidim(self):
# Automatically test combinations with complex indexes on 2nd (or 1st)
# spot and the simple ones in one other spot.
with warnings.catch_warnings():
# This is so that np.array(True) is not accepted in a full integer
# index, when running the file separately.
warnings.filterwarnings("error", "", DeprecationWarning)
warnings.filterwarnings("error", "", np.VisibleDeprecationWarning)
def isskip(idx):
return isinstance(idx, str) and idx == "skip"
for simple_pos in [0, 2, 3]:
tocheck = [
self.fill_indices,
self.complex_indices,
self.fill_indices,
self.fill_indices,
]
tocheck[simple_pos] = self.simple_indices
for index in product(*tocheck):
index = tuple(i for i in index if not isskip(i))
self._check_multi_index(self.a, index)
self._check_multi_index(self.b, index)
# Check very simple item getting:
self._check_multi_index(self.a, (0, 0, 0, 0))
self._check_multi_index(self.b, (0, 0, 0, 0))
# Also check (simple cases of) too many indices:
assert_raises(IndexError, self.a.__getitem__, (0, 0, 0, 0, 0))
assert_raises(IndexError, self.a.__setitem__, (0, 0, 0, 0, 0), 0)
assert_raises(IndexError, self.a.__getitem__, (0, 0, [1], 0, 0))
assert_raises(IndexError, self.a.__setitem__, (0, 0, [1], 0, 0), 0)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@xfail # (reason="XXX: requires broadcast() and broadcast_to()")
class TestMultiIndexingAutomated(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_1d
|
def test_1d(self):
a = np.arange(10)
for index in self.complex_indices:
self._check_single_index(a, index)
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
@xfail # (reason="XXX: requires broadcast() and broadcast_to()")
class TestMultiIndexingAutomated(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_valid_indexing
|
def test_valid_indexing(self):
# These should raise no errors.
a = np.array([[[5]]])
a[np.array([0])]
a[[0, 0]]
a[:, [0, 0]]
a[:, 0, :]
a[:, :, :]
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestFloatNonIntegerArgument(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_indexing.py
|
test_valid_slicing
|
def test_valid_slicing(self):
# These should raise no errors.
a = np.array([[[5]]])
a[::]
a[0:]
a[:2]
a[0:2]
a[::2]
a[1::2]
a[:2:2]
a[1:2:2]
|
import functools
import operator
import re
import sys
import warnings
from itertools import product
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_TORCHDYNAMO,
TestCase,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_array_equal,
assert_equal,
assert_warns,
HAS_REFCOUNT,
)
skip = functools.partial(skipif, True)
class TestFloatNonIntegerArgument(TestCase):
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
temppath
|
def temppath(*args, **kwargs):
"""Context manager for temporary files.
Context manager that returns the path to a closed temporary file. Its
parameters are the same as for tempfile.mkstemp and are passed directly
to that function. The underlying file is removed when the context is
exited, so it should be closed at that time.
Windows does not allow a temporary file to be opened if it is already
open, so the underlying file must be closed after opening before it
can be opened again.
"""
fd, path = mkstemp(*args, **kwargs)
os.close(fd)
try:
yield path
finally:
os.remove(path)
# FIXME
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
# #### end stubs
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
_aligned_zeros
|
def _aligned_zeros(shape, dtype=float, order="C", align=None):
"""
Allocate a new ndarray with aligned memory.
The ndarray is guaranteed *not* aligned to twice the requested alignment.
Eg, if align=4, guarantees it is not aligned to 8. If align=None uses
dtype.alignment."""
dtype = np.dtype(dtype)
if dtype == np.dtype(object):
# Can't do this, fall back to standard allocation (which
# should always be sufficiently aligned)
if align is not None:
raise ValueError("object array alignment not supported")
return np.zeros(shape, dtype=dtype, order=order)
if align is None:
align = dtype.alignment
if not hasattr(shape, "__len__"):
shape = (shape,)
size = functools.reduce(operator.mul, shape) * dtype.itemsize
buf = np.empty(size + 2 * align + 1, np.uint8)
ptr = buf.__array_interface__["data"][0]
offset = ptr % align
if offset != 0:
offset = align - offset
if (ptr % (2 * align)) == 0:
offset += align
# Note: slices producing 0-size arrays do not necessarily change
# data pointer --- so we use and allocate size+1
buf = buf[offset : offset + size + 1][:-1]
buf.fill(0)
data = np.ndarray(shape, dtype, buf, order=order)
return data
@xpassIfTorchDynamo # (reason="TODO: flags")
@instantiate_parametrized_tests
class TestFlag(TestCase):
def setUp(self):
self.a = np.arange(10)
@xfail
def test_writeable(self):
mydict = locals()
self.a.flags.writeable = False
assert_raises(ValueError, runstring, "self.a[0] = 3", mydict)
assert_raises(ValueError, runstring, "self.a[0:1].itemset(3)", mydict)
self.a.flags.writeable = True
self.a[0] = 5
self.a[0] = 0
def test_writeable_any_base(self):
# Ensure that any base being writeable is sufficient to change flag;
# this is especially interesting for arrays from an array interface.
arr = np.arange(10)
class subclass(np.ndarray):
pass
# Create subclass so base will not be collapsed, this is OK to change
view1 = arr.view(subclass)
view2 = view1[...]
arr.flags.writeable = False
view2.flags.writeable = False
view2.flags.writeable = True # Can be set to True again.
arr = np.arange(10)
class frominterface:
def __init__(self, arr):
self.arr = arr
self.__array_interface__ = arr.__array_interface__
view1 = np.asarray(frominterface)
view2 = view1[...]
view2.flags.writeable = False
view2.flags.writeable = True
view1.flags.writeable = False
view2.flags.writeable = False
with assert_raises(ValueError):
# Must assume not writeable, since only base is not:
view2.flags.writeable = True
def test_writeable_from_readonly(self):
# gh-9440 - make sure fromstring, from buffer on readonly buffers
# set writeable False
data = b"\x00" * 100
vals = np.frombuffer(data, "B")
assert_raises(ValueError, vals.setflags, write=True)
types = np.dtype([("vals", "u1"), ("res3", "S4")])
values = np.core.records.fromstring(data, types)
vals = values["vals"]
assert_raises(ValueError, vals.setflags, write=True)
def test_writeable_from_buffer(self):
data = bytearray(b"\x00" * 100)
vals = np.frombuffer(data, "B")
assert_(vals.flags.writeable)
vals.setflags(write=False)
assert_(vals.flags.writeable is False)
vals.setflags(write=True)
assert_(vals.flags.writeable)
types = np.dtype([("vals", "u1"), ("res3", "S4")])
values = np.core.records.fromstring(data, types)
vals = values["vals"]
assert_(vals.flags.writeable)
vals.setflags(write=False)
assert_(vals.flags.writeable is False)
vals.setflags(write=True)
assert_(vals.flags.writeable)
@skipif(IS_PYPY, reason="PyPy always copies")
def test_writeable_pickle(self):
import pickle
# Small arrays will be copied without setting base.
# See condition for using PyArray_SetBaseObject in
# array_setstate.
a = np.arange(1000)
for v in range(pickle.HIGHEST_PROTOCOL):
vals = pickle.loads(pickle.dumps(a, v))
assert_(vals.flags.writeable)
assert_(isinstance(vals.base, bytes))
def test_warnonwrite(self):
a = np.arange(10)
a.flags._warn_on_write = True
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings("always")
a[1] = 10
a[2] = 10
# only warn once
assert_(len(w) == 1)
@parametrize(
"flag, flag_value, writeable",
[
("writeable", True, True),
# Delete _warn_on_write after deprecation and simplify
# the parameterization:
("_warn_on_write", True, False),
("writeable", False, False),
],
)
def test_readonly_flag_protocols(self, flag, flag_value, writeable):
a = np.arange(10)
setattr(a.flags, flag, flag_value)
class MyArr:
__array_struct__ = a.__array_struct__
assert memoryview(a).readonly is not writeable
assert a.__array_interface__["data"][1] is not writeable
assert np.asarray(MyArr()).flags.writeable is writeable
@xfail
def test_otherflags(self):
assert_equal(self.a.flags.carray, True)
assert_equal(self.a.flags["C"], True)
assert_equal(self.a.flags.farray, False)
assert_equal(self.a.flags.behaved, True)
assert_equal(self.a.flags.fnc, False)
assert_equal(self.a.flags.forc, True)
assert_equal(self.a.flags.owndata, True)
assert_equal(self.a.flags.writeable, True)
assert_equal(self.a.flags.aligned, True)
assert_equal(self.a.flags.writebackifcopy, False)
assert_equal(self.a.flags["X"], False)
assert_equal(self.a.flags["WRITEBACKIFCOPY"], False)
@xfail # invalid dtype
def test_string_align(self):
a = np.zeros(4, dtype=np.dtype("|S4"))
assert_(a.flags.aligned)
# not power of two are accessed byte-wise and thus considered aligned
a = np.zeros(5, dtype=np.dtype("|S4"))
assert_(a.flags.aligned)
@xfail # structured dtypes
def test_void_align(self):
a = np.zeros(4, dtype=np.dtype([("a", "i4"), ("b", "i4")]))
assert_(a.flags.aligned)
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestHash(TestCase):
# see #3793
def test_int(self):
for st, ut, s in [
(np.int8, np.uint8, 8),
(np.int16, np.uint16, 16),
(np.int32, np.uint32, 32),
(np.int64, np.uint64, 64),
]:
for i in range(1, s):
assert_equal(
hash(st(-(2**i))), hash(-(2**i)), err_msg="%r: -2**%d" % (st, i)
)
assert_equal(
hash(st(2 ** (i - 1))),
hash(2 ** (i - 1)),
err_msg="%r: 2**%d" % (st, i - 1),
)
assert_equal(
hash(st(2**i - 1)),
hash(2**i - 1),
err_msg="%r: 2**%d - 1" % (st, i),
)
i = max(i - 1, 1)
assert_equal(
hash(ut(2 ** (i - 1))),
hash(2 ** (i - 1)),
err_msg="%r: 2**%d" % (ut, i - 1),
)
assert_equal(
hash(ut(2**i - 1)),
hash(2**i - 1),
err_msg="%r: 2**%d - 1" % (ut, i),
)
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestAttributes(TestCase):
def setUp(self):
self.one = np.arange(10)
self.two = np.arange(20).reshape(4, 5)
self.three = np.arange(60, dtype=np.float64).reshape(2, 5, 6)
def test_attributes(self):
assert_equal(self.one.shape, (10,))
assert_equal(self.two.shape, (4, 5))
assert_equal(self.three.shape, (2, 5, 6))
self.three.shape = (10, 3, 2)
assert_equal(self.three.shape, (10, 3, 2))
self.three.shape = (2, 5, 6)
assert_equal(self.one.strides, (self.one.itemsize,))
num = self.two.itemsize
assert_equal(self.two.strides, (5 * num, num))
num = self.three.itemsize
assert_equal(self.three.strides, (30 * num, 6 * num, num))
assert_equal(self.one.ndim, 1)
assert_equal(self.two.ndim, 2)
assert_equal(self.three.ndim, 3)
num = self.two.itemsize
assert_equal(self.two.size, 20)
assert_equal(self.two.nbytes, 20 * num)
assert_equal(self.two.itemsize, self.two.dtype.itemsize)
@xfailIfTorchDynamo # use ndarray.tensor._base to track the base tensor
def test_attributes_2(self):
assert_equal(self.two.base, np.arange(20))
def test_dtypeattr(self):
assert_equal(self.one.dtype, np.dtype(np.int_))
assert_equal(self.three.dtype, np.dtype(np.float64))
assert_equal(self.one.dtype.char, "l")
assert_equal(self.three.dtype.char, "d")
assert_(self.three.dtype.str[0] in "<>")
assert_equal(self.one.dtype.str[1], "i")
assert_equal(self.three.dtype.str[1], "f")
def test_stridesattr(self):
x = self.one
def make_array(size, offset, strides):
return np.ndarray(
size,
buffer=x,
dtype=int,
offset=offset * x.itemsize,
strides=strides * x.itemsize,
)
assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
assert_raises(ValueError, make_array, 4, 4, -2)
assert_raises(ValueError, make_array, 4, 2, -1)
assert_raises(ValueError, make_array, 8, 3, 1)
assert_equal(make_array(8, 3, 0), np.array([3] * 8))
# Check behavior reported in gh-2503:
assert_raises(ValueError, make_array, (2, 3), 5, np.array([-2, -3]))
make_array(0, 0, 10)
def test_set_stridesattr(self):
x = self.one
def make_array(size, offset, strides):
try:
r = np.ndarray([size], dtype=int, buffer=x, offset=offset * x.itemsize)
except Exception as e:
raise RuntimeError(e) # noqa: B904
r.strides = strides = strides * x.itemsize
return r
assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
assert_equal(make_array(7, 3, 1), np.array([3, 4, 5, 6, 7, 8, 9]))
assert_raises(ValueError, make_array, 4, 4, -2)
assert_raises(ValueError, make_array, 4, 2, -1)
assert_raises(RuntimeError, make_array, 8, 3, 1)
# Check that the true extent of the array is used.
# Test relies on as_strided base not exposing a buffer.
x = np.lib.stride_tricks.as_strided(np.arange(1), (10, 10), (0, 0))
def set_strides(arr, strides):
arr.strides = strides
assert_raises(ValueError, set_strides, x, (10 * x.itemsize, x.itemsize))
# Test for offset calculations:
x = np.lib.stride_tricks.as_strided(
np.arange(10, dtype=np.int8)[-1], shape=(10,), strides=(-1,)
)
assert_raises(ValueError, set_strides, x[::-1], -1)
a = x[::-1]
a.strides = 1
a[::2].strides = 2
# test 0d
arr_0d = np.array(0)
arr_0d.strides = ()
assert_raises(TypeError, set_strides, arr_0d, None)
def test_fill(self):
for t in "?bhilqpBHILQPfdgFDGO":
x = np.empty((3, 2, 1), t)
y = np.empty((3, 2, 1), t)
x.fill(1)
y[...] = 1
assert_equal(x, y)
def test_fill_max_uint64(self):
x = np.empty((3, 2, 1), dtype=np.uint64)
y = np.empty((3, 2, 1), dtype=np.uint64)
value = 2**64 - 1
y[...] = value
x.fill(value)
assert_array_equal(x, y)
def test_fill_struct_array(self):
# Filling from a scalar
x = np.array([(0, 0.0), (1, 1.0)], dtype="i4,f8")
x.fill(x[0])
assert_equal(x["f1"][1], x["f1"][0])
# Filling from a tuple that can be converted
# to a scalar
x = np.zeros(2, dtype=[("a", "f8"), ("b", "i4")])
x.fill((3.5, -2))
assert_array_equal(x["a"], [3.5, 3.5])
assert_array_equal(x["b"], [-2, -2])
def test_fill_readonly(self):
# gh-22922
a = np.zeros(11)
a.setflags(write=False)
with pytest.raises(ValueError, match=".*read-only"):
a.fill(0)
@instantiate_parametrized_tests
class TestArrayConstruction(TestCase):
def test_array(self):
d = np.ones(6)
r = np.array([d, d])
assert_equal(r, np.ones((2, 6)))
d = np.ones(6)
tgt = np.ones((2, 6))
r = np.array([d, d])
assert_equal(r, tgt)
tgt[1] = 2
r = np.array([d, d + 1])
assert_equal(r, tgt)
d = np.ones(6)
r = np.array([[d, d]])
assert_equal(r, np.ones((1, 2, 6)))
d = np.ones(6)
r = np.array([[d, d], [d, d]])
assert_equal(r, np.ones((2, 2, 6)))
d = np.ones((6, 6))
r = np.array([d, d])
assert_equal(r, np.ones((2, 6, 6)))
tgt = np.ones((2, 3), dtype=bool)
tgt[0, 2] = False
tgt[1, 0:2] = False
r = np.array([[True, True, False], [False, False, True]])
assert_equal(r, tgt)
r = np.array([[True, False], [True, False], [False, True]])
assert_equal(r, tgt.T)
@skip(reason="object arrays")
def test_array_object(self):
d = np.ones((6,))
r = np.array([[d, d + 1], d + 2], dtype=object)
assert_equal(len(r), 2)
assert_equal(r[0], [d, d + 1])
assert_equal(r[1], d + 2)
def test_array_empty(self):
assert_raises(TypeError, np.array)
def test_0d_array_shape(self):
assert np.ones(np.array(3)).shape == (3,)
def test_array_copy_false(self):
d = np.array([1, 2, 3])
e = np.array(d, copy=False)
d[1] = 3
assert_array_equal(e, [1, 3, 3])
@xpassIfTorchDynamo # (reason="order='F'")
def test_array_copy_false_2(self):
d = np.array([1, 2, 3])
e = np.array(d, copy=False, order="F")
d[1] = 4
assert_array_equal(e, [1, 4, 3])
e[2] = 7
assert_array_equal(d, [1, 4, 7])
def test_array_copy_true(self):
d = np.array([[1, 2, 3], [1, 2, 3]])
e = np.array(d, copy=True)
d[0, 1] = 3
e[0, 2] = -7
assert_array_equal(e, [[1, 2, -7], [1, 2, 3]])
assert_array_equal(d, [[1, 3, 3], [1, 2, 3]])
@xfail # (reason="order='F'")
def test_array_copy_true_2(self):
d = np.array([[1, 2, 3], [1, 2, 3]])
e = np.array(d, copy=True, order="F")
d[0, 1] = 5
e[0, 2] = 7
assert_array_equal(e, [[1, 3, 7], [1, 2, 3]])
assert_array_equal(d, [[1, 5, 3], [1, 2, 3]])
@xfailIfTorchDynamo
def test_array_cont(self):
d = np.ones(10)[::2]
assert_(np.ascontiguousarray(d).flags.c_contiguous)
assert_(np.ascontiguousarray(d).flags.f_contiguous)
assert_(np.asfortranarray(d).flags.c_contiguous)
# assert_(np.asfortranarray(d).flags.f_contiguous) # XXX: f ordering
d = np.ones((10, 10))[::2, ::2]
assert_(np.ascontiguousarray(d).flags.c_contiguous)
# assert_(np.asfortranarray(d).flags.f_contiguous)
@parametrize(
"func",
[
subtest(np.array, name="array"),
subtest(np.asarray, name="asarray"),
subtest(np.asanyarray, name="asanyarray"),
subtest(np.ascontiguousarray, name="ascontiguousarray"),
subtest(np.asfortranarray, name="asfortranarray"),
],
)
def test_bad_arguments_error(self, func):
with pytest.raises(TypeError):
func(3, dtype="bad dtype")
with pytest.raises(TypeError):
func() # missing arguments
with pytest.raises(TypeError):
func(1, 2, 3, 4, 5, 6, 7, 8) # too many arguments
@skip(reason="np.array w/keyword argument")
@parametrize(
"func",
[
subtest(np.array, name="array"),
subtest(np.asarray, name="asarray"),
subtest(np.asanyarray, name="asanyarray"),
subtest(np.ascontiguousarray, name="ascontiguousarray"),
subtest(np.asfortranarray, name="asfortranarray"),
],
)
def test_array_as_keyword(self, func):
# This should likely be made positional only, but do not change
# the name accidentally.
if func is np.array:
func(object=3)
else:
func(a=3)
class TestAssignment(TestCase):
def test_assignment_broadcasting(self):
a = np.arange(6).reshape(2, 3)
# Broadcasting the input to the output
a[...] = np.arange(3)
assert_equal(a, [[0, 1, 2], [0, 1, 2]])
a[...] = np.arange(2).reshape(2, 1)
assert_equal(a, [[0, 0, 0], [1, 1, 1]])
# For compatibility with <= 1.5, a limited version of broadcasting
# the output to the input.
#
# This behavior is inconsistent with NumPy broadcasting
# in general, because it only uses one of the two broadcasting
# rules (adding a new "1" dimension to the left of the shape),
# applied to the output instead of an input. In NumPy 2.0, this kind
# of broadcasting assignment will likely be disallowed.
a[...] = np.flip(np.arange(6)).reshape(1, 2, 3)
assert_equal(a, [[5, 4, 3], [2, 1, 0]])
# The other type of broadcasting would require a reduction operation.
def assign(a, b):
a[...] = b
assert_raises(
(RuntimeError, ValueError), assign, a, np.arange(12).reshape(2, 2, 3)
)
def test_assignment_errors(self):
# Address issue #2276
class C:
pass
a = np.zeros(1)
def assign(v):
a[0] = v
assert_raises((RuntimeError, TypeError), assign, C())
# assert_raises((TypeError, ValueError), assign, [1]) # numpy raises, we do not
@skip(reason="object arrays")
def test_unicode_assignment(self):
# gh-5049
from numpy.core.numeric import set_string_function
@contextmanager
def inject_str(s):
"""replace ndarray.__str__ temporarily"""
set_string_function(lambda x: s, repr=False)
try:
yield
finally:
set_string_function(None, repr=False)
a1d = np.array(["test"])
a0d = np.array("done")
with inject_str("bad"):
a1d[0] = a0d # previously this would invoke __str__
assert_equal(a1d[0], "done")
# this would crash for the same reason
np.array([np.array("\xe5\xe4\xf6")])
@skip(reason="object arrays")
def test_stringlike_empty_list(self):
# gh-8902
u = np.array(["done"])
b = np.array([b"done"])
class bad_sequence:
def __getitem__(self, value):
pass
def __len__(self):
raise RuntimeError
assert_raises(ValueError, operator.setitem, u, 0, [])
assert_raises(ValueError, operator.setitem, b, 0, [])
assert_raises(ValueError, operator.setitem, u, 0, bad_sequence())
assert_raises(ValueError, operator.setitem, b, 0, bad_sequence())
@skipif(
"torch._numpy" == np.__name__,
reason="torch._numpy does not support extended floats and complex dtypes",
)
def test_longdouble_assignment(self):
# only relevant if longdouble is larger than float
# we're looking for loss of precision
for dtype in (np.longdouble, np.clongdouble):
# gh-8902
tinyb = np.nextafter(np.longdouble(0), 1).astype(dtype)
tinya = np.nextafter(np.longdouble(0), -1).astype(dtype)
# construction
tiny1d = np.array([tinya])
assert_equal(tiny1d[0], tinya)
# scalar = scalar
tiny1d[0] = tinyb
assert_equal(tiny1d[0], tinyb)
# 0d = scalar
tiny1d[0, ...] = tinya
assert_equal(tiny1d[0], tinya)
# 0d = 0d
tiny1d[0, ...] = tinyb[...]
assert_equal(tiny1d[0], tinyb)
# scalar = 0d
tiny1d[0] = tinyb[...]
assert_equal(tiny1d[0], tinyb)
arr = np.array([np.array(tinya)])
assert_equal(arr[0], tinya)
@skip(reason="object arrays")
def test_cast_to_string(self):
# cast to str should do "str(scalar)", not "str(scalar.item())"
# Example: In python2, str(float) is truncated, so we want to avoid
# str(np.float64(...).item()) as this would incorrectly truncate.
a = np.zeros(1, dtype="S20")
a[:] = np.array(["1.12345678901234567890"], dtype="f8")
assert_equal(a[0], b"1.1234567890123457")
class TestDtypedescr(TestCase):
def test_construction(self):
d1 = np.dtype("i4")
assert_equal(d1, np.dtype(np.int32))
d2 = np.dtype("f8")
assert_equal(d2, np.dtype(np.float64))
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
def setUp(self):
self.d = np.array(0), np.array("x", object)
def test_ellipsis_subscript(self):
a, b = self.d
assert_equal(a[...], 0)
assert_equal(b[...], "x")
assert_(a[...].base is a) # `a[...] is a` in numpy <1.9.
assert_(b[...].base is b) # `b[...] is b` in numpy <1.9.
def test_empty_subscript(self):
a, b = self.d
assert_equal(a[()], 0)
assert_equal(b[()], "x")
assert_(type(a[()]) is a.dtype.type)
assert_(type(b[()]) is str)
def test_invalid_subscript(self):
a, b = self.d
assert_raises(IndexError, lambda x: x[0], a)
assert_raises(IndexError, lambda x: x[0], b)
assert_raises(IndexError, lambda x: x[np.array([], int)], a)
assert_raises(IndexError, lambda x: x[np.array([], int)], b)
def test_ellipsis_subscript_assignment(self):
a, b = self.d
a[...] = 42
assert_equal(a, 42)
b[...] = ""
assert_equal(b.item(), "")
def test_empty_subscript_assignment(self):
a, b = self.d
a[()] = 42
assert_equal(a, 42)
b[()] = ""
assert_equal(b.item(), "")
def test_invalid_subscript_assignment(self):
a, b = self.d
def assign(x, i, v):
x[i] = v
assert_raises(IndexError, assign, a, 0, 42)
assert_raises(IndexError, assign, b, 0, "")
assert_raises(ValueError, assign, a, (), "")
def test_newaxis(self):
a, b = self.d
assert_equal(a[np.newaxis].shape, (1,))
assert_equal(a[..., np.newaxis].shape, (1,))
assert_equal(a[np.newaxis, ...].shape, (1,))
assert_equal(a[..., np.newaxis].shape, (1,))
assert_equal(a[np.newaxis, ..., np.newaxis].shape, (1, 1))
assert_equal(a[..., np.newaxis, np.newaxis].shape, (1, 1))
assert_equal(a[np.newaxis, np.newaxis, ...].shape, (1, 1))
assert_equal(a[(np.newaxis,) * 10].shape, (1,) * 10)
def test_invalid_newaxis(self):
a, b = self.d
def subscript(x, i):
x[i]
assert_raises(IndexError, subscript, a, (np.newaxis, 0))
assert_raises(IndexError, subscript, a, (np.newaxis,) * 50)
def test_constructor(self):
x = np.ndarray(())
x[()] = 5
assert_equal(x[()], 5)
y = np.ndarray((), buffer=x)
y[()] = 6
assert_equal(x[()], 6)
# strides and shape must be the same length
with pytest.raises(ValueError):
np.ndarray((2,), strides=())
with pytest.raises(ValueError):
np.ndarray((), strides=(2,))
def test_output(self):
x = np.array(2)
assert_raises(ValueError, np.add, x, [1], x)
def test_real_imag(self):
# contiguity checks are for gh-11245
x = np.array(1j)
xr = x.real
xi = x.imag
assert_equal(xr, np.array(0))
assert_(type(xr) is np.ndarray)
assert_equal(xr.flags.contiguous, True)
assert_equal(xr.flags.f_contiguous, True)
assert_equal(xi, np.array(1))
assert_(type(xi) is np.ndarray)
assert_equal(xi.flags.contiguous, True)
assert_equal(xi.flags.f_contiguous, True)
class TestScalarIndexing(TestCase):
def setUp(self):
self.d = np.array([0, 1])[0]
def test_ellipsis_subscript(self):
a = self.d
assert_equal(a[...], 0)
assert_equal(a[...].shape, ())
def test_empty_subscript(self):
a = self.d
assert_equal(a[()], 0)
assert_equal(a[()].shape, ())
def test_invalid_subscript(self):
a = self.d
assert_raises(IndexError, lambda x: x[0], a)
assert_raises(IndexError, lambda x: x[np.array([], int)], a)
def test_invalid_subscript_assignment(self):
a = self.d
def assign(x, i, v):
x[i] = v
assert_raises((IndexError, TypeError), assign, a, 0, 42)
def test_newaxis(self):
a = self.d
assert_equal(a[np.newaxis].shape, (1,))
assert_equal(a[..., np.newaxis].shape, (1,))
assert_equal(a[np.newaxis, ...].shape, (1,))
assert_equal(a[..., np.newaxis].shape, (1,))
assert_equal(a[np.newaxis, ..., np.newaxis].shape, (1, 1))
assert_equal(a[..., np.newaxis, np.newaxis].shape, (1, 1))
assert_equal(a[np.newaxis, np.newaxis, ...].shape, (1, 1))
assert_equal(a[(np.newaxis,) * 10].shape, (1,) * 10)
def test_invalid_newaxis(self):
a = self.d
def subscript(x, i):
x[i]
assert_raises(IndexError, subscript, a, (np.newaxis, 0))
# this assersion fails because 50 > NPY_MAXDIMS = 32
# assert_raises(IndexError, subscript, a, (np.newaxis,)*50)
@xfail # (reason="pytorch disallows overlapping assignments")
def test_overlapping_assignment(self):
# With positive strides
a = np.arange(4)
a[:-1] = a[1:]
assert_equal(a, [1, 2, 3, 3])
a = np.arange(4)
a[1:] = a[:-1]
assert_equal(a, [0, 0, 1, 2])
# With positive and negative strides
a = np.arange(4)
a[:] = a[::-1]
assert_equal(a, [3, 2, 1, 0])
a = np.arange(6).reshape(2, 3)
a[::-1, :] = a[:, ::-1]
assert_equal(a, [[5, 4, 3], [2, 1, 0]])
a = np.arange(6).reshape(2, 3)
a[::-1, ::-1] = a[:, ::-1]
assert_equal(a, [[3, 4, 5], [0, 1, 2]])
# With just one element overlapping
a = np.arange(5)
a[:3] = a[2:]
assert_equal(a, [2, 3, 4, 3, 4])
a = np.arange(5)
a[2:] = a[:3]
assert_equal(a, [0, 1, 0, 1, 2])
a = np.arange(5)
a[2::-1] = a[2:]
assert_equal(a, [4, 3, 2, 3, 4])
a = np.arange(5)
a[2:] = a[2::-1]
assert_equal(a, [0, 1, 2, 1, 0])
a = np.arange(5)
a[2::-1] = a[:1:-1]
assert_equal(a, [2, 3, 4, 3, 4])
a = np.arange(5)
a[:1:-1] = a[2::-1]
assert_equal(a, [0, 1, 0, 1, 2])
@skip(reason="object, void, structured dtypes")
@instantiate_parametrized_tests
class TestCreation(TestCase):
"""
Test the np.array constructor
"""
def test_from_attribute(self):
class x:
def __array__(self, dtype=None):
pass
assert_raises(ValueError, np.array, x())
def test_from_string(self):
types = np.typecodes["AllInteger"] + np.typecodes["Float"]
nstr = ["123", "123"]
result = np.array([123, 123], dtype=int)
for type in types:
msg = f"String conversion for {type}"
assert_equal(np.array(nstr, dtype=type), result, err_msg=msg)
def test_void(self):
arr = np.array([], dtype="V")
assert arr.dtype == "V8" # current default
# Same length scalars (those that go to the same void) work:
arr = np.array([b"1234", b"1234"], dtype="V")
assert arr.dtype == "V4"
# Promoting different lengths will fail (pre 1.20 this worked)
# by going via S5 and casting to V5.
with pytest.raises(TypeError):
np.array([b"1234", b"12345"], dtype="V")
with pytest.raises(TypeError):
np.array([b"12345", b"1234"], dtype="V")
# Check the same for the casting path:
arr = np.array([b"1234", b"1234"], dtype="O").astype("V")
assert arr.dtype == "V4"
with pytest.raises(TypeError):
np.array([b"1234", b"12345"], dtype="O").astype("V")
@parametrize(
# "idx", [pytest.param(Ellipsis, id="arr"), pytest.param((), id="scalar")]
"idx",
[subtest(Ellipsis, name="arr"), subtest((), name="scalar")],
)
def test_structured_void_promotion(self, idx):
arr = np.array(
[np.array(1, dtype="i,i")[idx], np.array(2, dtype="i,i")[idx]], dtype="V"
)
assert_array_equal(arr, np.array([(1, 1), (2, 2)], dtype="i,i"))
# The following fails to promote the two dtypes, resulting in an error
with pytest.raises(TypeError):
np.array(
[np.array(1, dtype="i,i")[idx], np.array(2, dtype="i,i,i")[idx]],
dtype="V",
)
def test_too_big_error(self):
# 45341 is the smallest integer greater than sqrt(2**31 - 1).
# 3037000500 is the smallest integer greater than sqrt(2**63 - 1).
# We want to make sure that the square byte array with those dimensions
# is too big on 32 or 64 bit systems respectively.
if np.iinfo("intp").max == 2**31 - 1:
shape = (46341, 46341)
elif np.iinfo("intp").max == 2**63 - 1:
shape = (3037000500, 3037000500)
else:
return
assert_raises(ValueError, np.empty, shape, dtype=np.int8)
assert_raises(ValueError, np.zeros, shape, dtype=np.int8)
assert_raises(ValueError, np.ones, shape, dtype=np.int8)
@skipif(
np.dtype(np.intp).itemsize != 8, reason="malloc may not fail on 32 bit systems"
)
def test_malloc_fails(self):
# This test is guaranteed to fail due to a too large allocation
with assert_raises(np.core._exceptions._ArrayMemoryError):
np.empty(np.iinfo(np.intp).max, dtype=np.uint8)
def test_zeros(self):
types = np.typecodes["AllInteger"] + np.typecodes["AllFloat"]
for dt in types:
d = np.zeros((13,), dtype=dt)
assert_equal(np.count_nonzero(d), 0)
# true for ieee floats
assert_equal(d.sum(), 0)
assert_(not d.any())
d = np.zeros(2, dtype="(2,4)i4")
assert_equal(np.count_nonzero(d), 0)
assert_equal(d.sum(), 0)
assert_(not d.any())
d = np.zeros(2, dtype="4i4")
assert_equal(np.count_nonzero(d), 0)
assert_equal(d.sum(), 0)
assert_(not d.any())
d = np.zeros(2, dtype="(2,4)i4, (2,4)i4")
assert_equal(np.count_nonzero(d), 0)
@slow
def test_zeros_big(self):
# test big array as they might be allocated different by the system
types = np.typecodes["AllInteger"] + np.typecodes["AllFloat"]
for dt in types:
d = np.zeros((30 * 1024**2,), dtype=dt)
assert_(not d.any())
# This test can fail on 32-bit systems due to insufficient
# contiguous memory. Deallocating the previous array increases the
# chance of success.
del d
def test_zeros_obj(self):
# test initialization from PyLong(0)
d = np.zeros((13,), dtype=object)
assert_array_equal(d, [0] * 13)
assert_equal(np.count_nonzero(d), 0)
def test_zeros_obj_obj(self):
d = np.zeros(10, dtype=[("k", object, 2)])
assert_array_equal(d["k"], 0)
def test_zeros_like_like_zeros(self):
# test zeros_like returns the same as zeros
for c in np.typecodes["All"]:
if c == "V":
continue
d = np.zeros((3, 3), dtype=c)
assert_array_equal(np.zeros_like(d), d)
assert_equal(np.zeros_like(d).dtype, d.dtype)
# explicitly check some special cases
d = np.zeros((3, 3), dtype="S5")
assert_array_equal(np.zeros_like(d), d)
assert_equal(np.zeros_like(d).dtype, d.dtype)
d = np.zeros((3, 3), dtype="U5")
assert_array_equal(np.zeros_like(d), d)
assert_equal(np.zeros_like(d).dtype, d.dtype)
d = np.zeros((3, 3), dtype="<i4")
assert_array_equal(np.zeros_like(d), d)
assert_equal(np.zeros_like(d).dtype, d.dtype)
d = np.zeros((3, 3), dtype=">i4")
assert_array_equal(np.zeros_like(d), d)
assert_equal(np.zeros_like(d).dtype, d.dtype)
d = np.zeros((3, 3), dtype="<M8[s]")
assert_array_equal(np.zeros_like(d), d)
assert_equal(np.zeros_like(d).dtype, d.dtype)
d = np.zeros((3, 3), dtype=">M8[s]")
assert_array_equal(np.zeros_like(d), d)
assert_equal(np.zeros_like(d).dtype, d.dtype)
d = np.zeros((3, 3), dtype="f4,f4")
assert_array_equal(np.zeros_like(d), d)
assert_equal(np.zeros_like(d).dtype, d.dtype)
def test_empty_unicode(self):
# don't throw decode errors on garbage memory
for i in range(5, 100, 5):
d = np.empty(i, dtype="U")
str(d)
def test_sequence_non_homogeneous(self):
assert_equal(np.array([4, 2**80]).dtype, object)
assert_equal(np.array([4, 2**80, 4]).dtype, object)
assert_equal(np.array([2**80, 4]).dtype, object)
assert_equal(np.array([2**80] * 3).dtype, object)
assert_equal(np.array([[1, 1], [1j, 1j]]).dtype, complex)
assert_equal(np.array([[1j, 1j], [1, 1]]).dtype, complex)
assert_equal(np.array([[1, 1, 1], [1, 1j, 1.0], [1, 1, 1]]).dtype, complex)
def test_non_sequence_sequence(self):
"""Should not segfault.
Class Fail breaks the sequence protocol for new style classes, i.e.,
those derived from object. Class Map is a mapping type indicated by
raising a ValueError. At some point we may raise a warning instead
of an error in the Fail case.
"""
class Fail:
def __len__(self):
return 1
def __getitem__(self, index):
raise ValueError
class Map:
def __len__(self):
return 1
def __getitem__(self, index):
raise KeyError
a = np.array([Map()])
assert_(a.shape == (1,))
assert_(a.dtype == np.dtype(object))
assert_raises(ValueError, np.array, [Fail()])
def test_no_len_object_type(self):
# gh-5100, want object array from iterable object without len()
class Point2:
def __init__(self) -> None:
pass
def __getitem__(self, ind):
if ind in [0, 1]:
return ind
else:
raise IndexError
d = np.array([Point2(), Point2(), Point2()])
assert_equal(d.dtype, np.dtype(object))
def test_false_len_sequence(self):
# gh-7264, segfault for this example
class C:
def __getitem__(self, i):
raise IndexError
def __len__(self):
return 42
a = np.array(C()) # segfault?
assert_equal(len(a), 0)
def test_false_len_iterable(self):
# Special case where a bad __getitem__ makes us fall back on __iter__:
class C:
def __getitem__(self, x):
raise Exception # noqa: TRY002
def __iter__(self):
return iter(())
def __len__(self):
return 2
a = np.empty(2)
with assert_raises(ValueError):
a[:] = C() # Segfault!
assert_equal(np.array(C()), list(C()))
def test_failed_len_sequence(self):
# gh-7393
class A:
def __init__(self, data):
self._data = data
def __getitem__(self, item):
return type(self)(self._data[item])
def __len__(self):
return len(self._data)
# len(d) should give 3, but len(d[0]) will fail
d = A([1, 2, 3])
assert_equal(len(np.array(d)), 3)
def test_array_too_big(self):
# Test that array creation succeeds for arrays addressable by intp
# on the byte level and fails for too large arrays.
buf = np.zeros(100)
max_bytes = np.iinfo(np.intp).max
for dtype in ["intp", "S20", "b"]:
dtype = np.dtype(dtype)
itemsize = dtype.itemsize
np.ndarray(
buffer=buf, strides=(0,), shape=(max_bytes // itemsize,), dtype=dtype
)
assert_raises(
ValueError,
np.ndarray,
buffer=buf,
strides=(0,),
shape=(max_bytes // itemsize + 1,),
dtype=dtype,
)
def _ragged_creation(self, seq):
# without dtype=object, the ragged object raises
with pytest.raises(ValueError, match=".*detected shape was"):
a = np.array(seq)
return np.array(seq, dtype=object)
def test_ragged_ndim_object(self):
# Lists of mismatching depths are treated as object arrays
a = self._ragged_creation([[1], 2, 3])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
a = self._ragged_creation([1, [2], 3])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
a = self._ragged_creation([1, 2, [3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
def test_ragged_shape_object(self):
# The ragged dimension of a list is turned into an object array
a = self._ragged_creation([[1, 1], [2], [3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
a = self._ragged_creation([[1], [2, 2], [3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
a = self._ragged_creation([[1], [2], [3, 3]])
assert a.shape == (3,)
assert a.dtype == object
def test_array_of_ragged_array(self):
outer = np.array([None, None])
outer[0] = outer[1] = np.array([1, 2, 3])
assert np.array(outer).shape == (2,)
assert np.array([outer]).shape == (1, 2)
outer_ragged = np.array([None, None])
outer_ragged[0] = np.array([1, 2, 3])
outer_ragged[1] = np.array([1, 2, 3, 4])
# should both of these emit deprecation warnings?
assert np.array(outer_ragged).shape == (2,)
assert np.array([outer_ragged]).shape == (
1,
2,
)
def test_deep_nonragged_object(self):
# None of these should raise, even though they are missing dtype=object
a = np.array([[[Decimal(1)]]])
a = np.array([1, Decimal(1)])
a = np.array([[1], [Decimal(1)]])
@parametrize("dtype", [object, "O,O", "O,(3)O", "(2,3)O"])
@parametrize(
"function",
[
np.ndarray,
np.empty,
lambda shape, dtype: np.empty_like(np.empty(shape, dtype=dtype)),
],
)
def test_object_initialized_to_None(self, function, dtype):
# NumPy has support for object fields to be NULL (meaning None)
# but generally, we should always fill with the proper None, and
# downstream may rely on that. (For fully initialized arrays!)
arr = function(3, dtype=dtype)
# We expect a fill value of None, which is not NULL:
expected = np.array(None).tobytes()
expected = expected * (arr.nbytes // len(expected))
assert arr.tobytes() == expected
class TestBool(TestCase):
@xfail # (reason="bools not interned")
def test_test_interning(self):
a0 = np.bool_(0)
b0 = np.bool_(False)
assert_(a0 is b0)
a1 = np.bool_(1)
b1 = np.bool_(True)
assert_(a1 is b1)
assert_(np.array([True])[0] is a1)
assert_(np.array(True)[()] is a1)
def test_sum(self):
d = np.ones(101, dtype=bool)
assert_equal(d.sum(), d.size)
assert_equal(d[::2].sum(), d[::2].size)
# assert_equal(d[::-2].sum(), d[::-2].size)
@xpassIfTorchDynamo # (reason="frombuffer")
def test_sum_2(self):
d = np.frombuffer(b"\xff\xff" * 100, dtype=bool)
assert_equal(d.sum(), d.size)
assert_equal(d[::2].sum(), d[::2].size)
assert_equal(d[::-2].sum(), d[::-2].size)
def check_count_nonzero(self, power, length):
powers = [2**i for i in range(length)]
for i in range(2**power):
l = [(i & x) != 0 for x in powers]
a = np.array(l, dtype=bool)
c = builtins.sum(l)
assert_equal(np.count_nonzero(a), c)
av = a.view(np.uint8)
av *= 3
assert_equal(np.count_nonzero(a), c)
av *= 4
assert_equal(np.count_nonzero(a), c)
av[av != 0] = 0xFF
assert_equal(np.count_nonzero(a), c)
def test_count_nonzero(self):
# check all 12 bit combinations in a length 17 array
# covers most cases of the 16 byte unrolled code
self.check_count_nonzero(12, 17)
@slow
def test_count_nonzero_all(self):
# check all combinations in a length 17 array
# covers all cases of the 16 byte unrolled code
self.check_count_nonzero(17, 17)
def test_count_nonzero_unaligned(self):
# prevent mistakes as e.g. gh-4060
for o in range(7):
a = np.zeros((18,), dtype=bool)[o + 1 :]
a[:o] = True
assert_equal(np.count_nonzero(a), builtins.sum(a.tolist()))
a = np.ones((18,), dtype=bool)[o + 1 :]
a[:o] = False
assert_equal(np.count_nonzero(a), builtins.sum(a.tolist()))
def _test_cast_from_flexible(self, dtype):
# empty string -> false
for n in range(3):
v = np.array(b"", (dtype, n))
assert_equal(bool(v), False)
assert_equal(bool(v[()]), False)
assert_equal(v.astype(bool), False)
assert_(isinstance(v.astype(bool), np.ndarray))
assert_(v[()].astype(bool) is np.False_)
# anything else -> true
for n in range(1, 4):
for val in [b"a", b"0", b" "]:
v = np.array(val, (dtype, n))
assert_equal(bool(v), True)
assert_equal(bool(v[()]), True)
assert_equal(v.astype(bool), True)
assert_(isinstance(v.astype(bool), np.ndarray))
assert_(v[()].astype(bool) is np.True_)
@skip(reason="np.void")
def test_cast_from_void(self):
self._test_cast_from_flexible(np.void)
@xfail # (reason="See gh-9847")
def test_cast_from_unicode(self):
self._test_cast_from_flexible(np.str_)
@xfail # (reason="See gh-9847")
def test_cast_from_bytes(self):
self._test_cast_from_flexible(np.bytes_)
@instantiate_parametrized_tests
class TestMethods(TestCase):
sort_kinds = ["quicksort", "heapsort", "stable"]
@xpassIfTorchDynamo # (reason="all(..., where=...)")
def test_all_where(self):
a = np.array([[True, False, True], [False, False, False], [True, True, True]])
wh_full = np.array(
[[True, False, True], [False, False, False], [True, False, True]]
)
wh_lower = np.array([[False], [False], [True]])
for _ax in [0, None]:
assert_equal(
a.all(axis=_ax, where=wh_lower), np.all(a[wh_lower[:, 0], :], axis=_ax)
)
assert_equal(
np.all(a, axis=_ax, where=wh_lower), a[wh_lower[:, 0], :].all(axis=_ax)
)
assert_equal(a.all(where=wh_full), True)
assert_equal(np.all(a, where=wh_full), True)
assert_equal(a.all(where=False), True)
assert_equal(np.all(a, where=False), True)
@xpassIfTorchDynamo # (reason="any(..., where=...)")
def test_any_where(self):
a = np.array([[True, False, True], [False, False, False], [True, True, True]])
wh_full = np.array(
[[False, True, False], [True, True, True], [False, False, False]]
)
wh_middle = np.array([[False], [True], [False]])
for _ax in [0, None]:
assert_equal(
a.any(axis=_ax, where=wh_middle),
np.any(a[wh_middle[:, 0], :], axis=_ax),
)
assert_equal(
np.any(a, axis=_ax, where=wh_middle),
a[wh_middle[:, 0], :].any(axis=_ax),
)
assert_equal(a.any(where=wh_full), False)
assert_equal(np.any(a, where=wh_full), False)
assert_equal(a.any(where=False), False)
assert_equal(np.any(a, where=False), False)
@xpassIfTorchDynamo # (reason="TODO: compress")
def test_compress(self):
tgt = [[5, 6, 7, 8, 9]]
arr = np.arange(10).reshape(2, 5)
out = arr.compress([0, 1], axis=0)
assert_equal(out, tgt)
tgt = [[1, 3], [6, 8]]
out = arr.compress([0, 1, 0, 1, 0], axis=1)
assert_equal(out, tgt)
tgt = [[1], [6]]
arr = np.arange(10).reshape(2, 5)
out = arr.compress([0, 1], axis=1)
assert_equal(out, tgt)
arr = np.arange(10).reshape(2, 5)
out = arr.compress([0, 1])
assert_equal(out, 1)
def test_choose(self):
x = 2 * np.ones((3,), dtype=int)
y = 3 * np.ones((3,), dtype=int)
x2 = 2 * np.ones((2, 3), dtype=int)
y2 = 3 * np.ones((2, 3), dtype=int)
ind = np.array([0, 0, 1])
A = ind.choose((x, y))
assert_equal(A, [2, 2, 3])
A = ind.choose((x2, y2))
assert_equal(A, [[2, 2, 3], [2, 2, 3]])
A = ind.choose((x, y2))
assert_equal(A, [[2, 2, 3], [2, 2, 3]])
out = np.array(0)
ret = np.choose(np.array(1), [10, 20, 30], out=out)
assert out is ret
assert_equal(out[()], 20)
@xpassIfTorchDynamo # (reason="choose(..., mode=...) not implemented")
def test_choose_2(self):
# gh-6272 check overlap on out
x = np.arange(5)
y = np.choose([0, 0, 0], [x[:3], x[:3], x[:3]], out=x[1:4], mode="wrap")
assert_equal(y, np.array([0, 1, 2]))
def test_prod(self):
ba = [1, 2, 10, 11, 6, 5, 4]
ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]]
for ctype in [
np.int16,
np.int32,
np.float32,
np.float64,
np.complex64,
np.complex128,
]:
a = np.array(ba, ctype)
a2 = np.array(ba2, ctype)
if ctype in ["1", "b"]:
assert_raises(ArithmeticError, a.prod)
assert_raises(ArithmeticError, a2.prod, axis=1)
else:
assert_equal(a.prod(axis=0), 26400)
assert_array_equal(a2.prod(axis=0), np.array([50, 36, 84, 180], ctype))
assert_array_equal(a2.prod(axis=-1), np.array([24, 1890, 600], ctype))
def test_repeat(self):
m = np.array([1, 2, 3, 4, 5, 6])
m_rect = m.reshape((2, 3))
A = m.repeat([1, 3, 2, 1, 1, 2])
assert_equal(A, [1, 2, 2, 2, 3, 3, 4, 5, 6, 6])
A = m.repeat(2)
assert_equal(A, [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
A = m_rect.repeat([2, 1], axis=0)
assert_equal(A, [[1, 2, 3], [1, 2, 3], [4, 5, 6]])
A = m_rect.repeat([1, 3, 2], axis=1)
assert_equal(A, [[1, 2, 2, 2, 3, 3], [4, 5, 5, 5, 6, 6]])
A = m_rect.repeat(2, axis=0)
assert_equal(A, [[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]])
A = m_rect.repeat(2, axis=1)
assert_equal(A, [[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6]])
@xpassIfTorchDynamo # (reason="reshape(..., order='F')")
def test_reshape(self):
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
tgt = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
assert_equal(arr.reshape(2, 6), tgt)
tgt = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
assert_equal(arr.reshape(3, 4), tgt)
tgt = [[1, 10, 8, 6], [4, 2, 11, 9], [7, 5, 3, 12]]
assert_equal(arr.reshape((3, 4), order="F"), tgt)
tgt = [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
assert_equal(arr.T.reshape((3, 4), order="C"), tgt)
def test_round(self):
def check_round(arr, expected, *round_args):
assert_equal(arr.round(*round_args), expected)
# With output array
out = np.zeros_like(arr)
res = arr.round(*round_args, out=out)
assert_equal(out, expected)
assert out is res
check_round(np.array([1.2, 1.5]), [1, 2])
check_round(np.array(1.5), 2)
check_round(np.array([12.2, 15.5]), [10, 20], -1)
check_round(np.array([12.15, 15.51]), [12.2, 15.5], 1)
# Complex rounding
check_round(np.array([4.5 + 1.5j]), [4 + 2j])
check_round(np.array([12.5 + 15.5j]), [10 + 20j], -1)
def test_squeeze(self):
a = np.array([[[1], [2], [3]]])
assert_equal(a.squeeze(), [1, 2, 3])
assert_equal(a.squeeze(axis=(0,)), [[1], [2], [3]])
# assert_raises(ValueError, a.squeeze, axis=(1,)) # a noop in pytorch
assert_equal(a.squeeze(axis=(2,)), [[1, 2, 3]])
def test_transpose(self):
a = np.array([[1, 2], [3, 4]])
assert_equal(a.transpose(), [[1, 3], [2, 4]])
assert_raises((RuntimeError, ValueError), lambda: a.transpose(0))
assert_raises((RuntimeError, ValueError), lambda: a.transpose(0, 0))
assert_raises((RuntimeError, ValueError), lambda: a.transpose(0, 1, 2))
def test_sort(self):
# test ordering for floats and complex containing nans. It is only
# necessary to check the less-than comparison, so sorts that
# only follow the insertion sort path are sufficient. We only
# test doubles and complex doubles as the logic is the same.
# check doubles
msg = "Test real sort order with nans"
a = np.array([np.nan, 1, 0])
b = np.sort(a)
assert_equal(b, np.flip(a), msg)
@xpassIfTorchDynamo # (reason="sort complex")
def test_sort_complex_nans(self):
# check complex
msg = "Test complex sort order with nans"
a = np.zeros(9, dtype=np.complex128)
a.real += [np.nan, np.nan, np.nan, 1, 0, 1, 1, 0, 0]
a.imag += [np.nan, 1, 0, np.nan, np.nan, 1, 0, 1, 0]
b = np.sort(a)
assert_equal(b, a[::-1], msg)
# all c scalar sorts use the same code with different types
# so it suffices to run a quick check with one type. The number
# of sorted items must be greater than ~50 to check the actual
# algorithm because quick and merge sort fall over to insertion
# sort for small arrays.
@parametrize("dtype", [np.uint8, np.float16, np.float32, np.float64])
def test_sort_unsigned(self, dtype):
a = np.arange(101, dtype=dtype)
b = np.flip(a)
for kind in self.sort_kinds:
msg = f"scalar sort, kind={kind}"
c = a.copy()
c.sort(kind=kind)
assert_equal(c, a, msg)
c = b.copy()
c.sort(kind=kind)
assert_equal(c, a, msg)
@parametrize(
"dtype",
[np.int8, np.int16, np.int32, np.int64, np.float16, np.float32, np.float64],
)
def test_sort_signed(self, dtype):
a = np.arange(-50, 51, dtype=dtype)
b = np.flip(a)
for kind in self.sort_kinds:
msg = f"scalar sort, kind={kind}"
c = a.copy()
c.sort(kind=kind)
assert_equal(c, a, msg)
c = b.copy()
c.sort(kind=kind)
assert_equal(c, a, msg)
@xpassIfTorchDynamo # (reason="sort complex")
@parametrize("dtype", [np.float32, np.float64])
@parametrize("part", ["real", "imag"])
def test_sort_complex(self, part, dtype):
# test complex sorts. These use the same code as the scalars
# but the compare function differs.
cdtype = {
np.single: np.csingle,
np.double: np.cdouble,
}[dtype]
a = np.arange(-50, 51, dtype=dtype)
b = a[::-1].copy()
ai = (a * (1 + 1j)).astype(cdtype)
bi = (b * (1 + 1j)).astype(cdtype)
setattr(ai, part, 1)
setattr(bi, part, 1)
for kind in self.sort_kinds:
msg = f"complex sort, {part} part == 1, kind={kind}"
c = ai.copy()
c.sort(kind=kind)
assert_equal(c, ai, msg)
c = bi.copy()
c.sort(kind=kind)
assert_equal(c, ai, msg)
def test_sort_axis(self):
# check axis handling. This should be the same for all type
# specific sorts, so we only check it for one type and one kind
a = np.array([[3, 2], [1, 0]])
b = np.array([[1, 0], [3, 2]])
c = np.array([[2, 3], [0, 1]])
d = a.copy()
d.sort(axis=0)
assert_equal(d, b, "test sort with axis=0")
d = a.copy()
d.sort(axis=1)
assert_equal(d, c, "test sort with axis=1")
d = a.copy()
d.sort()
assert_equal(d, c, "test sort with default axis")
def test_sort_size_0(self):
# check axis handling for multidimensional empty arrays
a = np.array([])
a = a.reshape(3, 2, 1, 0)
for axis in range(-a.ndim, a.ndim):
msg = f"test empty array sort with axis={axis}"
assert_equal(np.sort(a, axis=axis), a, msg)
msg = "test empty array sort with axis=None"
assert_equal(np.sort(a, axis=None), a.ravel(), msg)
@skip(reason="waaay tooo sloooow")
def test_sort_degraded(self):
# test degraded dataset would take minutes to run with normal qsort
d = np.arange(1000000)
do = d.copy()
x = d
# create a median of 3 killer where each median is the sorted second
# last element of the quicksort partition
while x.size > 3:
mid = x.size // 2
x[mid], x[-2] = x[-2], x[mid]
x = x[:-2]
assert_equal(np.sort(d), do)
assert_equal(d[np.argsort(d)], do)
@xfail # (reason="order='F'")
def test_copy(self):
def assert_fortran(arr):
assert_(arr.flags.fortran)
assert_(arr.flags.f_contiguous)
assert_(not arr.flags.c_contiguous)
def assert_c(arr):
assert_(not arr.flags.fortran)
assert_(not arr.flags.f_contiguous)
assert_(arr.flags.c_contiguous)
a = np.empty((2, 2), order="F")
# Test copying a Fortran array
assert_c(a.copy())
assert_c(a.copy("C"))
assert_fortran(a.copy("F"))
assert_fortran(a.copy("A"))
# Now test starting with a C array.
a = np.empty((2, 2), order="C")
assert_c(a.copy())
assert_c(a.copy("C"))
assert_fortran(a.copy("F"))
assert_c(a.copy("A"))
@skip(reason="no .ctypes attribute")
@parametrize("dtype", [np.int32])
def test__deepcopy__(self, dtype):
# Force the entry of NULLs into array
a = np.empty(4, dtype=dtype)
ctypes.memset(a.ctypes.data, 0, a.nbytes)
# Ensure no error is raised, see gh-21833
b = a.__deepcopy__({})
a[0] = 42
with pytest.raises(AssertionError):
assert_array_equal(a, b)
def test_argsort(self):
# all c scalar argsorts use the same code with different types
# so it suffices to run a quick check with one type. The number
# of sorted items must be greater than ~50 to check the actual
# algorithm because quick and merge sort fall over to insertion
# sort for small arrays.
for dtype in [np.int32, np.uint8, np.float32]:
a = np.arange(101, dtype=dtype)
b = np.flip(a)
for kind in self.sort_kinds:
msg = f"scalar argsort, kind={kind}, dtype={dtype}"
assert_equal(a.copy().argsort(kind=kind), a, msg)
assert_equal(b.copy().argsort(kind=kind), b, msg)
@skip(reason="argsort complex")
def test_argsort_complex(self):
a = np.arange(101, dtype=np.float32)
b = np.flip(a)
# test complex argsorts. These use the same code as the scalars
# but the compare function differs.
ai = a * 1j + 1
bi = b * 1j + 1
for kind in self.sort_kinds:
msg = f"complex argsort, kind={kind}"
assert_equal(ai.copy().argsort(kind=kind), a, msg)
assert_equal(bi.copy().argsort(kind=kind), b, msg)
ai = a + 1j
bi = b + 1j
for kind in self.sort_kinds:
msg = f"complex argsort, kind={kind}"
assert_equal(ai.copy().argsort(kind=kind), a, msg)
assert_equal(bi.copy().argsort(kind=kind), b, msg)
# test argsort of complex arrays requiring byte-swapping, gh-5441
for endianness in "<>":
for dt in np.typecodes["Complex"]:
arr = np.array([1 + 3.0j, 2 + 2.0j, 3 + 1.0j], dtype=endianness + dt)
msg = f"byte-swapped complex argsort, dtype={dt}"
assert_equal(arr.argsort(), np.arange(len(arr), dtype=np.intp), msg)
@xpassIfTorchDynamo # (reason="argsort axis TODO")
def test_argsort_axis(self):
# check axis handling. This should be the same for all type
# specific argsorts, so we only check it for one type and one kind
a = np.array([[3, 2], [1, 0]])
b = np.array([[1, 1], [0, 0]])
c = np.array([[1, 0], [1, 0]])
assert_equal(a.copy().argsort(axis=0), b)
assert_equal(a.copy().argsort(axis=1), c)
assert_equal(a.copy().argsort(), c)
# check axis handling for multidimensional empty arrays
a = np.array([])
a = a.reshape(3, 2, 1, 0)
for axis in range(-a.ndim, a.ndim):
msg = f"test empty array argsort with axis={axis}"
assert_equal(np.argsort(a, axis=axis), np.zeros_like(a, dtype=np.intp), msg)
msg = "test empty array argsort with axis=None"
assert_equal(
np.argsort(a, axis=None), np.zeros_like(a.ravel(), dtype=np.intp), msg
)
# check that stable argsorts are stable
r = np.arange(100)
# scalars
a = np.zeros(100)
assert_equal(a.argsort(kind="m"), r)
# complex
a = np.zeros(100, dtype=complex)
assert_equal(a.argsort(kind="m"), r)
# string
a = np.array(["aaaaaaaaa" for i in range(100)])
assert_equal(a.argsort(kind="m"), r)
# unicode
a = np.array(["aaaaaaaaa" for i in range(100)], dtype=np.str_)
assert_equal(a.argsort(kind="m"), r)
@xpassIfTorchDynamo # (reason="TODO: searchsorted with nans differs in pytorch")
@parametrize(
"a",
[
subtest(np.array([0, 1, np.nan], dtype=np.float16), name="f16"),
subtest(np.array([0, 1, np.nan], dtype=np.float32), name="f32"),
subtest(np.array([0, 1, np.nan]), name="default_dtype"),
],
)
def test_searchsorted_floats(self, a):
# test for floats arrays containing nans. Explicitly test
# half, single, and double precision floats to verify that
# the NaN-handling is correct.
msg = f"Test real ({a.dtype}) searchsorted with nans, side='l'"
b = a.searchsorted(a, side="left")
assert_equal(b, np.arange(3), msg)
msg = f"Test real ({a.dtype}) searchsorted with nans, side='r'"
b = a.searchsorted(a, side="right")
assert_equal(b, np.arange(1, 4), msg)
# check keyword arguments
a.searchsorted(v=1)
x = np.array([0, 1, np.nan], dtype="float32")
y = np.searchsorted(x, x[-1])
assert_equal(y, 2)
@xfail # (
# reason="'searchsorted_out_cpu' not implemented for 'ComplexDouble'"
# )
def test_searchsorted_complex(self):
# test for complex arrays containing nans.
# The search sorted routines use the compare functions for the
# array type, so this checks if that is consistent with the sort
# order.
# check double complex
a = np.zeros(9, dtype=np.complex128)
a.real += [0, 0, 1, 1, 0, 1, np.nan, np.nan, np.nan]
a.imag += [0, 1, 0, 1, np.nan, np.nan, 0, 1, np.nan]
msg = "Test complex searchsorted with nans, side='l'"
b = a.searchsorted(a, side="left")
assert_equal(b, np.arange(9), msg)
msg = "Test complex searchsorted with nans, side='r'"
b = a.searchsorted(a, side="right")
assert_equal(b, np.arange(1, 10), msg)
msg = "Test searchsorted with little endian, side='l'"
a = np.array([0, 128], dtype="<i4")
b = a.searchsorted(np.array(128, dtype="<i4"))
assert_equal(b, 1, msg)
msg = "Test searchsorted with big endian, side='l'"
a = np.array([0, 128], dtype=">i4")
b = a.searchsorted(np.array(128, dtype=">i4"))
assert_equal(b, 1, msg)
def test_searchsorted_n_elements(self):
# Check 0 elements
a = np.ones(0)
b = a.searchsorted([0, 1, 2], "left")
assert_equal(b, [0, 0, 0])
b = a.searchsorted([0, 1, 2], "right")
assert_equal(b, [0, 0, 0])
a = np.ones(1)
# Check 1 element
b = a.searchsorted([0, 1, 2], "left")
assert_equal(b, [0, 0, 1])
b = a.searchsorted([0, 1, 2], "right")
assert_equal(b, [0, 1, 1])
# Check all elements equal
a = np.ones(2)
b = a.searchsorted([0, 1, 2], "left")
assert_equal(b, [0, 0, 2])
b = a.searchsorted([0, 1, 2], "right")
assert_equal(b, [0, 2, 2])
@xpassIfTorchDynamo # (
# reason="RuntimeError: self.storage_offset() must be divisible by 8"
# )
def test_searchsorted_unaligned_array(self):
# Test searching unaligned array
a = np.arange(10)
aligned = np.empty(a.itemsize * a.size + 1, dtype="uint8")
unaligned = aligned[1:].view(a.dtype)
unaligned[:] = a
# Test searching unaligned array
b = unaligned.searchsorted(a, "left")
assert_equal(b, a)
b = unaligned.searchsorted(a, "right")
assert_equal(b, a + 1)
# Test searching for unaligned keys
b = a.searchsorted(unaligned, "left")
assert_equal(b, a)
b = a.searchsorted(unaligned, "right")
assert_equal(b, a + 1)
def test_searchsorted_resetting(self):
# Test smart resetting of binsearch indices
a = np.arange(5)
b = a.searchsorted([6, 5, 4], "left")
assert_equal(b, [5, 5, 4])
b = a.searchsorted([6, 5, 4], "right")
assert_equal(b, [5, 5, 5])
def test_searchsorted_type_specific(self):
# Test all type specific binary search functions
types = "".join((np.typecodes["AllInteger"], np.typecodes["Float"]))
for dt in types:
if dt == "?":
a = np.arange(2, dtype=dt)
out = np.arange(2)
else:
a = np.arange(0, 5, dtype=dt)
out = np.arange(5)
b = a.searchsorted(a, "left")
assert_equal(b, out)
b = a.searchsorted(a, "right")
assert_equal(b, out + 1)
@xpassIfTorchDynamo # (reason="ndarray ctor")
def test_searchsorted_type_specific_2(self):
# Test all type specific binary search functions
types = "".join((np.typecodes["AllInteger"], np.typecodes["AllFloat"], "?"))
for dt in types:
if dt == "?":
a = np.arange(2, dtype=dt)
out = np.arange(2)
else:
a = np.arange(0, 5, dtype=dt)
out = np.arange(5)
# Test empty array, use a fresh array to get warnings in
# valgrind if access happens.
e = np.ndarray(shape=0, buffer=b"", dtype=dt)
b = e.searchsorted(a, "left")
assert_array_equal(b, np.zeros(len(a), dtype=np.intp))
b = a.searchsorted(e, "left")
assert_array_equal(b, np.zeros(0, dtype=np.intp))
def test_searchsorted_with_invalid_sorter(self):
a = np.array([5, 2, 1, 3, 4])
s = np.argsort(a)
assert_raises((TypeError, RuntimeError), np.searchsorted, a, 0, sorter=[1.1])
assert_raises(
(ValueError, RuntimeError), np.searchsorted, a, 0, sorter=[1, 2, 3, 4]
)
assert_raises(
(ValueError, RuntimeError), np.searchsorted, a, 0, sorter=[1, 2, 3, 4, 5, 6]
)
# bounds check : XXX torch does not raise
# assert_raises(ValueError, np.searchsorted, a, 4, sorter=[0, 1, 2, 3, 5])
# assert_raises(ValueError, np.searchsorted, a, 0, sorter=[-1, 0, 1, 2, 3])
# assert_raises(ValueError, np.searchsorted, a, 0, sorter=[4, 0, -1, 2, 3])
@xpassIfTorchDynamo # (reason="self.storage_offset() must be divisible by 8")
def test_searchsorted_with_sorter(self):
a = np.random.rand(300)
s = a.argsort()
b = np.sort(a)
k = np.linspace(0, 1, 20)
assert_equal(b.searchsorted(k), a.searchsorted(k, sorter=s))
a = np.array([0, 1, 2, 3, 5] * 20)
s = a.argsort()
k = [0, 1, 2, 3, 5]
expected = [0, 20, 40, 60, 80]
assert_equal(a.searchsorted(k, side="left", sorter=s), expected)
expected = [20, 40, 60, 80, 100]
assert_equal(a.searchsorted(k, side="right", sorter=s), expected)
# Test searching unaligned array
keys = np.arange(10)
a = keys.copy()
np.random.shuffle(s)
s = a.argsort()
aligned = np.empty(a.itemsize * a.size + 1, dtype="uint8")
unaligned = aligned[1:].view(a.dtype)
# Test searching unaligned array
unaligned[:] = a
b = unaligned.searchsorted(keys, "left", s)
assert_equal(b, keys)
b = unaligned.searchsorted(keys, "right", s)
assert_equal(b, keys + 1)
# Test searching for unaligned keys
unaligned[:] = keys
b = a.searchsorted(unaligned, "left", s)
assert_equal(b, keys)
b = a.searchsorted(unaligned, "right", s)
assert_equal(b, keys + 1)
# Test all type specific indirect binary search functions
types = "".join((np.typecodes["AllInteger"], np.typecodes["AllFloat"], "?"))
for dt in types:
if dt == "?":
a = np.array([1, 0], dtype=dt)
# We want the sorter array to be of a type that is different
# from np.intp in all platforms, to check for #4698
s = np.array([1, 0], dtype=np.int16)
out = np.array([1, 0])
else:
a = np.array([3, 4, 1, 2, 0], dtype=dt)
# We want the sorter array to be of a type that is different
# from np.intp in all platforms, to check for #4698
s = np.array([4, 2, 3, 0, 1], dtype=np.int16)
out = np.array([3, 4, 1, 2, 0], dtype=np.intp)
b = a.searchsorted(a, "left", s)
assert_equal(b, out)
b = a.searchsorted(a, "right", s)
assert_equal(b, out + 1)
# Test empty array, use a fresh array to get warnings in
# valgrind if access happens.
e = np.ndarray(shape=0, buffer=b"", dtype=dt)
b = e.searchsorted(a, "left", s[:0])
assert_array_equal(b, np.zeros(len(a), dtype=np.intp))
b = a.searchsorted(e, "left", s)
assert_array_equal(b, np.zeros(0, dtype=np.intp))
# Test non-contiguous sorter array
a = np.array([3, 4, 1, 2, 0])
srt = np.empty((10,), dtype=np.intp)
srt[1::2] = -1
srt[::2] = [4, 2, 3, 0, 1]
s = srt[::2]
out = np.array([3, 4, 1, 2, 0], dtype=np.intp)
b = a.searchsorted(a, "left", s)
assert_equal(b, out)
b = a.searchsorted(a, "right", s)
assert_equal(b, out + 1)
@xpassIfTorchDynamo # (reason="TODO argpartition")
@parametrize("dtype", "efdFDBbhil?")
def test_argpartition_out_of_range(self, dtype):
# Test out of range values in kth raise an error, gh-5469
d = np.arange(10).astype(dtype=dtype)
assert_raises(ValueError, d.argpartition, 10)
assert_raises(ValueError, d.argpartition, -11)
@xpassIfTorchDynamo # (reason="TODO partition")
@parametrize("dtype", "efdFDBbhil?")
def test_partition_out_of_range(self, dtype):
# Test out of range values in kth raise an error, gh-5469
d = np.arange(10).astype(dtype=dtype)
assert_raises(ValueError, d.partition, 10)
assert_raises(ValueError, d.partition, -11)
@xpassIfTorchDynamo # (reason="TODO argpartition")
def test_argpartition_integer(self):
# Test non-integer values in kth raise an error/
d = np.arange(10)
assert_raises(TypeError, d.argpartition, 9.0)
# Test also for generic type argpartition, which uses sorting
# and used to not bound check kth
d_obj = np.arange(10, dtype=object)
assert_raises(TypeError, d_obj.argpartition, 9.0)
@xpassIfTorchDynamo # (reason="TODO partition")
def test_partition_integer(self):
# Test out of range values in kth raise an error, gh-5469
d = np.arange(10)
assert_raises(TypeError, d.partition, 9.0)
# Test also for generic type partition, which uses sorting
# and used to not bound check kth
d_obj = np.arange(10, dtype=object)
assert_raises(TypeError, d_obj.partition, 9.0)
@xpassIfTorchDynamo # (reason="TODO partition")
@parametrize("kth_dtype", "Bbhil")
def test_partition_empty_array(self, kth_dtype):
# check axis handling for multidimensional empty arrays
kth = np.array(0, dtype=kth_dtype)[()]
a = np.array([])
a.shape = (3, 2, 1, 0)
for axis in range(-a.ndim, a.ndim):
msg = f"test empty array partition with axis={axis}"
assert_equal(np.partition(a, kth, axis=axis), a, msg)
msg = "test empty array partition with axis=None"
assert_equal(np.partition(a, kth, axis=None), a.ravel(), msg)
@xpassIfTorchDynamo # (reason="TODO argpartition")
@parametrize("kth_dtype", "Bbhil")
def test_argpartition_empty_array(self, kth_dtype):
# check axis handling for multidimensional empty arrays
kth = np.array(0, dtype=kth_dtype)[()]
a = np.array([])
a.shape = (3, 2, 1, 0)
for axis in range(-a.ndim, a.ndim):
msg = f"test empty array argpartition with axis={axis}"
assert_equal(
np.partition(a, kth, axis=axis), np.zeros_like(a, dtype=np.intp), msg
)
msg = "test empty array argpartition with axis=None"
assert_equal(
np.partition(a, kth, axis=None),
np.zeros_like(a.ravel(), dtype=np.intp),
msg,
)
@xpassIfTorchDynamo # (reason="TODO partition")
def test_partition(self):
d = np.arange(10)
assert_raises(TypeError, np.partition, d, 2, kind=1)
assert_raises(ValueError, np.partition, d, 2, kind="nonsense")
assert_raises(ValueError, np.argpartition, d, 2, kind="nonsense")
assert_raises(ValueError, d.partition, 2, axis=0, kind="nonsense")
assert_raises(ValueError, d.argpartition, 2, axis=0, kind="nonsense")
for k in ("introselect",):
d = np.array([])
assert_array_equal(np.partition(d, 0, kind=k), d)
assert_array_equal(np.argpartition(d, 0, kind=k), d)
d = np.ones(1)
assert_array_equal(np.partition(d, 0, kind=k)[0], d)
assert_array_equal(
d[np.argpartition(d, 0, kind=k)], np.partition(d, 0, kind=k)
)
# kth not modified
kth = np.array([30, 15, 5])
okth = kth.copy()
np.partition(np.arange(40), kth)
assert_array_equal(kth, okth)
for r in ([2, 1], [1, 2], [1, 1]):
d = np.array(r)
tgt = np.sort(d)
assert_array_equal(np.partition(d, 0, kind=k)[0], tgt[0])
assert_array_equal(np.partition(d, 1, kind=k)[1], tgt[1])
assert_array_equal(
d[np.argpartition(d, 0, kind=k)], np.partition(d, 0, kind=k)
)
assert_array_equal(
d[np.argpartition(d, 1, kind=k)], np.partition(d, 1, kind=k)
)
for i in range(d.size):
d[i:].partition(0, kind=k)
assert_array_equal(d, tgt)
for r in (
[3, 2, 1],
[1, 2, 3],
[2, 1, 3],
[2, 3, 1],
[1, 1, 1],
[1, 2, 2],
[2, 2, 1],
[1, 2, 1],
):
d = np.array(r)
tgt = np.sort(d)
assert_array_equal(np.partition(d, 0, kind=k)[0], tgt[0])
assert_array_equal(np.partition(d, 1, kind=k)[1], tgt[1])
assert_array_equal(np.partition(d, 2, kind=k)[2], tgt[2])
assert_array_equal(
d[np.argpartition(d, 0, kind=k)], np.partition(d, 0, kind=k)
)
assert_array_equal(
d[np.argpartition(d, 1, kind=k)], np.partition(d, 1, kind=k)
)
assert_array_equal(
d[np.argpartition(d, 2, kind=k)], np.partition(d, 2, kind=k)
)
for i in range(d.size):
d[i:].partition(0, kind=k)
assert_array_equal(d, tgt)
d = np.ones(50)
assert_array_equal(np.partition(d, 0, kind=k), d)
assert_array_equal(
d[np.argpartition(d, 0, kind=k)], np.partition(d, 0, kind=k)
)
# sorted
d = np.arange(49)
assert_equal(np.partition(d, 5, kind=k)[5], 5)
assert_equal(np.partition(d, 15, kind=k)[15], 15)
assert_array_equal(
d[np.argpartition(d, 5, kind=k)], np.partition(d, 5, kind=k)
)
assert_array_equal(
d[np.argpartition(d, 15, kind=k)], np.partition(d, 15, kind=k)
)
# rsorted
d = np.arange(47)[::-1]
assert_equal(np.partition(d, 6, kind=k)[6], 6)
assert_equal(np.partition(d, 16, kind=k)[16], 16)
assert_array_equal(
d[np.argpartition(d, 6, kind=k)], np.partition(d, 6, kind=k)
)
assert_array_equal(
d[np.argpartition(d, 16, kind=k)], np.partition(d, 16, kind=k)
)
assert_array_equal(np.partition(d, -6, kind=k), np.partition(d, 41, kind=k))
assert_array_equal(
np.partition(d, -16, kind=k), np.partition(d, 31, kind=k)
)
assert_array_equal(
d[np.argpartition(d, -6, kind=k)], np.partition(d, 41, kind=k)
)
# median of 3 killer, O(n^2) on pure median 3 pivot quickselect
# exercises the median of median of 5 code used to keep O(n)
d = np.arange(1000000)
x = np.roll(d, d.size // 2)
mid = x.size // 2 + 1
assert_equal(np.partition(x, mid)[mid], mid)
d = np.arange(1000001)
x = np.roll(d, d.size // 2 + 1)
mid = x.size // 2 + 1
assert_equal(np.partition(x, mid)[mid], mid)
# max
d = np.ones(10)
d[1] = 4
assert_equal(np.partition(d, (2, -1))[-1], 4)
assert_equal(np.partition(d, (2, -1))[2], 1)
assert_equal(d[np.argpartition(d, (2, -1))][-1], 4)
assert_equal(d[np.argpartition(d, (2, -1))][2], 1)
d[1] = np.nan
assert_(np.isnan(d[np.argpartition(d, (2, -1))][-1]))
assert_(np.isnan(np.partition(d, (2, -1))[-1]))
# equal elements
d = np.arange(47) % 7
tgt = np.sort(np.arange(47) % 7)
np.random.shuffle(d)
for i in range(d.size):
assert_equal(np.partition(d, i, kind=k)[i], tgt[i])
assert_array_equal(
d[np.argpartition(d, 6, kind=k)], np.partition(d, 6, kind=k)
)
assert_array_equal(
d[np.argpartition(d, 16, kind=k)], np.partition(d, 16, kind=k)
)
for i in range(d.size):
d[i:].partition(0, kind=k)
assert_array_equal(d, tgt)
d = np.array(
[0, 1, 2, 3, 4, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9]
)
kth = [0, 3, 19, 20]
assert_equal(np.partition(d, kth, kind=k)[kth], (0, 3, 7, 7))
assert_equal(d[np.argpartition(d, kth, kind=k)][kth], (0, 3, 7, 7))
d = np.array([2, 1])
d.partition(0, kind=k)
assert_raises(ValueError, d.partition, 2)
assert_raises(np.AxisError, d.partition, 3, axis=1)
assert_raises(ValueError, np.partition, d, 2)
assert_raises(np.AxisError, np.partition, d, 2, axis=1)
assert_raises(ValueError, d.argpartition, 2)
assert_raises(np.AxisError, d.argpartition, 3, axis=1)
assert_raises(ValueError, np.argpartition, d, 2)
assert_raises(np.AxisError, np.argpartition, d, 2, axis=1)
d = np.arange(10).reshape((2, 5))
d.partition(1, axis=0, kind=k)
d.partition(4, axis=1, kind=k)
np.partition(d, 1, axis=0, kind=k)
np.partition(d, 4, axis=1, kind=k)
np.partition(d, 1, axis=None, kind=k)
np.partition(d, 9, axis=None, kind=k)
d.argpartition(1, axis=0, kind=k)
d.argpartition(4, axis=1, kind=k)
np.argpartition(d, 1, axis=0, kind=k)
np.argpartition(d, 4, axis=1, kind=k)
np.argpartition(d, 1, axis=None, kind=k)
np.argpartition(d, 9, axis=None, kind=k)
assert_raises(ValueError, d.partition, 2, axis=0)
assert_raises(ValueError, d.partition, 11, axis=1)
assert_raises(TypeError, d.partition, 2, axis=None)
assert_raises(ValueError, np.partition, d, 9, axis=1)
assert_raises(ValueError, np.partition, d, 11, axis=None)
assert_raises(ValueError, d.argpartition, 2, axis=0)
assert_raises(ValueError, d.argpartition, 11, axis=1)
assert_raises(ValueError, np.argpartition, d, 9, axis=1)
assert_raises(ValueError, np.argpartition, d, 11, axis=None)
td = [
(dt, s) for dt in [np.int32, np.float32, np.complex64] for s in (9, 16)
]
for dt, s in td:
aae = assert_array_equal
at = assert_
d = np.arange(s, dtype=dt)
np.random.shuffle(d)
d1 = np.tile(np.arange(s, dtype=dt), (4, 1))
map(np.random.shuffle, d1)
d0 = np.transpose(d1)
for i in range(d.size):
p = np.partition(d, i, kind=k)
assert_equal(p[i], i)
# all before are smaller
assert_array_less(p[:i], p[i])
# all after are larger
assert_array_less(p[i], p[i + 1 :])
aae(p, d[np.argpartition(d, i, kind=k)])
p = np.partition(d1, i, axis=1, kind=k)
aae(p[:, i], np.array([i] * d1.shape[0], dtype=dt))
# array_less does not seem to work right
at(
(p[:, :i].T <= p[:, i]).all(),
msg="%d: %r <= %r" % (i, p[:, i], p[:, :i].T),
)
at(
(p[:, i + 1 :].T > p[:, i]).all(),
msg="%d: %r < %r" % (i, p[:, i], p[:, i + 1 :].T),
)
aae(
p,
d1[
np.arange(d1.shape[0])[:, None],
np.argpartition(d1, i, axis=1, kind=k),
],
)
p = np.partition(d0, i, axis=0, kind=k)
aae(p[i, :], np.array([i] * d1.shape[0], dtype=dt))
# array_less does not seem to work right
at(
(p[:i, :] <= p[i, :]).all(),
msg="%d: %r <= %r" % (i, p[i, :], p[:i, :]),
)
at(
(p[i + 1 :, :] > p[i, :]).all(),
msg="%d: %r < %r" % (i, p[i, :], p[:, i + 1 :]),
)
aae(
p,
d0[
np.argpartition(d0, i, axis=0, kind=k),
np.arange(d0.shape[1])[None, :],
],
)
# check inplace
dc = d.copy()
dc.partition(i, kind=k)
assert_equal(dc, np.partition(d, i, kind=k))
dc = d0.copy()
dc.partition(i, axis=0, kind=k)
assert_equal(dc, np.partition(d0, i, axis=0, kind=k))
dc = d1.copy()
dc.partition(i, axis=1, kind=k)
assert_equal(dc, np.partition(d1, i, axis=1, kind=k))
def assert_partitioned(self, d, kth):
prev = 0
for k in np.sort(kth):
assert_array_less(d[prev:k], d[k], err_msg="kth %d" % k)
assert_(
(d[k:] >= d[k]).all(),
msg="kth %d, %r not greater equal %d" % (k, d[k:], d[k]),
)
prev = k + 1
@xpassIfTorchDynamo # (reason="TODO partition")
def test_partition_iterative(self):
d = np.arange(17)
kth = (0, 1, 2, 429, 231)
assert_raises(ValueError, d.partition, kth)
assert_raises(ValueError, d.argpartition, kth)
d = np.arange(10).reshape((2, 5))
assert_raises(ValueError, d.partition, kth, axis=0)
assert_raises(ValueError, d.partition, kth, axis=1)
assert_raises(ValueError, np.partition, d, kth, axis=1)
assert_raises(ValueError, np.partition, d, kth, axis=None)
d = np.array([3, 4, 2, 1])
p = np.partition(d, (0, 3))
self.assert_partitioned(p, (0, 3))
self.assert_partitioned(d[np.argpartition(d, (0, 3))], (0, 3))
assert_array_equal(p, np.partition(d, (-3, -1)))
assert_array_equal(p, d[np.argpartition(d, (-3, -1))])
d = np.arange(17)
np.random.shuffle(d)
d.partition(range(d.size))
assert_array_equal(np.arange(17), d)
np.random.shuffle(d)
assert_array_equal(np.arange(17), d[d.argpartition(range(d.size))])
# test unsorted kth
d = np.arange(17)
np.random.shuffle(d)
keys = np.array([1, 3, 8, -2])
np.random.shuffle(d)
p = np.partition(d, keys)
self.assert_partitioned(p, keys)
p = d[np.argpartition(d, keys)]
self.assert_partitioned(p, keys)
np.random.shuffle(keys)
assert_array_equal(np.partition(d, keys), p)
assert_array_equal(d[np.argpartition(d, keys)], p)
# equal kth
d = np.arange(20)[::-1]
self.assert_partitioned(np.partition(d, [5] * 4), [5])
self.assert_partitioned(np.partition(d, [5] * 4 + [6, 13]), [5] * 4 + [6, 13])
self.assert_partitioned(d[np.argpartition(d, [5] * 4)], [5])
self.assert_partitioned(
d[np.argpartition(d, [5] * 4 + [6, 13])], [5] * 4 + [6, 13]
)
d = np.arange(12)
np.random.shuffle(d)
d1 = np.tile(np.arange(12), (4, 1))
map(np.random.shuffle, d1)
d0 = np.transpose(d1)
kth = (1, 6, 7, -1)
p = np.partition(d1, kth, axis=1)
pa = d1[np.arange(d1.shape[0])[:, None], d1.argpartition(kth, axis=1)]
assert_array_equal(p, pa)
for i in range(d1.shape[0]):
self.assert_partitioned(p[i, :], kth)
p = np.partition(d0, kth, axis=0)
pa = d0[np.argpartition(d0, kth, axis=0), np.arange(d0.shape[1])[None, :]]
assert_array_equal(p, pa)
for i in range(d0.shape[1]):
self.assert_partitioned(p[:, i], kth)
@xpassIfTorchDynamo # (reason="TODO partition")
def test_partition_fuzz(self):
# a few rounds of random data testing
for j in range(10, 30):
for i in range(1, j - 2):
d = np.arange(j)
np.random.shuffle(d)
d = d % np.random.randint(2, 30)
idx = np.random.randint(d.size)
kth = [0, idx, i, i + 1]
tgt = np.sort(d)[kth]
assert_array_equal(
np.partition(d, kth)[kth],
tgt,
err_msg=f"data: {d!r}\n kth: {kth!r}",
)
@xpassIfTorchDynamo # (reason="TODO partition")
@parametrize("kth_dtype", "Bbhil")
def test_argpartition_gh5524(self, kth_dtype):
# A test for functionality of argpartition on lists.
kth = np.array(1, dtype=kth_dtype)[()]
d = [6, 7, 3, 2, 9, 0]
p = np.argpartition(d, kth)
self.assert_partitioned(np.array(d)[p], [1])
@xpassIfTorchDynamo # (reason="TODO order='F'")
def test_flatten(self):
x0 = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
x1 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], np.int32)
y0 = np.array([1, 2, 3, 4, 5, 6], np.int32)
y0f = np.array([1, 4, 2, 5, 3, 6], np.int32)
y1 = np.array([1, 2, 3, 4, 5, 6, 7, 8], np.int32)
y1f = np.array([1, 5, 3, 7, 2, 6, 4, 8], np.int32)
assert_equal(x0.flatten(), y0)
assert_equal(x0.flatten("F"), y0f)
assert_equal(x0.flatten("F"), x0.T.flatten())
assert_equal(x1.flatten(), y1)
assert_equal(x1.flatten("F"), y1f)
assert_equal(x1.flatten("F"), x1.T.flatten())
@parametrize("func", (np.dot, np.matmul))
def test_arr_mult(self, func):
a = np.array([[1, 0], [0, 1]])
b = np.array([[0, 1], [1, 0]])
c = np.array([[9, 1], [1, -9]])
d = np.arange(24).reshape(4, 6)
ddt = np.array(
[
[55, 145, 235, 325],
[145, 451, 757, 1063],
[235, 757, 1279, 1801],
[325, 1063, 1801, 2539],
]
)
dtd = np.array(
[
[504, 540, 576, 612, 648, 684],
[540, 580, 620, 660, 700, 740],
[576, 620, 664, 708, 752, 796],
[612, 660, 708, 756, 804, 852],
[648, 700, 752, 804, 856, 908],
[684, 740, 796, 852, 908, 964],
]
)
# gemm vs syrk optimizations
for et in [np.float32, np.float64, np.complex64, np.complex128]:
eaf = a.astype(et)
assert_equal(func(eaf, eaf), eaf)
assert_equal(func(eaf.T, eaf), eaf)
assert_equal(func(eaf, eaf.T), eaf)
assert_equal(func(eaf.T, eaf.T), eaf)
assert_equal(func(eaf.T.copy(), eaf), eaf)
assert_equal(func(eaf, eaf.T.copy()), eaf)
assert_equal(func(eaf.T.copy(), eaf.T.copy()), eaf)
# syrk validations
for et in [np.float32, np.float64, np.complex64, np.complex128]:
eaf = a.astype(et)
ebf = b.astype(et)
assert_equal(func(ebf, ebf), eaf)
assert_equal(func(ebf.T, ebf), eaf)
assert_equal(func(ebf, ebf.T), eaf)
assert_equal(func(ebf.T, ebf.T), eaf)
# syrk - different shape
for et in [np.float32, np.float64, np.complex64, np.complex128]:
edf = d.astype(et)
eddtf = ddt.astype(et)
edtdf = dtd.astype(et)
assert_equal(func(edf, edf.T), eddtf)
assert_equal(func(edf.T, edf), edtdf)
assert_equal(
func(edf[: edf.shape[0] // 2, :], edf[::2, :].T),
func(edf[: edf.shape[0] // 2, :].copy(), edf[::2, :].T.copy()),
)
assert_equal(
func(edf[::2, :], edf[: edf.shape[0] // 2, :].T),
func(edf[::2, :].copy(), edf[: edf.shape[0] // 2, :].T.copy()),
)
@skip(reason="dot/matmul with negative strides")
@parametrize("func", (np.dot, np.matmul))
def test_arr_mult_2(self, func):
# syrk - different shape, stride, and view validations
for et in [np.float32, np.float64, np.complex64, np.complex128]:
edf = d.astype(et)
assert_equal(
func(edf[::-1, :], edf.T), func(edf[::-1, :].copy(), edf.T.copy())
)
assert_equal(
func(edf[:, ::-1], edf.T), func(edf[:, ::-1].copy(), edf.T.copy())
)
assert_equal(func(edf, edf[::-1, :].T), func(edf, edf[::-1, :].T.copy()))
assert_equal(func(edf, edf[:, ::-1].T), func(edf, edf[:, ::-1].T.copy()))
@parametrize("func", (np.dot, np.matmul))
@parametrize("dtype", "ifdFD")
def test_no_dgemv(self, func, dtype):
# check vector arg for contiguous before gemv
# gh-12156
a = np.arange(8.0, dtype=dtype).reshape(2, 4)
b = np.broadcast_to(1.0, (4, 1))
ret1 = func(a, b)
ret2 = func(a, b.copy())
assert_equal(ret1, ret2)
ret1 = func(b.T, a.T)
ret2 = func(b.T.copy(), a.T)
assert_equal(ret1, ret2)
@skip(reason="__array_interface__")
@parametrize("func", (np.dot, np.matmul))
@parametrize("dtype", "ifdFD")
def test_no_dgemv_2(self, func, dtype):
# check for unaligned data
dt = np.dtype(dtype)
a = np.zeros(8 * dt.itemsize // 2 + 1, dtype="int16")[1:].view(dtype)
a = a.reshape(2, 4)
b = a[0]
# make sure it is not aligned
assert_(a.__array_interface__["data"][0] % dt.itemsize != 0)
ret1 = func(a, b)
ret2 = func(a.copy(), b.copy())
assert_equal(ret1, ret2)
ret1 = func(b.T, a.T)
ret2 = func(b.T.copy(), a.T.copy())
assert_equal(ret1, ret2)
def test_dot(self):
a = np.array([[1, 0], [0, 1]])
b = np.array([[0, 1], [1, 0]])
c = np.array([[9, 1], [1, -9]])
# function versus methods
assert_equal(np.dot(a, b), a.dot(b))
assert_equal(np.dot(np.dot(a, b), c), a.dot(b).dot(c))
# test passing in an output array
c = np.zeros_like(a)
a.dot(b, c)
assert_equal(c, np.dot(a, b))
# test keyword args
c = np.zeros_like(a)
a.dot(b=b, out=c)
assert_equal(c, np.dot(a, b))
@xpassIfTorchDynamo # (reason="_aligned_zeros")
def test_dot_out_mem_overlap(self):
np.random.seed(1)
# Test BLAS and non-BLAS code paths, including all dtypes
# that dot() supports
dtypes = [np.dtype(code) for code in np.typecodes["All"] if code not in "USVM"]
for dtype in dtypes:
a = np.random.rand(3, 3).astype(dtype)
# Valid dot() output arrays must be aligned
b = _aligned_zeros((3, 3), dtype=dtype)
b[...] = np.random.rand(3, 3)
y = np.dot(a, b)
x = np.dot(a, b, out=b)
assert_equal(x, y, err_msg=repr(dtype))
# Check invalid output array
assert_raises(ValueError, np.dot, a, b, out=b[::2])
assert_raises(ValueError, np.dot, a, b, out=b.T)
@xpassIfTorchDynamo # (reason="TODO: overlapping memor in matmul")
def test_matmul_out(self):
# overlapping memory
a = np.arange(18).reshape(2, 3, 3)
b = np.matmul(a, a)
c = np.matmul(a, a, out=a)
assert_(c is a)
assert_equal(c, b)
a = np.arange(18).reshape(2, 3, 3)
c = np.matmul(a, a, out=a[::-1, ...])
assert_(c.base is a.base)
assert_equal(c, b)
def test_diagonal(self):
a = np.arange(12).reshape((3, 4))
assert_equal(a.diagonal(), [0, 5, 10])
assert_equal(a.diagonal(0), [0, 5, 10])
assert_equal(a.diagonal(1), [1, 6, 11])
assert_equal(a.diagonal(-1), [4, 9])
assert_raises(np.AxisError, a.diagonal, axis1=0, axis2=5)
assert_raises(np.AxisError, a.diagonal, axis1=5, axis2=0)
assert_raises(np.AxisError, a.diagonal, axis1=5, axis2=5)
assert_raises((ValueError, RuntimeError), a.diagonal, axis1=1, axis2=1)
b = np.arange(8).reshape((2, 2, 2))
assert_equal(b.diagonal(), [[0, 6], [1, 7]])
assert_equal(b.diagonal(0), [[0, 6], [1, 7]])
assert_equal(b.diagonal(1), [[2], [3]])
assert_equal(b.diagonal(-1), [[4], [5]])
assert_raises((ValueError, RuntimeError), b.diagonal, axis1=0, axis2=0)
assert_equal(b.diagonal(0, 1, 2), [[0, 3], [4, 7]])
assert_equal(b.diagonal(0, 0, 1), [[0, 6], [1, 7]])
assert_equal(b.diagonal(offset=1, axis1=0, axis2=2), [[1], [3]])
# Order of axis argument doesn't matter:
assert_equal(b.diagonal(0, 2, 1), [[0, 3], [4, 7]])
@xfail # (reason="no readonly views")
def test_diagonal_view_notwriteable(self):
a = np.eye(3).diagonal()
assert_(not a.flags.writeable)
assert_(not a.flags.owndata)
a = np.diagonal(np.eye(3))
assert_(not a.flags.writeable)
assert_(not a.flags.owndata)
a = np.diag(np.eye(3))
assert_(not a.flags.writeable)
assert_(not a.flags.owndata)
def test_diagonal_memleak(self):
# Regression test for a bug that crept in at one point
a = np.zeros((100, 100))
if HAS_REFCOUNT:
assert_(sys.getrefcount(a) < 50)
for i in range(100):
a.diagonal()
if HAS_REFCOUNT:
assert_(sys.getrefcount(a) < 50)
def test_size_zero_memleak(self):
# Regression test for issue 9615
# Exercises a special-case code path for dot products of length
# zero in cblasfuncs (making it is specific to floating dtypes).
a = np.array([], dtype=np.float64)
x = np.array(2.0)
for _ in range(100):
np.dot(a, a, out=x)
if HAS_REFCOUNT:
assert_(sys.getrefcount(x) < 50)
def test_trace(self):
a = np.arange(12).reshape((3, 4))
assert_equal(a.trace(), 15)
assert_equal(a.trace(0), 15)
assert_equal(a.trace(1), 18)
assert_equal(a.trace(-1), 13)
b = np.arange(8).reshape((2, 2, 2))
assert_equal(b.trace(), [6, 8])
assert_equal(b.trace(0), [6, 8])
assert_equal(b.trace(1), [2, 3])
assert_equal(b.trace(-1), [4, 5])
assert_equal(b.trace(0, 0, 1), [6, 8])
assert_equal(b.trace(0, 0, 2), [5, 9])
assert_equal(b.trace(0, 1, 2), [3, 11])
assert_equal(b.trace(offset=1, axis1=0, axis2=2), [1, 3])
out = np.array(1)
ret = a.trace(out=out)
assert ret is out
def test_put(self):
icodes = np.typecodes["AllInteger"]
fcodes = np.typecodes["AllFloat"]
for dt in icodes + fcodes:
tgt = np.array([0, 1, 0, 3, 0, 5], dtype=dt)
# test 1-d
a = np.zeros(6, dtype=dt)
a.put([1, 3, 5], [1, 3, 5])
assert_equal(a, tgt)
# test 2-d
a = np.zeros((2, 3), dtype=dt)
a.put([1, 3, 5], [1, 3, 5])
assert_equal(a, tgt.reshape(2, 3))
for dt in "?":
tgt = np.array([False, True, False, True, False, True], dtype=dt)
# test 1-d
a = np.zeros(6, dtype=dt)
a.put([1, 3, 5], [True] * 3)
assert_equal(a, tgt)
# test 2-d
a = np.zeros((2, 3), dtype=dt)
a.put([1, 3, 5], [True] * 3)
assert_equal(a, tgt.reshape(2, 3))
# when calling np.put, make sure a
# TypeError is raised if the object
# isn't an ndarray
bad_array = [1, 2, 3]
assert_raises(TypeError, np.put, bad_array, [0, 2], 5)
@xpassIfTorchDynamo # (reason="TODO: implement order='F'")
def test_ravel(self):
a = np.array([[0, 1], [2, 3]])
assert_equal(a.ravel(), [0, 1, 2, 3])
assert_(not a.ravel().flags.owndata)
assert_equal(a.ravel("F"), [0, 2, 1, 3])
assert_equal(a.ravel(order="C"), [0, 1, 2, 3])
assert_equal(a.ravel(order="F"), [0, 2, 1, 3])
assert_equal(a.ravel(order="A"), [0, 1, 2, 3])
assert_(not a.ravel(order="A").flags.owndata)
assert_equal(a.ravel(order="K"), [0, 1, 2, 3])
assert_(not a.ravel(order="K").flags.owndata)
assert_equal(a.ravel(), a.reshape(-1))
a = np.array([[0, 1], [2, 3]], order="F")
assert_equal(a.ravel(), [0, 1, 2, 3])
assert_equal(a.ravel(order="A"), [0, 2, 1, 3])
assert_equal(a.ravel(order="K"), [0, 2, 1, 3])
assert_(not a.ravel(order="A").flags.owndata)
assert_(not a.ravel(order="K").flags.owndata)
assert_equal(a.ravel(), a.reshape(-1))
assert_equal(a.ravel(order="A"), a.reshape(-1, order="A"))
a = np.array([[0, 1], [2, 3]])[::-1, :]
assert_equal(a.ravel(), [2, 3, 0, 1])
assert_equal(a.ravel(order="C"), [2, 3, 0, 1])
assert_equal(a.ravel(order="F"), [2, 0, 3, 1])
assert_equal(a.ravel(order="A"), [2, 3, 0, 1])
# 'K' doesn't reverse the axes of negative strides
assert_equal(a.ravel(order="K"), [2, 3, 0, 1])
assert_(a.ravel(order="K").flags.owndata)
# Test simple 1-d copy behaviour:
a = np.arange(10)[::2]
assert_(a.ravel("K").flags.owndata)
assert_(a.ravel("C").flags.owndata)
assert_(a.ravel("F").flags.owndata)
# Not contiguous and 1-sized axis with non matching stride
a = np.arange(2**3 * 2)[::2]
a = a.reshape(2, 1, 2, 2).swapaxes(-1, -2)
strides = list(a.strides)
strides[1] = 123
a.strides = strides
assert_(a.ravel(order="K").flags.owndata)
assert_equal(a.ravel("K"), np.arange(0, 15, 2))
# contiguous and 1-sized axis with non matching stride works:
a = np.arange(2**3)
a = a.reshape(2, 1, 2, 2).swapaxes(-1, -2)
strides = list(a.strides)
strides[1] = 123
a.strides = strides
assert_(np.may_share_memory(a.ravel(order="K"), a))
assert_equal(a.ravel(order="K"), np.arange(2**3))
# Test negative strides (not very interesting since non-contiguous):
a = np.arange(4)[::-1].reshape(2, 2)
assert_(a.ravel(order="C").flags.owndata)
assert_(a.ravel(order="K").flags.owndata)
assert_equal(a.ravel("C"), [3, 2, 1, 0])
assert_equal(a.ravel("K"), [3, 2, 1, 0])
# 1-element tidy strides test:
a = np.array([[1]])
a.strides = (123, 432)
# If the following stride is not 8, NPY_RELAXED_STRIDES_DEBUG is
# messing them up on purpose:
if np.ones(1).strides == (8,):
assert_(np.may_share_memory(a.ravel("K"), a))
assert_equal(a.ravel("K").strides, (a.dtype.itemsize,))
for order in ("C", "F", "A", "K"):
# 0-d corner case:
a = np.array(0)
assert_equal(a.ravel(order), [0])
assert_(np.may_share_memory(a.ravel(order), a))
# Test that certain non-inplace ravels work right (mostly) for 'K':
b = np.arange(2**4 * 2)[::2].reshape(2, 2, 2, 2)
a = b[..., ::2]
assert_equal(a.ravel("K"), [0, 4, 8, 12, 16, 20, 24, 28])
assert_equal(a.ravel("C"), [0, 4, 8, 12, 16, 20, 24, 28])
assert_equal(a.ravel("A"), [0, 4, 8, 12, 16, 20, 24, 28])
assert_equal(a.ravel("F"), [0, 16, 8, 24, 4, 20, 12, 28])
a = b[::2, ...]
assert_equal(a.ravel("K"), [0, 2, 4, 6, 8, 10, 12, 14])
assert_equal(a.ravel("C"), [0, 2, 4, 6, 8, 10, 12, 14])
assert_equal(a.ravel("A"), [0, 2, 4, 6, 8, 10, 12, 14])
assert_equal(a.ravel("F"), [0, 8, 4, 12, 2, 10, 6, 14])
@xfailIfTorchDynamo # flags["OWNDATA"]
def test_swapaxes(self):
a = np.arange(1 * 2 * 3 * 4).reshape(1, 2, 3, 4).copy()
idx = np.indices(a.shape)
assert_(a.flags["OWNDATA"])
b = a.copy()
# check exceptions
assert_raises(np.AxisError, a.swapaxes, -5, 0)
assert_raises(np.AxisError, a.swapaxes, 4, 0)
assert_raises(np.AxisError, a.swapaxes, 0, -5)
assert_raises(np.AxisError, a.swapaxes, 0, 4)
for i in range(-4, 4):
for j in range(-4, 4):
for k, src in enumerate((a, b)):
c = src.swapaxes(i, j)
# check shape
shape = list(src.shape)
shape[i] = src.shape[j]
shape[j] = src.shape[i]
assert_equal(c.shape, shape, str((i, j, k)))
# check array contents
i0, i1, i2, i3 = (dim - 1 for dim in c.shape)
j0, j1, j2, j3 = (dim - 1 for dim in src.shape)
assert_equal(
src[idx[j0], idx[j1], idx[j2], idx[j3]],
c[idx[i0], idx[i1], idx[i2], idx[i3]],
str((i, j, k)),
)
# check a view is always returned, gh-5260
assert_(not c.flags["OWNDATA"], str((i, j, k)))
# check on non-contiguous input array
if k == 1:
b = c
def test_conjugate(self):
a = np.array([1 - 1j, 1 + 1j, 23 + 23.0j])
ac = a.conj()
assert_equal(a.real, ac.real)
assert_equal(a.imag, -ac.imag)
assert_equal(ac, a.conjugate())
assert_equal(ac, np.conjugate(a))
a = np.array([1 - 1j, 1 + 1j, 23 + 23.0j], "F")
ac = a.conj()
assert_equal(a.real, ac.real)
assert_equal(a.imag, -ac.imag)
assert_equal(ac, a.conjugate())
assert_equal(ac, np.conjugate(a))
a = np.array([1, 2, 3])
ac = a.conj()
assert_equal(a, ac)
assert_equal(ac, a.conjugate())
assert_equal(ac, np.conjugate(a))
a = np.array([1.0, 2.0, 3.0])
ac = a.conj()
assert_equal(a, ac)
assert_equal(ac, a.conjugate())
assert_equal(ac, np.conjugate(a))
def test_conjugate_out(self):
# Minimal test for the out argument being passed on correctly
# NOTE: The ability to pass `out` is currently undocumented!
a = np.array([1 - 1j, 1 + 1j, 23 + 23.0j])
out = np.empty_like(a)
res = a.conjugate(out)
assert res is out
assert_array_equal(out, a.conjugate())
def test__complex__(self):
dtypes = [
"i1",
"i2",
"i4",
"i8",
"u1",
"f",
"d",
"F",
"D",
"?",
]
for dt in dtypes:
a = np.array(7, dtype=dt)
b = np.array([7], dtype=dt)
c = np.array([[[[[7]]]]], dtype=dt)
msg = f"dtype: {dt}"
ap = complex(a)
assert_equal(ap, a, msg)
bp = complex(b)
assert_equal(bp, b, msg)
cp = complex(c)
assert_equal(cp, c, msg)
def test__complex__should_not_work(self):
dtypes = [
"i1",
"i2",
"i4",
"i8",
"u1",
"f",
"d",
"F",
"D",
"?",
]
for dt in dtypes:
a = np.array([1, 2, 3], dtype=dt)
assert_raises((TypeError, ValueError), complex, a)
c = np.array([(1.0, 3), (2e-3, 7)], dtype=dt)
assert_raises((TypeError, ValueError), complex, c)
class TestCequenceMethods(TestCase):
def test_array_contains(self):
assert_(4.0 in np.arange(16.0).reshape(4, 4))
assert_(20.0 not in np.arange(16.0).reshape(4, 4))
class TestBinop(TestCase):
def test_inplace(self):
# test refcount 1 inplace conversion
assert_array_almost_equal(np.array([0.5]) * np.array([1.0, 2.0]), [0.5, 1.0])
d = np.array([0.5, 0.5])[::2]
assert_array_almost_equal(d * (d * np.array([1.0, 2.0])), [0.25, 0.5])
a = np.array([0.5])
b = np.array([0.5])
c = a + b
c = a - b
c = a * b
c = a / b
assert_equal(a, b)
assert_almost_equal(c, 1.0)
c = a + b * 2.0 / b * a - a / b
assert_equal(a, b)
assert_equal(c, 0.5)
# true divide
a = np.array([5])
b = np.array([3])
c = (a * a) / b
assert_almost_equal(c, 25 / 3, decimal=5)
assert_equal(a, 5)
assert_equal(b, 3)
class TestSubscripting(TestCase):
def test_test_zero_rank(self):
x = np.array([1, 2, 3])
assert_(isinstance(x[0], (np.int_, np.ndarray)))
assert_(type(x[0, ...]) is np.ndarray)
class TestFancyIndexing(TestCase):
def test_list(self):
x = np.ones((1, 1))
x[:, [0]] = 2.0
assert_array_equal(x, np.array([[2.0]]))
x = np.ones((1, 1, 1))
x[:, :, [0]] = 2.0
assert_array_equal(x, np.array([[[2.0]]]))
def test_tuple(self):
x = np.ones((1, 1))
x[:, (0,)] = 2.0
assert_array_equal(x, np.array([[2.0]]))
x = np.ones((1, 1, 1))
x[:, :, (0,)] = 2.0
assert_array_equal(x, np.array([[[2.0]]]))
def test_mask(self):
x = np.array([1, 2, 3, 4])
m = np.array([0, 1, 0, 0], bool)
assert_array_equal(x[m], np.array([2]))
def test_mask2(self):
x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
m = np.array([0, 1], bool)
m2 = np.array([[0, 1, 0, 0], [1, 0, 0, 0]], bool)
m3 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], bool)
assert_array_equal(x[m], np.array([[5, 6, 7, 8]]))
assert_array_equal(x[m2], np.array([2, 5]))
assert_array_equal(x[m3], np.array([2]))
def test_assign_mask(self):
x = np.array([1, 2, 3, 4])
m = np.array([0, 1, 0, 0], bool)
x[m] = 5
assert_array_equal(x, np.array([1, 5, 3, 4]))
def test_assign_mask2(self):
xorig = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
m = np.array([0, 1], bool)
m2 = np.array([[0, 1, 0, 0], [1, 0, 0, 0]], bool)
m3 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], bool)
x = xorig.copy()
x[m] = 10
assert_array_equal(x, np.array([[1, 2, 3, 4], [10, 10, 10, 10]]))
x = xorig.copy()
x[m2] = 10
assert_array_equal(x, np.array([[1, 10, 3, 4], [10, 6, 7, 8]]))
x = xorig.copy()
x[m3] = 10
assert_array_equal(x, np.array([[1, 10, 3, 4], [5, 6, 7, 8]]))
@instantiate_parametrized_tests
class TestArgmaxArgminCommon(TestCase):
sizes = [
(),
(3,),
(3, 2),
(2, 3),
(3, 3),
(2, 3, 4),
(4, 3, 2),
(1, 2, 3, 4),
(2, 3, 4, 1),
(3, 4, 1, 2),
(4, 1, 2, 3),
(64,),
(128,),
(256,),
]
@parametrize(
"size, axis",
list(
itertools.chain(
*[
[
(size, axis)
for axis in list(range(-len(size), len(size))) + [None]
]
for size in sizes
]
)
),
)
@skipif(numpy.__version__ < "1.23", reason="keepdims is new in numpy 1.22")
@parametrize("method", [np.argmax, np.argmin])
def test_np_argmin_argmax_keepdims(self, size, axis, method):
arr = np.random.normal(size=size)
if size is None or size == ():
arr = np.asarray(arr)
# contiguous arrays
if axis is None:
new_shape = [1 for _ in range(len(size))]
else:
new_shape = list(size)
new_shape[axis] = 1
new_shape = tuple(new_shape)
_res_orig = method(arr, axis=axis)
res_orig = _res_orig.reshape(new_shape)
res = method(arr, axis=axis, keepdims=True)
assert_equal(res, res_orig)
assert_(res.shape == new_shape)
outarray = np.empty(res.shape, dtype=res.dtype)
res1 = method(arr, axis=axis, out=outarray, keepdims=True)
assert_(res1 is outarray)
assert_equal(res, outarray)
if len(size) > 0:
wrong_shape = list(new_shape)
if axis is not None:
wrong_shape[axis] = 2
else:
wrong_shape[0] = 2
wrong_outarray = np.empty(wrong_shape, dtype=res.dtype)
with pytest.raises(ValueError):
method(arr.T, axis=axis, out=wrong_outarray, keepdims=True)
# non-contiguous arrays
if axis is None:
new_shape = [1 for _ in range(len(size))]
else:
new_shape = list(size)[::-1]
new_shape[axis] = 1
new_shape = tuple(new_shape)
_res_orig = method(arr.T, axis=axis)
res_orig = _res_orig.reshape(new_shape)
res = method(arr.T, axis=axis, keepdims=True)
assert_equal(res, res_orig)
assert_(res.shape == new_shape)
outarray = np.empty(new_shape[::-1], dtype=res.dtype)
outarray = outarray.T
res1 = method(arr.T, axis=axis, out=outarray, keepdims=True)
assert_(res1 is outarray)
assert_equal(res, outarray)
if len(size) > 0:
# one dimension lesser for non-zero sized
# array should raise an error
with pytest.raises(ValueError):
method(arr[0], axis=axis, out=outarray, keepdims=True)
if len(size) > 0:
wrong_shape = list(new_shape)
if axis is not None:
wrong_shape[axis] = 2
else:
wrong_shape[0] = 2
wrong_outarray = np.empty(wrong_shape, dtype=res.dtype)
with pytest.raises(ValueError):
method(arr.T, axis=axis, out=wrong_outarray, keepdims=True)
@xpassIfTorchDynamo # (reason="TODO: implement choose")
@parametrize("method", ["max", "min"])
def test_all(self, method):
a = np.random.normal(0, 1, (4, 5, 6, 7, 8))
arg_method = getattr(a, "arg" + method)
val_method = getattr(a, method)
for i in range(a.ndim):
a_maxmin = val_method(i)
aarg_maxmin = arg_method(i)
axes = list(range(a.ndim))
axes.remove(i)
assert_(np.all(a_maxmin == aarg_maxmin.choose(*a.transpose(i, *axes))))
@parametrize("method", ["argmax", "argmin"])
def test_output_shape(self, method):
# see also gh-616
a = np.ones((10, 5))
arg_method = getattr(a, method)
# Check some simple shape mismatches
out = np.ones(11, dtype=np.int_)
assert_raises(ValueError, arg_method, -1, out)
out = np.ones((2, 5), dtype=np.int_)
assert_raises(ValueError, arg_method, -1, out)
# these could be relaxed possibly (used to allow even the previous)
out = np.ones((1, 10), dtype=np.int_)
assert_raises(ValueError, arg_method, -1, out)
out = np.ones(10, dtype=np.int_)
arg_method(-1, out=out)
assert_equal(out, arg_method(-1))
@parametrize("ndim", [0, 1])
@parametrize("method", ["argmax", "argmin"])
def test_ret_is_out(self, ndim, method):
a = np.ones((4,) + (256,) * ndim)
arg_method = getattr(a, method)
out = np.empty((256,) * ndim, dtype=np.intp)
ret = arg_method(axis=0, out=out)
assert ret is out
@parametrize(
"arr_method, np_method", [("argmax", np.argmax), ("argmin", np.argmin)]
)
def test_np_vs_ndarray(self, arr_method, np_method):
# make sure both ndarray.argmax/argmin and
# numpy.argmax/argmin support out/axis args
a = np.random.normal(size=(2, 3))
arg_method = getattr(a, arr_method)
# check positional args
out1 = np.zeros(2, dtype=int)
out2 = np.zeros(2, dtype=int)
assert_equal(arg_method(1, out1), np_method(a, 1, out2))
assert_equal(out1, out2)
# check keyword args
out1 = np.zeros(3, dtype=int)
out2 = np.zeros(3, dtype=int)
assert_equal(arg_method(out=out1, axis=0), np_method(a, out=out2, axis=0))
assert_equal(out1, out2)
@instantiate_parametrized_tests
class TestArgmax(TestCase):
usg_data = [
([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 0),
([3, 3, 3, 3, 2, 2, 2, 2], 0),
([0, 1, 2, 3, 4, 5, 6, 7], 7),
([7, 6, 5, 4, 3, 2, 1, 0], 0),
]
sg_data = usg_data + [
([1, 2, 3, 4, -4, -3, -2, -1], 3),
([1, 2, 3, 4, -1, -2, -3, -4], 3),
]
darr = [
(np.array(d[0], dtype=t), d[1])
for d, t in (itertools.product(usg_data, (np.uint8,)))
]
darr += [
(np.array(d[0], dtype=t), d[1])
for d, t in (
itertools.product(
sg_data, (np.int8, np.int16, np.int32, np.int64, np.float32, np.float64)
)
)
]
darr += [
(np.array(d[0], dtype=t), d[1])
for d, t in (
itertools.product(
(
([0, 1, 2, 3, np.nan], 4),
([0, 1, 2, np.nan, 3], 3),
([np.nan, 0, 1, 2, 3], 0),
([np.nan, 0, np.nan, 2, 3], 0),
# To hit the tail of SIMD multi-level(x4, x1) inner loops
# on variant SIMD widthes
([1] * (2 * 5 - 1) + [np.nan], 2 * 5 - 1),
([1] * (4 * 5 - 1) + [np.nan], 4 * 5 - 1),
([1] * (8 * 5 - 1) + [np.nan], 8 * 5 - 1),
([1] * (16 * 5 - 1) + [np.nan], 16 * 5 - 1),
([1] * (32 * 5 - 1) + [np.nan], 32 * 5 - 1),
),
(np.float32, np.float64),
)
)
]
nan_arr = darr + [
# RuntimeError: "max_values_cpu" not implemented for 'ComplexDouble'
# ([0, 1, 2, 3, complex(0, np.nan)], 4),
# ([0, 1, 2, 3, complex(np.nan, 0)], 4),
# ([0, 1, 2, complex(np.nan, 0), 3], 3),
# ([0, 1, 2, complex(0, np.nan), 3], 3),
# ([complex(0, np.nan), 0, 1, 2, 3], 0),
# ([complex(np.nan, np.nan), 0, 1, 2, 3], 0),
# ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, 1)], 0),
# ([complex(np.nan, np.nan), complex(np.nan, 2), complex(np.nan, 1)], 0),
# ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, np.nan)], 0),
# ([complex(0, 0), complex(0, 2), complex(0, 1)], 1),
# ([complex(1, 0), complex(0, 2), complex(0, 1)], 0),
# ([complex(1, 0), complex(0, 2), complex(1, 1)], 2),
([False, False, False, False, True], 4),
([False, False, False, True, False], 3),
([True, False, False, False, False], 0),
([True, False, True, False, False], 0),
]
@parametrize("data", nan_arr)
def test_combinations(self, data):
arr, pos = data
with suppress_warnings() as sup:
sup.filter(RuntimeWarning, "invalid value encountered in reduce")
val = np.max(arr)
assert_equal(np.argmax(arr), pos, err_msg=f"{arr!r}")
assert_equal(arr[np.argmax(arr)], val, err_msg=f"{arr!r}")
# add padding to test SIMD loops
rarr = np.repeat(arr, 129)
rpos = pos * 129
assert_equal(np.argmax(rarr), rpos, err_msg=f"{rarr!r}")
assert_equal(rarr[np.argmax(rarr)], val, err_msg=f"{rarr!r}")
padd = np.repeat(np.min(arr), 513)
rarr = np.concatenate((arr, padd))
rpos = pos
assert_equal(np.argmax(rarr), rpos, err_msg=f"{rarr!r}")
assert_equal(rarr[np.argmax(rarr)], val, err_msg=f"{rarr!r}")
def test_maximum_signed_integers(self):
a = np.array([1, 2**7 - 1, -(2**7)], dtype=np.int8)
assert_equal(np.argmax(a), 1)
a = a.repeat(129)
assert_equal(np.argmax(a), 129)
a = np.array([1, 2**15 - 1, -(2**15)], dtype=np.int16)
assert_equal(np.argmax(a), 1)
a = a.repeat(129)
assert_equal(np.argmax(a), 129)
a = np.array([1, 2**31 - 1, -(2**31)], dtype=np.int32)
assert_equal(np.argmax(a), 1)
a = a.repeat(129)
assert_equal(np.argmax(a), 129)
a = np.array([1, 2**63 - 1, -(2**63)], dtype=np.int64)
assert_equal(np.argmax(a), 1)
a = a.repeat(129)
assert_equal(np.argmax(a), 129)
@instantiate_parametrized_tests
class TestArgmin(TestCase):
usg_data = [
([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 8),
([3, 3, 3, 3, 2, 2, 2, 2], 4),
([0, 1, 2, 3, 4, 5, 6, 7], 0),
([7, 6, 5, 4, 3, 2, 1, 0], 7),
]
sg_data = usg_data + [
([1, 2, 3, 4, -4, -3, -2, -1], 4),
([1, 2, 3, 4, -1, -2, -3, -4], 7),
]
darr = [
(np.array(d[0], dtype=t), d[1])
for d, t in (itertools.product(usg_data, (np.uint8,)))
]
darr += [
(np.array(d[0], dtype=t), d[1])
for d, t in (
itertools.product(
sg_data, (np.int8, np.int16, np.int32, np.int64, np.float32, np.float64)
)
)
]
darr += [
(np.array(d[0], dtype=t), d[1])
for d, t in (
itertools.product(
(
([0, 1, 2, 3, np.nan], 4),
([0, 1, 2, np.nan, 3], 3),
([np.nan, 0, 1, 2, 3], 0),
([np.nan, 0, np.nan, 2, 3], 0),
# To hit the tail of SIMD multi-level(x4, x1) inner loops
# on variant SIMD widthes
([1] * (2 * 5 - 1) + [np.nan], 2 * 5 - 1),
([1] * (4 * 5 - 1) + [np.nan], 4 * 5 - 1),
([1] * (8 * 5 - 1) + [np.nan], 8 * 5 - 1),
([1] * (16 * 5 - 1) + [np.nan], 16 * 5 - 1),
([1] * (32 * 5 - 1) + [np.nan], 32 * 5 - 1),
),
(np.float32, np.float64),
)
)
]
nan_arr = darr + [
# RuntimeError: "min_values_cpu" not implemented for 'ComplexDouble'
# ([0, 1, 2, 3, complex(0, np.nan)], 4),
# ([0, 1, 2, 3, complex(np.nan, 0)], 4),
# ([0, 1, 2, complex(np.nan, 0), 3], 3),
# ([0, 1, 2, complex(0, np.nan), 3], 3),
# ([complex(0, np.nan), 0, 1, 2, 3], 0),
# ([complex(np.nan, np.nan), 0, 1, 2, 3], 0),
# ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, 1)], 0),
# ([complex(np.nan, np.nan), complex(np.nan, 2), complex(np.nan, 1)], 0),
# ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, np.nan)], 0),
# ([complex(0, 0), complex(0, 2), complex(0, 1)], 0),
# ([complex(1, 0), complex(0, 2), complex(0, 1)], 2),
# ([complex(1, 0), complex(0, 2), complex(1, 1)], 1),
([True, True, True, True, False], 4),
([True, True, True, False, True], 3),
([False, True, True, True, True], 0),
([False, True, False, True, True], 0),
]
@parametrize("data", nan_arr)
def test_combinations(self, data):
arr, pos = data
with suppress_warnings() as sup:
sup.filter(RuntimeWarning, "invalid value encountered in reduce")
min_val = np.min(arr)
assert_equal(np.argmin(arr), pos, err_msg=f"{arr!r}")
assert_equal(arr[np.argmin(arr)], min_val, err_msg=f"{arr!r}")
# add padding to test SIMD loops
rarr = np.repeat(arr, 129)
rpos = pos * 129
assert_equal(np.argmin(rarr), rpos, err_msg=f"{rarr!r}")
assert_equal(rarr[np.argmin(rarr)], min_val, err_msg=f"{rarr!r}")
padd = np.repeat(np.max(arr), 513)
rarr = np.concatenate((arr, padd))
rpos = pos
assert_equal(np.argmin(rarr), rpos, err_msg=f"{rarr!r}")
assert_equal(rarr[np.argmin(rarr)], min_val, err_msg=f"{rarr!r}")
def test_minimum_signed_integers(self):
a = np.array([1, -(2**7), -(2**7) + 1, 2**7 - 1], dtype=np.int8)
assert_equal(np.argmin(a), 1)
a = a.repeat(129)
assert_equal(np.argmin(a), 129)
a = np.array([1, -(2**15), -(2**15) + 1, 2**15 - 1], dtype=np.int16)
assert_equal(np.argmin(a), 1)
a = a.repeat(129)
assert_equal(np.argmin(a), 129)
a = np.array([1, -(2**31), -(2**31) + 1, 2**31 - 1], dtype=np.int32)
assert_equal(np.argmin(a), 1)
a = a.repeat(129)
assert_equal(np.argmin(a), 129)
a = np.array([1, -(2**63), -(2**63) + 1, 2**63 - 1], dtype=np.int64)
assert_equal(np.argmin(a), 1)
a = a.repeat(129)
assert_equal(np.argmin(a), 129)
class TestMinMax(TestCase):
@xpassIfTorchDynamo
def test_scalar(self):
assert_raises(np.AxisError, np.amax, 1, 1)
assert_raises(np.AxisError, np.amin, 1, 1)
assert_equal(np.amax(1, axis=0), 1)
assert_equal(np.amin(1, axis=0), 1)
assert_equal(np.amax(1, axis=None), 1)
assert_equal(np.amin(1, axis=None), 1)
def test_axis(self):
assert_raises(np.AxisError, np.amax, [1, 2, 3], 1000)
assert_equal(np.amax([[1, 2, 3]], axis=1), 3)
class TestNewaxis(TestCase):
def test_basic(self):
sk = np.array([0, -0.1, 0.1])
res = 250 * sk[:, np.newaxis]
assert_almost_equal(res.ravel(), 250 * sk)
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
class TestClip(TestCase):
def _check_range(self, x, cmin, cmax):
assert_(np.all(x >= cmin))
assert_(np.all(x <= cmax))
def _clip_type(
self,
type_group,
array_max,
clip_min,
clip_max,
inplace=False,
expected_min=None,
expected_max=None,
):
if expected_min is None:
expected_min = clip_min
if expected_max is None:
expected_max = clip_max
for T in _sctypes[type_group]:
if sys.byteorder == "little":
byte_orders = ["=", ">"]
else:
byte_orders = ["<", "="]
for byteorder in byte_orders:
dtype = np.dtype(T).newbyteorder(byteorder)
x = (np.random.random(1000) * array_max).astype(dtype)
if inplace:
# The tests that call us pass clip_min and clip_max that
# might not fit in the destination dtype. They were written
# assuming the previous unsafe casting, which now must be
# passed explicitly to avoid a warning.
x.clip(clip_min, clip_max, x, casting="unsafe")
else:
x = x.clip(clip_min, clip_max)
byteorder = "="
if x.dtype.byteorder == "|":
byteorder = "|"
assert_equal(x.dtype.byteorder, byteorder)
self._check_range(x, expected_min, expected_max)
return x
@skip(reason="endianness")
def test_basic(self):
for inplace in [False, True]:
self._clip_type("float", 1024, -12.8, 100.2, inplace=inplace)
self._clip_type("float", 1024, 0, 0, inplace=inplace)
self._clip_type("int", 1024, -120, 100, inplace=inplace)
self._clip_type("int", 1024, 0, 0, inplace=inplace)
self._clip_type("uint", 1024, 0, 0, inplace=inplace)
self._clip_type("uint", 1024, -120, 100, inplace=inplace, expected_min=0)
def test_max_or_min(self):
val = np.array([0, 1, 2, 3, 4, 5, 6, 7])
x = val.clip(3)
assert_(np.all(x >= 3))
x = val.clip(min=3)
assert_(np.all(x >= 3))
x = val.clip(max=4)
assert_(np.all(x <= 4))
def test_nan(self):
input_arr = np.array([-2.0, np.nan, 0.5, 3.0, 0.25, np.nan])
result = input_arr.clip(-1, 1)
expected = np.array([-1.0, np.nan, 0.5, 1.0, 0.25, np.nan])
assert_array_equal(result, expected)
@xpassIfTorchDynamo # (reason="TODO")
class TestCompress(TestCase):
def test_axis(self):
tgt = [[5, 6, 7, 8, 9]]
arr = np.arange(10).reshape(2, 5)
out = np.compress([0, 1], arr, axis=0)
assert_equal(out, tgt)
tgt = [[1, 3], [6, 8]]
out = np.compress([0, 1, 0, 1, 0], arr, axis=1)
assert_equal(out, tgt)
def test_truncate(self):
tgt = [[1], [6]]
arr = np.arange(10).reshape(2, 5)
out = np.compress([0, 1], arr, axis=1)
assert_equal(out, tgt)
def test_flatten(self):
arr = np.arange(10).reshape(2, 5)
out = np.compress([0, 1], arr)
assert_equal(out, 1)
@xpassIfTorchDynamo # (reason="TODO")
@instantiate_parametrized_tests
class TestPutmask(TestCase):
def tst_basic(self, x, T, mask, val):
np.putmask(x, mask, val)
assert_equal(x[mask], np.array(val, T))
def test_ip_types(self):
unchecked_types = [bytes, str, np.void]
x = np.random.random(1000) * 100
mask = x < 40
for val in [-100, 0, 15]:
for types in "efdFDBbhil?":
for T in types:
if T not in unchecked_types:
if val < 0 and np.dtype(T).kind == "u":
val = np.iinfo(T).max - 99
self.tst_basic(x.copy().astype(T), T, mask, val)
# Also test string of a length which uses an untypical length
dt = np.dtype("S3")
self.tst_basic(x.astype(dt), dt.type, mask, dt.type(val)[:3])
def test_mask_size(self):
assert_raises(ValueError, np.putmask, np.array([1, 2, 3]), [True], 5)
@parametrize("greater", (True, False))
def test_byteorder(self, greater):
dtype = ">i4" if greater else "<i4"
x = np.array([1, 2, 3], dtype)
np.putmask(x, [True, False, True], -1)
assert_array_equal(x, [-1, 2, -1])
def test_record_array(self):
# Note mixed byteorder.
rec = np.array(
[(-5, 2.0, 3.0), (5.0, 4.0, 3.0)],
dtype=[("x", "<f8"), ("y", ">f8"), ("z", "<f8")],
)
np.putmask(rec["x"], [True, False], 10)
assert_array_equal(rec["x"], [10, 5])
assert_array_equal(rec["y"], [2, 4])
assert_array_equal(rec["z"], [3, 3])
np.putmask(rec["y"], [True, False], 11)
assert_array_equal(rec["x"], [10, 5])
assert_array_equal(rec["y"], [11, 4])
assert_array_equal(rec["z"], [3, 3])
def test_overlaps(self):
# gh-6272 check overlap
x = np.array([True, False, True, False])
np.putmask(x[1:4], [True, True, True], x[:3])
assert_equal(x, np.array([True, True, False, True]))
x = np.array([True, False, True, False])
np.putmask(x[1:4], x[:3], [True, False, True])
assert_equal(x, np.array([True, True, True, True]))
def test_writeable(self):
a = np.arange(5)
a.flags.writeable = False
with pytest.raises(ValueError):
np.putmask(a, a >= 2, 3)
def test_kwargs(self):
x = np.array([0, 0])
np.putmask(x, [0, 1], [-1, -2])
assert_array_equal(x, [0, -2])
x = np.array([0, 0])
np.putmask(x, mask=[0, 1], values=[-1, -2])
assert_array_equal(x, [0, -2])
x = np.array([0, 0])
np.putmask(x, values=[-1, -2], mask=[0, 1])
assert_array_equal(x, [0, -2])
with pytest.raises(TypeError):
np.putmask(a=x, values=[-1, -2], mask=[0, 1])
@instantiate_parametrized_tests
class TestTake(TestCase):
def tst_basic(self, x):
ind = list(range(x.shape[0]))
assert_array_equal(np.take(x, ind, axis=0), x)
def test_ip_types(self):
x = np.random.random(24) * 100
x = np.reshape(x, (2, 3, 4))
for types in "efdFDBbhil?":
for T in types:
self.tst_basic(x.copy().astype(T))
def test_raise(self):
x = np.random.random(24) * 100
x = np.reshape(x, (2, 3, 4))
assert_raises(IndexError, np.take, x, [0, 1, 2], axis=0)
assert_raises(IndexError, np.take, x, [-3], axis=0)
assert_array_equal(np.take(x, [-1], axis=0)[0], x[1])
@xpassIfTorchDynamo # (reason="XXX: take(..., mode='clip')")
def test_clip(self):
x = np.random.random(24) * 100
x = np.reshape(x, (2, 3, 4))
assert_array_equal(np.take(x, [-1], axis=0, mode="clip")[0], x[0])
assert_array_equal(np.take(x, [2], axis=0, mode="clip")[0], x[1])
@xpassIfTorchDynamo # (reason="XXX: take(..., mode='wrap')")
def test_wrap(self):
x = np.random.random(24) * 100
x = np.reshape(x, (2, 3, 4))
assert_array_equal(np.take(x, [-1], axis=0, mode="wrap")[0], x[1])
assert_array_equal(np.take(x, [2], axis=0, mode="wrap")[0], x[0])
assert_array_equal(np.take(x, [3], axis=0, mode="wrap")[0], x[1])
@xpassIfTorchDynamo # (reason="XXX: take(mode='wrap')")
def test_out_overlap(self):
# gh-6272 check overlap on out
x = np.arange(5)
y = np.take(x, [1, 2, 3], out=x[2:5], mode="wrap")
assert_equal(y, np.array([1, 2, 3]))
@parametrize("shape", [(1, 2), (1,), ()])
def test_ret_is_out(self, shape):
# 0d arrays should not be an exception to this rule
x = np.arange(5)
inds = np.zeros(shape, dtype=np.intp)
out = np.zeros(shape, dtype=x.dtype)
ret = np.take(x, inds, out=out)
assert ret is out
@xpassIfTorchDynamo # (reason="TODO")
@instantiate_parametrized_tests
class TestLexsort(TestCase):
@parametrize(
"dtype",
[
np.uint8,
np.int8,
np.int16,
np.int32,
np.int64,
np.float16,
np.float32,
np.float64,
],
)
def test_basic(self, dtype):
a = np.array([1, 2, 1, 3, 1, 5], dtype=dtype)
b = np.array([0, 4, 5, 6, 2, 3], dtype=dtype)
idx = np.lexsort((b, a))
expected_idx = np.array([0, 4, 2, 1, 3, 5])
assert_array_equal(idx, expected_idx)
assert_array_equal(a[idx], np.sort(a))
def test_mixed(self):
a = np.array([1, 2, 1, 3, 1, 5])
b = np.array([0, 4, 5, 6, 2, 3], dtype="datetime64[D]")
idx = np.lexsort((b, a))
expected_idx = np.array([0, 4, 2, 1, 3, 5])
assert_array_equal(idx, expected_idx)
def test_datetime(self):
a = np.array([0, 0, 0], dtype="datetime64[D]")
b = np.array([2, 1, 0], dtype="datetime64[D]")
idx = np.lexsort((b, a))
expected_idx = np.array([2, 1, 0])
assert_array_equal(idx, expected_idx)
a = np.array([0, 0, 0], dtype="timedelta64[D]")
b = np.array([2, 1, 0], dtype="timedelta64[D]")
idx = np.lexsort((b, a))
expected_idx = np.array([2, 1, 0])
assert_array_equal(idx, expected_idx)
def test_object(self): # gh-6312
a = np.random.choice(10, 1000)
b = np.random.choice(["abc", "xy", "wz", "efghi", "qwst", "x"], 1000)
for u in a, b:
left = np.lexsort((u.astype("O"),))
right = np.argsort(u, kind="mergesort")
assert_array_equal(left, right)
for u, v in (a, b), (b, a):
idx = np.lexsort((u, v))
assert_array_equal(idx, np.lexsort((u.astype("O"), v)))
assert_array_equal(idx, np.lexsort((u, v.astype("O"))))
u, v = np.array(u, dtype="object"), np.array(v, dtype="object")
assert_array_equal(idx, np.lexsort((u, v)))
def test_invalid_axis(self): # gh-7528
x = np.linspace(0.0, 1.0, 42 * 3).reshape(42, 3)
assert_raises(np.AxisError, np.lexsort, x, axis=2)
@skip(reason="dont worry about IO")
class TestIO(TestCase):
"""Test tofile, fromfile, tobytes, and fromstring"""
@pytest.fixture()
def x(self):
shape = (2, 4, 3)
rand = np.random.random
x = rand(shape) + rand(shape).astype(complex) * 1j
x[0, :, 1] = [np.nan, np.inf, -np.inf, np.nan]
return x
@pytest.fixture(params=["string", "path_obj"])
def tmp_filename(self, tmp_path, request):
# This fixture covers two cases:
# one where the filename is a string and
# another where it is a pathlib object
filename = tmp_path / "file"
if request.param == "string":
filename = str(filename)
return filename
def test_nofile(self):
# this should probably be supported as a file
# but for now test for proper errors
b = io.BytesIO()
assert_raises(OSError, np.fromfile, b, np.uint8, 80)
d = np.ones(7)
assert_raises(OSError, lambda x: x.tofile(b), d)
def test_bool_fromstring(self):
v = np.array([True, False, True, False], dtype=np.bool_)
y = np.fromstring("1 0 -2.3 0.0", sep=" ", dtype=np.bool_)
assert_array_equal(v, y)
def test_uint64_fromstring(self):
d = np.fromstring(
"9923372036854775807 104783749223640", dtype=np.uint64, sep=" "
)
e = np.array([9923372036854775807, 104783749223640], dtype=np.uint64)
assert_array_equal(d, e)
def test_int64_fromstring(self):
d = np.fromstring("-25041670086757 104783749223640", dtype=np.int64, sep=" ")
e = np.array([-25041670086757, 104783749223640], dtype=np.int64)
assert_array_equal(d, e)
def test_fromstring_count0(self):
d = np.fromstring("1,2", sep=",", dtype=np.int64, count=0)
assert d.shape == (0,)
def test_empty_files_text(self, tmp_filename):
with open(tmp_filename, "w") as f:
pass
y = np.fromfile(tmp_filename)
assert_(y.size == 0, "Array not empty")
def test_empty_files_binary(self, tmp_filename):
with open(tmp_filename, "wb") as f:
pass
y = np.fromfile(tmp_filename, sep=" ")
assert_(y.size == 0, "Array not empty")
def test_roundtrip_file(self, x, tmp_filename):
with open(tmp_filename, "wb") as f:
x.tofile(f)
# NB. doesn't work with flush+seek, due to use of C stdio
with open(tmp_filename, "rb") as f:
y = np.fromfile(f, dtype=x.dtype)
assert_array_equal(y, x.flat)
def test_roundtrip(self, x, tmp_filename):
x.tofile(tmp_filename)
y = np.fromfile(tmp_filename, dtype=x.dtype)
assert_array_equal(y, x.flat)
def test_roundtrip_dump_pathlib(self, x, tmp_filename):
p = Path(tmp_filename)
x.dump(p)
y = np.load(p, allow_pickle=True)
assert_array_equal(y, x)
def test_roundtrip_binary_str(self, x):
s = x.tobytes()
y = np.frombuffer(s, dtype=x.dtype)
assert_array_equal(y, x.flat)
s = x.tobytes("F")
y = np.frombuffer(s, dtype=x.dtype)
assert_array_equal(y, x.flatten("F"))
def test_roundtrip_str(self, x):
x = x.real.ravel()
s = "@".join(map(str, x))
y = np.fromstring(s, sep="@")
# NB. str imbues less precision
nan_mask = ~np.isfinite(x)
assert_array_equal(x[nan_mask], y[nan_mask])
assert_array_almost_equal(x[~nan_mask], y[~nan_mask], decimal=5)
def test_roundtrip_repr(self, x):
x = x.real.ravel()
s = "@".join(map(repr, x))
y = np.fromstring(s, sep="@")
assert_array_equal(x, y)
def test_unseekable_fromfile(self, x, tmp_filename):
# gh-6246
x.tofile(tmp_filename)
def fail(*args, **kwargs):
raise OSError("Can not tell or seek")
with open(tmp_filename, "rb", buffering=0) as f:
f.seek = fail
f.tell = fail
assert_raises(OSError, np.fromfile, f, dtype=x.dtype)
def test_io_open_unbuffered_fromfile(self, x, tmp_filename):
# gh-6632
x.tofile(tmp_filename)
with open(tmp_filename, "rb", buffering=0) as f:
y = np.fromfile(f, dtype=x.dtype)
assert_array_equal(y, x.flat)
def test_largish_file(self, tmp_filename):
# check the fallocate path on files > 16MB
d = np.zeros(4 * 1024**2)
d.tofile(tmp_filename)
assert_equal(os.path.getsize(tmp_filename), d.nbytes)
assert_array_equal(d, np.fromfile(tmp_filename))
# check offset
with open(tmp_filename, "r+b") as f:
f.seek(d.nbytes)
d.tofile(f)
assert_equal(os.path.getsize(tmp_filename), d.nbytes * 2)
# check append mode (gh-8329)
open(tmp_filename, "w").close() # delete file contents
with open(tmp_filename, "ab") as f:
d.tofile(f)
assert_array_equal(d, np.fromfile(tmp_filename))
with open(tmp_filename, "ab") as f:
d.tofile(f)
assert_equal(os.path.getsize(tmp_filename), d.nbytes * 2)
def test_io_open_buffered_fromfile(self, x, tmp_filename):
# gh-6632
x.tofile(tmp_filename)
with open(tmp_filename, "rb", buffering=-1) as f:
y = np.fromfile(f, dtype=x.dtype)
assert_array_equal(y, x.flat)
def test_file_position_after_fromfile(self, tmp_filename):
# gh-4118
sizes = [
io.DEFAULT_BUFFER_SIZE // 8,
io.DEFAULT_BUFFER_SIZE,
io.DEFAULT_BUFFER_SIZE * 8,
]
for size in sizes:
with open(tmp_filename, "wb") as f:
f.seek(size - 1)
f.write(b"\0")
for mode in ["rb", "r+b"]:
err_msg = "%d %s" % (size, mode)
with open(tmp_filename, mode) as f:
f.read(2)
np.fromfile(f, dtype=np.float64, count=1)
pos = f.tell()
assert_equal(pos, 10, err_msg=err_msg)
def test_file_position_after_tofile(self, tmp_filename):
# gh-4118
sizes = [
io.DEFAULT_BUFFER_SIZE // 8,
io.DEFAULT_BUFFER_SIZE,
io.DEFAULT_BUFFER_SIZE * 8,
]
for size in sizes:
err_msg = "%d" % (size,)
with open(tmp_filename, "wb") as f:
f.seek(size - 1)
f.write(b"\0")
f.seek(10)
f.write(b"12")
np.array([0], dtype=np.float64).tofile(f)
pos = f.tell()
assert_equal(pos, 10 + 2 + 8, err_msg=err_msg)
with open(tmp_filename, "r+b") as f:
f.read(2)
f.seek(0, 1) # seek between read&write required by ANSI C
np.array([0], dtype=np.float64).tofile(f)
pos = f.tell()
assert_equal(pos, 10, err_msg=err_msg)
def test_load_object_array_fromfile(self, tmp_filename):
# gh-12300
with open(tmp_filename, "w") as f:
# Ensure we have a file with consistent contents
pass
with open(tmp_filename, "rb") as f:
assert_raises_regex(
ValueError,
"Cannot read into object array",
np.fromfile,
f,
dtype=object,
)
assert_raises_regex(
ValueError,
"Cannot read into object array",
np.fromfile,
tmp_filename,
dtype=object,
)
def test_fromfile_offset(self, x, tmp_filename):
with open(tmp_filename, "wb") as f:
x.tofile(f)
with open(tmp_filename, "rb") as f:
y = np.fromfile(f, dtype=x.dtype, offset=0)
assert_array_equal(y, x.flat)
with open(tmp_filename, "rb") as f:
count_items = len(x.flat) // 8
offset_items = len(x.flat) // 4
offset_bytes = x.dtype.itemsize * offset_items
y = np.fromfile(f, dtype=x.dtype, count=count_items, offset=offset_bytes)
assert_array_equal(y, x.flat[offset_items : offset_items + count_items])
# subsequent seeks should stack
offset_bytes = x.dtype.itemsize
z = np.fromfile(f, dtype=x.dtype, offset=offset_bytes)
assert_array_equal(z, x.flat[offset_items + count_items + 1 :])
with open(tmp_filename, "wb") as f:
x.tofile(f, sep=",")
with open(tmp_filename, "rb") as f:
assert_raises_regex(
TypeError,
"'offset' argument only permitted for binary files",
np.fromfile,
tmp_filename,
dtype=x.dtype,
sep=",",
offset=1,
)
@skipif(IS_PYPY, reason="bug in PyPy's PyNumber_AsSsize_t")
def test_fromfile_bad_dup(self, x, tmp_filename):
def dup_str(fd):
return "abc"
def dup_bigint(fd):
return 2**68
old_dup = os.dup
try:
with open(tmp_filename, "wb") as f:
x.tofile(f)
for dup, exc in ((dup_str, TypeError), (dup_bigint, OSError)):
os.dup = dup
assert_raises(exc, np.fromfile, f)
finally:
os.dup = old_dup
def _check_from(self, s, value, filename, **kw):
if "sep" not in kw:
y = np.frombuffer(s, **kw)
else:
y = np.fromstring(s, **kw)
assert_array_equal(y, value)
with open(filename, "wb") as f:
f.write(s)
y = np.fromfile(filename, **kw)
assert_array_equal(y, value)
@pytest.fixture(params=["period", "comma"])
def decimal_sep_localization(self, request):
"""
Including this fixture in a test will automatically
execute it with both types of decimal separator.
So::
def test_decimal(decimal_sep_localization):
pass
is equivalent to the following two tests::
def test_decimal_period_separator():
pass
def test_decimal_comma_separator():
with CommaDecimalPointLocale():
pass
"""
if request.param == "period":
yield
elif request.param == "comma":
with CommaDecimalPointLocale():
yield
else:
raise AssertionError(request.param)
def test_nan(self, tmp_filename, decimal_sep_localization):
self._check_from(
b"nan +nan -nan NaN nan(foo) +NaN(BAR) -NAN(q_u_u_x_)",
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
tmp_filename,
sep=" ",
)
def test_inf(self, tmp_filename, decimal_sep_localization):
self._check_from(
b"inf +inf -inf infinity -Infinity iNfInItY -inF",
[np.inf, np.inf, -np.inf, np.inf, -np.inf, np.inf, -np.inf],
tmp_filename,
sep=" ",
)
def test_numbers(self, tmp_filename, decimal_sep_localization):
self._check_from(
b"1.234 -1.234 .3 .3e55 -123133.1231e+133",
[1.234, -1.234, 0.3, 0.3e55, -123133.1231e133],
tmp_filename,
sep=" ",
)
def test_binary(self, tmp_filename):
self._check_from(
b"\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@",
np.array([1, 2, 3, 4]),
tmp_filename,
dtype="<f4",
)
@slow # takes > 1 minute on mechanical hard drive
def test_big_binary(self):
"""Test workarounds for 32-bit limit for MSVC fwrite, fseek, and ftell
These normally would hang doing something like this.
See : https://github.com/numpy/numpy/issues/2256
"""
if sys.platform != "win32" or "[GCC " in sys.version:
return
try:
# before workarounds, only up to 2**32-1 worked
fourgbplus = 2**32 + 2**16
testbytes = np.arange(8, dtype=np.int8)
n = len(testbytes)
flike = tempfile.NamedTemporaryFile()
f = flike.file
np.tile(testbytes, fourgbplus // testbytes.nbytes).tofile(f)
flike.seek(0)
a = np.fromfile(f, dtype=np.int8)
flike.close()
assert_(len(a) == fourgbplus)
# check only start and end for speed:
assert_((a[:n] == testbytes).all())
assert_((a[-n:] == testbytes).all())
except (MemoryError, ValueError):
pass
def test_string(self, tmp_filename):
self._check_from(b"1,2,3,4", [1.0, 2.0, 3.0, 4.0], tmp_filename, sep=",")
def test_counted_string(self, tmp_filename, decimal_sep_localization):
self._check_from(
b"1,2,3,4", [1.0, 2.0, 3.0, 4.0], tmp_filename, count=4, sep=","
)
self._check_from(b"1,2,3,4", [1.0, 2.0, 3.0], tmp_filename, count=3, sep=",")
self._check_from(
b"1,2,3,4", [1.0, 2.0, 3.0, 4.0], tmp_filename, count=-1, sep=","
)
def test_string_with_ws(self, tmp_filename):
self._check_from(
b"1 2 3 4 ", [1, 2, 3, 4], tmp_filename, dtype=int, sep=" "
)
def test_counted_string_with_ws(self, tmp_filename):
self._check_from(
b"1 2 3 4 ", [1, 2, 3], tmp_filename, count=3, dtype=int, sep=" "
)
def test_ascii(self, tmp_filename, decimal_sep_localization):
self._check_from(b"1 , 2 , 3 , 4", [1.0, 2.0, 3.0, 4.0], tmp_filename, sep=",")
self._check_from(
b"1,2,3,4", [1.0, 2.0, 3.0, 4.0], tmp_filename, dtype=float, sep=","
)
def test_malformed(self, tmp_filename, decimal_sep_localization):
with assert_warns(DeprecationWarning):
self._check_from(b"1.234 1,234", [1.234, 1.0], tmp_filename, sep=" ")
def test_long_sep(self, tmp_filename):
self._check_from(b"1_x_3_x_4_x_5", [1, 3, 4, 5], tmp_filename, sep="_x_")
def test_dtype(self, tmp_filename):
v = np.array([1, 2, 3, 4], dtype=np.int_)
self._check_from(b"1,2,3,4", v, tmp_filename, sep=",", dtype=np.int_)
def test_dtype_bool(self, tmp_filename):
# can't use _check_from because fromstring can't handle True/False
v = np.array([True, False, True, False], dtype=np.bool_)
s = b"1,0,-2.3,0"
with open(tmp_filename, "wb") as f:
f.write(s)
y = np.fromfile(tmp_filename, sep=",", dtype=np.bool_)
assert_(y.dtype == "?")
assert_array_equal(y, v)
def test_tofile_sep(self, tmp_filename, decimal_sep_localization):
x = np.array([1.51, 2, 3.51, 4], dtype=float)
with open(tmp_filename, "w") as f:
x.tofile(f, sep=",")
with open(tmp_filename) as f:
s = f.read()
# assert_equal(s, '1.51,2.0,3.51,4.0')
y = np.array([float(p) for p in s.split(",")])
assert_array_equal(x, y)
def test_tofile_format(self, tmp_filename, decimal_sep_localization):
x = np.array([1.51, 2, 3.51, 4], dtype=float)
with open(tmp_filename, "w") as f:
x.tofile(f, sep=",", format="%.2f")
with open(tmp_filename) as f:
s = f.read()
assert_equal(s, "1.51,2.00,3.51,4.00")
def test_tofile_cleanup(self, tmp_filename):
x = np.zeros((10), dtype=object)
with open(tmp_filename, "wb") as f:
assert_raises(OSError, lambda: x.tofile(f, sep=""))
# Dup-ed file handle should be closed or remove will fail on Windows OS
os.remove(tmp_filename)
# Also make sure that we close the Python handle
assert_raises(OSError, lambda: x.tofile(tmp_filename))
os.remove(tmp_filename)
def test_fromfile_subarray_binary(self, tmp_filename):
# Test subarray dtypes which are absorbed into the shape
x = np.arange(24, dtype="i4").reshape(2, 3, 4)
x.tofile(tmp_filename)
res = np.fromfile(tmp_filename, dtype="(3,4)i4")
assert_array_equal(x, res)
x_str = x.tobytes()
with assert_warns(DeprecationWarning):
# binary fromstring is deprecated
res = np.fromstring(x_str, dtype="(3,4)i4")
assert_array_equal(x, res)
def test_parsing_subarray_unsupported(self, tmp_filename):
# We currently do not support parsing subarray dtypes
data = "12,42,13," * 50
with pytest.raises(ValueError):
expected = np.fromstring(data, dtype="(3,)i", sep=",")
with open(tmp_filename, "w") as f:
f.write(data)
with pytest.raises(ValueError):
np.fromfile(tmp_filename, dtype="(3,)i", sep=",")
def test_read_shorter_than_count_subarray(self, tmp_filename):
# Test that requesting more values does not cause any problems
# in conjunction with subarray dimensions being absorbed into the
# array dimension.
expected = np.arange(511 * 10, dtype="i").reshape(-1, 10)
binary = expected.tobytes()
with pytest.raises(ValueError):
with pytest.warns(DeprecationWarning):
np.fromstring(binary, dtype="(10,)i", count=10000)
expected.tofile(tmp_filename)
res = np.fromfile(tmp_filename, dtype="(10,)i", count=10000)
assert_array_equal(res, expected)
@xpassIfTorchDynamo # (reason="TODO")
@instantiate_parametrized_tests
class TestFromBuffer(TestCase):
@parametrize(
"byteorder", [subtest("little", name="little"), subtest("big", name="big")]
)
@parametrize("dtype", [float, int, complex])
def test_basic(self, byteorder, dtype):
dt = np.dtype(dtype).newbyteorder(byteorder)
x = (np.random.random((4, 7)) * 5).astype(dt)
buf = x.tobytes()
assert_array_equal(np.frombuffer(buf, dtype=dt), x.flat)
# @xpassIfTorchDynamo
@parametrize(
"obj", [np.arange(10), subtest("12345678", decorators=[xfailIfTorchDynamo])]
)
def test_array_base(self, obj):
# Objects (including NumPy arrays), which do not use the
# `release_buffer` slot should be directly used as a base object.
# See also gh-21612
if isinstance(obj, str):
# @parametrize breaks with bytes objects
obj = bytes(obj, enconding="latin-1")
new = np.frombuffer(obj)
assert new.base is obj
def test_empty(self):
assert_array_equal(np.frombuffer(b""), np.array([]))
@skip("fails on CI, we are unlikely to implement this")
@skipif(
IS_PYPY,
reason="PyPy's memoryview currently does not track exports. See: "
"https://foss.heptapod.net/pypy/pypy/-/issues/3724",
)
def test_mmap_close(self):
# The old buffer protocol was not safe for some things that the new
# one is. But `frombuffer` always used the old one for a long time.
# Checks that it is safe with the new one (using memoryviews)
with tempfile.TemporaryFile(mode="wb") as tmp:
tmp.write(b"asdf")
tmp.flush()
mm = mmap.mmap(tmp.fileno(), 0)
arr = np.frombuffer(mm, dtype=np.uint8)
with pytest.raises(BufferError):
mm.close() # cannot close while array uses the buffer
del arr
mm.close()
@skip # (reason="TODO") # FIXME: skip -> xfail (a0.shape = (4, 5) raises)
class TestFlat(TestCase):
def setUp(self):
a0 = np.arange(20.0)
a = a0.reshape(4, 5)
a0.shape = (4, 5)
a.flags.writeable = False
self.a = a
self.b = a[::2, ::2]
self.a0 = a0
self.b0 = a0[::2, ::2]
def test_contiguous(self):
testpassed = False
try:
self.a.flat[12] = 100.0
except ValueError:
testpassed = True
assert_(testpassed)
assert_(self.a.flat[12] == 12.0)
def test_discontiguous(self):
testpassed = False
try:
self.b.flat[4] = 100.0
except ValueError:
testpassed = True
assert_(testpassed)
assert_(self.b.flat[4] == 12.0)
def test___array__(self):
c = self.a.flat.__array__()
d = self.b.flat.__array__()
e = self.a0.flat.__array__()
f = self.b0.flat.__array__()
assert_(c.flags.writeable is False)
assert_(d.flags.writeable is False)
assert_(e.flags.writeable is True)
assert_(f.flags.writeable is False)
assert_(c.flags.writebackifcopy is False)
assert_(d.flags.writebackifcopy is False)
assert_(e.flags.writebackifcopy is False)
assert_(f.flags.writebackifcopy is False)
@skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
def test_refcount(self):
# includes regression test for reference count error gh-13165
inds = [np.intp(0), np.array([True] * self.a.size), np.array([0]), None]
indtype = np.dtype(np.intp)
rc_indtype = sys.getrefcount(indtype)
for ind in inds:
rc_ind = sys.getrefcount(ind)
for _ in range(100):
try:
self.a.flat[ind]
except IndexError:
pass
assert_(abs(sys.getrefcount(ind) - rc_ind) < 50)
assert_(abs(sys.getrefcount(indtype) - rc_indtype) < 50)
def test_index_getset(self):
it = np.arange(10).reshape(2, 1, 5).flat
with pytest.raises(AttributeError):
it.index = 10
for _ in it:
pass
# Check the value of `.index` is updated correctly (see also gh-19153)
# If the type was incorrect, this would show up on big-endian machines
assert it.index == it.base.size
class TestResize(TestCase):
@_no_tracing
def test_basic(self):
x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
if IS_PYPY:
x.resize((5, 5), refcheck=False)
else:
x.resize((5, 5))
assert_array_equal(
x.ravel()[:9], np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).ravel()
)
assert_array_equal(x[9:].ravel(), 0)
@skip(reason="how to find if someone is refencing an array")
def test_check_reference(self):
x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
y = x
assert_raises(ValueError, x.resize, (5, 1))
del y # avoid pyflakes unused variable warning.
@_no_tracing
def test_int_shape(self):
x = np.eye(3)
if IS_PYPY:
x.resize(3, refcheck=False)
else:
x.resize(3)
assert_array_equal(x, np.eye(3)[0, :])
def test_none_shape(self):
x = np.eye(3)
x.resize(None)
assert_array_equal(x, np.eye(3))
x.resize()
assert_array_equal(x, np.eye(3))
def test_0d_shape(self):
# to it multiple times to test it does not break alloc cache gh-9216
for i in range(10):
x = np.empty((1,))
x.resize(())
assert_equal(x.shape, ())
assert_equal(x.size, 1)
x = np.empty(())
x.resize((1,))
assert_equal(x.shape, (1,))
assert_equal(x.size, 1)
def test_invalid_arguments(self):
assert_raises(TypeError, np.eye(3).resize, "hi")
assert_raises(ValueError, np.eye(3).resize, -1)
assert_raises(TypeError, np.eye(3).resize, order=1)
assert_raises((NotImplementedError, TypeError), np.eye(3).resize, refcheck="hi")
@_no_tracing
def test_freeform_shape(self):
x = np.eye(3)
if IS_PYPY:
x.resize(3, 2, 1, refcheck=False)
else:
x.resize(3, 2, 1)
assert_(x.shape == (3, 2, 1))
@_no_tracing
def test_zeros_appended(self):
x = np.eye(3)
if IS_PYPY:
x.resize(2, 3, 3, refcheck=False)
else:
x.resize(2, 3, 3)
assert_array_equal(x[0], np.eye(3))
assert_array_equal(x[1], np.zeros((3, 3)))
def test_empty_view(self):
# check that sizes containing a zero don't trigger a reallocate for
# already empty arrays
x = np.zeros((10, 0), int)
x_view = x[...]
x_view.resize((0, 10))
x_view.resize((0, 100))
@skip(reason="ignore weakrefs for ndarray.resize")
def test_check_weakref(self):
x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
xref = weakref.ref(x)
assert_raises(ValueError, x.resize, (5, 1))
del xref # avoid pyflakes unused variable warning.
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_writeable
|
def test_writeable(self):
mydict = locals()
self.a.flags.writeable = False
assert_raises(ValueError, runstring, "self.a[0] = 3", mydict)
assert_raises(ValueError, runstring, "self.a[0:1].itemset(3)", mydict)
self.a.flags.writeable = True
self.a[0] = 5
self.a[0] = 0
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
@xpassIfTorchDynamo # (reason="TODO: flags")
@instantiate_parametrized_tests
class TestFlag(TestCase):
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_writeable_any_base
|
def test_writeable_any_base(self):
# Ensure that any base being writeable is sufficient to change flag;
# this is especially interesting for arrays from an array interface.
arr = np.arange(10)
class subclass(np.ndarray):
pass
# Create subclass so base will not be collapsed, this is OK to change
view1 = arr.view(subclass)
view2 = view1[...]
arr.flags.writeable = False
view2.flags.writeable = False
view2.flags.writeable = True # Can be set to True again.
arr = np.arange(10)
class frominterface:
def __init__(self, arr):
self.arr = arr
self.__array_interface__ = arr.__array_interface__
view1 = np.asarray(frominterface)
view2 = view1[...]
view2.flags.writeable = False
view2.flags.writeable = True
view1.flags.writeable = False
view2.flags.writeable = False
with assert_raises(ValueError):
# Must assume not writeable, since only base is not:
view2.flags.writeable = True
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
@xpassIfTorchDynamo # (reason="TODO: flags")
@instantiate_parametrized_tests
class TestFlag(TestCase):
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
__init__
|
def __init__(self, arr):
self.arr = arr
self.__array_interface__ = arr.__array_interface__
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
_sctypes = {
"int": [np.int8, np.int16, np.int32, np.int64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64],
"float": [np.float32, np.float64],
"complex": [np.complex64, np.complex128]
# no complex256 in torch._numpy
+ ([np.clongdouble] if hasattr(np, "clongdouble") else []),
}
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
NEIGH_MODE = {"zero": 0, "one": 1, "constant": 2, "circular": 3, "mirror": 4}
from numpy.core._internal import _dtype_from_pep3118
class Foo:
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_writeable_from_readonly
|
def test_writeable_from_readonly(self):
# gh-9440 - make sure fromstring, from buffer on readonly buffers
# set writeable False
data = b"\x00" * 100
vals = np.frombuffer(data, "B")
assert_raises(ValueError, vals.setflags, write=True)
types = np.dtype([("vals", "u1"), ("res3", "S4")])
values = np.core.records.fromstring(data, types)
vals = values["vals"]
assert_raises(ValueError, vals.setflags, write=True)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
@xpassIfTorchDynamo # (reason="TODO: flags")
@instantiate_parametrized_tests
class TestFlag(TestCase):
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_writeable_pickle
|
def test_writeable_pickle(self):
import pickle
# Small arrays will be copied without setting base.
# See condition for using PyArray_SetBaseObject in
# array_setstate.
a = np.arange(1000)
for v in range(pickle.HIGHEST_PROTOCOL):
vals = pickle.loads(pickle.dumps(a, v))
assert_(vals.flags.writeable)
assert_(isinstance(vals.base, bytes))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
@xpassIfTorchDynamo # (reason="TODO: flags")
@instantiate_parametrized_tests
class TestFlag(TestCase):
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_array_object
|
def test_array_object(self):
d = np.ones((6,))
r = np.array([[d, d + 1], d + 2], dtype=object)
assert_equal(len(r), 2)
assert_equal(r[0], [d, d + 1])
assert_equal(r[1], d + 2)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@instantiate_parametrized_tests
class TestArrayConstruction(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_array_empty
|
def test_array_empty(self):
assert_raises(TypeError, np.array)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@instantiate_parametrized_tests
class TestArrayConstruction(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_0d_array_shape
|
def test_0d_array_shape(self):
assert np.ones(np.array(3)).shape == (3,)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@instantiate_parametrized_tests
class TestArrayConstruction(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_array_copy_false_2
|
def test_array_copy_false_2(self):
d = np.array([1, 2, 3])
e = np.array(d, copy=False, order="F")
d[1] = 4
assert_array_equal(e, [1, 4, 3])
e[2] = 7
assert_array_equal(d, [1, 4, 7])
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@instantiate_parametrized_tests
class TestArrayConstruction(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_array_as_keyword
|
def test_array_as_keyword(self, func):
# This should likely be made positional only, but do not change
# the name accidentally.
if func is np.array:
func(object=3)
else:
func(a=3)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@instantiate_parametrized_tests
class TestArrayConstruction(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_stridesattr
|
def test_stridesattr(self):
x = self.one
def make_array(size, offset, strides):
return np.ndarray(
size,
buffer=x,
dtype=int,
offset=offset * x.itemsize,
strides=strides * x.itemsize,
)
assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
assert_raises(ValueError, make_array, 4, 4, -2)
assert_raises(ValueError, make_array, 4, 2, -1)
assert_raises(ValueError, make_array, 8, 3, 1)
assert_equal(make_array(8, 3, 0), np.array([3] * 8))
# Check behavior reported in gh-2503:
assert_raises(ValueError, make_array, (2, 3), 5, np.array([-2, -3]))
make_array(0, 0, 10)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestAttributes(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
make_array
|
def make_array(size, offset, strides):
return np.ndarray(
size,
buffer=x,
dtype=int,
offset=offset * x.itemsize,
strides=strides * x.itemsize,
)
assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
assert_raises(ValueError, make_array, 4, 4, -2)
assert_raises(ValueError, make_array, 4, 2, -1)
assert_raises(ValueError, make_array, 8, 3, 1)
assert_equal(make_array(8, 3, 0), np.array([3] * 8))
# Check behavior reported in gh-2503:
assert_raises(ValueError, make_array, (2, 3), 5, np.array([-2, -3]))
make_array(0, 0, 10)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_set_stridesattr
|
def test_set_stridesattr(self):
x = self.one
def make_array(size, offset, strides):
try:
r = np.ndarray([size], dtype=int, buffer=x, offset=offset * x.itemsize)
except Exception as e:
raise RuntimeError(e) # noqa: B904
r.strides = strides = strides * x.itemsize
return r
assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
assert_equal(make_array(7, 3, 1), np.array([3, 4, 5, 6, 7, 8, 9]))
assert_raises(ValueError, make_array, 4, 4, -2)
assert_raises(ValueError, make_array, 4, 2, -1)
assert_raises(RuntimeError, make_array, 8, 3, 1)
# Check that the true extent of the array is used.
# Test relies on as_strided base not exposing a buffer.
x = np.lib.stride_tricks.as_strided(np.arange(1), (10, 10), (0, 0))
def set_strides(arr, strides):
arr.strides = strides
assert_raises(ValueError, set_strides, x, (10 * x.itemsize, x.itemsize))
# Test for offset calculations:
x = np.lib.stride_tricks.as_strided(
np.arange(10, dtype=np.int8)[-1], shape=(10,), strides=(-1,)
)
assert_raises(ValueError, set_strides, x[::-1], -1)
a = x[::-1]
a.strides = 1
a[::2].strides = 2
# test 0d
arr_0d = np.array(0)
arr_0d.strides = ()
assert_raises(TypeError, set_strides, arr_0d, None)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestAttributes(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
make_array
|
def make_array(size, offset, strides):
return np.ndarray(
size,
buffer=x,
dtype=int,
offset=offset * x.itemsize,
strides=strides * x.itemsize,
)
assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
assert_raises(ValueError, make_array, 4, 4, -2)
assert_raises(ValueError, make_array, 4, 2, -1)
assert_raises(ValueError, make_array, 8, 3, 1)
assert_equal(make_array(8, 3, 0), np.array([3] * 8))
# Check behavior reported in gh-2503:
assert_raises(ValueError, make_array, (2, 3), 5, np.array([-2, -3]))
make_array(0, 0, 10)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
set_strides
|
def set_strides(arr, strides):
arr.strides = strides
assert_raises(ValueError, set_strides, x, (10 * x.itemsize, x.itemsize))
# Test for offset calculations:
x = np.lib.stride_tricks.as_strided(
np.arange(10, dtype=np.int8)[-1], shape=(10,), strides=(-1,)
)
assert_raises(ValueError, set_strides, x[::-1], -1)
a = x[::-1]
a.strides = 1
a[::2].strides = 2
# test 0d
arr_0d = np.array(0)
arr_0d.strides = ()
assert_raises(TypeError, set_strides, arr_0d, None)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_fill
|
def test_fill(self):
for t in "?bhilqpBHILQPfdgFDGO":
x = np.empty((3, 2, 1), t)
y = np.empty((3, 2, 1), t)
x.fill(1)
y[...] = 1
assert_equal(x, y)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestAttributes(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_fill_max_uint64
|
def test_fill_max_uint64(self):
x = np.empty((3, 2, 1), dtype=np.uint64)
y = np.empty((3, 2, 1), dtype=np.uint64)
value = 2**64 - 1
y[...] = value
x.fill(value)
assert_array_equal(x, y)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestAttributes(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_fill_struct_array
|
def test_fill_struct_array(self):
# Filling from a scalar
x = np.array([(0, 0.0), (1, 1.0)], dtype="i4,f8")
x.fill(x[0])
assert_equal(x["f1"][1], x["f1"][0])
# Filling from a tuple that can be converted
# to a scalar
x = np.zeros(2, dtype=[("a", "f8"), ("b", "i4")])
x.fill((3.5, -2))
assert_array_equal(x["a"], [3.5, 3.5])
assert_array_equal(x["b"], [-2, -2])
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestAttributes(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_fill_readonly
|
def test_fill_readonly(self):
# gh-22922
a = np.zeros(11)
a.setflags(write=False)
with pytest.raises(ValueError, match=".*read-only"):
a.fill(0)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestAttributes(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_void_align
|
def test_void_align(self):
a = np.zeros(4, dtype=np.dtype([("a", "i4"), ("b", "i4")]))
assert_(a.flags.aligned)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
@xpassIfTorchDynamo # (reason="TODO: flags")
@instantiate_parametrized_tests
class TestFlag(TestCase):
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_int
|
def test_int(self):
for st, ut, s in [
(np.int8, np.uint8, 8),
(np.int16, np.uint16, 16),
(np.int32, np.uint32, 32),
(np.int64, np.uint64, 64),
]:
for i in range(1, s):
assert_equal(
hash(st(-(2**i))), hash(-(2**i)), err_msg="%r: -2**%d" % (st, i)
)
assert_equal(
hash(st(2 ** (i - 1))),
hash(2 ** (i - 1)),
err_msg="%r: 2**%d" % (st, i - 1),
)
assert_equal(
hash(st(2**i - 1)),
hash(2**i - 1),
err_msg="%r: 2**%d - 1" % (st, i),
)
i = max(i - 1, 1)
assert_equal(
hash(ut(2 ** (i - 1))),
hash(2 ** (i - 1)),
err_msg="%r: 2**%d" % (ut, i - 1),
)
assert_equal(
hash(ut(2**i - 1)),
hash(2**i - 1),
err_msg="%r: 2**%d - 1" % (ut, i),
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestHash(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_attributes_2
|
def test_attributes_2(self):
assert_equal(self.two.base, np.arange(20))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestAttributes(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_dtypeattr
|
def test_dtypeattr(self):
assert_equal(self.one.dtype, np.dtype(np.int_))
assert_equal(self.three.dtype, np.dtype(np.float64))
assert_equal(self.one.dtype.char, "l")
assert_equal(self.three.dtype.char, "d")
assert_(self.three.dtype.str[0] in "<>")
assert_equal(self.one.dtype.str[1], "i")
assert_equal(self.three.dtype.str[1], "f")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
@xpassIfTorchDynamo # (reason="TODO: hash")
class TestAttributes(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_assignment_broadcasting
|
def test_assignment_broadcasting(self):
a = np.arange(6).reshape(2, 3)
# Broadcasting the input to the output
a[...] = np.arange(3)
assert_equal(a, [[0, 1, 2], [0, 1, 2]])
a[...] = np.arange(2).reshape(2, 1)
assert_equal(a, [[0, 0, 0], [1, 1, 1]])
# For compatibility with <= 1.5, a limited version of broadcasting
# the output to the input.
#
# This behavior is inconsistent with NumPy broadcasting
# in general, because it only uses one of the two broadcasting
# rules (adding a new "1" dimension to the left of the shape),
# applied to the output instead of an input. In NumPy 2.0, this kind
# of broadcasting assignment will likely be disallowed.
a[...] = np.flip(np.arange(6)).reshape(1, 2, 3)
assert_equal(a, [[5, 4, 3], [2, 1, 0]])
# The other type of broadcasting would require a reduction operation.
def assign(a, b):
a[...] = b
assert_raises(
(RuntimeError, ValueError), assign, a, np.arange(12).reshape(2, 2, 3)
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
class TestAssignment(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
assign
|
def assign(a, b):
a[...] = b
assert_raises(
(RuntimeError, ValueError), assign, a, np.arange(12).reshape(2, 2, 3)
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
assign
|
def assign(a, b):
a[...] = b
assert_raises(
(RuntimeError, ValueError), assign, a, np.arange(12).reshape(2, 2, 3)
)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
inject_str
|
def inject_str(s):
"""replace ndarray.__str__ temporarily"""
set_string_function(lambda x: s, repr=False)
try:
yield
finally:
set_string_function(None, repr=False)
a1d = np.array(["test"])
a0d = np.array("done")
with inject_str("bad"):
a1d[0] = a0d # previously this would invoke __str__
assert_equal(a1d[0], "done")
# this would crash for the same reason
np.array([np.array("\xe5\xe4\xf6")])
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
__getitem__
|
def __getitem__(self, value):
pass
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class bad_sequence:
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
__len__
|
def __len__(self):
raise RuntimeError
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class bad_sequence:
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_newaxis
|
def test_newaxis(self):
a, b = self.d
assert_equal(a[np.newaxis].shape, (1,))
assert_equal(a[..., np.newaxis].shape, (1,))
assert_equal(a[np.newaxis, ...].shape, (1,))
assert_equal(a[..., np.newaxis].shape, (1,))
assert_equal(a[np.newaxis, ..., np.newaxis].shape, (1, 1))
assert_equal(a[..., np.newaxis, np.newaxis].shape, (1, 1))
assert_equal(a[np.newaxis, np.newaxis, ...].shape, (1, 1))
assert_equal(a[(np.newaxis,) * 10].shape, (1,) * 10)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_invalid_newaxis
|
def test_invalid_newaxis(self):
a, b = self.d
def subscript(x, i):
x[i]
assert_raises(IndexError, subscript, a, (np.newaxis, 0))
assert_raises(IndexError, subscript, a, (np.newaxis,) * 50)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
subscript
|
def subscript(x, i):
x[i]
assert_raises(IndexError, subscript, a, (np.newaxis, 0))
assert_raises(IndexError, subscript, a, (np.newaxis,) * 50)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_constructor
|
def test_constructor(self):
x = np.ndarray(())
x[()] = 5
assert_equal(x[()], 5)
y = np.ndarray((), buffer=x)
y[()] = 6
assert_equal(x[()], 6)
# strides and shape must be the same length
with pytest.raises(ValueError):
np.ndarray((2,), strides=())
with pytest.raises(ValueError):
np.ndarray((), strides=(2,))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_output
|
def test_output(self):
x = np.array(2)
assert_raises(ValueError, np.add, x, [1], x)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_real_imag
|
def test_real_imag(self):
# contiguity checks are for gh-11245
x = np.array(1j)
xr = x.real
xi = x.imag
assert_equal(xr, np.array(0))
assert_(type(xr) is np.ndarray)
assert_equal(xr.flags.contiguous, True)
assert_equal(xr.flags.f_contiguous, True)
assert_equal(xi, np.array(1))
assert_(type(xi) is np.ndarray)
assert_equal(xi.flags.contiguous, True)
assert_equal(xi.flags.f_contiguous, True)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_ellipsis_subscript
|
def test_ellipsis_subscript(self):
a, b = self.d
assert_equal(a[...], 0)
assert_equal(b[...], "x")
assert_(a[...].base is a) # `a[...] is a` in numpy <1.9.
assert_(b[...].base is b) # `b[...] is b` in numpy <1.9.
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_empty_subscript
|
def test_empty_subscript(self):
a, b = self.d
assert_equal(a[()], 0)
assert_equal(b[()], "x")
assert_(type(a[()]) is a.dtype.type)
assert_(type(b[()]) is str)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_invalid_subscript
|
def test_invalid_subscript(self):
a, b = self.d
assert_raises(IndexError, lambda x: x[0], a)
assert_raises(IndexError, lambda x: x[0], b)
assert_raises(IndexError, lambda x: x[np.array([], int)], a)
assert_raises(IndexError, lambda x: x[np.array([], int)], b)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_cast_to_string
|
def test_cast_to_string(self):
# cast to str should do "str(scalar)", not "str(scalar.item())"
# Example: In python2, str(float) is truncated, so we want to avoid
# str(np.float64(...).item()) as this would incorrectly truncate.
a = np.zeros(1, dtype="S20")
a[:] = np.array(["1.12345678901234567890"], dtype="f8")
assert_equal(a[0], b"1.1234567890123457")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
class TestAssignment(TestCase):
from numpy.core.numeric import set_string_function
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_construction
|
def test_construction(self):
d1 = np.dtype("i4")
assert_equal(d1, np.dtype(np.int32))
d2 = np.dtype("f8")
assert_equal(d2, np.dtype(np.float64))
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
class TestDtypedescr(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_ellipsis_subscript
|
def test_ellipsis_subscript(self):
a, b = self.d
assert_equal(a[...], 0)
assert_equal(b[...], "x")
assert_(a[...].base is a) # `a[...] is a` in numpy <1.9.
assert_(b[...].base is b) # `b[...] is b` in numpy <1.9.
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_empty_subscript
|
def test_empty_subscript(self):
a, b = self.d
assert_equal(a[()], 0)
assert_equal(b[()], "x")
assert_(type(a[()]) is a.dtype.type)
assert_(type(b[()]) is str)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_invalid_subscript
|
def test_invalid_subscript(self):
a, b = self.d
assert_raises(IndexError, lambda x: x[0], a)
assert_raises(IndexError, lambda x: x[0], b)
assert_raises(IndexError, lambda x: x[np.array([], int)], a)
assert_raises(IndexError, lambda x: x[np.array([], int)], b)
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
||
torch
|
test/torch_np/numpy_tests/core/test_multiarray.py
|
test_ellipsis_subscript_assignment
|
def test_ellipsis_subscript_assignment(self):
a, b = self.d
a[...] = 42
assert_equal(a, 42)
b[...] = ""
assert_equal(b.item(), "")
|
import builtins
import collections.abc
import ctypes
import functools
import io
import itertools
import mmap
import operator
import os
import sys
import tempfile
import warnings
import weakref
from contextlib import contextmanager
from decimal import Decimal
from pathlib import Path
from tempfile import mkstemp
from unittest import expectedFailure as xfail, skipIf as skipif, SkipTest
import numpy
import pytest
from pytest import raises as assert_raises
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
slowTest as slow,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
xfailIfTorchDynamo,
xpassIfTorchDynamo,
)
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
import torch._numpy as np
from torch._numpy.testing import (
assert_,
assert_allclose,
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
assert_equal,
assert_raises_regex,
assert_warns,
suppress_warnings,
)
skip = functools.partial(skipif, True)
IS_PYPY = False
IS_PYSTON = False
HAS_REFCOUNT = True
from numpy.core.tests._locales import CommaDecimalPointLocale
from numpy.testing._private.utils import _no_tracing, requires_memory
np.asanyarray = np.asarray
np.asfortranarray = np.asarray
import pickle
from numpy.core.numeric import set_string_function
@skip # (reason="TODO: zero-rank?") # FIXME: revert skip into xfail
class TestZeroRank(TestCase):
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
from numpy.core.multiarray import dot
import operator
from numpy.core._internal import _dtype_from_pep3118
from numpy.testing import IS_PYPY
|
c263bd43e8e8502d4726643bc6fd046f0130ac0e
|
32f585d9346e316e554c8d9bf7548af9f62141fc
|
added
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.