Skip to content

Commit 9f14006

Browse files
committed
Add v2.Problem.get_changes_for_period
Easier access to changes associated with a given period.
1 parent f9bad6a commit 9f14006

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
@@ -1120,6 +1120,20 @@ def model_dump(self, **kwargs) -> dict[str, Any]:
11201120
)
11211121
return res
11221122

1123+
def get_changes_for_period(
1124+
self, period: core.ExperimentPeriod
1125+
) -> list[core.Change]:
1126+
"""Get the changes for a given experiment period.
1127+
1128+
:param period: The experiment period to get the changes for.
1129+
:return: A list of changes for the given period.
1130+
"""
1131+
return list(
1132+
chain.from_iterable(
1133+
self[condition].changes for condition in period.condition_ids
1134+
)
1135+
)
1136+
11231137

11241138
class ModelFile(BaseModel):
11251139
"""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
@@ -198,3 +198,28 @@ def test_sample_startpoint_shape():
198198
n_starts = 10
199199
sp = problem.sample_parameter_startpoints(n_starts=n_starts)
200200
assert sp.shape == (n_starts, 2)
201+
202+
203+
def test_get_changes_for_period():
204+
"""Test getting changes for a specific period."""
205+
problem = Problem()
206+
ch1 = Change(target_id="target1", target_value=1.0)
207+
ch2 = Change(target_id="target2", target_value=2.0)
208+
ch3 = Change(target_id="target3", target_value=3.0)
209+
cond1 = Condition(id="condition1_1", changes=[ch1])
210+
cond2 = Condition(id="condition1_2", changes=[ch2])
211+
cond3 = Condition(id="condition2", changes=[ch3])
212+
problem += cond1
213+
problem += cond2
214+
problem += cond3
215+
216+
p1 = ExperimentPeriod(
217+
id="p1", time=0, condition_ids=["condition1_1", "condition1_2"]
218+
)
219+
p2 = ExperimentPeriod(id="p2", time=1, condition_ids=["condition2"])
220+
problem += Experiment(
221+
id="exp1",
222+
periods=[p1, p2],
223+
)
224+
assert problem.get_changes_for_period(p1) == [ch1, ch2]
225+
assert problem.get_changes_for_period(p2) == [ch3]

0 commit comments

Comments
 (0)