Mesa Interactive
Overview
Mesa Interactive is a specialized Python package designed to provide dynamic visualization and interaction capabilities for Mesa models. It extends the functionalities of the Mesa framework for agent-based modeling.
Quick Installation
To quickly install the package, open your terminal and run:
pip install mesa_interactive
Tutorial: Visualizing the Schelling Segregation Model
This section provides a step-by-step walkthrough to visualize a Schelling Segregation model using Mesa Interactive.
Prerequisites
Ensure that you have mesa-models installed. If not, run the following command:
pip install -U -e git+https://github.com/projectmesa/mesa-examples#egg=mesa-models
Importing Dependencies
Import the essential libraries as follows:
import solara
from mesa_models.schelling.model import Schelling
from mesa_interactive import MesaInteractive, slide, static
from mesa_interactive.components import create_chart, create_grid, create_markdown
Building Components
- Instructions: Add Markdown instructions. We use the static helper function, since it doesn't rely on model parameters.
Instructions = static(solara.Markdown("Click on an agent to switch its type."))
- GridView: Construct a grid view, enabling a
switch_agent_typefunction on agent click.
def switch_agent_type(model, x, y):
agent = model.grid.get_cell_list_contents([(x, y)])[0]
agent.type = 1 - agent.type
GridView = create_grid(color="type", on_click=switch_agent_type)
- HappyCount: Display the number of satisfied agents via a Markdown component.
HappyCount = create_markdown(
lambda model: f"**Happy Agents: {model.happy} of {len(model.schedule.agents)}**"
)
- HappyChart: Implement a time-series chart to track the number of happy agents.
HappyChart = create_chart(variables=["happy"], "Happy Agents")
Configuring Model Parameters
Define user-adjustable model parameters using default values and the slide helper functions. It works like the built-in range function but supports arbitrary steps and a default value.
model_params = {
"width": 20,
"height": 20,
"density": slide(0, 1, 0.1, default=0.65),
"minority_pc": slide(0, 1, 0.05, default=0.2),
"homophily": slide(0, 8, 1, default=3),
}
Creating the Interactive Page
- Initialize the Mesa Interactive interface with the Schelling model, model parameters, and assembled components.
page = MesaInteractive(
Schelling,
model_params,
components=[HappyCount, Instructions, GridView, HappyChart],
name="Schelling",
show_dataframe="model",
)
- To render your visulazation save your python file as
app.pyand from a terminal run
solara run app.py
Alternatively you can but everything in a Jupyter notebook and simply call
page
to show the visualization.
Now you have built a fully interactive visualization for the Schelling Segregation model!
Metadata
Release files for mesa-interactive 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mesa_interactive-0.0.5.tar.gz | 9.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mesa_interactive-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.9 kB
Release files / mesa_interactive-0.0.5.tar.gz
| Download URL | mesa_interactive-0.0.5.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a06dc671e0c2912f15f271746741446b9ffc0d0d1bf8716e182efbba389f0ebe
|
|
BLAKE2b-256 checksum How to use checksums |
47bcaeb52eca36452dbab0829f786b88c81a0fc382fb0708626c40f5b9abbd4f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Release files / mesa_interactive-0.0.5-py3-none-any.whl
| Download URL | mesa_interactive-0.0.5-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a52bd1d9241b5e735f5222ccfeaa6d4769526b4f3debc36242b4373b27afe777
|
|
BLAKE2b-256 checksum How to use checksums |
a8d9c9653d5edf310bf5ae87dfd9f17dd9d15e2065273f3182a37adf67ea9ebd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|