Subclassing scipy sparse matrix class
![](https://secure.gravatar.com/avatar/17e9b68ea7a65829d6905c79363c26f7.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/242ff9f31f6fd94d69a65573c45e04ac.jpg?s=120&d=mm&r=g)
On Wed, Nov 30, 2011 at 3:01 AM, Per Nielsen <evilper@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
![](https://secure.gravatar.com/avatar/17e9b68ea7a65829d6905c79363c26f7.jpg?s=120&d=mm&r=g)
Hi all, Indeed it seems to work as I wanted it to. Thanks alot for the help :) Per On Thu, Dec 1, 2011 at 23:36, Aronne Merrelli <aronne.merrelli@gmail.com>wrote:
On Wed, Nov 30, 2011 at 3:01 AM, Per Nielsen <evilper@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
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/38d5ac232150013cbf1a4639538204c0.jpg?s=120&d=mm&r=g)
On 12/01/2011 04:36 PM, Aronne Merrelli wrote:
On Wed, Nov 30, 2011 at 3:01 AM, Per Nielsen <evilper@gmail.com <mailto:evilper@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@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/242ff9f31f6fd94d69a65573c45e04ac.jpg?s=120&d=mm&r=g)
On Fri, Dec 2, 2011 at 8:39 AM, Bruce Southey <bsouthey@gmail.com> wrote:
** On 12/01/2011 04:36 PM, Aronne Merrelli wrote:
On Wed, Nov 30, 2011 at 3:01 AM, Per Nielsen <evilper@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
participants (3)
-
Aronne Merrelli
-
Bruce Southey
-
Per Nielsen