You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello here and sorry for bothering you again. I noticed that nx.trophic_levels will sometimes return INF values for an unsupported graph instead of the following message.
networkx.exception.NetworkXError:
Trophic levels are only defined for graphs where every node has a path from a basal node (basal nodes are nodes with no incoming edges).
By investigating the source code, I found that the root cause may be the insufficient checking for whether the matrix can be inverse (reference link).
To solve the issue, the lines 57 to 66 of trophic.py could be changed by the following code:
ifnp.linalg.cond(i-p) <np.finfo((i-p).dtype).eps:
n=np.linalg.inv(i-p)
else:
# LinAlgError is raised when there is a non-basal nodemsg= (
"Trophic levels are only defined for graphs where every "+"node has a path from a basal node (basal nodes are nodes "+"with no incoming edges)."
)
raisenx.NetworkXError(msg)
After I use this effective way to check it, this problem is solved well. It would be highly appreciated if you could further confirm and investigate the issue. I will also create a PR for fixing it if you approve.
I also found that using np.linalg.cond(i - p) < np.finfo((i - p).dtype).eps to fix the issue may not be good, since it will crash in the empty graph. I think it will be good that if we use a method other than Linear Algebra to check whether the graph is supported. Because it will let us get rid of the rounding and the corner cases.
To fully avoid this issue, we can also use BFS to determine whether the graph is supported first. This will not cost much time since its time complexity is linear, compared with the time-consuming matrix operations.
Hello here and sorry for bothering you again. I noticed that
nx.trophic_levels
will sometimes return INF values for an unsupported graph instead of the following message.By investigating the source code, I found that the root cause may be the insufficient checking for whether the matrix can be inverse (reference link).
To solve the issue, the lines 57 to 66 of trophic.py could be changed by the following code:
After I use this effective way to check it, this problem is solved well. It would be highly appreciated if you could further confirm and investigate the issue. I will also create a PR for fixing it if you approve.
Best regards,
Joye
Step to Reproduce
Running the following Python code:
Actual Result:
an extremely large positive value
Expected Result:
Environment
Python 3.10
NetworkX 3.1
The text was updated successfully, but these errors were encountered: