Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions petab/v2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,11 @@ def has_preequilibration(self) -> bool:
"""Check if the experiment has preequilibration enabled."""
return any(period.is_preequilibration for period in self.periods)

@property
def sorted_periods(self) -> list[ExperimentPeriod]:
"""Get the periods of the experiment sorted by time."""
return sorted(self.periods, key=lambda period: period.time)

def sort_periods(self) -> None:
"""Sort the periods of the experiment by time."""
self.periods.sort(key=lambda period: period.time)
Expand Down
26 changes: 22 additions & 4 deletions tests/v2/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sympy.abc import x, y

import petab.v2 as petab
from petab.v2 import Problem
from petab.v2 import C, Problem
from petab.v2.C import (
CONDITION_ID,
ESTIMATE,
Expand Down Expand Up @@ -256,18 +256,36 @@ def test_parameter():

def test_experiment():
Experiment(id="experiment1")
Experiment(
id="experiment1", periods=[ExperimentPeriod(time=1, condition_id="c1")]
)

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

# ID required
with pytest.raises(ValidationError, match="Field required"):
Experiment()

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

periods = [
ExperimentPeriod(time=C.TIME_PREEQUILIBRATION, condition_ids=["c1"]),
ExperimentPeriod(time=-1, condition_id="c1"),
ExperimentPeriod(time=1, condition_id="c1"),
]
e = Experiment(id="experiment1", periods=list(reversed(periods)))

assert e.has_preequilibration is True

assert e.sorted_periods == periods
assert e.periods != periods

e.sort_periods()
assert e.periods == periods

e.periods.pop(0)
assert e.has_preequilibration is False


def test_condition_table():
assert ConditionTable().free_symbols == set()
Expand Down
Loading