2525
2626import asyncio
2727import copy
28+ import warnings
2829from collections .abc import Callable
2930from typing import TYPE_CHECKING , Literal
3031
3132import reacton .core
3233import solara
3334
34- import mesa .visualization .components .altair as components_altair
35+ from mesa .visualization .components .altair import SpaceAltair
36+ from mesa .visualization .components .matplotlib import PlotMatplotlib , SpaceMatplotlib
3537from mesa .visualization .UserParam import Slider
3638from mesa .visualization .utils import force_update , update_counter
3739
3840if TYPE_CHECKING :
3941 from mesa .model import Model
4042
4143
44+ def make_space_matplotlib (* args , ** kwargs ): # noqa: D103
45+ warnings .warn (
46+ "make_space_matplotlib has been renamed to make_space_component" ,
47+ DeprecationWarning ,
48+ stacklevel = 2 ,
49+ )
50+ return make_space_component (* args , ** kwargs )
51+
52+
53+ def make_space_altair (agent_portrayal = None ): # noqa: D103
54+ warnings .warn (
55+ "make_space_altair has been renamed to make_space_component with backend='altair'" ,
56+ DeprecationWarning ,
57+ stacklevel = 2 ,
58+ )
59+ if agent_portrayal is None :
60+
61+ def agent_portrayal (a ):
62+ return {"id" : a .unique_id }
63+
64+ def MakeSpaceAltair (model ):
65+ return SpaceAltair (model , agent_portrayal )
66+
67+ return MakeSpaceAltair
68+
69+
70+ def make_space_component (
71+ agent_portrayal : Callable | None = None ,
72+ propertylayer_portrayal : dict | None = None ,
73+ post_process : Callable | None = None ,
74+ backend : Literal ["matplotlib" , "altair" ] = "matplotlib" ,
75+ ** space_drawing_kwargs ,
76+ ):
77+ """Create a Matplotlib-based space visualization component.
78+
79+ Args:
80+ agent_portrayal: Function to portray agents.
81+ propertylayer_portrayal: Dictionary of PropertyLayer portrayal specifications
82+ post_process : a callable that will be called with the Axes instance. Allows for fine tuning plots (e.g., control ticks)
83+ backend: The backend to use for rendering the space. Can be "matplotlib" or "altair".
84+ space_drawing_kwargs : additional keyword arguments to be passed on to the underlying space drawer function. See
85+ the functions for drawing the various spaces for further details.
86+
87+ ``agent_portrayal`` is called with an agent and should return a dict. Valid fields in this dict are "color",
88+ "size", "marker", and "zorder". Other field are ignored and will result in a user warning.
89+
90+
91+ Returns:
92+ function: A function that creates a SpaceMatplotlib component
93+ """
94+ if agent_portrayal is None :
95+
96+ def agent_portrayal (a ):
97+ return {}
98+
99+ def MakeSpaceMatplotlib (model ):
100+ return SpaceMatplotlib (
101+ model ,
102+ agent_portrayal ,
103+ propertylayer_portrayal ,
104+ post_process = post_process ,
105+ ** space_drawing_kwargs ,
106+ )
107+
108+ def MakeSpaceAltair (model ):
109+ return SpaceAltair (model , agent_portrayal )
110+
111+ match backend :
112+ case "matplotlib" :
113+ return MakeSpaceMatplotlib
114+ case "altair" :
115+ return MakeSpaceAltair
116+ case _:
117+ raise ValueError (f"Invalid backend: { backend } " )
118+
119+
120+ def make_plot_measure (* args , ** kwargs ): # noqa: D103
121+ warnings .warn (
122+ "make_plot_measure has been renamed to make_plot_component" ,
123+ DeprecationWarning ,
124+ stacklevel = 2 ,
125+ )
126+ return make_plot_component (* args , ** kwargs )
127+
128+
129+ def make_plot_component (
130+ measure : str | dict [str , str ] | list [str ] | tuple [str ],
131+ post_process : Callable | None = None ,
132+ save_format = "png" ,
133+ ):
134+ """Create a plotting function for a specified measure.
135+
136+ Args:
137+ measure (str | dict[str, str] | list[str] | tuple[str]): Measure(s) to plot.
138+ post_process: a user-specified callable to do post-processing called with the Axes instance.
139+ save_format: save format of figure in solara backend
140+
141+ Returns:
142+ function: A function that creates a PlotMatplotlib component.
143+ """
144+
145+ def MakePlotMatplotlib (model ):
146+ return PlotMatplotlib (
147+ model , measure , post_process = post_process , save_format = save_format
148+ )
149+
150+ return MakePlotMatplotlib
151+
152+
42153@solara .component
43154def SolaraViz (
44155 model : Model | solara .Reactive [Model ],
@@ -63,7 +174,7 @@ def SolaraViz(
63174 components (list[solara.component] | Literal["default"], optional): List of solara
64175 components or functions that return a solara component.
65176 These components are used to render different parts of the model visualization.
66- Defaults to "default", which uses the default Altair space visualization.
177+ Defaults to "default", which uses the default Matplotlib space visualization.
67178 play_interval (int, optional): Interval for playing the model steps in milliseconds.
68179 This controls the speed of the model's automatic stepping. Defaults to 100 ms.
69180 model_params (dict, optional): Parameters for (re-)instantiating a model.
@@ -87,7 +198,7 @@ def SolaraViz(
87198 value results in faster stepping, while a higher value results in slower stepping.
88199 """
89200 if components == "default" :
90- components = [components_altair . make_space_altair ()]
201+ components = [make_space_component ()]
91202
92203 # Convert model to reactive
93204 if not isinstance (model , solara .Reactive ):
0 commit comments