-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfit_models.py
More file actions
325 lines (298 loc) · 13.7 KB
/
Copy pathfit_models.py
File metadata and controls
325 lines (298 loc) · 13.7 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
"""
@author: micha
"""
import numpy as np
from scipy.special import voigt_profile
def func_baskovian(x, *params):
""" 3
Baskovian fit function (3 parameters):
:param x: 1D array (x-data)
:param params: c, x0, gamma (c=area, gamma=FWHM, gamma_=beta**2)
:return: 1D array (y-data)
"""
c, x0, gamma = params
gamma_ = (gamma**2)/(4.*(2**(2/3)-1.))
return c*gamma_**(3/2)/(2.* ( (x-x0)**2 + gamma_ )**(3/2) )
#return c*gamma_/(2.* ( (x-x0)**2 + gamma_ )**(3/2) )
def func_baskovian_par(*params):
"""
:return: x0, peak, FWHM
"""
c, x0, gamma = params
return func_baskovian(x0, *params), x0, gamma
def func_gaussian(x, *params):
""" 3
Gaussian fit function (3 parameters): c*exp(-(x-x0)**2/(2*sigma**2))
:param x: 1D array (x-data)
:param params: c, x0, sigma
:return: 1D array (y-data)
"""
return params[0]*np.exp(-(x-params[1])**2/(2*params[2]**2))
def func_lorentzian(x, *params):
""" 3
Lorentzian fit function (3 parameters):
:param x: 1D array (x-data)
:param params: c, x0, gamma
:return: 1D array (y-data)
"""
c, x0, gamma = params
return c * gamma**2 / ((x - x0)**2 + gamma**2)
def func_bwf(x, *params):
""" 3
Breit-Wigner Fanon fit function (3 parameters):
:param x: 1D array (x-data)
:param params: c, x0, gamma, q
:return: 1D array (y-data)
"""
c, x0, gamma, q = params
s = (x-x0)/gamma
return c * (1+s/q)**2 / (1 + (s)**2)
def func_voigt(x, *params):
""" 4
Voigt fit function (4 parameters).
:param x: 1D array (x-data)
:param params: c, x0, sigma, gamma
:return: 1D array (y-data)
"""
return params[0]*voigt_profile(x-params[1], params[2], params[3])
def func_lin(x, *params):
""" 2
Linear fit function (2 parameters): k*x+d
:param x: 1D array (x-data)
:param params: k, d
:return: 1D array (y-data)
"""
return params[0]*x + params[1]
def func_poly(x, *params):
""" n
Polynomial fit function (n parameters): a0 + a1*x +...+ an*x**n
:param x: 1D array (x-data)
:param params: a0, a1,... , an
:return: 1D array (y-data)
"""
#return sum([p*(x**i) for i, p in enumerate(params)])
return np.polynomial.Polynomial(params)(x)
# bounds or here also possible with abs in func definitions
def bounds_gaussian():
return [0,-np.inf,0], [np.inf,np.inf,np.inf]
def bounds_voigt():
return [0,-np.inf,0,0], [np.inf,np.inf,np.inf,np.inf]
def bounds_baskovian():
return [0,0,0], [np.inf,np.inf,np.inf]
def bounds_lorentzian():
return [0,0,0], [np.inf,np.inf,np.inf]
def bounds_bwf():
return [0,0,0,-np.inf], [np.inf,np.inf,np.inf,0]
class Model(object):
def __init__(self, model_type=None, params=[]):
self._total = 0
self._num_params = []
self._name_params = []
self._params = []
self._bounds_low = []
self._bounds_high = []
self._funcs = []
self._model_types = []
if model_type is not None:
self._total += 1
self._num_params += [len(params)]
self._params += [params]
self.funcs_update(model_type)
if model_type in ['gaussian']:
self._bounds_low, self._bounds_high = bounds_gaussian()
elif model_type in ['voigt']:
self._bounds_low, self._bounds_high = bounds_voigt()
elif model_type in ['baskovian']:
self._bounds_low, self._bounds_high = bounds_baskovian()
elif model_type in ['bwf']:
self._bounds_low, self._bounds_high = bounds_bwf()
def params_update(self, params:list, bounds=None):
self._params += [params]
self._num_params += [len(params)]
if bounds is None:
bounds = [[-np.inf]*len(params), [np.inf]*len(params)]
self.bounds_update(bounds[0], bounds[1])
def bounds_update(self, bounds_low, bounds_high):
self._bounds_low += [bounds_low]
self._bounds_high += [bounds_high]
def param_names_update(self, model_type, params:list):
if model_type in ['linear']:
self._name_params.extend(['k','d'])
elif model_type in ['polynomial']:
self._name_params.extend(['a'+str(i) for i in range(len(params))])
elif model_type in ['gaussian']:
self._name_params.extend(['c','x0','sigma'])
elif model_type in ['lorentzian']:
self._name_params.extend(['c','x0','gamma'])
elif model_type in ['voigt']:
self._name_params.extend(['c', 'x0', 'sigma', 'gamma'])
elif model_type in ['baskovian']:
self._name_params.extend(['c','x0','gamma'])
elif model_type in ['bwf']:
self._name_params.extend(['c','x0','gamma','q'])
def funcs_update(self, model_type):
if model_type in ['linear']:
self._funcs += [func_lin]
self._model_types += ['linear']
elif model_type in ['polynomial']:
self._funcs += [func_poly]
self._model_types += ['polynomial']
elif model_type in ['gaussian']:
self._funcs += [func_gaussian]
self._model_types += ['gaussian']
elif model_type in ['lorentzian']:
self._funcs += [func_lorentzian]
self._model_types += ['lorentzian']
elif model_type in ['voigt']:
self._funcs += [func_voigt]
self._model_types += ['voigt']
elif model_type in ['baskovian']:
self._funcs += [func_baskovian]
self._model_types += ['baskovian']
elif model_type in ['bwf']:
self._funcs += [func_bwf]
self._model_types += ['bwf']
def set_fit_params(self, params, y_stretch=1):
par_start = 0
for i, (num_par, model_type) in enumerate(zip(self._num_params, self._model_types)):
par_end = par_start + num_par
if model_type in ['linear']:
self._params[i] = [y_stretch*params[i] for i in range(par_start, par_end)]
elif model_type in ['polynomial']:
self._params[i] = [y_stretch*params[i] for i in range(par_start, par_end)]
elif model_type in ['gaussian']:
self._params[i] = [params[i] for i in range(par_start, par_end)]
self._params[i][0] *= y_stretch
elif model_type in ['lorentzian']:
self._params[i] = [params[i] for i in range(par_start, par_end)]
self._params[i][0] *= y_stretch
elif model_type in ['voigt']:
self._params[i] = [params[i] for i in range(par_start, par_end)]
self._params[i][0] *= y_stretch
elif model_type in ['baskovian']:
self._params[i] = [params[i] for i in range(par_start, par_end)]
self._params[i][0] *= y_stretch
elif model_type in ['bwf']:
self._params[i] = [params[i] for i in range(par_start, par_end)]
self._params[i][0] *= y_stretch
par_start = par_end
def renorm_fits(self):
par_start = 0
for i, (num_par, model_type) in enumerate(zip(self._num_params, self._model_types)):
par_end = par_start + num_par
if model_type in ['linear']:
break
elif model_type in ['polynomial']:
break
elif model_type in ['gaussian']:
break
elif model_type in ['lorentzian']:
self._params[i][0] *= func_lorentzian(self._params[i][1], 1, self._params[i][1], self._params[i][2])
elif model_type in ['voigt']:
self._params[i][0] *= func_voigt(self._params[i][1], 1, self._params[i][1], self._params[i][2], self._params[i][3])
elif model_type in ['baskovian']:
self._params[i][0] *= func_baskovian(self._params[i][1], 1, self._params[i][1], self._params[i][2])
elif model_type in ['bwf']:
self._params[i][0] *= func_bwf(self._params[i][1], 1, self._params[i][1], self._params[i][2], self._params[i][3])
#break
par_start = par_end
def check_params(self, model_type, params:dict, **kwargs):
params_list = []
if model_type in ['linear']:
keys_required = [key in params for key in ('k', 'd')]
if all(keys_required):
params_list = [params[key] for key in ['k', 'd']]
else:
print("Linear model needs initial values k and d.")
if len(params.keys()) > len(keys_required) or keys_required[0] is False:
print("Too many or wrong keys for this model type.")
elif model_type in ['polynomial']:
for a_i in range(len(params)):
if 'a'+str(a_i) in params:
params_list.append(params['a'+str(a_i)])
else:
print("Polynomial model only supports a_i with i=0,1,2... as initial values.")
elif model_type in ['gaussian']:
keys_required = [key in params for key in ('c', 'x0', 'sigma')]
if all(keys_required):
params_list = [params[key] for key in ['c', 'x0', 'sigma']]
else:
print("Gaussian model needs initial values c, x0 and sigma.")
if len(params.keys()) > len(keys_required) or keys_required[0] is False:
print("Too many or wrong keys for this model type.")
elif model_type in ['lorentzian']:
keys_required = [key in params for key in ('c', 'x0', 'gamma')]
if all(keys_required):
params_list = [None]*3
for i_k,key in enumerate(['c', 'x0', 'gamma']):
params_list[i_k] = params[key]
if key == 'c':
params_list[i_k] *= 1/func_lorentzian(params['x0'], 1, params['x0'], params['gamma'])
else:
print("Lorentzian model needs initial values c, x0 and gamma.")
if len(params.keys()) > len(keys_required) or keys_required[0] is False:
print("Too many or wrong keys for this model type.")
elif model_type in ['voigt']:
keys_required = [key in params for key in ('c', 'x0', 'sigma', 'gamma')]
if all(keys_required):
params_list = [None]*4
for i_k,key in enumerate(['c', 'x0', 'sigma', 'gamma']):
params_list[i_k] = params[key]
if key == 'c':
params_list[i_k] *= 1/func_voigt(params['x0'], 1, params['x0'], params['sigma'], params['gamma'])
else:
print("Voigt model needs initial values c, x0, sigma and gamma.")
if len(params.keys()) > len(keys_required) or keys_required[0] is False:
print("Too many or wrong keys for this model type.")
elif model_type in ['baskovian']:
keys_required = [key in params for key in ('c', 'x0', 'gamma')]
if all(keys_required):
params_list = [None]*3
for i_k,key in enumerate(['c', 'x0', 'gamma']):
params_list[i_k] = params[key]
if key == 'c':
params_list[i_k] *= 1/func_baskovian(params['x0'], 1, params['x0'], params['gamma'])
else:
print("Baskovian model needs initial values c, x0 and gamma.")
if len(params.keys()) > len(keys_required) or keys_required[0] is False:
print("Too many or wrong keys for this model type.")
elif model_type in ['bwf']:
keys_required = [key in params for key in ('c', 'x0', 'gamma', 'q')]
if all(keys_required):
params_list = [None]*4
for i_k,key in enumerate(['c', 'x0', 'gamma', 'q']):
params_list[i_k] = params[key]
if key == 'c':
params_list[i_k] *= 1/func_voigt(params['x0'], 1, params['x0'], params['gamma'], params['q'])
else:
print("Breit-Wigner Fanon model needs initial values c, x0, gamma and q.")
if len(params.keys()) > len(keys_required) or keys_required[0] is False:
print("Too many or wrong keys for this model type.")
return params_list
def add_fit_func(self, model_type, params:dict, bounds=None):
self.funcs_update(model_type)
params_list = self.check_params(model_type, params)
self.params_update(params_list, bounds)
self.param_names_update(model_type, params_list)
def get_params(self, separate:bool=False):
if separate is False:
return np.concatenate(self._params)
else:
return self._params
def get_fit_params(self):
return self.get_params()
def get_bounds(self):
return (np.concatenate(self._bounds_low), np.concatenate(self._bounds_high))
def get_fit_func(self, separate:bool=False):
if separate is False:
def func_total(x, *params):
res = 0.
par_start = 0
for i,func in enumerate(self._funcs):
par_end = par_start+self._num_params[i]
res += func(x, *params[par_start:par_end])
par_start = par_end
return res
return func_total
else:
return self._funcs