Skip to content

Add feature to make storing node contraction data optional - #7902

Merged
dschult merged 19 commits into
networkx:mainfrom
rossbar:contracted-nodes-data-optional
Apr 26, 2025
Merged

dschult merged 19 commits into
networkx:mainfrom
rossbar:contracted-nodes-data-optional

Conversation

@rossbar

@rossbar rossbar commented Mar 8, 2025

Copy link
Copy Markdown
Contributor

Implements the feature discussed in #7781 - adds a store_contraction kwarg to contracted_nodes to allow the user to specify whether to store info about the contraction as node/edge attributes on the resulting graph.

The feature was relatively straightforward to implement - unfortunately the diff is kind of intimidating due to all the test suite changes. Despite the removals in the test suite, all changes are about beefing up the existing tests; particularly parametrizing over the boolean flags that already exist (self_loops and copy). However, I know how much I personally dislike changes to the test suite when reviewing, so I'm happy to split this up into two PRs where one covers the changes to the existing tests (without the store_contraction feature), and a second which simply adds the coverage related to store_contraction.

Finally, I there's likely an API discussion to be had here. I went with a boolean flag to indicate whether to store data, but hard-coding attribute names is not very networkx-y. We could also take this opportunity to do something similar to the layouts, i.e. add store_contraction_as="contraction". Happy to do so here or in follow-up PRs - LMK what you think! Either way, the addition will be backward-compatible!

@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! Looks good!

Yes -- let's make the kwarg provide an attribute name to store the contraction data. If it is None or False then don't store any data. (Or if there is a better way that's fine too.) The fixed attribute name would be good to make flexible.

I like the refactor of the tests. It's easier to read IMO.

@eriknw

eriknw commented Mar 14, 2025

Copy link
Copy Markdown
Contributor

I fixed the tests by updating some of the dispatch testing code. There is a bit of technical debt here, so there ought to be a better way to address issues like this if I'm not around.

@rossbar

rossbar commented Mar 15, 2025

Copy link
Copy Markdown
Contributor Author

Okay, two major changes since the last round of review:

  1. I re-organized the API to make the attribute name configurable (instead of hard-coded to "contraction"). This change is mostly encapsulated in 0e95262 for easy diffing!
  2. I added the same feature to contracted_edge, which is a thin wrapper around contracted_nodes. These changes are encapsulated in cdb2f77.

@eriknw eriknw 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.

LGTM, thanks @rossbar!

I updated this to the latest main branch, which did indeed unblock this PR--dispatch CI job passes 🎉

I left a minor suggestion to consider.

@amcandio amcandio 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.

Looks good to me

@dschult
dschult merged commit b3f38c3 into networkx:main Apr 26, 2025
@jarrodmillman jarrodmillman added this to the 3.5 milestone May 29, 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