Skip to content

Save Layouts on Graphs - #7571

Merged
rossbar merged 23 commits into
networkx:mainfrom
mjschwenne:layouts-on-graphs
Jan 16, 2025
Merged

rossbar merged 23 commits into
networkx:mainfrom
mjschwenne:layouts-on-graphs

Conversation

@mjschwenne

Copy link
Copy Markdown
Contributor

Adds the option to save positions onto the graph. Everything still returns an dict for now for backwards compatibility. In the future we'll want the default behavior to write the position back onto the graph, but that will require a deprecation cycle. I'm particularly interested in advice on how to structure the API during the cycle and still need to add the warnings.

In the meantime, I still need to add tests and look into the possibility that it's more efficient to compute the positions directly on the graph rather than use set_edge_attribute at the end.

@mjschwenne mjschwenne added Visualization Related to graph visualization or layout type: API labels Jul 22, 2024
@dschult

dschult commented Jul 24, 2024

Copy link
Copy Markdown
Member

If the layout functions create a dict/array of positions during the algorithm and then stores it on the graph it might be good to return the dict in addition to storing it on the graph. I believe layout might be useful for more than just drawing. It is certainly related to planar embeddings. :)

@mjschwenne

Copy link
Copy Markdown
Contributor Author

This is a great point and would minimize changes to the API as well.

@mjschwenne
mjschwenne marked this pull request as ready for review July 24, 2024 15:11
@mjschwenne mjschwenne mentioned this pull request Aug 9, 2024

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

Each layout algorithm function gets a new "attribute" parameter. But why does only one of the rescaling functions get it (and then not do anything with it)?

Also, the "attribute" name is very generic (perhaps on purpose). And it doesn't indicate that this is the name of the attribute, not it's value or a boolean indicating something. How about something like: pos_attr_name (too many abbreviations?)

And that brings up a naming issue: we've used 'pos' to mean position to such an extent that it doesn't even feel like an abbreviation after a while. (similar to G standing for Graph.) Do we still want to do that? If not what to do instead of spelling out position every time?

@mjschwenne

Copy link
Copy Markdown
Contributor Author

I'm not sure why only one of the rescale layouts has the attribute parameter (perhaps due to rescale_layout_dict not being exported in the __all__ dunder) but neither of them actually takes a graph object, so there really is no way to save the new layout onto the graph. It might actually make more sense to remove the new parameter from rescale_layout.

About the new parameter name, attribute is pretty vague. Maybe even something like attribute_name or result_attr_name would make more sense. I'm personally not opposed to pos_attr_name but even if you understand that this expands to "position attribute name" it doesn't contain any information about if we use this for input or output purposes. The position abbreviation to pos is already fairly common in our codebase, so I don't think we need to be worried about it being hard to understand. If we did want to get rid of it, the only other option is probably to spell out position every time. A quick grep shows that pos appears in docstrings as the name of a parameter or return value about 37 times.

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

I think this one is ready to megre after removing "attribute" from rescale_layout.
Looks good!

@mjschwenne

Copy link
Copy Markdown
Contributor Author

So, are we OK with the parameter name being attribute and so far I've left a warning block in the documentation about the default value of attribute changing in the future. Does that technically need to have a deprecation warning too since the default functionality (after this PR is merged) will be changing?

@dschult

dschult commented Aug 22, 2024

Copy link
Copy Markdown
Member

I've left a warning block in the documentation about the default value of attribute changing in the future.

It feels like more than just a change in the default attribute value -- though that is how the change is implemented. The change (if I understand it) is to automatically add "pos" values to the node attributes in the graph getting the layout. That means we are changing the parameter G from an input to an input that gets changed during the function. Previous code that depends on the layout function not changing the graph will need to explicitly set attribute=None to work the same as it used to.

I'm not sure whether that is officially a deprecation or some other kind of change. But it seems pretty important for users to notice it. Right now it is a generic Warning. It does seem like a DeprecationWarning might be more appropriate. Perhaps @rossbar or @MridulS have thoughts about how to announce that this change is coming. The test currently is:
.. Warning:: In future releases, `attribute` will default to `pos`.. It should probably be specific about which version (or indicate more long term by saying something like "Eventually, in a future release..."). And it should have another sentence with something like: "This means that layouts will be by default written to a node attribute of G".

As for names, you point out that result_attr_name indicates that it is the name for the output, but it loses the info that it is the position being stored. I guess something like store_pos_attr_name. Or how about store_pos_as?

@dschult dschult added this to the 3.4 milestone Aug 28, 2024
@mjschwenne

Copy link
Copy Markdown
Contributor Author

As per the deprecation policy, changing the default value of the function does require a deprecation cycle. I've added the needed warnings and renamed the attribute parameter to store_pos_as which I think is a nice and concise way to convey that the position is being written to an attribute of the corresponding name.

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

I approve this PR. It is ready to merge as far as I'm concerned.

@MridulS

MridulS commented Sep 7, 2024

Copy link
Copy Markdown
Member

(Just putting in a comment here to not forget about the conversation with @dschult)

I think it may make sense to bump up the deprecation release cycle to something like 3 or 4 instead of standard 2 cycles in the broader context of the new draw API. It should give us extra time to flush out the new API changes.

@dschult

dschult commented Sep 7, 2024

Copy link
Copy Markdown
Member

One way to make the release cycles 3 or 4 instead of 2 would be to merge this PR with the deprecation warnings removed. Then 3.4 has both functions without any deprecation warnings. We advertise the changes in the release notes, etc. Then once 3.4 (or 3.5) is released, we add the deprecation warnings for the old functions.

This would give us some time with early adopters before we make the "scary" deprecation warnings nudge less early adopters. And then we finally remove the functions. Thoughts?

@mjschwenne

Copy link
Copy Markdown
Contributor Author

As far as I'm concerned either option is valid. On one hand, having all of the changes land at once is appealing but they are a large set of changes so being cautious about them is probably also wise. I think that having a longer deprecation cycle would be less cognitive burden on the developers at least, but it would mean that the users have to spend more time with the annoying warnings popping up. Of the two options, I'd probably agree with Dan to minimize the impact on the users, but I wasn't at the conversation you're referencing.

If we decide to wait a cycle or two before adding the deprecation warning, I strongly think that we should add warnings in 3.4 when the new draw API takes effect.

@dschult

dschult commented Sep 11, 2024

Copy link
Copy Markdown
Member

If we decide to wait a cycle or two before adding the deprecation warning, I strongly think that we should add warnings in 3.4 when the new draw API takes effect.

@mjschwenne can you repeat or reword that last phrase? We can't wait a cycle or two for warnings and also include the warnings in 3.4. Are you saying that we should have some sort of warning even if it isn't a deprecation warning? Or is the 3.4 number a typo? I want to make sure I understand what you are thinking.

@mjschwenne

Copy link
Copy Markdown
Contributor Author

My bad, I got the release numbers mixed up in my head. If we wait before adding the deprecation warnings, we should add them when the rest of the next drawing API becomes the only drawing API in 3.6.

@jarrodmillman jarrodmillman modified the milestones: 3.4, 3.5 Oct 10, 2024
@mjschwenne

Copy link
Copy Markdown
Contributor Author

I've removed plans to write layouts on graphs by default and added examples to the doc strings showing how to do that.

@mjschwenne

Copy link
Copy Markdown
Contributor Author

Bumping this PR. It should just need another round of reviews.

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

I think this is a fairly clean PR that might seem long because it is a repeated application of the same changes for many functions. It really shouldn't take very long to work through once the pattern is clear in the first layout function.

I read through it again and have two small comments/suggestions. Feel free to leave them as is if that is better. I just had to re-read those words to understand them, that's all.

I have already approved this PR and that still stands.

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

Copy link
Copy Markdown
Contributor Author

Still looking for another review on this PR, maybe from @rossbar or @MridulS?

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

Thanks @mjschwenne - sorry for the slow burn on this. I always struggle with proposals to expand API, but adding this hook for convenience makes a lot of sense in the context of a visualization system where all visualization information can be extracted from the graphs themselves!

I took the liberty of removing a warnings filter that I believe was left over from a previous iteration + a minor rst tweak. I'd also like to see if the examples can't be prettified, but that's not a blocker. Thanks again so much for your patience!

@rossbar

rossbar commented Jan 16, 2025

Copy link
Copy Markdown
Contributor

The CI failures are unrelated, so I'll go ahead and get this in. Thanks again @mjschwenne !

@rossbar
rossbar merged commit 3ee0390 into networkx:main Jan 16, 2025
eriknw added a commit to eriknw/networkx that referenced this pull request Jan 16, 2025
to handle new `store_pos_as=` parameter introduced in networkx#7571.
rossbar pushed a commit that referenced this pull request Jan 19, 2025
to handle new `store_pos_as=` parameter introduced in #7571.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs review type: API Visualization Related to graph visualization or layout

Development

Successfully merging this pull request may close these issues.

5 participants