|
1 | 1 | import tempfile |
2 | 2 | from pathlib import Path |
3 | 3 |
|
| 4 | +import pytest |
4 | 5 | import sympy as sp |
| 6 | +from pydantic import ValidationError |
| 7 | +from sympy.abc import x, y |
5 | 8 |
|
6 | | -from petab.v2.core import ( |
7 | | - Change, |
8 | | - Condition, |
9 | | - ConditionsTable, |
10 | | - Experiment, |
11 | | - ExperimentPeriod, |
12 | | - ObservablesTable, |
13 | | -) |
| 9 | +from petab.v2.core import * |
14 | 10 | from petab.v2.petab1to2 import petab1to2 |
15 | 11 |
|
16 | 12 | example_dir_fujita = Path(__file__).parents[2] / "doc/example/example_Fujita" |
@@ -73,3 +69,116 @@ def test_conditions_table_add_changes(): |
73 | 69 | conditions_table += c2 |
74 | 70 |
|
75 | 71 | assert conditions_table.conditions == [c1, c2] |
| 72 | + |
| 73 | + |
| 74 | +def test_measurments(): |
| 75 | + Measurement( |
| 76 | + observable_id="obs1", time=1, experiment_id="exp1", measurement=1 |
| 77 | + ) |
| 78 | + Measurement( |
| 79 | + observable_id="obs1", time="1", experiment_id="exp1", measurement="1" |
| 80 | + ) |
| 81 | + Measurement( |
| 82 | + observable_id="obs1", time="inf", experiment_id="exp1", measurement="1" |
| 83 | + ) |
| 84 | + |
| 85 | + Measurement( |
| 86 | + observable_id="obs1", |
| 87 | + time=1, |
| 88 | + experiment_id="exp1", |
| 89 | + measurement=1, |
| 90 | + observable_parameters=["p1"], |
| 91 | + noise_parameters=["n1"], |
| 92 | + ) |
| 93 | + |
| 94 | + Measurement( |
| 95 | + observable_id="obs1", |
| 96 | + time=1, |
| 97 | + experiment_id="exp1", |
| 98 | + measurement=1, |
| 99 | + observable_parameters=[1], |
| 100 | + noise_parameters=[2], |
| 101 | + ) |
| 102 | + |
| 103 | + Measurement( |
| 104 | + observable_id="obs1", |
| 105 | + time=1, |
| 106 | + experiment_id="exp1", |
| 107 | + measurement=1, |
| 108 | + observable_parameters=[sp.sympify("x ** y")], |
| 109 | + noise_parameters=[sp.sympify("x ** y")], |
| 110 | + ) |
| 111 | + |
| 112 | + assert ( |
| 113 | + Measurement( |
| 114 | + observable_id="obs1", |
| 115 | + time=1, |
| 116 | + experiment_id="exp1", |
| 117 | + measurement=1, |
| 118 | + non_petab=1, |
| 119 | + ).non_petab |
| 120 | + == 1 |
| 121 | + ) |
| 122 | + |
| 123 | + with pytest.raises(ValidationError, match="got -inf"): |
| 124 | + Measurement( |
| 125 | + observable_id="obs1", |
| 126 | + time="-inf", |
| 127 | + experiment_id="exp1", |
| 128 | + measurement=1, |
| 129 | + ) |
| 130 | + |
| 131 | + with pytest.raises(ValidationError, match="Invalid ID"): |
| 132 | + Measurement( |
| 133 | + observable_id="1_obs", time=1, experiment_id="exp1", measurement=1 |
| 134 | + ) |
| 135 | + |
| 136 | + with pytest.raises(ValidationError, match="Invalid ID"): |
| 137 | + Measurement( |
| 138 | + observable_id="obs", time=1, experiment_id=" exp1", measurement=1 |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +def test_observable(): |
| 143 | + Observable(id="obs1", formula=x + y) |
| 144 | + Observable(id="obs1", formula="x + y", noise_formula="x + y") |
| 145 | + Observable(id="obs1", formula=1, noise_formula=2) |
| 146 | + Observable( |
| 147 | + id="obs1", |
| 148 | + formula="x + y", |
| 149 | + noise_formula="x + y", |
| 150 | + observable_parameters=["p1"], |
| 151 | + noise_parameters=["n1"], |
| 152 | + ) |
| 153 | + Observable( |
| 154 | + id="obs1", |
| 155 | + formula=sp.sympify("x + y"), |
| 156 | + noise_formula=sp.sympify("x + y"), |
| 157 | + observable_parameters=[sp.Symbol("p1")], |
| 158 | + noise_parameters=[sp.Symbol("n1")], |
| 159 | + ) |
| 160 | + assert Observable(id="obs1", formula="x + y", non_petab=1).non_petab == 1 |
| 161 | + |
| 162 | + o = Observable(id="obs1", formula=x + y) |
| 163 | + assert o.observable_placeholders == set() |
| 164 | + assert o.noise_placeholders == set() |
| 165 | + |
| 166 | + o = Observable( |
| 167 | + id="obs1", |
| 168 | + formula="observableParameter1_obs1", |
| 169 | + noise_formula="noiseParameter1_obs1", |
| 170 | + ) |
| 171 | + assert o.observable_placeholders == { |
| 172 | + sp.Symbol("observableParameter1_obs1", real=True), |
| 173 | + } |
| 174 | + assert o.noise_placeholders == { |
| 175 | + sp.Symbol("noiseParameter1_obs1", real=True) |
| 176 | + } |
| 177 | + |
| 178 | + # TODO: this should raise an error |
| 179 | + # (numbering is not consecutive / not starting from 1) |
| 180 | + # TODO: clarify if observableParameter0_obs1 would be allowed |
| 181 | + # as regular parameter |
| 182 | + # |
| 183 | + # with pytest.raises(ValidationError): |
| 184 | + # Observable(id="obs1", formula="observableParameter2_obs1") |
0 commit comments