diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2016-01-19 13:54:55 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2016-01-19 13:54:55 -0700 |
commit | 275fa79351e5fb30c19d78f29513a5be646ca1c8 (patch) | |
tree | 90d53a926101fb4bef8e3191dad664602250a82f | |
parent | 5a36018eda95da0ecc10033c3301c11f038ed5ac (diff) | |
parent | 44c49f311f3c2e0fa6440ddde1c8fef9a4b5a93e (diff) | |
download | numpy-275fa79351e5fb30c19d78f29513a5be646ca1c8.tar.gz |
Merge pull request #7027 from gfyoung/test_fromnumeric_expand
TST, MAINT: Lots of new tests for fromnumeric.py
-rw-r--r-- | numpy/core/tests/test_fromnumeric.py | 17 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 313 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 297 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 61 |
4 files changed, 490 insertions, 198 deletions
diff --git a/numpy/core/tests/test_fromnumeric.py b/numpy/core/tests/test_fromnumeric.py deleted file mode 100644 index 0fba10b6e..000000000 --- a/numpy/core/tests/test_fromnumeric.py +++ /dev/null @@ -1,17 +0,0 @@ -from __future__ import division, absolute_import, print_function - -from numpy import put -from numpy.testing import TestCase, assert_raises - - -class TestPut(TestCase): - - def test_bad_array(self): - # We want to raise a TypeError in the - # case that a non-ndarray object is passed - # in since `np.put` modifies in place and - # hence would do nothing to a non-ndarray - v = 5 - indx = [0, 2] - bad_array = [1, 2, 3] - assert_raises(TypeError, put, bad_array, indx, v) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 1666af4f1..f432aa975 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -37,7 +37,7 @@ from datetime import timedelta if sys.version_info[:2] > (3, 2): - # In Python 3.3 the representation of empty shape, strides and suboffsets + # In Python 3.3 the representation of empty shape, strides and sub-offsets # is an empty tuple instead of None. # http://docs.python.org/dev/whatsnew/3.3.html#api-changes EMPTY = () @@ -72,7 +72,7 @@ class TestFlags(TestCase): def test_string_align(self): a = np.zeros(4, dtype=np.dtype('|S4')) assert_(a.flags.aligned) - # not power of two are accessed bytewise and thus considered 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) @@ -80,6 +80,7 @@ class TestFlags(TestCase): a = np.zeros(4, dtype=np.dtype([("a", "i4"), ("b", "i4")])) assert_(a.flags.aligned) + class TestHash(TestCase): # see #3793 def test_int(self): @@ -101,6 +102,7 @@ class TestHash(TestCase): assert_equal(hash(ut(2**i - 1)), hash(2**i - 1), err_msg="%r: 2**%d - 1" % (ut, i)) + class TestAttributes(TestCase): def setUp(self): self.one = np.arange(10) @@ -159,8 +161,8 @@ class TestAttributes(TestCase): def make_array(size, offset, strides): return np.ndarray(size, buffer=x, dtype=int, - offset=offset*x.itemsize, - strides=strides*x.itemsize) + offset=offset*x.itemsize, + strides=strides*x.itemsize) assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1])) self.assertRaises(ValueError, make_array, 4, 4, -2) @@ -351,6 +353,7 @@ class TestAssignment(TestCase): assert_raises((AttributeError, TypeError), assign, C()) assert_raises(ValueError, assign, [1]) + class TestDtypedescr(TestCase): def test_construction(self): d1 = np.dtype('i4') @@ -362,6 +365,7 @@ class TestDtypedescr(TestCase): self.assertNotEqual(np.dtype('<i4'), np.dtype('>i4')) self.assertNotEqual(np.dtype([('a', '<i4')]), np.dtype([('a', '>i4')])) + class TestZeroRank(TestCase): def setUp(self): self.d = np.array(0), np.array('x', object) @@ -539,6 +543,7 @@ class TestScalarIndexing(TestCase): a[:1:-1] = a[2::-1] assert_equal(a, [0, 1, 0, 1, 2]) + class TestCreation(TestCase): def test_from_attribute(self): class x(object): @@ -583,7 +588,7 @@ class TestCreation(TestCase): @dec.slow def test_zeros_big(self): - # test big array as they might be allocated different by the sytem + # 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) @@ -867,7 +872,7 @@ class TestStructured(TestCase): b[0]['x'] = np.arange(3) assert_equal(b[0]['x'], np.arange(3)) - #check that broadcasting check still works + # check that broadcasting check still works c = np.zeros(1, dtype=[('x', 'O', 5)]) def testassign(): @@ -875,6 +880,7 @@ class TestStructured(TestCase): assert_raises(ValueError, testassign) + class TestBool(TestCase): def test_test_interning(self): a0 = np.bool_(0) @@ -933,7 +939,107 @@ class TestBool(TestCase): a[:o] = False self.assertEqual(np.count_nonzero(a), builtins.sum(a.tolist())) + class TestMethods(TestCase): + 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]]) + + 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.uint16, np.int32, np.uint32, + np.float32, np.float64, np.complex64, np.complex128]: + a = np.array(ba, ctype) + a2 = np.array(ba2, ctype) + if ctype in ['1', 'b']: + self.assertRaises(ArithmeticError, a.prod) + self.assertRaises(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]]) + + 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) @@ -951,6 +1057,13 @@ class TestMethods(TestCase): 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,)) + 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]]) @@ -960,7 +1073,7 @@ class TestMethods(TestCase): def test_sort(self): # test ordering for floats and complex containing nans. It is only - # necessary to check the lessthan comparison, so sorts that + # 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. @@ -1198,7 +1311,7 @@ class TestMethods(TestCase): assert_equal(b.copy().argsort(kind=kind), b, msg) # test complex argsorts. These use the same code as the scalars - # but the compare fuction differs. + # but the compare function differs. ai = a*1j + 1 bi = b*1j + 1 for kind in ['q', 'm', 'h']: @@ -1292,8 +1405,6 @@ class TestMethods(TestCase): assert_equal(a.copy().argsort(axis=0), b) assert_equal(a.copy().argsort(axis=1), c) assert_equal(a.copy().argsort(), c) - # using None is known fail at this point - #assert_equal(a.copy().argsort(axis=None, c) # check axis handling for multidimensional empty arrays a = np.array([]) @@ -1440,7 +1551,7 @@ class TestMethods(TestCase): 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100197_1', 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100198_1', 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100199_1'], - dtype=np.unicode) + dtype=np.unicode) ind = np.arange(len(a)) assert_equal([a.searchsorted(v, 'left') for v in a], ind) assert_equal([a.searchsorted(v, 'right') for v in a], ind + 1) @@ -1596,7 +1707,7 @@ class TestMethods(TestCase): 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)) + 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)) @@ -1637,13 +1748,13 @@ class TestMethods(TestCase): d[i:].partition(0, kind=k) assert_array_equal(d, tgt) - d = np.ones((50)) + 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)) + d = np.arange(49) self.assertEqual(np.partition(d, 5, kind=k)[5], 5) self.assertEqual(np.partition(d, 15, kind=k)[15], 15) assert_array_equal(d[np.argpartition(d, 5, kind=k)], @@ -1652,7 +1763,7 @@ class TestMethods(TestCase): np.partition(d, 15, kind=k)) # rsorted - d = np.arange((47))[::-1] + d = np.arange(47)[::-1] self.assertEqual(np.partition(d, 6, kind=k)[6], 6) self.assertEqual(np.partition(d, 16, kind=k)[16], 16) assert_array_equal(d[np.argpartition(d, 6, kind=k)], @@ -1690,8 +1801,8 @@ class TestMethods(TestCase): assert_(np.isnan(np.partition(d, (2, -1))[-1])) # equal elements - d = np.arange((47)) % 7 - tgt = np.sort(np.arange((47)) % 7) + d = np.arange(47) % 7 + tgt = np.sort(np.arange(47) % 7) np.random.shuffle(d) for i in range(d.size): self.assertEqual(np.partition(d, i, kind=k)[i], tgt[i]) @@ -1743,7 +1854,7 @@ class TestMethods(TestCase): 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 s in (9, 16)] for dt, s in td: aae = assert_array_equal at = self.assertTrue @@ -1773,15 +1884,14 @@ class TestMethods(TestCase): 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)) + 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:])) + 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,:]]) + np.arange(d0.shape[1])[None, :]]) # check inplace dc = d.copy() @@ -2153,6 +2263,12 @@ class TestMethods(TestCase): a.flags.writeable = False assert_raises(ValueError, a.put, [1, 3, 5], [1, 3, 5]) + # 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) + def test_ravel(self): a = np.array([[0, 1], [2, 3]]) assert_equal(a.ravel(), [0, 1, 2, 3]) @@ -2778,7 +2894,7 @@ class TestPickling(TestCase): def test_version0_object(self): s = '\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x02\x85cnumpy\ndtype\nq\x04U\x02O8K\x00K\x01\x87Rq\x05(U\x01|NNJ\xff\xff\xff\xffJ\xff\xff\xff\xfftb\x89]q\x06(}q\x07U\x01aK\x01s}q\x08U\x01bK\x02setb.' - a = np.array([{'a':1}, {'b':2}]) + a = np.array([{'a': 1}, {'b': 2}]) p = self._loads(asbytes(s)) assert_equal(a, p) @@ -2797,7 +2913,7 @@ class TestPickling(TestCase): def test_version1_object(self): s = '\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x01K\x02\x85cnumpy\ndtype\nq\x04U\x02O8K\x00K\x01\x87Rq\x05(K\x01U\x01|NNJ\xff\xff\xff\xffJ\xff\xff\xff\xfftb\x89]q\x06(}q\x07U\x01aK\x01s}q\x08U\x01bK\x02setb.' - a = np.array([{'a':1}, {'b':2}]) + a = np.array([{'a': 1}, {'b': 2}]) p = self._loads(asbytes(s)) assert_equal(a, p) @@ -2815,7 +2931,7 @@ class TestFancyIndexing(TestCase): assert_array_equal(x, np.array([[2.0]])) x = np.ones((1, 1, 1)) - x[:,:, [0]] = 2.0 + x[:, :, [0]] = 2.0 assert_array_equal(x, np.array([[[2.0]]])) def test_tuple(self): @@ -2823,7 +2939,7 @@ class TestFancyIndexing(TestCase): x[:, (0,)] = 2.0 assert_array_equal(x, np.array([[2.0]])) x = np.ones((1, 1, 1)) - x[:,:, (0,)] = 2.0 + x[:, :, (0,)] = 2.0 assert_array_equal(x, np.array([[[2.0]]])) def test_mask(self): @@ -2957,11 +3073,6 @@ class TestArgmax(TestCase): ([False, False, False, True, False], 3), ([True, False, False, False, False], 0), ([True, False, True, False, False], 0), - - # Can't reduce a "flexible type" - #(['a', 'z', 'aa', 'zz'], 3), - #(['zz', 'a', 'aa', 'a'], 0), - #(['aa', 'z', 'zz', 'a'], 2), ] def test_all(self): @@ -2990,7 +3101,7 @@ class TestArgmax(TestCase): # these could be relaxed possibly (used to allow even the previous) out = np.ones((1, 10), dtype=np.int_) - assert_raises(ValueError, a.argmax, -1, np.ones((1, 10))) + assert_raises(ValueError, a.argmax, -1, out) out = np.ones(10, dtype=np.int_) a.argmax(-1, out=out) @@ -3005,13 +3116,13 @@ class TestArgmax(TestCase): # make sure both ndarray.argmax and numpy.argmax support out/axis args a = np.random.normal(size=(2,3)) - #check positional args + # check positional args out1 = np.zeros(2, dtype=int) out2 = np.zeros(2, dtype=int) assert_equal(a.argmax(1, out1), np.argmax(a, 1, out2)) assert_equal(out1, out2) - #check keyword args + # check keyword args out1 = np.zeros(3, dtype=int) out2 = np.zeros(3, dtype=int) assert_equal(a.argmax(out=out1, axis=0), np.argmax(a, out=out2, axis=0)) @@ -3091,11 +3202,6 @@ class TestArgmin(TestCase): ([True, True, True, False, True], 3), ([False, True, True, True, True], 0), ([False, True, False, True, True], 0), - - # Can't reduce a "flexible type" - #(['a', 'z', 'aa', 'zz'], 0), - #(['zz', 'a', 'aa', 'a'], 1), - #(['aa', 'z', 'zz', 'a'], 3), ] def test_all(self): @@ -3138,7 +3244,7 @@ class TestArgmin(TestCase): # these could be relaxed possibly (used to allow even the previous) out = np.ones((1, 10), dtype=np.int_) - assert_raises(ValueError, a.argmin, -1, np.ones((1, 10))) + assert_raises(ValueError, a.argmin, -1, out) out = np.ones(10, dtype=np.int_) a.argmin(-1, out=out) @@ -3151,15 +3257,15 @@ class TestArgmin(TestCase): def test_np_vs_ndarray(self): # make sure both ndarray.argmin and numpy.argmin support out/axis args - a = np.random.normal(size=(2,3)) + a = np.random.normal(size=(2, 3)) - #check positional args + # check positional args out1 = np.zeros(2, dtype=int) out2 = np.ones(2, dtype=int) assert_equal(a.argmin(1, out1), np.argmin(a, 1, out2)) assert_equal(out1, out2) - #check keyword args + # check keyword args out1 = np.zeros(3, dtype=int) out2 = np.ones(3, dtype=int) assert_equal(a.argmin(out=out1, axis=0), np.argmin(a, out=out2, axis=0)) @@ -3281,6 +3387,29 @@ class TestClip(TestCase): assert_(np.all(x <= 4)) +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) + + class TestPutmask(object): def tst_basic(self, x, T, mask, val): np.putmask(x, mask, val) @@ -3324,12 +3453,6 @@ class TestPutmask(object): assert_array_equal(rec['y'], [11, 4]) assert_array_equal(rec['z'], [3, 3]) - def test_masked_array(self): - ## x = np.array([1,2,3]) - ## z = np.ma.array(x,mask=[True,False,False]) - ## np.putmask(z,[True,True,True],3) - pass - class TestTake(object): def tst_basic(self, x): @@ -3774,6 +3897,7 @@ class TestFlat(TestCase): assert_(f.flags.updateifcopy is True) assert_(f.base is self.b0) + class TestResize(TestCase): def test_basic(self): x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) @@ -3919,7 +4043,7 @@ class TestRecord(TestCase): assert_equal(b[fn3][sfn1], 1) assert_raises(ValueError, b[fn3].__setitem__, fnn, 1) assert_raises(ValueError, b[fn3].__getitem__, fnn) - # multiple Subfields + # multiple subfields fn2 = func('f2') b[fn2] = 3 assert_equal(b[['f1', 'f2']][0].tolist(), (2, 3)) @@ -4010,15 +4134,19 @@ class TestView(TestCase): assert_array_equal(y, z) assert_array_equal(y, [67305985, 134678021]) + def _mean(a, **args): return a.mean(**args) + def _var(a, **args): return a.var(**args) + def _std(a, **args): return a.std(**args) + class TestStats(TestCase): funcs = [_mean, _var, _std] @@ -4111,7 +4239,7 @@ class TestStats(TestCase): # this needs definition as there are lots places along the line # where type casting may take place. - #for f in self.funcs: + # for f in self.funcs: # for c in np.typecodes['AllInteger']: # tgt = np.dtype(c).type # res = f(mat, axis=1, dtype=c).dtype.type @@ -4867,6 +4995,7 @@ if sys.version_info[:2] >= (3, 5): exec_ = getattr(builtins, "exec") assert_raises(TypeError, exec_, "a @= b", globals(), locals()) + class TestInner(TestCase): def test_inner_type_mismatch(self): @@ -4979,6 +5108,25 @@ class TestSummarization(TestCase): ' [ 501, 502, 503, ..., 999, 1000, 1001]])' assert_(repr(A) == reprA) + +class TestAlen(TestCase): + def test_basic(self): + m = np.array([1, 2, 3]) + self.assertEqual(np.alen(m), 3) + + m = np.array([[1, 2, 3], [4, 5, 7]]) + self.assertEqual(np.alen(m), 2) + + m = [1, 2, 3] + self.assertEqual(np.alen(m), 3) + + m = [[1, 2, 3], [4, 5, 7]] + self.assertEqual(np.alen(m), 2) + + def test_singleton(self): + self.assertEqual(np.alen(5), 1) + + class TestChoose(TestCase): def setUp(self): self.x = 2*np.ones((3,), dtype=int) @@ -5000,8 +5148,47 @@ class TestChoose(TestCase): assert_equal(A, [[2, 2, 3], [2, 2, 3]]) +class TestRepeat(TestCase): + def setUp(self): + self.m = np.array([1, 2, 3, 4, 5, 6]) + self.m_rect = self.m.reshape((2, 3)) + + def test_basic(self): + A = np.repeat(self.m, [1, 3, 2, 1, 1, 2]) + assert_equal(A, [1, 2, 2, 2, 3, + 3, 4, 5, 6, 6]) + + def test_broadcast1(self): + A = np.repeat(self.m, 2) + assert_equal(A, [1, 1, 2, 2, 3, 3, + 4, 4, 5, 5, 6, 6]) + + def test_axis_spec(self): + A = np.repeat(self.m_rect, [2, 1], axis=0) + assert_equal(A, [[1, 2, 3], + [1, 2, 3], + [4, 5, 6]]) + + A = np.repeat(self.m_rect, [1, 3, 2], axis=1) + assert_equal(A, [[1, 2, 2, 2, 3, 3], + [4, 5, 5, 5, 6, 6]]) + + def test_broadcast2(self): + A = np.repeat(self.m_rect, 2, axis=0) + assert_equal(A, [[1, 2, 3], + [1, 2, 3], + [4, 5, 6], + [4, 5, 6]]) + + A = np.repeat(self.m_rect, 2, axis=1) + assert_equal(A, [[1, 1, 2, 2, 3, 3], + [4, 4, 5, 5, 6, 6]]) + + # TODO: test for multidimensional NEIGH_MODE = {'zero': 0, 'one': 1, 'constant': 2, 'circular': 3, 'mirror': 4} + + class TestNeighborhoodIter(TestCase): # Simple, 2d tests def _test_simple2d(self, dt): @@ -5266,6 +5453,7 @@ class TestWarnings(object): assert_raises(np.ComplexWarning, x.__setitem__, slice(None), y) assert_equal(x, [1, 2]) + class TestMinScalarType(object): def test_usigned_shortshort(self): @@ -5299,6 +5487,7 @@ if sys.version_info[:2] == (2, 6): from numpy.core._internal import _dtype_from_pep3118 + class TestPEP3118Dtype(object): def _check(self, spec, wanted): dt = np.dtype(wanted) @@ -5381,6 +5570,7 @@ class TestPEP3118Dtype(object): self._check('(3)T{ix}', ({'f0': ('i', 0), '': (VV(1), 4)}, (3,))) + class TestNewBufferProtocol(object): def _check_roundtrip(self, obj): obj = np.asarray(obj) @@ -5762,10 +5952,12 @@ def test_flat_element_deletion(): except: raise AssertionError + def test_scalar_element_deletion(): a = np.zeros(2, dtype=[('x', 'int'), ('y', 'int')]) assert_raises(ValueError, a[0].__delitem__, 'x') + class TestMemEventHook(TestCase): def test_mem_seteventhook(self): # The actual tests are within the C code in @@ -5818,7 +6010,7 @@ class TestAsCArray(TestCase): class TestConversion(TestCase): def test_array_scalar_relational_operation(self): - #All integer + # All integer for dt1 in np.typecodes['AllInteger']: assert_(1 > np.array(0, dtype=dt1), "type %s failed" % (dt1,)) assert_(not 1 < np.array(0, dtype=dt1), "type %s failed" % (dt1,)) @@ -5829,13 +6021,13 @@ class TestConversion(TestCase): assert_(not np.array(1, dtype=dt1) < np.array(0, dtype=dt2), "type %s and %s failed" % (dt1, dt2)) - #Unsigned integers + # Unsigned integers for dt1 in 'BHILQP': assert_(-1 < np.array(1, dtype=dt1), "type %s failed" % (dt1,)) assert_(not -1 > np.array(1, dtype=dt1), "type %s failed" % (dt1,)) assert_(-1 != np.array(1, dtype=dt1), "type %s failed" % (dt1,)) - #unsigned vs signed + # Unsigned vs signed for dt2 in 'bhilqp': assert_(np.array(1, dtype=dt1) > np.array(-1, dtype=dt2), "type %s and %s failed" % (dt1, dt2)) @@ -5844,7 +6036,7 @@ class TestConversion(TestCase): assert_(np.array(1, dtype=dt1) != np.array(-1, dtype=dt2), "type %s and %s failed" % (dt1, dt2)) - #Signed integers and floats + # Signed integers and floats for dt1 in 'bhlqp' + np.typecodes['Float']: assert_(1 > np.array(-1, dtype=dt1), "type %s failed" % (dt1,)) assert_(not 1 < np.array(-1, dtype=dt1), "type %s failed" % (dt1,)) @@ -5902,7 +6094,8 @@ class TestWhere(TestCase): e = np.array(['5z', '0l', nan, 'Wz', nan, nan, 'Xq', 'cs', nan, nan, 'QN', nan, nan, 'Fd', nan, nan, 'kp', nan, '36', 'i1'], dtype=object) - m = np.array([0,0,1,0,1,1,0,0,1,1,0,1,1,0,1,1,0,1,0,0], dtype=bool) + m = np.array([0, 0, 1, 0, 1, 1, 0, 0, 1, 1, + 0, 1, 1, 0, 1, 1, 0, 1, 0, 0], dtype=bool) r = e[:] r[np.where(m)] = d[np.where(m)] @@ -5971,7 +6164,7 @@ class TestWhere(TestCase): dtype=np.float64) a = np.ones(1, dtype='>i4') b = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.], - dtype=np.float64) + dtype=np.float64) assert_equal(np.where(c, a, b), r) b = b.astype('>f8') diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index dafdbc48b..34be84135 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -44,19 +44,41 @@ class TestResize(TestCase): class TestNonarrayArgs(TestCase): # check that non-array arguments to functions wrap them in arrays - def test_squeeze(self): - A = [[[1, 1, 1], [2, 2, 2], [3, 3, 3]]] - assert_(np.squeeze(A).shape == (3, 3)) + def test_choose(self): + choices = [[0, 1, 2], + [3, 4, 5], + [5, 6, 7]] + tgt = [5, 1, 5] + a = [2, 0, 1] + + out = np.choose(a, choices) + assert_equal(out, tgt) + + def test_clip(self): + arr = [-1, 5, 2, 3, 10, -4, -9] + out = np.clip(arr, 2, 7) + tgt = [2, 5, 2, 3, 7, 2, 2] + assert_equal(out, tgt) + + def test_compress(self): + arr = [[0, 1, 2, 3, 4], + [5, 6, 7, 8, 9]] + tgt = [[5, 6, 7, 8, 9]] + out = np.compress([0, 1], arr, axis=0) + assert_equal(out, tgt) def test_cumproduct(self): A = [[1, 2, 3], [4, 5, 6]] assert_(np.all(np.cumproduct(A) == np.array([1, 2, 6, 24, 120, 720]))) - def test_size(self): - A = [[1, 2, 3], [4, 5, 6]] - assert_(np.size(A) == 6) - assert_(np.size(A, 0) == 2) - assert_(np.size(A, 1) == 3) + def test_diagonal(self): + a = [[0, 1, 2, 3], + [4, 5, 6, 7], + [8, 9, 10, 11]] + out = np.diagonal(a) + tgt = [0, 5, 10] + + assert_equal(out, tgt) def test_mean(self): A = [[1, 2, 3], [4, 5, 6]] @@ -69,6 +91,55 @@ class TestNonarrayArgs(TestCase): assert_(np.isnan(np.mean([]))) assert_(w[0].category is RuntimeWarning) + def test_ptp(self): + a = [3, 4, 5, 10, -3, -5, 6.0] + assert_equal(np.ptp(a, axis=0), 15.0) + + def test_prod(self): + arr = [[1, 2, 3, 4], + [5, 6, 7, 9], + [10, 3, 4, 5]] + tgt = [24, 1890, 600] + + assert_equal(np.prod(arr, axis=-1), tgt) + + def test_ravel(self): + a = [[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(np.ravel(a), tgt) + + def test_repeat(self): + a = [1, 2, 3] + tgt = [1, 1, 2, 2, 3, 3] + + out = np.repeat(a, 2) + assert_equal(out, tgt) + + def test_reshape(self): + arr = [[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(np.reshape(arr, (2, 6)), tgt) + + def test_round(self): + arr = [1.56, 72.54, 6.35, 3.25] + tgt = [1.6, 72.5, 6.4, 3.2] + assert_equal(np.around(arr, decimals=1), tgt) + + def test_searchsorted(self): + arr = [-8, -5, -1, 3, 6, 10] + out = np.searchsorted(arr, 0) + assert_equal(out, 3) + + def test_size(self): + A = [[1, 2, 3], [4, 5, 6]] + assert_(np.size(A) == 6) + assert_(np.size(A, 0) == 2) + assert_(np.size(A, 1) == 3) + + def test_squeeze(self): + A = [[[1, 1, 1], [2, 2, 2], [3, 3, 3]]] + assert_(np.squeeze(A).shape == (3, 3)) + def test_std(self): A = [[1, 2, 3], [4, 5, 6]] assert_almost_equal(np.std(A), 1.707825127659933) @@ -80,6 +151,38 @@ class TestNonarrayArgs(TestCase): assert_(np.isnan(np.std([]))) assert_(w[0].category is RuntimeWarning) + def test_swapaxes(self): + tgt = [[[0, 4], [2, 6]], [[1, 5], [3, 7]]] + a = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]] + out = np.swapaxes(a, 0, 2) + assert_equal(out, tgt) + + def test_sum(self): + m = [[1, 2, 3], + [4, 5, 6], + [7, 8, 9]] + tgt = [[6], [15], [24]] + out = np.sum(m, axis=1, keepdims=True) + + assert_equal(tgt, out) + + def test_take(self): + tgt = [2, 3, 5] + indices = [1, 2, 4] + a = [1, 2, 3, 4, 5] + + out = np.take(a, indices) + assert_equal(out, tgt) + + def test_trace(self): + c = [[1, 2], [3, 4], [5, 6]] + assert_equal(np.trace(c), 5) + + def test_transpose(self): + arr = [[1, 2], [3, 4], [5, 6]] + tgt = [[1, 3, 5], [2, 4, 6]] + assert_equal(np.transpose(arr, (1, 0)), tgt) + def test_var(self): A = [[1, 2, 3], [4, 5, 6]] assert_almost_equal(np.var(A), 2.9166666666666665) @@ -259,7 +362,6 @@ class TestBoolCmp(TestCase): self.signf[4::6][self.ef[4::6]] = -0. self.signd[4::6][self.ed[4::6]] = -0. - def test_float(self): # offset for alignment test for i in range(4): @@ -279,7 +381,7 @@ class TestBoolCmp(TestCase): assert_array_equal(r2.view(np.int8), r2.astype(np.int8)) assert_array_equal(r3.view(np.int8), r3.astype(np.int8)) - # isnan on amd64 takes the same codepath + # isnan on amd64 takes the same code path assert_array_equal(np.isnan(self.nf[i:]), self.ef[i:]) assert_array_equal(np.isfinite(self.nf[i:]), ~self.ef[i:]) assert_array_equal(np.isfinite(self.inff[i:]), ~self.ef[i:]) @@ -305,7 +407,7 @@ class TestBoolCmp(TestCase): assert_array_equal(r2.view(np.int8), r2.astype(np.int8)) assert_array_equal(r3.view(np.int8), r3.astype(np.int8)) - # isnan on amd64 takes the same codepath + # isnan on amd64 takes the same code path assert_array_equal(np.isnan(self.nd[i:]), self.ed[i:]) assert_array_equal(np.isfinite(self.nd[i:]), ~self.ed[i:]) assert_array_equal(np.isfinite(self.infd[i:]), ~self.ed[i:]) @@ -385,7 +487,7 @@ class TestSeterr(TestCase): # set errobj to something non default np.seterrobj([umath.UFUNC_BUFSIZE_DEFAULT, umath.ERR_DEFAULT + 1, None]) - #call a ufunc + # call a ufunc np.isnan(np.array([6])) # same with the default, lots of times to get rid of possible # pre-existing stack in the code @@ -413,7 +515,7 @@ class TestFloatExceptions(TestCase): # # Given a floating operation `flop` and two scalar values, check that # the operation raises the floating point exception specified by - #`fpeerr`. Tests all variants with 0-d array scalars as well. + # `fpeerr`. Tests all variants with 0-d array scalars as well. self.assert_raises_fpe(fpeerr, flop, sc1, sc2) self.assert_raises_fpe(fpeerr, flop, sc1[()], sc2) @@ -449,31 +551,31 @@ class TestFloatExceptions(TestCase): invalid = 'invalid' self.assert_raises_fpe(underflow, - lambda a, b:a/b, ft_tiny, ft_max) + lambda a, b: a/b, ft_tiny, ft_max) self.assert_raises_fpe(underflow, - lambda a, b:a*b, ft_tiny, ft_tiny) + lambda a, b: a*b, ft_tiny, ft_tiny) self.assert_raises_fpe(overflow, - lambda a, b:a*b, ft_max, ftype(2)) + lambda a, b: a*b, ft_max, ftype(2)) self.assert_raises_fpe(overflow, - lambda a, b:a/b, ft_max, ftype(0.5)) + lambda a, b: a/b, ft_max, ftype(0.5)) self.assert_raises_fpe(overflow, - lambda a, b:a+b, ft_max, ft_max*ft_eps) + lambda a, b: a+b, ft_max, ft_max*ft_eps) self.assert_raises_fpe(overflow, - lambda a, b:a-b, -ft_max, ft_max*ft_eps) + lambda a, b: a-b, -ft_max, ft_max*ft_eps) self.assert_raises_fpe(overflow, - np.power, ftype(2), ftype(2**fi.nexp)) + np.power, ftype(2), ftype(2**fi.nexp)) self.assert_raises_fpe(divbyzero, - lambda a, b:a/b, ftype(1), ftype(0)) + lambda a, b: a/b, ftype(1), ftype(0)) self.assert_raises_fpe(invalid, - lambda a, b:a/b, ftype(np.inf), ftype(np.inf)) + lambda a, b: a/b, ftype(np.inf), ftype(np.inf)) self.assert_raises_fpe(invalid, - lambda a, b:a/b, ftype(0), ftype(0)) + lambda a, b: a/b, ftype(0), ftype(0)) self.assert_raises_fpe(invalid, - lambda a, b:a-b, ftype(np.inf), ftype(np.inf)) + lambda a, b: a-b, ftype(np.inf), ftype(np.inf)) self.assert_raises_fpe(invalid, - lambda a, b:a+b, ftype(np.inf), ftype(-np.inf)) + lambda a, b: a+b, ftype(np.inf), ftype(-np.inf)) self.assert_raises_fpe(invalid, - lambda a, b:a*b, ftype(0), ftype(np.inf)) + lambda a, b: a*b, ftype(0), ftype(np.inf)) def test_warnings(self): # test warning code path @@ -496,7 +598,7 @@ class TestFloatExceptions(TestCase): class TestTypes(TestCase): def check_promotion_cases(self, promote_func): - #Tests that the scalars get coerced correctly. + # tests that the scalars get coerced correctly. b = np.bool_(0) i8, i16, i32, i64 = np.int8(0), np.int16(0), np.int32(0), np.int64(0) u8, u16, u32, u64 = np.uint8(0), np.uint16(0), np.uint32(0), np.uint64(0) @@ -582,7 +684,7 @@ class TestTypes(TestCase): 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) + "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 @@ -594,7 +696,7 @@ class TestTypes(TestCase): 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) + "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 @@ -603,22 +705,25 @@ class TestTypes(TestCase): # The following use-case is problematic, and to resolve its # tricky side-effects requires more changes. # - ## Use-case: (1-t)*a, where 't' is a boolean array and 'a' is - ## a float32, shouldn't promote to float64 - #a = np.array([1.0, 1.5], dtype=np.float32) - #t = np.array([True, False]) - #b = t*a - #assert_equal(b, [1.0, 0.0]) - #assert_equal(b.dtype, np.dtype('f4')) - #b = (1-t)*a - #assert_equal(b, [0.0, 1.5]) - #assert_equal(b.dtype, np.dtype('f4')) - ## Probably ~t (bitwise negation) is more proper to use here, - ## but this is arguably less intuitive to understand at a glance, and - ## would fail if 't' is actually an integer array instead of boolean: - #b = (~t)*a - #assert_equal(b, [0.0, 1.5]) - #assert_equal(b.dtype, np.dtype('f4')) + # Use-case: (1-t)*a, where 't' is a boolean array and 'a' is + # a float32, shouldn't promote to float64 + # + # a = np.array([1.0, 1.5], dtype=np.float32) + # t = np.array([True, False]) + # b = t*a + # assert_equal(b, [1.0, 0.0]) + # assert_equal(b.dtype, np.dtype('f4')) + # b = (1-t)*a + # assert_equal(b, [0.0, 1.5]) + # assert_equal(b.dtype, np.dtype('f4')) + # + # Probably ~t (bitwise negation) is more proper to use here, + # but this is arguably less intuitive to understand at a glance, and + # would fail if 't' is actually an integer array instead of boolean: + # + # b = (~t)*a + # assert_equal(b, [0.0, 1.5]) + # assert_equal(b.dtype, np.dtype('f4')) def test_result_type(self): self.check_promotion_cases(np.result_type) @@ -831,7 +936,7 @@ class TestNonzero(TestCase): assert_equal(np.nonzero(x), ([0, 2, 3, 6],)) x = np.array([(1, 2), (0, 0), (1, 1), (-1, 3), (0, 7)], - dtype=[('a', 'i4'), ('b', 'i2')]) + dtype=[('a', 'i4'), ('b', 'i2')]) assert_equal(np.count_nonzero(x['a']), 3) assert_equal(np.count_nonzero(x['b']), 4) assert_equal(np.nonzero(x['a']), ([0, 2, 3],)) @@ -886,6 +991,14 @@ class TestNonzero(TestCase): assert_(type(nzx_i) is np.ndarray) assert_(nzx_i.flags.writeable) + # Tests that the array method + # call works + def test_array_method(self): + m = np.array([[1, 0, 0], [4, 0, 6]]) + tgt = [[0, 1, 1], [0, 0, 2]] + + assert_equal(m.nonzero(), tgt) + class TestIndex(TestCase): def test_boolean(self): @@ -931,6 +1044,7 @@ class TestBaseRepr(TestCase): assert_equal(np.base_repr(-12, 10, 4), '-000012') assert_equal(np.base_repr(-12, 4), '-30') + class TestArrayComparisons(TestCase): def test_array_equal(self): res = np.array_equal(np.array([1, 2]), np.array([1, 2])) @@ -1049,7 +1163,7 @@ class TestClip(TestCase): # Now the real test cases def test_simple_double(self): - #Test native double input with scalar min/max. + # Test native double input with scalar min/max. a = self._generate_data(self.nr, self.nc) m = 0.1 M = 0.6 @@ -1058,7 +1172,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_simple_int(self): - #Test native int input with scalar min/max. + # Test native int input with scalar min/max. a = self._generate_int_data(self.nr, self.nc) a = a.astype(int) m = -2 @@ -1068,7 +1182,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_array_double(self): - #Test native double input with array min/max. + # Test native double input with array min/max. a = self._generate_data(self.nr, self.nc) m = np.zeros(a.shape) M = m + 0.5 @@ -1077,8 +1191,8 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_simple_nonnative(self): - #Test non native double input with scalar min/max. - #Test native double input with non native double scalar min/max. + # Test non native double input with scalar min/max. + # Test native double input with non native double scalar min/max. a = self._generate_non_native_data(self.nr, self.nc) m = -0.5 M = 0.6 @@ -1086,7 +1200,7 @@ class TestClip(TestCase): act = self.clip(a, m, M) assert_array_equal(ac, act) - #Test native double input with non native double scalar min/max. + # Test native double input with non native double scalar min/max. a = self._generate_data(self.nr, self.nc) m = -0.5 M = self._neg_byteorder(0.6) @@ -1096,8 +1210,8 @@ class TestClip(TestCase): assert_array_equal(ac, act) def test_simple_complex(self): - #Test native complex input with native double scalar min/max. - #Test native input with complex double scalar min/max. + # Test native complex input with native double scalar min/max. + # Test native input with complex double scalar min/max. a = 3 * self._generate_data_complex(self.nr, self.nc) m = -0.5 M = 1. @@ -1105,7 +1219,7 @@ class TestClip(TestCase): act = self.clip(a, m, M) assert_array_strict_equal(ac, act) - #Test native input with complex double scalar min/max. + # Test native input with complex double scalar min/max. a = 3 * self._generate_data(self.nr, self.nc) m = -0.5 + 1.j M = 1. + 2.j @@ -1126,7 +1240,7 @@ class TestClip(TestCase): assert_array_strict_equal(aM, a) def test_clip_non_contig(self): - #Test clip for non contiguous native input and native scalar min/max. + # Test clip for non contiguous native input and native scalar min/max. a = self._generate_data(self.nr * 2, self.nc * 3) a = a[::2, ::3] assert_(not a.flags['F_CONTIGUOUS']) @@ -1136,7 +1250,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_simple_out(self): - #Test native double input with scalar min/max. + # Test native double input with scalar min/max. a = self._generate_data(self.nr, self.nc) m = -0.5 M = 0.6 @@ -1147,7 +1261,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_simple_int32_inout(self): - #Test native int32 input with double min/max and int32 out. + # Test native int32 input with double min/max and int32 out. a = self._generate_int32_data(self.nr, self.nc) m = np.float64(0) M = np.float64(2) @@ -1158,7 +1272,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_simple_int64_out(self): - #Test native int32 input with int32 scalar min/max and int64 out. + # Test native int32 input with int32 scalar min/max and int64 out. a = self._generate_int32_data(self.nr, self.nc) m = np.int32(-1) M = np.int32(1) @@ -1169,7 +1283,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_simple_int64_inout(self): - #Test native int32 input with double array min/max and int32 out. + # Test native int32 input with double array min/max and int32 out. a = self._generate_int32_data(self.nr, self.nc) m = np.zeros(a.shape, np.float64) M = np.float64(1) @@ -1180,7 +1294,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_simple_int32_out(self): - #Test native double input with scalar min/max and int out. + # Test native double input with scalar min/max and int out. a = self._generate_data(self.nr, self.nc) m = -1.0 M = 2.0 @@ -1191,7 +1305,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_simple_inplace_01(self): - #Test native double input with array min/max in-place. + # Test native double input with array min/max in-place. a = self._generate_data(self.nr, self.nc) ac = a.copy() m = np.zeros(a.shape) @@ -1201,7 +1315,7 @@ class TestClip(TestCase): assert_array_strict_equal(a, ac) def test_simple_inplace_02(self): - #Test native double input with scalar min/max in-place. + # Test native double input with scalar min/max in-place. a = self._generate_data(self.nr, self.nc) ac = a.copy() m = -0.5 @@ -1211,7 +1325,7 @@ class TestClip(TestCase): assert_array_strict_equal(a, ac) def test_noncontig_inplace(self): - #Test non contiguous double input with double scalar min/max in-place. + # Test non contiguous double input with double scalar min/max in-place. a = self._generate_data(self.nr * 2, self.nc * 3) a = a[::2, ::3] assert_(not a.flags['F_CONTIGUOUS']) @@ -1224,7 +1338,7 @@ class TestClip(TestCase): assert_array_equal(a, ac) def test_type_cast_01(self): - #Test native double input with scalar min/max. + # Test native double input with scalar min/max. a = self._generate_data(self.nr, self.nc) m = -0.5 M = 0.6 @@ -1233,7 +1347,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_02(self): - #Test native int32 input with int32 scalar min/max. + # Test native int32 input with int32 scalar min/max. a = self._generate_int_data(self.nr, self.nc) a = a.astype(np.int32) m = -2 @@ -1243,7 +1357,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_03(self): - #Test native int32 input with float64 scalar min/max. + # Test native int32 input with float64 scalar min/max. a = self._generate_int32_data(self.nr, self.nc) m = -2 M = 4 @@ -1252,7 +1366,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_04(self): - #Test native int32 input with float32 scalar min/max. + # Test native int32 input with float32 scalar min/max. a = self._generate_int32_data(self.nr, self.nc) m = np.float32(-2) M = np.float32(4) @@ -1261,7 +1375,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_05(self): - #Test native int32 with double arrays min/max. + # Test native int32 with double arrays min/max. a = self._generate_int_data(self.nr, self.nc) m = -0.5 M = 1. @@ -1270,7 +1384,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_06(self): - #Test native with NON native scalar min/max. + # Test native with NON native scalar min/max. a = self._generate_data(self.nr, self.nc) m = 0.5 m_s = self._neg_byteorder(m) @@ -1280,7 +1394,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_07(self): - #Test NON native with native array min/max. + # Test NON native with native array min/max. a = self._generate_data(self.nr, self.nc) m = -0.5 * np.ones(a.shape) M = 1. @@ -1291,7 +1405,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_08(self): - #Test NON native with native scalar min/max. + # Test NON native with native scalar min/max. a = self._generate_data(self.nr, self.nc) m = -0.5 M = 1. @@ -1302,7 +1416,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_09(self): - #Test native with NON native array min/max. + # Test native with NON native array min/max. a = self._generate_data(self.nr, self.nc) m = -0.5 * np.ones(a.shape) M = 1. @@ -1313,7 +1427,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_10(self): - #Test native int32 with float min/max and float out for output argument. + # Test native int32 with float min/max and float out for output argument. a = self._generate_int_data(self.nr, self.nc) b = np.zeros(a.shape, dtype=np.float32) m = np.float32(-0.5) @@ -1323,7 +1437,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_type_cast_11(self): - #Test non native with native scalar, min/max, out non native + # Test non native with native scalar, min/max, out non native a = self._generate_non_native_data(self.nr, self.nc) b = a.copy() b = b.astype(b.dtype.newbyteorder('>')) @@ -1335,7 +1449,7 @@ class TestClip(TestCase): assert_array_strict_equal(b, bt) def test_type_cast_12(self): - #Test native int32 input and min/max and float out + # Test native int32 input and min/max and float out a = self._generate_int_data(self.nr, self.nc) b = np.zeros(a.shape, dtype=np.float32) m = np.int32(0) @@ -1345,7 +1459,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_clip_with_out_simple(self): - #Test native double input with scalar min/max + # Test native double input with scalar min/max a = self._generate_data(self.nr, self.nc) m = -0.5 M = 0.6 @@ -1356,7 +1470,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_clip_with_out_simple2(self): - #Test native int32 input with double min/max and int32 out + # Test native int32 input with double min/max and int32 out a = self._generate_int32_data(self.nr, self.nc) m = np.float64(0) M = np.float64(2) @@ -1367,7 +1481,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_clip_with_out_simple_int32(self): - #Test native int32 input with int32 scalar min/max and int64 out + # Test native int32 input with int32 scalar min/max and int64 out a = self._generate_int32_data(self.nr, self.nc) m = np.int32(-1) M = np.int32(1) @@ -1378,7 +1492,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_clip_with_out_array_int32(self): - #Test native int32 input with double array min/max and int32 out + # Test native int32 input with double array min/max and int32 out a = self._generate_int32_data(self.nr, self.nc) m = np.zeros(a.shape, np.float64) M = np.float64(1) @@ -1389,7 +1503,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_clip_with_out_array_outint32(self): - #Test native double input with scalar min/max and int out + # Test native double input with scalar min/max and int out a = self._generate_data(self.nr, self.nc) m = -1.0 M = 2.0 @@ -1400,7 +1514,7 @@ class TestClip(TestCase): assert_array_strict_equal(ac, act) def test_clip_inplace_array(self): - #Test native double input with array min/max + # Test native double input with array min/max a = self._generate_data(self.nr, self.nc) ac = a.copy() m = np.zeros(a.shape) @@ -1410,7 +1524,7 @@ class TestClip(TestCase): assert_array_strict_equal(a, ac) def test_clip_inplace_simple(self): - #Test native double input with scalar min/max + # Test native double input with scalar min/max a = self._generate_data(self.nr, self.nc) ac = a.copy() m = -0.5 @@ -1420,7 +1534,7 @@ class TestClip(TestCase): assert_array_strict_equal(a, ac) def test_clip_func_takes_out(self): - # Ensure that the clip() function takes an out= argument. + # Ensure that the clip() function takes an out=argument. a = self._generate_data(self.nr, self.nc) ac = a.copy() m = -0.5 @@ -1456,7 +1570,7 @@ class TestAllclose(object): assert_(not np.allclose(x, y), "%s and %s shouldn't be close" % (x, y)) def test_ip_allclose(self): - #Parametric test factory. + # Parametric test factory. arr = np.array([100, 1000]) aran = np.arange(125).reshape((5, 5, 5)) @@ -1476,7 +1590,7 @@ class TestAllclose(object): yield (self.tst_allclose, x, y) def test_ip_not_allclose(self): - #Parametric test factory. + # Parametric test factory. aran = np.arange(125).reshape((5, 5, 5)) atol = self.atol @@ -1714,7 +1828,7 @@ class TestStdVarComplex(TestCase): class TestCreationFuncs(TestCase): - #Test ones, zeros, empty and filled + # Test ones, zeros, empty and filled def setUp(self): self.dtypes = ('b', 'i', 'u', 'f', 'c', 'S', 'a', 'U', 'V') @@ -1902,6 +2016,7 @@ class TestLikeFuncs(TestCase): self.check_like_function(np.full_like, 123.456, True) self.check_like_function(np.full_like, np.inf, True) + class TestCorrelate(TestCase): def _setup(self, dt): self.x = np.array([1, 2, 3, 4, 5], dtype=dt) @@ -1966,6 +2081,7 @@ class TestConvolve(TestCase): assert_array_equal(d, np.ones(100)) assert_array_equal(k, np.ones(3)) + class TestArgwhere(object): def test_2D(self): x = np.arange(6).reshape((2, 3)) @@ -1978,7 +2094,9 @@ class TestArgwhere(object): def test_list(self): assert_equal(np.argwhere([4, 0, 2, 1, 3]), [[0], [2], [3], [4]]) + class TestStringFunction(object): + def test_set_string_function(self): a = np.array([1]) np.set_string_function(lambda x: "FOO", repr=True) @@ -1991,6 +2109,7 @@ class TestStringFunction(object): np.set_string_function(None, repr=False) assert_equal(str(a), "[1]") + class TestRoll(TestCase): def test_roll1d(self): x = np.arange(10) @@ -2299,7 +2418,7 @@ class TestRequire(object): class ArraySubclass(np.ndarray): pass - a = ArraySubclass((2,2)) + a = ArraySubclass((2, 2)) b = np.require(a, None, ['E']) assert_(type(b) is np.ndarray) @@ -2308,7 +2427,7 @@ class TestRequire(object): pass for flag in self.flag_names: - a = ArraySubclass((2,2)) + a = ArraySubclass((2, 2)) yield self.set_and_check_flag, flag, None, a diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 88a590517..d6a838f3a 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -255,7 +255,7 @@ class TestInsert(TestCase): assert_equal(insert(b, 0, b[0]), [0., 0., 1.]) assert_equal(insert(b, [], []), b) # Bools will be treated differently in the future: - #assert_equal(insert(a, np.array([True]*4), 9), [9,1,9,2,9,3,9]) + # assert_equal(insert(a, np.array([True]*4), 9), [9, 1, 9, 2, 9, 3, 9]) with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', FutureWarning) assert_equal( @@ -294,15 +294,15 @@ class TestInsert(TestCase): insert(a, 1, a[:, 2,:], axis=1)) # invalid axis value - assert_raises(IndexError, insert, a, 1, a[:, 2,:], axis=3) - assert_raises(IndexError, insert, a, 1, a[:, 2,:], axis=-4) + assert_raises(IndexError, insert, a, 1, a[:, 2, :], axis=3) + assert_raises(IndexError, insert, a, 1, a[:, 2, :], axis=-4) # negative axis value a = np.arange(24).reshape((2, 3, 4)) - assert_equal(insert(a, 1, a[:,:, 3], axis=-1), - insert(a, 1, a[:,:, 3], axis=2)) - assert_equal(insert(a, 1, a[:, 2,:], axis=-2), - insert(a, 1, a[:, 2,:], axis=1)) + assert_equal(insert(a, 1, a[:, :, 3], axis=-1), + insert(a, 1, a[:, :, 3], axis=2)) + assert_equal(insert(a, 1, a[:, 2, :], axis=-2), + insert(a, 1, a[:, 2, :], axis=1)) def test_0d(self): # This is an error in the future @@ -368,13 +368,13 @@ class TestAmin(TestCase): class TestPtp(TestCase): def test_basic(self): - a = [3, 4, 5, 10, -3, -5, 6.0] - assert_equal(np.ptp(a, axis=0), 15.0) - b = [[3, 6.0, 9.0], - [4, 10.0, 5.0], - [8, 3.0, 2.0]] - assert_equal(np.ptp(b, axis=0), [5.0, 7.0, 7.0]) - assert_equal(np.ptp(b, axis=-1), [6.0, 6.0, 6.0]) + a = np.array([3, 4, 5, 10, -3, -5, 6.0]) + assert_equal(a.ptp(axis=0), 15.0) + b = np.array([[3, 6.0, 9.0], + [4, 10.0, 5.0], + [8, 3.0, 2.0]]) + assert_equal(b.ptp(axis=0), [5.0, 7.0, 7.0]) + assert_equal(b.ptp(axis=-1), [6.0, 6.0, 6.0]) class TestCumsum(TestCase): @@ -411,12 +411,11 @@ class TestProd(TestCase): if ctype in ['1', 'b']: self.assertRaises(ArithmeticError, np.prod, a) self.assertRaises(ArithmeticError, np.prod, a2, 1) - self.assertRaises(ArithmeticError, np.prod, a) else: - assert_equal(np.prod(a, axis=0), 26400) - assert_array_equal(np.prod(a2, axis=0), + assert_equal(a.prod(axis=0), 26400) + assert_array_equal(a2.prod(axis=0), np.array([50, 36, 84, 180], ctype)) - assert_array_equal(np.prod(a2, axis=-1), + assert_array_equal(a2.prod(axis=-1), np.array([24, 1890, 600], ctype)) @@ -460,10 +459,10 @@ class TestDiff(TestCase): def test_nd(self): x = 20 * rand(10, 20, 30) - out1 = x[:,:, 1:] - x[:,:, :-1] - out2 = out1[:,:, 1:] - out1[:,:, :-1] - out3 = x[1:,:,:] - x[:-1,:,:] - out4 = out3[1:,:,:] - out3[:-1,:,:] + out1 = x[:, :, 1:] - x[:, :, :-1] + out2 = out1[:, :, 1:] - out1[:, :, :-1] + out3 = x[1:, :, :] - x[:-1, :, :] + out4 = out3[1:, :, :] - out3[:-1, :, :] assert_array_equal(diff(x), out1) assert_array_equal(diff(x, n=2), out2) assert_array_equal(diff(x, axis=0), out3) @@ -610,7 +609,7 @@ class TestGradient(TestCase): assert_array_equal(gradient(x, axis=0), dx[0]) assert_array_equal(gradient(x, axis=1), dx[1]) assert_array_equal(gradient(x, axis=-1), dx[1]) - assert_array_equal(gradient(x, axis=(1,0)), [dx[1], dx[0]]) + assert_array_equal(gradient(x, axis=(1, 0)), [dx[1], dx[0]]) # test axis=None which means all axes assert_almost_equal(gradient(x, axis=None), [dx[0], dx[1]]) @@ -618,7 +617,7 @@ class TestGradient(TestCase): assert_almost_equal(gradient(x, axis=None), gradient(x)) # test vararg order - assert_array_equal(gradient(x, 2, 3, axis=(1,0)), [dx[1]/2.0, dx[0]/3.0]) + assert_array_equal(gradient(x, 2, 3, axis=(1, 0)), [dx[1]/2.0, dx[0]/3.0]) # test maximal number of varargs assert_raises(SyntaxError, gradient, x, 1, 2, axis=1) @@ -1018,8 +1017,8 @@ class TestTrapz(TestCase): q = x[:, None, None] + y[None,:, None] + z[None, None,:] qx = (q * wx[:, None, None]).sum(axis=0) - qy = (q * wy[None,:, None]).sum(axis=1) - qz = (q * wz[None, None,:]).sum(axis=2) + qy = (q * wy[None, :, None]).sum(axis=1) + qz = (q * wz[None, None, :]).sum(axis=2) # n-d `x` r = trapz(q, x=x[:, None, None], axis=0) @@ -1501,14 +1500,12 @@ class TestHistogramdd(TestCase): assert_(hist[1] == 0.0) def test_finite_range(self): - vals = np.random.random((100,3)) - histogramdd(vals, range=[[0.0,1.0],[0.25,0.75],[0.25,0.5]]) + vals = np.random.random((100, 3)) + histogramdd(vals, range=[[0.0, 1.0], [0.25, 0.75], [0.25, 0.5]]) assert_raises(ValueError, histogramdd, vals, - range=[[0.0,1.0],[0.25,0.75],[0.25,np.inf]]) + range=[[0.0, 1.0], [0.25, 0.75], [0.25, np.inf]]) assert_raises(ValueError, histogramdd, vals, - range=[[0.0,1.0],[np.nan,0.75],[0.25,0.5]]) - - + range=[[0.0, 1.0], [np.nan, 0.75], [0.25, 0.5]]) class TestUnique(TestCase): |