Skip to content

Keep SpaceRenderer setup kwargs when SolaraViz resets - #3951

Merged
quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:fix/copy-renderer-draw-kwargs
Nov 7, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:fix/copy-renderer-draw-kwargs

Conversation

@AshNicolus

@AshNicolus AshNicolus commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

When you press Reset in SolaraViz, the space loses its styling. Anything you passed to setup_structure() or setup_agents() (like grid_color or cmap) is gone after the reset. This PR makes Reset keep those settings.

Bug / Issue

Fixes #3948

On Reset, SolaraViz builds a fresh renderer with copy_renderer(). That function copies a list of attributes from the old renderer, but two names on that list are outdated:

flowchart LR
    A[Reset clicked] --> B[copy_renderer]
    B --> C{"copy 'space_kwargs'<br/>and 'agent_kwargs'"}
    C -->|these names don't exist anymore| D[nothing copied]
    D --> E[space redrawn with defaults]
Loading

SpaceRenderer stores these as draw_space_kwargs and draw_agent_kwargs. Since copy_renderer uses getattr(renderer, attr, None), the old names just returned None and were skipped without any error.

Implementation

  • Use the correct names, draw_space_kwargs and draw_agent_kwargs, in copy_renderer.
  • Copy the dicts instead of passing the same object along, so the old and new renderer don't share them.

What you get after a reset:

renderer.setup_structure(grid_color="red").setup_agents(portrayal, cmap="viridis")
new_renderer = copy_renderer(renderer, model)

# before: {} {}
# after:  {'grid_color': 'red'} {'cmap': 'viridis'}
print(new_renderer.draw_space_kwargs, new_renderer.draw_agent_kwargs)

Testing

  • Added test_copy_renderer_keeps_setup_kwargs in tests/visualization/test_solara_viz.py. It fails on main (the copied kwargs come back as {}) and passes with this change.
  • Full test suite passes locally with pytest tests/ -Werror (876 passed).
  • pre-commit (ruff, pyupgrade, codespell) passes on the changed files.

Additional Notes

copy_renderer had no tests before, so this also gives it some basic coverage.

@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.2% [-0.2%, +0.6%] 🔵 -0.7% [-1.0%, -0.5%]
BoltzmannWealth large 🔵 +1.0% [+0.7%, +1.4%] 🔵 +3.6% [+2.5%, +4.6%]
Schelling small 🔵 -0.5% [-0.9%, -0.2%] 🔵 -0.3% [-1.0%, +0.5%]
Schelling large 🔵 -0.1% [-0.5%, +0.3%] 🔵 +0.3% [-1.9%, +2.6%]
WolfSheep small 🔵 -1.6% [-2.0%, -1.2%] 🔵 -4.1% [-8.3%, +0.9%]
WolfSheep large 🔵 -2.1% [-2.7%, -1.4%] 🔵 -2.4% [-3.2%, -1.5%]
SugarscapeG1mt small 🔵 -0.2% [-0.4%, +0.0%] 🔵 +0.1% [-1.2%, +1.5%]
SugarscapeG1mt large 🔵 +1.6% [+0.8%, +2.6%] 🔵 +2.0% [-0.1%, +4.2%]
BoidFlockers small 🔵 -0.9% [-1.1%, -0.6%] 🔵 -0.3% [-0.7%, +0.0%]
BoidFlockers large 🔵 -0.7% [-1.1%, -0.4%] 🔵 -0.1% [-0.3%, +0.2%]
MultiLevelAllianceModel small 🔵 +1.2% [+0.3%, +2.2%] 🔵 +1.4% [+1.2%, +1.6%]
MultiLevelAllianceModel large 🔵 -0.4% [-0.8%, -0.0%] 🔵 +0.8% [+0.5%, +1.1%]

copy_renderer still copied space_kwargs and agent_kwargs, but
SpaceRenderer stores them as draw_space_kwargs and draw_agent_kwargs.
The old names don't exist anymore, so pressing Reset silently dropped
everything passed to setup_structure() and setup_agents().

Copy the right attributes, and copy the dicts so the old and new
renderer don't share them. Add a regression test.

Fixes mesa#3948
@AshNicolus
AshNicolus force-pushed the fix/copy-renderer-draw-kwargs branch from 1a5032f to 83c1295 Compare November 6, 2026 10:12
@quaquel quaquel added the bug Release notes label label Nov 7, 2026
@quaquel
quaquel merged commit b687223 into mesa:main Nov 7, 2026
17 checks passed
@quaquel quaquel added the backport-candidate PRs we might want to backport to an earlier branch label Nov 7, 2026
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
copy_renderer still copied space_kwargs and agent_kwargs, but
SpaceRenderer stores them as draw_space_kwargs and draw_agent_kwargs.
The old names don't exist anymore, so pressing Reset silently dropped
everything passed to setup_structure() and setup_agents().

Copy the right attributes, and copy the dicts so the old and new
renderer don't share them. Add a regression test.

Fixes #3948

(cherry picked from commit b687223)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-candidate PRs we might want to backport to an earlier branch bug Release notes label testing Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SolaraViz Reset drops SpaceRenderer setup kwargs (copy_renderer copies stale attribute names)

2 participants