Skip to content

trophic_levels now checks for paths from each node to a basal node - #7453

Merged
dschult merged 11 commits into
networkx:mainfrom
Aditya-Shandilya1182:bugfix-for-issue-6899
Nov 13, 2024
Merged

dschult merged 11 commits into
networkx:mainfrom
Aditya-Shandilya1182:bugfix-for-issue-6899

Conversation

@Aditya-Shandilya1182

@Aditya-Shandilya1182 Aditya-Shandilya1182 commented May 20, 2024 •

Copy link
Copy Markdown
Contributor

Fixes #6899

@Aditya-Shandilya1182

Copy link
Copy Markdown
Contributor Author

I added the function bfs_reachable_from_basal to perform BFS and determine nodes reachable from basal nodes.

  • Initialized visited and reachable_nodes sets to track visited nodes and reachable nodes respectively.
  • Iterated over basal nodes and used BFS to explore all reachable nodes, adding them to the reachable_nodes set.
  • Raised nx.NetworkXError if no basal nodes are present, indicating that trophic levels are not defined.
  • Checked if all nodes in the graph are reachable from the basal nodes, raising nx.NetworkXError if not.

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:

inf

Result after changes:

infERR

@Aditya-Shandilya1182

Copy link
Copy Markdown
Contributor Author

This code is written to address issue #6899.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@Aditya-Shandilya1182

Aditya-Shandilya1182 commented May 21, 2024 •

Copy link
Copy Markdown
Contributor Author

I will try to add the test cases.

@dschult

dschult commented May 23, 2024 •

Copy link
Copy Markdown
Member

I include a comment with the term

Fixes #6899

so that merging this PR automatically closes that issue.
More on linking a Pr to an Issue
[Edit: -- oops! that phrase has to be in the original post. I added it there so they are connected.]

@dschult dschult changed the title Added function to check path from bassal node trophic_levels now checks for paths from each node to a basal node May 23, 2024
@dschult

dschult commented May 23, 2024

Copy link
Copy Markdown
Member

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.

@eriknw

eriknw commented May 23, 2024 •

Copy link
Copy Markdown
Contributor

This looks like a reasonable approach. The latest dev version of np.linalg.inv says this:

If a is detected to be singular, a LinAlgError is raised. If a is ill-conditioned, a LinAlgError may or may not be raised, and results may be inaccurate due to floating-point errors.

Should a more clear error message be given for the null graph? nx.trophic_levels(nx.DiGraph()) currently raises NetworkXError: Graph has no nodes or edges

@Aditya-Shandilya1182

Copy link
Copy Markdown
Contributor Author

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.

Are you referring to the test case I added and then removed?

@dschult

dschult commented May 24, 2024

Copy link
Copy Markdown
Member

Yes -- I was referring to the test added and then removed. That tested the "no basal nodes" exception.

@Aditya-Shandilya1182

Copy link
Copy Markdown
Contributor Author

Should I add it again then?

@dschult

dschult commented May 24, 2024

Copy link
Copy Markdown
Member

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?

@Aditya-Shandilya1182

Copy link
Copy Markdown
Contributor Author

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)

@rossbar

rossbar commented Nov 11, 2024

Copy link
Copy Markdown
Contributor

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?

I'm certainly no expert on trophic levels, but the docstring does mention that:

and nodes with $k^{in}_i = 0$ have $s_i = 1$ by convention.

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 rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! Thanks @rossbar for getting this over the finish line!

The docs CI has a red X, but it think it is not related to this.

@dschult
dschult merged commit e2afb7f into networkx:main Nov 13, 2024
@jarrodmillman jarrodmillman added this to the 3.5 milestone Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

"nx.trophic_levels" returning strange INF value caused by insufficient checking for singular matrix

5 participants