Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dok_matrix no longer allows update #8338

Open
amillb opened this issue Jan 28, 2018 · 5 comments
Open

dok_matrix no longer allows update #8338

amillb opened this issue Jan 28, 2018 · 5 comments

Comments

@amillb
Copy link

amillb commented Jan 28, 2018

In previous versions, scipy allowed an update on sparse.dok_matrix. This seems to be no longer the case with v1.0.0.

With version 0.19.0, no error is thrown, and the matrix is updated accordingly.

The docs imply that the update method is still supported. If this isn't a bug, is there a suggested approach to update a dok_matrix?

Reproducing code example:

from scipy import sparse
m = sparse.dok_matrix((10, 10))
m.update({(1,1):'a'})

Error message:

NotImplementedErrorTraceback (most recent call last)
<ipython-input-15-b3e12160bd9e> in <module>()
      1 from scipy import sparse
      2 m = sparse.dok_matrix((10, 10))
----> 3 m.update({(1,1):'a'})
      4 

/Users/amb/Library/Enthought/Canopy/edm/envs/User/lib/python2.7/site-packages/scipy/sparse/dok.pyc in update(self, val)
    115     def update(self, val):
    116         # Prevent direct usage of update
--> 117         raise NotImplementedError("Direct modification to dok_matrix element "
    118                                   "is not allowed.")
    119 

NotImplementedError: Direct modification to dok_matrix element is not allowed.

Scipy/Numpy/Python version information:

('1.0.0', '1.14.0', sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0))
@WarrenWeckesser
Copy link
Member

The change was made in #7673

@perimosocordiae, can you comment on this?

[...] is there a suggested approach to update a dok_matrix?

You can assign a value to a dok_matrix:

In [20]: m = sparse.dok_matrix((5, 10))

In [21]: m[1, 2] = -10.0

In [22]: m.A
Out[22]: 
array([[  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0., -10.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.]])

@amillb
Copy link
Author

amillb commented Jan 28, 2018

You can assign a value to a dok_matrix:

Thanks. However, the update function is convenient for assigning multiple values at once from a dictionary. I suppose that I could iterate through that dictionary, but that's more cumbersome.

@rgommers rgommers added defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse labels Jan 28, 2018
@perimosocordiae
Copy link
Member

The update method sets values directly into the dict that backs a dok_matrix, a consequence of subclassing dict. We removed access to it intentionally because it bypasses the sanity checking that normal assignment provides, and makes it easy to accidentally put the matrix into an inconsistent state.

The recommended way to bulk-add elements to a DOK matrix is either (a) use fancy indexing to assign like you would for a dense array/matrix, or (b) loop over your values and use scalar-index assignment.

If you really must have the speed of a raw dict.update, you can use the internal dok_matrix._update method, which bypasses the checks and just inserts whatever you give it.

@perimosocordiae perimosocordiae removed the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Jan 29, 2018
@WarrenWeckesser
Copy link
Member

Wouldn't it be possible to implement an update method that acted like dict.update but also did the appropriate sanity checking?

@perimosocordiae
Copy link
Member

Sure, that seems reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants