[Scipy-svn] r3688 - in trunk/scipy/sparse: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Tue Dec 18 21:08:32 EST 2007


Author: wnbell
Date: 2007-12-18 20:08:27 -0600 (Tue, 18 Dec 2007)
New Revision: 3688

Added:
   trunk/scipy/sparse/tests/test_sputils.py
Modified:
   trunk/scipy/sparse/compressed.py
   trunk/scipy/sparse/coo.py
   trunk/scipy/sparse/sputils.py
Log:
added sputils unittests
other minor changes


Modified: trunk/scipy/sparse/compressed.py
===================================================================
--- trunk/scipy/sparse/compressed.py	2007-12-18 23:20:23 UTC (rev 3687)
+++ trunk/scipy/sparse/compressed.py	2007-12-19 02:08:27 UTC (rev 3688)
@@ -64,7 +64,7 @@
                 M, N = self.shape
                 self.data    = zeros(0, getdtype(dtype, default=float))
                 self.indices = zeros(0, intc)
-                self.indptr  = zeros(self._swap(self.shape)[0] + 1, dtype='intc')
+                self.indptr  = zeros(self._swap((M,N))[0] + 1, dtype=intc)
             else:
                 if len(arg1) == 2:
                     # (data, ij) format
@@ -142,8 +142,8 @@
                     % self.indices.dtype.name )
 
         # only support 32-bit ints for now
-        self.indptr  = self.indptr.astype('intc')
-        self.indices = self.indices.astype('intc')
+        self.indptr  = self.indptr.astype(intc)
+        self.indices = self.indices.astype(intc)
         self.data    = to_native(self.data)
 
         # check array shapes

Modified: trunk/scipy/sparse/coo.py
===================================================================
--- trunk/scipy/sparse/coo.py	2007-12-18 23:20:23 UTC (rev 3687)
+++ trunk/scipy/sparse/coo.py	2007-12-19 02:08:27 UTC (rev 3688)
@@ -178,8 +178,8 @@
                     % self.col.dtype.name )
        
         # only support 32-bit ints for now
-        self.row  = self.row.astype('intc')
-        self.col  = self.col.astype('intc')
+        self.row  = self.row.astype(intc)
+        self.col  = self.col.astype(intc)
         self.data = to_native(self.data)
 
         if nnz > 0:

Modified: trunk/scipy/sparse/sputils.py
===================================================================
--- trunk/scipy/sparse/sputils.py	2007-12-18 23:20:23 UTC (rev 3687)
+++ trunk/scipy/sparse/sputils.py	2007-12-19 02:08:27 UTC (rev 3688)
@@ -1,7 +1,7 @@
 """ Utility functions for sparse matrix module
 """
 
-__all__ = ['getdtype','isscalarlike','isintlike',
+__all__ = ['upcast','getdtype','isscalarlike','isintlike',
             'isshape','issequence','isdense']
 
 import numpy
@@ -58,6 +58,7 @@
     are both None, construct a data type out of the 'default' parameter.
     Furthermore, 'dtype' must be in 'allowed' set.
     """
+    #TODO is this really what we want?
     canCast = True
     if dtype is None:
         try:

Added: trunk/scipy/sparse/tests/test_sputils.py
===================================================================
--- trunk/scipy/sparse/tests/test_sputils.py	2007-12-18 23:20:23 UTC (rev 3687)
+++ trunk/scipy/sparse/tests/test_sputils.py	2007-12-19 02:08:27 UTC (rev 3688)
@@ -0,0 +1,76 @@
+"""unit tests for sparse utility functions"""
+
+import numpy
+
+from numpy.testing import *
+set_package_path()
+from scipy.sparse.sputils import *
+restore_path()
+
+
+
+class TestSparseUtils(NumpyTestCase):
+
+    def check_upcast(self):
+        assert_equal(upcast('int32'),numpy.int32)
+        assert_equal(upcast('int32','float32'),numpy.float64)
+        assert_equal(upcast('bool',complex,float),numpy.complex128)
+        assert_equal(upcast('i','d'),numpy.float64)
+
+    def check_getdtype(self):
+        A = numpy.array([1],dtype='int8')
+
+        assert_equal(getdtype(None,default=float),numpy.float)
+        assert_equal(getdtype(None,a=A),numpy.int8)
+
+    def check_isscalarlike(self):
+        assert_equal(isscalarlike(3.0),True)
+        assert_equal(isscalarlike(-4),True)
+        assert_equal(isscalarlike(2.5),True)
+        assert_equal(isscalarlike(1 + 3j),True)
+        assert_equal(isscalarlike(numpy.array(3)),True)
+        assert_equal(isscalarlike( "16" ), True)
+
+        assert_equal(isscalarlike( numpy.array([3])), False)
+        assert_equal(isscalarlike( [[3]] ), False)
+        assert_equal(isscalarlike( (1,) ), False)
+        assert_equal(isscalarlike( (1,2) ), False)
+
+    def check_isintlike(self):
+        assert_equal(isintlike(3.0),True)
+        assert_equal(isintlike(-4),True)
+        assert_equal(isintlike(numpy.array(3)),True)
+        assert_equal(isintlike( numpy.array([3])), True)
+
+        assert_equal(isintlike(2.5),False)
+        assert_equal(isintlike(1 + 3j),False)
+        assert_equal(isintlike( (1,) ), False)
+        assert_equal(isintlike( (1,2) ), False)
+
+    def check_isshape(self):
+        assert_equal(isshape( (1,2) ),True)
+        assert_equal(isshape( (5,2) ),True)
+
+        assert_equal(isshape( (-1,4) ),False)
+        assert_equal(isshape( (1.5,2) ),False)
+        assert_equal(isshape( (0,4) ),False)
+        assert_equal(isshape( (2,2,2) ),False)
+
+    def check_issequence(self):
+        assert_equal(issequence( (1,) ),True)
+        assert_equal(issequence( (1,2,3) ),True)
+        assert_equal(issequence( [1] ),True)
+        assert_equal(issequence( [1,2,3] ),True)
+        assert_equal(issequence( numpy.array([1,2,3]) ),True)
+        
+        assert_equal(issequence( numpy.array([[1],[2],[3]]) ),False)
+        assert_equal(issequence( 3 ),False)
+
+    def check_isdense(self):
+        assert_equal(isdense( numpy.array([1]) ),True)
+        assert_equal(isdense( numpy.matrix([1]) ),True)
+                
+if __name__ == "__main__":
+    NumpyTest().run()
+
+




More information about the Scipy-svn mailing list