Mesa 3.0.0__py3-none-any.whl → 3.0.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of Mesa might be problematic. Click here for more details.
- mesa/__init__.py +3 -3
- mesa/agent.py +114 -406
- mesa/batchrunner.py +27 -54
- mesa/cookiecutter-mesa/cookiecutter.json +8 -0
- mesa/cookiecutter-mesa/hooks/post_gen_project.py +11 -0
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/README.md +4 -0
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/app.pytemplate +27 -0
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/setup.pytemplate +11 -0
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}/model.pytemplate +60 -0
- mesa/datacollection.py +29 -140
- mesa/experimental/__init__.py +1 -11
- mesa/experimental/cell_space/__init__.py +1 -16
- mesa/experimental/cell_space/cell.py +23 -93
- mesa/experimental/cell_space/cell_agent.py +21 -117
- mesa/experimental/cell_space/cell_collection.py +17 -54
- mesa/experimental/cell_space/discrete_space.py +8 -92
- mesa/experimental/cell_space/grid.py +8 -32
- mesa/experimental/cell_space/network.py +7 -12
- mesa/experimental/devs/__init__.py +0 -2
- mesa/experimental/devs/eventlist.py +14 -52
- mesa/experimental/devs/examples/epstein_civil_violence.py +39 -71
- mesa/experimental/devs/examples/wolf_sheep.py +45 -45
- mesa/experimental/devs/simulator.py +15 -55
- mesa/main.py +63 -0
- mesa/model.py +83 -211
- mesa/space.py +149 -215
- mesa/time.py +77 -62
- mesa/{experimental → visualization}/UserParam.py +6 -17
- mesa/visualization/__init__.py +2 -25
- mesa/{experimental → visualization}/components/altair.py +0 -10
- mesa/visualization/components/matplotlib.py +134 -0
- mesa/visualization/solara_viz.py +266 -267
- {mesa-3.0.0.dist-info → mesa-3.0.0a1.dist-info}/METADATA +13 -65
- mesa-3.0.0a1.dist-info/RECORD +38 -0
- mesa-3.0.0.dist-info/licenses/NOTICE → mesa-3.0.0a1.dist-info/licenses/LICENSE +2 -2
- mesa/examples/README.md +0 -37
- mesa/examples/__init__.py +0 -21
- mesa/examples/advanced/epstein_civil_violence/Epstein Civil Violence.ipynb +0 -116
- mesa/examples/advanced/epstein_civil_violence/Readme.md +0 -34
- mesa/examples/advanced/epstein_civil_violence/__init__.py +0 -0
- mesa/examples/advanced/epstein_civil_violence/agents.py +0 -164
- mesa/examples/advanced/epstein_civil_violence/app.py +0 -73
- mesa/examples/advanced/epstein_civil_violence/model.py +0 -114
- mesa/examples/advanced/pd_grid/Readme.md +0 -43
- mesa/examples/advanced/pd_grid/__init__.py +0 -0
- mesa/examples/advanced/pd_grid/agents.py +0 -50
- mesa/examples/advanced/pd_grid/analysis.ipynb +0 -228
- mesa/examples/advanced/pd_grid/app.py +0 -54
- mesa/examples/advanced/pd_grid/model.py +0 -71
- mesa/examples/advanced/sugarscape_g1mt/Readme.md +0 -64
- mesa/examples/advanced/sugarscape_g1mt/__init__.py +0 -0
- mesa/examples/advanced/sugarscape_g1mt/agents.py +0 -344
- mesa/examples/advanced/sugarscape_g1mt/app.py +0 -62
- mesa/examples/advanced/sugarscape_g1mt/model.py +0 -180
- mesa/examples/advanced/sugarscape_g1mt/sugar-map.txt +0 -50
- mesa/examples/advanced/sugarscape_g1mt/tests.py +0 -69
- mesa/examples/advanced/wolf_sheep/Readme.md +0 -57
- mesa/examples/advanced/wolf_sheep/__init__.py +0 -0
- mesa/examples/advanced/wolf_sheep/agents.py +0 -102
- mesa/examples/advanced/wolf_sheep/app.py +0 -84
- mesa/examples/advanced/wolf_sheep/model.py +0 -137
- mesa/examples/basic/__init__.py +0 -0
- mesa/examples/basic/boid_flockers/Readme.md +0 -22
- mesa/examples/basic/boid_flockers/__init__.py +0 -0
- mesa/examples/basic/boid_flockers/agents.py +0 -71
- mesa/examples/basic/boid_flockers/app.py +0 -58
- mesa/examples/basic/boid_flockers/model.py +0 -69
- mesa/examples/basic/boltzmann_wealth_model/Readme.md +0 -56
- mesa/examples/basic/boltzmann_wealth_model/__init__.py +0 -0
- mesa/examples/basic/boltzmann_wealth_model/agents.py +0 -31
- mesa/examples/basic/boltzmann_wealth_model/app.py +0 -74
- mesa/examples/basic/boltzmann_wealth_model/model.py +0 -43
- mesa/examples/basic/boltzmann_wealth_model/st_app.py +0 -115
- mesa/examples/basic/conways_game_of_life/Readme.md +0 -39
- mesa/examples/basic/conways_game_of_life/__init__.py +0 -0
- mesa/examples/basic/conways_game_of_life/agents.py +0 -47
- mesa/examples/basic/conways_game_of_life/app.py +0 -51
- mesa/examples/basic/conways_game_of_life/model.py +0 -31
- mesa/examples/basic/conways_game_of_life/st_app.py +0 -72
- mesa/examples/basic/schelling/Readme.md +0 -40
- mesa/examples/basic/schelling/__init__.py +0 -0
- mesa/examples/basic/schelling/agents.py +0 -26
- mesa/examples/basic/schelling/analysis.ipynb +0 -205
- mesa/examples/basic/schelling/app.py +0 -42
- mesa/examples/basic/schelling/model.py +0 -59
- mesa/examples/basic/virus_on_network/Readme.md +0 -61
- mesa/examples/basic/virus_on_network/__init__.py +0 -0
- mesa/examples/basic/virus_on_network/agents.py +0 -69
- mesa/examples/basic/virus_on_network/app.py +0 -114
- mesa/examples/basic/virus_on_network/model.py +0 -96
- mesa/experimental/cell_space/voronoi.py +0 -257
- mesa/experimental/components/matplotlib.py +0 -242
- mesa/experimental/solara_viz.py +0 -453
- mesa/visualization/components/__init__.py +0 -83
- mesa/visualization/components/altair_components.py +0 -188
- mesa/visualization/components/matplotlib_components.py +0 -175
- mesa/visualization/mpl_space_drawing.py +0 -593
- mesa/visualization/user_param.py +0 -69
- mesa/visualization/utils.py +0 -9
- mesa-3.0.0.dist-info/RECORD +0 -95
- mesa-3.0.0.dist-info/licenses/LICENSE +0 -202
- /mesa/{examples/advanced → cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}}/__init__.py +0 -0
- {mesa-3.0.0.dist-info → mesa-3.0.0a1.dist-info}/WHEEL +0 -0
- {mesa-3.0.0.dist-info → mesa-3.0.0a1.dist-info}/entry_points.txt +0 -0
mesa/experimental/solara_viz.py
DELETED
|
@@ -1,453 +0,0 @@
|
|
|
1
|
-
"""Mesa visualization module for creating interactive model visualizations.
|
|
2
|
-
|
|
3
|
-
This module provides components to create browser- and Jupyter notebook-based visualizations of
|
|
4
|
-
Mesa models, allowing users to watch models run step-by-step and interact with model parameters.
|
|
5
|
-
|
|
6
|
-
Key features:
|
|
7
|
-
- SolaraViz: Main component for creating visualizations, supporting grid displays and plots
|
|
8
|
-
- ModelController: Handles model execution controls (step, play, pause, reset)
|
|
9
|
-
- UserInputs: Generates UI elements for adjusting model parameters
|
|
10
|
-
- Card: Renders individual visualization elements (space, measures)
|
|
11
|
-
|
|
12
|
-
The module uses Solara for rendering in Jupyter notebooks or as standalone web applications.
|
|
13
|
-
It supports various types of visualizations including matplotlib plots, agent grids, and
|
|
14
|
-
custom visualization components.
|
|
15
|
-
|
|
16
|
-
Usage:
|
|
17
|
-
1. Define an agent_portrayal function to specify how agents should be displayed
|
|
18
|
-
2. Set up model_params to define adjustable parameters
|
|
19
|
-
3. Create a SolaraViz instance with your model, parameters, and desired measures
|
|
20
|
-
4. Display the visualization in a Jupyter notebook or run as a Solara app
|
|
21
|
-
|
|
22
|
-
See the Visualization Tutorial and example models for more details.
|
|
23
|
-
"""
|
|
24
|
-
|
|
25
|
-
import threading
|
|
26
|
-
|
|
27
|
-
import reacton.ipywidgets as widgets
|
|
28
|
-
import solara
|
|
29
|
-
from solara.alias import rv
|
|
30
|
-
|
|
31
|
-
import mesa.experimental.components.altair as components_altair
|
|
32
|
-
import mesa.experimental.components.matplotlib as components_matplotlib
|
|
33
|
-
from mesa.experimental.UserParam import Slider
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
# TODO: Turn this function into a Solara component once the current_step.value
|
|
37
|
-
# dependency is passed to measure()
|
|
38
|
-
def Card(
|
|
39
|
-
model, measures, agent_portrayal, space_drawer, dependencies, color, layout_type
|
|
40
|
-
):
|
|
41
|
-
"""Create a card component for visualizing model space or measures.
|
|
42
|
-
|
|
43
|
-
Args:
|
|
44
|
-
model: The Mesa model instance
|
|
45
|
-
measures: List of measures to be plotted
|
|
46
|
-
agent_portrayal: Function to define agent appearance
|
|
47
|
-
space_drawer: Method to render agent space
|
|
48
|
-
dependencies: List of dependencies for updating the visualization
|
|
49
|
-
color: Background color of the card
|
|
50
|
-
layout_type: Type of layout (Space or Measure)
|
|
51
|
-
|
|
52
|
-
Returns:
|
|
53
|
-
rv.Card: A card component containing the visualization
|
|
54
|
-
"""
|
|
55
|
-
with rv.Card(
|
|
56
|
-
style_=f"background-color: {color}; width: 100%; height: 100%"
|
|
57
|
-
) as main:
|
|
58
|
-
if "Space" in layout_type:
|
|
59
|
-
rv.CardTitle(children=["Space"])
|
|
60
|
-
if space_drawer == "default":
|
|
61
|
-
# draw with the default implementation
|
|
62
|
-
components_matplotlib.SpaceMatplotlib(
|
|
63
|
-
model, agent_portrayal, dependencies=dependencies
|
|
64
|
-
)
|
|
65
|
-
elif space_drawer == "altair":
|
|
66
|
-
components_altair.SpaceAltair(
|
|
67
|
-
model, agent_portrayal, dependencies=dependencies
|
|
68
|
-
)
|
|
69
|
-
elif space_drawer:
|
|
70
|
-
# if specified, draw agent space with an alternate renderer
|
|
71
|
-
space_drawer(model, agent_portrayal, dependencies=dependencies)
|
|
72
|
-
elif "Measure" in layout_type:
|
|
73
|
-
rv.CardTitle(children=["Measure"])
|
|
74
|
-
measure = measures[layout_type["Measure"]]
|
|
75
|
-
if callable(measure):
|
|
76
|
-
# Is a custom object
|
|
77
|
-
measure(model)
|
|
78
|
-
else:
|
|
79
|
-
components_matplotlib.PlotMatplotlib(
|
|
80
|
-
model, measure, dependencies=dependencies
|
|
81
|
-
)
|
|
82
|
-
return main
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
@solara.component
|
|
86
|
-
def SolaraViz(
|
|
87
|
-
model_class,
|
|
88
|
-
model_params,
|
|
89
|
-
measures=None,
|
|
90
|
-
name=None,
|
|
91
|
-
agent_portrayal=None,
|
|
92
|
-
space_drawer="default",
|
|
93
|
-
play_interval=150,
|
|
94
|
-
seed=None,
|
|
95
|
-
):
|
|
96
|
-
"""Initialize a component to visualize a model.
|
|
97
|
-
|
|
98
|
-
Args:
|
|
99
|
-
model_class: Class of the model to instantiate
|
|
100
|
-
model_params: Parameters for initializing the model
|
|
101
|
-
measures: List of callables or data attributes to plot
|
|
102
|
-
name: Name for display
|
|
103
|
-
agent_portrayal: Options for rendering agents (dictionary);
|
|
104
|
-
Default drawer supports custom `"size"`, `"color"`, and `"shape"`.
|
|
105
|
-
space_drawer: Method to render the agent space for
|
|
106
|
-
the model; default implementation is the `SpaceMatplotlib` component;
|
|
107
|
-
simulations with no space to visualize should
|
|
108
|
-
specify `space_drawer=False`
|
|
109
|
-
play_interval: Play interval (default: 150)
|
|
110
|
-
seed: The random seed used to initialize the model
|
|
111
|
-
"""
|
|
112
|
-
if name is None:
|
|
113
|
-
name = model_class.__name__
|
|
114
|
-
|
|
115
|
-
current_step = solara.use_reactive(0)
|
|
116
|
-
|
|
117
|
-
# 1. Set up model parameters
|
|
118
|
-
reactive_seed = solara.use_reactive(0)
|
|
119
|
-
user_params, fixed_params = split_model_params(model_params)
|
|
120
|
-
model_parameters, set_model_parameters = solara.use_state(
|
|
121
|
-
{**fixed_params, **{k: v.get("value") for k, v in user_params.items()}}
|
|
122
|
-
)
|
|
123
|
-
|
|
124
|
-
# 2. Set up Model
|
|
125
|
-
def make_model():
|
|
126
|
-
"""Create a new model instance with current parameters and seed."""
|
|
127
|
-
model = model_class.__new__(
|
|
128
|
-
model_class, **model_parameters, seed=reactive_seed.value
|
|
129
|
-
)
|
|
130
|
-
model.__init__(**model_parameters)
|
|
131
|
-
current_step.value = 0
|
|
132
|
-
return model
|
|
133
|
-
|
|
134
|
-
reset_counter = solara.use_reactive(0)
|
|
135
|
-
model = solara.use_memo(
|
|
136
|
-
make_model,
|
|
137
|
-
dependencies=[
|
|
138
|
-
*list(model_parameters.values()),
|
|
139
|
-
reset_counter.value,
|
|
140
|
-
reactive_seed.value,
|
|
141
|
-
],
|
|
142
|
-
)
|
|
143
|
-
|
|
144
|
-
def handle_change_model_params(name: str, value: any):
|
|
145
|
-
"""Update model parameters when user input changes."""
|
|
146
|
-
set_model_parameters({**model_parameters, name: value})
|
|
147
|
-
|
|
148
|
-
# 3. Set up UI
|
|
149
|
-
|
|
150
|
-
with solara.AppBar():
|
|
151
|
-
solara.AppBarTitle(name)
|
|
152
|
-
|
|
153
|
-
# render layout and plot
|
|
154
|
-
def do_reseed():
|
|
155
|
-
"""Update the random seed for the model."""
|
|
156
|
-
reactive_seed.value = model.random.random()
|
|
157
|
-
|
|
158
|
-
dependencies = [
|
|
159
|
-
*list(model_parameters.values()),
|
|
160
|
-
current_step.value,
|
|
161
|
-
reactive_seed.value,
|
|
162
|
-
]
|
|
163
|
-
|
|
164
|
-
# if space drawer is disabled, do not include it
|
|
165
|
-
layout_types = [{"Space": "default"}] if space_drawer else []
|
|
166
|
-
|
|
167
|
-
if measures:
|
|
168
|
-
layout_types += [{"Measure": elem} for elem in range(len(measures))]
|
|
169
|
-
|
|
170
|
-
grid_layout_initial = make_initial_grid_layout(layout_types=layout_types)
|
|
171
|
-
grid_layout, set_grid_layout = solara.use_state(grid_layout_initial)
|
|
172
|
-
|
|
173
|
-
with solara.Sidebar():
|
|
174
|
-
with solara.Card("Controls", margin=1, elevation=2):
|
|
175
|
-
solara.InputText(
|
|
176
|
-
label="Seed",
|
|
177
|
-
value=reactive_seed,
|
|
178
|
-
continuous_update=True,
|
|
179
|
-
)
|
|
180
|
-
UserInputs(user_params, on_change=handle_change_model_params)
|
|
181
|
-
ModelController(model, play_interval, current_step, reset_counter)
|
|
182
|
-
solara.Button(label="Reseed", color="primary", on_click=do_reseed)
|
|
183
|
-
with solara.Card("Information", margin=1, elevation=2):
|
|
184
|
-
solara.Markdown(md_text=f"Step - {current_step}")
|
|
185
|
-
|
|
186
|
-
items = [
|
|
187
|
-
Card(
|
|
188
|
-
model,
|
|
189
|
-
measures,
|
|
190
|
-
agent_portrayal,
|
|
191
|
-
space_drawer,
|
|
192
|
-
dependencies,
|
|
193
|
-
color="white",
|
|
194
|
-
layout_type=layout_types[i],
|
|
195
|
-
)
|
|
196
|
-
for i in range(len(layout_types))
|
|
197
|
-
]
|
|
198
|
-
solara.GridDraggable(
|
|
199
|
-
items=items,
|
|
200
|
-
grid_layout=grid_layout,
|
|
201
|
-
resizable=True,
|
|
202
|
-
draggable=True,
|
|
203
|
-
on_grid_layout=set_grid_layout,
|
|
204
|
-
)
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
JupyterViz = SolaraViz
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
@solara.component
|
|
211
|
-
def ModelController(model, play_interval, current_step, reset_counter):
|
|
212
|
-
"""Create controls for model execution (step, play, pause, reset).
|
|
213
|
-
|
|
214
|
-
Args:
|
|
215
|
-
model: The model being visualized
|
|
216
|
-
play_interval: Interval between steps during play
|
|
217
|
-
current_step: Reactive value for the current step
|
|
218
|
-
reset_counter: Counter to trigger model reset
|
|
219
|
-
"""
|
|
220
|
-
playing = solara.use_reactive(False)
|
|
221
|
-
thread = solara.use_reactive(None)
|
|
222
|
-
# We track the previous step to detect if user resets the model via
|
|
223
|
-
# clicking the reset button or changing the parameters. If previous_step >
|
|
224
|
-
# current_step, it means a model reset happens while the simulation is
|
|
225
|
-
# still playing.
|
|
226
|
-
previous_step = solara.use_reactive(0)
|
|
227
|
-
|
|
228
|
-
def on_value_play(change):
|
|
229
|
-
"""Handle play/pause state changes."""
|
|
230
|
-
if previous_step.value > current_step.value and current_step.value == 0:
|
|
231
|
-
# We add extra checks for current_step.value == 0, just to be sure.
|
|
232
|
-
# We automatically stop the playing if a model is reset.
|
|
233
|
-
playing.value = False
|
|
234
|
-
elif model.running:
|
|
235
|
-
do_step()
|
|
236
|
-
else:
|
|
237
|
-
playing.value = False
|
|
238
|
-
|
|
239
|
-
def do_step():
|
|
240
|
-
"""Advance the model by one step."""
|
|
241
|
-
model.step()
|
|
242
|
-
previous_step.value = current_step.value
|
|
243
|
-
current_step.value = model.steps
|
|
244
|
-
|
|
245
|
-
def do_play():
|
|
246
|
-
"""Run the model continuously."""
|
|
247
|
-
model.running = True
|
|
248
|
-
while model.running:
|
|
249
|
-
do_step()
|
|
250
|
-
|
|
251
|
-
def threaded_do_play():
|
|
252
|
-
"""Start a new thread for continuous model execution."""
|
|
253
|
-
if thread is not None and thread.is_alive():
|
|
254
|
-
return
|
|
255
|
-
thread.value = threading.Thread(target=do_play)
|
|
256
|
-
thread.start()
|
|
257
|
-
|
|
258
|
-
def do_pause():
|
|
259
|
-
"""Pause the model execution."""
|
|
260
|
-
if (thread is None) or (not thread.is_alive()):
|
|
261
|
-
return
|
|
262
|
-
model.running = False
|
|
263
|
-
thread.join()
|
|
264
|
-
|
|
265
|
-
def do_reset():
|
|
266
|
-
"""Reset the model."""
|
|
267
|
-
reset_counter.value += 1
|
|
268
|
-
|
|
269
|
-
def do_set_playing(value):
|
|
270
|
-
"""Set the playing state."""
|
|
271
|
-
if current_step.value == 0:
|
|
272
|
-
# This means the model has been recreated, and the step resets to
|
|
273
|
-
# 0. We want to avoid triggering the playing.value = False in the
|
|
274
|
-
# on_value_play function.
|
|
275
|
-
previous_step.value = current_step.value
|
|
276
|
-
playing.set(value)
|
|
277
|
-
|
|
278
|
-
with solara.Row():
|
|
279
|
-
solara.Button(label="Step", color="primary", on_click=do_step)
|
|
280
|
-
# This style is necessary so that the play widget has almost the same
|
|
281
|
-
# height as typical Solara buttons.
|
|
282
|
-
solara.Style(
|
|
283
|
-
"""
|
|
284
|
-
.widget-play {
|
|
285
|
-
height: 35px;
|
|
286
|
-
}
|
|
287
|
-
.widget-play button {
|
|
288
|
-
color: white;
|
|
289
|
-
background-color: #1976D2; // Solara blue color
|
|
290
|
-
}
|
|
291
|
-
"""
|
|
292
|
-
)
|
|
293
|
-
widgets.Play(
|
|
294
|
-
value=0,
|
|
295
|
-
interval=play_interval,
|
|
296
|
-
repeat=True,
|
|
297
|
-
show_repeat=False,
|
|
298
|
-
on_value=on_value_play,
|
|
299
|
-
playing=playing.value,
|
|
300
|
-
on_playing=do_set_playing,
|
|
301
|
-
)
|
|
302
|
-
solara.Button(label="Reset", color="primary", on_click=do_reset)
|
|
303
|
-
# threaded_do_play is not used for now because it
|
|
304
|
-
# doesn't work in Google colab. We use
|
|
305
|
-
# ipywidgets.Play until it is fixed. The threading
|
|
306
|
-
# version is definite a much better implementation,
|
|
307
|
-
# if it works.
|
|
308
|
-
# solara.Button(label="▶", color="primary", on_click=viz.threaded_do_play)
|
|
309
|
-
# solara.Button(label="⏸︎", color="primary", on_click=viz.do_pause)
|
|
310
|
-
# solara.Button(label="Reset", color="primary", on_click=do_reset)
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
def split_model_params(model_params):
|
|
314
|
-
"""Split model parameters into user-adjustable and fixed parameters.
|
|
315
|
-
|
|
316
|
-
Args:
|
|
317
|
-
model_params: Dictionary of all model parameters
|
|
318
|
-
|
|
319
|
-
Returns:
|
|
320
|
-
tuple: (user_adjustable_params, fixed_params)
|
|
321
|
-
"""
|
|
322
|
-
model_params_input = {}
|
|
323
|
-
model_params_fixed = {}
|
|
324
|
-
for k, v in model_params.items():
|
|
325
|
-
if check_param_is_fixed(v):
|
|
326
|
-
model_params_fixed[k] = v
|
|
327
|
-
else:
|
|
328
|
-
model_params_input[k] = v
|
|
329
|
-
return model_params_input, model_params_fixed
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
def check_param_is_fixed(param):
|
|
333
|
-
"""Check if a parameter is fixed (not user-adjustable).
|
|
334
|
-
|
|
335
|
-
Args:
|
|
336
|
-
param: Parameter to check
|
|
337
|
-
|
|
338
|
-
Returns:
|
|
339
|
-
bool: True if parameter is fixed, False otherwise
|
|
340
|
-
"""
|
|
341
|
-
if isinstance(param, Slider):
|
|
342
|
-
return False
|
|
343
|
-
if not isinstance(param, dict):
|
|
344
|
-
return True
|
|
345
|
-
if "type" not in param:
|
|
346
|
-
return True
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
@solara.component
|
|
350
|
-
def UserInputs(user_params, on_change=None):
|
|
351
|
-
"""Initialize user inputs for configurable model parameters.
|
|
352
|
-
|
|
353
|
-
Currently supports :class:`solara.SliderInt`, :class:`solara.SliderFloat`,
|
|
354
|
-
:class:`solara.Select`, and :class:`solara.Checkbox`.
|
|
355
|
-
|
|
356
|
-
Args:
|
|
357
|
-
user_params: Dictionary with options for the input, including label,
|
|
358
|
-
min and max values, and other fields specific to the input type.
|
|
359
|
-
on_change: Function to be called with (name, value) when the value of an input changes.
|
|
360
|
-
"""
|
|
361
|
-
for name, options in user_params.items():
|
|
362
|
-
|
|
363
|
-
def change_handler(value, name=name):
|
|
364
|
-
on_change(name, value)
|
|
365
|
-
|
|
366
|
-
if isinstance(options, Slider):
|
|
367
|
-
slider_class = (
|
|
368
|
-
solara.SliderFloat if options.is_float_slider else solara.SliderInt
|
|
369
|
-
)
|
|
370
|
-
slider_class(
|
|
371
|
-
options.label,
|
|
372
|
-
value=options.value,
|
|
373
|
-
on_value=change_handler,
|
|
374
|
-
min=options.min,
|
|
375
|
-
max=options.max,
|
|
376
|
-
step=options.step,
|
|
377
|
-
)
|
|
378
|
-
continue
|
|
379
|
-
|
|
380
|
-
# label for the input is "label" from options or name
|
|
381
|
-
label = options.get("label", name)
|
|
382
|
-
input_type = options.get("type")
|
|
383
|
-
if input_type == "SliderInt":
|
|
384
|
-
solara.SliderInt(
|
|
385
|
-
label,
|
|
386
|
-
value=options.get("value"),
|
|
387
|
-
on_value=change_handler,
|
|
388
|
-
min=options.get("min"),
|
|
389
|
-
max=options.get("max"),
|
|
390
|
-
step=options.get("step"),
|
|
391
|
-
)
|
|
392
|
-
elif input_type == "SliderFloat":
|
|
393
|
-
solara.SliderFloat(
|
|
394
|
-
label,
|
|
395
|
-
value=options.get("value"),
|
|
396
|
-
on_value=change_handler,
|
|
397
|
-
min=options.get("min"),
|
|
398
|
-
max=options.get("max"),
|
|
399
|
-
step=options.get("step"),
|
|
400
|
-
)
|
|
401
|
-
elif input_type == "Select":
|
|
402
|
-
solara.Select(
|
|
403
|
-
label,
|
|
404
|
-
value=options.get("value"),
|
|
405
|
-
on_value=change_handler,
|
|
406
|
-
values=options.get("values"),
|
|
407
|
-
)
|
|
408
|
-
elif input_type == "Checkbox":
|
|
409
|
-
solara.Checkbox(
|
|
410
|
-
label=label,
|
|
411
|
-
on_value=change_handler,
|
|
412
|
-
value=options.get("value"),
|
|
413
|
-
)
|
|
414
|
-
else:
|
|
415
|
-
raise ValueError(f"{input_type} is not a supported input type")
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
def make_text(renderer):
|
|
419
|
-
"""Create a function that renders text using Markdown.
|
|
420
|
-
|
|
421
|
-
Args:
|
|
422
|
-
renderer: Function that takes a model and returns a string
|
|
423
|
-
|
|
424
|
-
Returns:
|
|
425
|
-
function: A function that renders the text as Markdown
|
|
426
|
-
"""
|
|
427
|
-
|
|
428
|
-
def function(model):
|
|
429
|
-
solara.Markdown(renderer(model))
|
|
430
|
-
|
|
431
|
-
return function
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
def make_initial_grid_layout(layout_types):
|
|
435
|
-
"""Create an initial grid layout for visualization components.
|
|
436
|
-
|
|
437
|
-
Args:
|
|
438
|
-
layout_types: List of layout types (Space or Measure)
|
|
439
|
-
|
|
440
|
-
Returns:
|
|
441
|
-
list: Initial grid layout configuration
|
|
442
|
-
"""
|
|
443
|
-
return [
|
|
444
|
-
{
|
|
445
|
-
"i": i,
|
|
446
|
-
"w": 6,
|
|
447
|
-
"h": 10,
|
|
448
|
-
"moved": False,
|
|
449
|
-
"x": 6 * (i % 2),
|
|
450
|
-
"y": 16 * (i - i % 2),
|
|
451
|
-
}
|
|
452
|
-
for i in range(len(layout_types))
|
|
453
|
-
]
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"""custom solara components."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
from collections.abc import Callable
|
|
6
|
-
|
|
7
|
-
from .altair_components import SpaceAltair, make_altair_space
|
|
8
|
-
from .matplotlib_components import (
|
|
9
|
-
SpaceMatplotlib,
|
|
10
|
-
make_mpl_plot_component,
|
|
11
|
-
make_mpl_space_component,
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def make_space_component(
|
|
16
|
-
agent_portrayal: Callable | None = None,
|
|
17
|
-
propertylayer_portrayal: dict | None = None,
|
|
18
|
-
post_process: Callable | None = None,
|
|
19
|
-
backend: str = "matplotlib",
|
|
20
|
-
**space_drawing_kwargs,
|
|
21
|
-
) -> SpaceMatplotlib | SpaceAltair:
|
|
22
|
-
"""Create a Matplotlib-based space visualization component.
|
|
23
|
-
|
|
24
|
-
Args:
|
|
25
|
-
agent_portrayal: Function to portray agents.
|
|
26
|
-
propertylayer_portrayal: Dictionary of PropertyLayer portrayal specifications
|
|
27
|
-
post_process : a callable that will be called with the Axes instance. Allows for fine-tuning plots (e.g., control ticks)
|
|
28
|
-
backend: the backend to use {"matplotlib", "altair"}
|
|
29
|
-
space_drawing_kwargs : additional keyword arguments to be passed on to the underlying backend specific space drawer function. See
|
|
30
|
-
the functions for drawing the various spaces for the appropriate backend further details.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
Returns:
|
|
34
|
-
function: A function that creates a space component
|
|
35
|
-
"""
|
|
36
|
-
if backend == "matplotlib":
|
|
37
|
-
return make_mpl_space_component(
|
|
38
|
-
agent_portrayal,
|
|
39
|
-
propertylayer_portrayal,
|
|
40
|
-
post_process,
|
|
41
|
-
**space_drawing_kwargs,
|
|
42
|
-
)
|
|
43
|
-
elif backend == "altair":
|
|
44
|
-
return make_altair_space(
|
|
45
|
-
agent_portrayal,
|
|
46
|
-
propertylayer_portrayal,
|
|
47
|
-
post_process,
|
|
48
|
-
**space_drawing_kwargs,
|
|
49
|
-
)
|
|
50
|
-
else:
|
|
51
|
-
raise ValueError(
|
|
52
|
-
f"unknown backend {backend}, must be one of matplotlib, altair"
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
def make_plot_component(
|
|
57
|
-
measure: str | dict[str, str] | list[str] | tuple[str],
|
|
58
|
-
post_process: Callable | None = None,
|
|
59
|
-
backend: str = "matplotlib",
|
|
60
|
-
**plot_drawing_kwargs,
|
|
61
|
-
):
|
|
62
|
-
"""Create a plotting function for a specified measure using the specified backend.
|
|
63
|
-
|
|
64
|
-
Args:
|
|
65
|
-
measure (str | dict[str, str] | list[str] | tuple[str]): Measure(s) to plot.
|
|
66
|
-
post_process: a user-specified callable to do post-processing called with the Axes instance.
|
|
67
|
-
backend: the backend to use {"matplotlib", "altair"}
|
|
68
|
-
plot_drawing_kwargs: additional keyword arguments to pass onto the backend specific function for making a plotting component
|
|
69
|
-
|
|
70
|
-
Notes:
|
|
71
|
-
altair plotting backend is not yet implemented and planned for mesa 3.1.
|
|
72
|
-
|
|
73
|
-
Returns:
|
|
74
|
-
function: A function that creates a plot component
|
|
75
|
-
"""
|
|
76
|
-
if backend == "matplotlib":
|
|
77
|
-
return make_mpl_plot_component(measure, post_process, **plot_drawing_kwargs)
|
|
78
|
-
elif backend == "altair":
|
|
79
|
-
raise NotImplementedError("altair line plots are not yet implemented")
|
|
80
|
-
else:
|
|
81
|
-
raise ValueError(
|
|
82
|
-
f"unknown backend {backend}, must be one of matplotlib, altair"
|
|
83
|
-
)
|