-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCurvePrep.py
367 lines (311 loc) · 12 KB
/
CurvePrep.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import numpy as np
import scipy.optimize as sco
import matplotlib.pyplot as plt
from matplotlib import path
from matplotlib.widgets import Lasso
from matplotlib import colors as mcolors
class LassoSelector:
def __init__(self, spec_wave, chirp_wave, centers, corr):
"""
Initializes a LassoSelector object containing a figure displaying
the chirp to select data points.
Parameters
----------
spec_wave : np.array
The wavelengths of the entire sample measurement.
chirp_wave : np.array
The wavelengths of the chirp / OKE meaasurement.
centers : np.array
The minima for each chirp_wave spectrum.
corr : ChirpCorrector
The ChirpCorrector object in which the LassoSelector was
initialized.
Returns
-------
None.
"""
self.corr = corr
self.fig, self.ax = plt.subplots()
self.collection = self.ax.scatter(chirp_wave, centers, marker='o')
self.collection.set(alpha=0.5, clim=[0, 1], cmap=mcolors.ListedColormap(["tab:blue", "tab:red"]), edgecolor="black", label="centers")
self.ax.legend(loc='lower right')
self.ax.set_title("Please select the data points for the curve fit by pressing and holding the left mouse button and draging the cursor around the data points. \n Press ENTER once you are finished.")
self.spec_wave = spec_wave
self.fig.canvas.mpl_connect('key_press_event', self.on_Enter)
canvas = self.ax.figure.canvas
canvas.mpl_connect('button_press_event', self.on_press)
canvas.mpl_connect('button_release_event', self.on_release)
def callback(self, verts):
"""
Saves the data points selected by the lasso.
Parameters
----------
verts : list
A collection of (x,y) coordinate tuples of the selected data inside
of the lasso.
Returns
-------
None.
"""
self.data = self.collection.get_offsets()
self.collection.set_array(path.Path(verts).contains_points(self.data))
canvas = self.collection.figure.canvas
canvas.draw_idle()
del self.lasso
def on_press(self, event):
"""
On pressing the mouse button creates a lasso.
Parameters
----------
event : mouse press event
The event triggerd by pressing the mouse button.
Returns
-------
None.
"""
canvas = self.collection.figure.canvas
if event.inaxes is not self.collection.axes or canvas.widgetlock.locked():
return
self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
canvas.widgetlock(self.lasso)
def on_release(self, event):
"""
On releasing the mouse button stops the lasso.
Parameters
----------
event : mouse press event
The event triggerd by pressing the mouse button.
Returns
-------
None.
"""
canvas = self.collection.figure.canvas
if hasattr(self, 'lasso') and canvas.widgetlock.isowner(self.lasso):
canvas.widgetlock.release(self.lasso)
def fitCurve(self, centers):
"""
Fits a curve to the selected data points and plots the curve.
Parameters
----------
centers : np.array
The the minima of the spectrum of each wavelength of the
OKE / Chirp measurement.
Returns
-------
None.
"""
if len(self.ax.lines) > 1:
self.ax.lines.clear()
fitfun = lambda x, a1, a2, a3: a1 + 1e5 * a2 / x**2 + 1e6 * a3 / x**4
self.popt, pcov = sco.curve_fit(fitfun, centers[0], centers[1])
self.shift = fitfun(self.spec_wave, *self.popt)
self.ax.plot(centers[0], fitfun(centers[0], *self.popt), 'k-', label=f"a + 10**5*b/x**2 + 10**6*c/x**4 \n a = {round(self.popt[0])}, b = {round(self.popt[1])}, c = {round(self.popt[2])}")
self.ax.legend(loc='lower right')
self.fig.canvas.draw_idle()
def on_Enter(self, event):
"""
Fits a curve to the selected data points and hands over the curvefit
parameters to the ChirpCorrector object and starts the correction.
Parameters
----------
event : button press event
The event trigged by pressing any button.
Returns
-------
None.
"""
if event.key == "enter":
if self.collection.get_array().any():
self.fitCurve(self.data[self.collection.get_array()].T)
self.corr.popt = self.popt
self.corr.correctShift(self.shift)
class AutoSelector:
def __init__(self, spec_wave, chirp_wave, centers):
"""
Initializes an AutoSelector object which automatically removes data
spikes.
Parameters
----------
spec_wave : np.array
The wavelengths of the entire sample measurement.
chirp_wave : np.array
The wavelengths of the chirp / OKE meaasurement.
centers : np.array
The minima for each chirp_wave spectrum.
Returns
-------
None.
"""
self.spec_wave = spec_wave
self.chirp_wave = chirp_wave
self.centers = centers
def removeSpikes(self):
"""
Removes data spikes by if the difference between two consecutive
values is greater than 0.2 and removing them.
Returns
-------
ns_wave : np.array
The wavelengths without wavelengths where spikes were detected.
ns_centers : np.array
The centers without the spikes.
"""
ns_wave = self.chirp_wave
ns_centers = self.centers
while True:
prev_length = len(ns_centers)
diff = np.absolute(np.diff(ns_centers))
diff = np.insert(diff, 0, diff[0])
diff = self.sepSpikesAndFollowValues(diff, ns_centers)
lv = (diff < 0.2)
ns_wave = ns_wave[lv]
ns_centers = ns_centers[lv]
new_length = len(ns_centers)
if prev_length == new_length:
break
return ns_wave, ns_centers
def sepSpikesAndFollowValues(self, diff, centers):
"""
Makes sure that only spikes and not the following value whos difference
is also greater than 0.2 are deleted by setting the difference of the
spike following value to 0.
Parameters
----------
diff : np.array
The array containing all the differences.
centers : np.array
The the minima of the spectrum of each wavelength of the
OKE / Chirp measurement.
Returns
-------
diff : np.array
The array containing all the differences with the spike follow
value difference set to 0.
"""
idx = 0
diff_len = len(diff)
while idx < diff_len - 1:
if diff[idx] > 0.2:
temp_idx = idx + 1
while temp_idx < diff_len and abs(centers[idx - 1] - centers[temp_idx]) > 0.2:
temp_idx += 1
if temp_idx < diff_len and diff[temp_idx] != 0:
diff[temp_idx] = 0
idx = temp_idx
else:
idx += 1
return diff
def fitCurve(self, ns_wave, ns_centers):
"""
Fits a curve to the selected data points and plots the curve.
Parameters
----------
ns_wave: np.array
The wavelengths without wavelengths where spikes were detected.
ns_centers : np.array
The the minima of the spectrum of each wavelength of the
OKE / Chirp measurement without the spikes.
Returns
-------
shift : np.array
The temporal shift for each wavelength.
popt : np.array
The curvefit parameters.
"""
fitfun = lambda x, a1, a2, a3: a1 + 1e5 * a2 / x**2 + 1e6 * a3 / x**4
popt, pcov = sco.curve_fit(fitfun, ns_wave, ns_centers)
shift_plot = fitfun(ns_wave, *popt)
shift = fitfun(self.spec_wave, *popt)
plt.figure()
plt.plot(self.chirp_wave, self.centers, '.', markersize="10", mfc='none', color="blue", label="Centers with spikes", alpha=0.5)
plt.plot(ns_wave, ns_centers, '.', markersize="10", mfc='none', color="red", label="Centers without spikes", alpha=0.7)
plt.plot(ns_wave, shift_plot, 'k-', label=f"a + 10**5*b/x**2 + 10**6*c/x**4 \n a = {round(popt[0])}, b = {round(popt[1])}, c = {round(popt[2])}")
plt.show()
plt.legend(loc='lower right')
return shift, popt
class CurveClicker:
def __init__(self, wave, time, spec, corr):
"""
Initializes a CurveClicker object containing a figure displaying
the sample measurement to select data points to form the chirp curve.
Parameters
----------
wave : np.array
The wavelengths of the sample measurement.
time : np.array
The dalays of the sample measurement.
spec : np.ndarray
The spectral data of the sample measurement.
corr : ChirpCorrector
The ChirpCorrector object in which the CurveClicker was
initialized.
Returns
-------
None.
"""
self.chirp = []
self.wave = wave
self.corr = corr
self.fig, (self.ax1, self.ax2) = plt.subplots(2, 1)
X, Y = np.meshgrid(self.wave, time)
self.ax1.contourf(X, Y, spec, cmap="seismic")
self.ax1.set_yscale('symlog')
self.ax1.set_title("Sample Measurement from -1 to +1 ps \n Please select multiple points along the chirp by left-clicking. \n Press ENTER once you are finished.")
self.fig.canvas.mpl_connect('key_press_event', self.on_Enter)
self.fig.canvas.mpl_connect('button_press_event', self.on_Click)
def on_Click(self, event):
"""
Saves the point where the click was registered and plots the point
for visual confirmation.
Parameters
----------
event : mouse click event
The event triggered by a mouse click.
Returns
-------
None.
"""
if event.xdata is not None and event.ydata is not None:
self.chirp.append([event.xdata, event.ydata])
self.ax1.scatter(event.xdata, event.ydata, color="black", marker='o')
self.fig.canvas.draw_idle()
def on_Enter(self, event):
"""
Plots all selected points in another axes and fits a curve to them.
Parameters
----------
event : button press event
The event triggered by pressing any button.
Returns
-------
None.
"""
if event.key == "enter":
self.chirp = np.array(self.chirp)
self.chirp = self.chirp.T
self.ax2.scatter(self.chirp[0], self.chirp[1])
self.fig.canvas.draw_idle()
shift = self.fitCurve(self.chirp[0], self.chirp[1])
self.corr.correctShift(shift)
def fitCurve(self, xs, ys):
"""
Fits a curve to the selected data points and plots the curve.
Parameters
----------
xs : np.array
The wavelengths of the chirp points.
ys : np.array
The delays of the chirp points.
Returns
-------
shift : np.array
The temporal shift of each wavelength.
"""
fitfun = lambda x, a1, a2, a3: a1 + 1e5 * a2 / x**2 + 1e6 * a3 / x**4
popt, pcov = sco.curve_fit(fitfun, xs, ys)
ploty = fitfun(xs, *popt)
shift = fitfun(self.wave, *popt)
self.corr.popt = popt
self.ax2.plot(xs, ploty, 'k-', label=f"a + 10**5*b/x**2 + 10**6*c/x**4 \n a = {round(popt[0])}, b = {round(popt[1])}, c = {round(popt[2])}")
self.ax2.legend()
return shift