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

scipy-svn@scip... scipy-svn@scip...
Sun Oct 26 23:14:57 CDT 2008


Author: wnbell
Date: 2008-10-26 23:14:53 -0500 (Sun, 26 Oct 2008)
New Revision: 4856

Modified:
   trunk/scipy/sparse/base.py
   trunk/scipy/sparse/bsr.py
   trunk/scipy/sparse/compressed.py
   trunk/scipy/sparse/dia.py
   trunk/scipy/sparse/dok.py
   trunk/scipy/sparse/lil.py
   trunk/scipy/sparse/tests/test_base.py
Log:
fixed handling of dtype= constructor argument in sparse case
ensure that .astype(dtype) maintains sparse format


Modified: trunk/scipy/sparse/base.py
===================================================================
--- trunk/scipy/sparse/base.py	2008-10-27 03:56:22 UTC (rev 4855)
+++ trunk/scipy/sparse/base.py	2008-10-27 04:14:53 UTC (rev 4856)
@@ -87,7 +87,7 @@
         raise NotImplementedError
 
     def astype(self, t):
-        return self.tocsr().astype(t)
+        return self.tocsr().astype(t).asformat(self.format)
 
     def asfptype(self):
         """Upcast matrix to a floating point format (if necessary)"""

Modified: trunk/scipy/sparse/bsr.py
===================================================================
--- trunk/scipy/sparse/bsr.py	2008-10-27 03:56:22 UTC (rev 4855)
+++ trunk/scipy/sparse/bsr.py	2008-10-27 04:14:53 UTC (rev 4856)
@@ -156,17 +156,20 @@
                     M = len(self.indptr) - 1
                     N = self.indices.max() + 1
                 except:
-                    raise ValueError,'unable to infer matrix dimensions'
+                    raise ValueError('unable to infer matrix dimensions')
                 else:
                     R,C = self.blocksize
                     self.shape = (M*R,N*C)
 
         if self.shape is None:
             if shape is None:
-                #infer shape here
-                raise ValueError,'need to infer shape'
+                #TODO infer shape here
+                raise ValueError('need to infer shape')
             else:
                 self.shape = shape
+        
+        if dtype is not None:
+            self.data = self.data.astype(dtype)
 
         self.check_format(full_check=False)
 

Modified: trunk/scipy/sparse/compressed.py
===================================================================
--- trunk/scipy/sparse/compressed.py	2008-10-27 03:56:22 UTC (rev 4855)
+++ trunk/scipy/sparse/compressed.py	2008-10-27 04:14:53 UTC (rev 4856)
@@ -83,6 +83,9 @@
                     raise ValueError,'unable to infer matrix dimensions'
                 else:
                     self.shape = self._swap((major_dim,minor_dim))
+        
+        if dtype is not None:
+            self.data = self.data.astype(dtype)
 
         self.check_format(full_check=False)
 

Modified: trunk/scipy/sparse/dia.py
===================================================================
--- trunk/scipy/sparse/dia.py	2008-10-27 03:56:22 UTC (rev 4855)
+++ trunk/scipy/sparse/dia.py	2008-10-27 04:14:53 UTC (rev 4856)
@@ -100,6 +100,9 @@
             self.shape   = A.shape
 
 
+        if dtype is not None:
+            self.data = self.data.astype(dtype)
+
         #check format
         if self.offsets.ndim != 1:
             raise ValueError('offsets array must have rank 1')

Modified: trunk/scipy/sparse/dok.py
===================================================================
--- trunk/scipy/sparse/dok.py	2008-10-27 03:56:22 UTC (rev 4855)
+++ trunk/scipy/sparse/dok.py	2008-10-27 04:14:53 UTC (rev 4856)
@@ -59,6 +59,10 @@
                 arg1 = arg1.copy()
             else:
                 arg1 = arg1.todok()
+            
+            if dtype is not None:
+                arg1 = arg1.astype(dtype)
+
             self.update(arg1)
             self.shape = arg1.shape
             self.dtype = arg1.dtype

