-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPDEparams.py
421 lines (301 loc) · 13.6 KB
/
PDEparams.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
416
417
418
419
420
421
import warnings
warnings.filterwarnings('ignore')
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error, mean_squared_log_error, mean_absolute_error, median_absolute_error
from scipy.integrate import odeint
from scipy.optimize import differential_evolution, minimize
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
plt.rcParams['font.size'] = 20
plt.rcParams['font.family'] = 'serif'
plt.rcParams['text.usetex'] = True
sns.set_palette(["#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2",
"#D55E00", "#CC79A7"])
ncolours = len(plt.rcParams['axes.prop_cycle'])
colours = [list(plt.rcParams['axes.prop_cycle'])[i]['color'] for i in range(ncolours)]
from tqdm.auto import tqdm
class PDEmodel:
def __init__(self, data, model, initfunc, bounds, param_names=None, nvars=1,
ndims=1, nreplicates=1, obsidx=None, outfunc=None):
'''Initialises the PDEmodel object.
Parameters
----------
data: DataFrame of data points with each entry in the form:
[timepoint, coordinates, fuction values]
model: the PDE model to fit to the data. Should accept the parameters to
estimate as its last inputs and return the time derivatives of the
functions.
initfunc: array of functions defining the initial conditions for the model.
bounds: the contraints for the parameter values to use in the estimation,
as a tuple of tuples or list of tuples.
param_names (optional, default: None): parameter names to be used in tables
and plots. If None, names appear as
"parameter 1", "parameter 2", etc.
nvars (optional, default: 1): the number of variables in the system.
ndims (optional, default: 1): the number of spatial dimensions in the system.
nreplicates (optional, default: 1): the number of measurements per time-coodinate
pair in the data.
obsidx (optional, default: None): the indices (starting from zero) of the measured
variables. If None, all outputs are used.
outfunc (optional, default: None): function to be applied to the output.
If None, raw outputs are used.
Returns
-------
The constructed object.
'''
self.model = model
self.initfunc = initfunc
self.data = data
self.bounds = bounds
self.nvars = nvars
self.spacedims = ndims
self.nreplicates = nreplicates
self.obsidx = obsidx
self.outfunc = outfunc
self.nparams = len(self.bounds)
if param_names is not None:
self.param_names = param_names
else:
self.param_names = ['parameter ' + str(i+1) for i in range(self.nparams)]
datacols = data.columns.values
alloutputs = data[datacols[1+ndims:]].values
allcoordinates = data[datacols[1:1+ndims]].values
self.timedata = np.sort(np.unique(data[datacols[0]]))
dt = self.timedata[1] - self.timedata[0]
self.time = np.concatenate((np.arange(0,self.timedata[0],dt), self.timedata))
self.timeidxs = np.array([np.argwhere(np.isclose(t, self.time))[0][0] for t in self.timedata])
if self.spacedims==1:
self.space = np.sort(np.unique(allcoordinates))
elif self.spacedims>1:
shapes = np.empty(self.spacedims).astype(int)
self.spacerange = []
grid = []
for i in range(self.spacedims):
sortedspace = np.sort(np.unique(allcoordinates[:,i]))
self.spacerange.append([np.min(sortedspace), np.max(sortedspace)])
grid.append(sortedspace)
shapes[i] = sortedspace.shape[0]
shapes = tuple(np.append(shapes, self.spacedims))
self.spacerange = np.array(self.spacerange)
self.space = np.array(np.meshgrid(*(v for v in grid))).T.reshape(shapes)
self.shapes = shapes
if self.spacedims == 0:
self.initial_condition = np.array([self.initfunc[i]() for i in range(self.nvars)])
elif self.spacedims == 1:
self.initial_condition = np.array([np.vectorize(self.initfunc[i])(self.space) for i in range(self.nvars)])
else:
self.initial_condition = np.array([np.apply_along_axis(self.initfunc[i], -1, self.space) for i in range(self.nvars)])
if self.nvars == 1:
self.initial_condition = self.initial_condition[0]
self.functiondata = alloutputs
return
def costfn(self, params, initial_condition, functiondata, bootstrap=False):
'''Integrates the model and computes the cost function
Parameters
----------
params: parameter values.
Returns
-------
error: float, the value of the chosen error (see 'fit()') for the given set of parameters.
'''
if self.spacedims == 0:
if self.nparams == 1:
ft = odeint(self.model, initial_condition, self.time, args=(params[0],))
else:
ft = odeint(self.model, initial_condition, self.time, args=tuple(params))
ft = ft[self.timeidxs]
if not bootstrap:
ft = np.repeat(ft, self.nreplicates, axis=0)
if self.outfunc is not None:
ft = np.apply_along_axis(self.outfunc, -1, ft)
elif self.obsidx is not None:
ft = ft[:, self.obsidx]
try:
error = self.error(ft, functiondata)
except:
error = np.inf
if self.sqrt:
try:
error = np.sqrt(error)
except:
error = np.inf
return error
else:
if self.spacedims > 1 or self.nvars > 1:
initial_condition = initial_condition.reshape(-1)
ft = odeint(self.model, initial_condition, self.time, args=(self.space, *params))
if self.nvars>1:
ft = ft.reshape(ft.shape[0], self.nvars, -1)
ft = np.array([np.transpose([ft[:,j,:][i] for j in range(self.nvars)]) for i in range(ft.shape[0])])
if self.spacedims > 1:
if self.nvars > 1:
ft = ft.reshape(ft.shape[0], *self.shapes[:-1], self.nvars)
else:
ft = ft.reshape(ft.shape[0], *self.shapes[:-1])
ft = ft[self.timeidxs]
if self.nvars > 1:
ft = ft.reshape(-1,self.nvars)
else:
ft = ft.reshape(-1)
if not bootstrap:
ft = np.repeat(ft, self.nreplicates, axis=0)
if self.outfunc is not None:
ft = np.apply_along_axis(self.outfunc, -1, ft)
elif self.obsidx is not None:
ft = ft[:, self.obsidx]
try:
error = self.error(ft, functiondata)
except:
error = np.inf
if self.sqrt:
try:
error = np.sqrt(error)
except:
error = np.inf
return error
def fit(self, error='mse'):
'''Finds the parameters that minimise the cost function using differential evolution.
Prints them and assigns them to the Estimator object.
Parameters
----------
error: the type of error to minimise.
- 'mse': mean squared error
- 'rmse': root mean squared error
- 'msle': mean squared logarithmic error
- 'rmsle': mean squared logarithmic error
- 'mae': mean absolute error
- 'medae': median absolute error
Returns
-------
None
'''
if error is 'rmse' or error is 'rmsle':
self.sqrt = True
else:
self.sqrt = False
if error is 'mse' or error is 'rmse':
self.error = mean_squared_error
elif error is 'msle' or error is 'rmsle':
self.error = mean_squared_log_error
elif error is 'mae':
self.error = mean_absolute_error
elif error is 'medae':
self.error = median_absolute_error
optimisation = differential_evolution(self.costfn, bounds=self.bounds, args=(self.initial_condition, self.functiondata))
params = optimisation.x
best_params = {self.param_names[i]: [params[i]] for i in range(self.nparams)}
self.best_params = pd.DataFrame(best_params)
self.best_error = optimisation.fun
print(self.best_params)
return
def likelihood_profiles(self, param_values=None, npoints=100):
'''Computes the likelihood profile of each parameter.
Parameters
----------
param_values (optional, default: None): a list of arrays of values for each parameter.
npoints (optional, default: 100): the number of values in which to divide the parameter
domain given by the bounds, if param_values is not specified.
Returns
-------
None
'''
if not hasattr(self, 'error'):
self.error = mean_squared_error
self.sqrt = False
summary = pd.DataFrame({'parameter': [], 'value': [], 'error': []})
for i in tqdm(range(self.nparams), desc='parameters'):
xmin, xmax = self.bounds[i]
pname = self.param_names[i]
if param_values is None:
pvalues = np.linspace(xmin, xmax, npoints)
else:
pvalues = param_values[i]
new_bounds = [bound for bound in self.bounds]
errors = []
for pvalue in tqdm(pvalues, desc='values within parameters'):
new_bounds[i] = (pvalue, pvalue)
optimisation = differential_evolution(self.costfn, bounds=tuple(new_bounds), args=(self.initial_condition, self.functiondata))
errors.append(optimisation.fun)
summary = pd.concat([summary, pd.DataFrame({'parameter': pname, 'value': pvalues, 'error': np.array(errors)})], ignore_index=True)
self.result_profiles = summary
return
def plot_profiles(self):
'''Plots the likelihood profiles.
Parameters
----------
None
Returns
-------
None
'''
for i, pname in enumerate(self.param_names):
data = self.result_profiles[self.result_profiles.parameter == pname]
plt.plot(data.value.values, data.error.values, c=colours[5])
if np.max(data.error.values) > 250*np.min(data.error.values):
plt.ylim(-10.*np.min(data.error.values), 250*np.min(data.error.values))
else:
plt.ylim(bottom=-3.*np.min(data.error.values))
if hasattr(self, 'best_params'):
plt.scatter([self.best_params[pname][0]], [self.best_error], c=colours[1])
plt.tight_layout()
plt.xlabel(pname)
plt.ylabel('error')
plt.show()
return
def bootstrap(self, nruns=100):
'''Perform bootstrapping by randomly selecting one replicate per time-coodinate pair
and finding the best fit parameters in multiple rounds.
Parameters
----------
nruns (optional, default: 100): the number of times to perform the estimation.
Returns
-------
None
'''
if not hasattr(self, 'error'):
self.error = mean_squared_error
self.sqrt = False
summary = {self.param_names[i]: [] for i in range(self.nparams)}
for run in tqdm(range(nruns), desc='runs'):
idxs = np.arange(self.data.shape[0])[::self.nreplicates]+np.random.randint(self.nreplicates, size=self.data.shape[0]//self.nreplicates)
data = self.data.iloc[idxs]
functiondata = self.functiondata[idxs]
initial_data = data[data[data.columns[0]] == self.timedata[0]]
optimisation = differential_evolution(self.costfn, bounds=self.bounds, args=(self.initial_condition, functiondata, True))
params = optimisation.x
for i in range(self.nparams):
summary[self.param_names[i]].append(params[i])
summary = pd.DataFrame(summary)
self.bootstrap_raw = summary
self.bootstrap_summary = summary.describe()
print(self.bootstrap_summary)
return
def plot_bootstrap(self):
'''Plots the bootstrapping results.
Parameters
----------
None
Returns
-------
None
'''
if self.nparams > 1:
if hasattr(self, 'best_params'):
data = self.bootstrap_raw.copy()
data = pd.concat([data, self.best_params], ignore_index=True)
data['best'] = 0
data.best.iloc[-1] = 1
g = sns.pairplot(data, vars=data.columns[:-1], hue='best', palette={0: colours[5], 1: colours[1]}, diag_kind='kde', diag_kws=dict(shade=True))
g._legend.remove()
else:
g = sns.pairplot(self.bootstrap_raw)
else:
g = sns.distplot(self.bootstrap_raw, hist=False, kde_kws=dict(shade=True), color=colours[5])
plt.xlabel(self.param_names[0])
plt.axvline(x=self.best_params.values[0,0], color=colours[1], linestyle='--', linewidth=1.5)
plt.tight_layout()
plt.show()
return