Skip to content

TST: trim tests of unsupported numpy features #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 3 additions & 83 deletions torch_np/tests/numpy_tests/core/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,19 +750,15 @@ def check_promotion_cases(self, promote_func):
b = np.bool_(0)
i8, i16, i32, i64 = np.int8(0), np.int16(0), np.int32(0), np.int64(0)
u8 = np.uint8(0)
f32, f64, fld = np.float32(0), np.float64(0), np.longdouble(0)
c64, c128, cld = np.complex64(0), np.complex128(0), np.clongdouble(0)
f32, f64 = np.float32(0), np.float64(0)
c64, c128 = np.complex64(0), np.complex128(0)

# coercion within the same kind
assert_equal(promote_func(i8, i16), np.dtype(np.int16))
assert_equal(promote_func(i32, i8), np.dtype(np.int32))
assert_equal(promote_func(i16, i64), np.dtype(np.int64))
assert_equal(promote_func(f32, f64), np.dtype(np.float64))
assert_equal(promote_func(fld, f32), np.dtype(np.longdouble))
assert_equal(promote_func(f64, fld), np.dtype(np.longdouble))
assert_equal(promote_func(c128, c64), np.dtype(np.complex128))
assert_equal(promote_func(cld, c128), np.dtype(np.clongdouble))
assert_equal(promote_func(c64, fld), np.dtype(np.clongdouble))

# coercion between kinds
assert_equal(promote_func(b, i32), np.dtype(np.int32))
Expand All @@ -777,7 +773,6 @@ def check_promotion_cases(self, promote_func):
assert_equal(promote_func(f32, u32), np.dtype(np.float64))
assert_equal(promote_func(f32, c64), np.dtype(np.complex64))
assert_equal(promote_func(c128, f32), np.dtype(np.complex128))
assert_equal(promote_func(cld, f64), np.dtype(np.clongdouble))

# coercion between scalars and 1-D arrays
assert_equal(promote_func(np.array([b]), i8), np.dtype(np.int8))
Expand Down Expand Up @@ -822,9 +817,6 @@ def res_type(a, b):
for a in [np.array([True, False]), np.array([-3, 12], dtype=np.int8)]:
b = 1.234 * a
assert_equal(b.dtype, np.dtype('f8'), "array type %s" % a.dtype)
b = np.longdouble(1.234) * a
assert_equal(b.dtype, np.dtype(np.longdouble),
"array type %s" % a.dtype)
b = np.float64(1.234) * a
assert_equal(b.dtype, np.dtype('f8'), "array type %s" % a.dtype)
b = np.float32(1.234) * a
Expand All @@ -834,9 +826,6 @@ def res_type(a, b):

b = 1.234j * a
assert_equal(b.dtype, np.dtype('c16'), "array type %s" % a.dtype)
b = np.clongdouble(1.234j) * a
assert_equal(b.dtype, np.dtype(np.clongdouble),
"array type %s" % a.dtype)
b = np.complex128(1.234j) * a
assert_equal(b.dtype, np.dtype('c16'), "array type %s" % a.dtype)
b = np.complex64(1.234j) * a
Expand Down Expand Up @@ -1097,41 +1086,6 @@ def test_can_cast(self):
# Also test keyword arguments
assert_(np.can_cast(from_=np.int32, to=np.int64))

def test_can_cast_simple_to_structured(self):
# Non-structured can only be cast to structured in 'unsafe' mode.
assert_(not np.can_cast('i4', 'i4,i4'))
assert_(not np.can_cast('i4', 'i4,i2'))
assert_(np.can_cast('i4', 'i4,i4', casting='unsafe'))
assert_(np.can_cast('i4', 'i4,i2', casting='unsafe'))
# Even if there is just a single field which is OK.
assert_(not np.can_cast('i2', [('f1', 'i4')]))
assert_(not np.can_cast('i2', [('f1', 'i4')], casting='same_kind'))
assert_(np.can_cast('i2', [('f1', 'i4')], casting='unsafe'))
# It should be the same for recursive structured or subarrays.
assert_(not np.can_cast('i2', [('f1', 'i4,i4')]))
assert_(np.can_cast('i2', [('f1', 'i4,i4')], casting='unsafe'))
assert_(not np.can_cast('i2', [('f1', '(2,3)i4')]))
assert_(np.can_cast('i2', [('f1', '(2,3)i4')], casting='unsafe'))

