-
Notifications
You must be signed in to change notification settings - Fork 1
/
lmfit_ngauss.py
117 lines (89 loc) · 2.83 KB
/
lmfit_ngauss.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
#!/usr/bin/python
## Implementation of a function to fit n gaussians (Normalized absorption lines - 1 (to put continuum at 0))
## using lmfit (https://lmfit.github.io/lmfit-py/)
##imports:
from lmfit.models import GaussianModel
## My functions:
def lmfit_ngauss(x,y, params):
"""
INPUT:
x - is the wavelength array
y - is the normalized flux
params - is a list/array of initial guess values for the parameters
(this controls the number of gaussians to be fitted
number of gaussians: len(params)/3 - 3 parameters per Gaussian)
OUTPUT:
mod - the lmfit model object used for the fit
out - the lmfit fit object that contains all the results of the fit
init- array with the initial guess model (usefull to see the initial guess when plotting)
"""
mods = []
prefixes = []
for i in range(0, len(params), 3):
pref = "g%02i_" % (i/3)
gauss_i = GaussianModel(prefix=pref)
if i == 0:
pars = gauss_i.guess(y, x=x)
else:
pars.update(gauss_i.make_params())
A = params[i]
l_cen = params[i+1]
sigma = params[i+2]
pars[pref+'amplitude'].set(A)
pars[pref+'center'].set(l_cen)
pars[pref+'sigma'].set(sigma)
mods.append(gauss_i)
prefixes.append(pref)
mod = mods[0]
if len(mods) > 1:
for m in mods[1:]:
mod += m
init = mod.eval(pars, x=x)
out = mod.fit(y, pars, x=x)
return mod, out, init
def lmfit_ngauss_constrains(x,y, params, constrains):
"""
INPUT:
x - is the wavelength array
y - is the normalized flux
params - is a list/array of initial guess values for the parameters
(this controls the number of gaussians to be fitted
number of gaussians: len(params)/3 - 3 parameters per Gaussian)
contrains - the limits of the constrains for the fit of the parameters
OUTPUT:
mod - the lmfit model object used for the fit
out - the lmfit fit object that contains all the results of the fit
init- array with the initial guess model (usefull to see the initial guess when plotting)
"""
mods = []
prefixes = []
for i in range(0, len(params), 3):
pref = "g%02i_" % (i/3)
gauss_i = GaussianModel(prefix=pref)
if i == 0:
pars = gauss_i.guess(y, x=x)
else:
pars.update(gauss_i.make_params())
A = params[i]
limA = constrains[i]
l_cen = params[i+1]
limL = constrains[i+1]
sigma = params[i+2]
limS = constrains[i+2]
pars[pref+'amplitude'].set(A, min=limA[0], max=limA[1])
pars[pref+'center'].set(l_cen, min=limL[0], max=limL[1])
pars[pref+'sigma'].set(sigma, min=limS[0], max=limS[1])
mods.append(gauss_i)
prefixes.append(pref)
mod = mods[0]
if len(mods) > 1:
for m in mods[1:]:
mod += m
init = mod.eval(pars, x=x)
out = mod.fit(y, pars, x=x)
return mod, out, init
### Main program:
def main():
print "Hello"
if __name__ == "__main__":
main()