Skip to content

Commit ded4c49

Browse files
Updating Examples and Docs inline with the new API. (#2819)
* Added updated basic examples * fix schelling agent_portrayal * fixed marker import logic * update boltzman wealth model * update remaining basic examples * remove unwanted parameter * fix network agents error * fix word repetetion * added sys paths * Revert "added sys paths" * remove sys paths * updated advanced examples * remove unnecessary docstring * fix alpha not working in matplotlib error * added ordering functionality to altair * Revert "added ordering functionality to altair" * updated first tutorial * updated second tutorial * added 3rd visualization tutorial * updated 3rd example * added reviewed changes * added property layer tutorial * updated getting_started.md * updated getting started last part * empty commit * added post_process test and updated example doc * updated test
1 parent f1231bf commit ded4c49

File tree

21 files changed

+1334
-164
lines changed

21 files changed

+1334
-164
lines changed

docs/getting_started.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ If you want to get a quick start on how to build agent based models with MESA, c
1313
- [AgentSet](tutorials/3_agentset): Learn how to more effectively manage agents with Mesa's AgentSet.
1414
- [Basic Visualization](tutorials/4_visualization_basic): Learn how to build an interactive dashboard with Mesa's visualization module.
1515
- [Dynamic Agent Visualization](tutorials/5_visualization_dynamic_agents): Learn how to dynamically represent your agents in your interactive dashboard.
16-
- [Custom Visualization Components](tutorials/6_visualization_custom): Learn how to add custom visual components to your interactive dashboard.
17-
- [Parameter Sweeps](tutorials/7_batch_run): Learn how to conduct parameter sweeps on multiple processors with Mesa's BatchRunner.
18-
- [Comparing Scenarios](tutorials/8_comparing_scenarios): Think through how to analyze your parameter sweep results to find insight in your Mesa simulations.
16+
- [Visualization using SpaceRenderer](tutorials/6_visualization_rendering_with_space_renderer.ipynb): Learn how to use SpaceRenderer to its full extent to enhance your visualizations.
17+
- [Property Layer Visualization](tutorials/7_visualization_propertylayer_visualization.ipynb): Learn how to visualize property layers in mesa.
18+
- [Custom Visualization Components](tutorials/8_visualization_custom): Learn how to add custom visual components to your interactive dashboard.
19+
- [Parameter Sweeps](tutorials/9_batch_run): Learn how to conduct parameter sweeps on multiple processors with Mesa's BatchRunner.
20+
- [Comparing Scenarios](tutorials/10_comparing_scenarios): Think through how to analyze your parameter sweep results to find insight in your Mesa simulations.
1921

2022
## Examples
2123
Mesa ships with a collection of example models. These are classic ABMs, so if you are familiar with ABMs and want to get a quick sense of how MESA works, these examples are great place to start. You can find them [here](examples).
@@ -60,9 +62,11 @@ Collecting Data <tutorials/2_collecting_data>
6062
AgentSet <tutorials/3_agentset>
6163
Basic Visualization <tutorials/4_visualization_basic>
6264
Dynamic Agent Visualization <tutorials/5_visualization_dynamic_agents>
63-
Custom Visualization Components <tutorials/6_visualization_custom>
64-
Parameter Sweeps <tutorials/7_batch_run>
65-
Comparing Scenarios <tutorials/8_comparing_scenarios>
65+
Visualisation using SpaceRenderer <tutorials/6_visualization_rendering_with_space_renderer>
66+
Property Layer Visualization <tutorials/7_visualization_propertylayer_visualization>
67+
Custom Visualization Components <tutorials/8_visualization_custom>
68+
Parameter Sweeps <tutorials/9_batch_run>
69+
Comparing Scenarios <tutorials/10_comparing_scenarios>
6670
Best Practices <best-practices>
6771
6872
File renamed without changes.

docs/tutorials/4_visualization_basic.ipynb

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
"\n",
6161
"import mesa\n",
6262
"from mesa.discrete_space import CellAgent, OrthogonalMooreGrid\n",
63-
"from mesa.visualization import SolaraViz, make_plot_component, make_space_component"
63+
"from mesa.visualization import SolaraViz, SpaceRenderer, make_plot_component\n",
64+
"from mesa.visualization.components import AgentPortrayalStyle"
6465
]
6566
},
6667
{
@@ -187,7 +188,11 @@
187188
"source": [
188189
"#### Grid Visualization\n",
189190
"\n",
190-
"Mesa's grid visualizer works by looping over every cell in a grid, and generating a portrayal for every agent it finds. A portrayal is a dictionary (which can easily be turned into a JSON object) which tells Matplotlib the color and size of the scatterplot markers (each signifying an agent). The only thing we need to provide is a function which takes an agent, and returns a portrayal dictionary. Here's the simplest one: it'll draw each agent as a blue, filled circle, with a radius size of 50."
191+
"Mesa’s grid visualizer works by iterating over each cell in the grid and generating a portrayal for every agent it finds. The portrayal function is called for each agent and returns an `AgentPortrayalStyle`—an object that defines how the agent is visually represented.\n",
192+
"\n",
193+
"All you need to provide is a function that takes an agent as input and returns an `AgentPortrayalStyle`.\n",
194+
"\n",
195+
"Here's the simplest example: it draws each agent as a filled orange circle with a radius of 50."
191196
]
192197
},
193198
{
@@ -197,10 +202,7 @@
197202
"outputs": [],
198203
"source": [
199204
"def agent_portrayal(agent):\n",
200-
" return {\n",
201-
" \"color\": \"tab:blue\",\n",
202-
" \"size\": 50,\n",
203-
" }"
205+
" return AgentPortrayalStyle(color=\"tab:orange\", size=50)"
204206
]
205207
},
206208
{
@@ -236,7 +238,7 @@
236238
"source": [
237239
"Next, we instantiate the visualization object which (by default) displays the grid containing the agents, and timeseries of values computed by the model's data collector. In this example, we specify the Gini coefficient.\n",
238240
"\n",
239-
"There are 3 buttons:\n",
241+
"There are 3 main buttons (we will discuss the play interval, render interval and use threads in lesson 6):\n",
240242
"- the step button, which advances the model by 1 step\n",
241243
"- the play button, which advances the model indefinitely until it is paused\n",
242244
"- the pause button, which pauses the model\n",
@@ -247,6 +249,26 @@
247249
"3. Press reset "
248250
]
249251
},
252+
{
253+
"cell_type": "markdown",
254+
"metadata": {},
255+
"source": [
256+
"##### SpaceRenderer\n",
257+
"This is the Python object used to visualize the grid, agents, and property layers associated with the space and the model and is passed to the `SolaraViz`\n",
258+
"\n",
259+
"We initialize the SpaceRenderer with a model instance (e.g., `money_model` in this case) and specify the rendering backend. The available backends are `matplotlib`(default) and `altair`.\n",
260+
"\n",
261+
"Both backends can be extended using the post_process function, which can either be passed directly to the render() method or set as a property of the renderer. (We’ll cover this in more detail later.)\n",
262+
"\n",
263+
"The method shown here is the quickest way to set up the visualization using the `render()` function.\n",
264+
"\n",
265+
"It renders the space, agents, and property layers—provided that the corresponding portrayal functions are supplied for both agents and property layers.\n",
266+
"\n",
267+
"> **Note:**\n",
268+
"> \n",
269+
"> You can make the small window full screen by clicking the button in the top-right corner of the title bar."
270+
]
271+
},
250272
{
251273
"cell_type": "code",
252274
"execution_count": null,
@@ -256,12 +278,16 @@
256278
"# Create initial model instance\n",
257279
"money_model = MoneyModel(n=50, width=10, height=10) # keyword arguments\n",
258280
"\n",
259-
"SpaceGraph = make_space_component(agent_portrayal)\n",
281+
"renderer = SpaceRenderer(model=money_model, backend=\"matplotlib\").render(\n",
282+
" agent_portrayal=agent_portrayal\n",
283+
")\n",
284+
"\n",
260285
"GiniPlot = make_plot_component(\"Gini\")\n",
261286
"\n",
262287
"page = SolaraViz(\n",
263288
" money_model,\n",
264-
" components=[SpaceGraph, GiniPlot],\n",
289+
" renderer,\n",
290+
" components=[GiniPlot],\n",
265291
" model_params=model_params,\n",
266292
" name=\"Boltzmann Wealth Model\",\n",
267293
")\n",
@@ -291,7 +317,7 @@
291317
"metadata": {
292318
"anaconda-cloud": {},
293319
"kernelspec": {
294-
"display_name": "Python 3 (ipykernel)",
320+
"display_name": "env",
295321
"language": "python",
296322
"name": "python3"
297323
},
@@ -305,7 +331,7 @@
305331
"name": "python",
306332
"nbconvert_exporter": "python",
307333
"pygments_lexer": "ipython3",
308-
"version": "3.12.5"
334+
"version": "3.13.1"
309335
},
310336
"widgets": {
311337
"state": {},

docs/tutorials/5_visualization_dynamic_agents.ipynb

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"metadata": {},
1515
"source": [
1616
"If you want to get straight to the tutorial checkout these environment providers:<br>\n",
17-
"[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/projectmesa/mesa/main?labpath=docs%2Ftutorials%2F4_visualization_basic.ipynb) (This can take 30 seconds to 5 minutes to load)\n",
17+
"[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/projectmesa/mesa/main?labpath=docs%2Ftutorials%2F5_visualization_dynamic_agents.ipynb) (This can take 30 seconds to 5 minutes to load)\n",
1818
"\n",
1919
"Due to conflict with Colab and Solara there are no colab links for this tutorial\n",
2020
"\n",
@@ -60,7 +60,8 @@
6060
"\n",
6161
"import mesa\n",
6262
"from mesa.discrete_space import CellAgent, OrthogonalMooreGrid\n",
63-
"from mesa.visualization import SolaraViz, make_plot_component, make_space_component"
63+
"from mesa.visualization import SolaraViz, SpaceRenderer, make_plot_component\n",
64+
"from mesa.visualization.components import AgentPortrayalStyle"
6465
]
6566
},
6667
{
@@ -189,9 +190,18 @@
189190
"\n",
190191
"In the first visualization, all we could see is the agents moving around -- but not how much money they had, or anything else of interest. In this tutorial let's change it so that agents are represented by the units of wealth they have. So those who are broke (wealth 0) are drawn in red, smaller. \n",
191192
"\n",
192-
"As Mesa is open source, it is important to point out that currently, we can't direct the drawing order of the circles, so a broke agent may be overshadowed by a wealthy agent. If you have some ideas, please feel free to [contribute](https://github.com/projectmesa/mesa/blob/main/CONTRIBUTING.md). \n",
193+
"Since Mesa is open source, if you have ideas to improve the visualization stack, feel free to [contribute](https://github.com/projectmesa/mesa/blob/main/CONTRIBUTING.md).\n",
194+
"\n",
195+
"When using the default drawer, an agent's shape can be customized in addition to its size and color.\n",
196+
"\n",
197+
"* For `matplotlib`, allowed shape values can be found [here](https://matplotlib.org/stable/api/markers_api.html).\n",
198+
"* For `altair`, supported shapes include: `\"circle\"`, `\"square\"`, `\"cross\"`, `\"diamond\"`, `\"triangle-up\"`, `\"triangle-down\"`, `\"triangle-right\"`, and `\"triangle-left\"`.\n",
199+
"\n",
200+
"**Note:**\n",
201+
"Always check which backend is being used before assigning shapes. If a shape name doesn't match what's supported by the selected backend, it may cause errors.\n",
202+
"\n",
203+
"In some cases, Mesa implicitly converts common shape names between Altair and Matplotlib (e.g., from Altair to Matplotlib), but this mapping is limited—and there's no support for conversion in the reverse direction (Matplotlib to Altair).\n",
193204
"\n",
194-
"In addition to size and color, an agent's shape can also be customized when using the default drawer. The allowed values for shapes can be found [here](https://matplotlib.org/stable/api/markers_api.html).\n",
195205
"\n",
196206
"To do this, we go back to our `agent_portrayal` code and add some code to change the portrayal based on the agent properties and launch the server again."
197207
]
@@ -203,12 +213,10 @@
203213
"outputs": [],
204214
"source": [
205215
"def agent_portrayal(agent):\n",
206-
" size = 10\n",
207-
" color = \"tab:red\"\n",
216+
" portrayal = AgentPortrayalStyle(size=50, color=\"tab:orange\")\n",
208217
" if agent.wealth > 0:\n",
209-
" size = 50\n",
210-
" color = \"tab:blue\"\n",
211-
" return {\"size\": size, \"color\": color}"
218+
" portrayal.update((\"color\", \"tab:blue\"), (\"size\", 100))\n",
219+
" return portrayal"
212220
]
213221
},
214222
{
@@ -244,7 +252,7 @@
244252
"source": [
245253
"Then just like last time we instantiate the visualization object which (by default) displays the grid containing the agents, and timeseries of values computed by the model's data collector. In this example, we specify the Gini coefficient.\n",
246254
"\n",
247-
"There are 3 buttons:\n",
255+
"There are 3 main buttons (we will discuss the play interval, render interval and use threads in lesson 6):\n",
248256
"- the step button, which advances the model by 1 step\n",
249257
"- the play button, which advances the model indefinitely until it is paused\n",
250258
"- the pause button, which pauses the model\n",
@@ -263,13 +271,16 @@
263271
"source": [
264272
"# Create initial model instance\n",
265273
"money_model = MoneyModel(n=50, width=10, height=10)\n",
274+
"renderer = SpaceRenderer(model=money_model, backend=\"matplotlib\").render(\n",
275+
" agent_portrayal=agent_portrayal\n",
276+
")\n",
266277
"\n",
267-
"SpaceGraph = make_space_component(agent_portrayal)\n",
268278
"GiniPlot = make_plot_component(\"Gini\")\n",
269279
"\n",
270280
"page = SolaraViz(\n",
271281
" money_model,\n",
272-
" components=[SpaceGraph, GiniPlot],\n",
282+
" renderer,\n",
283+
" components=[GiniPlot],\n",
273284
" model_params=model_params,\n",
274285
" name=\"Boltzmann Wealth Model\",\n",
275286
")\n",
@@ -292,7 +303,7 @@
292303
"source": [
293304
"## Next Steps\n",
294305
"\n",
295-
"Check out the next [visualization tutorial custom components](https://mesa.readthedocs.io/latest/tutorials/6_visualization_custom.html) on how to further enhance your interactive dashboard."
306+
"Check out the next tutorial [visualization rendering with `SpaceRenderer`](https://mesa.readthedocs.io/latest/tutorials/6_visualization_rendering_with_space_renderer.html) on how to further enhance your interactive dashboard."
296307
]
297308
},
298309
{
@@ -308,7 +319,7 @@
308319
"metadata": {
309320
"anaconda-cloud": {},
310321
"kernelspec": {
311-
"display_name": "Python 3 (ipykernel)",
322+
"display_name": "env",
312323
"language": "python",
313324
"name": "python3"
314325
},
@@ -322,7 +333,7 @@
322333
"name": "python",
323334
"nbconvert_exporter": "python",
324335
"pygments_lexer": "ipython3",
325-
"version": "3.12.5"
336+
"version": "3.13.1"
326337
},
327338
"widgets": {
328339
"state": {},

0 commit comments

Comments
 (0)