Skip to content

Commit a49e40d

Browse files
authored
api reorganization (#2447)
1 parent 04354db commit a49e40d

File tree

12 files changed

+345
-195
lines changed

12 files changed

+345
-195
lines changed

docs/apis/visualization.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,26 @@ For a detailed tutorial, please refer to our [Visualization Tutorial](../tutoria
2121
```
2222

2323

24-
## Matplotlib-based components
24+
## Matplotlib-based visualizations
2525

2626
```{eval-rst}
27-
.. automodule:: mesa.visualization.components.matplotlib
27+
.. automodule:: mesa.visualization.components.matplotlib_components
2828
:members:
2929
:undoc-members:
3030
:show-inheritance:
3131
```
3232

33-
## Altair-based components
33+
```{eval-rst}
34+
.. automodule:: mesa.visualization.mpl_space_drawing
35+
:members:
36+
:undoc-members:
37+
:show-inheritance:
38+
```
39+
40+
## Altair-based visualizations
3441

3542
```{eval-rst}
36-
.. automodule:: mesa.visualization.components.altair
43+
.. automodule:: mesa.visualization.components.altair_components
3744
:members:
3845
:undoc-members:
3946
:show-inheritance:

mesa/examples/advanced/pd_grid/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"""
44

55
from mesa.examples.advanced.pd_grid.model import PdGrid
6-
from mesa.visualization import SolaraViz, make_plot_component, make_space_component
6+
from mesa.visualization import (
7+
SolaraViz,
8+
make_plot_component,
9+
make_space_component,
10+
)
711
from mesa.visualization.UserParam import Slider
812

913

mesa/examples/basic/boid_flockers/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def boid_draw(agent):
5151

5252
page = SolaraViz(
5353
model,
54-
[make_space_component(agent_portrayal=boid_draw)],
54+
[make_space_component(agent_portrayal=boid_draw, backend="matplotlib")],
5555
model_params=model_params,
5656
name="Boid Flocking Model",
5757
)

mesa/examples/basic/boltzmann_wealth_model/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from mesa.examples.basic.boltzmann_wealth_model.model import BoltzmannWealthModel
2-
from mesa.visualization import SolaraViz, make_plot_component, make_space_component
2+
from mesa.visualization import (
3+
SolaraViz,
4+
make_plot_component,
5+
make_space_component,
6+
)
37

48

59
def agent_portrayal(agent):

mesa/visualization/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""Solara based visualization for Mesa models."""
22

3-
from .components.altair import make_space_altair
4-
from .components.matplotlib import make_plot_component, make_space_component
3+
from mesa.visualization.mpl_space_drawing import (
4+
draw_space,
5+
)
6+
7+
from .components import make_plot_component, make_space_component
8+
from .components.altair_components import make_space_altair
59
from .solara_viz import JupyterViz, SolaraViz
610
from .UserParam import Slider
711

@@ -10,6 +14,7 @@
1014
"SolaraViz",
1115
"Slider",
1216
"make_space_altair",
13-
"make_space_component",
17+
"draw_space",
1418
"make_plot_component",
19+
"make_space_component",
1520
]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
)

mesa/visualization/components/altair.py renamed to mesa/visualization/components/altair_components.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Altair based solara components for visualization mesa spaces."""
22

33
import contextlib
4+
import warnings
45

56
import solara
67

@@ -12,7 +13,33 @@
1213
from mesa.visualization.utils import update_counter
1314

1415

15-
def make_space_altair(agent_portrayal=None): # noqa: D103
16+
def make_space_altair(*args, **kwargs): # noqa: D103
17+
warnings.warn(
18+
"make_space_altair has been renamed to make_altair_space",
19+
DeprecationWarning,
20+
stacklevel=2,
21+
)
22+
return make_altair_space(*args, **kwargs)
23+
24+
25+
def make_altair_space(
26+
agent_portrayal, propertylayer_portrayal, post_process, **space_drawing_kwargs
27+
):
28+
"""Create an Altair-based space visualization component.
29+
30+
Args:
31+
agent_portrayal: Function to portray agents.
32+
propertylayer_portrayal: not yet implemented
33+
post_process :not yet implemented
34+
space_drawing_kwargs : not yet implemented
35+
36+
``agent_portrayal`` is called with an agent and should return a dict. Valid fields in this dict are "color",
37+
"size", "marker", and "zorder". Other field are ignored and will result in a user warning.
38+
39+
40+
Returns:
41+
function: A function that creates a SpaceMatplotlib component
42+
"""
1643
if agent_portrayal is None:
1744

1845
def agent_portrayal(a):
@@ -25,7 +52,12 @@ def MakeSpaceAltair(model):
2552

2653

2754
@solara.component
28-
def SpaceAltair(model, agent_portrayal, dependencies: list[any] | None = None): # noqa: D103
55+
def SpaceAltair(model, agent_portrayal, dependencies: list[any] | None = None):
56+
"""Create an Altair-based space visualization component.
57+
58+
Returns:
59+
a solara FigureAltair instance
60+
"""
2961
update_counter.get()
3062
space = getattr(model, "grid", None)
3163
if space is None:

0 commit comments

Comments
 (0)