Skip to content

Commit

Permalink
split handle_step function
Browse files Browse the repository at this point in the history
  • Loading branch information
Corvince committed Sep 22, 2023
1 parent 643b684 commit cd9184b
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions mesa/experimental/jupyter_viz.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import copy
from typing import Optional

import matplotlib.pyplot as plt
import networkx as nx
Expand Down Expand Up @@ -63,40 +62,38 @@ def make_model():
def handle_change_model_params(name: str, value: any):
set_model_parameters({**model_parameters, name: value})

def handle_step(step: Optional[int] = None):
"""Change the model to the next step, or to the specified step.
def do_step():
if not model.running:
return

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

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L67

Added line #L67 was not covered by tests

If step is specified, the model is cached at that step.
updated_model = copy.deepcopy(model)
updated_model.step()
set_model(updated_model)

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

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L69-L71

Added lines #L69 - L71 were not covered by tests

Args:
step: step to change the model to
"""
if not model.running:
def handle_step_timeline(step: int):
if step == model.schedule.steps:
return

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

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L75

Added line #L75 was not covered by tests

set_model_cache({**model_cache, model.schedule.steps: model})

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

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L77

Added line #L77 was not covered by tests

if step in model_cache:
updated_model = model_cache[step]
previous_model = model_cache[step]
set_model(previous_model)

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

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L80-L81

Added lines #L80 - L81 were not covered by tests
else:
updated_model = copy.deepcopy(model)
updated_model.step()

if step is not None:
set_model_cache({**model_cache, step: updated_model})

set_model(updated_model)
do_step()

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

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L83

Added line #L83 was not covered by tests

# 3. Set up UI
solara.Markdown(name)
UserInputs(user_params, on_change=handle_change_model_params)
TimelineControls(
play_interval=play_interval,
on_step=handle_step,
on_step=handle_step_timeline,
on_reset=make_model,
current_step=model.schedule.steps,
max_step=max(model_cache.keys()),
max_step=max(*model_cache.keys(), model.schedule.steps),
) if timeline else BaseControls(
play_interval=play_interval,
on_step=handle_step,
on_step=do_step,
on_reset=make_model,
)

Expand Down

0 comments on commit cd9184b

Please sign in to comment.