Skip to main content

Interactive visualization of Mesa simulations

Project description

Mesa Interactive

PyPi

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

  1. 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."))

  1. GridView: Construct a grid view, enabling a switch_agent_type function 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)
  1. 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)}**"
)
  1. 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

  1. 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",
)
  1. To render your visulazation save your python file as app.py and 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!

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

mesa_interactive-0.0.5.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

mesa_interactive-0.0.5-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file mesa_interactive-0.0.5.tar.gz.

File metadata

  • Download URL: mesa_interactive-0.0.5.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for mesa_interactive-0.0.5.tar.gz
Algorithm Hash digest
SHA256 a06dc671e0c2912f15f271746741446b9ffc0d0d1bf8716e182efbba389f0ebe
MD5 c86b241feea09ca48d7411c3c20f078a
BLAKE2b-256 47bcaeb52eca36452dbab0829f786b88c81a0fc382fb0708626c40f5b9abbd4f

See more details on using hashes here.

File details

Details for the file mesa_interactive-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for mesa_interactive-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a52bd1d9241b5e735f5222ccfeaa6d4769526b4f3debc36242b4373b27afe777
MD5 d6347026383e4faa584598ef721ac536
BLAKE2b-256 a8d9c9653d5edf310bf5ae87dfd9f17dd9d15e2065273f3182a37adf67ea9ebd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page