Mesa 2.3.4__py3-none-any.whl → 3.0.0__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 -5
- mesa/agent.py +393 -116
- mesa/batchrunner.py +58 -31
- mesa/datacollection.py +141 -30
- mesa/examples/README.md +37 -0
- mesa/examples/__init__.py +21 -0
- mesa/examples/advanced/epstein_civil_violence/Epstein Civil Violence.ipynb +116 -0
- mesa/examples/advanced/epstein_civil_violence/Readme.md +34 -0
- mesa/examples/advanced/epstein_civil_violence/__init__.py +0 -0
- mesa/examples/advanced/epstein_civil_violence/agents.py +164 -0
- mesa/examples/advanced/epstein_civil_violence/app.py +73 -0
- mesa/examples/advanced/epstein_civil_violence/model.py +114 -0
- mesa/examples/advanced/pd_grid/Readme.md +43 -0
- mesa/examples/advanced/pd_grid/__init__.py +0 -0
- mesa/examples/advanced/pd_grid/agents.py +50 -0
- mesa/examples/advanced/pd_grid/analysis.ipynb +228 -0
- mesa/examples/advanced/pd_grid/app.py +54 -0
- mesa/examples/advanced/pd_grid/model.py +71 -0
- mesa/examples/advanced/sugarscape_g1mt/Readme.md +64 -0
- mesa/examples/advanced/sugarscape_g1mt/__init__.py +0 -0
- mesa/examples/advanced/sugarscape_g1mt/agents.py +344 -0
- mesa/examples/advanced/sugarscape_g1mt/app.py +62 -0
- mesa/examples/advanced/sugarscape_g1mt/model.py +180 -0
- mesa/examples/advanced/sugarscape_g1mt/sugar-map.txt +50 -0
- mesa/examples/advanced/sugarscape_g1mt/tests.py +69 -0
- mesa/examples/advanced/wolf_sheep/Readme.md +57 -0
- mesa/examples/advanced/wolf_sheep/__init__.py +0 -0
- mesa/examples/advanced/wolf_sheep/agents.py +102 -0
- mesa/examples/advanced/wolf_sheep/app.py +84 -0
- mesa/examples/advanced/wolf_sheep/model.py +137 -0
- mesa/examples/basic/__init__.py +0 -0
- mesa/examples/basic/boid_flockers/Readme.md +22 -0
- mesa/examples/basic/boid_flockers/__init__.py +0 -0
- mesa/examples/basic/boid_flockers/agents.py +71 -0
- mesa/examples/basic/boid_flockers/app.py +58 -0
- mesa/examples/basic/boid_flockers/model.py +69 -0
- mesa/examples/basic/boltzmann_wealth_model/Readme.md +56 -0
- mesa/examples/basic/boltzmann_wealth_model/__init__.py +0 -0
- mesa/examples/basic/boltzmann_wealth_model/agents.py +31 -0
- mesa/examples/basic/boltzmann_wealth_model/app.py +74 -0
- mesa/examples/basic/boltzmann_wealth_model/model.py +43 -0
- mesa/examples/basic/boltzmann_wealth_model/st_app.py +115 -0
- mesa/examples/basic/conways_game_of_life/Readme.md +39 -0
- mesa/examples/basic/conways_game_of_life/__init__.py +0 -0
- mesa/examples/basic/conways_game_of_life/agents.py +47 -0
- mesa/examples/basic/conways_game_of_life/app.py +51 -0
- mesa/examples/basic/conways_game_of_life/model.py +31 -0
- mesa/examples/basic/conways_game_of_life/st_app.py +72 -0
- mesa/examples/basic/schelling/Readme.md +40 -0
- mesa/examples/basic/schelling/__init__.py +0 -0
- mesa/examples/basic/schelling/agents.py +26 -0
- mesa/examples/basic/schelling/analysis.ipynb +205 -0
- mesa/examples/basic/schelling/app.py +42 -0
- mesa/examples/basic/schelling/model.py +59 -0
- mesa/examples/basic/virus_on_network/Readme.md +61 -0
- mesa/examples/basic/virus_on_network/__init__.py +0 -0
- mesa/examples/basic/virus_on_network/agents.py +69 -0
- mesa/examples/basic/virus_on_network/app.py +114 -0
- mesa/examples/basic/virus_on_network/model.py +96 -0
- mesa/experimental/UserParam.py +18 -7
- mesa/experimental/__init__.py +10 -2
- mesa/experimental/cell_space/__init__.py +16 -1
- mesa/experimental/cell_space/cell.py +93 -23
- mesa/experimental/cell_space/cell_agent.py +117 -21
- mesa/experimental/cell_space/cell_collection.py +56 -19
- mesa/experimental/cell_space/discrete_space.py +92 -8
- mesa/experimental/cell_space/grid.py +33 -9
- mesa/experimental/cell_space/network.py +15 -10
- mesa/experimental/cell_space/voronoi.py +257 -0
- mesa/experimental/components/altair.py +11 -2
- mesa/experimental/components/matplotlib.py +132 -26
- mesa/experimental/devs/__init__.py +2 -0
- mesa/experimental/devs/eventlist.py +54 -15
- mesa/experimental/devs/examples/epstein_civil_violence.py +71 -39
- mesa/experimental/devs/examples/wolf_sheep.py +45 -45
- mesa/experimental/devs/simulator.py +57 -16
- mesa/experimental/{jupyter_viz.py → solara_viz.py} +151 -98
- mesa/model.py +212 -84
- mesa/space.py +217 -151
- mesa/time.py +63 -80
- mesa/visualization/__init__.py +25 -6
- mesa/visualization/components/__init__.py +83 -0
- mesa/visualization/components/altair_components.py +188 -0
- mesa/visualization/components/matplotlib_components.py +175 -0
- mesa/visualization/mpl_space_drawing.py +593 -0
- mesa/visualization/solara_viz.py +458 -0
- mesa/visualization/user_param.py +69 -0
- mesa/visualization/utils.py +9 -0
- {mesa-2.3.4.dist-info → mesa-3.0.0.dist-info}/METADATA +65 -19
- mesa-3.0.0.dist-info/RECORD +95 -0
- mesa-3.0.0.dist-info/licenses/LICENSE +202 -0
- mesa-2.3.4.dist-info/licenses/LICENSE → mesa-3.0.0.dist-info/licenses/NOTICE +2 -2
- mesa/cookiecutter-mesa/cookiecutter.json +0 -8
- mesa/cookiecutter-mesa/hooks/post_gen_project.py +0 -11
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/README.md +0 -4
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/run.pytemplate +0 -3
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/setup.pytemplate +0 -11
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}/model.pytemplate +0 -60
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}/server.pytemplate +0 -36
- mesa/flat/__init__.py +0 -6
- mesa/flat/visualization.py +0 -5
- mesa/main.py +0 -63
- mesa/visualization/ModularVisualization.py +0 -1
- mesa/visualization/TextVisualization.py +0 -1
- mesa/visualization/UserParam.py +0 -1
- mesa/visualization/modules.py +0 -1
- mesa-2.3.4.dist-info/RECORD +0 -45
- /mesa/{cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}} → examples/advanced}/__init__.py +0 -0
- {mesa-2.3.4.dist-info → mesa-3.0.0.dist-info}/WHEEL +0 -0
- {mesa-2.3.4.dist-info → mesa-3.0.0.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"""Matplotlib based solara components for visualization MESA spaces and plots."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import warnings
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
|
|
8
|
+
import matplotlib.pyplot as plt
|
|
9
|
+
import solara
|
|
10
|
+
from matplotlib.figure import Figure
|
|
11
|
+
|
|
12
|
+
from mesa.visualization.mpl_space_drawing import draw_space
|
|
13
|
+
from mesa.visualization.utils import update_counter
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def make_space_matplotlib(*args, **kwargs): # noqa: D103
|
|
17
|
+
warnings.warn(
|
|
18
|
+
"make_space_matplotlib has been renamed to make_mpl_space_component",
|
|
19
|
+
DeprecationWarning,
|
|
20
|
+
stacklevel=2,
|
|
21
|
+
)
|
|
22
|
+
return make_mpl_space_component(*args, **kwargs)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def make_mpl_space_component(
|
|
26
|
+
agent_portrayal: Callable | None = None,
|
|
27
|
+
propertylayer_portrayal: dict | None = None,
|
|
28
|
+
post_process: Callable | None = None,
|
|
29
|
+
**space_drawing_kwargs,
|
|
30
|
+
) -> SpaceMatplotlib:
|
|
31
|
+
"""Create a Matplotlib-based space visualization component.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
agent_portrayal: Function to portray agents.
|
|
35
|
+
propertylayer_portrayal: Dictionary of PropertyLayer portrayal specifications
|
|
36
|
+
post_process : a callable that will be called with the Axes instance. Allows for fine tuning plots (e.g., control ticks)
|
|
37
|
+
space_drawing_kwargs : additional keyword arguments to be passed on to the underlying space drawer function. See
|
|
38
|
+
the functions for drawing the various spaces for further details.
|
|
39
|
+
|
|
40
|
+
``agent_portrayal`` is called with an agent and should return a dict. Valid fields in this dict are "color",
|
|
41
|
+
"size", "marker", "zorder", alpha, linewidths, and edgecolors. Other field are ignored and will result in a user warning.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
function: A function that creates a SpaceMatplotlib component
|
|
45
|
+
"""
|
|
46
|
+
if agent_portrayal is None:
|
|
47
|
+
|
|
48
|
+
def agent_portrayal(a):
|
|
49
|
+
return {}
|
|
50
|
+
|
|
51
|
+
def MakeSpaceMatplotlib(model):
|
|
52
|
+
return SpaceMatplotlib(
|
|
53
|
+
model,
|
|
54
|
+
agent_portrayal,
|
|
55
|
+
propertylayer_portrayal,
|
|
56
|
+
post_process=post_process,
|
|
57
|
+
**space_drawing_kwargs,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
return MakeSpaceMatplotlib
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@solara.component
|
|
64
|
+
def SpaceMatplotlib(
|
|
65
|
+
model,
|
|
66
|
+
agent_portrayal,
|
|
67
|
+
propertylayer_portrayal,
|
|
68
|
+
dependencies: list[any] | None = None,
|
|
69
|
+
post_process: Callable | None = None,
|
|
70
|
+
**space_drawing_kwargs,
|
|
71
|
+
):
|
|
72
|
+
"""Create a Matplotlib-based space visualization component."""
|
|
73
|
+
update_counter.get()
|
|
74
|
+
|
|
75
|
+
space = getattr(model, "grid", None)
|
|
76
|
+
if space is None:
|
|
77
|
+
space = getattr(model, "space", None)
|
|
78
|
+
|
|
79
|
+
fig = Figure()
|
|
80
|
+
ax = fig.add_subplot()
|
|
81
|
+
|
|
82
|
+
draw_space(
|
|
83
|
+
space,
|
|
84
|
+
agent_portrayal,
|
|
85
|
+
propertylayer_portrayal=propertylayer_portrayal,
|
|
86
|
+
ax=ax,
|
|
87
|
+
**space_drawing_kwargs,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
if post_process is not None:
|
|
91
|
+
post_process(ax)
|
|
92
|
+
|
|
93
|
+
solara.FigureMatplotlib(
|
|
94
|
+
fig, format="png", bbox_inches="tight", dependencies=dependencies
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def make_plot_measure(*args, **kwargs): # noqa: D103
|
|
99
|
+
warnings.warn(
|
|
100
|
+
"make_plot_measure has been renamed to make_plot_component",
|
|
101
|
+
DeprecationWarning,
|
|
102
|
+
stacklevel=2,
|
|
103
|
+
)
|
|
104
|
+
return make_mpl_plot_component(*args, **kwargs)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def make_mpl_plot_component(
|
|
108
|
+
measure: str | dict[str, str] | list[str] | tuple[str],
|
|
109
|
+
post_process: Callable | None = None,
|
|
110
|
+
save_format="png",
|
|
111
|
+
):
|
|
112
|
+
"""Create a plotting function for a specified measure.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
measure (str | dict[str, str] | list[str] | tuple[str]): Measure(s) to plot.
|
|
116
|
+
post_process: a user-specified callable to do post-processing called with the Axes instance.
|
|
117
|
+
save_format: save format of figure in solara backend
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
function: A function that creates a PlotMatplotlib component.
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
def MakePlotMatplotlib(model):
|
|
124
|
+
return PlotMatplotlib(
|
|
125
|
+
model, measure, post_process=post_process, save_format=save_format
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
return MakePlotMatplotlib
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@solara.component
|
|
132
|
+
def PlotMatplotlib(
|
|
133
|
+
model,
|
|
134
|
+
measure,
|
|
135
|
+
dependencies: list[any] | None = None,
|
|
136
|
+
post_process: Callable | None = None,
|
|
137
|
+
save_format="png",
|
|
138
|
+
):
|
|
139
|
+
"""Create a Matplotlib-based plot for a measure or measures.
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
model (mesa.Model): The model instance.
|
|
143
|
+
measure (str | dict[str, str] | list[str] | tuple[str]): Measure(s) to plot.
|
|
144
|
+
dependencies (list[any] | None): Optional dependencies for the plot.
|
|
145
|
+
post_process: a user-specified callable to do post-processing called with the Axes instance.
|
|
146
|
+
save_format: format used for saving the figure.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
solara.FigureMatplotlib: A component for rendering the plot.
|
|
150
|
+
"""
|
|
151
|
+
update_counter.get()
|
|
152
|
+
fig = Figure()
|
|
153
|
+
ax = fig.subplots()
|
|
154
|
+
df = model.datacollector.get_model_vars_dataframe()
|
|
155
|
+
if isinstance(measure, str):
|
|
156
|
+
ax.plot(df.loc[:, measure])
|
|
157
|
+
ax.set_ylabel(measure)
|
|
158
|
+
elif isinstance(measure, dict):
|
|
159
|
+
for m, color in measure.items():
|
|
160
|
+
ax.plot(df.loc[:, m], label=m, color=color)
|
|
161
|
+
ax.legend(loc="best")
|
|
162
|
+
elif isinstance(measure, list | tuple):
|
|
163
|
+
for m in measure:
|
|
164
|
+
ax.plot(df.loc[:, m], label=m)
|
|
165
|
+
ax.legend(loc="best")
|
|
166
|
+
|
|
167
|
+
if post_process is not None:
|
|
168
|
+
post_process(ax)
|
|
169
|
+
|
|
170
|
+
ax.set_xlabel("Step")
|
|
171
|
+
# Set integer x axis
|
|
172
|
+
ax.xaxis.set_major_locator(plt.MaxNLocator(integer=True))
|
|
173
|
+
solara.FigureMatplotlib(
|
|
174
|
+
fig, format=save_format, bbox_inches="tight", dependencies=dependencies
|
|
175
|
+
)
|