Repository navigation
Prioritize edgelist representations in to_networkx_graph - #7424
Merged
Merged
Conversation
rossbar
commented
Apr 18, 2024
| except Exception as err: | ||
| raise nx.NetworkXError("Input is not a correct NetworkX graph.") from err | ||
|
|
||
| # pygraphviz agraph |
Contributor
Author
There was a problem hiding this comment.
This is an unfortunately rendering of the diff... FWIW the check has not been modified at all, it was just moved so that the added edgelist check is first!
jarrodmillman
approved these changes
Apr 24, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR was originally motivated by #7401. The main issue there is the extraneous
ImportWarnings that are raised byto_networkx_graph(often via the graph constructors) when creating graphs in an environment without the default dependencies (numpy, scipy, pandas). This is the case because converting from these data structures is actually attempted before attempting to interpret the input as an edgelist. However, creating a graph from an edge list (or edgelist-like, such as an EdgeView or generator of edges) is a very common operation - certainly much more so than from numpy/scipy/pandas objects within NetworkX itself. This latter point can be demonstrated relatively simply: see #7402 (comment).I wanted to investigate whether it'd be possible to reorganize the control-flow of
to_networkx_graphso that the most common (at least according to the internal NX usage heuristic) data structures are handled first. This has the advantage of solving #7401 for basically all cases1. I suspect it may result in a minor performance improvement too, though the use-case where that's actually meaningful may not be that common (i.e. an analysis where many graph instances are being created). I've added a benchmark to demonstrate this last point as well; results on my machine:~10% improvement for this branch
The potential downside of this approach is that there may be subtle corner cases due to the change in the order/"breadth" of the isinstance checks. I'm confident that this works for every case that the NetworkX test suite covers (using the procedure described here), but there may be cases that are uncovered. For example, it turns out there is a single test in the test suite that uses a tuple of edges instead of a list of edges. That case would have been missed were it not for that single test!
Footnotes
The only exceptions being branches where an exception is explicitly raised. ↩