[Numpy-svn] r5404 - trunk/numpy/lib/tests

numpy-svn at scipy.org numpy-svn at scipy.org
Sun Jul 13 17:47:49 EDT 2008


Author: alan.mcintyre
Date: 2008-07-13 16:47:43 -0500 (Sun, 13 Jul 2008)
New Revision: 5404

Modified:
   trunk/numpy/lib/tests/test_function_base.py
   trunk/numpy/lib/tests/test_index_tricks.py
   trunk/numpy/lib/tests/test_io.py
   trunk/numpy/lib/tests/test_shape_base.py
   trunk/numpy/lib/tests/test_type_check.py
Log:
Added tests to improve coverage.
Renamed TestPiecewise methods so that they will be picked up by nose.


Modified: trunk/numpy/lib/tests/test_function_base.py
===================================================================
--- trunk/numpy/lib/tests/test_function_base.py	2008-07-13 21:43:50 UTC (rev 5403)
+++ trunk/numpy/lib/tests/test_function_base.py	2008-07-13 21:47:43 UTC (rev 5404)
@@ -616,8 +616,171 @@
         assert(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)
+
+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       ],
+                         [ 0.84386844,  0.38944762,  0.23913896],
+                         [ 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],
+                                   [ 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],
+                                   [ 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],
+                                   [ 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],
+                                   [ 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],
+                                   [ 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 ],
+                                   [ 0.37910852,  0.87964135,  0.34306596],
+                                   [ 0.72687499,  0.23913896,  0.33850425]]))
+
+    def test_nanargmin(self):
+        assert_almost_equal(nanargmin(self.A), 1)
+        assert_almost_equal(nanargmin(self.A,0), 
+                            array([[1, 0, 0],
+                                   [0, 2, 2],
+                                   [0, 0, 2]]))
+        assert_almost_equal(nanargmin(self.A,1), 
+                            array([[1, 0, 0],
+                                   [0, 1, 2],
+                                   [0, 2, 1]]))
+        assert_almost_equal(nanargmin(self.A,2), 
+                            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],
+                                   [ 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],
+                                   [ 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],
+                                   [ 0.59541557,  0.87964135,  0.70543747],
+                                   [ 0.91084584,  0.84386844,  0.37068164]]))
+
+
+class TestCorrCoef(TestCase):
+    def test_simple(self):
+        A = 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],
+                   [ 0.82872117,  0.77801674,  0.39226705],
+                   [ 0.9314666 ,  0.66800209,  0.03538394]])
+        assert_almost_equal(corrcoef(A), 
+                            array([[ 1.        ,  0.9379533 , -0.04931983],
+                                   [ 0.9379533 ,  1.        ,  0.30007991],
+                                   [-0.04931983,  0.30007991,  1.        ]]))
+        assert_almost_equal(corrcoef(A,B), 
+                            array([[ 1.        ,  0.9379533 , -0.04931983, 
+                                     0.30151751,  0.66318558, 0.51532523],
+                                   [ 0.9379533 ,  1.        , 0.30007991, 
+                                     -0.04781421,  0.88157256, 0.78052386],
+                                   [-0.04931983,  0.30007991,  1.        ,
+                                     -0.96717111,  0.71483595, 0.83053601],
+                                   [ 0.30151751, -0.04781421, -0.96717111,  
+                                     1.        , -0.51366032, -0.66173113],
+                                   [ 0.66318558,  0.88157256,  0.71483595, 
+                                     -0.51366032,  1.        , 0.98317823],
+                                   [ 0.51532523,  0.78052386,  0.83053601, 
+                                     -0.66173113,  0.98317823, 1.        ]]))
+
+
+
+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(A), 
+                            array([ 1.06307822,  1.12518299,  1.01214991,  1.00006049]))
+        B = 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],
+                                   [ 1.21147086,  1.0389829 ],
+                                   [ 1.03633899,  1.00067775],
+                                   [ 1.03352052,  1.13557954],
+                                   [ 1.0588429 ,  1.06432317]]))
+
+class TestKaiser(TestCase):
+    def test_simple(self):
+        assert_almost_equal(kaiser(0,1.0), array([]))
+        assert isnan(kaiser(1,1.0))
+        assert_almost_equal(kaiser(2,1.0), array([ 0.78984831,  0.78984831]))
+        assert_almost_equal(kaiser(5,1.0), 
+                            array([ 0.78984831,  0.94503323,  1.        , 
+                                    0.94503323,  0.78984831]))
+        assert_almost_equal(kaiser(5,1.56789), 
+                            array([ 0.58285404,  0.88409679,  1.        ,  
+                                    0.88409679,  0.58285404]))
+
+class TestMsort(TestCase):
+    def test_simple(self):
+        A = 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],
+                                   [ 0.44567325,  0.52929049,  0.5490053 ],
+                                   [ 0.64864341,  0.79115165,  0.96098397]]))
+
+class TestMeshgrid(TestCase):
+    def test_simple(self):
+        [X, Y] = meshgrid([1,2,3], [4,5,6,7])
+        assert all(X == array([[1, 2, 3],
+                               [1, 2, 3],
+                               [1, 2, 3],
+                               [1, 2, 3]]))
+        assert all(Y == array([[4, 4, 4],
+                               [5, 5, 5],
+                               [6, 6, 6],
+                               [7, 7, 7]]))
+
+
 class TestPiecewise(TestCase):
