Skip to content

Fix SpaceRenderer.canvas crash for Altair with property layers - #3953

Merged
EwoutH merged 5 commits into
mesa:mainfrom
AshNicolus:fix/altair-canvas-property-layer
Nov 16, 2026
Merged

EwoutH merged 5 commits into
mesa:mainfrom
AshNicolus:fix/altair-canvas-property-layer

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

SpaceRenderer.canvas crashes with KeyError: 0 when you use the Altair backend and have a property layer set up. This PR makes it return the combined chart like it should.

Bug / Issue

Fixes #3950

canvas expects the property layer drawing to give back two things, a chart and a colorbar. The matplotlib backend does that, but the Altair backend has always returned a single chart, with the legend already inside it. So canvas tries to unpack one chart into two values and fails.

flowchart LR
    A[renderer.canvas] --> B[draw_property_layer]
    B --> C[Altair returns one chart]
    C --> D{"canvas does<br/>chart, colorbar = result"}
    D --> E["KeyError: 0"]
Loading

SolaraViz isn't affected because it builds the Altair chart on its own and never calls canvas. This mostly hits people working in a notebook who call renderer.canvas directly.

Implementation

  • In canvas, treat the Altair property layer result as one chart and layer it with the structure and agents.
  • Remove the colorbar branches for Altair. They could never run, since the backend never returns a separate colorbar.
  • Fix the AltairBackend.draw_property_layer docstring, which said it returns a tuple. That's likely how the mismatch happened.
renderer = (
    SpaceRenderer(model, backend="altair")
    .setup_agents(agent_portrayal)
    .setup_property_layer(lambda _: PropertyLayerStyle(colormap="viridis"))
)
renderer.render()
renderer.canvas

# before: KeyError: 0
# after:  alt.LayerChart with the grid, the property layer and the agents

Testing

  • Added test_altair_canvas_with_property_layer in tests/visualization/test_space_renderer.py. It fails on main with KeyError: 0 and passes with this change. It also checks the chart turns into a valid Vega-Lite spec.
  • Checked by hand that the canvas includes the property layer, both with and without agents.
  • Full suite passes locally with pytest tests/ -Werror (876 passed), and pre-commit passes.

Additional Notes

The Altair canvas had no tests before, so this also adds the first one.

canvas unpacked the property layer result into a chart and a colorbar,
but the Altair backend has always returned a single chart with the
legend built in. Unpacking it raised KeyError: 0, so canvas never worked
for Altair once a property layer was set up.

Treat the result as one chart and drop the colorbar branches, which
could never be reached. Also fix the backend docstring that said it
returns a tuple, and add a test for the Altair canvas.

Fixes mesa#3950
@github-actions github-actions Bot added testing Release notes label visualization labels Oct 19, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.9% [-2.2%, +0.4%] 🔵 +0.6% [+0.1%, +1.0%]
BoltzmannWealth large 🔵 -2.3% [-3.6%, -1.2%] 🔵 -6.7% [-10.8%, -1.7%]
Schelling small 🔵 -0.9% [-2.1%, +0.1%] 🔵 -2.7% [-3.6%, -2.0%]
Schelling large 🔵 -2.3% [-4.0%, -0.5%] 🔵 -0.5% [-5.5%, +4.8%]
WolfSheep small 🔵 -2.8% [-4.1%, -1.6%] 🔵 +1.0% [-2.3%, +4.5%]
WolfSheep large 🔵 -3.8% [-7.2%, -0.3%] 🔵 -4.4% [-10.7%, +1.4%]
SugarscapeG1mt small 🔵 -3.6% [-5.5%, -1.6%] 🔵 -0.2% [-2.2%, +1.7%]
SugarscapeG1mt large 🟢 -6.7% [-8.6%, -4.8%] 🔵 -3.4% [-5.7%, -0.7%]
BoidFlockers small 🟢 -3.9% [-4.7%, -3.0%] 🔵 -0.1% [-0.7%, +0.4%]
BoidFlockers large 🔵 -3.4% [-4.7%, -2.2%] 🔵 +0.0% [-0.6%, +0.7%]
MultiLevelAllianceModel small 🟢 -8.1% [-9.7%, -6.7%] 🔴 +9.0% [+8.3%, +9.7%]
MultiLevelAllianceModel large 🟢 -5.3% [-6.5%, -4.2%] 🔴 +8.4% [+6.7%, +9.8%]

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

Checked fc0eb688: the linked crash is fixed. The full PR test suite passes with -Werror (876 tests), and 24 extra canvas cases pass, including skipped layers, multiple layers, legends, repeated reads and data updates. The property-layer regression fails on the pre-fix code.

The description follows the bug template. Please add the before/after visual evidence requested in its Testing section: the original KeyError: 0 and the rendered chart with the grid, property layer and agent.

One optional test improvement: parameterize test_altair_canvas_with_property_layer with and without agents. For the agent case, place a CellAgent at (1, 1), call setup_agents, and assert that the final chart has three layers with the agent point layer last. This would automate the agent overlay check already mentioned in the description.

CI still has one failing check: the examples job stops during collection because the warehouse example imports mesa.meta_agents. The same import fails on the unchanged base, so this is separate from the canvas fix. A clean examples rerun is still needed before calling the PR fully green; no unrelated warehouse change belongs in this fix.

No blocking issue found in the changed code.

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

@AshNicolus the fix and the new agent overlay test look good. on python 3.13, all 1116 tests pass locally. both new test cases fail on the unchanged base with the original KeyError.

the CI Playwright screenshot failure didn't reproduce in five runs each on base and head, but that check still needs a clean rerun. please add the before/after images when you get a chance.

no blocking issue found in the changed code. lgtm

@quaquel quaquel added the bug Release notes label label Nov 12, 2026
@quaquel

quaquel commented Nov 12, 2026

Copy link
Copy Markdown
Member

There is a merge conflict that needs to be resolved. Also one of the builds seems to have failed. Can you please check?

# Conflicts:
#	tests/visualization/test_space_renderer.py
@AshNicolus

Copy link
Copy Markdown
Contributor Author

Merged main in. The conflict was only in test_space_renderer.py, where #4050 and this PR both added tests at the end, so I kept both. Full suite passes locally (1120).

The failed build was test_pd_grid_model in the Playwright tests (locator("img") matched 2 elements), not anything in this change. The same test flaked on another of my PRs this week and passes locally, so it should be fine on the new run.

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

Thanks, reproduced the KeyError: 0 on main and confirmed a valid 3-layer chart here.

@EwoutH

EwoutH commented Nov 16, 2026

Copy link
Copy Markdown
Member

Merging, thanks!

@EwoutH
EwoutH merged commit e43e3c9 into mesa:main Nov 16, 2026
16 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
Adapted for 3.5.x: uses 3.5's propertylayer_mesh / draw_propertylayer / setup_propertylayer names.

(cherry picked from commit e43e3c9)

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpaceRenderer.canvas raises KeyError with Altair backend when a property layer is set

4 participants