forked from Ulm-IQO/qudi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaser_scanner_logic.py
674 lines (534 loc) · 23.5 KB
/
laser_scanner_logic.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# -*- coding: utf-8 -*-
"""
This file contains a Qudi logic module for controlling scans of the
fourth analog output channel. It was originally written for
scanning laser frequency, but it can be used to control any parameter
in the experiment that is voltage controlled. The hardware
range is typically -10 to +10 V.
Qudi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Qudi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Qudi. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/>
"""
from collections import OrderedDict
import datetime
import matplotlib.pyplot as plt
import numpy as np
import time
from core.connector import Connector
from core.statusvariable import StatusVar
from core.util.mutex import Mutex
from logic.generic_logic import GenericLogic
from qtpy import QtCore
class LaserScannerLogic(GenericLogic):
"""This logic module controls scans of DC voltage on the fourth analog
output channel of the NI Card. It collects countrate as a function of voltage.
"""
sig_data_updated = QtCore.Signal()
# declare connectors
confocalscanner1 = Connector(interface='ConfocalScannerInterface')
savelogic = Connector(interface='SaveLogic')
scan_range = StatusVar('scan_range', [-10, 10])
number_of_repeats = StatusVar(default=10)
resolution = StatusVar('resolution', 500)
_scan_speed = StatusVar('scan_speed', 10)
_static_v = StatusVar('goto_voltage', 5)
sigChangeVoltage = QtCore.Signal(float)
sigVoltageChanged = QtCore.Signal(float)
sigScanNextLine = QtCore.Signal()
sigUpdatePlots = QtCore.Signal()
sigScanFinished = QtCore.Signal()
sigScanStarted = QtCore.Signal()
def __init__(self, **kwargs):
""" Create VoltageScanningLogic object with connectors.
@param dict kwargs: optional parameters
"""
super().__init__(**kwargs)
# locking for thread safety
self.threadlock = Mutex()
self.stopRequested = False
self.fit_x = []
self.fit_y = []
self.plot_x = []
self.plot_y = []
self.plot_y2 = []
def on_activate(self):
""" Initialisation performed during activation of the module.
"""
self._scanning_device = self.confocalscanner1()
self._save_logic = self.savelogic()
# Reads in the maximal scanning range. The unit of that scan range is
# micrometer!
self.a_range = self._scanning_device.get_position_range()[3]
# Initialise the current position of all four scanner channels.
self.current_position = self._scanning_device.get_scanner_position()
# initialise the range for scanning
self.set_scan_range(self.scan_range)
# Keep track of the current static voltage even while a scan may cause the real-time
# voltage to change.
self.goto_voltage(self._static_v)
# Sets connections between signals and functions
self.sigChangeVoltage.connect(self._change_voltage, QtCore.Qt.QueuedConnection)
self.sigScanNextLine.connect(self._do_next_line, QtCore.Qt.QueuedConnection)
# Initialization of internal counter for scanning
self._scan_counter_up = 0
self._scan_counter_down = 0
# Keep track of scan direction
self.upwards_scan = True
# calculated number of points in a scan, depends on speed and max step size
self._num_of_steps = 50 # initialising. This is calculated for a given ramp.
#############################
# TODO: allow configuration with respect to measurement duration
self.acquire_time = 20 # seconds
# default values for clock frequency and slowness
# slowness: steps during retrace line
self.set_resolution(self.resolution)
self._goto_speed = 10 # 0.01 # volt / second
self.set_scan_speed(self._scan_speed)
self._smoothing_steps = 10 # steps to accelerate between 0 and scan_speed
self._max_step = 0.01 # volt
##############################
# Initialie data matrix
self._initialise_data_matrix(100)
def on_deactivate(self):
""" Deinitialisation performed during deactivation of the module.
"""
self.stopRequested = True
@QtCore.Slot(float)
def goto_voltage(self, volts=None):
"""Forwarding the desired output voltage to the scanning device.
@param float volts: desired voltage (volts)
@return int: error code (0:OK, -1:error)
"""
# print(tag, x, y, z)
# Changes the respective value
if volts is not None:
self._static_v = volts
# Checks if the scanner is still running
if (self.module_state() == 'locked'
or self._scanning_device.module_state() == 'locked'):
self.log.error('Cannot goto, because scanner is locked!')
return -1
else:
self.sigChangeVoltage.emit(volts)
return 0
def _change_voltage(self, new_voltage):
""" Threaded method to change the hardware voltage for a goto.
@return int: error code (0:OK, -1:error)
"""
ramp_scan = self._generate_ramp(self.get_current_voltage(), new_voltage, self._goto_speed)
self._initialise_scanner()
ignored_counts = self._scan_line(ramp_scan)
self._close_scanner()
self.sigVoltageChanged.emit(new_voltage)
return 0
def _goto_during_scan(self, voltage=None):
if voltage is None:
return -1
goto_ramp = self._generate_ramp(self.get_current_voltage(), voltage, self._goto_speed)
ignored_counts = self._scan_line(goto_ramp)
return 0
def set_clock_frequency(self, clock_frequency):
"""Sets the frequency of the clock
@param int clock_frequency: desired frequency of the clock
@return int: error code (0:OK, -1:error)
"""
self._clock_frequency = float(clock_frequency)
# checks if scanner is still running
if self.module_state() == 'locked':
return -1
else:
return 0
def set_resolution(self, resolution):
""" Calculate clock rate from scan speed and desired number of pixels """
self.resolution = resolution
scan_range = abs(self.scan_range[1] - self.scan_range[0])
duration = scan_range / self._scan_speed
new_clock = resolution / duration
return self.set_clock_frequency(new_clock)
def set_scan_range(self, scan_range):
""" Set the scan rnage """
r_max = np.clip(scan_range[1], self.a_range[0], self.a_range[1])
r_min = np.clip(scan_range[0], self.a_range[0], r_max)
self.scan_range = [r_min, r_max]
def set_voltage(self, volts):
""" Set the channel idle voltage """
self._static_v = np.clip(volts, self.a_range[0], self.a_range[1])
self.goto_voltage(self._static_v)
def set_scan_speed(self, scan_speed):
""" Set scan speed in volt per second """
self._scan_speed = np.clip(scan_speed, 1e-9, 1e6)
self._goto_speed = self._scan_speed
def set_scan_lines(self, scan_lines):
self.number_of_repeats = int(np.clip(scan_lines, 1, 1e6))
def _initialise_data_matrix(self, scan_length):
""" Initializing the ODMR matrix plot. """
self.scan_matrix = np.zeros((self.number_of_repeats, scan_length))
self.scan_matrix2 = np.zeros((self.number_of_repeats, scan_length))
self.plot_x = np.linspace(self.scan_range[0], self.scan_range[1], scan_length)
self.plot_y = np.zeros(scan_length)
self.plot_y2 = np.zeros(scan_length)
self.fit_x = np.linspace(self.scan_range[0], self.scan_range[1], scan_length)
self.fit_y = np.zeros(scan_length)
def get_current_voltage(self):
"""returns current voltage of hardware device(atm NIDAQ 4th output)"""
return self._scanning_device.get_scanner_position()[3]
def _initialise_scanner(self):
"""Initialise the clock and locks for a scan"""
self.module_state.lock()
self._scanning_device.module_state.lock()
returnvalue = self._scanning_device.set_up_scanner_clock(
clock_frequency=self._clock_frequency)
if returnvalue < 0:
self._scanning_device.module_state.unlock()
self.module_state.unlock()
self.set_position('scanner')
return -1
returnvalue = self._scanning_device.set_up_scanner()
if returnvalue < 0:
self._scanning_device.module_state.unlock()
self.module_state.unlock()
self.set_position('scanner')
return -1
return 0
def start_scanning(self, v_min=None, v_max=None):
"""Setting up the scanner device and starts the scanning procedure
@return int: error code (0:OK, -1:error)
"""
self.current_position = self._scanning_device.get_scanner_position()
print(self.current_position)
if v_min is not None:
self.scan_range[0] = v_min
else:
v_min = self.scan_range[0]
if v_max is not None:
self.scan_range[1] = v_max
else:
v_max = self.scan_range[1]
self._scan_counter_up = 0
self._scan_counter_down = 0
self.upwards_scan = True
# TODO: Generate Ramps
self._upwards_ramp = self._generate_ramp(v_min, v_max, self._scan_speed)
self._downwards_ramp = self._generate_ramp(v_max, v_min, self._scan_speed)
self._initialise_data_matrix(len(self._upwards_ramp[3]))
# Lock and set up scanner
returnvalue = self._initialise_scanner()
if returnvalue < 0:
# TODO: error message
return -1
self.sigScanNextLine.emit()
self.sigScanStarted.emit()
return 0
def stop_scanning(self):
"""Stops the scan
@return int: error code (0:OK, -1:error)
"""
with self.threadlock:
if self.module_state() == 'locked':
self.stopRequested = True
return 0
def _close_scanner(self):
"""Close the scanner and unlock"""
with self.threadlock:
self.kill_scanner()
self.stopRequested = False
if self.module_state.can('unlock'):
self.module_state.unlock()
def _do_next_line(self):
""" If stopRequested then finish the scan, otherwise perform next repeat of the scan line
"""
# stops scanning
if self.stopRequested or self._scan_counter_down >= self.number_of_repeats:
print(self.current_position)
self._goto_during_scan(self._static_v)
self._close_scanner()
self.sigScanFinished.emit()
return
if self._scan_counter_up == 0:
# move from current voltage to start of scan range.
self._goto_during_scan(self.scan_range[0])
if self.upwards_scan:
counts = self._scan_line(self._upwards_ramp)
self.scan_matrix[self._scan_counter_up] = counts
self.plot_y += counts
self._scan_counter_up += 1
self.upwards_scan = False
else:
counts = self._scan_line(self._downwards_ramp)
self.scan_matrix2[self._scan_counter_down] = counts
self.plot_y2 += counts
self._scan_counter_down += 1
self.upwards_scan = True
self.sigUpdatePlots.emit()
self.sigScanNextLine.emit()
def _generate_ramp(self, voltage1, voltage2, speed):
"""Generate a ramp vrom voltage1 to voltage2 that
satisfies the speed, step, smoothing_steps parameters. Smoothing_steps=0 means that the
ramp is just linear.
@param float voltage1: voltage at start of ramp.
@param float voltage2: voltage at end of ramp.
"""
# It is much easier to calculate the smoothed ramp for just one direction (upwards),
# and then to reverse it if a downwards ramp is required.
v_min = min(voltage1, voltage2)
v_max = max(voltage1, voltage2)
if v_min == v_max:
ramp = np.array([v_min, v_max])
else:
# These values help simplify some of the mathematical expressions
linear_v_step = speed / self._clock_frequency
smoothing_range = self._smoothing_steps + 1
# Sanity check in case the range is too short
# The voltage range covered while accelerating in the smoothing steps
v_range_of_accel = sum(
n * linear_v_step / smoothing_range for n in range(0, smoothing_range)
)
# Obtain voltage bounds for the linear part of the ramp
v_min_linear = v_min + v_range_of_accel
v_max_linear = v_max - v_range_of_accel
if v_min_linear > v_max_linear:
self.log.warning(
'Voltage ramp too short to apply the '
'configured smoothing_steps. A simple linear ramp '
'was created instead.')
num_of_linear_steps = np.rint((v_max - v_min) / linear_v_step)
ramp = np.linspace(v_min, v_max, num_of_linear_steps)
else:
num_of_linear_steps = np.rint((v_max_linear - v_min_linear) / linear_v_step)
# Calculate voltage step values for smooth acceleration part of ramp
smooth_curve = np.array(
[sum(
n * linear_v_step / smoothing_range for n in range(1, N)
) for N in range(1, smoothing_range)
])
accel_part = v_min + smooth_curve
decel_part = v_max - smooth_curve[::-1]
linear_part = np.linspace(v_min_linear, v_max_linear, num_of_linear_steps)
ramp = np.hstack((accel_part, linear_part, decel_part))
# Reverse if downwards ramp is required
if voltage2 < voltage1:
ramp = ramp[::-1]
# Put the voltage ramp into a scan line for the hardware (4-dimension)
spatial_pos = self._scanning_device.get_scanner_position()
scan_line = np.vstack((
np.ones((len(ramp), )) * spatial_pos[0],
np.ones((len(ramp), )) * spatial_pos[1],
np.ones((len(ramp), )) * spatial_pos[2],
ramp
))
return scan_line
def _scan_line(self, line_to_scan=None):
"""do a single voltage scan from voltage1 to voltage2
"""
if line_to_scan is None:
self.log.error('Voltage scanning logic needs a line to scan!')
return -1
try:
# scan of a single line
counts_on_scan_line = self._scanning_device.scan_line(line_to_scan)
return counts_on_scan_line.transpose()[0]
except Exception as e:
self.log.error('The scan went wrong, killing the scanner.')
self.stop_scanning()
self.sigScanNextLine.emit()
raise e
def kill_scanner(self):
"""Closing the scanner device.
@return int: error code (0:OK, -1:error)
"""
try:
self._scanning_device.close_scanner()
self._scanning_device.close_scanner_clock()
except Exception as e:
self.log.exception('Could not even close the scanner, giving up.')
raise e
try:
if self._scanning_device.module_state.can('unlock'):
self._scanning_device.module_state.unlock()
except:
self.log.exception('Could not unlock scanning device.')
return 0
def save_data(self, tag=None, colorscale_range=None, percentile_range=None):
""" Save the counter trace data and writes it to a file.
@return int: error code (0:OK, -1:error)
"""
if tag is None:
tag = ''
self._saving_stop_time = time.time()
filepath = self._save_logic.get_path_for_module(module_name='LaserScanning')
filepath2 = self._save_logic.get_path_for_module(module_name='LaserScanning')
filepath3 = self._save_logic.get_path_for_module(module_name='LaserScanning')
timestamp = datetime.datetime.now()
if len(tag) > 0:
filelabel = tag + '_volt_data'
filelabel2 = tag + '_volt_data_raw_trace'
filelabel3 = tag + '_volt_data_raw_retrace'
else:
filelabel = 'volt_data'
filelabel2 = 'volt_data_raw_trace'
filelabel3 = 'volt_data_raw_retrace'
# prepare the data in a dict or in an OrderedDict:
data = OrderedDict()
data['frequency (Hz)'] = self.plot_x
data['trace count data (counts/s)'] = self.plot_y
data['retrace count data (counts/s)'] = self.plot_y2
data2 = OrderedDict()
data2['count data (counts/s)'] = self.scan_matrix[:self._scan_counter_up, :]
data3 = OrderedDict()
data3['count data (counts/s)'] = self.scan_matrix2[:self._scan_counter_down, :]
parameters = OrderedDict()
parameters['Number of frequency sweeps (#)'] = self._scan_counter_up
parameters['Start Voltage (V)'] = self.scan_range[0]
parameters['Stop Voltage (V)'] = self.scan_range[1]
parameters['Scan speed [V/s]'] = self._scan_speed
parameters['Clock Frequency (Hz)'] = self._clock_frequency
fig = self.draw_figure(
self.scan_matrix,
self.plot_x,
self.plot_y,
self.fit_x,
self.fit_y,
cbar_range=colorscale_range,
percentile_range=percentile_range)
fig2 = self.draw_figure(
self.scan_matrix2,
self.plot_x,
self.plot_y2,
self.fit_x,
self.fit_y,
cbar_range=colorscale_range,
percentile_range=percentile_range)
self._save_logic.save_data(
data,
filepath=filepath,
parameters=parameters,
filelabel=filelabel,
fmt='%.6e',
delimiter='\t',
timestamp=timestamp
)
self._save_logic.save_data(
data2,
filepath=filepath2,
parameters=parameters,
filelabel=filelabel2,
fmt='%.6e',
delimiter='\t',
timestamp=timestamp,
plotfig=fig
)
self._save_logic.save_data(
data3,
filepath=filepath3,
parameters=parameters,
filelabel=filelabel3,
fmt='%.6e',
delimiter='\t',
timestamp=timestamp,
plotfig=fig2
)
self.log.info('Laser Scan saved to:\n{0}'.format(filepath))
return 0
def draw_figure(self, matrix_data, freq_data, count_data, fit_freq_vals, fit_count_vals, cbar_range=None, percentile_range=None):
""" Draw the summary figure to save with the data.
@param: list cbar_range: (optional) [color_scale_min, color_scale_max].
If not supplied then a default of data_min to data_max
will be used.
@param: list percentile_range: (optional) Percentile range of the chosen cbar_range.
@return: fig fig: a matplotlib figure object to be saved to file.
"""
# If no colorbar range was given, take full range of data
if cbar_range is None:
cbar_range = np.array([np.min(matrix_data), np.max(matrix_data)])
else:
cbar_range = np.array(cbar_range)
prefix = ['', 'k', 'M', 'G', 'T']
prefix_index = 0
# Rescale counts data with SI prefix
while np.max(count_data) > 1000:
count_data = count_data / 1000
fit_count_vals = fit_count_vals / 1000
prefix_index = prefix_index + 1
counts_prefix = prefix[prefix_index]
# Rescale frequency data with SI prefix
prefix_index = 0
while np.max(freq_data) > 1000:
freq_data = freq_data / 1000
fit_freq_vals = fit_freq_vals / 1000
prefix_index = prefix_index + 1
mw_prefix = prefix[prefix_index]
# Rescale matrix counts data with SI prefix
prefix_index = 0
while np.max(matrix_data) > 1000:
matrix_data = matrix_data / 1000
cbar_range = cbar_range / 1000
prefix_index = prefix_index + 1
cbar_prefix = prefix[prefix_index]
# Use qudi style
plt.style.use(self._save_logic.mpl_qd_style)
# Create figure
fig, (ax_mean, ax_matrix) = plt.subplots(nrows=2, ncols=1)
ax_mean.plot(freq_data, count_data, linestyle=':', linewidth=0.5)
# Do not include fit curve if there is no fit calculated.
if max(fit_count_vals) > 0:
ax_mean.plot(fit_freq_vals, fit_count_vals, marker='None')
ax_mean.set_ylabel('Fluorescence (' + counts_prefix + 'c/s)')
ax_mean.set_xlim(np.min(freq_data), np.max(freq_data))
matrixplot = ax_matrix.imshow(
matrix_data,
cmap=plt.get_cmap('inferno'), # reference the right place in qd
origin='lower',
vmin=cbar_range[0],
vmax=cbar_range[1],
extent=[
np.min(freq_data),
np.max(freq_data),
0,
self.number_of_repeats
],
aspect='auto',
interpolation='nearest')
ax_matrix.set_xlabel('Frequency (' + mw_prefix + 'Hz)')
ax_matrix.set_ylabel('Scan #')
# Adjust subplots to make room for colorbar
fig.subplots_adjust(right=0.8)
# Add colorbar axis to figure
cbar_ax = fig.add_axes([0.85, 0.15, 0.02, 0.7])
# Draw colorbar
cbar = fig.colorbar(matrixplot, cax=cbar_ax)
cbar.set_label('Fluorescence (' + cbar_prefix + 'c/s)')
# remove ticks from colorbar for cleaner image
cbar.ax.tick_params(which='both', length=0)
# If we have percentile information, draw that to the figure
if percentile_range is not None:
cbar.ax.annotate(str(percentile_range[0]),
xy=(-0.3, 0.0),
xycoords='axes fraction',
horizontalalignment='right',
verticalalignment='center',
rotation=90
)
cbar.ax.annotate(str(percentile_range[1]),
xy=(-0.3, 1.0),
xycoords='axes fraction',
horizontalalignment='right',
verticalalignment='center',
rotation=90
)
cbar.ax.annotate('(percentile)',
xy=(-0.3, 0.5),
xycoords='axes fraction',
horizontalalignment='right',
verticalalignment='center',
rotation=90
)
return fig