Repository navigation
trophic_levels now checks for paths from each node to a basal node - #7453
Conversation
|
I added the function
Following code was used to check the proper functioning of the feature. edges = [(0, 5), (0, 3), (0, 33), (3, 2), (3, 24), (3, 32), (33, 1), (33, 10), (2, 3), (2, 16), (1, 33), (10, 16), (16, 4), (16, 9), (18, 11), (11, 5), (11, 21), (4, 2), (4, 33), (9, 16), (9, 26), (9, 17), (26, 19), (17, 10), (19, 10), (19, 9), (19, 7), (19, 0)]
G = nx.DiGraph(edges)
res = nx.trophic_levels(G)
print(res[0])Result before changes: Result after changes: |
|
This code is written to address issue #6899. |
dschult
left a comment
There was a problem hiding this comment.
Thanks for this!
I think the bulk of this could call the existing nx.bfs_layers function which takes a parameter for multiple source nodes.
See below.
rossbar
left a comment
There was a problem hiding this comment.
At first glance this seems to be on the right track, thanks! This should also have some test cases to demonstrate the changed/fixed behavior in trophic_levels!
|
I will try to add the test cases. |
|
I include a comment with the term Fixes #6899 so that merging this PR automatically closes that issue. |
|
I think the tests actually passed -- it was a red X because one test got canceled, but rerunning it allowed it to pass. A second test could be a graph that works plus an isolated node. That will trigger the other exception added in this PR. |
|
This looks like a reasonable approach. The latest dev version of
Should a more clear error message be given for the null graph? |
Are you referring to the test case I added and then removed? |
|
Yes -- I was referring to the test added and then removed. That tested the "no basal nodes" exception. |
|
Should I add it again then? |
|
Yes -- could you add it again, and a test for a network that should work, but with an added isolated node so that it doesn't work -- and raises an exception from the 2nd code-check. Does this make sense? |
|
I tried adding a test for isolated nodes. But the test failed. Should I add a condition to handle isolated nodes? Or is there something I am missing here? Test: def test_trophic_levels_with_isolated_node():
G = nx.DiGraph()
# Adding edges to create a graph with a basal node
edges = [(0, 1), (1, 2), (2, 3)]
G.add_edges_from(edges)
# Adding an isolated node
G.add_node(4)
print(G.nodes)
with pytest.raises(nx.NetworkXError):
nx.trophic_levels(G) |
I'm certainly no expert on trophic levels, but the docstring does mention that:
which on it's own means that isolated nodes are currently handled properly: >>> G = nx.DiGraph([(0, 1), (1, 2), (2, 3)])
>>> G.add_node(4)
>>> nx.trophic_levels(G) # node 4 has trophic level 1, as expected
{0: 1, 4: 1, 1: 2.0, 2: 3.0, 3: 4.0}From a quick scan of the wikipedia article on trophic coherence I didn't see any specific mention of isolated nodes. My impression is that isolated nodes are sort of uninteresting in the context of trophic analysis, but like I said I'm no expert! |
rossbar
left a comment
There was a problem hiding this comment.
I took the liberty of pushing up some minor style changes to the tests, but otherwise this LGTM. I think there are likely to be some performance hits for larger graphs due to the additional input checking, but that seems to be the price of reliably catching these corner cases.
I think this also means we can remove the try-except around np.linalg.inv, but that can be left for a follow-up PR. Thanks @Aditya-Shandilya1182 !
dschult
left a comment
There was a problem hiding this comment.
I suggest a couple of changes below. Push back if there are better options.
- the first error message focus on "no basal nodes" rather than paths from basal nodes.
- add test that reaches second error message
- tweaked the iteration over
in_degree
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>


Fixes #6899