[Scipy-svn] r3770 - in trunk/scipy/io: matlab tests

scipy-svn@scip... scipy-svn@scip...
Thu Jan 3 13:06:02 CST 2008


Author: stefan
Date: 2008-01-03 13:05:43 -0600 (Thu, 03 Jan 2008)
New Revision: 3770

Modified:
   trunk/scipy/io/matlab/mio5.py
   trunk/scipy/io/tests/test_mio.py
Log:
Fix Matlab v5 sparse array writer.


Modified: trunk/scipy/io/matlab/mio5.py
===================================================================
--- trunk/scipy/io/matlab/mio5.py	2008-01-03 09:20:06 UTC (rev 3769)
+++ trunk/scipy/io/matlab/mio5.py	2008-01-03 19:05:43 UTC (rev 3770)
@@ -603,7 +603,8 @@
         af['nzmax'] = nzmax
         self.write_dtype(af)
         # write array shape
-        self.arr=N.atleast_2d(self.arr)
+        if self.arr.ndim < 2:
+            self.arr=N.atleast_2d(self.arr)
         self.write_element(N.array(self.arr.shape, dtype='i4'))
         # write name
         self.write_element(N.ndarray(shape=len(self.name), dtype='S1', buffer=self.name))
@@ -663,26 +664,23 @@
 
     def write(self):
         ''' Sparse matrices are 2D
-        See docstring for Mat5SparseGetter
+
         '''
-        imagf = self.arr.dtype.kind == 'c'
-        N = self.arr.nnz
-        ijd = N.zeros((N+1, 3+imagf), dtype='f8')
-        for i in range(N):
-            ijd[i,0], ijd[i,1] = self.arr.rowcol(i)
-        ijd[:-1,0:2] += 1 # 1 based indexing
-        if imagf:
-            ijd[:-1,2] = self.arr.data.real
-            ijd[:-1,3] = self.arr.data.imag
-        else:
-            ijd[:-1,2] = self.arr.data
-        ijd[-1,0:2] = self.arr.shape
-        self.write_header(P=miDOUBLE,
-                          T=mxSPARSE_CLASS,
-                          dims=ijd.shape)
-        self.write_bytes(ijd)
+        A = self.arr.tocsc() # convert to sparse COO format (ijv)
+        is_complex = (A.dtype.kind == 'c')
+        nz = A.nnz
 
+        self.write_header(mclass=mxSPARSE_CLASS,
+                          is_complex=is_complex,
+                          nzmax=nz)
+        self.write_element(A.indices.astype('i4'))
+        self.write_element(A.indptr.astype('i4'))
+        self.write_element(A.data.real)
+        if is_complex:
+            self.write_element(A.data.imag)
+        self.update_matrix_tag()
 
+
 class Mat5WriterGetter(object):
     ''' Wraps stream and options, provides methods for getting Writer objects '''
     def __init__(self, stream, unicode_strings):

Modified: trunk/scipy/io/tests/test_mio.py
===================================================================
--- trunk/scipy/io/tests/test_mio.py	2008-01-03 09:20:06 UTC (rev 3769)
+++ trunk/scipy/io/tests/test_mio.py	2008-01-03 19:05:43 UTC (rev 3770)
@@ -11,8 +11,8 @@
 import scipy.sparse as SP
 
 set_package_path()
-from matlab.mio import loadmat, savemat
-from matlab.mio5 import mat_obj, mat_struct
+from io.matlab.mio import loadmat, savemat
+from io.matlab.mio5 import mat_obj, mat_struct
 restore_path()
 
 try:  # Python 2.3 support
@@ -165,7 +165,14 @@
     case_table5_rt = [
         {'name': '3dmatrix',
          'expected': {'test3dmatrix': transpose(reshape(range(1,25), (4,3,2)))}
-         }]
+         },
+        {'name': 'sparsefloat',
+         'expected': {'testsparsefloat': SP.csc_matrix(array([[1,0,2],[0,-3.5,0]]))},
+         },
+        {'name': 'sparsecomplex',
+         'expected': {'testsparsefloat': SP.csc_matrix(array([[-1+2j,0,2],[0,-3j,0]]))},
+         },
+        ]
     st = mat_struct()
     st.stringfield = u'Rats live on no evil star.'
     st.doublefield = array([sqrt(2),exp(1),pi])



More information about the Scipy-svn mailing list