diff options
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r-- | numpy/lib/tests/test__iotools.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 73 | ||||
-rw-r--r-- | numpy/lib/tests/test_index_tricks.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 40 | ||||
-rw-r--r-- | numpy/lib/tests/test_regression.py | 52 | ||||
-rw-r--r-- | numpy/lib/tests/test_shape_base.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_type_check.py | 12 |
7 files changed, 120 insertions, 63 deletions
diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py index a7ee9cbff..03192896c 100644 --- a/numpy/lib/tests/test__iotools.py +++ b/numpy/lib/tests/test__iotools.py @@ -257,7 +257,7 @@ class TestMiscFunctions(object): def test_has_nested_dtype(self): "Test has_nested_dtype" - ndtype = np.dtype(np.float) + ndtype = np.dtype(float) assert_equal(has_nested_fields(ndtype), False) ndtype = np.dtype([('A', '|S3'), ('B', float)]) assert_equal(has_nested_fields(ndtype), False) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 099a2d407..4c90abbf6 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -6,6 +6,7 @@ import sys import decimal import numpy as np +from numpy import ma from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_array_equal, assert_almost_equal, assert_array_almost_equal, assert_raises, @@ -647,6 +648,19 @@ class TestDiff(object): assert_array_equal(diff(x), out) assert_array_equal(diff(x, n=2), out2) + def test_axis(self): + x = np.zeros((10, 20, 30)) + x[:, 1::2, :] = 1 + exp = np.ones((10, 19, 30)) + exp[:, 1::2, :] = -1 + assert_array_equal(diff(x), np.zeros((10, 20, 29))) + assert_array_equal(diff(x, axis=-1), np.zeros((10, 20, 29))) + assert_array_equal(diff(x, axis=0), np.zeros((9, 20, 30))) + assert_array_equal(diff(x, axis=1), exp) + assert_array_equal(diff(x, axis=-2), exp) + assert_raises(np.AxisError, diff, x, axis=3) + assert_raises(np.AxisError, diff, x, axis=-4) + def test_nd(self): x = 20 * rand(10, 20, 30) out1 = x[:, :, 1:] - x[:, :, :-1] @@ -658,6 +672,45 @@ class TestDiff(object): assert_array_equal(diff(x, axis=0), out3) assert_array_equal(diff(x, n=2, axis=0), out4) + def test_n(self): + x = list(range(3)) + assert_raises(ValueError, diff, x, n=-1) + output = [diff(x, n=n) for n in range(1, 5)] + expected = [[1, 1], [0], [], []] + assert_(diff(x, n=0) is x) + for n, (expected, out) in enumerate(zip(expected, output), start=1): + assert_(type(out) is np.ndarray) + assert_array_equal(out, expected) + 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_subclass(self): + x = ma.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], + mask=[[False, False], [True, False], + [False, True], [True, True], [False, False]]) + out = diff(x) + assert_array_equal(out.data, [[1], [1], [1], [1], [1]]) + assert_array_equal(out.mask, [[False], [True], + [True], [True], [False]]) + assert_(type(out) is type(x)) + + out3 = diff(x, n=3) + assert_array_equal(out3.data, [[], [], [], [], []]) + assert_array_equal(out3.mask, [[], [], [], [], []]) + assert_(type(out3) is type(x)) + class TestDelete(object): @@ -1658,16 +1711,16 @@ class TestHistogram(object): # Check the type of the returned histogram a = np.arange(10) + .5 h, b = histogram(a) - assert_(np.issubdtype(h.dtype, int)) + assert_(np.issubdtype(h.dtype, np.integer)) h, b = histogram(a, normed=True) - assert_(np.issubdtype(h.dtype, float)) + assert_(np.issubdtype(h.dtype, np.floating)) h, b = histogram(a, weights=np.ones(10, int)) - assert_(np.issubdtype(h.dtype, int)) + assert_(np.issubdtype(h.dtype, np.integer)) h, b = histogram(a, weights=np.ones(10, float)) - assert_(np.issubdtype(h.dtype, float)) + assert_(np.issubdtype(h.dtype, np.floating)) def test_f32_rounding(self): # gh-4799, check that the rounding of the edges works with float32 @@ -2610,28 +2663,28 @@ class TestInterp(object): incres = interp(incpts, xp, yp) decres = interp(decpts, xp, yp) - inctgt = np.array([1, 1, 1, 1], dtype=np.float) + inctgt = np.array([1, 1, 1, 1], dtype=float) dectgt = inctgt[::-1] assert_equal(incres, inctgt) assert_equal(decres, dectgt) incres = interp(incpts, xp, yp, left=0) decres = interp(decpts, xp, yp, left=0) - inctgt = np.array([0, 1, 1, 1], dtype=np.float) + inctgt = np.array([0, 1, 1, 1], dtype=float) dectgt = inctgt[::-1] assert_equal(incres, inctgt) assert_equal(decres, dectgt) incres = interp(incpts, xp, yp, right=2) decres = interp(decpts, xp, yp, right=2) - inctgt = np.array([1, 1, 1, 2], dtype=np.float) + inctgt = np.array([1, 1, 1, 2], dtype=float) dectgt = inctgt[::-1] assert_equal(incres, inctgt) assert_equal(decres, dectgt) incres = interp(incpts, xp, yp, left=0, right=2) decres = interp(decpts, xp, yp, left=0, right=2) - inctgt = np.array([0, 1, 1, 2], dtype=np.float) + inctgt = np.array([0, 1, 1, 2], dtype=float) dectgt = inctgt[::-1] assert_equal(incres, inctgt) assert_equal(decres, dectgt) @@ -2958,7 +3011,7 @@ class TestPercentile(object): o = np.random.normal(size=(71, 23)) x = np.dstack([o] * 10) assert_equal(np.percentile(x, 30, axis=(0, 1)), np.percentile(o, 30)) - x = np.rollaxis(x, -1, 0) + x = np.moveaxis(x, -1, 0) assert_equal(np.percentile(x, 30, axis=(-2, -1)), np.percentile(o, 30)) x = x.swapaxes(0, 1).copy() assert_equal(np.percentile(x, 30, axis=(0, -1)), np.percentile(o, 30)) @@ -3339,7 +3392,7 @@ class TestMedian(object): o = np.random.normal(size=(71, 23)) x = np.dstack([o] * 10) assert_equal(np.median(x, axis=(0, 1)), np.median(o)) - x = np.rollaxis(x, -1, 0) + x = np.moveaxis(x, -1, 0) assert_equal(np.median(x, axis=(-2, -1)), np.median(o)) x = x.swapaxes(0, 1).copy() assert_equal(np.median(x, axis=(0, -1)), np.median(o)) diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index f06406c9e..452b3d6a2 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -243,7 +243,7 @@ class TestIx_(object): for k, (a, sz) in enumerate(zip(arrays, sizes)): assert_equal(a.shape[k], sz) assert_(all(sh == 1 for j, sh in enumerate(a.shape) if j != k)) - assert_(np.issubdtype(a.dtype, int)) + assert_(np.issubdtype(a.dtype, np.integer)) def test_bool(self): bool_a = [True, False, True, True] diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 4bc2a1b1b..f2fd37230 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -373,7 +373,7 @@ class TestSaveTxt(object): # Test the functionality of the header and footer keyword argument. c = BytesIO() - a = np.array([(1, 2), (3, 4)], dtype=np.int) + a = np.array([(1, 2), (3, 4)], dtype=int) test_header_footer = 'Test header / footer' # Test the header keyword argument np.savetxt(c, a, fmt='%1d', header=test_header_footer) @@ -485,7 +485,7 @@ class TestLoadTxt(object): c.write('1 2\n3 4') c.seek(0) - x = np.loadtxt(c, dtype=np.int) + x = np.loadtxt(c, dtype=int) a = np.array([[1, 2], [3, 4]], int) assert_array_equal(x, a) @@ -721,7 +721,7 @@ class TestLoadTxt(object): # Test using an explicit dtype with an object data = """ 1; 2001-01-01 2; 2002-01-31 """ - ndtype = [('idx', int), ('code', np.object)] + ndtype = [('idx', int), ('code', object)] func = lambda s: strptime(s.strip(), "%Y-%m-%d") converters = {1: func} test = np.loadtxt(TextIO(data), delimiter=";", dtype=ndtype, @@ -751,11 +751,11 @@ class TestLoadTxt(object): # IEEE doubles and floats only, otherwise the float32 # conversion may fail. tgt = np.logspace(-10, 10, 5).astype(np.float32) - tgt = np.hstack((tgt, -tgt)).astype(np.float) + tgt = np.hstack((tgt, -tgt)).astype(float) inp = '\n'.join(map(float.hex, tgt)) c = TextIO() c.write(inp) - for dt in [np.float, np.float32]: + for dt in [float, np.float32]: c.seek(0) res = np.loadtxt(c, dtype=dt) assert_equal(res, tgt, err_msg="%s" % dt) @@ -765,7 +765,7 @@ class TestLoadTxt(object): c = TextIO() c.write("%s %s" % tgt) c.seek(0) - res = np.loadtxt(c, dtype=np.complex) + res = np.loadtxt(c, dtype=complex) assert_equal(res, tgt) def test_universal_newline(self): @@ -1190,7 +1190,7 @@ M 33 21.99 # Test using an explicit dtype with an object data = """ 1; 2001-01-01 2; 2002-01-31 """ - ndtype = [('idx', int), ('code', np.object)] + ndtype = [('idx', int), ('code', object)] func = lambda s: strptime(s.strip(), "%Y-%m-%d") converters = {1: func} test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype, @@ -1200,7 +1200,7 @@ M 33 21.99 dtype=ndtype) assert_equal(test, control) - ndtype = [('nest', [('idx', int), ('code', np.object)])] + ndtype = [('nest', [('idx', int), ('code', object)])] try: test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype, converters=converters) @@ -1337,7 +1337,7 @@ M 33 21.99 test = np.mafromtxt(data, dtype=None, **kwargs) control = ma.array([(0, 1), (2, -1)], mask=[(False, False), (False, True)], - dtype=[('A', np.int), ('B', np.int)]) + dtype=[('A', int), ('B', int)]) assert_equal(test, control) assert_equal(test.mask, control.mask) # @@ -1345,7 +1345,7 @@ M 33 21.99 test = np.mafromtxt(data, **kwargs) control = ma.array([(0, 1), (2, -1)], mask=[(False, False), (False, True)], - dtype=[('A', np.float), ('B', np.float)]) + dtype=[('A', float), ('B', float)]) assert_equal(test, control) assert_equal(test.mask, control.mask) @@ -1414,7 +1414,7 @@ M 33 21.99 missing_values='-999.0', names=True,) control = ma.array([(0, 1.5), (2, -1.)], mask=[(False, False), (False, True)], - dtype=[('A', np.int), ('B', np.float)]) + dtype=[('A', int), ('B', float)]) assert_equal(test, control) assert_equal(test.mask, control.mask) @@ -1682,7 +1682,7 @@ M 33 21.99 kwargs = dict(delimiter=",", missing_values="N/A", names=True) test = np.recfromtxt(data, **kwargs) control = np.array([(0, 1), (2, 3)], - dtype=[('A', np.int), ('B', np.int)]) + dtype=[('A', int), ('B', int)]) assert_(isinstance(test, np.recarray)) assert_equal(test, control) # @@ -1690,7 +1690,7 @@ M 33 21.99 test = np.recfromtxt(data, dtype=None, usemask=True, **kwargs) control = ma.array([(0, 1), (2, -1)], mask=[(False, False), (False, True)], - dtype=[('A', np.int), ('B', np.int)]) + dtype=[('A', int), ('B', int)]) assert_equal(test, control) assert_equal(test.mask, control.mask) assert_equal(test.A, [0, 2]) @@ -1701,7 +1701,7 @@ M 33 21.99 kwargs = dict(missing_values="N/A", names=True, case_sensitive=True) test = np.recfromcsv(data, dtype=None, **kwargs) control = np.array([(0, 1), (2, 3)], - dtype=[('A', np.int), ('B', np.int)]) + dtype=[('A', int), ('B', int)]) assert_(isinstance(test, np.recarray)) assert_equal(test, control) # @@ -1709,7 +1709,7 @@ M 33 21.99 test = np.recfromcsv(data, dtype=None, usemask=True, **kwargs) control = ma.array([(0, 1), (2, -1)], mask=[(False, False), (False, True)], - dtype=[('A', np.int), ('B', np.int)]) + dtype=[('A', int), ('B', int)]) assert_equal(test, control) assert_equal(test.mask, control.mask) assert_equal(test.A, [0, 2]) @@ -1717,12 +1717,12 @@ M 33 21.99 data = TextIO('A,B\n0,1\n2,3') test = np.recfromcsv(data, missing_values='N/A',) control = np.array([(0, 1), (2, 3)], - dtype=[('a', np.int), ('b', np.int)]) + dtype=[('a', int), ('b', int)]) assert_(isinstance(test, np.recarray)) assert_equal(test, control) # data = TextIO('A,B\n0,1\n2,3') - dtype = [('a', np.int), ('b', np.float)] + dtype = [('a', int), ('b', float)] test = np.recfromcsv(data, missing_values='N/A', dtype=dtype) control = np.array([(0, 1), (2, 3)], dtype=dtype) @@ -1827,7 +1827,7 @@ M 33 21.99 assert_equal(test.dtype.names, ['f0', 'f1', 'f2']) - assert_(test.dtype['f0'] == np.float) + assert_(test.dtype['f0'] == float) assert_(test.dtype['f1'] == np.int64) assert_(test.dtype['f2'] == np.integer) @@ -1919,7 +1919,7 @@ class TestPathUsage(object): kwargs = dict(delimiter=",", missing_values="N/A", names=True) test = np.recfromtxt(path, **kwargs) control = np.array([(0, 1), (2, 3)], - dtype=[('A', np.int), ('B', np.int)]) + dtype=[('A', int), ('B', int)]) assert_(isinstance(test, np.recarray)) assert_equal(test, control) @@ -1933,7 +1933,7 @@ class TestPathUsage(object): kwargs = dict(missing_values="N/A", names=True, case_sensitive=True) test = np.recfromcsv(path, dtype=None, **kwargs) control = np.array([(0, 1), (2, 3)], - dtype=[('A', np.int), ('B', np.int)]) + dtype=[('A', int), ('B', int)]) assert_(isinstance(test, np.recarray)) assert_equal(test, control) diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 1567219d6..d96d3422d 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -10,16 +10,14 @@ from numpy.testing import ( ) from numpy.compat import unicode -rlevel = 1 - class TestRegression(object): - def test_poly1d(self, level=rlevel): + def test_poly1d(self): # Ticket #28 assert_equal(np.poly1d([1]) - np.poly1d([1, 0]), np.poly1d([-1, 1])) - def test_cov_parameters(self, level=rlevel): + def test_cov_parameters(self): # Ticket #91 x = np.random.random((3, 3)) y = x.copy() @@ -27,57 +25,57 @@ class TestRegression(object): np.cov(y, rowvar=0) assert_array_equal(x, y) - def test_mem_digitize(self, level=rlevel): + def test_mem_digitize(self): # Ticket #95 for i in range(100): np.digitize([1, 2, 3, 4], [1, 3]) np.digitize([0, 1, 2, 3, 4], [1, 3]) - def test_unique_zero_sized(self, level=rlevel): + def test_unique_zero_sized(self): # Ticket #205 assert_array_equal([], np.unique(np.array([]))) - def test_mem_vectorise(self, level=rlevel): + def test_mem_vectorise(self): # Ticket #325 vt = np.vectorize(lambda *args: args) vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1, 1, 2))) vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1, 1, 2)), np.zeros((2, 2))) - def test_mgrid_single_element(self, level=rlevel): + def test_mgrid_single_element(self): # Ticket #339 assert_array_equal(np.mgrid[0:0:1j], [0]) assert_array_equal(np.mgrid[0:0], []) - def test_refcount_vectorize(self, level=rlevel): + def test_refcount_vectorize(self): # Ticket #378 def p(x, y): return 123 v = np.vectorize(p) _assert_valid_refcount(v) - def test_poly1d_nan_roots(self, level=rlevel): + def test_poly1d_nan_roots(self): # Ticket #396 p = np.poly1d([np.nan, np.nan, 1], r=0) assert_raises(np.linalg.LinAlgError, getattr, p, "r") - def test_mem_polymul(self, level=rlevel): + def test_mem_polymul(self): # Ticket #448 np.polymul([], [1.]) - def test_mem_string_concat(self, level=rlevel): + def test_mem_string_concat(self): # Ticket #469 x = np.array([]) np.append(x, 'asdasd\tasdasd') - def test_poly_div(self, level=rlevel): + def test_poly_div(self): # Ticket #553 u = np.poly1d([1, 2, 3]) v = np.poly1d([1, 2, 3, 4, 5]) q, r = np.polydiv(u, v) assert_equal(q*v + r, u) - def test_poly_eq(self, level=rlevel): + def test_poly_eq(self): # Ticket #554 x = np.poly1d([1, 2, 3]) y = np.poly1d([3, 4]) @@ -108,13 +106,13 @@ class TestRegression(object): def test_polydiv_type(self): # Make polydiv work for complex types msg = "Wrong type, should be complex" - x = np.ones(3, dtype=np.complex) + x = np.ones(3, dtype=complex) q, r = np.polydiv(x, x) - assert_(q.dtype == np.complex, msg) + assert_(q.dtype == complex, msg) msg = "Wrong type, should be float" - x = np.ones(3, dtype=np.int) + x = np.ones(3, dtype=int) q, r = np.polydiv(x, x) - assert_(q.dtype == np.float, msg) + assert_(q.dtype == float, msg) def test_histogramdd_too_many_bins(self): # Ticket 928. @@ -123,22 +121,22 @@ class TestRegression(object): def test_polyint_type(self): # Ticket #944 msg = "Wrong type, should be complex" - x = np.ones(3, dtype=np.complex) - assert_(np.polyint(x).dtype == np.complex, msg) + x = np.ones(3, dtype=complex) + assert_(np.polyint(x).dtype == complex, msg) msg = "Wrong type, should be float" - x = np.ones(3, dtype=np.int) - assert_(np.polyint(x).dtype == np.float, msg) + x = np.ones(3, dtype=int) + assert_(np.polyint(x).dtype == float, msg) def test_ndenumerate_crash(self): # Ticket 1140 # Shouldn't crash: list(np.ndenumerate(np.array([[]]))) - def test_asfarray_none(self, level=rlevel): + def test_asfarray_none(self): # Test for changeset r5065 assert_array_equal(np.array([np.nan]), np.asfarray([None])) - def test_large_fancy_indexing(self, level=rlevel): + def test_large_fancy_indexing(self): # Large enough to fail on 64-bit. nbits = np.dtype(np.intp).itemsize * 8 thesize = int((2**nbits)**(1.0/5.0)+1) @@ -158,12 +156,12 @@ class TestRegression(object): assert_raises(ValueError, dp) assert_raises(ValueError, dp2) - def test_void_coercion(self, level=rlevel): + def test_void_coercion(self): dt = np.dtype([('a', 'f4'), ('b', 'i4')]) x = np.zeros((1,), dt) assert_(np.r_[x, x].dtype == dt) - def test_who_with_0dim_array(self, level=rlevel): + def test_who_with_0dim_array(self): # ticket #1243 import os import sys @@ -234,7 +232,7 @@ class TestRegression(object): def test_nansum_with_boolean(self): # gh-2978 - a = np.zeros(2, dtype=np.bool) + a = np.zeros(2, dtype=bool) try: np.nansum(a) except Exception: diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py index 9e739ea63..d0afeefd9 100644 --- a/numpy/lib/tests/test_shape_base.py +++ b/numpy/lib/tests/test_shape_base.py @@ -19,7 +19,7 @@ class TestApplyAlongAxis(object): assert_array_equal( apply_along_axis(len, 0, a), len(a)*np.ones(a.shape[1])) - def test_simple101(self, level=11): + def test_simple101(self): a = np.ones((10, 101), 'd') assert_array_equal( apply_along_axis(len, 0, a), len(a)*np.ones(a.shape[1])) diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py index 259fcd4e5..8945b61ea 100644 --- a/numpy/lib/tests/test_type_check.py +++ b/numpy/lib/tests/test_type_check.py @@ -3,7 +3,7 @@ from __future__ import division, absolute_import, print_function import numpy as np from numpy.compat import long from numpy.testing import ( - assert_, assert_equal, assert_array_equal, run_module_suite + assert_, assert_equal, assert_array_equal, run_module_suite, assert_raises ) from numpy.lib.type_check import ( common_type, mintypecode, isreal, iscomplex, isposinf, isneginf, @@ -374,7 +374,7 @@ class TestNanToNum(object): vals = nan_to_num(1) assert_all(vals == 1) vals = nan_to_num([1]) - assert_array_equal(vals, np.array([1], np.int)) + assert_array_equal(vals, np.array([1], int)) def test_complex_good(self): vals = nan_to_num(1+1j) @@ -420,7 +420,13 @@ class TestArrayConversion(object): def test_asfarray(self): a = asfarray(np.array([1, 2, 3])) assert_equal(a.__class__, np.ndarray) - assert_(np.issubdtype(a.dtype, np.float)) + assert_(np.issubdtype(a.dtype, np.floating)) + + # previously this would infer dtypes from arrays, unlike every single + # other numpy function + assert_raises(TypeError, + asfarray, np.array([1, 2, 3]), dtype=np.array(1.0)) + if __name__ == "__main__": run_module_suite() |