Skip to content

Commit 469cebd

Browse files
authored
enable run_for to take a string (#679)
1 parent 7405410 commit 469cebd

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
**3.6.1 - 11/13/25**
2+
3+
- Enable setting simulation duration as a string in InteractiveContext.run_for()
4+
15
**3.6.0 - 10/27/25**
26

37
- Move simulate profile command to vivarium_profiling package

src/vivarium/interface/interactive.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,16 @@ def run(self, with_logging: bool = True) -> None: # type: ignore [override]
8282
"""
8383
self.run_until(self._clock.stop_time, with_logging=with_logging)
8484

85-
def run_for(self, duration: ClockStepSize, with_logging: bool = True) -> None:
85+
def run_for(self, duration: ClockStepSize | str, with_logging: bool = True) -> None:
8686
"""Run the simulation for the given time duration.
8787
8888
Parameters
8989
----------
9090
duration
9191
The length of time to run the simulation for. Should be compatible
9292
with the simulation clock's step size (usually a pandas
93-
Timedelta).
93+
Timedelta). If a string is provided, it will be passed to
94+
`pandas.Timedelta` to be converted.
9495
with_logging
9596
Whether or not to log the simulation steps. Only works in an ipython
9697
environment.
@@ -99,6 +100,8 @@ def run_for(self, duration: ClockStepSize, with_logging: bool = True) -> None:
99100
-------
100101
The number of steps the simulation took.
101102
"""
103+
if isinstance(duration, str):
104+
duration = pd.Timedelta(duration)
102105
self.run_until(self._clock.time + duration, with_logging=with_logging) # type: ignore [operator]
103106

104107
def run_until(self, end_time: ClockTime, with_logging: bool = True) -> None:

tests/interface/test_interactive.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pandas as pd
12
import pytest
23

34
from vivarium import InteractiveContext
@@ -13,3 +14,14 @@ def test_list_values() -> None:
1314
sim.get_value("foo")
1415
# ensure that 'foo' did not get added to the list of values
1516
assert sim.list_values() == ["simulant_step_size"]
17+
18+
19+
def test_run_for_duration() -> None:
20+
sim = InteractiveContext()
21+
initial_time = sim._clock.time
22+
23+
sim.run_for(pd.Timedelta("10 days"))
24+
assert sim._clock.time == initial_time + pd.Timedelta("10 days") # type: ignore[operator]
25+
26+
sim.run_for("5 days")
27+
assert sim._clock.time == initial_time + pd.Timedelta("15 days") # type: ignore[operator]

0 commit comments

Comments
 (0)