[SciPy-User] Subclassing scipy sparse matrix class

167 views
Skip to first unread message

Per Nielsen

unread,
Nov 30, 2011, 4:01:27 AM11/30/11
to scipy...@scipy.org
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

-----------------

#!/usr/bin/env python

from scipy.sparse.csr import csr_matrix as spmatrix

class sparsematrix_addons(spmatrix):
    """
    subclass for standard scipy sparse class to add missing functionality
    """

    def __new__(cls, matrix):
        obj = spmatrix.__init__(cls, matrix)
        return obj

    def square_spmat(self, M):
        return M ** 2

    def ravel_spmat(self, M):
        pass

if __name__ == '__main__':

    import numpy as np

    x = (np.random.rand(10, 10) * 2).astype(int).astype(float)
    xsp = sparsematrix_addons(x)

--------------------

However, this generates the following error:

TypeError: unbound method __init__() must be called with csr_matrix instance as first argument (got type instance instead)

I am not strong in python OOP, or OOP in general, so I am sure this is a rather trivial problem to solve.

Anyone got any solutions or ideas to point me in the right direction?

Thanks in advance,

Per

Aronne Merrelli

unread,
Dec 1, 2011, 5:36:04 PM12/1/11
to SciPy Users List
On Wed, Nov 30, 2011 at 3:01 AM, Per Nielsen <evi...@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:

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 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


Per Nielsen

unread,
Dec 2, 2011, 3:47:41 AM12/2/11
to SciPy Users List
Hi all,

Indeed it seems to work as I wanted it to. Thanks alot for the help :)

Per

_______________________________________________
SciPy-User mailing list
SciPy...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user


Bruce Southey

unread,
Dec 2, 2011, 9:39:24 AM12/2/11
to scipy...@scipy.org
On 12/01/2011 04:36 PM, Aronne Merrelli wrote:

On Wed, Nov 30, 2011 at 3:01 AM, Per Nielsen <evi...@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

_______________________________________________ SciPy-User mailing list SciPy...@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user

Aronne Merrelli

unread,
Dec 2, 2011, 10:52:07 PM12/2/11
to SciPy Users List
On Fri, Dec 2, 2011 at 8:39 AM, Bruce Southey <bsou...@gmail.com> wrote:
On 12/01/2011 04:36 PM, Aronne Merrelli wrote:

On Wed, Nov 30, 2011 at 3:01 AM, Per Nielsen <evi...@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.


Well, not exactly - it looks the actual values and indices defining the sparse matrix are stored inside numpy ndarrays in separate object attributes, even though the sparse matrix itself is just a "plain" python object. So it is not quite "self-contained". I'm sure there are good reasons for implementing it that way, but it isn't obvious without knowing those reasons why it couldn't be a direct subclass of ndarray.


Aronne
Reply all
Reply to author
Forward
0 new messages