Skip to content

Commit

Permalink
refactor: Move default grid drawer to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored and tpike3 committed Oct 15, 2023
1 parent 477778c commit 44ab8cd
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions mesa/experimental/jupyter_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,23 @@ def change_handler(value, name=name):


def make_space(model, agent_portrayal):
space_fig = Figure()
space_ax = space_fig.subplots()
space = getattr(model, "grid", None)
if space is None:
# Sometimes the space is defined as model.space instead of model.grid
space = model.space
if isinstance(space, mesa.space.NetworkGrid):
_draw_network_grid(space, space_ax, agent_portrayal)
elif isinstance(space, mesa.space.ContinuousSpace):
_draw_continuous_space(space, space_ax, agent_portrayal)
else:
_draw_grid(space, space_ax, agent_portrayal)
space_ax.set_axis_off()
solara.FigureMatplotlib(space_fig, format="png")


def _draw_grid(space, space_ax, agent_portrayal):
def portray(g):
x = []
y = []
Expand Down Expand Up @@ -263,20 +280,7 @@ def portray(g):
out["c"] = c
return out

space_fig = Figure()
space_ax = space_fig.subplots()
space = getattr(model, "grid", None)
if space is None:
# Sometimes the space is defined as model.space instead of model.grid
space = model.space
if isinstance(space, mesa.space.NetworkGrid):
_draw_network_grid(space, space_ax, agent_portrayal)
elif isinstance(space, mesa.space.ContinuousSpace):
_draw_continuous_space(space, space_ax, agent_portrayal)
else:
space_ax.scatter(**portray(space))
space_ax.set_axis_off()
solara.FigureMatplotlib(space_fig, format="png")
space_ax.scatter(**portray(space))


def _draw_network_grid(space, space_ax, agent_portrayal):
Expand Down

0 comments on commit 44ab8cd

Please sign in to comment.