-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps12.py
338 lines (274 loc) · 9.73 KB
/
ps12.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
"""
Module to compute and sample the temporal distribution of S1 and S2 photons.
"""
import numpy as np
from scipy import stats, special, optimize
from matplotlib import pyplot as plt
import numba
import numba_scipy_special
import sampling_bounds
import textbox
def pexp(t, tau):
"""
Exponential pdf with scale tau.
"""
return stats.expon.pdf(t, scale=tau)
def pgauss(t, sigma):
"""
Normal pdf with scale sigma.
"""
return stats.norm.pdf(t, scale=sigma)
def ps1(t, p1, tau1, tau2):
"""
Mixture of two exponential pdfs with scale parameters tau1 and tau2. p1
is the amplitude of the tau1 component.
"""
return p1 * pexp(t, tau1) + (1 - p1) * pexp(t, tau2)
@numba.vectorize(nopython=True)
def pexpunif(t, tau, T):
"""
Convolution of an exponential with scale tau and a uniform in (0, T).
"""
if t <= 0:
return 0
elif 0 < t <= T:
return -np.expm1(-t / tau) / T
else:
return np.exp(-(t - T) / tau) * special.exprel(-T / tau) / tau
def ps2(t, p1, tau1, tau2, T):
"""
Mixture of two exponentials with scales tau1 and tau2 convoluted with
a uniform in (0, T). p1 is the weight of the tau1 component.
"""
return p1 * pexpunif(t, tau1, T) + (1 - p1) * pexpunif(t, tau2, T)
@numba.vectorize(nopython=True)
def logq(t, tau, sigma):
"""
Logarithm of exponential pdf with scale tau convoluted with a normal with
scale sigma without the 1/tau factor.
"""
return -t / tau + 1/2 * (sigma / tau) ** 2 + special.log_ndtr(t / sigma - sigma / tau)
@numba.njit
def logpexpgauss(t, tau, sigma):
"""
Logarithm of exponential pdf with scale tau convoluted with a normal with
scale sigma.
"""
return logq(t, tau, sigma) - np.log(tau)
@numba.njit
def pexpgauss(t, tau, sigma):
"""
Exponential pdf with scale tau convoluted with a normal with scale sigma.
"""
return np.exp(logpexpgauss(t, tau, sigma))
@numba.vectorize(nopython=True)
def pexpunifgauss(t, tau, T, sigma):
"""
Convolution of an exponential with scale tau, a uniform in (0, T), and a
normal with scale sigma.
"""
return 1/T * (special.ndtr(t / sigma) - special.ndtr((t - T) / sigma) - np.exp(logq(t, tau, sigma)) + np.exp(logq(t - T, tau, sigma)))
@numba.njit
def ps1gauss(t, p1, tau1, tau2, sigma):
"""
ps1 convoluted with a normal with scale sigma.
"""
return p1 * pexpgauss(t, tau1, sigma) + (1 - p1) * pexpgauss(t, tau2, sigma)
@numba.njit
def logps1gauss(t, p1, tau1, tau2, sigma):
"""
Logarithm of ps1 convoluted with a normal with scale sigma.
"""
return np.logaddexp(
np.log(p1) + logpexpgauss(t, tau1, sigma),
np.log1p(-p1) + logpexpgauss(t, tau2, sigma)
)
@numba.njit
def ps2gauss(t, p1, tau1, tau2, T, sigma):
"""
ps2 convoluted with a normal with scale sigma.
"""
return p1 * pexpunifgauss(t, tau1, T, sigma) + (1 - p1) * pexpunifgauss(t, tau2, T, sigma)
def ps1gaussmax(p1, tau1, tau2, sigma):
"""
Return the position of the maximum of ps1gauss with the given parameters.
"""
fun = lambda t: -logps1gauss(t, p1, tau1, tau2, sigma)
bracket = (0, sigma)
result = optimize.minimize_scalar(fun, bracket)
assert result.success
assert -sigma <= result.x <= 5 * sigma
return result.x
def ps2gaussmax(p1, tau1, tau2, T, sigma):
"""
Return the position of the maximum of ps2gauss with the given parameters.
"""
fun = lambda t: -np.log(ps2gauss(t, p1, tau1, tau2, T, sigma))
bracket = (0, T/2)
result = optimize.minimize_scalar(fun, bracket)
assert result.success
assert -sigma <= result.x <= T + sigma
return result.x
def ps12range(p1, tau1, tau2, T, sigma, nsamples=1, p=0.01):
"""
Return an interval (left, right) that approximately will contain all the
samples from ps2gauss with probability `p` if `nsamples` samples are drawn.
"""
left, _ = sampling_bounds.sampling_bounds('norm', nsamples, p)
left *= sigma
neff = nsamples * (p1 if tau1 > tau2 else 1 - p1)
_, right = sampling_bounds.sampling_bounds('expon', neff, p)
right *= max(tau1, tau2)
right += T
return left, right
def gens12(size, p1, tau1, tau2, T, sigma, generator=None):
"""
Draw samples from ps2gauss. In particular if T = 0 the distribution is
ps1gauss.
Parameters
----------
size : int or tuple
The length or shape of the samples array.
p1, tau1, tau2, T, sigma : scalar
The parameters of ps2gauss.
generator : np.random.Generator, optional
Random generator.
Return
------
samp : array
Samples array with shape specified by `size`.
"""
size = (size,) if np.isscalar(size) else tuple(size)
if generator is None:
generator = np.random.default_rng()
samp = generator.standard_exponential(size=size)
choice = generator.binomial(n=1, p=p1, size=size).astype(bool)
samp[choice] *= tau1
samp[~choice] *= tau2
if sigma != 0:
normsamp = generator.standard_normal(size=size)
normsamp *= sigma
samp += normsamp
if T != 0:
unifsamp = generator.uniform(0, T, size=size)
samp += unifsamp
return samp
def check_ps1(p1=0.75, tau1=7, tau2=1600, sigma=10):
"""
Plot ps1 and ps1gauss.
"""
fig, ax = plt.subplots(num='ps12.check_ps1', clear=True)
ax.set_title('Time distribution of S1 photons')
ax.set_xlabel('Time [ns]')
ax.set_ylabel('Probability density [ns$^{-1}$]')
t = np.linspace(-3 * sigma, tau2, 10000)
y = ps1(t, p1, tau1, tau2)
yc = ps1gauss(t, p1, tau1, tau2, sigma)
yc2 = np.exp(logps1gauss(t, p1, tau1, tau2, sigma))
y2 = ps2gauss(t, p1, tau1, tau2, min(tau1, tau2, sigma) / 100, sigma)
yg = pgauss(t, sigma)
mx = ps1gaussmax(p1, tau1, tau2, sigma)
my = ps1gauss(mx, p1, tau1, tau2, sigma)
ax.plot(t, y, label='S1')
ax.plot(t, yc, label='S1 + res')
ax.plot(t, yc2, '--', label='S1 + res (explog)')
ax.plot(t, y2, ':', label='S2(T$\\approx$0) + res')
ax.plot(t, yg, label='res')
ax.plot(mx, my, 'xk', label='maximum')
ax.legend(loc='best')
ax.minorticks_on()
ax.grid(True, which='major', linestyle='--')
ax.grid(True, which='minor', linestyle=':')
ax.set_yscale('log')
ax.set_ylim(np.min(yc), np.max(y))
fig.tight_layout()
fig.show()
return fig
def check_ps2(p1=0.1, tau1=11, tau2=3200, T=15000, sigma=1000):
"""
Plot ps2 and ps2gauss.
"""
fig, ax = plt.subplots(num='ps12.check_ps2', clear=True)
ax.set_title('Time distribution of S2 photons')
ax.set_xlabel('Time [ns]')
ax.set_ylabel('Probability density [arb. un.]')
t = np.linspace(-3 * sigma, T + 3 * tau2, 10000)
y = ps2(t, p1, tau1, tau2, T)
yc = ps2gauss(t, p1, tau1, tau2, T, sigma)
tg = np.linspace(-3 * sigma, 3 * sigma)
yg = pgauss(tg, sigma)
mx = ps2gaussmax(p1, tau1, tau2, T, sigma)
my = ps2gauss(mx, p1, tau1, tau2, T, sigma)
ax.plot(t, y / np.max(y), label='S2')
ax.plot(tg, yg / np.max(yg), label='diffusion')
ax.plot(t, yc / np.max(y), label='S2 + diffusion')
ax.plot(mx, my / np.max(y), 'xk', label='maximum')
textbox.textbox(ax, f"""\
$p_1$ = {p1}
$\\tau_1$ = {tau1} ns
$\\tau_2$ = {tau2} ns
$T$ = {T} ns
$\\sigma$ = {sigma} ns""", fontsize='medium', loc='lower center')
ax.legend(loc='upper right')
ax.minorticks_on()
ax.grid(True, which='major', linestyle='--')
ax.grid(True, which='minor', linestyle=':')
fig.tight_layout()
fig.show()
return fig
def check_gens1(p1=0.75, tau1=10, tau2=100, sigma=5):
"""
Check gens12 with T = 0.
"""
fig, ax = plt.subplots(num='ps12.check_gens1', clear=True)
ax.set_title('Time distribution of S1 photons')
ax.set_xlabel('Time [ns]')
ax.set_ylabel('Probabilty density [ns$^{-1}$]')
gen = np.random.default_rng(202012181733)
s = gens12(100000, p1, tau1, tau2, 0, sigma, gen)
# left, right = np.quantile(s, [0.001, 0.999])
# s = s[(s >= left) & (s <= right)]
ax.hist(s, bins='auto', histtype='step', density=True, label='samples histogram', zorder=2)
t = np.linspace(np.min(s), np.max(s), 1000)
y = ps1gauss(t, p1, tau1, tau2, sigma)
ax.plot(t, y, label='pdf')
p = 0.1
l, r = ps12range(p1, tau1, tau2, 0, sigma, nsamples=len(s), p=p)
ax.axvspan(l, r, color='#ddd', label=f'{p * 100:.2g} % bounds')
ax.legend(loc='best')
ax.set_yscale('log')
ax.minorticks_on()
ax.grid(True, which='major', linestyle='--')
ax.grid(True, which='minor', linestyle=':')
fig.tight_layout()
fig.show()
return fig
def check_gens2(p1=0.1, tau1=11, tau2=3200, T=15000, sigma=1000):
"""
Check gens2 with T != 0.
"""
fig, ax = plt.subplots(num='ps12.check_gens2', clear=True)
ax.set_title('Time distribution of S2 photons')
ax.set_xlabel('Time [ns]')
ax.set_ylabel('Probabilty density [ns$^{-1}$]')
gen = np.random.default_rng(202012181733)
s = gens12(100000, p1, tau1, tau2, T, sigma, gen)
ax.hist(s, bins='auto', histtype='step', density=True, label='samples histogram', zorder=2)
t = np.linspace(np.min(s), np.max(s), 1000)
y = ps2gauss(t, p1, tau1, tau2, T, sigma)
ax.plot(t, y, label='pdf')
p = 0.1
l, r = ps12range(p1, tau1, tau2, T, sigma, nsamples=len(s), p=p)
ax.axvspan(l, r, color='#ddd', label=f'{p * 100:.2g} % bounds')
ax.legend(loc='best')
ax.minorticks_on()
ax.grid(True, which='major', linestyle='--')
ax.grid(True, which='minor', linestyle=':')
fig.tight_layout()
fig.show()
return fig
if __name__ == '__main__':
check_ps1()
check_ps2()
check_gens1()
check_gens2()