Skip to content

Commit fd9afb4

Browse files
committed
get_measurements_for_experiment
1 parent c75fa4f commit fd9afb4

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

petab/v2/problem.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,20 @@ def get_changes_for_period(
11401140
)
11411141
)
11421142

1143+
def get_measurements_for_experiment(
1144+
self, experiment: core.Experiment
1145+
) -> list[core.Measurement]:
1146+
"""Get the measurements for a given experiment.
1147+
1148+
:param experiment: The experiment to get the measurements for.
1149+
:return: A list of measurements for the given experiment.
1150+
"""
1151+
return [
1152+
measurement
1153+
for measurement in self.measurements
1154+
if measurement.experiment_id == experiment.id
1155+
]
1156+
11431157

11441158
class ModelFile(BaseModel):
11451159
"""A file in the PEtab problem configuration."""

tests/v2/test_problem.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,56 @@ def test_get_changes_for_period():
250250
)
251251
assert problem.get_changes_for_period(p1) == [ch1, ch2]
252252
assert problem.get_changes_for_period(p2) == [ch3]
253+
254+
255+
def test_get_measurements_for_experiment():
256+
"""Test getting measurements for an experiment."""
257+
problem = Problem()
258+
problem += Condition(
259+
id="condition1",
260+
changes=[Change(target_id="target1", target_value=1.0)],
261+
)
262+
problem += Condition(
263+
id="condition2",
264+
changes=[Change(target_id="target2", target_value=2.0)],
265+
)
266+
267+
e1 = Experiment(
268+
id="exp1",
269+
periods=[
270+
ExperimentPeriod(id="p1", time=0, condition_ids=["condition1"]),
271+
],
272+
)
273+
e2 = Experiment(
274+
id="exp2",
275+
periods=[
276+
ExperimentPeriod(id="p2", time=1, condition_ids=["condition2"]),
277+
],
278+
)
279+
problem += e1
280+
problem += e2
281+
282+
m1 = Measurement(
283+
observable_id="observable1",
284+
experiment_id="exp1",
285+
time=0,
286+
measurement=10.0,
287+
)
288+
m2 = Measurement(
289+
observable_id="observable2",
290+
experiment_id="exp1",
291+
time=1,
292+
measurement=20.0,
293+
)
294+
m3 = Measurement(
295+
observable_id="observable3",
296+
experiment_id="exp2",
297+
time=1,
298+
measurement=30.0,
299+
)
300+
problem += m1
301+
problem += m2
302+
problem += m3
303+
304+
assert problem.get_measurements_for_experiment(e1) == [m1, m2]
305+
assert problem.get_measurements_for_experiment(e2) == [m3]

0 commit comments

Comments
 (0)