forked from PEtab-dev/libpetab-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeasurements.py
More file actions
357 lines (300 loc) · 10.8 KB
/
measurements.py
File metadata and controls
357 lines (300 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""Functions operating on the PEtab measurement table"""
# noqa: F405
import itertools
import math
import numbers
from pathlib import Path
import numpy as np
import pandas as pd
from . import core, lint, observables
from .C import * # noqa: F403
__all__ = [
"assert_overrides_match_parameter_count",
"create_measurement_df",
"get_measurement_df",
"get_measurement_parameter_ids",
"get_rows_for_condition",
"get_simulation_conditions",
"measurements_have_replicates",
"measurement_is_at_steady_state",
"split_parameter_replacement_list",
"write_measurement_df",
]
def get_measurement_df(
measurement_file: None | str | Path | pd.DataFrame,
) -> pd.DataFrame:
"""
Read the provided measurement file into a ``pandas.Dataframe``.
Arguments:
measurement_file: Name of file to read from or pandas.Dataframe
Returns:
Measurement DataFrame
"""
if measurement_file is None:
return measurement_file
if isinstance(measurement_file, str | Path):
measurement_file = pd.read_csv(
measurement_file, sep="\t", float_precision="round_trip"
)
lint.assert_no_leading_trailing_whitespace(
measurement_file.columns.values, MEASUREMENT
)
return measurement_file
def write_measurement_df(df: pd.DataFrame, filename: str | Path) -> None:
"""Write PEtab measurement table
Arguments:
df: PEtab measurement table
filename: Destination file name. The parent directory will be created
if necessary.
"""
df = get_measurement_df(df)
Path(filename).parent.mkdir(parents=True, exist_ok=True)
df.to_csv(filename, sep="\t", index=False)
def get_simulation_conditions(measurement_df: pd.DataFrame) -> pd.DataFrame:
"""
Create a table of separate simulation conditions. A simulation condition
is a specific combination of simulationConditionId and
preequilibrationConditionId.
Arguments:
measurement_df: PEtab measurement table
Returns:
Dataframe with columns 'simulationConditionId' and
'preequilibrationConditionId'. All-null columns will be omitted.
Missing 'preequilibrationConditionId's will be set to '' (empty
string).
"""
if measurement_df.empty:
return pd.DataFrame(data={SIMULATION_CONDITION_ID: []})
# find columns to group by (i.e. if not all nans).
# can be improved by checking for identical condition vectors
grouping_cols = core.get_notnull_columns(
measurement_df,
[SIMULATION_CONDITION_ID, PREEQUILIBRATION_CONDITION_ID],
)
# group by cols and return dataframe containing each combination
# of those rows only once (and an additional counting row)
# We require NaN-containing rows, but they are ignored by `groupby`,
# therefore replace them before
simulation_conditions = (
measurement_df.fillna("")
.groupby(grouping_cols)
.size()
.reset_index()[grouping_cols]
)
# sort to be really sure that we always get the same order
return simulation_conditions.sort_values(grouping_cols, ignore_index=True)
def get_rows_for_condition(
measurement_df: pd.DataFrame,
condition: pd.Series | pd.DataFrame | dict,
) -> pd.DataFrame:
"""
Extract rows in `measurement_df` for `condition` according
to 'preequilibrationConditionId' and 'simulationConditionId' in
`condition`.
Arguments:
measurement_df:
PEtab measurement DataFrame
condition:
DataFrame with single row (or Series) and columns
'preequilibrationConditionId' and 'simulationConditionId'.
Or dictionary with those keys.
Returns:
The subselection of rows in ``measurement_df`` for the condition
``condition``.
"""
# filter rows for condition
row_filter = 1
# check for equality in all grouping cols
if PREEQUILIBRATION_CONDITION_ID in condition:
row_filter = (
measurement_df[PREEQUILIBRATION_CONDITION_ID].fillna("")
== condition[PREEQUILIBRATION_CONDITION_ID]
) & row_filter
if SIMULATION_CONDITION_ID in condition:
row_filter = (
measurement_df[SIMULATION_CONDITION_ID]
== condition[SIMULATION_CONDITION_ID]
) & row_filter
# apply filter
cur_measurement_df = measurement_df.loc[row_filter, :]
return cur_measurement_df
def get_measurement_parameter_ids(measurement_df: pd.DataFrame) -> list[str]:
"""
Return list of ID of parameters which occur in measurement table as
observable or noise parameter overrides.
Arguments:
measurement_df:
PEtab measurement DataFrame
Returns:
List of parameter IDs
"""
def get_unique_parameters(series):
return core.unique_preserve_order(
itertools.chain.from_iterable(
series.apply(split_parameter_replacement_list)
)
)
return core.unique_preserve_order(
get_unique_parameters(measurement_df[OBSERVABLE_PARAMETERS])
+ get_unique_parameters(measurement_df[NOISE_PARAMETERS])
)
def split_parameter_replacement_list(
list_string: str | numbers.Number, delim: str = PARAMETER_SEPARATOR
) -> list[str | numbers.Number]:
"""
Split values in observableParameters and noiseParameters in measurement
table.
Arguments:
list_string: delim-separated stringified list
delim: delimiter
Returns:
List of split values. Numeric values may be converted to `float`,
and parameter IDs are kept as strings.
"""
if list_string is None or list_string == "":
return []
if isinstance(list_string, numbers.Number):
# Empty cells in pandas might be turned into nan
# We might want to allow nan as replacement...
if np.isnan(list_string):
return []
return [list_string]
result = [x.strip() for x in list_string.split(delim)]
def convert_and_check(x):
x = core.to_float_if_float(x)
if isinstance(x, float):
return x
if lint.is_valid_identifier(x):
return x
raise ValueError(
f"The value '{x}' in the parameter replacement list "
f"'{list_string}' is neither a number, nor a valid parameter ID."
)
return list(map(convert_and_check, result))
def create_measurement_df() -> pd.DataFrame:
"""Create empty measurement dataframe
Returns:
Created DataFrame
"""
return pd.DataFrame(
data={
OBSERVABLE_ID: [],
PREEQUILIBRATION_CONDITION_ID: [],
SIMULATION_CONDITION_ID: [],
MEASUREMENT: [],
TIME: [],
OBSERVABLE_PARAMETERS: [],
NOISE_PARAMETERS: [],
DATASET_ID: [],
REPLICATE_ID: [],
}
)
def measurements_have_replicates(measurement_df: pd.DataFrame) -> bool:
"""Tests whether the measurements come with replicates
Arguments:
measurement_df: Measurement table
Returns:
``True`` if there are replicates, ``False`` otherwise
"""
grouping_cols = core.get_notnull_columns(
measurement_df,
[
OBSERVABLE_ID,
SIMULATION_CONDITION_ID,
PREEQUILIBRATION_CONDITION_ID,
TIME,
],
)
return np.any(
measurement_df.fillna("").groupby(grouping_cols).size().values - 1
)
def assert_overrides_match_parameter_count(
measurement_df: pd.DataFrame, observable_df: pd.DataFrame
) -> None:
"""Ensure that number of parameters in the observable definition matches
the number of overrides in ``measurement_df``
Arguments:
measurement_df: PEtab measurement table
observable_df: PEtab observable table
"""
# sympify only once and save number of parameters
observable_parameters_count = {
obs_id: len(
observables.get_formula_placeholders(formula, obs_id, "observable")
)
for obs_id, formula in zip(
observable_df.index.values,
observable_df[OBSERVABLE_FORMULA],
strict=True,
)
}
noise_parameters_count = (
{
obs_id: len(
observables.get_formula_placeholders(formula, obs_id, "noise")
)
for obs_id, formula in zip(
observable_df.index.values,
observable_df[NOISE_FORMULA],
strict=True,
)
}
if NOISE_FORMULA in observable_df.columns
else {obs_id: 0 for obs_id in observable_df.index.values}
)
for _, row in measurement_df.iterrows():
# check observable parameters
try:
expected = observable_parameters_count[row[OBSERVABLE_ID]]
except KeyError as e:
raise ValueError(
f"Observable {row[OBSERVABLE_ID]} used in measurement table "
f"is not defined."
) from e
actual = len(
split_parameter_replacement_list(
row.get(OBSERVABLE_PARAMETERS, None)
)
)
# No overrides are also allowed
if actual != expected:
formula = observable_df.loc[row[OBSERVABLE_ID], OBSERVABLE_FORMULA]
raise AssertionError(
f"Mismatch of observable parameter overrides for "
f"{row[OBSERVABLE_ID]} ({formula})"
f"in:\n{row}\n"
f"Expected {expected} but got {actual}"
)
# check noise parameters
replacements = split_parameter_replacement_list(
row.get(NOISE_PARAMETERS, None)
)
try:
expected = noise_parameters_count[row[OBSERVABLE_ID]]
# No overrides are also allowed
if len(replacements) != expected:
raise AssertionError(
f"Mismatch of noise parameter overrides in:\n{row}\n"
f"Expected {expected} but got {len(replacements)}"
)
except KeyError as err:
# no overrides defined, but a numerical sigma can be provided
# anyways
if len(replacements) != 1 or not isinstance(
replacements[0], numbers.Number
):
raise AssertionError(
f"No placeholders have been specified in the noise model "
f"for observable {row[OBSERVABLE_ID]}, but parameter ID "
"or multiple overrides were specified in the "
"noiseParameters column."
) from err
def measurement_is_at_steady_state(time: float) -> bool:
"""Check whether a measurement is at steady state.
Arguments:
time:
The time.
Returns:
Whether the measurement is at steady state.
"""
return math.isinf(time)