Skip to content

Commit 24fdc16

Browse files
tpike3pre-commit-ci[bot]quaquel
authored
Tutorial Improvements (#2415)
* update_tutorial envirnment - Adds a Binder link in intro for non-google account holders - Removes Colab link for visual as Solara does not render properly - Adds py.cafe link for visualization tutorial - Adds binder link for visualization tutorial --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jan Kwakkel <[email protected]>
1 parent a41914e commit 24fdc16

File tree

3 files changed

+53
-76
lines changed

3 files changed

+53
-76
lines changed

docs/index.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,25 @@ To install our latest stable release (2.4.x), run:
4444
pip install -U mesa
4545
```
4646

47-
To install our latest pre-release (3.0 alpha), run:
47+
Mesa >= 3.0 beta includes several installations options:
4848

4949
``` bash
50-
pip install -U --pre mesa
50+
pip install -U --pre mesa[rec]
5151
```
52+
**mesa[rec]** (recommend install) installs - mesa, [networkx](https://networkx.org/), [matplotlib](https://matplotlib.org/stable/install/index.html)
53+
and [solara](https://solara.dev/)
5254

53-
To launch an example model, clone the [repository](https://github.com/projectmesa/mesa) folder and invoke `mesa runserver` for one of the `examples/` subdirectories:
55+
### Other installation options include:
5456

55-
```bash
56-
mesa runserver examples/wolf_sheep
57-
```
57+
- **mesa[network]** installs mesa and [networkx](https://networkx.org/)
58+
- **mesa[viz]** installs, mesa, [matplotlib](https://matplotlib.org/stable/install/index.html) and [solara](https://solara.dev/)
59+
- **mesa[dev]** installs mesa[rec], [ruff](https://docs.astral.sh/ruff/), [pytest](https://docs.pytest.org/en/stable/), [pytest-cov](https://pytest-cov.readthedocs.io/en/latest/), [sphinx](https://www.sphinx-doc.org/en/master/man/sphinx-build.html#cmdoption-sphinx-build-e), [pytest-mock](https://pytest-mock.readthedocs.io/en/latest/), [cookiecutter](https://cookiecutter.readthedocs.io/en/stable/)
60+
- **mesa[examples]** installs mesa[rec], [pytest](https://docs.pytest.org/en/stable/), and [scipy](https://scipy.org/)
61+
- **mesa[docs]** installs mesa[rec], [sphinx](https://www.sphinx-doc.org/en/master/man/sphinx-build.html#cmdoption-sphinx-build-e), [ipython](https://ipython.readthedocs.io/en/stable/install/kernel_install.html), [pydata_sphinx_theme](https://pydata-sphinx-theme.readthedocs.io/en/stable/),
62+
[seaborn](https://seaborn.pydata.org/), [myst-nb](https://myst-nb.readthedocs.io/en/latest/), [myst-parser](https://myst-parser.readthedocs.io/en/latest/)
63+
64+
Then mesa[all] installs all these sub installation options:
65+
**mesa[all]** = mesa[network,viz,dev,examples,docs]
5866

5967
For more help on using Mesa, check out the following resources:
6068

docs/tutorials/intro_tutorial.ipynb

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@
8282
"Install Mesa:\n",
8383
"\n",
8484
"```bash\n",
85-
"pip install --upgrade mesa\n",
85+
"pip install --upgrade mesa[rec] \n",
8686
"```\n",
8787
"\n",
8888
"If you want to use our newest features, you can also opt to install our latest pre-release version:\n",
8989
"\n",
9090
"```bash\n",
91-
"pip install --upgrade --pre mesa\n",
91+
"pip install --upgrade --pre mesa[rec]\n",
9292
"```\n",
9393
"\n",
9494
"Install Jupyter notebook (optional):\n",
@@ -115,10 +115,10 @@
115115
]
116116
},
117117
{
118-
"cell_type": "markdown",
118+
"cell_type": "raw",
119119
"metadata": {},
120120
"source": [
121-
"pip install --quiet --upgrade --pre mesa #installs Mesa 3.0"
121+
"pip install --quiet --upgrade --pre mesa[rec] #installs Mesa 3.0"
122122
]
123123
},
124124
{
@@ -706,18 +706,14 @@
706706
" self.model.grid.move_agent(self, new_position)\n",
707707
"\n",
708708
" def give_money(self):\n",
709-
" cellmates = self.model.grid.get_cell_list_contents([self.pos])\n",
710-
" if len(cellmates) > 1:\n",
711-
" other_agent = self.random.choice(cellmates)\n",
712-
" other_agent.wealth += 1\n",
713-
" self.wealth -= 1\n",
714-
"\n",
715-
" def step(self):\n",
716-
" self.move()\n",
717-
" if self.wealth > 0:\n",
718-
" self.give_money()\n",
719-
"\n",
709+
" if self.wealth > 0: \n",
710+
" cellmates = self.model.grid.get_cell_list_contents([self.pos])\n",
711+
" if len(cellmates) > 1:\n",
712+
" other_agent = self.random.choice(cellmates)\n",
713+
" other_agent.wealth += 1\n",
714+
" self.wealth -= 1\n",
720715
"\n",
716+
" \n",
721717
"class MoneyModel(mesa.Model):\n",
722718
" \"\"\"A model with some number of agents.\"\"\"\n",
723719
"\n",
@@ -735,7 +731,8 @@
735731
" self.grid.place_agent(a, (x, y))\n",
736732
"\n",
737733
" def step(self):\n",
738-
" self.agents.shuffle_do(\"step\")"
734+
" self.agents.shuffle_do(\"move\")\n",
735+
" self.agents.shuffle_do(\"give_money\")"
739736
]
740737
},
741738
{
@@ -858,7 +855,8 @@
858855
" if other == self:\n",
859856
" print(\"I JUST GAVE MOnEY TO MYSELF HEHEHE!\")\n",
860857
"\n",
861-
" def step(self):\n",
858+
" # There are several ways in which one can combine functions to execute the model\n",
859+
" def agent_act(self): \n",
862860
" self.move()\n",
863861
" if self.wealth > 0:\n",
864862
" self.give_money()\n",
@@ -870,7 +868,12 @@
870868
" def __init__(self, n, width, height):\n",
871869
" super().__init__()\n",
872870
" self.num_agents = n\n",
871+
" # create the space\n",
873872
" self.grid = mesa.space.MultiGrid(width, height, True)\n",
873+
" # collect the output\n",
874+
" self.datacollector = mesa.DataCollector(\n",
875+
" model_reporters={\"Gini\": compute_gini}, agent_reporters={\"Wealth\": \"wealth\"}\n",
876+
" )\n",
874877
"\n",
875878
" # Create agents\n",
876879
" for _ in range(self.num_agents):\n",
@@ -880,13 +883,10 @@
880883
" y = self.random.randrange(self.grid.height)\n",
881884
" self.grid.place_agent(a, (x, y))\n",
882885
"\n",
883-
" self.datacollector = mesa.DataCollector(\n",
884-
" model_reporters={\"Gini\": compute_gini}, agent_reporters={\"Wealth\": \"wealth\"}\n",
885-
" )\n",
886-
"\n",
886+
" \n",
887887
" def step(self):\n",
888888
" self.datacollector.collect(self)\n",
889-
" self.agents.shuffle_do(\"step\")"
889+
" self.agents.shuffle_do(\"agent_act\")"
890890
]
891891
},
892892
{
@@ -998,7 +998,7 @@
998998
"metadata": {},
999999
"outputs": [],
10001000
"source": [
1001-
"# Get the wealth of agent 14 over time\n",
1001+
"# Get the wealth of agent 7 over time\n",
10021002
"one_agent_wealth = agent_wealth.xs(7, level=\"AgentID\")\n",
10031003
"\n",
10041004
"# Plot the wealth of agent 7 over time\n",
@@ -1336,7 +1336,7 @@
13361336
" def step(self):\n",
13371337
" self.datacollector.collect(self)\n",
13381338
" # groupby returns a dictionary of the different ethnicities with a list of agents\n",
1339-
" grouped_agents = grouped_agents = model.agents.groupby(\"ethnicity\")\n",
1339+
" grouped_agents = model.agents.groupby(\"ethnicity\")\n",
13401340
"\n",
13411341
" for ethnic, similars in grouped_agents: \n",
13421342
" if ethnic != \"Mixed\": \n",
@@ -1445,7 +1445,7 @@
14451445
"\n",
14461446
" def give_money(self):\n",
14471447
" cellmates = self.model.grid.get_cell_list_contents([self.pos])\n",
1448-
" if len(cellmates) > 1:\n",
1448+
" if len(cellmates) > 1 and self.wealth > 0:\n",
14491449
" other = self.random.choice(cellmates)\n",
14501450
" other.wealth += 1\n",
14511451
" self.wealth -= 1\n",
@@ -1513,7 +1513,7 @@
15131513
"results = mesa.batch_run(\n",
15141514
" MoneyModel,\n",
15151515
" parameters=params,\n",
1516-
" iterations=7,\n",
1516+
" iterations=5,\n",
15171517
" max_steps=100,\n",
15181518
" number_processes=1,\n",
15191519
" data_collection_period=1,\n",
@@ -1687,8 +1687,8 @@
16871687
"results_5s = mesa.batch_run(\n",
16881688
" MoneyModel,\n",
16891689
" parameters=params,\n",
1690-
" iterations=100,\n",
1691-
" max_steps=120,\n",
1690+
" iterations=25,\n",
1691+
" max_steps=100,\n",
16921692
" number_processes=1,\n",
16931693
" data_collection_period=1, # Important, otherwise the datacollector will only collect data of the last time step\n",
16941694
" display_progress=True,\n",

docs/tutorials/visualization_tutorial.ipynb

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"*This version of the visualisation tutorial is updated for Mesa 3.0, and works with Mesa `3.0.0a4` and above. If you are using Mesa 2.3.x, check out the [stable version](https://mesa.readthedocs.io/stable/tutorials/visualization_tutorial.html) of this tutorial on Readthedocs.*\n",
1515
"\n",
1616
"**Important:** \n",
17-
"- If you are just exploring Mesa and want the fastest way to the the dashboard and code checkout [![py.cafe](https://img.shields.io/badge/launch-py.cafe-blue)](https://py.cafe/app/maartenbreddels/mesa-example-wealth) (click \"Editor\" to see the code)\n",
18-
"- If you want to see the dashboard in an interactive notebook try [[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/projectmesa/mesa/main?labpath=docs%2Ftutorials%2Fvisualization_tutorial.ipynb)\n",
17+
"- If you are just exploring Mesa and want the fastest way to the the dashboard and code checkout [![py.cafe](https://img.shields.io/badge/launch-py.cafe-blue)](https://py.cafe/app/tpike3/boltzmann-wealth-model) (click \"Editor\" to see the code)\n",
18+
"- If you want to see the dashboard in an interactive notebook try [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/projectmesa/mesa/main?labpath=docs%2Ftutorials%2Fvisualization_tutorial.ipynb)\n",
1919
"- If you have installed mesa and are running locally, please ensure that your [Mesa version](https://pypi.org/project/Mesa/) is up-to-date in order to run this tutorial.\n",
2020
"\n",
2121
"### Adding visualization\n",
@@ -256,7 +256,10 @@
256256
"source": [
257257
"import mesa\n",
258258
"print(f\"Mesa version: {mesa.__version__}\")\n",
259+
"import solara\n",
260+
"from matplotlib.figure import Figure\n",
259261
"\n",
262+
"from mesa.visualization.utils import update_counter\n",
260263
"from mesa.visualization import SolaraViz, make_plot_measure, make_space_matplotlib\n",
261264
"# Import the local MoneyModel.py\n",
262265
"from MoneyModel import MoneyModel\n"
@@ -291,18 +294,10 @@
291294
]
292295
},
293296
{
294-
"cell_type": "code",
295-
"execution_count": null,
297+
"cell_type": "markdown",
296298
"metadata": {},
297-
"outputs": [],
298299
"source": [
299-
"from mesa.visualization import SolaraViz, make_plot_measure, make_space_matplotlib\n",
300-
"\n",
301-
"# Create initial model instance\n",
302-
"model1 = MoneyModel(50, 10, 10)\n",
303-
"\n",
304-
"SpaceGraph = make_space_matplotlib(agent_portrayal)\n",
305-
"GiniPlot = make_plot_measure(\"Gini\")"
300+
"Next, we update our solara frontend to use this new component"
306301
]
307302
},
308303
{
@@ -311,10 +306,6 @@
311306
"metadata": {},
312307
"outputs": [],
313308
"source": [
314-
"import solara\n",
315-
"from matplotlib.figure import Figure\n",
316-
"from mesa.visualization.utils import update_counter\n",
317-
"\n",
318309
"@solara.component\n",
319310
"def Histogram(model):\n",
320311
" update_counter.get() # This is required to update the counter\n",
@@ -335,33 +326,11 @@
335326
"metadata": {},
336327
"outputs": [],
337328
"source": [
338-
"def agent_portrayal(agent):\n",
339-
" size = 10\n",
340-
" color = \"tab:red\"\n",
341-
" if agent.wealth > 0:\n",
342-
" size = 50\n",
343-
" color = \"tab:blue\"\n",
344-
" return {\"size\": size, \"color\": color}\n",
329+
"# Create initial model instance\n",
330+
"model1 = MoneyModel(50, 10, 10)\n",
345331
"\n",
346-
"model_params = {\n",
347-
" \"n\": {\n",
348-
" \"type\": \"SliderInt\",\n",
349-
" \"value\": 50,\n",
350-
" \"label\": \"Number of agents:\",\n",
351-
" \"min\": 10,\n",
352-
" \"max\": 100,\n",
353-
" \"step\": 1,\n",
354-
" },\n",
355-
" \"width\": 10,\n",
356-
" \"height\": 10,\n",
357-
"}"
358-
]
359-
},
360-
{
361-
"cell_type": "markdown",
362-
"metadata": {},
363-
"source": [
364-
"Next, we update our solara frontend to use this new component"
332+
"SpaceGraph = make_space_matplotlib(agent_portrayal)\n",
333+
"GiniPlot = make_plot_measure(\"Gini\")"
365334
]
366335
},
367336
{

0 commit comments

Comments
 (0)