Skip to content

Commit f35c4d0

Browse files
Deprecate agent_portrayal and update Migration guide (#2872)
Updates the migration guide inline with the new visualization API. Also activates the deprecation warnings for portrayal components.
1 parent e48c1cd commit f35c4d0

File tree

5 files changed

+138
-16
lines changed

5 files changed

+138
-16
lines changed

docs/migration_guide.md

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,114 @@ This guide contains breaking changes between major Mesa versions and how to reso
33

44
Non-breaking changes aren't included, for those see our [Release history](https://github.com/projectmesa/mesa/releases).
55

6+
## Mesa 3.3.0
7+
8+
Mesa 3.3.0 is a visualization upgrade introducing a new and improved API, full support for both `altair` and `matplotlib` backends, and resolving several recurring issues from previous versions.
9+
For full details on how to visualize your model, refer to the [Mesa Documentation](https://mesa.readthedocs.io/latest/tutorials/4_visualization_basic.html).
610

7-
## Mesa 3.0
8-
Mesa 3.0 introduces significant changes to core functionalities, including agent and model initialization, scheduling, and visualization. The guide below outlines these changes and provides instructions for migrating your existing Mesa projects to version 3.0.
911

1012
_This guide is a work in progress. The development of it is tracked in [Issue #2233](https://github.com/projectmesa/mesa/issues/2233)._
1113

1214

15+
### Defining Portrayal Components
16+
Previously, `agent_portrayal` returned a dictionary. Now, it returns an instance of a dedicated portrayal component called `AgentPortrayalStyle`.
17+
18+
```python
19+
# Old
20+
def agent_portrayal(agent):
21+
return {
22+
"color": "white" if agent.state == 0 else "black",
23+
"marker": "s",
24+
"size": "30"
25+
}
26+
27+
# New
28+
def agent_portrayal(agent):
29+
return AgentPortrayalStyle(
30+
color="white" if agent.state == 0 else "black",
31+
marker="s",
32+
size=30,
33+
)
34+
```
35+
36+
Similarly, `propertylayer_portrayal` has moved from a dictionary-based interface to a function-based one, following the same pattern as `agent_portrayal`. It now returns a `PropertyLayerStyle` instance instead of a dictionary.
37+
38+
```python
39+
# Old
40+
propertylayer_portrayal = {
41+
"sugar": {
42+
"colormap": "pastel1",
43+
"alpha": 0.75,
44+
"colorbar": True,
45+
"vmin": 0,
46+
"vmax": 10,
47+
}
48+
}
49+
50+
# New
51+
def propertylayer_portrayal(layer):
52+
if layer.name == "sugar":
53+
return PropertyLayerStyle(
54+
color="pastel1", alpha=0.75, colorbar=True, vmin=0, vmax=10
55+
)
56+
```
57+
58+
* Ref: [PR #2786](https://github.com/projectmesa/mesa/pull/2786)
59+
60+
### Default Space Visualization
61+
While the visualization methods from Mesa versions before 3.3.0 still work, version 3.3.0 introduces `SpaceRenderer`, which changes how space visualizations are rendered. Check out the updated [Mesa documentation](https://mesa.readthedocs.io/latest/tutorials/4_visualization_basic.html) for guidance on upgrading your model’s visualization using `SpaceRenderer`.
62+
63+
A basic example of how `SpaceRenderer` works:
64+
65+
```python
66+
# Old
67+
from mesa.visualization import SolaraViz, make_space_component
68+
69+
SolaraViz(model, components=[make_space_component(agent_portrayal)])
70+
71+
# New
72+
from mesa.visualization import SolaraViz, SpaceRenderer
73+
74+
renderer = SpaceRenderer(model, backend="matplotlib").render(
75+
agent_portrayal=agent_portrayal,
76+
...
77+
)
78+
79+
SolaraViz(
80+
model,
81+
renderer,
82+
components=[],
83+
...
84+
)
85+
```
86+
87+
* Ref: [PR #2803](https://github.com/projectmesa/mesa/pull/2803), [PR #2810](https://github.com/projectmesa/mesa/pull/2810)
88+
89+
### Page Tab View
90+
91+
Version 3.3.0 adds support for defining pages for different plot components. Learn more in the [Mesa documentation](https://mesa.readthedocs.io/latest/tutorials/6_visualization_rendering_with_space_renderer.html).
92+
93+
In short, you can define multiple pages using the following syntax:
94+
95+
```python
96+
from mesa.visualization import SolaraViz, make_plot_component
97+
98+
SolaraViz(
99+
model,
100+
components=[
101+
make_plot_component("foo", page=1),
102+
make_plot_component("bar", "baz", page=2),
103+
],
104+
)
105+
```
106+
107+
* Ref: [PR #2827](https://github.com/projectmesa/mesa/pull/2827)
108+
109+
110+
## Mesa 3.0
111+
Mesa 3.0 introduces significant changes to core functionalities, including agent and model initialization, scheduling, and visualization. The guide below outlines these changes and provides instructions for migrating your existing Mesa projects to version 3.0.
112+
113+
13114
### Upgrade strategy
14115
We recommend the following upgrade strategy:
15116
- Update to the latest Mesa 2.x release (`mesa<3`).

mesa/visualization/backends/altair_backend.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ def collect_agent_data(
107107

108108
if isinstance(portray_input, dict):
109109
warnings.warn(
110-
"Returning a dict from agent_portrayal is deprecated. "
111-
"Please return an AgentPortrayalStyle instance instead.",
112-
PendingDeprecationWarning,
110+
(
111+
"Returning a dict from agent_portrayal is deprecated. "
112+
"Please return an AgentPortrayalStyle instance instead. "
113+
"For more information, refer to the migration guide: "
114+
"https://mesa.readthedocs.io/latest/migration_guide.html#defining-portrayal-components"
115+
),
116+
DeprecationWarning,
113117
stacklevel=2,
114118
)
115119
dict_data = portray_input.copy()

mesa/visualization/backends/matplotlib_backend.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ def collect_agent_data(self, space, agent_portrayal, default_size=None):
107107

108108
if isinstance(portray_input, dict):
109109
warnings.warn(
110-
"Returning a dict from agent_portrayal is deprecated. "
111-
"Please return an AgentPortrayalStyle instance instead.",
112-
PendingDeprecationWarning,
110+
(
111+
"Returning a dict from agent_portrayal is deprecated. "
112+
"Please return an AgentPortrayalStyle instance instead. "
113+
"For more information, refer to the migration guide: "
114+
"https://mesa.readthedocs.io/latest/migration_guide.html#defining-portrayal-components"
115+
),
116+
DeprecationWarning,
113117
stacklevel=2,
114118
)
115119
# Handle legacy dict input

mesa/visualization/mpl_space_drawing.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ def get_agent_pos(agent, space):
104104

105105
if isinstance(portray_input, dict):
106106
warnings.warn(
107-
"Returning a dict from agent_portrayal is deprecated and will be removed "
108-
"in a future version. Please return an AgentPortrayalStyle instance instead.",
109-
PendingDeprecationWarning,
107+
(
108+
"Returning a dict from agent_portrayal is deprecated. "
109+
"Please return an AgentPortrayalStyle instance instead. "
110+
"For more information, refer to the migration guide: "
111+
"https://mesa.readthedocs.io/latest/migration_guide.html#defining-portrayal-components"
112+
),
113+
DeprecationWarning,
110114
stacklevel=2,
111115
)
112116
dict_data = portray_input.copy()
@@ -297,8 +301,13 @@ def style_callable(layer_object: Any):
297301
params = propertylayer_portrayal.get(layer_name)
298302

299303
warnings.warn(
300-
"The propertylayer_portrayal dict is deprecated. Use a callable that returns PropertyLayerStyle instead.",
301-
PendingDeprecationWarning,
304+
(
305+
"The propertylayer_portrayal dict is deprecated. "
306+
"Please use a callable that returns a PropertyLayerStyle instance instead. "
307+
"For more information, refer to the migration guide: "
308+
"https://mesa.readthedocs.io/latest/migration_guide.html#defining-portrayal-components"
309+
),
310+
DeprecationWarning,
302311
stacklevel=2,
303312
)
304313

mesa/visualization/space_renderer.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,13 @@ def style_callable(layer_object):
234234
params = portrayal_dict.get(layer_name)
235235

236236
warnings.warn(
237-
"Dict propertylayer_portrayal is deprecated. "
238-
"Use a callable returning PropertyLayerStyle instead.",
239-
PendingDeprecationWarning,
237+
(
238+
"The propertylayer_portrayal dict is deprecated. "
239+
"Please use a callable that returns a PropertyLayerStyle instance instead. "
240+
"For more information, refer to the migration guide: "
241+
"https://mesa.readthedocs.io/latest/migration_guide.html#defining-portrayal-components"
242+
),
243+
DeprecationWarning,
240244
stacklevel=2,
241245
)
242246

0 commit comments

Comments
 (0)