-
Notifications
You must be signed in to change notification settings - Fork 6
/
simulate.py
258 lines (223 loc) · 8.95 KB
/
simulate.py
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
"""PEtab simulator base class and related functions."""
import abc
import pathlib
import shutil
import tempfile
from typing import Dict, Optional, Union
import numpy as np
import pandas as pd
import sympy as sp
import petab
__all__ = ["Simulator", "sample_noise"]
class Simulator(abc.ABC):
"""Base class that specific simulators should inherit.
Specific simulators should minimally implement the
:meth:`petab.simulate.Simulator.simulate_without_noise` method.
Example (AMICI): https://bit.ly/33SUSG4
Attributes:
noise_formulas:
The formulae that will be used to calculate the scale of noise
distributions.
petab_problem:
A PEtab problem, which will be simulated.
rng:
A NumPy random generator, used to sample from noise distributions.
temporary_working_dir:
Whether ``working_dir`` is a temporary directory, which can be
deleted without significant consequence.
working_dir:
All simulator-specific output files will be saved here. This
directory and its contents may be modified and deleted, and
should be considered ephemeral.
"""
def __init__(
self,
petab_problem: petab.Problem,
working_dir: Optional[Union[pathlib.Path, str]] = None,
):
"""Initialize the simulator.
Initialize the simulator with sufficient information to perform a
simulation. If no working directory is specified, a temporary one is
created.
Arguments:
petab_problem:
A PEtab problem.
working_dir:
All simulator-specific output files will be saved here. This
directory and its contents may be modified and deleted, and
should be considered ephemeral.
"""
self.petab_problem = petab_problem
self.temporary_working_dir = False
if working_dir is None:
working_dir = tempfile.mkdtemp()
self.temporary_working_dir = True
if not isinstance(working_dir, pathlib.Path):
working_dir = pathlib.Path(working_dir)
self.working_dir = working_dir
self.working_dir.mkdir(parents=True, exist_ok=True)
self.noise_formulas = petab.calculate.get_symbolic_noise_formulas(
self.petab_problem.observable_df
)
self.rng = np.random.default_rng()
def remove_working_dir(self, force: bool = False, **kwargs) -> None:
"""Remove the simulator working directory, and all files within.
See the :meth:`petab.simulate.Simulator.__init__` method arguments.
Arguments:
force:
If ``True``, the working directory is removed regardless of
whether it is a temporary directory.
**kwargs:
Additional keyword arguments are passed to
:func:`shutil.rmtree`.
"""
if force or self.temporary_working_dir:
shutil.rmtree(self.working_dir, **kwargs)
if self.working_dir.is_dir():
print(
"Failed to remove the working directory: "
+ str(self.working_dir)
)
else:
print(
"By default, specified working directories are not removed. "
"Please call this method with `force=True`, or manually "
f"delete the working directory: {self.working_dir}"
)
@abc.abstractmethod
def simulate_without_noise(self) -> pd.DataFrame:
"""Simulate the PEtab problem.
This is an abstract method that should be implemented with a simulation
package. Examples of this are referenced in the class docstring.
Returns:
Simulated data, as a PEtab measurements table, which should be
equivalent to replacing all values in the
:const:`petab.C.MEASUREMENT` column of the measurements table (of
the PEtab problem supplied to the
:meth:`petab.simulate.Simulator.__init__` method), with
simulated values.
"""
raise NotImplementedError()
def simulate(
self,
noise: bool = False,
noise_scaling_factor: float = 1,
as_measurement: bool = False,
**kwargs,
) -> pd.DataFrame:
"""Simulate a PEtab problem, optionally with noise.
Arguments:
noise: If True, noise is added to simulated data.
noise_scaling_factor:
A multiplier of the scale of the noise distribution.
as_measurement:
Whether the data column is named :const:`petab.C.MEASUREMENT`
(`True`) or :const:`petab.C.SIMULATION` (`False`).
**kwargs:
Additional keyword arguments are passed to
:meth:`petab.simulate.Simulator.simulate_without_noise`.
Returns:
Simulated data, as a PEtab measurements table.
"""
simulation_df = self.simulate_without_noise(**kwargs)
if noise:
simulation_df = self.add_noise(simulation_df, noise_scaling_factor)
columns = {petab.C.MEASUREMENT: petab.C.SIMULATION}
if as_measurement:
columns = {petab.C.SIMULATION: petab.C.MEASUREMENT}
simulation_df = simulation_df.rename(columns=columns)
return simulation_df
def add_noise(
self,
simulation_df: pd.DataFrame,
noise_scaling_factor: float = 1,
**kwargs,
) -> pd.DataFrame:
"""Add noise to simulated data.
Arguments:
simulation_df:
A PEtab measurements table that contains simulated data.
noise_scaling_factor:
A multiplier of the scale of the noise distribution.
**kwargs:
Additional keyword arguments are passed to
:func:`sample_noise`.
Returns:
Simulated data with noise, as a PEtab measurements table.
"""
simulation_df_with_noise = simulation_df.copy()
simulation_df_with_noise[petab.C.MEASUREMENT] = [
sample_noise(
self.petab_problem,
row,
row[petab.C.MEASUREMENT],
self.noise_formulas,
self.rng,
noise_scaling_factor,
**kwargs,
)
for _, row in simulation_df_with_noise.iterrows()
]
return simulation_df_with_noise
def sample_noise(
petab_problem: petab.Problem,
measurement_row: pd.Series,
simulated_value: float,
noise_formulas: Optional[Dict[str, sp.Expr]] = None,
rng: Optional[np.random.Generator] = None,
noise_scaling_factor: float = 1,
zero_bounded: bool = False,
) -> float:
"""Generate a sample from a PEtab noise distribution.
Arguments:
petab_problem:
The PEtab problem used to generate the simulated value.
Instance of :class:`petab.Problem`.
measurement_row:
The row in the PEtab problem measurement table that corresponds
to the simulated value.
simulated_value:
A simulated value without noise.
noise_formulas:
Processed noise formulas from the PEtab observables table, in the
form output by :func:`petab.calculate.get_symbolic_noise_formulas`.
rng:
A NumPy random generator.
noise_scaling_factor:
A multiplier of the scale of the noise distribution.
zero_bounded:
Return zero if the sign of the return value and ``simulated_value``
differ. Can be used to ensure non-negative and non-positive values,
if the sign of ``simulated_value`` should not change.
Returns:
The sample from the PEtab noise distribution.
"""
if noise_formulas is None:
noise_formulas = petab.calculate.get_symbolic_noise_formulas(
petab_problem.observable_df
)
if rng is None:
rng = np.random.default_rng()
noise_value = petab.calculate.evaluate_noise_formula(
measurement_row,
noise_formulas,
petab_problem.parameter_df,
simulated_value,
)
# default noise distribution is petab.C.NORMAL
noise_distribution = petab_problem.observable_df.loc[
measurement_row[petab.C.OBSERVABLE_ID]
].get(petab.C.NOISE_DISTRIBUTION, petab.C.NORMAL)
# an empty noise distribution column in an observables table can result in
# `noise_distribution == float('nan')`
if pd.isna(noise_distribution):
noise_distribution = petab.C.NORMAL
# below is e.g.: `np.random.normal(loc=simulation, scale=noise_value)`
simulated_value_with_noise = getattr(rng, noise_distribution)(
loc=simulated_value, scale=noise_value * noise_scaling_factor
)
if zero_bounded and np.sign(simulated_value) != np.sign(
simulated_value_with_noise
):
return 0.0
return simulated_value_with_noise