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

Call to scipy.sparse.coo_matrix fails when passed Cython typed memoryviews #5123

Closed
jbweston opened this issue Aug 7, 2015 · 2 comments · Fixed by #6120
Closed

Call to scipy.sparse.coo_matrix fails when passed Cython typed memoryviews #5123

jbweston opened this issue Aug 7, 2015 · 2 comments · Fixed by #6120
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse
Milestone

Comments

@jbweston
Copy link
Contributor

jbweston commented Aug 7, 2015

Under Python 3, when calling scipy.sparse.coo_matrix from a Cython extension module and passing Cython typed memoryviews as arguments, the call raises a ValueError. What follows is an example of the relevant calling code:

import scipy.sparse as sp

...  # snip

cdef signed int [:] to_off
cdef signed int [:] to_off
cdef signed int [:, :] rows_cols
cdef complex [:] data

...  # snip -- assign the memoryviews to valid buffers

return sp.coo_matrix((data[:], rows_cols[:, :]), shape=(to_off[-1], from_off[-1]))

Produces the following traceback

Traceback (most recent call last):
  File "/home/jbw/data/src/kwant/python3-env/lib/python3.4/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/jbw/data/src/kwant/python3-env/lib/python3.4/site-packages/kwant/tests/test_system.py", line 33, in test_hamiltonian_submatrix
    mat = sys2.hamiltonian_submatrix(sparse=True)
  File "kwant/_system.pyx", line 313, in kwant._system.hamiltonian_submatrix (kwant/_system.c:8029)
  File "kwant/_system.pyx", line 151, in kwant._system.make_sparse_full (kwant/_system.c:4482)
  File "/home/jbw/data/src/kwant/python3-env/lib/python3.4/site-packages/scipy/sparse/coo.py", line 120, in __init__
    if isshape(arg1):
  File "/home/jbw/data/src/kwant/python3-env/lib/python3.4/site-packages/scipy/sparse/sputils.py", line 212, in isshape
    if isintlike(M) and isintlike(N):
  File "/home/jbw/data/src/kwant/python3-env/lib/python3.4/site-packages/scipy/sparse/sputils.py", line 195, in isintlike
    if int(x) == x:
ValueError: invalid literal for int() with base 10: b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\x00\x00\x00

The problem has been traced to scipy.sparse.sputils.isintlike. There is a call to int inside a try block where only TypeError is caught and handled. In Python 2 calling int on a memoryview produced a TypeError, but in Python 3 it produces a ValueError.

The problem has been reproduced with the current development version of Scipy: 0.17.0.dev0+fa26145 and Python 3.4.3+.

@jbweston
Copy link
Contributor Author

jbweston commented Aug 7, 2015

It should be noted that the above does work under Python 2.7.

@ewmoore
Copy link
Member

ewmoore commented Aug 7, 2015

You can work around this by converting your memoryviews to numpy arrays. They'll share memory, so this shouldn't be a big deal. Additionally, if the memoryviews were detected as sequences, they'll be converted to arrays anyway.

I kinda suspect that you're going to find issues like this when feeding memoryviews to arbitrary functions. Most things that expect numpy arrays call asarray first, but not everything does and nothing is expecting to be passed a memoryview.

@rgommers rgommers added defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse labels Oct 9, 2015
perimosocordiae added a commit to perimosocordiae/scipy that referenced this issue Apr 30, 2016
This should fix scipygh-5123, and also removes some unnecessary branching.
@pv pv closed this as completed in #6120 Apr 30, 2016
@pv pv added this to the 0.18.0 milestone Apr 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants