Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: drop the use of node attribute "first_nbr" in PlanarEmbedding #7202

Merged
merged 15 commits into from Jan 10, 2024

Conversation

mdealencar
Copy link
Contributor

The use of a node attribute called "first_nbr" makes it impossible to represent a valid PlanarEmbedding with a dict_of_dicts. This complicates the creation of valid and invalid embeddings for testing purposes, particularly if the changes in the PR to solve issue #6798 (which forbids the use of PlanarEmbedding.add_edge*) are merged.

The attribute "first_nbr" seems to be relevant only to LRPlanarity.lr_planarity() and the way the methods add_half_edge_first() and add_half_edge_ccw() update that attribute are quite unintuitive and undocumented. This means I doubt there is someone out there relying on this attribute

This PR introduces a new method add_half_edge(), which covers the functionality of add_half_edge_cw(), add_half_edge_ccw() and add_half_edge_first() while mimicking their behavior regarding the role of "first_nbr". These three methods are still fully functional, but could be deprecated in favor of add_half_edge() (although some form of add_half_edge_first() will be still necessary for LRPlanarity to work). The attribute "first_nbr" is no longer necessary and its functionality is obtained by relying on the keeping of insertion order of dict since Python 3.7.

Tests related to planarity.py were updated to use the new method.

Copy link
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 seems like a good performance choice -- and it simplifies the methods api so probably better for reading the code too. Thanks!

Could you test the new function to ensure that it raises when it should, e.g. cw and ccw specified and the other cases? There is a nice match option on pytest.raises so you can put in a key part of the error message to make sure the right one gets caught.

It looks like the other (existing) tests put building the PlanarEmbedding inside the context with the "check_structure". It'd be a better test if it built the PlanarEmbedding first and just had the check call inside the context. If that's too much for this PR we can put it in a separate one. But this touches that code already, so if you're up for it I'd appreciate that. Or let me know and I'll add it. :)

networkx/algorithms/planarity.py Outdated Show resolved Hide resolved
networkx/algorithms/tests/test_planarity.py Show resolved Hide resolved
networkx/algorithms/planarity.py Outdated Show resolved Hide resolved
networkx/algorithms/planarity.py Outdated Show resolved Hide resolved
networkx/algorithms/tests/test_planarity.py Outdated Show resolved Hide resolved
mdealencar and others added 3 commits January 4, 2024 22:05
Co-authored-by: Dan Schult <dschult@colgate.edu>
…test for `add_half_edge()` (planarity.py, test_planarity.py)
Copy link
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!

Copy link
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.

Generally LGTM - I'm no expert on planar embeddings but this new API is certainly more intuitive.

@mdealencar will merging this interfere with #6798, or have these changes already been incorporated there? Either way I'm happy to push forward, just trying to get a handle on which changes are repeated where!

networkx/algorithms/planarity.py Outdated Show resolved Hide resolved
networkx/algorithms/planarity.py Outdated Show resolved Hide resolved
networkx/algorithms/tests/test_planarity.py Show resolved Hide resolved
@mdealencar
Copy link
Contributor Author

mdealencar commented Jan 10, 2024

@mdealencar will merging this interfere with #6798, or have these changes already been incorporated there? Either way I'm happy to push forward, just trying to get a handle on which changes are repeated where!

I had already rebased #6798 to be applied after this one, as some changes here affect the implementation of new methods there. This PR stand by itself, though it does not resolve issue #6796, which is the motivation behind both PRs. I'm not sure if pushing only #6798 will carry the changes from here, but I think applying this first and then #6798 will leave a clearer record of the rationale behind the changes.

Copy link
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.

Thanks for the summary @mdealencar - in that case, let's get this one in and rebase #6798 on main once this has been merged.

Thanks for sticking it out through all the iterations and making review easier!

@rossbar rossbar merged commit 0204a24 into networkx:main Jan 10, 2024
39 checks passed
@jarrodmillman jarrodmillman added this to the 3.3 milestone Jan 10, 2024
@mdealencar mdealencar deleted the del_first_nbr branch January 10, 2024 23:18
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
Add a new method for adding edges to PlanarEmbedding as an
alternative to the existing three methods add..._cw, add..._ccw, and add..._first_edge.

This tightens the API and allows for dropping explicit tracking of the `first_nbr`
internal state variable.

* feat: drop the use of node attribute "first_nbr" in PlanarEmbedding (planarity.py)

* feat: add method PlanarEmbedding.add_half_edge(), which superceeds the _cw and _ccw varieties, plus update the planar_drawing and tests accordingly (planar_drawing.py, planarity.py, test_planarity.py)

---------

Co-authored-by: mdealencar <mdealencar@users.noreply.github.com>
Co-authored-by: Dan Schult <dschult@colgate.edu>
Co-authored-by: Ross Barnowski <rossbar@caltech.edu>
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.

None yet

4 participants