Skip to content

Fix node attributes on lattice graphs - #8311

Merged
MridulS merged 6 commits into
networkx:mainfrom
rossbar:fix-hexagonal-lattice-no-pos
Oct 6, 2025
Merged

MridulS merged 6 commits into
networkx:mainfrom
rossbar:fix-hexagonal-lattice-no-pos

Conversation

@rossbar

@rossbar rossbar commented Oct 4, 2025

Copy link
Copy Markdown
Contributor

There are two minor fixes in this PR, a result of me taking a closer look at some of the lattice graphs for a maze-making application I'm thinking about.

  1. hexagonal_lattice_graph has a with_positions boolean kwarg. It is True by default, so the positions of the nodes are computed and stored on a node attr named "pos". It turns out that with_positions is being ignored in hexagonal_lattice_graph - it's a very easy fix though, just move the existing position calculation under an if with_positions condition (this was already in place for triangular_lattice_graph

  2. The second is more subtle - both hexagonal_lattice_graph and triangular_lattice_graph use contracted_nodes under-the-hood to identify boundary nodes when periodic=True. By default, contracted_nodes stores information about the contraction on the nodes/edges in the resultant graph. In this particular case, the contractions are an implementation detail - the information about the contraction itself shouldn't be stored on the resulting lattice graph. This PR adds tests for this and uses the new store_contraction_as kwarg (see Add feature to make storing node contraction data optional #7902) to suppress this info on the final returned lattice graph.

Finally, one stylistic change: I removed the explicit import of contracted_nodes from algorithms.minors within the functions and simply call it from the top-level nx namespace instead!

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

This looks like a nice clean-up.
Thanks @rossbar

@MridulS MridulS added this to the 3.6 milestone Oct 6, 2025
@MridulS
MridulS merged commit 36d7ba3 into networkx:main Oct 6, 2025
@rossbar
rossbar deleted the fix-hexagonal-lattice-no-pos branch October 6, 2025 21:25
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.

3 participants