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 committed Oct 7, 2023
1 parent 576698f commit f63f9e9
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)

Check warning on line 240 in mesa/experimental/jupyter_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L238-L240

Added lines #L238 - L240 were not covered by tests
if space is None:
# Sometimes the space is defined as model.space instead of model.grid
space = model.space

Check warning on line 243 in mesa/experimental/jupyter_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L243

Added line #L243 was not covered by tests
if isinstance(space, mesa.space.NetworkGrid):
_draw_network_grid(space, space_ax, agent_portrayal)

Check warning on line 245 in mesa/experimental/jupyter_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L245

Added line #L245 was not covered by tests
elif isinstance(space, mesa.space.ContinuousSpace):
_draw_continuous_space(space, space_ax, agent_portrayal)

Check warning on line 247 in mesa/experimental/jupyter_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L247

Added line #L247 was not covered by tests
else:
_draw_grid(space, space_ax, agent_portrayal)
space_ax.set_axis_off()
solara.FigureMatplotlib(space_fig, format="png")

Check warning on line 251 in mesa/experimental/jupyter_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L249-L251

Added lines #L249 - L251 were not covered by tests


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))

Check warning on line 283 in mesa/experimental/jupyter_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L283

Added line #L283 was not covered by tests


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

0 comments on commit f63f9e9

Please sign in to comment.