Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Altair spaces #2397

Merged
merged 56 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
d90b0f7
further updates
quaquel Jan 21, 2024
9586490
Update benchmarks/WolfSheep/__init__.py
quaquel Jan 21, 2024
4aaa35d
Merge remote-tracking branch 'upstream/main'
quaquel Feb 21, 2024
d31478c
Merge remote-tracking branch 'upstream/main'
quaquel Apr 22, 2024
6e4c72e
Merge remote-tracking branch 'upstream/main'
quaquel Aug 14, 2024
70fbaf5
Merge remote-tracking branch 'upstream/main'
quaquel Aug 18, 2024
724c8db
Merge remote-tracking branch 'upstream/main'
quaquel Aug 21, 2024
45184a4
Merge remote-tracking branch 'upstream/main'
quaquel Aug 22, 2024
3d75d30
Merge remote-tracking branch 'upstream/main'
quaquel Aug 27, 2024
2759244
Update __init__.py
quaquel Aug 27, 2024
fc8aaea
Merge remote-tracking branch 'upstream/main'
quaquel Aug 28, 2024
1ba465d
Merge remote-tracking branch 'upstream/main'
quaquel Aug 30, 2024
2b5e822
Merge remote-tracking branch 'upstream/main'
quaquel Sep 2, 2024
3847799
Merge remote-tracking branch 'upstream/main'
quaquel Sep 4, 2024
301d80e
Merge remote-tracking branch 'upstream/main'
quaquel Sep 4, 2024
fe3d655
Merge remote-tracking branch 'upstream/main'
quaquel Sep 4, 2024
7d18880
Merge remote-tracking branch 'upstream/main'
quaquel Sep 5, 2024
6b49a3b
Merge remote-tracking branch 'upstream/main'
quaquel Sep 5, 2024
b9909e6
Merge remote-tracking branch 'upstream/main'
quaquel Sep 9, 2024
8ce3d83
Merge remote-tracking branch 'upstream/main'
quaquel Sep 10, 2024
88fbf74
Merge remote-tracking branch 'upstream/main'
quaquel Sep 13, 2024
077aca5
Merge remote-tracking branch 'upstream/main'
quaquel Sep 16, 2024
c771e3b
Merge remote-tracking branch 'upstream/main'
quaquel Sep 18, 2024
3fa0c27
Merge remote-tracking branch 'upstream/main'
quaquel Sep 18, 2024
8fdcf5c
Merge remote-tracking branch 'upstream/main'
quaquel Sep 21, 2024
475b060
Merge remote-tracking branch 'upstream/main'
quaquel Sep 21, 2024
fb93261
Merge remote-tracking branch 'upstream/main'
quaquel Sep 25, 2024
86f2f0c
Merge remote-tracking branch 'upstream/main'
quaquel Sep 25, 2024
dc1d843
Merge remote-tracking branch 'upstream/main'
quaquel Sep 25, 2024
9857ef1
Merge remote-tracking branch 'upstream/main'
quaquel Sep 25, 2024
afdd836
Merge remote-tracking branch 'upstream/main'
quaquel Sep 29, 2024
46c3d13
Merge remote-tracking branch 'upstream/main'
quaquel Sep 30, 2024
a5205eb
Merge remote-tracking branch 'upstream/main'
quaquel Oct 7, 2024
59a0413
Merge remote-tracking branch 'upstream/main'
quaquel Oct 12, 2024
56f00af
Merge remote-tracking branch 'upstream/main'
quaquel Oct 12, 2024
135c978
Merge remote-tracking branch 'upstream/main'
quaquel Oct 14, 2024
bd4ca35
Merge remote-tracking branch 'upstream/main'
quaquel Oct 15, 2024
5a69aee
Merge remote-tracking branch 'upstream/main'
quaquel Oct 15, 2024
98c4616
Merge remote-tracking branch 'upstream/main'
quaquel Oct 16, 2024
2aa5326
Merge remote-tracking branch 'upstream/main'
quaquel Oct 16, 2024
91e6c0f
Merge remote-tracking branch 'upstream/main'
quaquel Oct 16, 2024
5976962
Merge remote-tracking branch 'upstream/main'
quaquel Oct 17, 2024
95da9b7
Merge remote-tracking branch 'upstream/main'
quaquel Oct 17, 2024
66b70eb
Merge remote-tracking branch 'upstream/main'
quaquel Oct 18, 2024
7022bff
Merge remote-tracking branch 'upstream/main'
quaquel Oct 18, 2024
e39e01c
Merge remote-tracking branch 'upstream/main'
quaquel Oct 21, 2024
d246d82
Merge remote-tracking branch 'upstream/main'
quaquel Oct 21, 2024
2c5d6f2
add support for new style grids and old style continuous space
quaquel Oct 21, 2024
d7cf0ef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 21, 2024
7f1b2be
fix typo in docstring
quaquel Oct 21, 2024
42ef16a
update datatype encoding for x and y
quaquel Oct 21, 2024
46f8d44
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 21, 2024
cab46ab
fixes for continous space visualization
quaquel Oct 21, 2024
3dfa996
typo in function name
quaquel Oct 21, 2024
71de07c
Update altair.py
quaquel Oct 21, 2024
2a49ee0
ruff fix
quaquel Oct 21, 2024
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
106 changes: 87 additions & 19 deletions mesa/visualization/components/altair.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
with contextlib.suppress(ImportError):
import altair as alt

