Skip to content

Commit 9572e65

Browse files
rlskoeserrht
authored andcommitted
Revise, test, & document JupyterViz options for drawing agent space
Correct linter rerors flagged by ruff check
1 parent 494c917 commit 9572e65

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

mesa/experimental/jupyter_viz.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,23 @@ def JupyterViz(
2020
measures=None,
2121
name="Mesa Model",
2222
agent_portrayal=None,
23-
space_drawer=None,
23+
space_drawer="default",
2424
play_interval=400,
2525
):
26+
"""Initialize a component to visualize a model.
27+
Args:
28+
model_class: class of the model to instantiate
29+
model_params: parameters for initializing the model
30+
measures: list of callables or data attributes to plot
31+
name: name for display
32+
agent_portrayal: options for rendering agents (dictionary)
33+
space_drawer: method to render the agent space for
34+
the model; default implementation is :meth:`make_space`;
35+
simulations with no space to visualize should
36+
specify `space_drawer=False`
37+
play_interval: play interval (default: 400)
38+
"""
39+
2640
current_step, set_current_step = solara.use_state(0)
2741

2842
solara.Markdown(name)
@@ -48,10 +62,14 @@ def make_model():
4862

4963
with solara.GridFixed(columns=2):
5064
# 4. Space
51-
if space_drawer is None:
65+
if space_drawer == "default":
66+
# draw with the default implementation
5267
make_space(model, agent_portrayal)
53-
else:
68+
elif space_drawer:
69+
# if specified, draw agent space with an alternate renderer
5470
space_drawer(model, agent_portrayal)
71+
# otherwise, do nothing (do not draw space)
72+
5573
# 5. Plots
5674
for measure in measures:
5775
if callable(measure):

tests/test_jupyter_viz.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import unittest
2-
from unittest.mock import patch
2+
from unittest.mock import Mock, patch
33

4-
from mesa.experimental.jupyter_viz import make_user_input
4+
import solara
5+
6+
from mesa.experimental.jupyter_viz import JupyterViz, make_user_input
57

68

79
class TestMakeUserInput(unittest.TestCase):
@@ -46,3 +48,54 @@ def test_label_fallback(self, mock_solara):
4648
mock_solara.SliderInt.assert_called_with(
4749
name, value=value, min=None, max=None, step=None
4850
)
51+
52+
53+
class TestJupyterViz(unittest.TestCase):
54+
@patch("mesa.experimental.jupyter_viz.make_space")
55+
def test_call_space_drawer(self, mock_make_space):
56+
mock_model_class = Mock()
57+
agent_portrayal = {
58+
"Shape": "circle",
59+
"color": "gray",
60+
}
61+
# initialize with space drawer unspecified (use default)
62+
# component must be rendered for code to run
63+
solara.render(
64+
JupyterViz(
65+
model_class=mock_model_class,
66+
model_params={},
67+
agent_portrayal=agent_portrayal,
68+
)
69+
)
70+
# should call default method with class instance and agent portrayal
71+
mock_make_space.assert_called_with(
72+
mock_model_class.return_value, agent_portrayal
73+
)
74+
75+
# specify no space should be drawn; any false value should work
76+
for falsy_value in [None, False, 0]:
77+
mock_make_space.reset_mock()
78+
solara.render(
79+
JupyterViz(
80+
model_class=mock_model_class,
81+
model_params={},
82+
agent_portrayal=agent_portrayal,
83+
space_drawer=falsy_value,
84+
)
85+
)
86+
# should call default method with class instance and agent portrayal
87+
assert mock_make_space.call_count == 0
88+
89+
# specify a custom space method
90+
altspace_drawer = Mock()
91+
solara.render(
92+
JupyterViz(
93+
model_class=mock_model_class,
94+
model_params={},
95+
agent_portrayal=agent_portrayal,
96+
space_drawer=altspace_drawer,
97+
)
98+
)
99+
altspace_drawer.assert_called_with(
100+
mock_model_class.return_value, agent_portrayal
101+
)

0 commit comments

Comments
 (0)