-    def check_simple(self):
+    def test_simple(self):
         # Condition is single bool list
         x = piecewise([0, 0], [True, False], [1])
         assert_array_equal(x, [1, 0])
@@ -645,7 +808,7 @@
         x = piecewise([1, 2], [[True, False], [False, True]], [3, 4])
         assert_array_equal(x, [3, 4])
 
-    def check_default(self):
+    def test_default(self):
         # No value specified for x[1], should be 0
         x = piecewise([1, 2], [True, False], [2])
         assert_array_equal(x, [2, 0])

Modified: trunk/numpy/lib/tests/test_index_tricks.py
===================================================================
--- trunk/numpy/lib/tests/test_index_tricks.py	2008-07-13 21:43:50 UTC (rev 5403)
+++ trunk/numpy/lib/tests/test_index_tricks.py	2008-07-13 21:47:43 UTC (rev 5404)
@@ -1,6 +1,13 @@
 from numpy.testing import *
-from numpy import array, ones, r_, mgrid
+from numpy import array, ones, r_, mgrid, unravel_index
 
+class TestUnravelIndex(TestCase):
+    def test_basic(self):
+        assert unravel_index(2,(2,2)) == (1,0)
+        assert unravel_index(254,(17,94)) == (2, 66)
+        assert_raises(ValueError, unravel_index, 4,(2,2))
+
+
 class TestGrid(TestCase):
     def test_basic(self):
         a = mgrid[-1:1:10j]
@@ -26,6 +33,7 @@
         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 TestConcatenator(TestCase):
     def test_1d(self):
         assert_array_equal(r_[1,2,3,4,5,6],array([1,2,3,4,5,6]))

Modified: trunk/numpy/lib/tests/test_io.py
===================================================================
--- trunk/numpy/lib/tests/test_io.py	2008-07-13 21:43:50 UTC (rev 5403)
+++ trunk/numpy/lib/tests/test_io.py	2008-07-13 21:47:43 UTC (rev 5404)
@@ -2,6 +2,63 @@
 import numpy as np
 import StringIO
 
