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

scipy-svn@scip... scipy-svn@scip...
Sun Feb 10 16:52:59 CST 2008


Author: wnbell
Date: 2008-02-10 16:52:55 -0600 (Sun, 10 Feb 2008)
New Revision: 3912

Modified:
   trunk/scipy/sparse/base.py
   trunk/scipy/sparse/compressed.py
   trunk/scipy/sparse/coo.py
   trunk/scipy/sparse/csc.py
   trunk/scipy/sparse/csr.py
   trunk/scipy/sparse/dok.py
   trunk/scipy/sparse/tests/test_base.py
Log:
made str() work for all sparse mats
deprecated rowcol() and getdata()


Modified: trunk/scipy/sparse/base.py
===================================================================
--- trunk/scipy/sparse/base.py	2008-02-09 21:28:39 UTC (rev 3911)
+++ trunk/scipy/sparse/base.py	2008-02-10 22:52:55 UTC (rev 3912)
@@ -6,7 +6,7 @@
 from warnings import warn
 
 import numpy
-from numpy import asarray, asmatrix, asanyarray, ones
+from numpy import asarray, asmatrix, asanyarray, ones, deprecate
 
 from sputils import isdense, isscalarlike, isintlike
 
@@ -136,9 +136,11 @@
             format = 'und'
         return format
 
+    @deprecate
     def rowcol(self, num):
         return (None, None)
 
+    @deprecate
     def getdata(self, num):
         return None
 
@@ -156,17 +158,26 @@
                (self.shape + (self.dtype.type, nnz, _formats[format][1]))
 
     def __str__(self):
+        maxprint = self.getmaxprint()
+
+        A   = self.tocoo()
         nnz = self.getnnz()
-        maxprint = self.getmaxprint()
-        val = ''
+
+        # helper function, outputs "(i,j)  v"
+        def tostr(row,col,data):
+            triples = zip(zip(row,col),data)
+            return '\n'.join( [ ('  %s\t%s' % t) for t in triples] )
+
         if nnz > maxprint:
