Allow PropertyLayerStyle instance in draw_propertylayer#2936
Merged
Sahil-Chhoker merged 12 commits intomesa:mainfrom Dec 18, 2025
Merged
Allow PropertyLayerStyle instance in draw_propertylayer#2936Sahil-Chhoker merged 12 commits intomesa:mainfrom
Sahil-Chhoker merged 12 commits intomesa:mainfrom
Conversation
|
Performance benchmarks:
|
Contributor
Author
|
@EwoutH Please review my PR. |
Collaborator
|
@ShreyasN707 Thanks for this PR! |
Member
|
Yes, very fair, let’s get #2893 in first. |
Contributor
Author
|
@Sahil-Chhoker Sounds good to me! Let’s get #2893 merged first. Once that’s in, I’ll rebase this PR and fix up anything that needs adjusting. |
Collaborator
|
@ShreyasN707 sorry for the delay, #2893 is merged now so can you update this PR accordingly. |
Contributor
Author
@Sahil-Chhoker sure! Will do it by EOD. |
Contributor
Author
|
@Sahil-Chhoker @EwoutH Please review the changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR improves the PropertyLayer visualization API by allowing users to pass a PropertyLayerStyle instance directly to draw_propertylayer. This removes the need to wrap the style in a function for simple, uniform styling cases.
issue #2923
Background / Problem
Previously, even the simplest styling required a callable, like:
def propertylayer_portrayal(layer):
return PropertyLayerStyle(colormap="viridis", colorbar=True)
For cases where the style is the same across all layers, this wrapper didn't add any value and made the API feel unnecessarily verbose.
This PR addresses feedback suggesting the API should accept both:
a direct PropertyLayerStyle instance (for uniform styling)
a callable (for conditional or per-layer styling)
This makes the API more intuitive and aligns with common Python patterns.
Implementation
Changes in mesa/visualization/space_renderer.py:
Updated draw_propertylayer to detect when propertylayer_portrayal is a PropertyLayerStyle instance.
When an instance is provided, it's automatically wrapped in a small lambda so the existing backend logic continues to work without modification.
Updated type hints to reflect that the argument may now be either a callable or a style instance.
Testing
Added a dedicated test case: test_property_layer_style_instance in tests/test_space_renderer.py to confirm that passing a PropertyLayerStyle instance works correctly.
Ran the existing test suite (pytest tests/test_space_renderer.py) to ensure no regressions — all 16 tests passed.
Additional Notes
This change reduces boilerplate for the common simple-use scenario while still supporting the more flexible function-based approach for complex styling. It should make the API easier and more pleasant to use without breaking any existing behavior.