diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2012-04-01 09:40:12 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2012-04-01 09:40:12 -0600 |
commit | 1957cb3c3574aaecf45d1512bddc10a0bcc39639 (patch) | |
tree | 10359bdaf478a95827ec940419b5119ac68e0d95 /numpy/lib/tests/test_function_base.py | |
parent | b9576ed22a57cb8c7bf04038c5792bb2b499f390 (diff) | |
download | numpy-1957cb3c3574aaecf45d1512bddc10a0bcc39639.tar.gz |
STY: Style cleanups in numpy/lib/tests/test_function_base.
Make imports from numpy.testing explicit.
Use np namespace.
Diffstat (limited to 'numpy/lib/tests/test_function_base.py')
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 485 |
1 files changed, 245 insertions, 240 deletions
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 67a5204a1..910fd5283 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1,27 +1,28 @@ import warnings - -from numpy.testing import * -import numpy.lib +import numpy as np +from numpy.testing import ( + run_module_suite, TestCase, assert_, assert_equal, + assert_array_equal, assert_almost_equal, assert_array_almost_equal, + assert_raises, assert_allclose, assert_array_max_ulp + ) +from numpy.random import rand from numpy.lib import * -from numpy.core import * -from numpy import matrix, asmatrix -import numpy as np class TestAny(TestCase): def test_basic(self): y1 = [0, 0, 1, 0] y2 = [0, 0, 0, 0] y3 = [1, 0, 1, 0] - assert_(any(y1)) - assert_(any(y3)) - assert_(not any(y2)) + assert_(np.any(y1)) + assert_(np.any(y3)) + assert_(not np.any(y2)) def test_nd(self): y1 = [[0, 0, 0], [0, 1, 0], [1, 1, 0]] - assert_(any(y1)) - assert_array_equal(sometrue(y1, axis=0), [1, 1, 0]) - assert_array_equal(sometrue(y1, axis=1), [0, 1, 1]) + assert_(np.any(y1)) + assert_array_equal(np.sometrue(y1, axis=0), [1, 1, 0]) + assert_array_equal(np.sometrue(y1, axis=1), [0, 1, 1]) class TestAll(TestCase): @@ -29,28 +30,28 @@ class TestAll(TestCase): y1 = [0, 1, 1, 0] y2 = [0, 0, 0, 0] y3 = [1, 1, 1, 1] - assert_(not all(y1)) - assert_(all(y3)) - assert_(not all(y2)) - assert_(all(~array(y2))) + assert_(not np.all(y1)) + assert_(np.all(y3)) + assert_(not np.all(y2)) + assert_(np.all(~np.array(y2))) def test_nd(self): y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]] - assert_(not all(y1)) - assert_array_equal(alltrue(y1, axis=0), [0, 0, 1]) - assert_array_equal(alltrue(y1, axis=1), [0, 0, 1]) + assert_(not np.all(y1)) + assert_array_equal(np.alltrue(y1, axis=0), [0, 0, 1]) + assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1]) class TestAverage(TestCase): def test_basic(self): - y1 = array([1, 2, 3]) + y1 = np.array([1, 2, 3]) assert_(average(y1, axis=0) == 2.) - y2 = array([1., 2., 3.]) + y2 = np.array([1., 2., 3.]) assert_(average(y2, axis=0) == 2.) y3 = [0., 0., 0.] assert_(average(y3, axis=0) == 0.) - y4 = ones((4, 4)) + y4 = np.ones((4, 4)) y4[0, 1] = 0 y4[1, 0] = 2 assert_almost_equal(y4.mean(0), average(y4, 0)) @@ -60,25 +61,25 @@ class TestAverage(TestCase): assert_almost_equal(y5.mean(0), average(y5, 0)) assert_almost_equal(y5.mean(1), average(y5, 1)) - y6 = matrix(rand(5, 5)) + y6 = np.matrix(rand(5, 5)) assert_array_equal(y6.mean(0), average(y6, 0)) def test_weights(self): - y = arange(10) - w = arange(10) + y = np.arange(10) + w = np.arange(10) actual = average(y, weights=w) - desired = (arange(10) ** 2).sum()*1. / arange(10).sum() + desired = (np.arange(10) ** 2).sum()*1. / np.arange(10).sum() assert_almost_equal(actual, desired) - y1 = array([[1, 2, 3], [4, 5, 6]]) + y1 = np.array([[1, 2, 3], [4, 5, 6]]) w0 = [1, 2] actual = average(y1, weights=w0, axis=0) - desired = array([3., 4., 5.]) + desired = np.array([3., 4., 5.]) assert_almost_equal(actual, desired) w1 = [0, 0, 1] actual = average(y1, weights=w1, axis=1) - desired = array([3., 6.]) + desired = np.array([3., 6.]) assert_almost_equal(actual, desired) # This should raise an error. Can we test for that ? @@ -86,35 +87,35 @@ class TestAverage(TestCase): # 2D Case w2 = [[0, 0, 1], [0, 0, 2]] - desired = array([3., 6.]) + desired = np.array([3., 6.]) assert_array_equal(average(y1, weights=w2, axis=1), desired) assert_equal(average(y1, weights=w2), 5.) def test_returned(self): - y = array([[1, 2, 3], [4, 5, 6]]) + y = np.array([[1, 2, 3], [4, 5, 6]]) # No weights avg, scl = average(y, returned=True) assert_equal(scl, 6.) avg, scl = average(y, 0, returned=True) - assert_array_equal(scl, array([2., 2., 2.])) + assert_array_equal(scl, np.array([2., 2., 2.])) avg, scl = average(y, 1, returned=True) - assert_array_equal(scl, array([3., 3.])) + assert_array_equal(scl, np.array([3., 3.])) # With weights w0 = [1, 2] avg, scl = average(y, weights=w0, axis=0, returned=True) - assert_array_equal(scl, array([3., 3., 3.])) + assert_array_equal(scl, np.array([3., 3., 3.])) w1 = [1, 2, 3] avg, scl = average(y, weights=w1, axis=1, returned=True) - assert_array_equal(scl, array([6., 6.])) + assert_array_equal(scl, np.array([6., 6.])) w2 = [[0, 0, 1], [1, 2, 3]] avg, scl = average(y, weights=w2, axis=1, returned=True) - assert_array_equal(scl, array([1., 6.])) + assert_array_equal(scl, np.array([1., 6.])) class TestSelect(TestCase): @@ -125,12 +126,12 @@ class TestSelect(TestCase): return output def test_basic(self): - choices = [array([1, 2, 3]), - array([4, 5, 6]), - array([7, 8, 9])] - conditions = [array([0, 0, 0]), - array([0, 1, 0]), - array([0, 0, 1])] + choices = [np.array([1, 2, 3]), + np.array([4, 5, 6]), + np.array([7, 8, 9])] + conditions = [np.array([0, 0, 0]), + np.array([0, 1, 0]), + np.array([0, 0, 1])] assert_array_equal(select(conditions, choices, default=15), self._select(conditions, choices, default=15)) @@ -149,104 +150,104 @@ class TestInsert(TestCase): class TestAmax(TestCase): def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6.0] - assert_equal(amax(a), 10.0) + assert_equal(np.amax(a), 10.0) b = [[3, 6.0, 9.0], [4, 10.0, 5.0], [8, 3.0, 2.0]] - assert_equal(amax(b, axis=0), [8.0, 10.0, 9.0]) - assert_equal(amax(b, axis=1), [9.0, 10.0, 8.0]) + assert_equal(np.amax(b, axis=0), [8.0, 10.0, 9.0]) + assert_equal(np.amax(b, axis=1), [9.0, 10.0, 8.0]) class TestAmin(TestCase): def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6.0] - assert_equal(amin(a), -5.0) + assert_equal(np.amin(a), -5.0) b = [[3, 6.0, 9.0], [4, 10.0, 5.0], [8, 3.0, 2.0]] - assert_equal(amin(b, axis=0), [3.0, 3.0, 2.0]) - assert_equal(amin(b, axis=1), [3.0, 4.0, 2.0]) + assert_equal(np.amin(b, axis=0), [3.0, 3.0, 2.0]) + assert_equal(np.amin(b, axis=1), [3.0, 4.0, 2.0]) class TestPtp(TestCase): def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6.0] - assert_equal(ptp(a, axis=0), 15.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(ptp(b, axis=0), [5.0, 7.0, 7.0]) - assert_equal(ptp(b, axis= -1), [6.0, 6.0, 6.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]) class TestCumsum(TestCase): def test_basic(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 [int8, uint8, int16, uint16, int32, uint32, - float32, float64, complex64, complex128]: - a = array(ba, ctype) - a2 = array(ba2, ctype) - assert_array_equal(cumsum(a, axis=0), array([1, 3, 13, 24, 30, 35, 39], ctype)) - assert_array_equal(cumsum(a2, axis=0), array([[1, 2, 3, 4], [6, 8, 10, 13], - [16, 11, 14, 18]], ctype)) - assert_array_equal(cumsum(a2, axis=1), - array([[1, 3, 6, 10], - [5, 11, 18, 27], - [10, 13, 17, 22]], ctype)) + for ctype in [np.int8, np.uint8, 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) + + tgt = np.array([1, 3, 13, 24, 30, 35, 39], ctype) + assert_array_equal(np.cumsum(a, axis=0), tgt) + + tgt = np.array([[1, 2, 3, 4], [6, 8, 10, 13], [16, 11, 14, 18]], ctype) + assert_array_equal(np.cumsum(a2, axis=0), tgt) + + tgt = np.array([[1, 3, 6, 10], [5, 11, 18, 27], [10, 13, 17, 22]], ctype) + assert_array_equal(np.cumsum(a2, axis=1), tgt) class TestProd(TestCase): def test_basic(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 [int16, uint16, int32, uint32, - float32, float64, complex64, complex128]: - a = array(ba, ctype) - a2 = array(ba2, ctype) + 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, prod, a) self.assertRaises(ArithmeticError, prod, a2, 1) self.assertRaises(ArithmeticError, prod, a) else: - assert_equal(prod(a, axis=0), 26400) - assert_array_equal(prod(a2, axis=0), - array([50, 36, 84, 180], ctype)) - assert_array_equal(prod(a2, axis= -1), array([24, 1890, 600], ctype)) + assert_equal(np.prod(a, axis=0), 26400) + assert_array_equal(np.prod(a2, axis=0), + np.array([50, 36, 84, 180], ctype)) + assert_array_equal(np.prod(a2, axis= -1), + np.array([24, 1890, 600], ctype)) class TestCumprod(TestCase): def test_basic(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 [int16, uint16, int32, uint32, - float32, float64, complex64, complex128]: - a = array(ba, ctype) - a2 = array(ba2, ctype) + 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, cumprod, a) self.assertRaises(ArithmeticError, cumprod, a2, 1) self.assertRaises(ArithmeticError, cumprod, a) else: - assert_array_equal(cumprod(a, axis= -1), - array([1, 2, 20, 220, - 1320, 6600, 26400], ctype)) - assert_array_equal(cumprod(a2, axis=0), - array([[ 1, 2, 3, 4], - [ 5, 12, 21, 36], - [50, 36, 84, 180]], ctype)) - assert_array_equal(cumprod(a2, axis= -1), - array([[ 1, 2, 6, 24], - [ 5, 30, 210, 1890], - [10, 30, 120, 600]], ctype)) + assert_array_equal(np.cumprod(a, axis= -1), + np.array([1, 2, 20, 220, 1320, 6600, 26400], ctype)) + assert_array_equal(np.cumprod(a2, axis=0), + np.array([[ 1, 2, 3, 4], [ 5, 12, 21, 36], + [50, 36, 84, 180]], ctype)) + assert_array_equal(np.cumprod(a2, axis= -1), + np.array([[ 1, 2, 6, 24], [ 5, 30, 210, 1890], + [10, 30, 120, 600]], ctype)) class TestDiff(TestCase): def test_basic(self): x = [1, 4, 6, 7, 12] - out = array([3, 2, 1, 5]) - out2 = array([-1, -1, 4]) - out3 = array([0, 5]) + out = np.array([3, 2, 1, 5]) + out2 = np.array([-1, -1, 4]) + out3 = np.array([0, 5]) assert_array_equal(diff(x), out) assert_array_equal(diff(x, n=2), out2) assert_array_equal(diff(x, n=3), out3) @@ -266,17 +267,17 @@ class TestDiff(TestCase): class TestGradient(TestCase): def test_basic(self): v = [[1, 1], [3, 4]] - x = array(v) - dx = [array([[2., 3.], [2., 3.]]), - array([[0., 0.], [1., 1.]])] + x = np.array(v) + dx = [np.array([[2., 3.], [2., 3.]]), + np.array([[0., 0.], [1., 1.]])] assert_array_equal(gradient(x), dx) assert_array_equal(gradient(v), dx) def test_badargs(self): # for 2D array, gradient can take 0,1, or 2 extra args - x = array([[1, 1], [3, 4]]) - assert_raises(SyntaxError, gradient, x, array([1., 1.]), - array([1., 1.]), array([1., 1.])) + x = np.array([[1, 1], [3, 4]]) + assert_raises(SyntaxError, gradient, x, np.array([1., 1.]), + np.array([1., 1.]), np.array([1., 1.])) def test_masked(self): # Make sure that gradient supports subclasses like masked arrays @@ -285,29 +286,30 @@ class TestGradient(TestCase): def test_datetime64(self): # Make sure gradient() can handle special types like datetime64 - x = array(['1910-08-16', '1910-08-11', '1910-08-10', '1910-08-12', + x = np.array(['1910-08-16', '1910-08-11', '1910-08-10', '1910-08-12', '1910-10-12', '1910-12-12', '1912-12-12'], dtype='datetime64[D]') - dx = array([ -5, -3, 0, 31, 61, 396, 731], dtype='timedelta64[D]') + dx = np.array([ -5, -3, 0, 31, 61, 396, 731], dtype='timedelta64[D]') assert_array_equal(gradient(x), dx) assert_(dx.dtype == np.dtype('timedelta64[D]')) def test_timedelta64(self): # Make sure gradient() can handle special types like timedelta64 - x = array([-5, -3, 10, 12, 61, 321, 300], dtype='timedelta64[D]') - dx = array([ 2, 7, 7, 25, 154, 119, -21], dtype='timedelta64[D]') + 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]')) class TestAngle(TestCase): def test_basic(self): - x = [1 + 3j, sqrt(2) / 2.0 + 1j * sqrt(2) / 2, 1, 1j, -1, -1j, 1 - 3j, -1 + 3j] + x = [1 + 3j, np.sqrt(2) / 2.0 + 1j * np.sqrt(2) / 2, + 1, 1j, -1, -1j, 1 - 3j, -1 + 3j] y = angle(x) - yo = [arctan(3.0 / 1.0), arctan(1.0), 0, pi / 2, pi, -pi / 2.0, - - arctan(3.0 / 1.0), pi - arctan(3.0 / 1.0)] + yo = [np.arctan(3.0 / 1.0), np.arctan(1.0), 0, np.pi / 2, np.pi, -np.pi / 2.0, + - np.arctan(3.0 / 1.0), np.pi - np.arctan(3.0 / 1.0)] z = angle(x, deg=1) - zo = array(yo) * 180 / pi + zo = np.array(yo) * 180 / np.pi assert_array_almost_equal(y, yo, 11) assert_array_almost_equal(z, zo, 11) @@ -316,29 +318,29 @@ class TestTrimZeros(TestCase): """ only testing for integer splits. """ def test_basic(self): - a = array([0, 0, 1, 2, 3, 4, 0]) + a = np.array([0, 0, 1, 2, 3, 4, 0]) res = trim_zeros(a) - assert_array_equal(res, array([1, 2, 3, 4])) + assert_array_equal(res, np.array([1, 2, 3, 4])) def test_leading_skip(self): - a = array([0, 0, 1, 0, 2, 3, 4, 0]) + a = np.array([0, 0, 1, 0, 2, 3, 4, 0]) res = trim_zeros(a) - assert_array_equal(res, array([1, 0, 2, 3, 4])) + assert_array_equal(res, np.array([1, 0, 2, 3, 4])) def test_trailing_skip(self): - a = array([0, 0, 1, 0, 2, 3, 0, 4, 0]) + a = np.array([0, 0, 1, 0, 2, 3, 0, 4, 0]) res = trim_zeros(a) - assert_array_equal(res, array([1, 0, 2, 3, 0, 4])) + assert_array_equal(res, np.array([1, 0, 2, 3, 0, 4])) class TestExtins(TestCase): def test_basic(self): - a = array([1, 3, 2, 1, 2, 3, 3]) + a = np.array([1, 3, 2, 1, 2, 3, 3]) b = extract(a > 1, a) assert_array_equal(b, [3, 2, 2, 3, 3]) def test_place(self): - a = array([1, 4, 3, 2, 5, 8, 7]) + a = np.array([1, 4, 3, 2, 5, 8, 7]) place(a, [0, 1, 0, 1, 0, 1, 0], [2, 4, 6]) assert_array_equal(a, [1, 2, 3, 4, 5, 6, 7]) @@ -374,7 +376,7 @@ class TestVectorize(TestCase): assert_array_equal(r, [5, 8, 1, 4]) def test_large(self): - x = linspace(-3, 2, 10000) + x = np.linspace(-3, 2, 10000) f = vectorize(lambda x: x) y = f(x) assert_array_equal(y, x) @@ -382,9 +384,9 @@ class TestVectorize(TestCase): def test_ufunc(self): import math f = vectorize(math.cos) - args = array([0, 0.5*pi, pi, 1.5*pi, 2*pi]) + args = np.array([0, 0.5*np.pi, np.pi, 1.5*np.pi, 2*np.pi]) r1 = f(args) - r2 = cos(args) + r2 = np.cos(args) assert_array_equal(r1, r2) def test_keywords(self): @@ -392,12 +394,12 @@ class TestVectorize(TestCase): def foo(a, b=1): return a + b f = vectorize(foo) - args = array([1,2,3]) + args = np.array([1,2,3]) r1 = f(args) - r2 = array([2,3,4]) + r2 = np.array([2,3,4]) assert_array_equal(r1, r2) r1 = f(args, 2) - r2 = array([3,4,5]) + r2 = np.array([3,4,5]) assert_array_equal(r1, r2) def test_keywords_no_func_code(self): @@ -413,27 +415,27 @@ class TestVectorize(TestCase): class TestDigitize(TestCase): def test_forward(self): - x = arange(-6, 5) - bins = arange(-5, 5) - assert_array_equal(digitize(x, bins), arange(11)) + x = np.arange(-6, 5) + bins = np.arange(-5, 5) + assert_array_equal(digitize(x, bins), np.arange(11)) def test_reverse(self): - x = arange(5, -6, -1) - bins = arange(5, -5, -1) - assert_array_equal(digitize(x, bins), arange(11)) + x = np.arange(5, -6, -1) + bins = np.arange(5, -5, -1) + assert_array_equal(digitize(x, bins), np.arange(11)) def test_random(self): x = rand(10) - bin = linspace(x.min(), x.max(), 10) - assert_(all(digitize(x, bin) != 0)) + bin = np.linspace(x.min(), x.max(), 10) + assert_(np.all(digitize(x, bin) != 0)) class TestUnwrap(TestCase): def test_simple(self): #check that unwrap removes jumps greather that 2*pi - assert_array_equal(unwrap([1, 1 + 2 * pi]), [1, 1]) + assert_array_equal(unwrap([1, 1 + 2 * np.pi]), [1, 1]) #check that unwrap maintans continuity - assert_(all(diff(unwrap(rand(10) * 100)) < pi)) + assert_(np.all(diff(unwrap(rand(10) * 100)) < np.pi)) class TestFilterwindows(TestCase): @@ -442,48 +444,49 @@ class TestFilterwindows(TestCase): w = hanning(10) assert_array_almost_equal(w, flipud(w), 7) #check known value - assert_almost_equal(sum(w, axis=0), 4.500, 4) + assert_almost_equal(np.sum(w, axis=0), 4.500, 4) def test_hamming(self): #check symmetry w = hamming(10) assert_array_almost_equal(w, flipud(w), 7) #check known value - assert_almost_equal(sum(w, axis=0), 4.9400, 4) + assert_almost_equal(np.sum(w, axis=0), 4.9400, 4) def test_bartlett(self): #check symmetry w = bartlett(10) assert_array_almost_equal(w, flipud(w), 7) #check known value - assert_almost_equal(sum(w, axis=0), 4.4444, 4) + assert_almost_equal(np.sum(w, axis=0), 4.4444, 4) def test_blackman(self): #check symmetry w = blackman(10) assert_array_almost_equal(w, flipud(w), 7) #check known value - assert_almost_equal(sum(w, axis=0), 3.7800, 4) + assert_almost_equal(np.sum(w, axis=0), 3.7800, 4) class TestTrapz(TestCase): def test_simple(self): - r = trapz(exp(-1.0 / 2 * (arange(-10, 10, .1)) ** 2) / sqrt(2 * pi), dx=0.1) + x = np.arange(-10, 10, .1) + r = trapz(np.exp(-.5*x**2) / np.sqrt(2*np.pi), dx=0.1) #check integral of normal equals 1 assert_almost_equal(r, 1, 7) def test_ndim(self): - x = linspace(0, 1, 3) - y = linspace(0, 2, 8) - z = linspace(0, 3, 13) + x = np.linspace(0, 1, 3) + y = np.linspace(0, 2, 8) + z = np.linspace(0, 3, 13) - wx = ones_like(x) * (x[1] - x[0]) + wx = np.ones_like(x) * (x[1] - x[0]) wx[0] /= 2 wx[-1] /= 2 - wy = ones_like(y) * (y[1] - y[0]) + wy = np.ones_like(y) * (y[1] - y[0]) wy[0] /= 2 wy[-1] /= 2 - wz = ones_like(z) * (z[1] - z[0]) + wz = np.ones_like(z) * (z[1] - z[0]) wz[0] /= 2 wz[-1] /= 2 @@ -512,7 +515,7 @@ class TestTrapz(TestCase): def test_masked(self): #Testing that masked arrays behave as if the function is 0 where #masked - x = arange(5) + x = np.arange(5) y = x * x mask = x == 2 ym = np.ma.array(y, mask=mask) @@ -527,11 +530,11 @@ class TestTrapz(TestCase): def test_matrix(self): #Test to make sure matrices give the same answer as ndarrays - x = linspace(0, 5) + x = np.linspace(0, 5) y = x * x r = trapz(y, x) - mx = matrix(x) - my = matrix(y) + mx = np.matrix(x) + my = np.matrix(y) mr = trapz(my, mx) assert_almost_equal(mr, r) @@ -539,13 +542,13 @@ class TestTrapz(TestCase): class TestSinc(TestCase): def test_simple(self): assert_(sinc(0) == 1) - w = sinc(linspace(-1, 1, 100)) + w = sinc(np.linspace(-1, 1, 100)) #check symmetry assert_array_almost_equal(w, flipud(w), 7) def test_array_like(self): x = [0, 0.5] - y1 = sinc(array(x)) + y1 = sinc(np.array(x)) y2 = sinc(list(x)) y3 = sinc(tuple(x)) assert_array_equal(y1, y2) @@ -563,10 +566,10 @@ class TestHistogram(TestCase): v = rand(n) (a, b) = histogram(v) #check if the sum of the bins equals the number of samples - assert_equal(sum(a, axis=0), n) + assert_equal(np.sum(a, axis=0), n) #check that the bin counts are evenly spaced when the data is from a # linear function - (a, b) = histogram(linspace(0, 10, 100)) + (a, b) = histogram(np.linspace(0, 10, 100)) assert_array_equal(a, 10) def test_one_bin(self): @@ -576,22 +579,22 @@ class TestHistogram(TestCase): assert_array_equal(edges, [1, 2]) assert_raises(ValueError, histogram, [1, 2], bins=0) h, e = histogram([1,2], bins=1) - assert_equal(h, array([2])) - assert_allclose(e, array([1., 2.])) + assert_equal(h, np.array([2])) + assert_allclose(e, np.array([1., 2.])) def test_normed(self): # Check that the integral of the density equals 1. n = 100 v = rand(n) a, b = histogram(v, normed=True) - area = sum(a * diff(b)) + area = np.sum(a * diff(b)) assert_almost_equal(area, 1) # Check with non-constant bin widths (buggy but backwards compatible) v = np.arange(10) bins = [0, 1, 5, 9, 10] a, b = histogram(v, bins, normed=True) - area = sum(a * diff(b)) + area = np.sum(a * diff(b)) assert_almost_equal(area, 1) def test_density(self): @@ -599,7 +602,7 @@ class TestHistogram(TestCase): n = 100 v = rand(n) a, b = histogram(v, density=True) - area = sum(a * diff(b)) + area = np.sum(a * diff(b)) assert_almost_equal(area, 1) # Check with non-constant bin widths @@ -607,7 +610,7 @@ class TestHistogram(TestCase): bins = [0,1,3,6,10] a, b = histogram(v, bins, density=True) assert_array_equal(a, .1) - assert_equal(sum(a*diff(b)), 1) + assert_equal(np.sum(a*diff(b)), 1) # Variale bin widths are especially useful to deal with # infinities. @@ -623,7 +626,7 @@ class TestHistogram(TestCase): def test_outliers(self): # Check that outliers are not tallied - a = arange(10) + .5 + a = np.arange(10) + .5 # Lower outliers h, b = histogram(a, range=[0, 9]) @@ -638,7 +641,7 @@ class TestHistogram(TestCase): assert_equal((h * diff(b)).sum(), 1) # Weights - w = arange(10) + .5 + w = np.arange(10) + .5 h, b = histogram(a, range=[1, 9], weights=w, normed=True) assert_equal((h * diff(b)).sum(), 1) @@ -647,22 +650,22 @@ class TestHistogram(TestCase): def test_type(self): # Check the type of the returned histogram - a = arange(10) + .5 + a = np.arange(10) + .5 h, b = histogram(a) assert_(issubdtype(h.dtype, int)) h, b = histogram(a, normed=True) assert_(issubdtype(h.dtype, float)) - h, b = histogram(a, weights=ones(10, int)) + h, b = histogram(a, weights=np.ones(10, int)) assert_(issubdtype(h.dtype, int)) - h, b = histogram(a, weights=ones(10, float)) + h, b = histogram(a, weights=np.ones(10, float)) assert_(issubdtype(h.dtype, float)) def test_weights(self): v = rand(100) - w = ones(100) * 5 + w = np.ones(100) * 5 a, b = histogram(v) na, nb = histogram(v, normed=True) wa, wb = histogram(v, weights=w) @@ -671,16 +674,16 @@ class TestHistogram(TestCase): assert_array_almost_equal(na, nwa) # Check weights are properly applied. - v = linspace(0, 10, 10) - w = concatenate((zeros(5), ones(5))) - wa, wb = histogram(v, bins=arange(11), weights=w) + v = np.linspace(0, 10, 10) + w = np.concatenate((np.zeros(5), np.ones(5))) + wa, wb = histogram(v, bins=np.arange(11), weights=w) assert_array_almost_equal(wa, w) # Check with integer weights wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1]) assert_array_equal(wa, [4, 5, 0, 1]) wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1], normed=True) - assert_array_almost_equal(wa, array([4, 5, 0, 1]) / 10. / 3. * 4) + assert_array_almost_equal(wa, np.array([4, 5, 0, 1]) / 10. / 3. * 4) # Check weights with non-uniform bin widths a,b = histogram(np.arange(9), [0,1,3,6,10], \ @@ -689,41 +692,41 @@ class TestHistogram(TestCase): def test_empty(self): a, b = histogram([], bins=([0,1])) - assert_array_equal(a, array([0])) - assert_array_equal(b, array([0, 1])) + assert_array_equal(a, np.array([0])) + assert_array_equal(b, np.array([0, 1])) class TestHistogramdd(TestCase): def test_simple(self): - x = array([[-.5, .5, 1.5], [-.5, 1.5, 2.5], [-.5, 2.5, .5], \ + x = np.array([[-.5, .5, 1.5], [-.5, 1.5, 2.5], [-.5, 2.5, .5], \ [.5, .5, 1.5], [.5, 1.5, 2.5], [.5, 2.5, 2.5]]) H, edges = histogramdd(x, (2, 3, 3), range=[[-1, 1], [0, 3], [0, 3]]) - answer = asarray([[[0, 1, 0], [0, 0, 1], [1, 0, 0]], [[0, 1, 0], [0, 0, 1], + answer = np.array([[[0, 1, 0], [0, 0, 1], [1, 0, 0]], [[0, 1, 0], [0, 0, 1], [0, 0, 1]]]) assert_array_equal(H, answer) # Check normalization ed = [[-2, 0, 2], [0, 1, 2, 3], [0, 1, 2, 3]] H, edges = histogramdd(x, bins=ed, normed=True) - assert_(all(H == answer / 12.)) + assert_(np.all(H == answer / 12.)) # Check that H has the correct shape. H, edges = histogramdd(x, (2, 3, 4), range=[[-1, 1], [0, 3], [0, 4]], normed=True) - answer = asarray([[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]], [[0, 1, 0, 0], + answer = np.array([[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]], [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]]]) assert_array_almost_equal(H, answer / 6., 4) # Check that a sequence of arrays is accepted and H has the correct # shape. - z = [squeeze(y) for y in split(x, 3, axis=1)] + z = [np.squeeze(y) for y in split(x, 3, axis=1)] H, edges = histogramdd(z, bins=(4, 3, 2), range=[[-2, 2], [0, 3], [0, 2]]) - answer = asarray([[[0, 0], [0, 0], [0, 0]], + answer = np.array([[[0, 0], [0, 0], [0, 0]], [[0, 1], [0, 0], [1, 0]], [[0, 1], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]) assert_array_equal(H, answer) - Z = zeros((5, 5, 5)) + Z = np.zeros((5, 5, 5)) Z[range(5), range(5), range(5)] = 1. - H, edges = histogramdd([arange(5), arange(5), arange(5)], 5) + H, edges = histogramdd([np.arange(5), np.arange(5), np.arange(5)], 5) assert_array_equal(H, Z) def test_shape_3d(self): @@ -753,21 +756,21 @@ class TestHistogramdd(TestCase): v = rand(100, 2) hist, edges = histogramdd(v) n_hist, edges = histogramdd(v, normed=True) - w_hist, edges = histogramdd(v, weights=ones(100)) + w_hist, edges = histogramdd(v, weights=np.ones(100)) assert_array_equal(w_hist, hist) - w_hist, edges = histogramdd(v, weights=ones(100) * 2, normed=True) + w_hist, edges = histogramdd(v, weights=np.ones(100) * 2, normed=True) assert_array_equal(w_hist, n_hist) - w_hist, edges = histogramdd(v, weights=ones(100, int) * 2) + w_hist, edges = histogramdd(v, weights=np.ones(100, int) * 2) assert_array_equal(w_hist, 2 * hist) def test_identical_samples(self): - x = zeros((10, 2), int) + x = np.zeros((10, 2), int) hist, edges = histogramdd(x, bins=2) - assert_array_equal(edges[0], array([-0.5, 0. , 0.5])) + assert_array_equal(edges[0], np.array([-0.5, 0. , 0.5])) def test_empty(self): a, b = histogramdd([[], []], bins=([0,1], [0,1])) - assert_array_max_ulp(a, array([[ 0.]])) + assert_array_max_ulp(a, np.array([[ 0.]])) a, b = np.histogramdd([[], [], []], bins=2) assert_array_max_ulp(a, np.zeros((2, 2, 2))) @@ -800,111 +803,111 @@ class TestHistogramdd(TestCase): class TestUnique(TestCase): def test_simple(self): - x = array([4, 3, 2, 1, 1, 2, 3, 4, 0]) - assert_(all(unique(x) == [0, 1, 2, 3, 4])) - assert_(unique(array([1, 1, 1, 1, 1])) == array([1])) + x = np.array([4, 3, 2, 1, 1, 2, 3, 4, 0]) + assert_(np.all(unique(x) == [0, 1, 2, 3, 4])) + assert_(unique(np.array([1, 1, 1, 1, 1])) == np.array([1])) x = ['widget', 'ham', 'foo', 'bar', 'foo', 'ham'] - assert_(all(unique(x) == ['bar', 'foo', 'ham', 'widget'])) - x = array([5 + 6j, 1 + 1j, 1 + 10j, 10, 5 + 6j]) - assert_(all(unique(x) == [1 + 1j, 1 + 10j, 5 + 6j, 10])) + assert_(np.all(unique(x) == ['bar', 'foo', 'ham', 'widget'])) + x = np.array([5 + 6j, 1 + 1j, 1 + 10j, 10, 5 + 6j]) + assert_(np.all(unique(x) == [1 + 1j, 1 + 10j, 5 + 6j, 10])) class TestCheckFinite(TestCase): def test_simple(self): a = [1, 2, 3] - b = [1, 2, inf] - c = [1, 2, nan] - numpy.lib.asarray_chkfinite(a) - assert_raises(ValueError, numpy.lib.asarray_chkfinite, b) - assert_raises(ValueError, numpy.lib.asarray_chkfinite, c) + b = [1, 2, np.inf] + c = [1, 2, np.nan] + np.lib.asarray_chkfinite(a) + assert_raises(ValueError, np.lib.asarray_chkfinite, b) + assert_raises(ValueError, np.lib.asarray_chkfinite, c) def test_dtype_order(self): """Regression test for missing dtype and order arguments""" a = [1, 2, 3] - a = numpy.lib.asarray_chkfinite(a, order='F', dtype=numpy.float64) - assert_(a.dtype == numpy.float64) + a = np.lib.asarray_chkfinite(a, order='F', dtype=np.float64) + assert_(a.dtype == np.float64) class TestNaNFuncts(TestCase): def setUp(self): - self.A = array([[[ nan, 0.01319214, 0.01620964], - [ 0.11704017, nan, 0.75157887], - [ 0.28333658, 0.1630199 , nan ]], - [[ 0.59541557, nan, 0.37910852], - [ nan, 0.87964135, nan ], - [ 0.70543747, nan, 0.34306596]], - [[ 0.72687499, 0.91084584, nan ], + self.A = np.array([[[ np.nan, 0.01319214, 0.01620964], + [ 0.11704017, np.nan, 0.75157887], + [ 0.28333658, 0.1630199 , np.nan ]], + [[ 0.59541557, np.nan, 0.37910852], + [ np.nan, 0.87964135, np.nan ], + [ 0.70543747, np.nan, 0.34306596]], + [[ 0.72687499, 0.91084584, np.nan ], [ 0.84386844, 0.38944762, 0.23913896], - [ nan, 0.37068164, 0.33850425]]]) + [ np.nan, 0.37068164, 0.33850425]]]) def test_nansum(self): assert_almost_equal(nansum(self.A), 8.0664079100000006) assert_almost_equal(nansum(self.A, 0), - array([[ 1.32229056, 0.92403798, 0.39531816], + np.array([[ 1.32229056, 0.92403798, 0.39531816], [ 0.96090861, 1.26908897, 0.99071783], [ 0.98877405, 0.53370154, 0.68157021]])) assert_almost_equal(nansum(self.A, 1), - array([[ 0.40037675, 0.17621204, 0.76778851], + np.array([[ 0.40037675, 0.17621204, 0.76778851], [ 1.30085304, 0.87964135, 0.72217448], [ 1.57074343, 1.6709751 , 0.57764321]])) assert_almost_equal(nansum(self.A, 2), - array([[ 0.02940178, 0.86861904, 0.44635648], + np.array([[ 0.02940178, 0.86861904, 0.44635648], [ 0.97452409, 0.87964135, 1.04850343], [ 1.63772083, 1.47245502, 0.70918589]])) def test_nanmin(self): assert_almost_equal(nanmin(self.A), 0.01319214) assert_almost_equal(nanmin(self.A, 0), - array([[ 0.59541557, 0.01319214, 0.01620964], + np.array([[ 0.59541557, 0.01319214, 0.01620964], [ 0.11704017, 0.38944762, 0.23913896], [ 0.28333658, 0.1630199 , 0.33850425]])) assert_almost_equal(nanmin(self.A, 1), - array([[ 0.11704017, 0.01319214, 0.01620964], + np.array([[ 0.11704017, 0.01319214, 0.01620964], [ 0.59541557, 0.87964135, 0.34306596], [ 0.72687499, 0.37068164, 0.23913896]])) assert_almost_equal(nanmin(self.A, 2), - array([[ 0.01319214, 0.11704017, 0.1630199 ], + np.array([[ 0.01319214, 0.11704017, 0.1630199 ], [ 0.37910852, 0.87964135, 0.34306596], [ 0.72687499, 0.23913896, 0.33850425]])) - assert_(np.isnan(nanmin([nan, nan]))) + assert_(np.isnan(nanmin([np.nan, np.nan]))) def test_nanargmin(self): assert_almost_equal(nanargmin(self.A), 1) assert_almost_equal(nanargmin(self.A, 0), - array([[1, 0, 0], + np.array([[1, 0, 0], [0, 2, 2], [0, 0, 2]])) assert_almost_equal(nanargmin(self.A, 1), - array([[1, 0, 0], + np.array([[1, 0, 0], [0, 1, 2], [0, 2, 1]])) assert_almost_equal(nanargmin(self.A, 2), - array([[1, 0, 1], + np.array([[1, 0, 1], [2, 1, 2], [0, 2, 2]])) def test_nanmax(self): assert_almost_equal(nanmax(self.A), 0.91084584000000002) assert_almost_equal(nanmax(self.A, 0), - array([[ 0.72687499, 0.91084584, 0.37910852], + np.array([[ 0.72687499, 0.91084584, 0.37910852], [ 0.84386844, 0.87964135, 0.75157887], [ 0.70543747, 0.37068164, 0.34306596]])) assert_almost_equal(nanmax(self.A, 1), - array([[ 0.28333658, 0.1630199 , 0.75157887], + np.array([[ 0.28333658, 0.1630199 , 0.75157887], [ 0.70543747, 0.87964135, 0.37910852], [ 0.84386844, 0.91084584, 0.33850425]])) assert_almost_equal(nanmax(self.A, 2), - array([[ 0.01620964, 0.75157887, 0.28333658], + np.array([[ 0.01620964, 0.75157887, 0.28333658], [ 0.59541557, 0.87964135, 0.70543747], [ 0.91084584, 0.84386844, 0.37068164]])) - assert_(np.isnan(nanmax([nan, nan]))) + assert_(np.isnan(nanmax([np.nan, np.nan]))) def test_nanmin_allnan_on_axis(self): - assert_array_equal(isnan(nanmin([[nan] * 2] * 3, axis=1)), + assert_array_equal(np.isnan(nanmin([[np.nan] * 2] * 3, axis=1)), [True, True, True]) def test_nanmin_masked(self): - a = np.ma.fix_invalid([[2, 1, 3, nan], [5, 2, 3, nan]]) + a = np.ma.fix_invalid([[2, 1, 3, np.nan], [5, 2, 3, np.nan]]) ctrl_mask = a._mask.copy() test = np.nanmin(a, axis=1) assert_equal(test, [1, 2]) @@ -914,10 +917,12 @@ class TestNaNFuncts(TestCase): class TestNanFunctsIntTypes(TestCase): - int_types = (int8, int16, int32, int64, uint8, uint16, uint32, uint64) + int_types = ( + np.int8, np.int16, np.int32, np.int64, np.uint8, + np.uint16, np.uint32, np.uint64) def setUp(self, *args, **kwargs): - self.A = array([127, 39, 93, 87, 46]) + self.A = np.array([127, 39, 93, 87, 46]) def integer_arrays(self): for dtype in self.int_types: @@ -934,27 +939,27 @@ class TestNanFunctsIntTypes(TestCase): assert_equal(nanmax(A), max_value) def test_nanargmin(self): - min_arg = argmin(self.A) + min_arg = np.argmin(self.A) for A in self.integer_arrays(): assert_equal(nanargmin(A), min_arg) def test_nanargmax(self): - max_arg = argmax(self.A) + max_arg = np.argmax(self.A) for A in self.integer_arrays(): assert_equal(nanargmax(A), max_arg) class TestCorrCoef(TestCase): - A = array([[ 0.15391142, 0.18045767, 0.14197213], + A = np.array([[ 0.15391142, 0.18045767, 0.14197213], [ 0.70461506, 0.96474128, 0.27906989], [ 0.9297531 , 0.32296769, 0.19267156]]) - B = array([[ 0.10377691, 0.5417086 , 0.49807457], + B = np.array([[ 0.10377691, 0.5417086 , 0.49807457], [ 0.82872117, 0.77801674, 0.39226705], [ 0.9314666 , 0.66800209, 0.03538394]]) - res1 = array([[ 1. , 0.9379533 , -0.04931983], + res1 = np.array([[ 1. , 0.9379533 , -0.04931983], [ 0.9379533 , 1. , 0.30007991], [-0.04931983, 0.30007991, 1. ]]) - res2 = array([[ 1. , 0.9379533 , -0.04931983, + res2 = np.array([[ 1. , 0.9379533 , -0.04931983, 0.30151751, 0.66318558, 0.51532523], [ 0.9379533 , 1. , 0.30007991, - 0.04781421, 0.88157256, 0.78052386], @@ -992,17 +997,17 @@ class TestCov(TestCase): class Test_i0(TestCase): def test_simple(self): - assert_almost_equal(i0(0.5), array(1.0634833707413234)) - A = array([ 0.49842636, 0.6969809 , 0.22011976, 0.0155549]) + assert_almost_equal(i0(0.5), np.array(1.0634833707413234)) + A = np.array([ 0.49842636, 0.6969809 , 0.22011976, 0.0155549]) assert_almost_equal(i0(A), - array([ 1.06307822, 1.12518299, 1.01214991, 1.00006049])) - B = array([[ 0.827002 , 0.99959078], + np.array([ 1.06307822, 1.12518299, 1.01214991, 1.00006049])) + B = np.array([[ 0.827002 , 0.99959078], [ 0.89694769, 0.39298162], [ 0.37954418, 0.05206293], [ 0.36465447, 0.72446427], [ 0.48164949, 0.50324519]]) assert_almost_equal(i0(B), - array([[ 1.17843223, 1.26583466], + np.array([[ 1.17843223, 1.26583466], [ 1.21147086, 1.0389829 ], [ 1.03633899, 1.00067775], [ 1.03352052, 1.13557954], @@ -1011,14 +1016,14 @@ class Test_i0(TestCase): class TestKaiser(TestCase): def test_simple(self): - assert_almost_equal(kaiser(0, 1.0), array([])) - assert_(isfinite(kaiser(1, 1.0))) - assert_almost_equal(kaiser(2, 1.0), array([ 0.78984831, 0.78984831])) + assert_almost_equal(kaiser(0, 1.0), np.array([])) + assert_(np.isfinite(kaiser(1, 1.0))) + assert_almost_equal(kaiser(2, 1.0), np.array([ 0.78984831, 0.78984831])) assert_almost_equal(kaiser(5, 1.0), - array([ 0.78984831, 0.94503323, 1. , + np.array([ 0.78984831, 0.94503323, 1. , 0.94503323, 0.78984831])) assert_almost_equal(kaiser(5, 1.56789), - array([ 0.58285404, 0.88409679, 1. , + np.array([ 0.58285404, 0.88409679, 1. , 0.88409679, 0.58285404])) def test_int_beta(self): @@ -1027,11 +1032,11 @@ class TestKaiser(TestCase): class TestMsort(TestCase): def test_simple(self): - A = array([[ 0.44567325, 0.79115165, 0.5490053 ], + A = np.array([[ 0.44567325, 0.79115165, 0.5490053 ], [ 0.36844147, 0.37325583, 0.96098397], [ 0.64864341, 0.52929049, 0.39172155]]) assert_almost_equal(msort(A), - array([[ 0.36844147, 0.37325583, 0.39172155], + np.array([[ 0.36844147, 0.37325583, 0.39172155], [ 0.44567325, 0.52929049, 0.5490053 ], [ 0.64864341, 0.79115165, 0.96098397]])) @@ -1039,11 +1044,11 @@ class TestMsort(TestCase): class TestMeshgrid(TestCase): def test_simple(self): [X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7]) - assert_(all(X == array([[1, 2, 3], + assert_(np.all(X == np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]))) - assert_(all(Y == array([[4, 4, 4], + assert_(np.all(Y == np.array([[4, 4, 4], [5, 5, 5], [6, 6, 6], [7, 7, 7]]))) @@ -1060,15 +1065,15 @@ class TestPiecewise(TestCase): assert_array_equal(x, [1, 0]) # Conditions is single bool array - x = piecewise([0, 0], array([True, False]), [1]) + x = piecewise([0, 0], np.array([True, False]), [1]) assert_array_equal(x, [1, 0]) # Condition is single int array - x = piecewise([0, 0], array([1, 0]), [1]) + x = piecewise([0, 0], np.array([1, 0]), [1]) assert_array_equal(x, [1, 0]) # List of conditions: int array - x = piecewise([0, 0], [array([1, 0])], [1]) + x = piecewise([0, 0], [np.array([1, 0])], [1]) assert_array_equal(x, [1, 0]) @@ -1088,7 +1093,7 @@ class TestPiecewise(TestCase): assert_array_equal(x, [2, 3]) def test_0d(self): - x = array(3) + x = np.array(3) y = piecewise(x, x > 3, [4, 0]) assert_(y.ndim == 0) assert_(y == 0) |