-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
scipy.sparse.csgraph.shortest_path
does not work on scipy.sparse.csr_matrix
or lil_matrix
#3466
Comments
It looks like a problem with the validation function. I've put a fix in #3467. Thanks for the report! |
Thanks @jakevdp! |
Yes - this workaround should work: import numpy as np
from scipy.sparse.csgraph._validation import validate_graph
from scipy.sparse.csgraph import shortest_path
G = ... # G is the graph you're using
directed = True # this is the default value in shortest_path. Setting it to False
# is fine, as long as the same value is used in both places below
G = validate_graph(G, directed, np.float64)
shortest_path(G, directed=directed) If that doesn't work for you, then my patch is fixing the wrong problem... |
Unfortunately, it didn't work for me. Which means that the problem is elsewhere. Here is my code:
and here is the stack trace:
If it can help you, in the same situation, |
Okay, thanks for following up... This is the line that's causing the problem: if ((isspmatrix(csgraph) and np.any(csgraph.data < 0)) or np.any(csgraph < 0)):
method = 'J' I think I see the issue now. The logic here is flawed. I think it should say the following: if ((isspmatrix(csgraph) and np.any(csgraph.data < 0))
or (not isspmatrix(csgraph) and np.any(csgraph < 0))):
method = 'J' A fix for your situation would be to call the function with |
I updated the fix, including adding a clarifying comment on why the result of @chiffa, let me know if setting |
@jakevdp, yep, this has actually solved the problem, thanks! |
Great! Again, thanks for reporting this bug. |
Thanks for answering and reacting so fast! Without this function my code would go from ten lines to literally thousands of lines. |
When called on
scipy.sparse.csr_matrix
orscipy.sparse.lil_matrix
,scipy.sparse.csgraph.shortest_path
emits the following error:File "_shortest_path.pyx", line 134, in scipy.sparse.csgraph._shortest_path.shortest_path (scipy/sparse/csgraph/_shortest_path.c:2172) File "/.../python2.7/site-packages/scipy/sparse/base.py", line 183, in __bool__ raise ValueError("The truth value of an array with more than one " ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
The text was updated successfully, but these errors were encountered: