Skip to content

Set length threshold in FR and use np.clip - #8145

Merged
dschult merged 4 commits into
networkx:mainfrom
rossbar:gh-8113
Sep 8, 2025
Merged

dschult merged 4 commits into
networkx:mainfrom
rossbar:gh-8113

Conversation

@rossbar

@rossbar rossbar commented Jul 11, 2025

Copy link
Copy Markdown
Contributor

Closes #8113

I have been convinced by @HirokiHamaguchi 's and @rudyarthur 's careful analysis of the thresholding in the FR layout that it's worth considering setting the length fill value to be equal to the threshold. While the change itself in source code is quite small, bear in mind this will break backward compatibility; i.e. spring_layout will no longer produce identical layouts to previous versions of NetworkX even when seeded identically. This is evidenced by the required change in the doctest.

IMO given how little the layouts will change (especially for the default number of iterations) and the minor improvements for some cases (see @HirokiHamaguchi 's analysis in #8113), I'm willing to accept this breakage. That's just my opinion though - if others feel different please share your perspective!

Switching to a fill value that matches the threshold also enables another minor improvement - we can use np.clip instead of np.where or any other mask-based approach, which is both faster and arguably more readable. I've included that change as well (and in another unrelated location where np.where could be replaced with clip).

Finally, I added a code comment explaining the clipping and referencing back to #8113. It may be a bit unorthodox to link to a GH discussion in source code, but the discussion there is so rich I feel it's worth pointing to.

@rossbar rossbar added type: Enhancements Visualization Related to graph visualization or layout labels Jul 11, 2025

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

My 2c on whether breaking backward-compatibility is an issue basically comes down to whether you consider this a bug to be fixed. I'm personally happy to take it as a bug fix, even though the current behavior is intentional!

Comment thread networkx/drawing/layout.py Resolved
Comment thread networkx/drawing/layout.py Resolved
Comment thread networkx/drawing/layout.py Resolved

@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 all for the helpful and interesting discussion around this seemingly minor issue. I agree with this solution -- and I don't think we need a deprecation cycle.

Of course, this PR has 2 failed tests because the images don't match exactly. Should this PR include updating the two images so the tests pass? How do we handle that?

@rossbar

rossbar commented Jul 16, 2025

Copy link
Copy Markdown
Contributor Author

Of course, this PR has 2 failed tests because the images don't match exactly. Should this PR include updating the two images so the tests pass?

It must!

How do we handle that?

I wasn't sure off the top of my head, but fortunately the contributor guide documents the procedure!

Going through this exercise also reminded another reason why we have the --mpl tests configured how we do in CI: we only run the image comparison tests on circleci because circleci is the service that allows you to introspect artifacts directly without having to download them. So if an image comparison fails, you can go to the artifacts tab and see the failure summary:

https://output.circle-artifacts.com/output/job/07d3559b-8b0e-4759-afeb-4dd494fd2652/artifacts/0/results/fig_comparison.html

It's been so long (ever?) since these tests have failed that I forgot all about this nice feature :)

@dschult
dschult merged commit c19a0e5 into networkx:main Sep 8, 2025
@dschult dschult added this to the 3.6 milestone Sep 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: Enhancements Visualization Related to graph visualization or layout

Development

Successfully merging this pull request may close these issues.

Unusual clipping in spring_layout

4 participants