Skip to content

Commit fa20cf3

Browse files
committed
get_measurements_for_experiment
1 parent 9f14006 commit fa20cf3

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
@@ -1134,6 +1134,20 @@ def get_changes_for_period(
11341134
)
11351135
)
11361136

1137+
def get_measurements_for_experiment(
1138+
self, experiment: core.Experiment
1139+
) -> list[core.Measurement]:
1140+
"""Get the measurements for a given experiment.
1141+
1142+
:param experiment: The experiment to get the measurements for.
1143+
:return: A list of measurements for the given experiment.
1144+
"""
1145+
return [
1146+
measurement
1147+
for measurement in self.measurements
1148+
if measurement.experiment_id == experiment.id
1149+
]
1150+
11371151

11381152
class ModelFile(BaseModel):
11391153
"""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
@@ -223,3 +223,56 @@ def test_get_changes_for_period():
223223
)
224224
assert problem.get_changes_for_period(p1) == [ch1, ch2]
225225
assert problem.get_changes_for_period(p2) == [ch3]
226+
227+
228+
def test_get_measurements_for_experiment():
229+
"""Test getting measurements for an experiment."""
230+
problem = Problem()
231+
problem += Condition(
232+
id="condition1",
233+
changes=[Change(target_id="target1", target_value=1.0)],
234+
)
235+
problem += Condition(
236+
id="condition2",
237+
changes=[Change(target_id="target2", target_value=2.0)],
238+
)
239+
240+
e1 = Experiment(
241+
id="exp1",
242+
periods=[
243+
ExperimentPeriod(id="p1", time=0, condition_ids=["condition1"]),
244+
],
245+
)
246+
e2 = Experiment(
247+
id="exp2",
248+
periods=[
249+
ExperimentPeriod(id="p2", time=1, condition_ids=["condition2"]),
250+
],
251+
)
252+
problem += e1
253+
problem += e2
254+
255+
m1 = Measurement(
256+
observable_id="observable1",
257+
experiment_id="exp1",
258+
time=0,
259+
measurement=10.0,
260+
)
261+
m2 = Measurement(
262+
observable_id="observable2",
263+
experiment_id="exp1",
264+
time=1,
265+
measurement=20.0,
266+
)
267+
m3 = Measurement(
268+
observable_id="observable3",
269+
experiment_id="exp2",
270+
time=1,
271+
measurement=30.0,
272+
)
273+
problem += m1
274+
problem += m2
275+
problem += m3
276+
277+
assert problem.get_measurements_for_experiment(e1) == [m1, m2]
278+
assert problem.get_measurements_for_experiment(e2) == [m3]

0 commit comments

Comments
 (0)