Skip to content

Commit 90a249d

Browse files
committed
Add Experiment.sorted_periods
Add Experiment.sorted_periods and extend tests.
1 parent d140230 commit 90a249d

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

petab/v2/core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ def has_preequilibration(self) -> bool:
597597
"""Check if the experiment has preequilibration enabled."""
598598
return any(period.is_preequilibration for period in self.periods)
599599

600+
@property
601+
def sorted_periods(self) -> list[ExperimentPeriod]:
602+
"""Get the periods of the experiment sorted by time."""
603+
return sorted(self.periods, key=lambda period: period.time)
604+
600605
def sort_periods(self) -> None:
601606
"""Sort the periods of the experiment by time."""
602607
self.periods.sort(key=lambda period: period.time)

tests/v2/test_core.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sympy.abc import x, y
1111

1212
import petab.v2 as petab
13-
from petab.v2 import Problem
13+
from petab.v2 import C, Problem
1414
from petab.v2.C import (
1515
CONDITION_ID,
1616
ESTIMATE,
@@ -255,18 +255,36 @@ def test_parameter():
255255

256256
def test_experiment():
257257
Experiment(id="experiment1")
258-
Experiment(
259-
id="experiment1", periods=[ExperimentPeriod(time=1, condition_id="c1")]
260-
)
261258

259+
# extra fields allowed
262260
assert Experiment(id="experiment1", non_petab=1).non_petab == 1
263261

262+
# ID required
264263
with pytest.raises(ValidationError, match="Field required"):
265264
Experiment()
266265

266+
# valid ID required
267267
with pytest.raises(ValidationError, match="Invalid ID"):
268268
Experiment(id="experiment 1")
269269

270+
periods = [
271+
ExperimentPeriod(time=C.TIME_PREEQUILIBRATION, condition_ids=["c1"]),
272+
ExperimentPeriod(time=-1, condition_id="c1"),
273+
ExperimentPeriod(time=1, condition_id="c1"),
274+
]
275+
e = Experiment(id="experiment1", periods=list(reversed(periods)))
276+
277+
assert e.has_preequilibration is True
278+
279+
assert e.sorted_periods == periods
280+
assert e.periods != periods
281+
282+
e.sort_periods()
283+
assert e.periods == periods
284+
285+
e.periods.pop(0)
286+
assert e.has_preequilibration is False
287+
270288

271289
def test_condition_table():
272290
assert ConditionTable().free_symbols == set()

0 commit comments

Comments
 (0)