-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfit-gp-tv.py
217 lines (188 loc) · 6.98 KB
/
fit-gp-tv.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
#!/usr/bin/env python3
from __future__ import print_function
import sys
sys.path.append('./method')
import os
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pints
from scipy.interpolate import interp1d
import model as m
import parametertransform
import priors
from priors import HalfNormalLogPrior, InverseGammaLogPrior
from sparse_gp_custom_likelihood import DiscrepancyLogLikelihood
"""
Run fit.
"""
model_list = ['A', 'B', 'C']
try:
which_model = sys.argv[1]
except:
print('Usage: python %s [str:which_model]' % os.path.basename(__file__)
+ ' --optional [N_repeats]')
sys.exit()
if which_model not in model_list:
raise ValueError('Input model %s is not available in the model list' \
% which_model)
# Get all input variables
import importlib
sys.path.append('./mmt-model-files')
info_id = 'model_%s' % which_model
info = importlib.import_module(info_id)
data_dir = './data'
savedir = './out/' + info_id + '-gp-tv'
if not os.path.isdir(savedir):
os.makedirs(savedir)
data_file_name = 'data-sinewave.csv'
print('Fitting to ', data_file_name)
print('Temperature: ', info.temperature)
saveas = data_file_name[5:][:-4]
# Protocol
protocol = np.loadtxt('./protocol-time-series/sinewave.csv', skiprows=1,
delimiter=',')
protocol_times = protocol[:, 0]
protocol = protocol[:, 1]
# Control fitting seed
# fit_seed = np.random.randint(0, 2**30)
fit_seed = 542811797
print('Fit seed: ', fit_seed)
np.random.seed(fit_seed)
# Set parameter transformation
transform_to_model_param = parametertransform.log_transform_to_model_param
transform_from_model_param = parametertransform.log_transform_from_model_param
# Load data
data = np.loadtxt(data_dir + '/' + data_file_name,
delimiter=',', skiprows=1) # headers
times = data[:, 0]
data = data[:, 1]
voltage = interp1d(protocol_times, protocol, kind='linear')(times)
noise_sigma = np.std(data[:500])
print('Estimated noise level: ', noise_sigma)
# Model
model = m.Model(info.model_file,
variables=info.parameters,
current_readout=info.current_list,
set_ion=info.ions_conc,
transform=transform_to_model_param,
temperature=273.15 + info.temperature, # K
)
LogPrior = {
'model_A': priors.ModelALogPrior,
'model_B': priors.ModelBLogPrior,
}
# Update protocol
model.set_fixed_form_voltage_protocol(protocol, protocol_times)
# Create Pints stuffs
n_non_model_p = 4 # Number of non-model parameters
inducing_times = times[::1000] # inducing or speudo points for the FITC GP
inducing_voltage = voltage[::1000]
problem = pints.SingleOutputProblem(model, times, data)
loglikelihood = DiscrepancyLogLikelihood(problem, inducing_times,
voltage=voltage, inducing_voltage=inducing_voltage, downsample=None)
logmodelprior = LogPrior[info_id](transform_to_model_param,
transform_from_model_param)
# Priors for discrepancy
# This will have considerable mass at the initial value
lognoiseprior = HalfNormalLogPrior(sd=25, transform=True)
logrhoprior_t = InverseGammaLogPrior(alpha=5, beta=5, transform=True)
logrhoprior_v = InverseGammaLogPrior(alpha=5, beta=5, transform=True)
logrhoprior = pints.ComposedLogPrior(logrhoprior_t, logrhoprior_v)
logkersdprior = InverseGammaLogPrior(alpha=1, beta=10, transform=True)
# Compose all priors
logprior = pints.ComposedLogPrior(logmodelprior, lognoiseprior, logrhoprior,
logkersdprior)
logposterior = pints.LogPosterior(loglikelihood, logprior)
# Check logposterior is working fine
priorparams = np.copy(info.base_param)
transform_priorparams = transform_from_model_param(priorparams)
# Stack non-model parameters together
initial_rho = [0.5, 0.5] # Kernel hyperparameter \rho
initial_ker_sigma = 5.0 # Kernel hyperparameter \ker_sigma
priorparams = np.hstack((priorparams, noise_sigma, initial_rho,
initial_ker_sigma))
transform_priorparams = np.hstack((transform_priorparams, np.log(noise_sigma),
np.log(initial_rho), np.log(initial_ker_sigma)))
print('Posterior at prior parameters: ',
logposterior(transform_priorparams))
for _ in range(10):
assert(logposterior(transform_priorparams) ==\
logposterior(transform_priorparams))
# Run
try:
N = int(sys.argv[2])
except IndexError:
N = 3
params, logposteriors = [], []
for i in range(N):
if i == 0:
x0 = transform_priorparams
else:
# Randomly pick a starting point
x0 = logprior.sample(n=1)[0]
print('Starting point: ', x0)
# Create optimiser
print('Starting logposterior: ', logposterior(x0))
opt = pints.OptimisationController(logposterior, x0, method=pints.CMAES)
opt.set_max_iterations(None)
opt.set_parallel(True)
# Run optimisation
try:
with np.errstate(all='ignore'):
# Tell numpy not to issue warnings
p, s = opt.run()
# model parameter transformation
p[:-n_non_model_p] = transform_to_model_param(p[:-n_non_model_p])
# non-model parameter transformation
p[-n_non_model_p:] = np.exp(p[-n_non_model_p:])
params.append(p)
logposteriors.append(s)
print('Found solution: Old parameters:' )
for k, x in enumerate(p):
print(pints.strfloat(x) + ' ' + \
pints.strfloat(priorparams[k]))
except ValueError:
import traceback
traceback.print_exc()
# Order from best to worst
order = np.argsort(logposteriors)[::-1] # (use [::-1] for LL)
logposteriors = np.asarray(logposteriors)[order]
params = np.asarray(params)[order]
# Show results
bestn = min(3, N)
print('Best %d logposteriors:' % bestn)
for i in range(bestn):
print(logposteriors[i])
print('Mean & std of logposterior:')
print(np.mean(logposteriors))
print(np.std(logposteriors))
print('Worst logposterior:')
print(logposteriors[-1])
# Extract best 3
obtained_logposterior0 = logposteriors[0]
obtained_parameters0 = params[0]
obtained_logposterior1 = logposteriors[1]
obtained_parameters1 = params[1]
obtained_logposterior2 = logposteriors[2]
obtained_parameters2 = params[2]
# Show results
print('Found solution: Old parameters:' )
# Store output
with open('%s/%s-solution-%s-1.txt' % (savedir, saveas, fit_seed), 'w') as f:
for k, x in enumerate(obtained_parameters0):
print(pints.strfloat(x) + ' ' + pints.strfloat(priorparams[k]))
f.write(pints.strfloat(x) + '\n')
print('Found solution: Old parameters:' )
# Store output
with open('%s/%s-solution-%s-2.txt' % (savedir, saveas, fit_seed), 'w') as f:
for k, x in enumerate(obtained_parameters1):
print(pints.strfloat(x) + ' ' + pints.strfloat(priorparams[k]))
f.write(pints.strfloat(x) + '\n')
print('Found solution: Old parameters:' )
# Store output
with open('%s/%s-solution-%s-3.txt' % (savedir, saveas, fit_seed), 'w') as f:
for k, x in enumerate(obtained_parameters2):
print(pints.strfloat(x) + ' ' + pints.strfloat(priorparams[k]))
f.write(pints.strfloat(x) + '\n')