/
bayes_example.py
415 lines (347 loc) · 12 KB
/
bayes_example.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Dict, Union, Sequence
import arviz as az
import numpy as np
import pandas as pd
from numpy.typing import ArrayLike
import pymc as pm
import pytensor.tensor as pt
import roadrunner
import xarray as xr
from matplotlib import pyplot as plt
from pytensor.compile.ops import as_op
from scipy import stats
from parameter_variability import MODEL_SIMPLE_PK, RESULTS_DIR
from parameter_variability.bayes.sampler import DistDefinition, Sampler, \
SampleSimulator, sampling_analysis
from parameter_variability.console import console
@dataclass
class BayesModel:
"""Perform Bayesian Inference on Parameter of ODE model.
Attributes
----------
sbml_model:
path to the SBML xml file
observable:
name of the compartment observed concentration e.g. [y_gut] or [y_cent]
init_values:
initial values on where to start the MCMC samplers
f_prior_dsns:
dict of PyMC prior distributions
prior_parameters:
parameters for the PyMC prior distributions
"""
sbml_model: Union[str, Path]
observable: str # FIXME: should be list
init_values: Dict[str, float]
f_prior_dsns: Dict[str, Callable]
prior_parameters: Dict[str, Dict[str, float]]
def ls_soln(self, data: xr.Dataset) -> Dict[str, np.ndarray]:
# TODO: Use the ls solution for the init values
pass
def setup(
self, data: xr.Dataset, end: int, steps: int, plot_model: bool = True,
use_true_thetas: bool = False
) -> pm.Model:
"""Initialization of Priors and Likelihood
Parameters
----------
data:
simulations dataframe
end:
end of the SBML forward simulation
steps:
steps on the SBML forward simulation
plot_model:
PLot model diagram after set up
use_true_thetas:
Use the true thetas as initial values
Returns
-------
model:
PyMC-based Bayesian model ready to be smapled
"""
coords: Dict[str, ArrayLike] = {'sim': data['sim'], 'time': data['time']}
rr_model: roadrunner.RoadRunner = roadrunner.RoadRunner(self.sbml_model)
# minimal selection
rr_model.timeCourseSelections = [self.observable]
@as_op(itypes=[pt.dmatrix, pt.ivector], otypes=[pt.dmatrix])
def pytensor_forward_model_matrix(theta: np.ndarray, sims: pt.TensorConstant):
"""ODE solution function.
Run the forward simulation for the sampled parameters theta.
Parameters
----------
theta:
draws coming from the MCMC sampler
sims:
amount of simulations in the data
Returns
-------
y:
forward simulation based on the SBML model
"""
y = np.empty(shape=(steps+1, sims.size))
for ksim in sims:
rr_model.resetAll()
for kkey, key in enumerate(self.prior_parameters):
rr_model.setValue(key, theta[ksim, kkey])
sim = rr_model.simulate(start=0, end=end, steps=steps)
# store data
# y[ksim, :, kobs] = sim[self.observable[kobs]]
y[:, ksim] = sim[self.observable]
return y
with pm.Model(coords=coords) as model:
# TODO: Add correlation matrix between priors and/or sims
# Simulation array for forward modelling
simulations = pm.ConstantData('simulations', data['sim'], dims='sim')
# prior distribution
p_prior_dsns: Dict[str, np.ndarray] = {}
for pid in self.prior_parameters:
dsn_pars = self.prior_parameters[pid]
dsn_f = self.f_prior_dsns[pid]
if use_true_thetas:
init = self.init_values[pid]
else:
init = np.repeat(self.init_values[pid], data['sim'].size)
p_prior_dsns[pid] = dsn_f(
pid,
mu=dsn_pars["loc"],
sigma=dsn_pars["s"],
initval=init,
# shape=(data['sim'].size,),
dims="sim",
)
# errors
sigma = pm.HalfNormal("sigma", sigma=1)
# ODE solution function
theta: Sequence[pt.TensorLike] = \
[p_prior_dsns[pid] for pid in self.prior_parameters]
theta_tensor: np.ndarray = pm.math.stack(theta, axis=1)
ode_soln = pytensor_forward_model_matrix(theta_tensor, simulations)
# likelihood
pm.LogNormal(
name=self.observable,
mu=ode_soln,
sigma=sigma,
observed=data[self.observable].transpose('time', 'sim'),
dims=('time', 'sim')
)
if plot_model:
pm.model_to_graphviz(model) \
.render(directory=RESULTS_DIR/'graph',
filename='bayes_model_graph.gv',
view=True)
return model
def sampler(
self, model: pm.Model, tune: int, draws: int, chains: int
) -> az.InferenceData:
"""Definition of the Sampling Process
Parameters
----------
model:
PyMC-based Model
tune:
amount of draws to be initially discarded
draws:
amount of draws to keep
chains:
number of chains to draw samples from
Returns
-------
trace:
MCMC draws from the specified model
"""
vars_list = list(model.values_to_rvs.keys())[:-1]
print(f"Variables: {vars_list}\n")
with model:
trace = pm.sample(
step=[pm.Slice(vars_list)], tune=tune, draws=draws, chains=chains,
# cores=15 # Number of CPUs - 2
)
return trace
def plot_trace(self, trace: az.InferenceData) -> None:
"""Trace plots of the parameters sampled
Parameters
----------
trace:
MCMC draws from the specified model
Returns
-------
plot:
Diagnostic plot of the samples
"""
console.print(az.summary(trace, stat_focus="median"))
az.plot_trace(trace, compact=True, kind="trace")
plt.suptitle("Trace plots")
plt.tight_layout()
plt.show()
def plot_simulations(
self, data: xr.Dataset, trace: az.InferenceData,
num_samples: int, forward_end: int, forward_steps: int
) -> None:
"""Plot observable with the simulations based on the MCMC samples
Parameters
----------
data:
simulations dataframe
trace:
MCMC draws from the specified model
num_samples:
amount of samples from the trace to be used on the SBML forward model
forward_end:
end of the SBML forward simulation
forward_steps:
steps on the SBML forward simulation
Returns
-------
plot:
Plot comparing the observed data with other possible simulations
based on the Bayesian sampler
"""
sims = data['sim'].values
n_sim = data['sim'].size
rr_model: roadrunner.RoadRunner = roadrunner.RoadRunner(self.sbml_model)
f, axes = plt.subplots(
nrows=n_sim,
ncols=1,
dpi=300,
figsize=(5, 5 * n_sim),
layout="constrained",
)
axes = [axes] if n_sim == 1 else axes
trace_ex = az.extract(trace, num_samples=num_samples)
for s, ax in zip(sims, axes):
df_s = data.sel(sim=s).to_dataframe().reset_index()
trace_s = trace_ex.sel(sim=s).to_dataframe().reset_index(drop=True)
# plot observable
ax.plot(
df_s["time"],
df_s[self.observable],
alpha=0.7,
color="tab:blue",
marker="o",
linestyle="None",
)
# plot sims
for _, row in trace_s.iterrows():
rr_model.resetAll()
for key in self.prior_parameters:
rr_model.setValue(key, row[key])
sim = rr_model.simulate(start=0, end=forward_end, steps=forward_steps)
sim = pd.DataFrame(sim, columns=sim.colnames)
ax.plot(
sim['time'],
sim[self.observable],
alpha=0.2,
lw=1,
linestyle='solid'
)
ax.set_xlabel("Time [min]")
ax.set_ylabel("Concentation [mM]")
ax.set_title(f"Compartment: {self.observable}\nSimulation: {s}")
plt.show()
def bayes_analysis(
bayes_model: BayesModel,
sampler: Sampler,
tune: int = 2000, draws: int = 4000, chains: int = 4,
n: int = 1, end: int = 20, steps: int = 100,
use_true_thetas: bool = False
) -> None:
"""Wrap up for the Bayesian analysis
Parameters
----------
bayes_model:
object with the PyMC setup
sampler:
object with the forward simulation
tune:
amount of draws to be initially discarded
draws:
amount of draws to keep
chains:
number of chains to draw samples from
n:
number of simulations to create for the toy example
end:
end of the SBML forward simulation
steps:
steps on the SBML forward simulation
use_true_thetas:
Use the true thetas as initial values
Returns
-------
result:
Result of the analysis
"""
# Sampling of data (FIXME: make this work only with the data; )
console.rule("Sampling", align="left", style="white")
data_err, true_thetas = sampling_analysis(
sampler=sampler,
n=n,
end=end,
steps=steps,
)
console.rule(f"Setup Bayes model")
if use_true_thetas:
bayes_model.init_values = true_thetas
mod = bayes_model.setup(data_err, end, steps, plot_model=True,
use_true_thetas=use_true_thetas)
console.print(mod)
console.rule(f"Sampling for {n=}") # FIXME: Save results to investigate later
sample = bayes_model.sampler(mod, tune=tune, draws=draws, chains=chains)
console.rule(f"Results for {n=}")
bayes_model.plot_trace(sample)
console.rule(f'Simulation for {n=}')
bayes_model.plot_simulations(data_err, sample, num_samples=25,
forward_end=end, forward_steps=steps)
if __name__ == "__main__":
# model definition
bayes_model = BayesModel(
sbml_model=MODEL_SIMPLE_PK,
observable="[y_gut]", # Other options are '[y_cent]' and '[y_peri]'
init_values={
"k": 2.0,
"CL": 1.0,
},
prior_parameters={
"k": {"loc": np.log(1.0), "s": 0.5},
"CL": {"loc": np.log(1.0), "s": 0.5}
},
f_prior_dsns={
"k": pm.LogNormal,
"CL": pm.LogNormal,
},
)
console.print(f"{bayes_model=}")
# example sampler
sampler = Sampler(
model=MODEL_SIMPLE_PK,
distributions=[
DistDefinition(
parameter="k",
f_distribution=stats.lognorm,
distribution_parameters={
"loc": np.log(2.5),
"s": 1,
},
),
DistDefinition(
parameter="CL",
f_distribution=stats.lognorm,
distribution_parameters={
"loc": np.log(2.5),
"s": 1,
},
)
],
)
console.print(f"{sampler=}")
bayes_analysis(
bayes_model=bayes_model,
tune=2000,
draws=4000,
chains=3,
sampler=sampler,
n=5
)