diff options
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r-- | numpy/lib/tests/test_arraysetops.py | 171 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 434 | ||||
-rw-r--r-- | numpy/lib/tests/test_getlimits.py | 55 | ||||
-rw-r--r-- | numpy/lib/tests/test_index_tricks.py | 51 | ||||
-rw-r--r-- | numpy/lib/tests/test_polynomial.py | 86 | ||||
-rw-r--r-- | numpy/lib/tests/test_shape_base.py | 408 | ||||
-rw-r--r-- | numpy/lib/tests/test_twodim_base.py | 187 | ||||
-rw-r--r-- | numpy/lib/tests/test_type_check.py | 274 | ||||
-rw-r--r-- | numpy/lib/tests/test_ufunclike.py | 66 |
9 files changed, 1732 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py new file mode 100644 index 000000000..ccdcc7556 --- /dev/null +++ b/numpy/lib/tests/test_arraysetops.py @@ -0,0 +1,171 @@ +""" Test functions for 1D array set operations. + +""" + +from numpy.testing import * +set_package_path() +import numpy +from numpy.lib.arraysetops import * +from numpy.lib.arraysetops import ediff1d +restore_path() + +################################################## + +class test_aso(NumpyTestCase): + ## + # 03.11.2005, c + def check_unique1d( self ): + + a = numpy.array( [5, 7, 1, 2, 1, 5, 7] ) + + ec = numpy.array( [1, 2, 5, 7] ) + c = unique1d( a ) + assert_array_equal( c, ec ) + + assert_array_equal([], unique1d([])) + + ## + # 03.11.2005, c + def check_intersect1d( self ): + + a = numpy.array( [5, 7, 1, 2] ) + b = numpy.array( [2, 4, 3, 1, 5] ) + + ec = numpy.array( [1, 2, 5] ) + c = intersect1d( a, b ) + assert_array_equal( c, ec ) + + assert_array_equal([], intersect1d([],[])) + + ## + # 03.11.2005, c + def check_intersect1d_nu( self ): + + a = numpy.array( [5, 5, 7, 1, 2] ) + b = numpy.array( [2, 1, 4, 3, 3, 1, 5] ) + + ec = numpy.array( [1, 2, 5] ) + c = intersect1d_nu( a, b ) + assert_array_equal( c, ec ) + + assert_array_equal([], intersect1d_nu([],[])) + + ## + # 03.11.2005, c + def check_setxor1d( self ): + + a = numpy.array( [5, 7, 1, 2] ) + b = numpy.array( [2, 4, 3, 1, 5] ) + + ec = numpy.array( [3, 4, 7] ) + c = setxor1d( a, b ) + assert_array_equal( c, ec ) + + a = numpy.array( [1, 2, 3] ) + b = numpy.array( [6, 5, 4] ) + + ec = numpy.array( [1, 2, 3, 4, 5, 6] ) + c = setxor1d( a, b ) + assert_array_equal( c, ec ) + + a = numpy.array( [1, 8, 2, 3] ) + b = numpy.array( [6, 5, 4, 8] ) + + ec = numpy.array( [1, 2, 3, 4, 5, 6] ) + c = setxor1d( a, b ) + assert_array_equal( c, ec ) + + assert_array_equal([], setxor1d([],[])) + + def check_ediff1d(self): + zero_elem = numpy.array([]) + one_elem = numpy.array([1]) + two_elem = numpy.array([1,2]) + + assert_array_equal([],ediff1d(zero_elem)) + assert_array_equal([0],ediff1d(zero_elem,to_begin=0)) + assert_array_equal([0],ediff1d(zero_elem,to_end=0)) + assert_array_equal([-1,0],ediff1d(zero_elem,to_begin=-1,to_end=0)) + assert_array_equal([],ediff1d(one_elem)) + assert_array_equal([1],ediff1d(two_elem)) + + ## + # 03.11.2005, c + def check_setmember1d( self ): + + a = numpy.array( [5, 7, 1, 2] ) + b = numpy.array( [2, 4, 3, 1, 5] ) + + ec = numpy.array( [True, False, True, True] ) + c = setmember1d( a, b ) + assert_array_equal( c, ec ) + + a[0] = 8 + ec = numpy.array( [False, False, True, True] ) + c = setmember1d( a, b ) + assert_array_equal( c, ec ) + + a[0], a[3] = 4, 8 + ec = numpy.array( [True, False, True, False] ) + c = setmember1d( a, b ) + assert_array_equal( c, ec ) + + assert_array_equal([], setmember1d([],[])) + + ## + # 03.11.2005, c + def check_union1d( self ): + + a = numpy.array( [5, 4, 7, 1, 2] ) + b = numpy.array( [2, 4, 3, 3, 2, 1, 5] ) + + ec = numpy.array( [1, 2, 3, 4, 5, 7] ) + c = union1d( a, b ) + assert_array_equal( c, ec ) + + assert_array_equal([], union1d([],[])) + + ## + # 03.11.2005, c + # 09.01.2006 + def check_setdiff1d( self ): + + a = numpy.array( [6, 5, 4, 7, 1, 2] ) + b = numpy.array( [2, 4, 3, 3, 2, 1, 5] ) + + ec = numpy.array( [6, 7] ) + c = setdiff1d( a, b ) + assert_array_equal( c, ec ) + + a = numpy.arange( 21 ) + b = numpy.arange( 19 ) + ec = numpy.array( [19, 20] ) + c = setdiff1d( a, b ) + assert_array_equal( c, ec ) + + assert_array_equal([], setdiff1d([],[])) + + + ## + # 03.11.2005, c + def check_manyways( self ): + + nItem = 100 + a = numpy.fix( nItem / 10 * numpy.random.random( nItem ) ) + b = numpy.fix( nItem / 10 * numpy.random.random( nItem ) ) + + c1 = intersect1d_nu( a, b ) + c2 = unique1d( intersect1d( a, b ) ) + assert_array_equal( c1, c2 ) + + a = numpy.array( [5, 7, 1, 2, 8] ) + b = numpy.array( [9, 8, 2, 4, 3, 1, 5] ) + + c1 = setxor1d( a, b ) + aux1 = intersect1d( a, b ) + aux2 = union1d( a, b ) + c2 = setdiff1d( aux2, aux1 ) + assert_array_equal( c1, c2 ) + +if __name__ == "__main__": + NumpyTest().run() diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py new file mode 100644 index 000000000..f0930ae5b --- /dev/null +++ b/numpy/lib/tests/test_function_base.py @@ -0,0 +1,434 @@ +import sys + +from numpy.testing import * +set_package_path() +import numpy.lib;reload(numpy.lib) +from numpy.lib import * +from numpy.core import * +del sys.path[0] + +class test_any(NumpyTestCase): + def check_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)) + + def check_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]) + +class test_all(NumpyTestCase): + def check_basic(self): + 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))) + + def check_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]) + +class test_average(NumpyTestCase): + def check_basic(self): + y1 = array([1,2,3]) + assert(average(y1,axis=0) == 2.) + y2 = 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[0,1] = 0 + y4[1,0] = 2 + assert_array_equal(y4.mean(0), average(y4, 0)) + assert_array_equal(y4.mean(1), average(y4, 1)) + + y5 = rand(5,5) + assert_array_equal(y5.mean(0), average(y5, 0)) + assert_array_equal(y5.mean(1), average(y5, 1)) + + def check_weighted(self): + y1 = array([[1,2,3], + [4,5,6]]) + actual = average(y1,weights=[1,2],axis=0) + desired = array([3.,4.,5.]) + assert_array_equal(actual, desired) + +class test_logspace(NumpyTestCase): + def check_basic(self): + y = logspace(0,6) + assert(len(y)==50) + y = logspace(0,6,num=100) + assert(y[-1] == 10**6) + y = logspace(0,6,endpoint=0) + assert(y[-1] < 10**6) + y = logspace(0,6,num=7) + assert_array_equal(y,[1,10,100,1e3,1e4,1e5,1e6]) + +class test_linspace(NumpyTestCase): + def check_basic(self): + y = linspace(0,10) + assert(len(y)==50) + y = linspace(2,10,num=100) + assert(y[-1] == 10) + y = linspace(2,10,endpoint=0) + assert(y[-1] < 10) + y,st = linspace(2,10,retstep=1) + assert_almost_equal(st,8/49.0) + assert_array_almost_equal(y,mgrid[2:10:50j],13) + + def check_corner(self): + y = list(linspace(0,1,1)) + assert y == [0.0], y + y = list(linspace(0,1,2.5)) + assert y == [0.0, 1.0] + + def check_type(self): + t1 = linspace(0,1,0).dtype + t2 = linspace(0,1,1).dtype + t3 = linspace(0,1,2).dtype + assert_equal(t1, t2) + assert_equal(t2, t3) + +class test_insert(NumpyTestCase): + def check_basic(self): + a = [1,2,3] + assert_equal(insert(a,0,1), [1,1,2,3]) + assert_equal(insert(a,3,1), [1,2,3,1]) + assert_equal(insert(a,[1,1,1],[1,2,3]), [1,1,2,3,2,3]) + +class test_amax(NumpyTestCase): + def check_basic(self): + a = [3,4,5,10,-3,-5,6.0] + assert_equal(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]) + +class test_amin(NumpyTestCase): + def check_basic(self): + a = [3,4,5,10,-3,-5,6.0] + assert_equal(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]) + +class test_ptp(NumpyTestCase): + def check_basic(self): + a = [3,4,5,10,-3,-5,6.0] + assert_equal(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]) + +class test_cumsum(NumpyTestCase): + def check_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)) + +class test_prod(NumpyTestCase): + def check_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) + if ctype in ['1', 'b']: + self.failUnlessRaises(ArithmeticError, prod, a) + self.failUnlessRaises(ArithmeticError, prod, a2, 1) + self.failUnlessRaises(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)) + +class test_cumprod(NumpyTestCase): + def check_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) + if ctype in ['1', 'b']: + self.failUnlessRaises(ArithmeticError, cumprod, a) + self.failUnlessRaises(ArithmeticError, cumprod, a2, 1) + self.failUnlessRaises(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)) + +class test_diff(NumpyTestCase): + def check_basic(self): + x = [1,4,6,7,12] + out = array([3,2,1,5]) + out2 = array([-1,-1,4]) + out3 = 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) + + def check_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,:,:] + assert_array_equal(diff(x),out1) + assert_array_equal(diff(x,n=2),out2) + assert_array_equal(diff(x,axis=0),out3) + assert_array_equal(diff(x,n=2,axis=0),out4) + +class test_angle(NumpyTestCase): + def check_basic(self): + x = [1+3j,sqrt(2)/2.0+1j*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)] + z = angle(x,deg=1) + zo = array(yo)*180/pi + assert_array_almost_equal(y,yo,11) + assert_array_almost_equal(z,zo,11) + +class test_trim_zeros(NumpyTestCase): + """ only testing for integer splits. + """ + def check_basic(self): + a= array([0,0,1,2,3,4,0]) + res = trim_zeros(a) + assert_array_equal(res,array([1,2,3,4])) + def check_leading_skip(self): + a= array([0,0,1,0,2,3,4,0]) + res = trim_zeros(a) + assert_array_equal(res,array([1,0,2,3,4])) + def check_trailing_skip(self): + a= 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])) + + +class test_extins(NumpyTestCase): + def check_basic(self): + a = array([1,3,2,1,2,3,3]) + b = extract(a>1,a) + assert_array_equal(b,[3,2,2,3,3]) + def check_place(self): + a = 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]) + def check_both(self): + a = rand(10) + mask = a > 0.5 + ac = a.copy() + c = extract(mask, a) + place(a,mask,0) + place(a,mask,c) + assert_array_equal(a,ac) + +class test_vectorize(NumpyTestCase): + def check_simple(self): + def addsubtract(a,b): + if a > b: + return a - b + else: + return a + b + f = vectorize(addsubtract) + r = f([0,3,6,9],[1,3,5,7]) + assert_array_equal(r,[1,6,1,2]) + def check_scalar(self): + def addsubtract(a,b): + if a > b: + return a - b + else: + return a + b + f = vectorize(addsubtract) + r = f([0,3,6,9],5) + assert_array_equal(r,[5,8,1,4]) + def check_large(self): + x = linspace(-3,2,10000) + f = vectorize(lambda x: x) + y = f(x) + assert_array_equal(y, x) + +class test_digitize(NumpyTestCase): + def check_forward(self): + x = arange(-6,5) + bins = arange(-5,5) + assert_array_equal(digitize(x,bins),arange(11)) + + def check_reverse(self): + x = arange(5,-6,-1) + bins = arange(5,-5,-1) + assert_array_equal(digitize(x,bins),arange(11)) + + def check_random(self): + x = rand(10) + bin = linspace(x.min(), x.max(), 10) + assert all(digitize(x,bin) != 0) + +class test_unwrap(NumpyTestCase): + def check_simple(self): + #check that unwrap removes jumps greather that 2*pi + assert_array_equal(unwrap([1,1+2*pi]),[1,1]) + #check that unwrap maintans continuity + assert(all(diff(unwrap(rand(10)*100))<pi)) + + +class test_filterwindows(NumpyTestCase): + def check_hanning(self): + #check symmetry + w=hanning(10) + assert_array_almost_equal(w,flipud(w),7) + #check known value + assert_almost_equal(sum(w,axis=0),4.500,4) + + def check_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) + + def check_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) + + def check_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) + + +class test_trapz(NumpyTestCase): + def check_simple(self): + r=trapz(exp(-1.0/2*(arange(-10,10,.1))**2)/sqrt(2*pi),dx=0.1) + #check integral of normal equals 1 + assert_almost_equal(sum(r,axis=0),1,7) + +class test_sinc(NumpyTestCase): + def check_simple(self): + assert(sinc(0)==1) + w=sinc(linspace(-1,1,100)) + #check symmetry + assert_array_almost_equal(w,flipud(w),7) + +class test_histogram(NumpyTestCase): + def check_simple(self): + n=100 + v=rand(n) + (a,b)=histogram(v) + #check if the sum of the bins equals the number of samples + assert(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)) + assert(all(a==10)) + +class test_histogramdd(NumpyTestCase): + def check_simple(self): + x = 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], [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.)) + # 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], [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)] + H, edges = histogramdd(z, bins=(4,3,2),range=[[-2,2], [0,3], [0,2]]) + answer = asarray([[[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[range(5), range(5), range(5)] = 1. + H,edges = histogramdd([arange(5), arange(5), arange(5)], 5) + assert_array_equal(H, Z) + + def check_shape(self): + x = rand(100,3) + hist3d, edges = histogramdd(x, bins = (5, 7, 6)) + assert_array_equal(hist3d.shape, (5,7,6)) + + def check_weights(self): + v = rand(100,2) + hist, edges = histogramdd(v) + n_hist, edges = histogramdd(v, normed=True) + w_hist, edges = histogramdd(v, weights=ones(100)) + assert_array_equal(w_hist, hist) + w_hist, edges = histogramdd(v, weights=ones(100)*2, normed=True) + assert_array_equal(w_hist, n_hist) + w_hist, edges = histogramdd(v, weights=ones(100, int)*2) + assert_array_equal(w_hist, 2*hist) + + def check_identical_samples(self): + x = zeros((10,2),int) + hist, edges = histogramdd(x, bins=2) + assert_array_equal(edges[0],array([-0.5, 0. , 0.5])) + +class test_unique(NumpyTestCase): + def check_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 = ['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])) + +def compare_results(res,desired): + for i in range(len(desired)): + assert_array_equal(res[i],desired[i]) + +if __name__ == "__main__": + NumpyTest('numpy.lib.function_base').run() diff --git a/numpy/lib/tests/test_getlimits.py b/numpy/lib/tests/test_getlimits.py new file mode 100644 index 000000000..7a4fea57a --- /dev/null +++ b/numpy/lib/tests/test_getlimits.py @@ -0,0 +1,55 @@ +""" Test functions for limits module. +""" + +from numpy.testing import * +set_package_path() +import numpy.lib;reload(numpy.lib) +from numpy.lib.getlimits import finfo, iinfo +from numpy import single,double,longdouble +import numpy as N +restore_path() + +################################################## + +class test_python_float(NumpyTestCase): + def check_singleton(self): + ftype = finfo(float) + ftype2 = finfo(float) + assert_equal(id(ftype),id(ftype2)) + +class test_single(NumpyTestCase): + def check_singleton(self): + ftype = finfo(single) + ftype2 = finfo(single) + assert_equal(id(ftype),id(ftype2)) + +class test_double(NumpyTestCase): + def check_singleton(self): + ftype = finfo(double) + ftype2 = finfo(double) + assert_equal(id(ftype),id(ftype2)) + +class test_longdouble(NumpyTestCase): + def check_singleton(self,level=2): + ftype = finfo(longdouble) + ftype2 = finfo(longdouble) + assert_equal(id(ftype),id(ftype2)) + +class test_iinfo(NumpyTestCase): + def check_basic(self): + dts = zip(['i1', 'i2', 'i4', 'i8', + 'u1', 'u2', 'u4', 'u8'], + [N.int8, N.int16, N.int32, N.int64, + N.uint8, N.uint16, N.uint32, N.uint64]) + for dt1, dt2 in dts: + assert_equal(iinfo(dt1).min, iinfo(dt2).min) + assert_equal(iinfo(dt1).max, iinfo(dt2).max) + self.assertRaises(ValueError, iinfo, 'f4') + + def check_unsigned_max(self): + types = N.sctypes['uint'] + for T in types: + assert_equal(iinfo(T).max, T(-1)) + +if __name__ == "__main__": + NumpyTest().run() diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py new file mode 100644 index 000000000..5d4f540b2 --- /dev/null +++ b/numpy/lib/tests/test_index_tricks.py @@ -0,0 +1,51 @@ +from numpy.testing import * +set_package_path() +from numpy import array, ones, r_, mgrid +restore_path() + +class test_grid(NumpyTestCase): + def check_basic(self): + a = mgrid[-1:1:10j] + b = mgrid[-1:1:0.1] + assert(a.shape == (10,)) + assert(b.shape == (20,)) + assert(a[0] == -1) + assert_almost_equal(a[-1],1) + assert(b[0] == -1) + assert_almost_equal(b[1]-b[0],0.1,11) + assert_almost_equal(b[-1],b[0]+19*0.1,11) + assert_almost_equal(a[1]-a[0],2.0/9.0,11) + + def check_nd(self): + c = mgrid[-1:1:10j,-2:2:10j] + d = mgrid[-1:1:0.1,-2:2:0.2] + assert(c.shape == (2,10,10)) + assert(d.shape == (2,20,20)) + assert_array_equal(c[0][0,:],-ones(10,'d')) + assert_array_equal(c[1][:,0],-2*ones(10,'d')) + assert_array_almost_equal(c[0][-1,:],ones(10,'d'),11) + assert_array_almost_equal(c[1][:,-1],2*ones(10,'d'),11) + assert_array_almost_equal(d[0,1,:]-d[0,0,:], 0.1*ones(20,'d'),11) + assert_array_almost_equal(d[1,:,1]-d[1,:,0], 0.2*ones(20,'d'),11) + +class test_concatenator(NumpyTestCase): + def check_1d(self): + assert_array_equal(r_[1,2,3,4,5,6],array([1,2,3,4,5,6])) + b = ones(5) + c = r_[b,0,0,b] + assert_array_equal(c,[1,1,1,1,1,0,0,1,1,1,1,1]) + + def check_2d(self): + b = rand(5,5) + c = rand(5,5) + d = r_['1',b,c] # append columns + assert(d.shape == (5,10)) + assert_array_equal(d[:,:5],b) + assert_array_equal(d[:,5:],c) + d = r_[b,c] + assert(d.shape == (10,5)) + assert_array_equal(d[:5,:],b) + assert_array_equal(d[5:,:],c) + +if __name__ == "__main__": + NumpyTest().run() diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py new file mode 100644 index 000000000..f3a8720d9 --- /dev/null +++ b/numpy/lib/tests/test_polynomial.py @@ -0,0 +1,86 @@ +""" +>>> import numpy.core as nx +>>> from numpy.lib.polynomial import poly1d, polydiv + +>>> p = poly1d([1.,2,3]) +>>> p +poly1d([ 1., 2., 3.]) +>>> print p + 2 +1 x + 2 x + 3 +>>> q = poly1d([3.,2,1]) +>>> q +poly1d([ 3., 2., 1.]) +>>> print q + 2 +3 x + 2 x + 1 + +>>> p(0) +3.0 +>>> p(5) +38.0 +>>> q(0) +1.0 +>>> q(5) +86.0 + +>>> p * q +poly1d([ 3., 8., 14., 8., 3.]) +>>> p / q +(poly1d([ 0.33333333]), poly1d([ 1.33333333, 2.66666667])) +>>> p + q +poly1d([ 4., 4., 4.]) +>>> p - q +poly1d([-2., 0., 2.]) +>>> p ** 4 +poly1d([ 1., 8., 36., 104., 214., 312., 324., 216., 81.]) + +>>> p(q) +poly1d([ 9., 12., 16., 8., 6.]) +>>> q(p) +poly1d([ 3., 12., 32., 40., 34.]) + +>>> nx.asarray(p) +array([ 1., 2., 3.]) +>>> len(p) +2 + +>>> p[0], p[1], p[2], p[3] +(3.0, 2.0, 1.0, 0) + +>>> p.integ() +poly1d([ 0.33333333, 1. , 3. , 0. ]) +>>> p.integ(1) +poly1d([ 0.33333333, 1. , 3. , 0. ]) +>>> p.integ(5) +poly1d([ 0.00039683, 0.00277778, 0.025 , 0. , 0. , + 0. , 0. , 0. ]) +>>> p.deriv() +poly1d([ 2., 2.]) +>>> p.deriv(2) +poly1d([ 2.]) + +>>> q = poly1d([1.,2,3], variable='y') +>>> print q + 2 +1 y + 2 y + 3 +>>> q = poly1d([1.,2,3], variable='lambda') +>>> print q + 2 +1 lambda + 2 lambda + 3 + +>>> polydiv(poly1d([1,0,-1]), poly1d([1,1])) +(poly1d([ 1., -1.]), poly1d([ 0.])) +""" + +from numpy.testing import * +import numpy as N + +class test_docs(NumpyTestCase): + def check_doctests(self): return self.rundocs() + + def check_roots(self): + assert_array_equal(N.roots([1,0,0]), [0,0]) + +if __name__ == "__main__": + NumpyTest().run() diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py new file mode 100644 index 000000000..6efd2cdf1 --- /dev/null +++ b/numpy/lib/tests/test_shape_base.py @@ -0,0 +1,408 @@ +from numpy.testing import * +set_package_path() +import numpy.lib; +from numpy.lib import * +from numpy.core import * +restore_path() + +class test_apply_along_axis(NumpyTestCase): + def check_simple(self): + a = ones((20,10),'d') + assert_array_equal(apply_along_axis(len,0,a),len(a)*ones(shape(a)[1])) + def check_simple101(self,level=11): + a = ones((10,101),'d') + assert_array_equal(apply_along_axis(len,0,a),len(a)*ones(shape(a)[1])) + + def check_3d(self): + a = arange(27).reshape((3,3,3)) + assert_array_equal(apply_along_axis(sum,0,a), [[27,30,33],[36,39,42],[45,48,51]]) + +class test_array_split(NumpyTestCase): + def check_integer_0_split(self): + a = arange(10) + try: + res = array_split(a,0) + assert(0) # it should have thrown a value error + except ValueError: + pass + def check_integer_split(self): + a = arange(10) + res = array_split(a,1) + desired = [arange(10)] + compare_results(res,desired) + + res = array_split(a,2) + desired = [arange(5),arange(5,10)] + compare_results(res,desired) + + res = array_split(a,3) + desired = [arange(4),arange(4,7),arange(7,10)] + compare_results(res,desired) + + res = array_split(a,4) + desired = [arange(3),arange(3,6),arange(6,8),arange(8,10)] + compare_results(res,desired) + + res = array_split(a,5) + desired = [arange(2),arange(2,4),arange(4,6),arange(6,8),arange(8,10)] + compare_results(res,desired) + + res = array_split(a,6) + desired = [arange(2),arange(2,4),arange(4,6),arange(6,8),arange(8,9), + arange(9,10)] + compare_results(res,desired) + + res = array_split(a,7) + desired = [arange(2),arange(2,4),arange(4,6),arange(6,7),arange(7,8), + arange(8,9), arange(9,10)] + compare_results(res,desired) + + res = array_split(a,8) + desired = [arange(2),arange(2,4),arange(4,5),arange(5,6),arange(6,7), + arange(7,8), arange(8,9), arange(9,10)] + compare_results(res,desired) + + res = array_split(a,9) + desired = [arange(2),arange(2,3),arange(3,4),arange(4,5),arange(5,6), + arange(6,7), arange(7,8), arange(8,9), arange(9,10)] + compare_results(res,desired) + + res = array_split(a,10) + desired = [arange(1),arange(1,2),arange(2,3),arange(3,4), + arange(4,5),arange(5,6), arange(6,7), arange(7,8), + arange(8,9), arange(9,10)] + compare_results(res,desired) + + res = array_split(a,11) + desired = [arange(1),arange(1,2),arange(2,3),arange(3,4), + arange(4,5),arange(5,6), arange(6,7), arange(7,8), + arange(8,9), arange(9,10),array([])] + compare_results(res,desired) + def check_integer_split_2D_rows(self): + a = array([arange(10),arange(10)]) + res = array_split(a,3,axis=0) + desired = [array([arange(10)]),array([arange(10)]),array([])] + compare_results(res,desired) + def check_integer_split_2D_cols(self): + a = array([arange(10),arange(10)]) + res = array_split(a,3,axis=-1) + desired = [array([arange(4),arange(4)]), + array([arange(4,7),arange(4,7)]), + array([arange(7,10),arange(7,10)])] + compare_results(res,desired) + def check_integer_split_2D_default(self): + """ This will fail if we change default axis + """ + a = array([arange(10),arange(10)]) + res = array_split(a,3) + desired = [array([arange(10)]),array([arange(10)]),array([])] + compare_results(res,desired) + #perhaps should check higher dimensions + + def check_index_split_simple(self): + a = arange(10) + indices = [1,5,7] + res = array_split(a,indices,axis=-1) + desired = [arange(0,1),arange(1,5),arange(5,7),arange(7,10)] + compare_results(res,desired) + + def check_index_split_low_bound(self): + a = arange(10) + indices = [0,5,7] + res = array_split(a,indices,axis=-1) + desired = [array([]),arange(0,5),arange(5,7),arange(7,10)] + compare_results(res,desired) + def check_index_split_high_bound(self): + a = arange(10) + indices = [0,5,7,10,12] + res = array_split(a,indices,axis=-1) + desired = [array([]),arange(0,5),arange(5,7),arange(7,10), + array([]),array([])] + compare_results(res,desired) + +class test_split(NumpyTestCase): + """* This function is essentially the same as array_split, + except that it test if splitting will result in an + equal split. Only test for this case. + *""" + def check_equal_split(self): + a = arange(10) + res = split(a,2) + desired = [arange(5),arange(5,10)] + compare_results(res,desired) + + def check_unequal_split(self): + a = arange(10) + try: + res = split(a,3) + assert(0) # should raise an error + except ValueError: + pass + +class test_atleast_1d(NumpyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=map(atleast_1d,[a,b]) + desired = [array([1]),array([2])] + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1,2]); b = array([2,3]); + res=map(atleast_1d,[a,b]) + desired = [array([1,2]),array([2,3])] + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + res=map(atleast_1d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + def check_3D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + a = array([a,a]);b = array([b,b]); + res=map(atleast_1d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + def check_r1array(self): + """ Test to make sure equivalent Travis O's r1array function + """ + assert(atleast_1d(3).shape == (1,)) + assert(atleast_1d(3j).shape == (1,)) + assert(atleast_1d(3L).shape == (1,)) + assert(atleast_1d(3.0).shape == (1,)) + assert(atleast_1d([[2,3],[4,5]]).shape == (2,2)) + +class test_atleast_2d(NumpyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=map(atleast_2d,[a,b]) + desired = [array([[1]]),array([[2]])] + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1,2]); b = array([2,3]); + res=map(atleast_2d,[a,b]) + desired = [array([[1,2]]),array([[2,3]])] + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + res=map(atleast_2d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + def check_3D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + a = array([a,a]);b = array([b,b]); + res=map(atleast_2d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + def check_r2array(self): + """ Test to make sure equivalent Travis O's r2array function + """ + assert(atleast_2d(3).shape == (1,1)) + assert(atleast_2d([3j,1]).shape == (1,2)) + assert(atleast_2d([[[3,1],[4,5]],[[3,5],[1,2]]]).shape == (2,2,2)) + +class test_atleast_3d(NumpyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=map(atleast_3d,[a,b]) + desired = [array([[[1]]]),array([[[2]]])] + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1,2]); b = array([2,3]); + res=map(atleast_3d,[a,b]) + desired = [array([[[1],[2]]]),array([[[2],[3]]])] + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + res=map(atleast_3d,[a,b]) + desired = [a[:,:,newaxis],b[:,:,newaxis]] + assert_array_equal(res,desired) + def check_3D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + a = array([a,a]);b = array([b,b]); + res=map(atleast_3d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + +class test_hstack(NumpyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=hstack([a,b]) + desired = array([1,2]) + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1]); b = array([2]); + res=hstack([a,b]) + desired = array([1,2]) + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1],[2]]); b = array([[1],[2]]); + res=hstack([a,b]) + desired = array([[1,1],[2,2]]) + assert_array_equal(res,desired) + +class test_vstack(NumpyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=vstack([a,b]) + desired = array([[1],[2]]) + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1]); b = array([2]); + res=vstack([a,b]) + desired = array([[1],[2]]) + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1],[2]]); b = array([[1],[2]]); + res=vstack([a,b]) + desired = array([[1],[2],[1],[2]]) + assert_array_equal(res,desired) + def check_2D_array2(self): + a = array([1,2]); b = array([1,2]); + res=vstack([a,b]) + desired = array([[1,2],[1,2]]) + assert_array_equal(res,desired) + +class test_dstack(NumpyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=dstack([a,b]) + desired = array([[[1,2]]]) + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1]); b = array([2]); + res=dstack([a,b]) + desired = array([[[1,2]]]) + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1],[2]]); b = array([[1],[2]]); + res=dstack([a,b]) + desired = array([[[1,1]],[[2,2,]]]) + assert_array_equal(res,desired) + def check_2D_array2(self): + a = array([1,2]); b = array([1,2]); + res=dstack([a,b]) + desired = array([[[1,1],[2,2]]]) + assert_array_equal(res,desired) + +""" array_split has more comprehensive test of splitting. + only do simple test on hsplit, vsplit, and dsplit +""" +class test_hsplit(NumpyTestCase): + """ only testing for integer splits. + """ + def check_0D_array(self): + a= array(1) + try: + hsplit(a,2) + assert(0) + except ValueError: + pass + def check_1D_array(self): + a= array([1,2,3,4]) + res = hsplit(a,2) + desired = [array([1,2]),array([3,4])] + compare_results(res,desired) + def check_2D_array(self): + a= array([[1,2,3,4], + [1,2,3,4]]) + res = hsplit(a,2) + desired = [array([[1,2],[1,2]]),array([[3,4],[3,4]])] + compare_results(res,desired) + +class test_vsplit(NumpyTestCase): + """ only testing for integer splits. + """ + def check_1D_array(self): + a= array([1,2,3,4]) + try: + vsplit(a,2) + assert(0) + except ValueError: + pass + def check_2D_array(self): + a= array([[1,2,3,4], + [1,2,3,4]]) + res = vsplit(a,2) + desired = [array([[1,2,3,4]]),array([[1,2,3,4]])] + compare_results(res,desired) + +class test_dsplit(NumpyTestCase): + """ only testing for integer splits. + """ + def check_2D_array(self): + a= array([[1,2,3,4], + [1,2,3,4]]) + try: + dsplit(a,2) + assert(0) + except ValueError: + pass + def check_3D_array(self): + a= array([[[1,2,3,4], + [1,2,3,4]], + [[1,2,3,4], + [1,2,3,4]]]) + res = dsplit(a,2) + desired = [array([[[1,2],[1,2]],[[1,2],[1,2]]]), + array([[[3,4],[3,4]],[[3,4],[3,4]]])] + compare_results(res,desired) + +class test_squeeze(NumpyTestCase): + def check_basic(self): + a = rand(20,10,10,1,1) + b = rand(20,1,10,1,20) + c = rand(1,1,20,10) + assert_array_equal(squeeze(a),reshape(a,(20,10,10))) + assert_array_equal(squeeze(b),reshape(b,(20,10,20))) + assert_array_equal(squeeze(c),reshape(c,(20,10))) + +class test_kron(NumpyTestCase): + def check_return_type(self): + a = ones([2,2]) + m = asmatrix(a) + assert_equal(type(kron(a,a)), ndarray) + assert_equal(type(kron(m,m)), matrix) + assert_equal(type(kron(a,m)), matrix) + assert_equal(type(kron(m,a)), matrix) + class myarray(ndarray): + __array_priority__ = 0.0 + ma = myarray(a.shape, a.dtype, a.data) + assert_equal(type(kron(a,a)), ndarray) + assert_equal(type(kron(ma,ma)), myarray) + assert_equal(type(kron(a,ma)), ndarray) + assert_equal(type(kron(ma,a)), myarray) + + +class test_tile(NumpyTestCase): + def check_basic(self): + a = array([0,1,2]) + b = [[1,2],[3,4]] + assert_equal(tile(a,2), [0,1,2,0,1,2]) + assert_equal(tile(a,(2,2)), [[0,1,2,0,1,2],[0,1,2,0,1,2]]) + assert_equal(tile(a,(1,2)), [[0,1,2,0,1,2]]) + assert_equal(tile(b, 2), [[1,2,1,2],[3,4,3,4]]) + assert_equal(tile(b,(2,1)),[[1,2],[3,4],[1,2],[3,4]]) + assert_equal(tile(b,(2,2)),[[1,2,1,2],[3,4,3,4], + [1,2,1,2],[3,4,3,4]]) + + def check_kroncompare(self): + import numpy.random as nr + reps=[(2,),(1,2),(2,1),(2,2),(2,3,2),(3,2)] + shape=[(3,),(2,3),(3,4,3),(3,2,3),(4,3,2,4),(2,2)] + for s in shape: + b = nr.randint(0,10,size=s) + for r in reps: + a = ones(r, b.dtype) + large = tile(b, r) + klarge = kron(a, b) + assert_equal(large, klarge) + +# Utility + +def compare_results(res,desired): + for i in range(len(desired)): + assert_array_equal(res[i],desired[i]) + + +if __name__ == "__main__": + NumpyTest().run() + diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py new file mode 100644 index 000000000..15ffb2777 --- /dev/null +++ b/numpy/lib/tests/test_twodim_base.py @@ -0,0 +1,187 @@ +""" Test functions for matrix module + +""" + +from numpy.testing import * +set_package_path() +from numpy import arange, rot90, add, fliplr, flipud, zeros, ones, eye, \ + array, diag, histogram2d +import numpy as np +restore_path() + +################################################## + + +def get_mat(n): + data = arange(n) + data = add.outer(data,data) + return data + +class test_eye(NumpyTestCase): + def check_basic(self): + assert_equal(eye(4),array([[1,0,0,0], + [0,1,0,0], + [0,0,1,0], + [0,0,0,1]])) + assert_equal(eye(4,dtype='f'),array([[1,0,0,0], + [0,1,0,0], + [0,0,1,0], + [0,0,0,1]],'f')) + def check_diag(self): + assert_equal(eye(4,k=1),array([[0,1,0,0], + [0,0,1,0], + [0,0,0,1], + [0,0,0,0]])) + assert_equal(eye(4,k=-1),array([[0,0,0,0], + [1,0,0,0], + [0,1,0,0], + [0,0,1,0]])) + def check_2d(self): + assert_equal(eye(4,3),array([[1,0,0], + [0,1,0], + [0,0,1], + [0,0,0]])) + assert_equal(eye(3,4),array([[1,0,0,0], + [0,1,0,0], + [0,0,1,0]])) + def check_diag2d(self): + assert_equal(eye(3,4,k=2),array([[0,0,1,0], + [0,0,0,1], + [0,0,0,0]])) + assert_equal(eye(4,3,k=-2),array([[0,0,0], + [0,0,0], + [1,0,0], + [0,1,0]])) + +class test_diag(NumpyTestCase): + def check_vector(self): + vals = (100*arange(5)).astype('l') + b = zeros((5,5)) + for k in range(5): + b[k,k] = vals[k] + assert_equal(diag(vals),b) + b = zeros((7,7)) + c = b.copy() + for k in range(5): + b[k,k+2] = vals[k] + c[k+2,k] = vals[k] + assert_equal(diag(vals,k=2), b) + assert_equal(diag(vals,k=-2), c) + + def check_matrix(self): + vals = (100*get_mat(5)+1).astype('l') + b = zeros((5,)) + for k in range(5): + b[k] = vals[k,k] + assert_equal(diag(vals),b) + b = b*0 + for k in range(3): + b[k] = vals[k,k+2] + assert_equal(diag(vals,2),b[:3]) + for k in range(3): + b[k] = vals[k+2,k] + assert_equal(diag(vals,-2),b[:3]) + +class test_fliplr(NumpyTestCase): + def check_basic(self): + self.failUnlessRaises(ValueError, fliplr, ones(4)) + a = get_mat(4) + b = a[:,::-1] + assert_equal(fliplr(a),b) + a = [[0,1,2], + [3,4,5]] + b = [[2,1,0], + [5,4,3]] + assert_equal(fliplr(a),b) + +class test_flipud(NumpyTestCase): + def check_basic(self): + a = get_mat(4) + b = a[::-1,:] + assert_equal(flipud(a),b) + a = [[0,1,2], + [3,4,5]] + b = [[3,4,5], + [0,1,2]] + assert_equal(flipud(a),b) + +class test_rot90(NumpyTestCase): + def check_basic(self): + self.failUnlessRaises(ValueError, rot90, ones(4)) + + a = [[0,1,2], + [3,4,5]] + b1 = [[2,5], + [1,4], + [0,3]] + b2 = [[5,4,3], + [2,1,0]] + b3 = [[3,0], + [4,1], + [5,2]] + b4 = [[0,1,2], + [3,4,5]] + + for k in range(-3,13,4): + assert_equal(rot90(a,k=k),b1) + for k in range(-2,13,4): + assert_equal(rot90(a,k=k),b2) + for k in range(-1,13,4): + assert_equal(rot90(a,k=k),b3) + for k in range(0,13,4): + assert_equal(rot90(a,k=k),b4) + + +class test_histogram2d(NumpyTestCase): + def check_simple(self): + x = array([ 0.41702200, 0.72032449, 0.00011437481, 0.302332573, 0.146755891]) + y = array([ 0.09233859, 0.18626021, 0.34556073, 0.39676747, 0.53881673]) + xedges = np.linspace(0,1,10) + yedges = np.linspace(0,1,10) + H = histogram2d(x, y, (xedges, yedges))[0] + answer = array([[0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]]) + assert_array_equal(H.T, answer) + H = histogram2d(x, y, xedges)[0] + assert_array_equal(H.T, answer) + H,xedges,yedges = histogram2d(range(10),range(10)) + assert_array_equal(H, eye(10,10)) + assert_array_equal(xedges, np.linspace(0,9,11)) + assert_array_equal(yedges, np.linspace(0,9,11)) + + def check_asym(self): + x = array([1, 1, 2, 3, 4, 4, 4, 5]) + y = array([1, 3, 2, 0, 1, 2, 3, 4]) + H, xed, yed = histogram2d(x,y, (6, 5), range = [[0,6],[0,5]], normed=True) + answer = array([[0.,0,0,0,0], + [0,1,0,1,0], + [0,0,1,0,0], + [1,0,0,0,0], + [0,1,1,1,0], + [0,0,0,0,1]]) + assert_array_almost_equal(H, answer/8., 3) + assert_array_equal(xed, np.linspace(0,6,7)) + assert_array_equal(yed, np.linspace(0,5,6)) + def check_norm(self): + x = array([1,2,3,1,2,3,1,2,3]) + y = array([1,1,1,2,2,2,3,3,3]) + H, xed, yed = histogram2d(x,y,[[1,2,3,5], [1,2,3,5]], normed=True) + answer=array([[1,1,.5], + [1,1,.5], + [.5,.5,.25]])/9. + assert_array_almost_equal(H, answer, 3) + + def check_all_outliers(self): + r = rand(100)+1. + H, xed, yed = histogram2d(r, r, (4, 5), range=([0,1], [0,1])) + assert_array_equal(H, 0) + +if __name__ == "__main__": + NumpyTest().run() diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py new file mode 100644 index 000000000..8b990c57e --- /dev/null +++ b/numpy/lib/tests/test_type_check.py @@ -0,0 +1,274 @@ +import sys + +from numpy.testing import * +set_package_path() +import numpy.lib;reload(numpy.lib);reload(numpy.lib.type_check) +from numpy.lib import * +from numpy.core import * +restore_path() + +def assert_all(x): + assert(all(x)), x + +class test_mintypecode(NumpyTestCase): + + def check_default_1(self): + for itype in '1bcsuwil': + assert_equal(mintypecode(itype),'d') + assert_equal(mintypecode('f'),'f') + assert_equal(mintypecode('d'),'d') + assert_equal(mintypecode('F'),'F') + assert_equal(mintypecode('D'),'D') + + def check_default_2(self): + for itype in '1bcsuwil': + assert_equal(mintypecode(itype+'f'),'f') + assert_equal(mintypecode(itype+'d'),'d') + assert_equal(mintypecode(itype+'F'),'F') + assert_equal(mintypecode(itype+'D'),'D') + assert_equal(mintypecode('ff'),'f') + assert_equal(mintypecode('fd'),'d') + assert_equal(mintypecode('fF'),'F') + assert_equal(mintypecode('fD'),'D') + assert_equal(mintypecode('df'),'d') + assert_equal(mintypecode('dd'),'d') + #assert_equal(mintypecode('dF',savespace=1),'F') + assert_equal(mintypecode('dF'),'D') + assert_equal(mintypecode('dD'),'D') + assert_equal(mintypecode('Ff'),'F') + #assert_equal(mintypecode('Fd',savespace=1),'F') + assert_equal(mintypecode('Fd'),'D') + assert_equal(mintypecode('FF'),'F') + assert_equal(mintypecode('FD'),'D') + assert_equal(mintypecode('Df'),'D') + assert_equal(mintypecode('Dd'),'D') + assert_equal(mintypecode('DF'),'D') + assert_equal(mintypecode('DD'),'D') + + def check_default_3(self): + assert_equal(mintypecode('fdF'),'D') + #assert_equal(mintypecode('fdF',savespace=1),'F') + assert_equal(mintypecode('fdD'),'D') + assert_equal(mintypecode('fFD'),'D') + assert_equal(mintypecode('dFD'),'D') + + assert_equal(mintypecode('ifd'),'d') + assert_equal(mintypecode('ifF'),'F') + assert_equal(mintypecode('ifD'),'D') + assert_equal(mintypecode('idF'),'D') + #assert_equal(mintypecode('idF',savespace=1),'F') + assert_equal(mintypecode('idD'),'D') + +class test_isscalar(NumpyTestCase): + def check_basic(self): + assert(isscalar(3)) + assert(not isscalar([3])) + assert(not isscalar((3,))) + assert(isscalar(3j)) + assert(isscalar(10L)) + assert(isscalar(4.0)) + +class test_real(NumpyTestCase): + def check_real(self): + y = rand(10,) + assert_array_equal(y,real(y)) + + def check_cmplx(self): + y = rand(10,)+1j*rand(10,) + assert_array_equal(y.real,real(y)) + +class test_imag(NumpyTestCase): + def check_real(self): + y = rand(10,) + assert_array_equal(0,imag(y)) + + def check_cmplx(self): + y = rand(10,)+1j*rand(10,) + assert_array_equal(y.imag,imag(y)) + +class test_iscomplex(NumpyTestCase): + def check_fail(self): + z = array([-1,0,1]) + res = iscomplex(z) + assert(not sometrue(res,axis=0)) + def check_pass(self): + z = array([-1j,1,0]) + res = iscomplex(z) + assert_array_equal(res,[1,0,0]) + +class test_isreal(NumpyTestCase): + def check_pass(self): + z = array([-1,0,1j]) + res = isreal(z) + assert_array_equal(res,[1,1,0]) + def check_fail(self): + z = array([-1j,1,0]) + res = isreal(z) + assert_array_equal(res,[0,1,1]) + +class test_iscomplexobj(NumpyTestCase): + def check_basic(self): + z = array([-1,0,1]) + assert(not iscomplexobj(z)) + z = array([-1j,0,-1]) + assert(iscomplexobj(z)) + +class test_isrealobj(NumpyTestCase): + def check_basic(self): + z = array([-1,0,1]) + assert(isrealobj(z)) + z = array([-1j,0,-1]) + assert(not isrealobj(z)) + +class test_isnan(NumpyTestCase): + def check_goodvalues(self): + z = array((-1.,0.,1.)) + res = isnan(z) == 0 + assert_all(alltrue(res,axis=0)) + def check_posinf(self): + olderr = seterr(divide='ignore') + assert_all(isnan(array((1.,))/0.) == 0) + seterr(**olderr) + def check_neginf(self): + olderr = seterr(divide='ignore') + assert_all(isnan(array((-1.,))/0.) == 0) + seterr(**olderr) + def check_ind(self): + olderr = seterr(divide='ignore', invalid='ignore') + assert_all(isnan(array((0.,))/0.) == 1) + seterr(**olderr) + #def check_qnan(self): log(-1) return pi*j now + # assert_all(isnan(log(-1.)) == 1) + def check_integer(self): + assert_all(isnan(1) == 0) + def check_complex(self): + assert_all(isnan(1+1j) == 0) + def check_complex1(self): + olderr = seterr(divide='ignore', invalid='ignore') + assert_all(isnan(array(0+0j)/0.) == 1) + seterr(**olderr) + +class test_isfinite(NumpyTestCase): + def check_goodvalues(self): + z = array((-1.,0.,1.)) + res = isfinite(z) == 1 + assert_all(alltrue(res,axis=0)) + def check_posinf(self): + olderr = seterr(divide='ignore') + assert_all(isfinite(array((1.,))/0.) == 0) + seterr(**olderr) + def check_neginf(self): + olderr = seterr(divide='ignore') + assert_all(isfinite(array((-1.,))/0.) == 0) + seterr(**olderr) + def check_ind(self): + olderr = seterr(divide='ignore', invalid='ignore') + assert_all(isfinite(array((0.,))/0.) == 0) + seterr(**olderr) + #def check_qnan(self): + # assert_all(isfinite(log(-1.)) == 0) + def check_integer(self): + assert_all(isfinite(1) == 1) + def check_complex(self): + assert_all(isfinite(1+1j) == 1) + def check_complex1(self): + olderr = seterr(divide='ignore', invalid='ignore') + assert_all(isfinite(array(1+1j)/0.) == 0) + seterr(**olderr) + +class test_isinf(NumpyTestCase): + def check_goodvalues(self): + z = array((-1.,0.,1.)) + res = isinf(z) == 0 + assert_all(alltrue(res,axis=0)) + def check_posinf(self): + olderr = seterr(divide='ignore') + assert_all(isinf(array((1.,))/0.) == 1) + seterr(**olderr) + def check_posinf_scalar(self): + olderr = seterr(divide='ignore') + assert_all(isinf(array(1.,)/0.) == 1) + seterr(**olderr) + def check_neginf(self): + olderr = seterr(divide='ignore') + assert_all(isinf(array((-1.,))/0.) == 1) + seterr(**olderr) + def check_neginf_scalar(self): + olderr = seterr(divide='ignore') + assert_all(isinf(array(-1.)/0.) == 1) + seterr(**olderr) + def check_ind(self): + olderr = seterr(divide='ignore', invalid='ignore') + assert_all(isinf(array((0.,))/0.) == 0) + seterr(**olderr) + #def check_qnan(self): + # assert_all(isinf(log(-1.)) == 0) + # assert_all(isnan(log(-1.)) == 1) + +class test_isposinf(NumpyTestCase): + def check_generic(self): + olderr = seterr(divide='ignore', invalid='ignore') + vals = isposinf(array((-1.,0,1))/0.) + seterr(**olderr) + assert(vals[0] == 0) + assert(vals[1] == 0) + assert(vals[2] == 1) + +class test_isneginf(NumpyTestCase): + def check_generic(self): + olderr = seterr(divide='ignore', invalid='ignore') + vals = isneginf(array((-1.,0,1))/0.) + seterr(**olderr) + assert(vals[0] == 1) + assert(vals[1] == 0) + assert(vals[2] == 0) + +class test_nan_to_num(NumpyTestCase): + def check_generic(self): + olderr = seterr(divide='ignore', invalid='ignore') + vals = nan_to_num(array((-1.,0,1))/0.) + seterr(**olderr) + assert_all(vals[0] < -1e10) and assert_all(isfinite(vals[0])) + assert(vals[1] == 0) + assert_all(vals[2] > 1e10) and assert_all(isfinite(vals[2])) + def check_integer(self): + vals = nan_to_num(1) + assert_all(vals == 1) + def check_complex_good(self): + vals = nan_to_num(1+1j) + assert_all(vals == 1+1j) + def check_complex_bad(self): + v = 1+1j + olderr = seterr(divide='ignore', invalid='ignore') + v += array(0+1.j)/0. + seterr(**olderr) + vals = nan_to_num(v) + # !! This is actually (unexpectedly) zero + assert_all(isfinite(vals)) + def check_complex_bad2(self): + v = 1+1j + olderr = seterr(divide='ignore', invalid='ignore') + v += array(-1+1.j)/0. + seterr(**olderr) + vals = nan_to_num(v) + assert_all(isfinite(vals)) + #assert_all(vals.imag > 1e10) and assert_all(isfinite(vals)) + # !! This is actually (unexpectedly) positive + # !! inf. Comment out for now, and see if it + # !! changes + #assert_all(vals.real < -1e10) and assert_all(isfinite(vals)) + + +class test_real_if_close(NumpyTestCase): + def check_basic(self): + a = rand(10) + b = real_if_close(a+1e-15j) + assert_all(isrealobj(b)) + assert_array_equal(a,b) + b = real_if_close(a+1e-7j) + assert_all(iscomplexobj(b)) + b = real_if_close(a+1e-7j,tol=1e-6) + assert_all(isrealobj(b)) + +if __name__ == "__main__": + NumpyTest().run() diff --git a/numpy/lib/tests/test_ufunclike.py b/numpy/lib/tests/test_ufunclike.py new file mode 100644 index 000000000..8f9ac2833 --- /dev/null +++ b/numpy/lib/tests/test_ufunclike.py @@ -0,0 +1,66 @@ +""" +>>> import numpy.core as nx +>>> import numpy.lib.ufunclike as U + +Test fix: +>>> a = nx.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]]) +>>> U.fix(a) +array([[ 1., 1., 1., 1.], + [ 0., -1., -1., -1.]]) +>>> y = nx.zeros(a.shape, float) +>>> U.fix(a, y) +array([[ 1., 1., 1., 1.], + [ 0., -1., -1., -1.]]) +>>> y +array([[ 1., 1., 1., 1.], + [ 0., -1., -1., -1.]]) + +Test isposinf, isneginf, sign +>>> a = nx.array([nx.Inf, -nx.Inf, nx.NaN, 0.0, 3.0, -3.0]) +>>> U.isposinf(a) +array([ True, False, False, False, False, False], dtype=bool) +>>> U.isneginf(a) +array([False, True, False, False, False, False], dtype=bool) +>>> olderr = nx.seterr(invalid='ignore') +>>> nx.sign(a) +array([ 1., -1., 0., 0., 1., -1.]) +>>> olderr = nx.seterr(**olderr) + +Same thing with an output array: +>>> y = nx.zeros(a.shape, bool) +>>> U.isposinf(a, y) +array([ True, False, False, False, False, False], dtype=bool) +>>> y +array([ True, False, False, False, False, False], dtype=bool) +>>> U.isneginf(a, y) +array([False, True, False, False, False, False], dtype=bool) +>>> y +array([False, True, False, False, False, False], dtype=bool) +>>> olderr = nx.seterr(invalid='ignore') +>>> nx.sign(a, y) +array([ True, True, False, False, True, True], dtype=bool) +>>> olderr = nx.seterr(**olderr) +>>> y +array([ True, True, False, False, True, True], dtype=bool) + +Now log2: +>>> a = nx.array([4.5, 2.3, 6.5]) +>>> U.log2(a) +array([ 2.169925 , 1.20163386, 2.70043972]) +>>> 2**_ +array([ 4.5, 2.3, 6.5]) +>>> y = nx.zeros(a.shape, float) +>>> U.log2(a, y) +array([ 2.169925 , 1.20163386, 2.70043972]) +>>> y +array([ 2.169925 , 1.20163386, 2.70043972]) + +""" + +from numpy.testing import * + +class test_docs(NumpyTestCase): + def check_doctests(self): return self.rundocs() + +if __name__ == "__main__": + NumpyTest().run() |