diff options
author | David Cournapeau <cournape@gmail.com> | 2009-09-16 07:15:21 +0000 |
---|---|---|
committer | David Cournapeau <cournape@gmail.com> | 2009-09-16 07:15:21 +0000 |
commit | 364f7b87708a0f31b4bf09a510e0f325615b6b3f (patch) | |
tree | 434495b4a728caf9e94ee9b5f6f055956094b31d /numpy/lib | |
parent | 064d50496ce946cb54e0901ac10967d9e0126b20 (diff) | |
download | numpy-364f7b87708a0f31b4bf09a510e0f325615b6b3f.tar.gz |
All non core regressions tests moved to their respective modules.
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/function_base.py | 2 | ||||
-rw-r--r-- | numpy/lib/polynomial.py | 3 | ||||
-rw-r--r-- | numpy/lib/shape_base.py | 259 | ||||
-rw-r--r-- | numpy/lib/tests/test_regression.py | 128 | ||||
-rw-r--r-- | numpy/lib/tests/test_shape_base.py | 139 |
5 files changed, 123 insertions, 408 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 2459ffc23..73526a84c 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -23,7 +23,7 @@ from numpy.core.umath import pi, multiply, add, arctan2, \ frompyfunc, isnan, cos, less_equal, sqrt, sin, mod, exp, log10 from numpy.core.fromnumeric import ravel, nonzero, choose, sort, mean from numpy.core.numerictypes import typecodes, number -from numpy.lib.shape_base import atleast_1d, atleast_2d +from numpy.core import atleast_1d, atleast_2d from numpy.lib.twodim_base import diag from _compiled_base import _insert, add_docstring from _compiled_base import digitize, bincount, interp as compiled_interp diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index a97c07e87..bab32bf02 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -10,9 +10,8 @@ import re import warnings import numpy.core.numeric as NX -from numpy.core import isscalar, abs, finfo +from numpy.core import isscalar, abs, finfo, atleast_1d, hstack from numpy.lib.twodim_base import diag, vander -from numpy.lib.shape_base import hstack, atleast_1d from numpy.lib.function_base import trim_zeros, sort_complex from numpy.lib.type_check import iscomplex, real, imag from numpy.linalg import eigvals, lstsq diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py index a5bf4d0ea..f21786805 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/shape_base.py @@ -1,5 +1,4 @@ -__all__ = ['atleast_1d','atleast_2d','atleast_3d','vstack','hstack', - 'column_stack','row_stack', 'dstack','array_split','split','hsplit', +__all__ = ['column_stack','row_stack', 'dstack','array_split','split','hsplit', 'vsplit','dsplit','apply_over_axes','expand_dims', 'apply_along_axis', 'kron', 'tile', 'get_array_wrap'] @@ -7,6 +6,7 @@ import numpy.core.numeric as _nx from numpy.core.numeric import asarray, zeros, newaxis, outer, \ concatenate, isscalar, array, asanyarray from numpy.core.fromnumeric import product, reshape +from numpy.core import hstack, vstack, atleast_3d def apply_along_axis(func1d,axis,arr,*args): """ @@ -249,261 +249,6 @@ def expand_dims(a, axis): axis = axis + len(shape) + 1 return a.reshape(shape[:axis] + (1,) + shape[axis:]) - -def atleast_1d(*arys): - """ - Convert inputs to arrays with at least one dimension. - - Scalar inputs are converted to 1-dimensional arrays, whilst - higher-dimensional inputs are preserved. - - Parameters - ---------- - array1, array2, ... : array_like - One or more input arrays. - - Returns - ------- - ret : ndarray - An array, or sequence of arrays, each with ``a.ndim >= 1``. - Copies are made only if necessary. - - See Also - -------- - atleast_2d, atleast_3d - - Examples - -------- - >>> np.atleast_1d(1.0) - array([ 1.]) - - >>> x = np.arange(9.0).reshape(3,3) - >>> np.atleast_1d(x) - array([[ 0., 1., 2.], - [ 3., 4., 5.], - [ 6., 7., 8.]]) - >>> np.atleast_1d(x) is x - True - - >>> np.atleast_1d(1, [3, 4]) - [array([1]), array([3, 4])] - - """ - res = [] - for ary in arys: - res.append(array(ary,copy=False,subok=True,ndmin=1)) - if len(res) == 1: - return res[0] - else: - return res - -def atleast_2d(*arys): - """ - View inputs as arrays with at least two dimensions. - - Parameters - ---------- - array1, array2, ... : array_like - One or more array-like sequences. Non-array inputs are converted - to arrays. Arrays that already have two or more dimensions are - preserved. - - Returns - ------- - res, res2, ... : ndarray - An array, or tuple of arrays, each with ``a.ndim >= 2``. - Copies are avoided where possible, and views with two or more - dimensions are returned. - - See Also - -------- - atleast_1d, atleast_3d - - Examples - -------- - >>> np.atleast_2d(3.0) - array([[ 3.]]) - - >>> x = np.arange(3.0) - >>> np.atleast_2d(x) - array([[ 0., 1., 2.]]) - >>> np.atleast_2d(x).base is x - True - - >>> np.atleast_2d(1, [1, 2], [[1, 2]]) - [array([[1]]), array([[1, 2]]), array([[1, 2]])] - - """ - res = [] - for ary in arys: - res.append(array(ary,copy=False,subok=True,ndmin=2)) - if len(res) == 1: - return res[0] - else: - return res - -def atleast_3d(*arys): - """ - View inputs as arrays with at least three dimensions. - - Parameters - ---------- - array1, array2, ... : array_like - One or more array-like sequences. Non-array inputs are converted - to arrays. Arrays that already have three or more dimensions are - preserved. - - Returns - ------- - res1, res2, ... : ndarray - An array, or tuple of arrays, each with ``a.ndim >= 3``. - Copies are avoided where possible, and views with three or more - dimensions are returned. For example, a 1-D array of shape ``N`` - becomes a view of shape ``(1, N, 1)``. A 2-D array of shape ``(M, N)`` - becomes a view of shape ``(M, N, 1)``. - - See Also - -------- - atleast_1d, atleast_2d - - Examples - -------- - >>> np.atleast_3d(3.0) - array([[[ 3.]]]) - - >>> x = np.arange(3.0) - >>> np.atleast_3d(x).shape - (1, 3, 1) - - >>> x = np.arange(12.0).reshape(4,3) - >>> np.atleast_3d(x).shape - (4, 3, 1) - >>> np.atleast_3d(x).base is x - True - - >>> for arr in np.atleast_3d([1, 2], [[1, 2]], [[[1, 2]]]): - ... print arr, arr.shape - ... - [[[1] - [2]]] (1, 2, 1) - [[[1] - [2]]] (1, 2, 1) - [[[1 2]]] (1, 1, 2) - - """ - res = [] - for ary in arys: - ary = asarray(ary) - if len(ary.shape) == 0: - result = ary.reshape(1,1,1) - elif len(ary.shape) == 1: - result = ary[newaxis,:,newaxis] - elif len(ary.shape) == 2: - result = ary[:,:,newaxis] - else: - result = ary - res.append(result) - if len(res) == 1: - return res[0] - else: - return res - - -def vstack(tup): - """ - Stack arrays in sequence vertically (row wise). - - Take a sequence of arrays and stack them vertically to make a single - array. Rebuild arrays divided by `vsplit`. - - Parameters - ---------- - tup : sequence of ndarrays - Tuple containing arrays to be stacked. The arrays must have the same - shape along all but the first axis. - - Returns - ------- - stacked : ndarray - The array formed by stacking the given arrays. - - See Also - -------- - hstack : Stack arrays in sequence horizontally (column wise). - dstack : Stack arrays in sequence depth wise (along third dimension). - concatenate : Join a sequence of arrays together. - vsplit : Split array into a list of multiple sub-arrays vertically. - - - Notes - ----- - Equivalent to ``np.concatenate(tup, axis=0)`` - - Examples - -------- - >>> a = np.array([1, 2, 3]) - >>> b = np.array([2, 3, 4]) - >>> np.vstack((a,b)) - array([[1, 2, 3], - [2, 3, 4]]) - - >>> a = np.array([[1], [2], [3]]) - >>> b = np.array([[2], [3], [4]]) - >>> np.vstack((a,b)) - array([[1], - [2], - [3], - [2], - [3], - [4]]) - - """ - return _nx.concatenate(map(atleast_2d,tup),0) - -def hstack(tup): - """ - Stack arrays in sequence horizontally (column wise). - - Take a sequence of arrays and stack them horizontally to make - a single array. Rebuild arrays divided by ``hsplit``. - - Parameters - ---------- - tup : sequence of ndarrays - All arrays must have the same shape along all but the second axis. - - Returns - ------- - stacked : ndarray - The array formed by stacking the given arrays. - - See Also - -------- - vstack : Stack arrays in sequence vertically (row wise). - dstack : Stack arrays in sequence depth wise (along third axis). - concatenate : Join a sequence of arrays together. - hsplit : Split array along second axis. - - Notes - ----- - Equivalent to ``np.concatenate(tup, axis=1)`` - - Examples - -------- - >>> a = np.array((1,2,3)) - >>> b = np.array((2,3,4)) - >>> np.hstack((a,b)) - array([1, 2, 3, 2, 3, 4]) - >>> a = np.array([[1],[2],[3]]) - >>> b = np.array([[2],[3],[4]]) - >>> np.hstack((a,b)) - array([[1, 2], - [2, 3], - [3, 4]]) - - """ - return _nx.concatenate(map(atleast_1d,tup),1) - row_stack = vstack def column_stack(tup): diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 5abf9aefe..714d0a78a 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -1,8 +1,81 @@ from numpy.testing import * import numpy as np +rlevel = 1 + +class TestRegression(TestCase): + def test_poly1d(self,level=rlevel): + """Ticket #28""" + assert_equal(np.poly1d([1]) - np.poly1d([1,0]), + np.poly1d([-1,1])) + + def test_cov_parameters(self,level=rlevel): + """Ticket #91""" + x = np.random.random((3,3)) + y = x.copy() + np.cov(x,rowvar=1) + np.cov(y,rowvar=0) + assert_array_equal(x,y) + + def test_mem_digitize(self,level=rlevel): + """Ticket #95""" + for i in range(100): + np.digitize([1,2,3,4],[1,3]) + np.digitize([0,1,2,3,4],[1,3]) + + def test_unique_zero_sized(self,level=rlevel): + """Ticket #205""" + assert_array_equal([], np.unique(np.array([]))) + + def test_mem_vectorise(self, level=rlevel): + """Ticket #325""" + vt = np.vectorize(lambda *args: args) + vt(np.zeros((1,2,1)), np.zeros((2,1,1)), np.zeros((1,1,2))) + vt(np.zeros((1,2,1)), np.zeros((2,1,1)), np.zeros((1,1,2)), np.zeros((2,2))) + + def test_mgrid_single_element(self, level=rlevel): + """Ticket #339""" + assert_array_equal(np.mgrid[0:0:1j],[0]) + assert_array_equal(np.mgrid[0:0],[]) + + def test_refcount_vectorize(self, level=rlevel): + """Ticket #378""" + def p(x,y): return 123 + v = np.vectorize(p) + assert_valid_refcount(v) + + def test_poly1d_nan_roots(self, level=rlevel): + """Ticket #396""" + p = np.poly1d([np.nan,np.nan,1], r=0) + self.failUnlessRaises(np.linalg.LinAlgError,getattr,p,"r") + + def test_mem_polymul(self, level=rlevel): + """Ticket #448""" + np.polymul([],[1.]) + + def test_mem_string_concat(self, level=rlevel): + """Ticket #469""" + x = np.array([]) + np.append(x,'asdasd\tasdasd') + + def test_poly_div(self, level=rlevel): + """Ticket #553""" + u = np.poly1d([1,2,3]) + v = np.poly1d([1,2,3,4,5]) + q,r = np.polydiv(u,v) + assert_equal(q*v + r, u) + + def test_poly_eq(self, level=rlevel): + """Ticket #554""" + x = np.poly1d([1,2,3]) + y = np.poly1d([3,4]) + assert x != y + assert x == x + + def test_mem_insert(self, level=rlevel): + """Ticket #572""" + np.lib.place(1,1,1) -class TestRegression(object): def test_polyfit_build(self): """Ticket #628""" ref = [-1.06123820e-06, 5.70886914e-04, -1.13822012e-01, @@ -24,14 +97,16 @@ class TestRegression(object): tested = np.polyfit(x, y, 4) assert_array_almost_equal(ref, tested) - def test_polyint_type(self) : - """Ticket #944""" - msg = "Wrong type, should be complex" - x = np.ones(3, dtype=np.complex) - assert_(np.polyint(x).dtype == np.complex, msg) - msg = "Wrong type, should be float" - x = np.ones(3, dtype=np.int) - assert_(np.polyint(x).dtype == np.float, msg) + def test_hist_bins_as_list(self, level=rlevel): + """Ticket #632""" + import warnings + warnings.simplefilter('ignore', Warning) + try: + hist,edges = np.histogram([1,2,3,4],[1,2], new=False) + assert_array_equal(hist,[1,3]) + assert_array_equal(edges,[1,2]) + finally: + warnings.resetwarnings() def test_polydiv_type(self) : """Make polydiv work for complex types""" @@ -48,10 +123,45 @@ class TestRegression(object): """Ticket 928.""" assert_raises(ValueError, np.histogramdd, np.ones((1,10)), bins=2**10) + def test_polyint_type(self) : + """Ticket #944""" + msg = "Wrong type, should be complex" + x = np.ones(3, dtype=np.complex) + assert_(np.polyint(x).dtype == np.complex, msg) + msg = "Wrong type, should be float" + x = np.ones(3, dtype=np.int) + assert_(np.polyint(x).dtype == np.float, msg) + def test_ndenumerate_crash(self): """Ticket 1140""" # Shouldn't crash: list(np.ndenumerate(np.array([[]]))) + def test_asfarray_none(self, level=rlevel): + """Test for changeset r5065""" + assert_array_equal(np.array([np.nan]), np.asfarray([None])) + + def test_large_fancy_indexing(self, level=rlevel): + # Large enough to fail on 64-bit. + nbits = np.dtype(np.intp).itemsize * 8 + thesize = int((2**nbits)**(1.0/5.0)+1) + def dp(): + n = 3 + a = np.ones((n,)*5) + i = np.random.randint(0,n,size=thesize) + a[np.ix_(i,i,i,i,i)] = 0 + def dp2(): + n = 3 + a = np.ones((n,)*5) + i = np.random.randint(0,n,size=thesize) + g = a[np.ix_(i,i,i,i,i)] + self.failUnlessRaises(ValueError, dp) + self.failUnlessRaises(ValueError, dp2) + + def test_void_coercion(self, level=rlevel): + dt = np.dtype([('a','f4'),('b','i4')]) + x = np.zeros((1,),dt) + assert(np.r_[x,x].dtype == dt) + if __name__ == "__main__": run_module_suite() diff --git a/numpy/lib/tests/test_shape_base.py b/numpy/lib/tests/test_shape_base.py index 66c396324..403761e93 100644 --- a/numpy/lib/tests/test_shape_base.py +++ b/numpy/lib/tests/test_shape_base.py @@ -154,145 +154,6 @@ class TestSplit(TestCase): pass -class TestAtleast1d(TestCase): - def test_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 test_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 test_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 test_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 test_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 TestAtleast2d(TestCase): - def test_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 test_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 test_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 test_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 test_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 TestAtleast3d(TestCase): - def test_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 test_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 test_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 test_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 TestHstack(TestCase): - def test_0D_array(self): - a = array(1); b = array(2); - res=hstack([a,b]) - desired = array([1,2]) - assert_array_equal(res,desired) - - def test_1D_array(self): - a = array([1]); b = array([2]); - res=hstack([a,b]) - desired = array([1,2]) - assert_array_equal(res,desired) - - def test_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 TestVstack(TestCase): - def test_0D_array(self): - a = array(1); b = array(2); - res=vstack([a,b]) - desired = array([[1],[2]]) - assert_array_equal(res,desired) - - def test_1D_array(self): - a = array([1]); b = array([2]); - res=vstack([a,b]) - desired = array([[1],[2]]) - assert_array_equal(res,desired) - - def test_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 test_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 TestDstack(TestCase): def test_0D_array(self): a = array(1); b = array(2); |