-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfit_utils.py
187 lines (163 loc) · 5.77 KB
/
fit_utils.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
from scipy.special import erf
import lmfit
import numpy as np
import pylab as plt
LOW_GAIN_GAUSS_PARAMS = lmfit.Parameters()
LOW_GAIN_GAUSS_PARAMS.add('wid0', value= 2.2, min=1)
LOW_GAIN_GAUSS_PARAMS.add('amp0', value= 0.25 , min=0)
LOW_GAIN_GAUSS_PARAMS.add('mu0', value= 0, min=-0.00001, max=0.00001)
LOW_GAIN_GAUSS_PARAMS.add('wid1', value= 3.,min=0.5, max=np.sqrt(4))
LOW_GAIN_GAUSS_PARAMS.add('amp1', value=0.01 , min=0)
LOW_GAIN_GAUSS_PARAMS.add('mu1', value= 4.2 , min=1) # max=5 )
#LOW_GAIN_GAUSS_PARAMS.add('alpha1', value=-2.2, max=0.2, min=-8)
#LOW_GAIN_GAUSS_PARAMS.add('off0', value= 0, min=0, max=.03) #max=32)
#LOW_GAIN_GAUSS_PARAMS.add('off1', value= 0, min=0, max=.03) #max=32)
HIGH_GAIN_GAUSS_PARAMS = lmfit.Parameters()
HIGH_GAIN_GAUSS_PARAMS.add('wid0', value= 3.,)
HIGH_GAIN_GAUSS_PARAMS.add('amp0', value= 0.1 , min=0)
HIGH_GAIN_GAUSS_PARAMS.add('mu0', value= 0 ,min=-0.0001, max=0.0001 )
#HIGH_GAIN_GAUSS_PARAMS.add('off0', value= 0, min=0, max=.03) #max=32)
#HIGH_GAIN_GAUSS_PARAMS.add('alpha0', value=2,)
HIGH_GAIN_GAUSS_PARAMS.add('wid1', value= 1.5, min=0, max=np.sqrt(28)) #, max=9) #max=2)
HIGH_GAIN_GAUSS_PARAMS.add('amp1', value= 0.005 , min=0)
HIGH_GAIN_GAUSS_PARAMS.add('mu1', value= 28, min=10, max=36) #max=32)
#HIGH_GAIN_GAUSS_PARAMS.add('off1', value= 0, min=0, max=.03) #max=32)
#HIGH_GAIN_GAUSS_PARAMS.add('alpha1', value=-2)
def Gauss(x,amp,mu,wid):
"""returns a Gaussian"""
return amp*np.exp( \
-((x - mu)/wid)**2)
def Cumu(x, mu,alpha):
"""returns a cummulative distribution"""
return 0.5*( 1 + erf( (alpha*(x-mu)) /np.sqrt(2)) )
def skew_gauss(x,amp,mu,wid,alpha, off):
"""returns a skewed normal dist"""
return off + Gauss(x, amp, mu,wid)* 1 #Cumu(x,mu,alpha)*2
def gauss_standard(params, xdata, ydata):
"""
This is for fitting the 0th order peak
"""
amp = params['amp'].value
wid = params['wid' ].value
mu= params['mu' ].value
gauss_model = Gauss( xdata, amp, mu, wid)
return gauss_model-ydata
#@profile
def gauss_and_skewgauss( params, xdata, ydata):
""" residual function for fitting sum of two skewed Gaussians"""
# fit the 0-photon peak Gaussian
amp0 = params['amp0'].value
wid0 = params['wid0' ].value
mu0 = params['mu0' ].value
if 'alpha0' in params.keys():
alpha0 = params['alpha0'].value
else:
alpha0 = 0
if 'off0' in params.keys():
off0 = params['off0'].value
else:
off0 =0
gauss_model0 = skew_gauss( xdata, amp0, mu0, wid0, alpha=alpha0, off=off0)
amp1 = params['amp1'].value
wid1 = params['wid1' ].value
mu1 = params['mu1' ].value
if 'alpha1' in params.keys():
alpha1 = params['alpha1'].value
else:
alpha1 = 0
if 'off1' in params.keys():
off1 = params['off1'].value
else:
off1 =0
gauss_model1 = skew_gauss( xdata, amp1, mu1, wid1, alpha=alpha1, off=off1)
return gauss_model0 + gauss_model1 - ydata
#@profile
def fit_low_gain_dist(xdata, ydata, plot=False):
result = lmfit.minimize(gauss_and_skewgauss, LOW_GAIN_GAUSS_PARAMS,
args=(xdata, ydata ))
rp = result.params
amp0 = rp['amp0'].value
mu0 = rp['mu0'].value
wid0 = rp['wid0'].value
if 'alpha0' in rp.keys():
alpha0 = rp['alpha0'].value
else:
alpha0 = 0
if 'off0' in rp.keys():
off0 = rp['off0'].value
else:
off0 =0
amp1 = rp['amp1'].value
mu1 = rp['mu1'].value
wid1 = rp['wid1'].value
if 'alpha1' in rp.keys():
alpha1 = rp['alpha1'].value
else:
alpha1 = 0
if 'off1' in rp.keys():
off1 = rp['off1'].value
else:
off1 =0
gauss0 = skew_gauss(xdata, amp0,mu0,wid0,alpha0,off0)
gauss1 = skew_gauss(xdata, amp1,mu1,wid1,alpha1, off1)
if plot:
plt.figure()
plt.gca().tick_params(labelsize=14)
plt.xlabel("ADU (dark subtracted)", fontsize=14)
plt.ylabel("bincount", fontsize=14)
plt.plot( xdata, ydata, '.', label="data")
plt.gca().set_yscale("log")
plt.ylim(0.0001,.3)
plt.xlim(-20,50)
plt.plot( xdata, gauss1, label="fit to 1-photon peak")
plt.plot( xdata, gauss0, label="fit to 0-photon peak")
plt.plot( xdata, gauss0+gauss1, label="fit")
plt.legend( prop={'size':13})
plt.draw()
plt.pause(0.1)
return gauss0,gauss1, result
#@profile
def fit_high_gain_dist(xdata, ydata, plot=False):
result = lmfit.minimize(gauss_and_skewgauss, HIGH_GAIN_GAUSS_PARAMS,
args=(xdata, ydata ))
rp = result.params
amp0 = rp['amp0'].value
mu0 = rp['mu0'].value
wid0 = rp['wid0'].value
if 'alpha0' in rp.keys():
alpha0 = rp['alpha0'].value
else:
alpha0 = 0
if 'off0' in rp.keys():
off0 = rp['off0'].value
else:
off0 =0
amp1 = rp['amp1'].value
mu1 = rp['mu1'].value
wid1 = rp['wid1'].value
if 'alpha1' in rp.keys():
alpha1 = rp['alpha1'].value
else:
alpha1 = 0
if 'off1' in rp.keys():
off1 = rp['off1'].value
else:
off1 =0
gauss0 = skew_gauss(xdata, amp0,mu0,wid0,alpha0, off0)
gauss1 = skew_gauss(xdata, amp1,mu1,wid1,alpha1, off1)
if plot:
plt.figure()
plt.gca().tick_params(labelsize=14)
plt.xlabel("ADU (dark subtracted)", fontsize=14)
plt.ylabel("bincount", fontsize=14)
plt.plot( xdata, ydata, '.', label="data")
plt.gca().set_yscale("log")
plt.ylim(0.0001,.3)
plt.xlim(-20,50)
plt.plot( xdata, gauss1, label="fit to 1-photon peak")
plt.plot( xdata, gauss0, label="fit to 0-photon peak")
plt.plot( xdata, gauss0+gauss1, label="fit")
plt.legend( prop={'size':13})
plt.draw()
plt.pause(0.1)
return gauss0,gauss1, result