Fix #2904: Use PropertyLayerStyle instead of dict for property layers#2912
Fix #2904: Use PropertyLayerStyle instead of dict for property layers#2912Tejasv-Singh wants to merge 3 commits intomesa:mainfrom
Conversation
|
Performance benchmarks:
|
| def propertylayer_portrayal(layer): | ||
| return PropertyLayerStyle(colormap="viridis", colorbar=True) |
There was a problem hiding this comment.
This function takes layer as input, but doesn't use it. Can you explain why?
There was a problem hiding this comment.
@EwoutH Sorry for the late reply, I was working on fixing the merge conflicts.
To answer your question: I kept the test simple because I just wanted to verify that the renderer accepts the new PropertyLayerStyle class, so inspecting the specific layer logic wasn't necessary for this fixture.
I have opened a fresh PR here: #2920 to handle the merge conflicts cleanly. In that new PR, I renamed the argument to _ as suggested to indicate it is unused. Was facing merge conflicts there as well. I will try to fix that as well.
There was a problem hiding this comment.
@Sahil-Chhoker do you know how this works?
Yeah, the property layer portrayal behaves exactly like agent portrayal, it taken in a layer and calls the portrayal function on every layer and you can pass different portrayals for different layers, in this case all layers are called with only one portrayal function.
There was a problem hiding this comment.
Thanks @Sahil-Chhoker for the explanation, I understand it now. The layer parameter enables conditional styling based on layer properties (name, data values, etc.), similar to how agent portrayal works. For this test, it's simply verifying that the renderer accepts PropertyLayerStyle objects, so the parameter isn't needed.
This PR is good to merge as-is.
However, I'm wondering if we should consider improving this API in a follow-up. The current design feels a bit awkward:
- Simple cases require writing a function wrapper that always returns the same object
- The function --> class pattern isn't the most intuitive
Right now users must write:
def propertylayer_portrayal(layer):
return PropertyLayerStyle(colormap="viridis", colorbar=True)Where the function wrapper adds no value when the style is uniform across all layers.
What if we allowed the visualization to accept either:
- A
PropertyLayerStyleinstance directly (for simple uniform styling) - A callable function (for conditional/per-layer styling)
Similar to how Python's sorted() accepts both key=str.lower and key=lambda x: complex_logic(x).
Not urgent at all, but might be worth following up on.
There was a problem hiding this comment.
Thanks @EwoutH for the detailed write-up, I also think the API can be improved and your approach is pretty good, but currently I'm a little busy with my exams, I'll be able to take a careful look by the end of this month.
|
Closing this PR in favor of #2920. |
Update Property Layer Portrayals to PropertyLayerStyle (Part of #2904)
This PR updates property layer portrayals in tests/test_components_matplotlib.py to use PropertyLayerStyle instead of deprecated dict-based portrayals. This removes related deprecation warnings and aligns the tests with the current visualization API.
Linked Issue
Closes #2904.
Summary of Changes
Added import for PropertyLayerStyle:
from mesa.visualization.components import PropertyLayerStyleRefactored test_draw_property_layers to use a callable portrayal function rather than a dict.
Before
draw_property_layers(grid, {"test": {"colormap": "viridis", "colorbar": True}}, ax)After
No changes required in library code (mpl_space_drawing.py, altair_backend.py) since they already support PropertyLayerStyle.
Testing
Targeted test
pytest tests/test_components_matplotlib.py::test_draw_property_layers -vResult: Passes; deprecation warnings resolved.
Full test file
pytest tests/test_components_matplotlib.py -vResult: All 7 tests pass with no property layer portrayal warnings.
Notes
This PR updates only the tests as part of the incremental migration plan in #2904.
The core visualization pipeline already supports PropertyLayerStyle; this change ensures the tests reflect the modern API.
This resolves item 1.2 from issue #2904.``