Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
fc382c6
added space_renderer, space_drawers and updated the backend solara_vi…
Sahil-Chhoker Jun 4, 2025
75d490d
Merge branch 'projectmesa:main' into space-renderer
Sahil-Chhoker Jun 5, 2025
98e2774
fix imports
Sahil-Chhoker Jun 5, 2025
3b12483
Merge branches 'space-renderer' and 'space-renderer' of https://githu…
Sahil-Chhoker Jun 5, 2025
2efaf2f
fix spelling
Sahil-Chhoker Jun 5, 2025
d010624
fix smaller issues
Sahil-Chhoker Jun 5, 2025
822dd25
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 5, 2025
41709cf
minor bug-fixes
Sahil-Chhoker Jun 5, 2025
5636b26
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 5, 2025
b4e78be
added render function for altair
Sahil-Chhoker Jun 6, 2025
4cfc043
added customizability using kwargs
Sahil-Chhoker Jun 6, 2025
3807395
added altair network drawing
Sahil-Chhoker Jun 8, 2025
789a05c
fix network related bugs
Sahil-Chhoker Jun 8, 2025
4a9163a
fix space drawer import
Sahil-Chhoker Jun 8, 2025
850444c
fix network indexing bug
Sahil-Chhoker Jun 8, 2025
1533f20
cleanup code
Sahil-Chhoker Jun 8, 2025
62ecea7
add precommit suggestions
Sahil-Chhoker Jun 8, 2025
45be52a
unified space drawers
Sahil-Chhoker Jun 9, 2025
1b8f44b
simplified ranges for altair agent plotting
Sahil-Chhoker Jun 9, 2025
f0655de
Overhauled the VoronioGrid and added its altair implementation
Sahil-Chhoker Jun 9, 2025
0dd29d3
Added image marker support for matplotlib and refactored the names of…
Sahil-Chhoker Jun 9, 2025
6f76f4f
added basic continuous space viz in altair
Sahil-Chhoker Jun 10, 2025
3d343a6
cleaned up the property layer drawing code
Sahil-Chhoker Jun 10, 2025
c148c5b
shift to renderer.canvas from renderer.ax in solara
Sahil-Chhoker Jun 13, 2025
0c24411
further decoupling:
Sahil-Chhoker Jun 13, 2025
ec61d4b
added basic altair rendering
Sahil-Chhoker Jun 13, 2025
b89311f
added configurable chart width and height
Sahil-Chhoker Jun 15, 2025
05fb2e5
updated docstring and imports
Sahil-Chhoker Jun 15, 2025
fdf4e00
added ax through kwargs in both backends
Sahil-Chhoker Jun 15, 2025
fd06cf5
redefined the ax usage, canvas and render management and uses, added …
Sahil-Chhoker Jun 15, 2025
a76a148
make SpaceRenderer a optional argument
Sahil-Chhoker Jun 20, 2025
3c7f1b5
added updated solara viz tests and fixed smaller bugs
Sahil-Chhoker Jun 20, 2025
b4e3eed
added coderabbit suggestion
Sahil-Chhoker Jun 20, 2025
e0e3106
added tests for space drawer classes
Sahil-Chhoker Jun 22, 2025
b59317c
added revisions
Sahil-Chhoker Jun 23, 2025
43157db
added tests for the SpaceRenderer and fixed a minor bug
Sahil-Chhoker Jun 24, 2025
fb6e797
added docs
Sahil-Chhoker Jun 27, 2025
04da76c
fix typo
Sahil-Chhoker Jun 27, 2025
3341b7a
Improved Docstrings
Sahil-Chhoker Jun 27, 2025
7662ea9
Improved docstring formatting
Sahil-Chhoker Jun 27, 2025
e335697
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 27, 2025
0e8bc73
change docstring formatting
Sahil-Chhoker Jun 27, 2025
caaf6e5
Merge branch 'space-renderer' of https://github.com/Sahil-Chhoker/mes…
Sahil-Chhoker Jun 27, 2025
c788045
resolving conflict
Sahil-Chhoker Jun 27, 2025
37ec6eb
added tests for both backends
Sahil-Chhoker Jun 28, 2025
7e4b917
Merge branch 'main' into space-renderer
Sahil-Chhoker Jun 28, 2025
e63c505
added null check if no renderer
Sahil-Chhoker Jun 28, 2025
ccfcd9b
Merge branch 'space-renderer' of https://github.com/Sahil-Chhoker/mes…
Sahil-Chhoker Jun 28, 2025
e037e65
added reviewed changes
Sahil-Chhoker Jun 29, 2025
17ca770
fix broken test
Sahil-Chhoker Jun 29, 2025
f12b732
centralized the canvas property and reintroduced post_process
Sahil-Chhoker Jun 30, 2025
3fcc371
remove bad test
Sahil-Chhoker Jun 30, 2025
84ee3cc
bug-fixes
Sahil-Chhoker Jul 3, 2025
f5723be
empty commit
Sahil-Chhoker Jul 3, 2025
27b76d0
bug fix part 2
Sahil-Chhoker Jul 3, 2025
3875deb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 3, 2025
20cd222
fix schelling markers
Sahil-Chhoker Jul 4, 2025
b7d1c1f
final fix for marker drawing logic
Sahil-Chhoker Jul 4, 2025
6fb787c
add pre-commit suggestions
Sahil-Chhoker Jul 4, 2025
d34fc83
made spacerenderer reactive
Sahil-Chhoker Jul 5, 2025
7e8e56c
remove realtivity from marker size
Sahil-Chhoker Jul 6, 2025
10f9ef9
fixed minor bugs
Sahil-Chhoker Jul 6, 2025
b33b1e1
fix null check
Sahil-Chhoker Jul 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion docs/apis/visualization.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,72 @@ For detailed tutorials, please refer to:
:show-inheritance:
```


## Command Console

```{eval-rst}
.. automodule:: mesa.visualization.command_console
:members:
:undoc-members:
:show-inheritance:
```
```


