Skip to content

fix: handle skipped property layers in matplotlib backend - #4050

Merged
quaquel merged 3 commits into
mesa:mainfrom
Jenil1905:fix/matplotlib-skipped-property-layers
Nov 12, 2026
Merged

quaquel merged 3 commits into
mesa:mainfrom
Jenil1905:fix/matplotlib-skipped-property-layers

Conversation

@Jenil1905

@Jenil1905 Jenil1905 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

Fix skipped property-layer rendering paths in the Matplotlib backend that could cause an UnboundLocalError or incorrectly discard a previously created colorbar.

Bug / Issue

Fixes #4049

MatplotlibBackend.draw_property_layer() could raise an UnboundLocalError when all property layers were skipped because the colorbar variable was not initialized before the loop.

Additionally, when multiple property layers were rendered, a later layer without a colorbar could reset the returned colorbar to None, even if an earlier layer had created one.

Expected Behavior

  • Skipped property layers should not cause an exception.
  • When no layer is drawn, the method should return (ax, None).
  • When an earlier layer creates a colorbar and a subsequent layer does not, the previously created colorbar should be preserved.

Implementation

  • Initialize cbar = None before iterating over property layers.
  • Avoid resetting the colorbar for every layer.
  • Clarify the return contract in the method docstring.
  • Add regression tests covering:
    • An empty portrayal dictionary.
    • A callable portrayal returning None.
    • A property layer skipped due to dimension mismatch.
    • Two rendered layers where the first creates a colorbar and the second does not.

Before / After Evidence

Case 1: All property layers skipped

Before:

UnboundLocalError: cannot access local variable 'cbar'
where it is not associated with a value

After:

Returned: (Axes, None)
mesa-pr4050-skipped-before-after

Case 2: Two property layers

  • Layer A requests a colorbar.
  • Layer B does not request a colorbar.

Before:

Both layers drawn.
Layer A colorbar created.
Returned colorbar: None

After:

Both layers drawn.
Layer A colorbar created.
Returned colorbar: Layer A's colorbar
mesa-pr4050-mixed-colorbar-before-after

Testing

  • Focused property-layer regression tests: 5 passed.
  • tests/visualization/test_backends.py: 26 passed.
  • tests/visualization/test_space_renderer.py: 11 passed.
  • tests/visualization/test_components_matplotlib.py: 17 passed.
  • Ruff lint: passed.
  • Ruff formatting check: passed.
  • git diff --check: passed.

The new mixed-colorbar regression test was also verified against the previous implementation, where the returned colorbar assertion failed as expected.

Additional Notes

Codex was used to assist with code investigation, implementation, and regression testing. The changes and test results were reviewed before submission.

Initialize the colorbar result before iterating so skipped layers return the existing axes/colorbar tuple with a None colorbar instead of raising UnboundLocalError.

Add regression coverage for an empty portrayal dictionary and a callable that returns None.
@github-actions github-actions Bot added testing Release notes label visualization labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.6% [+0.4%, +2.9%] 🔴 +3.3% [+3.2%, +3.5%]
BoltzmannWealth large 🔵 +3.2% [+1.0%, +5.7%] 🔵 +1.5% [-4.7%, +8.2%]
Schelling small 🔴 +4.5% [+3.3%, +5.8%] 🔵 +1.7% [+0.9%, +2.5%]
Schelling large 🔵 +3.6% [+2.9%, +4.3%] 🔴 +7.6% [+4.2%, +10.9%]
WolfSheep small 🔴 +10.6% [+10.1%, +11.2%] 🔵 +6.6% [+1.8%, +11.6%]
WolfSheep large 🔴 +9.0% [+8.2%, +9.8%] 🔴 +22.6% [+20.7%, +24.5%]
SugarscapeG1mt small 🔴 +14.4% [+13.6%, +15.2%] 🔴 +5.4% [+3.7%, +7.1%]
SugarscapeG1mt large 🔴 +13.8% [+11.8%, +15.7%] 🔵 +3.3% [+1.9%, +5.0%]
BoidFlockers small 🔵 +2.3% [+1.4%, +3.1%] 🔵 +0.5% [+0.1%, +0.9%]
BoidFlockers large 🔵 +3.2% [+2.6%, +3.8%] 🔵 +0.2% [-0.1%, +0.6%]
MultiLevelAllianceModel small 🔴 +7.6% [+4.9%, +10.3%] 🔵 +1.3% [+0.6%, +2.0%]
MultiLevelAllianceModel large 🟢 -5.0% [-6.1%, -3.9%] 🟢 -10.4% [-13.0%, -7.5%]