-            val = val + self.listprint(0, maxprint/2)
-            val = val + "  :\t:\n"
-            val = val + self.listprint(nnz-maxprint//2, nnz)
+            half = maxprint // 2 
+            out  = tostr(A.row[:half], A.col[:half], A.data[:half])
+            out +=  + "  :\t:\n"
+            out += tostr(A.row[:-half], A.col[:-half], A.data[:-half])
         else:
-            val = val + self.listprint(0, nnz)
-        return val[:-1]
+            out  = tostr(A.row, A.col, A.data)
 
+        return out[:-1]
+
     def __nonzero__(self):  # Simple -- other ideas?
         return self.getnnz() > 0
 
@@ -487,7 +498,6 @@
             for i,v in enumerate(values[:max_index]):
                 self[i, i + k] = v
 
-
     def save(self, file_name, format = '%d %d %f\n'):
         #deprecated on Dec 14 2007
         #remove after 0.7 release

Modified: trunk/scipy/sparse/compressed.py
===================================================================
--- trunk/scipy/sparse/compressed.py	2008-02-09 21:28:39 UTC (rev 3911)
+++ trunk/scipy/sparse/compressed.py	2008-02-10 22:52:55 UTC (rev 3912)
@@ -8,7 +8,7 @@
 import numpy
 from numpy import array, matrix, asarray, asmatrix, zeros, rank, intc, \
         empty, hstack, isscalar, ndarray, shape, searchsorted, empty_like, \
-        where, concatenate, transpose
+        where, concatenate, transpose, deprecate
 
 from base import spmatrix, isspmatrix, SparseEfficiencyWarning
 from data import _data_matrix
@@ -379,6 +379,7 @@
         else:
             return self.transpose().matvec( other )
 
+    @deprecate
     def getdata(self, ind):
         return self.data[ind]
     

Modified: trunk/scipy/sparse/coo.py
===================================================================
--- trunk/scipy/sparse/coo.py	2008-02-09 21:28:39 UTC (rev 3911)
+++ trunk/scipy/sparse/coo.py	2008-02-10 22:52:55 UTC (rev 3912)
@@ -6,7 +6,8 @@
 from warnings import warn 
 
 from numpy import array, asarray, empty, intc, zeros,  \
-        unique, searchsorted, atleast_2d, empty_like, rank
+        unique, searchsorted, atleast_2d, empty_like, rank, \
+        deprecate
 
 from sparsetools import coo_tocsr, coo_tocsc, coo_todense
 from base import isspmatrix
@@ -216,9 +217,11 @@
         # some functions pass floats
         self.shape = tuple([int(x) for x in self.shape])
 
+    @deprecate
     def rowcol(self, num):
         return (self.row[num], self.col[num])
 
+    @deprecate
     def getdata(self, num):
         return self.data[num]
     

Modified: trunk/scipy/sparse/csc.py
===================================================================
--- trunk/scipy/sparse/csc.py	2008-02-09 21:28:39 UTC (rev 3911)
+++ trunk/scipy/sparse/csc.py	2008-02-10 22:52:55 UTC (rev 3912)
@@ -7,7 +7,7 @@
 import numpy
 from numpy import array, matrix, asarray, asmatrix, zeros, rank, intc, \
         empty, hstack, isscalar, ndarray, shape, searchsorted, where, \
-        concatenate
+        concatenate, deprecate
 
 from base import spmatrix, isspmatrix
 from sparsetools import csc_tocsr
@@ -104,9 +104,9 @@
         for r in xrange(self.shape[0]):
             yield csr[r,:]
 
+    @deprecate
     def rowcol(self, ind):
         #TODO remove after 0.7
-        warn('rowcol() is deprecated',DeprecationWarning)
         row = self.indices[ind]
         col = searchsorted(self.indptr, ind+1)-1
         return (row, col)

Modified: trunk/scipy/sparse/csr.py
===================================================================
--- trunk/scipy/sparse/csr.py	2008-02-09 21:28:39 UTC (rev 3911)
+++ trunk/scipy/sparse/csr.py	2008-02-10 22:52:55 UTC (rev 3912)
@@ -8,7 +8,7 @@
 import numpy
 from numpy import array, matrix, asarray, asmatrix, zeros, rank, intc, \
         empty, hstack, isscalar, ndarray, shape, searchsorted, where, \
-        concatenate
+        concatenate, deprecate
 
 from base import spmatrix, isspmatrix
 from sparsetools import csr_tocsc, csr_tobsr, csr_count_blocks
@@ -102,10 +102,9 @@
         M,N = self.shape
         return csc_matrix((self.data,self.indices,self.indptr),(N,M),copy=copy)
 
-
+    @deprecate
     def rowcol(self, ind):
         #TODO remove after 0.7
-        warn('rowcol() is deprecated',DeprecationWarning)
         col = self.indices[ind]
         row = searchsorted(self.indptr, ind+1)-1
         return (row, col)

Modified: trunk/scipy/sparse/dok.py
===================================================================
--- trunk/scipy/sparse/dok.py	2008-02-09 21:28:39 UTC (rev 3911)
+++ trunk/scipy/sparse/dok.py	2008-02-10 22:52:55 UTC (rev 3912)
@@ -23,7 +23,7 @@
         #TODO deprecate argument A in favor of arg1 style
 
         dict.__init__(self)
-        spmatrix.__init__(self,shape)
+        spmatrix.__init__(self)
         self.dtype = getdtype(dtype, A, default=float)
         if A is not None:
             if isinstance(A, tuple):
@@ -59,25 +59,6 @@
     def __len__(self):
         return dict.__len__(self)
 
-    def __str__(self):
-        val = ''
-        keys = self.keys()
-        keys.sort()
-        #TODO why does dok_matrix wipe out .maxprint?
-        if self.nnz > self.maxprint:
-            for k in xrange(self.maxprint / 2):
-                key = keys[k]
-                val += "  %s\t%s\n" % (str(key), str(self[key]))
-            val = val + "   :    \t  :\n"
-            for k in xrange(self.nnz - self.maxprint/2, self.nnz):
-                key = keys[k]
-                val += "  %s\t%s\n" % (str(key), str(self[key]))
-        else:
-            for k in xrange(self.nnz):
-                key = keys[k]
-                val += "  %s\t%s\n" % (str(key), str(self[key]))
-        return val[:-1]
-
     def get(self, key, default=0.):
         """This overrides the dict.get method, providing type checking
         but otherwise equivalent functionality.

Modified: trunk/scipy/sparse/tests/test_base.py
===================================================================
--- trunk/scipy/sparse/tests/test_base.py	2008-02-09 21:28:39 UTC (rev 3911)
+++ trunk/scipy/sparse/tests/test_base.py	2008-02-10 22:52:55 UTC (rev 3912)
@@ -33,10 +33,8 @@
 warnings.simplefilter('ignore',SparseEfficiencyWarning)
 
 
-
 #TODO check that invalid shape in constructor raises exception
 #TODO check that spmatrix( ... , copy=X ) is respected
-#TODO test repr(spmatrix)
 #TODO test prune
 #TODO test has_sorted_indices
 class _TestCommon:
@@ -47,9 +45,11 @@
         self.datsp = self.spmatrix(self.dat)
    
     def test_repr(self):
-        """make sure __repr__ works"""
-        repr(self.spmatrix)
+        repr(self.datsp)
 
+    def test_str(self):
+        str(self.datsp)
+
     def test_empty(self):
         """Test manipulating empty matrices. Fails in SciPy SVN <= r1768
         """



More information about the Scipy-svn mailing list