Modified: trunk/scipy/sparse/lil.py
===================================================================
--- trunk/scipy/sparse/lil.py	2008-10-27 03:56:22 UTC (rev 4855)
+++ trunk/scipy/sparse/lil.py	2008-10-27 04:14:53 UTC (rev 4856)
@@ -66,6 +66,10 @@
                 A = arg1.copy()
             else:
                 A = arg1.tolil()
+
+            if dtype is not None:
+                A = A.astype(dtype)
+
             self.shape = A.shape
             self.dtype = A.dtype
             self.rows  = A.rows

Modified: trunk/scipy/sparse/tests/test_base.py
===================================================================
--- trunk/scipy/sparse/tests/test_base.py	2008-10-27 03:56:22 UTC (rev 4855)
+++ trunk/scipy/sparse/tests/test_base.py	2008-10-27 04:14:53 UTC (rev 4856)
@@ -150,13 +150,13 @@
 
     def test_from_array(self):
         A = array([[1,0,0],[2,3,4],[0,5,0],[0,0,0]])
-        assert_array_equal(self.spmatrix(A).todense(), A)
+        assert_array_equal(self.spmatrix(A).toarray(), A)
         
         A = array([[1.0 + 3j,       0,      0],
                    [       0, 2.0 + 5,      0],
                    [       0,       0,      0]])
-        assert_array_equal(self.spmatrix(A).todense(), A)
-        assert_array_equal(self.spmatrix(A, dtype='int16').todense(), A.astype('int16'))
+        assert_array_equal(self.spmatrix(A).toarray(), A)
+        assert_array_equal(self.spmatrix(A, dtype='int16').toarray(), A.astype('int16'))
 
     def test_from_matrix(self):
         A = matrix([[1,0,0],[2,3,4],[0,5,0],[0,0,0]])
@@ -165,8 +165,8 @@
         A = matrix([[1.0 + 3j,       0,      0],
                     [       0, 2.0 + 5,      0],
                     [       0,       0,      0]])
-        assert_array_equal(self.spmatrix(A).todense(), A)
-        assert_array_equal(self.spmatrix(A, dtype='int16').todense(), A.astype('int16'))
+        assert_array_equal(self.spmatrix(A).toarray(), A)
+        assert_array_equal(self.spmatrix(A, dtype='int16').toarray(), A.astype('int16'))
 
     def test_from_list(self):
         A = [[1,0,0],[2,3,4],[0,5,0],[0,0,0]]
@@ -175,9 +175,27 @@
         A = [[1.0 + 3j,       0,      0],
              [       0, 2.0 + 5,      0],
              [       0,       0,      0]]
-        assert_array_equal(self.spmatrix(A).todense(), array(A))
+        assert_array_equal(self.spmatrix(A).toarray(), array(A))
         assert_array_equal(self.spmatrix(A, dtype='int16').todense(), array(A).astype('int16'))
 
+    def test_from_sparse(self):
+        D = array([[1,0,0],[2,3,4],[0,5,0],[0,0,0]])
+        S = csr_matrix(D)
+        assert_array_equal(self.spmatrix(S).toarray(), D)
+        S = self.spmatrix(D)
+        assert_array_equal(self.spmatrix(S).toarray(), D)
+
+
+        D = array([[1.0 + 3j,       0,      0],
+                   [       0, 2.0 + 5,      0],
+                   [       0,       0,      0]])
+        S = csr_matrix(D)
+        assert_array_equal(self.spmatrix(S).toarray(), D)
+        assert_array_equal(self.spmatrix(S, dtype='int16').toarray(), D.astype('int16'))
+        S = self.spmatrix(D)
+        assert_array_equal(self.spmatrix(S).toarray(), D)
+        assert_array_equal(self.spmatrix(S, dtype='int16').toarray(), D.astype('int16'))
+
     #def test_array(self):
     #    """test array(A) where A is in sparse format"""
     #    assert_equal( array(self.datsp), self.dat )



More information about the Scipy-svn mailing list