+
+class RoundtripTest:
+    def test_array(self):
+        a = np.array( [[1,2],[3,4]], float)
+        self.do(a)
+
+        a = np.array( [[1,2],[3,4]], int)
+        self.do(a)
+
+        a = np.array( [[1+5j,2+6j],[3+7j,4+8j]], dtype=np.csingle)
+        self.do(a)
+
+        a = np.array( [[1+5j,2+6j],[3+7j,4+8j]], dtype=np.cdouble)
+        self.do(a)
+
+    def test_1D(self):
+        a = np.array([1,2,3,4], int)
+        self.do(a)
+
+    def test_record(self):
+        a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
+        self.do(a)
+
+class TestSaveLoad(RoundtripTest, TestCase):
+    def do(self, a):
+        c = StringIO.StringIO()
+        np.save(c, a)
+        c.seek(0)
+        a_reloaded = np.load(c)
+        assert_equal(a, a_reloaded)
+
+
+class TestSavezLoad(RoundtripTest, TestCase):
+    def do(self, *arrays):
+        c = StringIO.StringIO()
+        np.savez(c, *arrays)
+        c.seek(0)
+        l = np.load(c)
+        for n, a in enumerate(arrays):
+            assert_equal(a, l['arr_%d' % n])
+
+    def test_multiple_arrays(self):
+        a = np.array( [[1,2],[3,4]], float)
+        b = np.array( [[1+2j,2+7j],[3-6j,4+12j]], complex)
+        self.do(a,b)
+
+    def test_named_arrays(self):
+        a = np.array( [[1,2],[3,4]], float)
+        b = np.array( [[1+2j,2+7j],[3-6j,4+12j]], complex)
+        c = StringIO.StringIO()
+        np.savez(c, file_a=a, file_b=b)
+        c.seek(0)
+        l = np.load(c)
+        assert_equal(a, l['file_a'])
+        assert_equal(b, l['file_b'])
+        
+
 class TestSaveTxt(TestCase):
     def test_array(self):
         a =np.array( [[1,2],[3,4]], float)
@@ -32,6 +89,7 @@
         np.savetxt(c, a, fmt='%d')
         c.seek(0)
         assert_equal(c.readlines(), ['1 2\n', '3 4\n'])
+
     def test_delimiter(self):
         a = np.array([[1., 2.], [3., 4.]])
         c = StringIO.StringIO()

Modified: trunk/numpy/lib/tests/test_shape_base.py
===================================================================
--- trunk/numpy/lib/tests/test_shape_base.py	2008-07-13 21:43:50 UTC (rev 5403)
+++ trunk/numpy/lib/tests/test_shape_base.py	2008-07-13 21:47:43 UTC (rev 5404)
@@ -17,6 +17,13 @@
                            [[27,30,33],[36,39,42],[45,48,51]])
 
 
+class TestApplyOverAxes(TestCase):
+    def test_simple(self):
+        a = arange(24).reshape(2,3,4)
+        aoa_a = apply_over_axes(sum, a, [0,2])
+        assert_array_equal(aoa_a, array([[[60],[92],[124]]]))
+
+
 class TestArraySplit(TestCase):
     def test_integer_0_split(self):
         a = arange(10)

Modified: trunk/numpy/lib/tests/test_type_check.py
===================================================================
--- trunk/numpy/lib/tests/test_type_check.py	2008-07-13 21:43:50 UTC (rev 5403)
+++ trunk/numpy/lib/tests/test_type_check.py	2008-07-13 21:47:43 UTC (rev 5404)
@@ -1,11 +1,25 @@
 from numpy.testing import *
-import numpy.lib
 from numpy.lib import *
 from numpy.core import *
 
 def assert_all(x):
     assert(all(x)), x
 
+
+class TestCommonType(TestCase):
+    def test_basic(self):
+        ai32 = array([[1,2],[3,4]], dtype=int32)
+        af32 = array([[1,2],[3,4]], dtype=float32)
+        af64 = array([[1,2],[3,4]], dtype=float64)
+        acs = array([[1+5j,2+6j],[3+7j,4+8j]], dtype=csingle)
+        acd = array([[1+5j,2+6j],[3+7j,4+8j]], dtype=cdouble)
+        assert common_type(af32) == float32
+        assert common_type(af64) == float64
+        assert common_type(acs) == csingle
+        assert common_type(acd) == cdouble
+
+
+
 class TestMintypecode(TestCase):
 
     def test_default_1(self):




More information about the Numpy-svn mailing list