Repository navigation
fix(viz): align HexGrid property layer values with hexagon mesh - #4046
Conversation
|
Performance benchmarks:
|
AshNicolus
left a comment
There was a problem hiding this comment.
Checked this against main and with my #4043 and #3952 stacked on top. On a 5x3 HexGrid with three different values and an agent on each of those cells, the coloured hexagons and the agents line up in both the legacy draw_space path and SpaceRenderer; without this PR they don't, in either path. I also confirmed the reasoning in grid.py: cells come from product(range(width), range(height)), so all_cells is x-major, while _get_hexmesh is row-major. Both new tests fail on main and pass here, the full suite passes with all three PRs stacked, and #4043 merges cleanly with this one.
flowchart LR
subgraph New["SpaceRenderer (matplotlib backend)"]
N1["hexagons from space.all_cells<br/>x-major: (0,0),(0,1),(1,0)..."] --> N3{"same order?"}
N2["data shape (width, height)"] -->|"main: data.T.ravel() → row-major"| N3
N2 -->|"PR: data.ravel() → x-major"| N3
end
subgraph Legacy["legacy draw_property_layers"]
L1["_get_hexmesh<br/>row-major: (0,0),(1,0),(2,0)..."] --> L3{"same order?"}
L2["data shape (width, height)"] -->|"main: data.ravel() → x-major"| L3
L2 -->|"PR: data.T.ravel() → row-major"| L3
end
Two small optional suggestions inline: a one-line comment at each ravel saying which order it has to match, since that implicit ordering is what let this slip in. LGTM.
|
Thanks for the review and the helpful diagrams, I have added both the suggested comments. |
614fe4a to
6966733
Compare
|
Thanks for fixing the HexGrid property-layer alignment, @pojeesahil. The reported checks pass, but the branch now has merge conflicts with main. Please rebase onto main, resolve the conflicts, and rerun the visualization tests so this can proceed to maintainer review. Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot. |
6966733 to
6ed9dae
Compare
Done |
EwoutH
left a comment
There was a problem hiding this comment.
Thanks, this must have been a very fustrating bug for users to encounter. Difficult to debug if the visualisation is lying to you!
Assisted-by: GPT-6
Add ordering comments from review Co-authored-by: AshNicolus <[email protected]>
6ed9dae to
8de2f7f
Compare
Fixes property layer visualization on HexGrid where colors are drawn on the wrong hexagons due to mismatched array ordering. Partly backported for 3.5.x: only the legacy draw_property_layers fix. On 3.5 the SpaceRenderer hexagon mesh is still row-major, so the matplotlib backend already uses data.T.ravel() correctly and is left unchanged. The SpaceRenderer test checks the row-major order instead. (cherry picked from commit ac849e1) Assisted-by: Claude Opus 5.5
Pre-PR Checklist
Summary
Fixes property layer visualization on
HexGridwhere colors are drawn on the wrong hexagons due to mismatched array ordering.Bug / Issue
When visualizing a property layer on a
HexGridusing Matplotlib, the cell values show up on the wrong hexagons.For example, on a 4x2 hex grid, setting a value at coordinate
(3, 0)does not draw color at(3, 0). Instead, it shows up at(1, 1).Why this happens:
The list of hexagon shapes and the data array values are ordered differently:
SpaceRenderer(matplotlib_backend.py), the hexagons follow the model's cells in(x, y)order. However, the code flattened transposed data (data.T.ravel()), which swapped the axes and placed colors on the wrong cells.draw_property_layers(mpl_space_drawing.py), the reverse happened. The hexagon shapes were created row-by-row, but the data array was flattened without being transposed.Implementation
mesa/visualization/backends/matplotlib_backend.py: Changeddata.T.ravel()todata.ravel()so the flattened values match the cell order ofspace_drawer.hexagons.mesa/visualization/mpl_space_drawing.py: Changeddata.ravel()todata.T.ravel()so the flattened values match the row-major mesh generated by_get_hexmesh.test_space_renderer.pyandtest_components_matplotlib.pyverifying that a value placed at(3, 0)on a 4x2HexGridcorrectly colors hexagon(3, 0).Testing
pytest tests/visualization/(all 129 tests passed).ruff checkandruff format.