Skip to content

Nodelists for from_biadjacency_matrix#7993

Merged
rossbar merged 10 commits intonetworkx:mainfrom
LorentzFactor:nodelist-for-from-biadjacency-matrix
Nov 24, 2025
Merged

Nodelists for from_biadjacency_matrix#7993
rossbar merged 10 commits intonetworkx:mainfrom
LorentzFactor:nodelist-for-from-biadjacency-matrix

Conversation

@LorentzFactor
Copy link
Copy Markdown
Contributor

This pull request is a follow up to the discussion in #7960.

It adds two optional arguments to the from_biadjacency_matrix function: top_nodelist and bottom_nodelist. If either are specified, then the returned graph will use the provided lists to label the bipartite=0 and bipartite=1 nodes respectively. Otherwise, behavior is identical to before.

Comment on lines +119 to +120
top_nodelist=None,
bottom_nodelist=None,
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 think we should look more closely at established convention for the top/bottom arguments. It looks like top_nodes= is frequently used, and bottom_nodes is implied (so it's not a keyword argument).

Also, when adding new keyword arguments, we sometimes consider making them keyword only by using e.g. *, top_nodes=None in the signature. @rossbar, do you think keyword-only makes sense here?

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.

Good suggestions -- at least from my perspective:

  • only input top_nodes
  • make that new input keyword-only

I wonder if we have in the contributing docs that we prefer keyword only for new parameters. We should have that somewhere. :)

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 think we should look more closely at established convention for the top/bottom arguments. It looks like top_nodes= is frequently used, and bottom_nodes is implied (so it's not a keyword argument).

I think this is generally true, though IIUC the goal here is to provide a label for the nodes (i.e. instead of integers) in which case I wonder if the better approach isn't to break from the nodelist pattern and instead use an explicit mapping (though this assumes that the user will know what the integer indices will be a priori)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Regarding top_nodes vs. top_nodelist - top_nodes is reasonable choice, but I do think it's important to consider the conventions found in convert_matrix.py associated with creating graphs from adjacency matrices as well. There, they use nodelist for the analogous argument. The list part of the name also helps convey some information to the user - namely, that in this function the order of the input nodes matters - since top_nodelist(bottom_nodelist) will be mapped to the indices of A's rows(columns). That's not the case elsewhere within the module, so helping to highlight it here might be beneficial.

Additionally, I do think we need to keep the bottoms_nodes argument. Without it, users will only be able to provide custom labels to the top nodes of the output graph, corresponding to the rows of the input biadjacency matrix (since rows and columns here correspond to two different sets of nodes).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, crap. Did not refresh my page recently enough to see @rossbar's comment.

though IIUC the goal here is to provide a label for the nodes (i.e. instead of integers)

Yep, this is the goal here.

in which case I wonder if the better approach isn't to break from the nodelist pattern and instead use an explicit mapping (though this assumes that the user will know what the integer indices will be a priori)

This sounds like it could be a good idea to me. In any implementation, the user will need to know the indices a priori anyhow to meaningfully construct the nodelists with correct ordering. I suppose mapping could add additional confusion due to taking the integer of the node for the first row/column to 1 rather than 0, leading to an off-by-one error.

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.

All good points @LorentzFactor. Thank you!
We are indicating more than which node is in which set -- we are indicating the order of the nodes in each set.
So indeed -- we need two input parameters and the phrase nodelist in the names makes good sense too.

Copy link
Copy Markdown
Member

@dschult dschult left a comment

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! I have one whitespace suggestion that you can ignore if you prefer the test-function call split over lines.

@dschult dschult requested a review from amcandio November 20, 2025 19:57
@amcandio
Copy link
Copy Markdown
Contributor

This makes it consistent with from_numpy_array. Docs and tests look good. I approve this change.

@dschult
Copy link
Copy Markdown
Member

dschult commented Nov 21, 2025

@rossbar are you ok with using nodelists here? You suggested using dicts from node to index. But a nodelist is essentially an inverse version of that (index to node). And the nodelist approach is used heavily elsewhere when morphing from adj to array formats.

Copy link
Copy Markdown
Contributor

@rossbar rossbar left a comment

Choose a reason for hiding this comment

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

SGTM, thanks @LorentzFactor @amcandio and @dschult !

@rossbar rossbar merged commit 951e630 into networkx:main Nov 24, 2025
52 checks passed
@dschult dschult added this to the 3.6 milestone Nov 24, 2025
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.

5 participants