def test_can_cast_structured_to_simple(self):
# Need unsafe casting for structured to simple.
assert_(not np.can_cast([('f1', 'i4')], 'i4'))
assert_(np.can_cast([('f1', 'i4')], 'i4', casting='unsafe'))
assert_(np.can_cast([('f1', 'i4')], 'i2', casting='unsafe'))
# Since it is unclear what is being cast, multiple fields to
# single should not work even for unsafe casting.
assert_(not np.can_cast('i4,i4', 'i4', casting='unsafe'))
# But a single field inside a single field is OK.
assert_(not np.can_cast([('f1', [('x', 'i4')])], 'i4'))
assert_(np.can_cast([('f1', [('x', 'i4')])], 'i4', casting='unsafe'))
# And a subarray is fine too - it will just take the first element
# (arguably not very consistently; might also take the first field).
assert_(not np.can_cast([('f0', '(3,)i4')], 'i4'))
assert_(np.can_cast([('f0', '(3,)i4')], 'i4', casting='unsafe'))
# But a structured subarray with multiple fields should fail.
assert_(not np.can_cast([('f0', ('i4,i4'), (2,))], 'i4',
casting='unsafe'))

def test_can_cast_values(self):
# gh-5917
for dt in np.sctypes['int'] + np.sctypes['uint']:
Expand Down Expand Up @@ -1189,7 +1143,7 @@ def load_data(self, n, eindex):
raise NIterError('error at index %s' % eindex)
yield e

@pytest.mark.parametrize("dtype", [int, object])
@pytest.mark.parametrize("dtype", [int])
@pytest.mark.parametrize(["count", "error_index"], [(10, 5), (10, 9)])
def test_2592(self, count, error_index, dtype):
# Test iteration exceptions are correctly raised. The data/generator
Expand All @@ -1198,40 +1152,6 @@ def test_2592(self, count, error_index, dtype):
with pytest.raises(NIterError):
np.fromiter(iterable, dtype=dtype, count=count)

@pytest.mark.parametrize("dtype", ["S", "S0", "V0", "U0"])
def test_empty_not_structured(self, dtype):
# Note, "S0" could be allowed at some point, so long "S" (without
# any length) is rejected.
with pytest.raises(ValueError, match="Must specify length"):
np.fromiter([], dtype=dtype)

@pytest.mark.parametrize(["dtype", "data"],
[("d", [1, 2, 3, 4, 5, 6, 7, 8, 9]),
("O", [1, 2, 3, 4, 5, 6, 7, 8, 9]),
("i,O", [(1, 2), (5, 4), (2, 3), (9, 8), (6, 7)]),
# subarray dtypes (important because their dimensions end up
# in the result arrays dimension:
("2i", [(1, 2), (5, 4), (2, 3), (9, 8), (6, 7)]),
])
@pytest.mark.parametrize("length_hint", [0, 1])
def test_growth_and_complicated_dtypes(self, dtype, data, length_hint):
dtype = np.dtype(dtype)

data = data * 100 # make sure we realloc a bit

class MyIter:
# Class/example from gh-15789
def __length_hint__(self):
# only required to be an estimate, this is legal
return length_hint # 0 or 1

def __iter__(self):
return iter(data)

res = np.fromiter(MyIter(), dtype=dtype)
expected = np.array(data, dtype=dtype)

assert_array_equal(res, expected)

def test_empty_result(self):
class MyIter:
Expand Down
92 changes: 7 additions & 85 deletions torch_np/tests/numpy_tests/lib/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,18 +741,6 @@ def test_n(self):
assert_equal(out.dtype, np.int_)
assert_equal(len(out), max(0, len(x) - n))

def test_times(self):
x = np.arange('1066-10-13', '1066-10-16', dtype=np.datetime64)
expected = [
np.array([1, 1], dtype='timedelta64[D]'),
np.array([0], dtype='timedelta64[D]'),
]
expected.extend([np.array([], dtype='timedelta64[D]')] * 3)
for n, exp in enumerate(expected, start=1):
out = diff(x, n=n)
assert_array_equal(out, exp)
assert_equal(out.dtype, exp.dtype)

def test_prepend(self):
x = np.arange(5) + 1
assert_array_equal(diff(x, prepend=0), np.ones(5))
Expand Down Expand Up @@ -1068,17 +1056,6 @@ def test_specific_axes(self):
assert_raises(np.AxisError, gradient, x, axis=-3)
# assert_raises(TypeError, gradient, x, axis=[1,])

def test_timedelta64(self):
# Make sure gradient() can handle special types like timedelta64
x = np.array(
[-5, -3, 10, 12, 61, 321, 300],
dtype='timedelta64[D]')
dx = np.array(
[2, 7, 7, 25, 154, 119, -21],
dtype='timedelta64[D]')
assert_array_equal(gradient(x), dx)
assert_(dx.dtype == np.dtype('timedelta64[D]'))

def test_inexact_dtypes(self):
for dt in [np.float16, np.float32, np.float64]:
# dtypes should not be promoted in a different way to what diff does
Expand Down Expand Up @@ -1620,26 +1597,6 @@ def test_size_zero_output(self):
with assert_raises_regex(ValueError, 'new output dimensions'):
f(x)

def test_subclasses(self):
class subclass(np.ndarray):
pass

