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

Fix: Property layer visualization for HexGrid #2646

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Sahil-Chhoker
Copy link
Contributor

Summary

Property layers were not properly visualized for HexGrids, and this PR aims to resolve that issue. It utilizes the same coordinate system as the draw_hex_grid function and generates a PolyCollection that overlays color values on the grid.

Bug / Issue

fixes #2433

Before

Screenshot 2025-01-26 203400

After

Screenshot 2025-01-26 203521

Additional Notes:

I am not entirely sure if this was the best approach to address the issue. If there is a more effective or elegant solution, I would greatly appreciate your recommendations.

Copy link

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.3% [-3.4%, -1.2%] 🔵 -1.8% [-2.3%, -1.3%]
BoltzmannWealth large 🔵 -0.0% [-0.5%, +0.5%] 🔵 -0.3% [-0.8%, +0.4%]
Schelling small 🔵 -0.6% [-1.0%, -0.2%] 🔵 -0.3% [-0.4%, -0.2%]
Schelling large 🔵 -0.0% [-0.6%, +0.4%] 🔵 +2.9% [+1.6%, +4.1%]
WolfSheep small 🔵 +0.0% [-0.4%, +0.5%] 🔵 +0.6% [+0.5%, +0.8%]
WolfSheep large 🔵 +1.0% [+0.5%, +1.5%] 🔵 +2.3% [+0.5%, +4.4%]
BoidFlockers small 🔵 -1.1% [-1.9%, -0.3%] 🔵 -0.1% [-0.4%, +0.1%]
BoidFlockers large 🔵 +0.7% [+0.2%, +1.2%] 🔵 -0.1% [-0.5%, +0.2%]

@quaquel
Copy link
Member

quaquel commented Jan 26, 2025

Thanks for this. The example looks great. I am taking a look at the code now.

Copy link
Member

@quaquel quaquel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few questions and ideas. Looks very good already.

@Sahil-Chhoker
Copy link
Contributor Author

Are you sure it's fine as it is? I had a few questions about it:

  1. Regarding the repetitive part, I think I’ve got an answer for now. I’ll get back to you as early as tomorrow.
  2. I'm hardcoding the value of zorder to -1. Is this the correct approach?

@quaquel
Copy link
Member

quaquel commented Jan 26, 2025

I gave it a first quick pass and focussed on the high-level stuff. The zorder one is a good question (and actually interacts with #2642). Ideally (but beyond this pr), zorder should become controllable for property layers to explicitly control layering.

@Sahil-Chhoker
Copy link
Contributor Author

Thank you for the review! I have added the suggested changes. Please let me know if you have any additional recommendations to further improve the code quality.

Copy link
Member

@quaquel quaquel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, just a few remaining clarifications


@lru_cache(maxsize=1024, typed=True)
def _get_hexmesh(
width: int, height: int, size: float
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make size a keyword argument with a default value of 1?

@@ -159,6 +160,37 @@ def draw_space(
return ax


# Helper function for getting the vertices of a hexagon given the center and size
def _get_hex_vertices(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps move this to an inner function in _get_hexmesh?

# Edge logic, connecting each vertex to the next
for v1, v2 in pairwise([*vertices, vertices[0]]):
# Sort vertices to ensure consistent edge representation and avoid duplicates.
# Sort vertices to ensure consistent edge representation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to sort them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Grid and property layer for hexgrid
2 participants