Skip to content

Add a nodelist feature to from_numpy_array - #7412

Merged
dschult merged 8 commits into
networkx:mainfrom
rossbar:from-numpy-nodes
Apr 15, 2024
Merged

dschult merged 8 commits into
networkx:mainfrom
rossbar:from-numpy-nodes

Conversation

@rossbar

@rossbar rossbar commented Apr 13, 2024

Copy link
Copy Markdown
Contributor

Adds a kwarg to allow users to specify a sequence of nodes which map to the dimensions of the adjacency matrix. The mapping implicitly relies on the ordering of the nodes in the adjacency matrix, i.e. node_labels[0] -> row 0, node_labels[1] -> row 1, etc.

I'm not sure about the name of the kwarg. One motivating factor for this proposal is it fixes #7407 in an efficient way. I've added that change in a1a17ac7, but I'm happy to break that into a separate PR in order to focus on the new feature proposal!

@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 really good! I like it. :)

I especially like the organization of the tests. And the choice of how/when to switch from idxs to nodes.

As far as the kwarg name, the term label could be confusing since sometimes label is an attribute of the node rather than the node itself. The choice nodelist matches the kwarg in the function to_numpy_array, so might be good. But of course it doesn't have to be of a list. So ... ???

It's great that you have included the simple example from #7407 here as a test.

Comment thread networkx/convert_matrix.py Resolved
@rossbar rossbar changed the title Add a node_labels feature to from_numpy_array Add a nodelist feature to from_numpy_array Apr 13, 2024
@rossbar

rossbar commented Apr 13, 2024

Copy link
Copy Markdown
Contributor Author

As far as the kwarg name, the term label could be confusing since sometimes label is an attribute of the node rather than the node itself. The choice nodelist matches the kwarg in the function to_numpy_array, so might be good. But of course it doesn't have to be of a list. So ... ???

I totally agree, thanks for the suggestion - renamed to nodelist in 86fee52. +1 for consistencey with to_numpy_array, even if the list part is not strictly the most precise term. I definitely prefer it to nodesequence= :)

The docstring has been updated as well from list -> sequence, which I think is the right abstraction. On this front, it's worth noting that the decision to require the nodelist object to have a length (thus excluding generators) was made arbitrarily. My thinking was that it was best to start with the most restrictive option which allows for some minor input validation and raise exceptions when the sequence is a different length than the adjacency matrix dimension. It's not a necessary restriction though - it could be removed to allow iterators as well. If that's something that's desired now I'm happy to try to add it, but I'm also content to leave it as a follow-up enhancement w/ a separate, more focused discussion!

@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 to me...

One suggestion. But I approve either way.

Comment thread networkx/tests/test_convert_numpy.py Resolved
@dschult
dschult merged commit d7b61bd into networkx:main Apr 15, 2024
@jarrodmillman jarrodmillman added this to the 3.4 milestone Apr 15, 2024
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
* Add tests for node_labels in from_numpy_array.

* Implement node_labels feature.

* Add round-tripping test for different simple graphs.

* Fix for networkxgh-7407.

* Rename node_labels -> nodelist.

Co-authored-by: Dan Schult <[email protected]>

* Update docstring.

Co-authored-by: Dan Schult <[email protected]>

* Add match to pytest.raises.

* Update networkx/tests/test_convert_numpy.py

Co-authored-by: Dan Schult <[email protected]>

---------

Co-authored-by: Dan Schult <[email protected]>
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.

networkx.exception.NetworkXUnfeasible when creating a graph from pandas

3 participants