from mesa.experimental.cell_space import DiscreteSpace, Grid
from mesa.space import ContinuousSpace, _Grid
from mesa.visualization.utils import update_counter


Expand All @@ -29,35 +31,101 @@ def SpaceAltair(model, agent_portrayal, dependencies: list[any] | None = None):
if space is None:
# Sometimes the space is defined as model.space instead of model.grid
space = model.space

chart = _draw_grid(space, agent_portrayal)
solara.FigureAltair(chart)


def _get_agent_data_old__discrete_space(space, agent_portrayal):
"""Format agent portrayal data for old-style discrete spaces.

Args:
space: the mesa.space._Grid instance
agent_portrayal: the agent portrayal callable

Returns:
list of dicts

"""
all_agent_data = []
for content, (x, y) in space.coord_iter():
if not content:
continue
if not hasattr(content, "__iter__"):
# Is a single grid
content = [content] # noqa: PLW2901
for agent in content:
# use all data from agent portrayal, and add x,y coordinates
agent_data = agent_portrayal(agent)
agent_data["x"] = x
agent_data["y"] = y
all_agent_data.append(agent_data)
return all_agent_data


def _get_agent_data_new_discrete_space(space: DiscreteSpace, agent_portrayal):
"""Format agent portrayal data for new-style discrete spaces.

Args:
space: the mesa.experiment.cell_space.Grid instance
agent_portrayal: the agent portrayal callable

Returns:
list of dicts

"""
all_agent_data = []

for cell in space.all_cells:
for agent in cell.agents:
agent_data = agent_portrayal(agent)
agent_data["x"] = cell.coordinate[0]
agent_data["y"] = cell.coordinate[1]
all_agent_data.append(agent_data)
return all_agent_data


def _get_agent_data_continuous_space(space: ContinuousSpace, agent_portrayal):
"""Format agent portrayal data for continuous space.

Args:
space: the ContinuousSpace instance
agent_portrayal: the agent portrayal callable

Returns:
list of dicts
"""
all_agent_data = []
for agent in space._agent_to_index:
agent_data = agent_portrayal(agent)
agent_data["x"] = agent.pos[0]
agent_data["y"] = agent.pos[1]
all_agent_data.append(agent_data)
return all_agent_data


def _draw_grid(space, agent_portrayal):
def portray(g):
all_agent_data = []
for content, (x, y) in g.coord_iter():
if not content:
continue
if not hasattr(content, "__iter__"):
# Is a single grid
content = [content] # noqa: PLW2901
for agent in content:
# use all data from agent portrayal, and add x,y coordinates
agent_data = agent_portrayal(agent)
agent_data["x"] = x
agent_data["y"] = y
all_agent_data.append(agent_data)
return all_agent_data

all_agent_data = portray(space)
match space:
case Grid():
all_agent_data = _get_agent_data_new_discrete_space(space, agent_portrayal)
case _Grid():
all_agent_data = _get_agent_data_old__discrete_space(space, agent_portrayal)
case ContinuousSpace():
all_agent_data = _get_agent_data_continuous_space(space, agent_portrayal)
case _:
raise NotImplementedError(
f"visualizing {type(space)} is currently not supported through altair"
)

invalid_tooltips = ["color", "size", "x", "y"]

x_y_type = "ordinal" if not isinstance(space, ContinuousSpace) else "nominal"

encoding_dict = {
# no x-axis label
"x": alt.X("x", axis=None, type="ordinal"),
"x": alt.X("x", axis=None, type=x_y_type),
# no y-axis label
"y": alt.Y("y", axis=None, type="ordinal"),
"y": alt.Y("y", axis=None, type=x_y_type),
"tooltip": [
alt.Tooltip(key, type=alt.utils.infer_vegalite_type([value]))
for key, value in all_agent_data[0].items()
Expand Down
Loading