m = np.array([[1., 0., 0.],
[0., 0., 1.],
[0., 1., 0.]]).view(subclass)
v = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]).view(subclass)
# generalized (gufunc)
matvec = np.vectorize(np.matmul, signature='(m,m),(m)->(m)')
r = matvec(m, v)
assert_equal(type(r), subclass)
assert_equal(r, [[1., 3., 2.], [4., 6., 5.], [7., 9., 8.]])

# element-wise (ufunc)
mult = np.vectorize(lambda x, y: x*y)
r = mult(m, v)
assert_equal(type(r), subclass)
assert_equal(r, m * v)


@pytest.mark.xfail(reason='TODO: implement')
class TestDigitize:
Expand Down Expand Up @@ -1705,15 +1662,6 @@ def test_casting_error(self):
x, bins = bins, x
assert_raises(TypeError, digitize, x, bins)

def test_return_type(self):
# Functions returning indices should always return base ndarrays
class A(np.ndarray):
pass
a = np.arange(5).view(A)
b = np.arange(1, 3).view(A)
assert_(not isinstance(digitize(b, a, False), A))
assert_(not isinstance(digitize(b, a, True), A))

def test_large_integers_increasing(self):
# gh-11022
x = 2**54 # loses precision in a float
Expand Down Expand Up @@ -1757,7 +1705,7 @@ def test_period(self):

@pytest.mark.xfail(reason='TODO: implement')
@pytest.mark.parametrize(
"dtype", "O" + np.typecodes["AllInteger"] + np.typecodes["Float"]
"dtype", np.typecodes["AllInteger"] + np.typecodes["Float"]
)
@pytest.mark.parametrize("M", [0, 1, 10])
class TestFilterwindows:
Expand All @@ -1766,10 +1714,7 @@ def test_hanning(self, dtype: str, M: int) -> None:
scalar = np.array(M, dtype=dtype)[()]

w = hanning(scalar)
if dtype == "O":
ref_dtype = np.float64
else:
ref_dtype = np.result_type(scalar.dtype, np.float64)
ref_dtype = np.result_type(scalar.dtype, np.float64)
assert w.dtype == ref_dtype

# check symmetry
Expand All @@ -1787,10 +1732,7 @@ def test_hamming(self, dtype: str, M: int) -> None:
scalar = np.array(M, dtype=dtype)[()]

w = hamming(scalar)
if dtype == "O":
ref_dtype = np.float64
else:
ref_dtype = np.result_type(scalar.dtype, np.float64)
ref_dtype = np.result_type(scalar.dtype, np.float64)
assert w.dtype == ref_dtype

# check symmetry
Expand All @@ -1808,10 +1750,7 @@ def test_bartlett(self, dtype: str, M: int) -> None:
scalar = np.array(M, dtype=dtype)[()]

w = bartlett(scalar)
if dtype == "O":
ref_dtype = np.float64
else:
ref_dtype = np.result_type(scalar.dtype, np.float64)
ref_dtype = np.result_type(scalar.dtype, np.float64)
assert w.dtype == ref_dtype

# check symmetry
Expand All @@ -1829,10 +1768,7 @@ def test_blackman(self, dtype: str, M: int) -> None:
scalar = np.array(M, dtype=dtype)[()]

w = blackman(scalar)
if dtype == "O":
ref_dtype = np.float64
else:
ref_dtype = np.result_type(scalar.dtype, np.float64)
ref_dtype = np.result_type(scalar.dtype, np.float64)
assert w.dtype == ref_dtype

# check symmetry
Expand All @@ -1850,10 +1786,7 @@ def test_kaiser(self, dtype: str, M: int) -> None:
scalar = np.array(M, dtype=dtype)[()]

w = kaiser(scalar, 0)
if dtype == "O":
ref_dtype = np.float64
else:
ref_dtype = np.result_type(scalar.dtype, np.float64)
ref_dtype = np.result_type(scalar.dtype, np.float64)
assert w.dtype == ref_dtype

# check symmetry
Expand Down Expand Up @@ -2438,14 +2371,6 @@ def test_multidimensional_extrafunc(self):
assert_array_equal(y, np.array([[-1., -1., -1.],
[3., 3., 1.]]))

def test_subclasses(self):
class subclass(np.ndarray):
pass
x = np.arange(5.).view(subclass)
r = piecewise(x, [x<2., x>=4], [-1., 1., 0.])
assert_equal(type(r), subclass)
assert_equal(r, [-1., -1., 0., 0., 1.])


class TestBincount:

Expand Down Expand Up @@ -2840,10 +2765,7 @@ def test_linear_interpolation(self,
actual, expected_dtype.type(expected), 14)

if method in ["inverted_cdf", "closest_observation"]:
if input_dtype == "O":
np.testing.assert_equal(np.asarray(actual).dtype, np.float64)
else:
np.testing.assert_equal(np.asarray(actual).dtype,
np.testing.assert_equal(np.asarray(actual).dtype,
np.dtype(input_dtype))
else:
np.testing.assert_equal(np.asarray(actual).dtype,
Expand Down
Loading