Interactive visualization of Mesa simulations
Project description
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
Example: Visualizing a 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
Load the essential libraries as follows:
import solara
from mesa_interactive import slide, static
from mesa_interactive.components import create_chart, create_grid, create_markdown
from mesa_interactive.interactive import MesaInteractive
from mesa_models.schelling.model import Schelling
Defining Custom Functions
- Switch Agent Type: Create a function to toggle an agent's type when clicked on the grid.
def switch_agent_type(model, x, y):
agent = model.grid.get_cell_list_contents([(x, y)])[0]
agent.type = 1 - agent.type
Building Components
- GridView: Construct a grid view, enabling the
switch_agent_type
function on agent click.
GridView = create_grid("type", on_click=switch_agent_type)
- Instructions: Add Markdown instructions. These are static and don't rely on model parameters.
Instructions = static(solara.Markdown("Click on an agent to switch its 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("happy", "Happy Agents")
Configuring Model Parameters
Define user-adjustable model parameters using sliders:
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 and display all the components, simply call:
page
Now you have a fully interactive visualization for the Schelling Segregation model!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mesa_interactive-0.0.3.tar.gz
.
File metadata
- Download URL: mesa_interactive-0.0.3.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0781bbbc79098b796e4f07dea8365cc7cc7747d68798b7a76217c0b0fa89a5f7 |
|
MD5 | 189c0b1dbb9abeeacf95c993c4ffbdb4 |
|
BLAKE2b-256 | 4289d9763d54a77708278fe128aa1a8c44685dc350954fbc1ab03ba1acd4b7ac |
File details
Details for the file mesa_interactive-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: mesa_interactive-0.0.3-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e42613e3d41fc0ddcf31689a55194f69b8a263ea2ee441530c82fb11d089935 |
|
MD5 | e026547458a3076a8093a7337431d6ce |
|
BLAKE2b-256 | 977cd0199db39d1f19c0fd2cded9ac71a2ec193a95961dcd2ad373e94959965a |