[SciPy-User] Subclassing scipy sparse matrix class

Bruce Southey bsouthey at gmail.com
Fri Dec 2 09:39:24 EST 2011


On 12/01/2011 04:36 PM, Aronne Merrelli wrote:
>
> On Wed, Nov 30, 2011 at 3:01 AM, Per Nielsen <evilper at gmail.com 
> <mailto:evilper at gmail.com>> wrote:
>
>     Hi all
>
>     I am trying to create a subclass of the sparse matrix class in
>     scipy, to add some extra methods I need.
>
>     I have tried to follow the guide on:
>     http://www.scipy.org/Subclasses but without much luck, the view
>     method does not exist for the sparse matrix class. Below is a
>     script I have created
>
>
> Hi,
>
> It appears that sparse matrices do not inherit from numpy.ndarray:

Surely you did notice that sparse is part of scipy not numpy or even the 
c++ usage when looking at the code? :-)
As far as I know (which is not much) scipy.sparse is essentially 
self-contained in scipy/sparse directory. So you are better off just 
working with those files directly.

A common thought that I have when I reading about 'extra methods' is 
that other people could have them or would like them. So perhaps think 
about making a contribution.

Bruce

>
> In [5]: sparse_mat = csr_matrix( np.ones(3) )
> In [7]: isinstance( sparse_mat, np.ndarray )
> Out[7]: False
>
> So much of the numpy - specific information on that page at scipy.org 
> <http://scipy.org> is not relevant for a sparse matrix subclass. I 
> would assume subclassing csr_matrix would essentially look more like 
> plain python subclassing. However, playing around with this, I quickly 
> found what appears to be a sparse matrix-specific aspect. The sparse 
> matrix format is based on the name of the class - so if you want this 
> to work you have to name the subclass with the same 3 letters as the 
> desired subclass ("csr" in this case). Here is a minimal example that 
> works - note the fail_matrix doesn't work, and causes an attribute 
> error just because of the name:
>
> from scipy.sparse.csr import csr_matrix
>
> class csr_matrix_alt(csr_matrix):
>     def __init__(self, *args, **kwargs):
>         csr_matrix.__init__(self, *args, **kwargs)
>     def square_spmat(self):
>         return self ** 2
>
> class fail_matrix(csr_matrix):
>     pass
>
> x = np.array( [[1, 0], [1, 3]] )
> xsparse = csr_matrix_alt(x)
> xsparse_sq = xsparse.square_spmat()
>
> print xsparse.todense()
> print xsparse_sq.todense()
>
> xfail = fail_matrix(x)
>
>
>
> Here is the output I get, running from ipython:
>
> In [2]: execfile('spsub_example.py')
> [[1 0]
>  [1 3]]
> [[1 0]
>  [4 9]]
> ---------------------------------------------------------------------------
> AttributeError                            Traceback (most recent call 
> last)
> <snip>
> AttributeError: tofai not found
>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20111202/c3ea2b0a/attachment.html>


More information about the SciPy-User mailing list