## Portrayal Components
```{eval-rst}
.. automodule:: mesa.visualization.components.portrayal_components
:members:
:undoc-members:
:show-inheritance:
```


## Backends

```{eval-rst}
.. automodule:: mesa.visualization.backends.__init__
:members:
:undoc-members:
:show-inheritance:
```

```{eval-rst}
.. automodule:: mesa.visualization.backends.abstract_renderer
:members:
:undoc-members:
:show-inheritance:
```

```{eval-rst}
.. automodule:: mesa.visualization.backends.altair_backend
:members:
:undoc-members:
:show-inheritance:
```

```{eval-rst}
.. automodule:: mesa.visualization.backends.matplotlib_backend
:members:
:undoc-members:
:show-inheritance:
```


## Space Renderer

```{eval-rst}
.. automodule:: mesa.visualization.space_renderer
:members:
:undoc-members:
:show-inheritance:
```


## Space Drawers

```{eval-rst}
.. automodule:: mesa.visualization.space_drawers
:members:
:undoc-members:
:show-inheritance:
```
2 changes: 2 additions & 0 deletions mesa/visualization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
from .components import make_plot_component, make_space_component
from .components.altair_components import make_space_altair
from .solara_viz import JupyterViz, SolaraViz
from .space_renderer import SpaceRenderer
from .user_param import Slider

__all__ = [
"CommandConsole",
"JupyterViz",
"Slider",
"SolaraViz",
"SpaceRenderer",
"draw_space",
"make_plot_component",
"make_space_altair",
Expand Down
23 changes: 23 additions & 0 deletions mesa/visualization/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Visualization backends for Mesa space rendering.

This module provides different backend implementations for visualizing
Mesa agent-based model spaces and components.

Note:
These backends are used internally by the space renderer and are not intended for
direct use by end users. See `SpaceRenderer` for actual usage and setting up
visualizations.

Available Backends:
1. AltairBackend
2. MatplotlibBackend

"""

from .altair_backend import AltairBackend
from .matplotlib_backend import MatplotlibBackend

__all__ = [
"AltairBackend",
"MatplotlibBackend",
]
97 changes: 97 additions & 0 deletions mesa/visualization/backends/abstract_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Abstract base class for visualization backends in Mesa.

This module provides the foundational interface for implementing various
visualization backends for Mesa agent-based models.
"""

from abc import ABC, abstractmethod

import mesa
from mesa.discrete_space import (
OrthogonalMooreGrid,
OrthogonalVonNeumannGrid,
)
from mesa.space import (
HexMultiGrid,
HexSingleGrid,
MultiGrid,
NetworkGrid,
SingleGrid,
)

OrthogonalGrid = SingleGrid | MultiGrid | OrthogonalMooreGrid | OrthogonalVonNeumannGrid
HexGrid = HexSingleGrid | HexMultiGrid | mesa.discrete_space.HexGrid
Network = NetworkGrid | mesa.discrete_space.Network


class AbstractRenderer(ABC):
"""Abstract base class for visualization backends.

This class defines the interface for rendering Mesa spaces and agents.
For details on the methods checkout specific backend implementations.
"""

def __init__(self, space_drawer):
"""Initialize the renderer.

Args:
space_drawer: Object responsible for drawing space elements. Checkout `SpaceDrawer`
for more details on the detailed implementations of the drawing functions.
"""
self.space_drawer = space_drawer
self._canvas = None

def _get_agent_pos(self, agent, space):
"""Get agent position based on space type."""
if isinstance(space, NetworkGrid):
return agent.pos, agent.pos
elif isinstance(space, Network):
return agent.cell.coordinate, agent.cell.coordinate
else:
x = agent.pos[0] if agent.pos is not None else agent.cell.coordinate[0]
y = agent.pos[1] if agent.pos is not None else agent.cell.coordinate[1]
return x, y

@abstractmethod
def initialize_canvas(self):
"""Set up the drawing canvas."""

@abstractmethod
def draw_structure(self, **kwargs):
"""Draw the space structure.

Args:
**kwargs: Structure drawing configuration options.
"""

@abstractmethod
def collect_agent_data(self, space, agent_portrayal, default_size=None):
"""Collect plotting data for all agents in the space.

Args:
space: The Mesa space containing agents.
agent_portrayal (Callable): Function that returns AgentPortrayalStyle for each agent.
default_size (float, optional): Default marker size if not specified in portrayal.

Returns:
dict: Dictionary containing agent plotting data arrays with keys:
"""

@abstractmethod
def draw_agents(self, arguments, **kwargs):
"""Drawing agents on space.

Args:
arguments (dict): Dictionary containing agent data.
**kwargs: Additional drawing configuration options.
"""

@abstractmethod
def draw_propertylayer(self, space, property_layers, propertylayer_portrayal):
"""Draw property layers on the visualization.

Args:
space: The model's space object.
property_layers (dict): Dictionary of property layers to visualize.
propertylayer_portrayal (Callable): Function that returns PropertyLayerStyle.
"""
Loading