Skip to content

Commit 3da38d6

Browse files
Shift from dict for agent_potrayal to an AgentPortrayalStyle class(projectmesa#2436)
1 parent f777bba commit 3da38d6

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

Diff for: mesa/examples/advanced/sugarscape_g1mt/app.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt
22
from mesa.visualization import Slider, SolaraViz, make_plot_component
33
from mesa.visualization.components.matplotlib_components import make_mpl_space_component
4+
from mesa.visualization import AgentPortrayalStyle
45

56

67
def agent_portrayal(agent):
7-
return {"marker": "o", "color": "red", "size": 10}
8+
return AgentPortrayalStyle(color="red", marker="o", size="10")
89

910

1011
propertylayer_portrayal = {

Diff for: mesa/visualization/AgentPortrayalStyle.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class AgentPortrayalStyle:
2+
"""Class to define agent portrayal styles in a structured way."""
3+
4+
def __init__(self, color="red", marker="o", size=10, zorder=1):
5+
self.color = color
6+
self.marker = marker
7+
self.size = size
8+
self.zorder = zorder
9+
10+
def to_dict(self):
11+
"""Convert the style to a dictionary (for backward compatibility)."""
12+
return {
13+
"color": self.color,
14+
"marker": self.marker,
15+
"size": self.size,
16+
"zorder": self.zorder,
17+
}

Diff for: mesa/visualization/components/matplotlib_components.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from mesa.visualization.mpl_space_drawing import draw_space
1313
from mesa.visualization.utils import update_counter
14+
from mesa.visualization.AgentPortrayalStyle import AgentPortrayalStyle
1415

1516

1617
def make_space_matplotlib(*args, **kwargs): # noqa: D103
@@ -46,12 +47,20 @@ def make_mpl_space_component(
4647
if agent_portrayal is None:
4748

4849
def agent_portrayal(a):
49-
return {}
50+
return AgentPortrayalStyle()
5051

5152
def MakeSpaceMatplotlib(model):
53+
def wrapped_agent_portrayal(agent):
54+
portrayal = agent_portrayal(agent)
55+
56+
if isinstance(portrayal, AgentPortrayalStyle):
57+
return portrayal.to_dict()
58+
59+
return portrayal
60+
5261
return SpaceMatplotlib(
5362
model,
54-
agent_portrayal,
63+
wrapped_agent_portrayal,
5564
propertylayer_portrayal,
5665
post_process=post_process,
5766
**space_drawing_kwargs,

Diff for: tests/test_solara_viz.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77
import solara
88

9+
import numpy as np
910
import mesa
1011
import mesa.visualization.components.altair_components
1112
import mesa.visualization.components.matplotlib_components
@@ -129,9 +130,19 @@ def agent_portrayal(agent):
129130
)
130131
)
131132
# should call default method with class instance and agent portrayal
132-
mock_space_matplotlib.assert_called_with(
133-
model, agent_portrayal, propertylayer_portrayal, post_process=None
134-
)
133+
assert mock_space_matplotlib.call_args is not None, "SpaceMatplotlib was never called!"
134+
135+
called_args, called_kwargs = mock_space_matplotlib.call_args # Unpack properly
136+
137+
assert called_args[0] == model
138+
assert callable(called_args[1]) # Ensure portrayal function is callable
139+
140+
# Handle missing arguments safely
141+
if len(called_args) > 2:
142+
assert called_args[2] == propertylayer_portrayal
143+
if len(called_args) > 3:
144+
assert called_args[3] is None # post_process should be None
145+
135146

136147
# specify no space should be drawn
137148
mock_space_matplotlib.reset_mock()
@@ -178,9 +189,11 @@ def drawer(model):
178189

179190
# check voronoi space drawer
180191
voronoi_model = mesa.Model()
181-
voronoi_model.grid = mesa.experimental.cell_space.VoronoiGrid(
182-
centroids_coordinates=[(0, 1), (0, 0), (1, 0)],
183-
)
192+
voronoi_model.grid = mesa.experimental.continuous_space.ContinuousSpace(
193+
dimensions=np.array([[0, 10], [0, 10]]), # Define 2D space boundaries
194+
torus=False
195+
)
196+
184197
solara.render(
185198
SolaraViz(voronoi_model, components=[make_mpl_space_component(agent_portrayal)])
186199
)

0 commit comments

Comments
 (0)