@arzaanxeng

Copy link
Copy Markdown
Contributor

Thanks, the fix looks good: draw_property_layer no longer raises UnboundLocalError when every layer is skipped, and the new tests pass on my end.

A few small things:

  1. The cbar = None inside the loop (before "Add colorbar if requested") still resets the value for each layer. With two layers where A has a colorbar and B doesn't, a colorbar is drawn but the function returns None. Is the returned cbar meant to be the last one created? If so, removing that line would make it consistent. This was already the case before this PR, so I'm fine with it being a separate issue.

  2. The docstring still says it returns (Axes, colorbar). It could mention that the colorbar is None when no layer is drawn.

  3. The dimension-mismatch skip (the UserWarning path) also reaches this code, but isn't covered by a test. Might be worth adding one.

@Jenil1905

Copy link
Copy Markdown
Contributor Author

Thanks @arzaanxeng for the review! I've addressed all three points:

  • Removed the redundant per-layer colorbar reset.
  • Clarified the return value in the docstring, including when the colorbar is None.
  • Added regression coverage for the dimension-mismatch warning path.

The focused tests and full SpaceRenderer test module pass, and Ruff checks pass as well. Thanks again!

@Jenil1905

Copy link
Copy Markdown
Contributor Author

Hi @quaquel, could you please take a look at this PR when you get a chance?

I've addressed the feedback from arzaanxeng, pushed the requested changes, and verified the updated implementation with the relevant tests and lint checks.

I'd appreciate a maintainer review. Please let me know if anything else needs to be changed. Thanks!

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

Checked 7cde644c: the three skip-path regressions pass, and the visualization suite passes (130 tests). Eight extra cases also pass, including keeping A's colorbar when B is drawn without one. The skip-path tests and that mixed-colorbar case fail on the pre-fix code.

The earlier feedback is addressed. Could you add the two-layer A-with-colorbar/B-without-colorbar case to the PR's tests, asserting that the returned colorbar is A's? It would protect the behavior changed by removing the per-layer reset.

Also, please use the bug PR template and fill in its sections, including before/after evidence for the visualization fix. The current description explains the fix, but the template and visual evidence are missing.

@Jenil1905

Copy link
Copy Markdown
Contributor Author

Hi @atul-upadhyay-7, I've implemented your suggestions and pushed the updates to this PR.

I've added the two-layer regression test verifying that layer A's colorbar is preserved when layer B is drawn without one. I've also updated the PR description using Mesa's bug-fix template, including the before/after reproduction evidence.

Thanks for testing the additional cases and pointing these out!

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

@Jenil1905 the before/after screenshots are still missing from the description. could you add them? the text evidence is already there.

@Jenil1905

Copy link
Copy Markdown
Contributor Author

Hi @atul-upadhyay-7 I've added both before/after screenshots to the PR description under the respective reproduction cases. The screenshots also illustrate the returned colorbar behavior. Thanks for pointing this out!

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

checked the updated version, the fix looks good to me. lgtm

@quaquel
quaquel merged commit aa4ca48 into mesa:main Nov 12, 2026
16 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
Adapted for 3.5.x: tests use 3.5's setup_propertylayer/draw_propertylayer names and the _mesa_property_layers API; the fix itself applied unchanged.

(cherry picked from commit aa4ca48)

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

testing Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Matplotlib property-layer rendering crashes when all layers are skipped

4 participants