[Scipy-svn] r3461 - in trunk/scipy/io: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Wed Oct 24 18:55:21 EDT 2007


Author: wnbell
Date: 2007-10-24 17:55:18 -0500 (Wed, 24 Oct 2007)
New Revision: 3461

Modified:
   trunk/scipy/io/mmio.py
   trunk/scipy/io/tests/test_mmio.py
Log:
simplified and improved speed of matrix market sparse IO
added complex sparse MMIO unittest 


Modified: trunk/scipy/io/mmio.py
===================================================================
--- trunk/scipy/io/mmio.py	2007-10-24 21:20:22 UTC (rev 3460)
+++ trunk/scipy/io/mmio.py	2007-10-24 22:55:18 UTC (rev 3461)
@@ -14,6 +14,7 @@
 
 import os
 from numpy import asarray, real, imag, conj, zeros, ndarray
+from itertools import izip
 
 __all__ = ['mminfo','mmread','mmwrite']
 
@@ -334,12 +335,16 @@
         format = '%i %i ' + format
         target.write('%i %i %i\n' % (rows,cols,entries))
         assert symm=='general',`symm`
+
+        coo = a.tocoo() # convert to COOrdinate format
+        I,J,V = coo.row + 1, coo.col + 1, coo.data # change base 0 -> base 1
+
         if field in ['real','integer']:
-            for i in range(entries):
-                target.write(format % (a.rowcol(i)[0] + 1,a.rowcol(i)[1] + 1,a.getdata(i))) #convert base 0 to base 1
+            for ijv_tuple in izip(I,J,V):
+                target.writelines(format % ijv_tuple)
         elif field=='complex':
-            for i in range(entries):
-                target.write(format % (a.rowcol(i)[0] + 1,a.rowcol(i)[1] + 1,real(a.getdata(i)),imag(a.getdata(i))))
+            for ijv_tuple in izip(I,J,V.real,V.imag):
+                target.writelines(format % ijv_tuple)
         elif field=='pattern':
             raise NotImplementedError,`field`
         else:

Modified: trunk/scipy/io/tests/test_mmio.py
===================================================================
--- trunk/scipy/io/tests/test_mmio.py	2007-10-24 21:20:22 UTC (rev 3460)
+++ trunk/scipy/io/tests/test_mmio.py	2007-10-24 22:55:18 UTC (rev 3461)
@@ -152,7 +152,7 @@
         b = mmread(fn).todense()
         assert_array_almost_equal(a,b)
 
-    def check_simple_write_read(self):
+    def check_real_write_read(self):
         I = array([0, 0, 1, 2, 3, 3, 3, 4])
         J = array([0, 3, 1, 2, 1, 3, 4, 4])
         V = array([  1.0,   6.0,   10.5, 0.015,   250.5,  -280.0, 33.32, 12.0 ])
@@ -163,14 +163,25 @@
         mmwrite(fn,b)
         
         assert_equal(mminfo(fn),(5,5,8,'coordinate','real','general'))
-        a = [[1,    0,      0,       6,      0],
-             [0,   10.5,    0,       0,      0],
-             [0,    0,    .015,      0,      0],
-             [0,  250.5,    0,     -280,    33.32],
-             [0,    0,      0,       0,     12]]
+        a = b.todense()
         b = mmread(fn).todense()
         assert_array_almost_equal(a,b)
 
+    def check_complex_write_read(self):
+        I = array([0, 0, 1, 2, 3, 3, 3, 4])
+        J = array([0, 3, 1, 2, 1, 3, 4, 4])
+        V = array([  1.0 + 3j,    6.0 + 2j,  10.50 + 0.9j, 0.015 + -4.4j,   
+                   250.5 + 0j, -280.0 + 5j,  33.32 + 6.4j, 12.00 + 0.8j])
+        
+        b = scipy.sparse.coo_matrix((V,(I,J)),dims=(5,5))
+        
+        fn = mktemp()
+        mmwrite(fn,b)
+        
+        assert_equal(mminfo(fn),(5,5,8,'coordinate','complex','general'))
+        a = b.todense()
+        b = mmread(fn).todense()
+        assert_array_almost_equal(a,b)
 
 if __name__ == "__main__":
     NumpyTest().run()




More information about the Scipy-svn mailing list