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

Draw space(#2638) #2657

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions binder/environment.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: example-environment
name: mesa-tutorials
channels:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep PRs focussed, this is a separate issue and thus does not belong in this PR. I suggest reverting all changes to this file

- conda-forge
dependencies:
Expand All @@ -11,4 +11,4 @@ dependencies:
- matplotlib
- seaborn
- solara
- mesa[rec]==3.0.0b1
- mesa[rec]
36 changes: 22 additions & 14 deletions mesa/visualization/mpl_space_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def draw_space(
space,
agent_portrayal: Callable,
propertylayer_portrayal: dict | None = None,
ax: Axes | None = None,
ax_grid: Axes | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced that this is the ideal solution for an improved API. You now have to pass the axes multiple times in the most common use case. I suggest we hatch out the API design in #2640 first before opening a PR.

ax_properties: Axes | None = None,
ax_agents: Axes | None = None,
**space_drawing_kwargs,
):
"""Draw a Matplotlib-based visualization of the space.
Expand All @@ -127,37 +129,43 @@ def draw_space(
"size", "marker", "zorder", alpha, linewidths, and edgecolors. Other field are ignored and will result in a user warning.

"""
if ax is None:
fig, ax = plt.subplots()
if ax_grid is None:
fig, ax_grid = plt.subplots()

# https://stackoverflow.com/questions/67524641/convert-multiple-isinstance-checks-to-structural-pattern-matching
match space:
# order matters here given the class structure of old-style grid spaces
case HexSingleGrid() | HexMultiGrid() | mesa.experimental.cell_space.HexGrid():
draw_hex_grid(space, agent_portrayal, ax=ax, **space_drawing_kwargs)
draw_hex_grid(space, agent_portrayal, ax=ax_grid, **space_drawing_kwargs)
case (
mesa.space.SingleGrid()
| OrthogonalMooreGrid()
| OrthogonalVonNeumannGrid()
| mesa.space.MultiGrid()
):
draw_orthogonal_grid(space, agent_portrayal, ax=ax, **space_drawing_kwargs)
draw_orthogonal_grid(
space, agent_portrayal, ax=ax_grid, **space_drawing_kwargs
)
case mesa.space.NetworkGrid() | mesa.experimental.cell_space.Network():
draw_network(space, agent_portrayal, ax=ax, **space_drawing_kwargs)
case (
mesa.space.ContinuousSpace()
| mesa.experimental.continuous_space.ContinuousSpace()
):
draw_continuous_space(space, agent_portrayal, ax=ax)
draw_network(space, agent_portrayal, ax=ax_grid, **space_drawing_kwargs)
case mesa.space.ContinuousSpace():
draw_continuous_space(space, agent_portrayal, ax=ax_grid)
case VoronoiGrid():
draw_voronoi_grid(space, agent_portrayal, ax=ax)
draw_voronoi_grid(space, agent_portrayal, ax=ax_grid)
case _:
raise ValueError(f"Unknown space type: {type(space)}")

if propertylayer_portrayal:
draw_property_layers(space, propertylayer_portrayal, ax=ax)
if ax_properties is None:
ax_properties = ax_grid
draw_property_layers(space, propertylayer_portrayal, ax=ax_properties)

return ax
if hasattr(space, "agents") and space.agents:
if ax_agents is None:
ax_agents = ax_grid
draw_agents(space, agent_portrayal, ax=ax_agents)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good name for a new method. But I don't see an implementation of draw_agents at the moment?


return ax_grid


@lru_cache(maxsize=1024, typed=True)
Expand Down
Loading