Skip to content

Commit c75fa4f

Browse files
committed
Add v2.Problem.get_changes_for_period
Easier access to changes associated with a given period.
1 parent 11db39d commit c75fa4f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

petab/v2/problem.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,20 @@ def model_dump(self, **kwargs) -> dict[str, Any]:
11261126
)
11271127
return res
11281128

1129+
def get_changes_for_period(
1130+
self, period: core.ExperimentPeriod
1131+
) -> list[core.Change]:
1132+
"""Get the changes for a given experiment period.
1133+
1134+
:param period: The experiment period to get the changes for.
1135+
:return: A list of changes for the given period.
1136+
"""
1137+
return list(
1138+
chain.from_iterable(
1139+
self[condition].changes for condition in period.condition_ids
1140+
)
1141+
)
1142+
11291143

11301144
class ModelFile(BaseModel):
11311145
"""A file in the PEtab problem configuration."""

tests/v2/test_problem.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,28 @@ def test_problem_config_paths():
225225
# pc.parameter_files[0] = "foo.tsv"
226226
# assert isinstance(pc.parameter_files[0], Path)
227227
# see also https://github.com/pydantic/pydantic/issues/8575
228+
229+
230+
def test_get_changes_for_period():
231+
"""Test getting changes for a specific period."""
232+
problem = Problem()
233+
ch1 = Change(target_id="target1", target_value=1.0)
234+
ch2 = Change(target_id="target2", target_value=2.0)
235+
ch3 = Change(target_id="target3", target_value=3.0)
236+
cond1 = Condition(id="condition1_1", changes=[ch1])
237+
cond2 = Condition(id="condition1_2", changes=[ch2])
238+
cond3 = Condition(id="condition2", changes=[ch3])
239+
problem += cond1
240+
problem += cond2
241+
problem += cond3
242+
243+
p1 = ExperimentPeriod(
244+
id="p1", time=0, condition_ids=["condition1_1", "condition1_2"]
245+
)
246+
p2 = ExperimentPeriod(id="p2", time=1, condition_ids=["condition2"])
247+
problem += Experiment(
248+
id="exp1",
249+
periods=[p1, p2],
250+
)
251+
assert problem.get_changes_for_period(p1) == [ch1, ch2]
252+
assert problem.get_changes_for_period(p2) == [ch3]

0 commit comments

Comments
 (0)