-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
997 lines (805 loc) · 45.1 KB
/
main.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
import argparse
import datetime
import importlib.resources
import logging
import tkinter as tk
from threading import Thread
from tkinter import messagebox
from typing import Any, Protocol, Optional, Callable, List
import matplotlib
import nidaqmx
import numpy as np
import yaml
from matplotlib import pyplot as plt
from matplotlib.backend_bases import MouseEvent
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import qt3utils.nidaq
import qt3utils.pulsers.pulseblaster
from qt3utils.applications.qt3scan.controller import (
QT3ScanConfocalApplicationController,
QT3ScanHyperSpectralApplicationController,
STANDARD_COUNT_AGGREGATION_METHODS
)
from qt3utils.applications.controllers.utils import make_popup_window_and_take_threaded_action
from qt3utils.applications.qt3scan.interface import (
QT3ScanDAQControllerInterface,
QT3ScanPositionControllerInterface,
QT3ScanApplicationControllerInterface,
)
matplotlib.use('Agg')
parser = argparse.ArgumentParser(description='QT3Scan', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-v', '--verbose', type=int, default=2, help='0 = quiet, 1 = info, 2 = debug.')
args = parser.parse_args()
logger = logging.getLogger(__name__)
logging.basicConfig()
if args.verbose == 0:
logger.setLevel(logging.WARNING)
if args.verbose == 1:
logger.setLevel(logging.INFO)
if args.verbose == 2:
logger.setLevel(logging.DEBUG)
NIDAQ_DAQ_DEVICE_NAME = 'NIDAQ Edge Counter'
RANDOM_DATA_DAQ_DEVICE_NAME = 'Random Counter'
PRINCETON_SPECTROMETER_DAQ_DEVICE_NAME = 'Princeton Spectrometer'
ANDOR_SPECTROMETER_DAQ_DEVICE_NAME = 'Andor Spectrometer'
RANDOM_SPECTROMETER_DAQ_DEVICE_NAME = 'Random Spectrometer'
DEFAULT_DAQ_DEVICE_NAME = NIDAQ_DAQ_DEVICE_NAME
CONTROLLER_PATH = 'qt3utils.applications.controllers'
STANDARD_CONTROLLERS = {
NIDAQ_DAQ_DEVICE_NAME: {
'yaml': 'nidaq_edge_counter.yaml',
'application_controller_class': QT3ScanConfocalApplicationController
},
PRINCETON_SPECTROMETER_DAQ_DEVICE_NAME: {
'yaml': 'princeton_spectrometer.yaml',
'application_controller_class': QT3ScanHyperSpectralApplicationController
},
ANDOR_SPECTROMETER_DAQ_DEVICE_NAME: {
'yaml': 'andor_spectrometer.yaml',
'application_controller_class': QT3ScanHyperSpectralApplicationController
},
RANDOM_DATA_DAQ_DEVICE_NAME: {
'yaml': 'random_data_generator.yaml',
'application_controller_class': QT3ScanConfocalApplicationController
},
RANDOM_SPECTROMETER_DAQ_DEVICE_NAME: {
'yaml': 'random_spectrometer.yaml',
'application_controller_class': QT3ScanHyperSpectralApplicationController
}
}
# Hyper Spectral Imaging would add the following to STANDARD_CONTROLLERS
# PRINCETON_SPECTROMETER_DAQ_DEVICE_NAME = 'Princeton Spectrometer'
# PRINCETON_SPECTROMETER_DAQ_DEVICE_NAME: {'yaml':'princeton_spectromter.yaml',
# 'application_controller_class': QT3ScanHyperSpectralApplicationController}
CONFIG_FILE_APPLICATION_NAME = 'QT3Scan'
CONFIG_FILE_POSITION_CONTROLLER = 'PositionController'
CONFIG_FILE_DAQ_CONTROLLER = 'DAQController'
class ScanImage:
def __init__(self, mplcolormap: str = 'gray'):
self.fig, self.ax = plt.subplots()
self.cbar = None
self.cmap = mplcolormap
self.fig.canvas.mpl_connect('button_press_event', self.onclick)
self.ax.set_xlabel('x position (um)')
self.ax.set_ylabel('y position (um)')
self.log_data = False
self.plot_count_rate = True
self.pointer_line2d = None
self.position_line2d = None
self.app_controller_step_size = 0
self.xmin = None
self.ymin = None
def update(self, app_controller: QT3ScanApplicationControllerInterface) -> None:
if len(app_controller.scanned_count_rate) == 0:
return
if self.plot_count_rate:
data = app_controller.scanned_count_rate
else:
data = app_controller.scanned_raw_counts
if self.log_data:
data = np.log10(data)
data[np.isinf(data)] = 0 # protect against +-inf
# we must retain these values for callback functions (feels a bit hacky)
self.app_controller_step_size = app_controller.step_size
self.xmin = app_controller.xmin
self.ymin = app_controller.ymin
# shift the extent so that position centers are directly aligned with data
# rather than aligned on bin edges
self.artist = self.ax.imshow(data, origin='lower',
cmap=self.cmap,
extent=[app_controller.xmin - app_controller.step_size / 2.0,
app_controller.xmax + app_controller.step_size / 2.0,
app_controller.ymin - app_controller.step_size / 2.0,
app_controller.current_y - app_controller.step_size / 2.0])
if self.cbar is None:
self.cbar: plt.Colorbar = self.fig.colorbar(self.artist, ax=self.ax)
self.cbar.formatter.set_useOffset(False)
else:
self.cbar.update_normal(self.artist)
if self.log_data is False:
self.cbar.formatter.set_powerlimits((0, 3))
self.ax.set_xlabel('x position (um)')
self.ax.set_ylabel('y position (um)')
def reset(self) -> None:
self.ax.cla()
self.pointer_line2d = None
self.position_line2d = None
self.app_controller_step_size = 0
self.xmin = None
self.ymin = None
def set_onclick_callback(self, func: Callable) -> None:
"""
The callback function should expect three arguments
* matplotlib.backend_bases.MouseEvent
* pixel_x
* pixel_y
where pixel_x and pixel_y correspond to the pixel
index of the data
"""
self.onclick_callback = func
def set_rightclick_callback(self, func: Callable) -> None:
"""
The callback function should expect three arguments
* matplotlib.backend_bases.MouseEvent
* pixel_x
* pixel_y
where pixel_x and pixel_y correspond to the pixel
index of the data
"""
self.rightclick_callback = func
def update_pointer_indicator(self, x_position: float, y_position: float) -> None:
"""
Updates the pointer marker on the scan image to show a proposed new position on the image.
"""
if self.pointer_line2d is not None:
self.pointer_line2d[0].set_data([[x_position], [y_position]])
else:
self.pointer_line2d = self.ax.plot(x_position, y_position, 'yx', label='pointer')
def update_position_indicator(self, x_position: float, y_position: float) -> None:
"""
Updates the position marker on the scan image to show the current position on the image.
"""
if self.position_line2d is not None:
self.position_line2d[0].set_data([[x_position], [y_position]])
else:
self.position_line2d = self.ax.plot(x_position, y_position, 'ro', label='pos')
def onclick(self, event: MouseEvent) -> None:
if event.inaxes != self.ax:
return
logger.debug(f"Button {event.button} click at: ({event.xdata} microns, {event.ydata}) microns")
if event.xdata is None or event.ydata is None:
logger.debug("Button click outside of scan")
return
if self.xmin is None or self.ymin is None:
logger.debug("No data to display")
return
dist_x = event.xdata + self.app_controller_step_size / 2 - self.xmin # we have to subtract step_size / 2 because the GUI shifts the view.
dist_y = event.ydata + self.app_controller_step_size / 2 - self.ymin
logger.debug(f'Selected position at distance from lower left (x, y): {dist_x, dist_y}')
index_x = int(dist_x / self.app_controller_step_size)
index_y = int(dist_y / self.app_controller_step_size)
if event.button == 3: # Right click
self.rightclick_callback(event, index_x, index_y)
elif event.button == 1: # Left click
self.onclick_callback(event, index_x, index_y)
self.update_pointer_indicator(event.xdata, event.ydata)
self.fig.canvas.draw()
class SidePanel:
def __init__(self, application):
frame = tk.Frame(application.root_window)
frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
row = 0
tk.Label(frame, text="Scan Settings", font='Helvetica 16').grid(row=row, column=0, pady=10)
row += 1
tk.Label(frame, text="x range (um)").grid(row=row, column=0)
self.x_min_entry = tk.Entry(frame, width=10)
self.x_max_entry = tk.Entry(frame, width=10)
self.x_min_entry.grid(row=row, column=1)
self.x_max_entry.grid(row=row, column=2)
row += 1
tk.Label(frame, text="y range (um)").grid(row=row, column=0)
self.y_min_entry = tk.Entry(frame, width=10)
self.y_max_entry = tk.Entry(frame, width=10)
self.y_min_entry.grid(row=row, column=1)
self.y_max_entry.grid(row=row, column=2)
row += 1
tk.Label(frame, text="step size (um)").grid(row=row, column=0)
self.step_size_entry = tk.Entry(frame, width=10)
self.step_size_entry.insert(10, 1.0)
self.step_size_entry.grid(row=row, column=1)
row += 1
tk.Label(frame, text="set z (um)").grid(row=row, column=0)
self.z_entry_text = tk.DoubleVar()
self.z_entry = tk.Entry(frame, width=10, textvariable=self.z_entry_text)
self.z_entry.grid(row=row, column=1)
self.go_to_z_button = tk.Button(frame, text="Go To Z")
self.go_to_z_button.grid(row=row, column=2)
row += 1
self.startButton = tk.Button(frame, text="Start Scan")
self.startButton.grid(row=row, column=0)
self.stopButton = tk.Button(frame, text="Stop Scan")
self.stopButton.grid(row=row, column=1)
self.saveScanButton = tk.Button(frame, text="Save Scan")
self.saveScanButton.grid(row=row, column=2)
row += 1
self.popOutScanButton = tk.Button(frame, text="Popout Scan")
self.popOutScanButton.grid(row=row, column=0)
self.loadScanButton = tk.Button(frame, text="Load Scan")
self.loadScanButton.grid(row=row, column=1)
row += 1
tk.Label(frame, text="Position").grid(row=row, column=0, pady=10)
self.go_to_x_position_text = tk.DoubleVar()
self.go_to_y_position_text = tk.DoubleVar()
tk.Entry(frame, textvariable=self.go_to_x_position_text, width=7).grid(row=row, column=1, pady=5)
tk.Entry(frame, textvariable=self.go_to_y_position_text, width=7).grid(row=row, column=2, pady=5)
row += 1
self.gotoAfterScanBoolVar = tk.BooleanVar(value=False)
tk.Checkbutton(frame, text="'Go to' after scan", variable=self.gotoAfterScanBoolVar).grid(
row=row, column=0, columnspan=2)
self.gotoButton = tk.Button(frame, text="Go To Position")
self.gotoButton.grid(row=row, column=2)
# row += 1
# tk.Label(frame, text="Optimize Pos", font='Helvetica 16').grid(row=row, column=0, pady=10)
row += 1
tk.Label(frame, text="Optimize Range (um)").grid(row=row, column=0, columnspan=2)
self.optimize_range_entry = tk.Entry(frame, width=10)
self.optimize_range_entry.insert(5, 2)
self.optimize_range_entry.grid(row=row, column=2)
row += 1
tk.Label(frame, text="Optimize StepSize (um)").grid(row=row, column=0, columnspan=2)
self.optimize_step_size_entry = tk.Entry(frame, width=10)
self.optimize_step_size_entry.insert(5, 0.25)
self.optimize_step_size_entry.grid(row=row, column=2)
row += 1
self.optimize_x_button = tk.Button(frame, text="Optimize X")
self.optimize_x_button.grid(row=row, column=0)
self.optimize_y_button = tk.Button(frame, text="Optimize Y")
self.optimize_y_button.grid(row=row, column=1)
self.optimize_z_button = tk.Button(frame, text="Optimize Z")
self.optimize_z_button.grid(row=row, column=2)
row += 1
tk.Label(frame, text="DAQ Settings", font='Helvetica 16').grid(row=row, column=0, pady=10)
row += 1
self.controller_option = tk.StringVar(frame)
self.controller_option.set(DEFAULT_DAQ_DEVICE_NAME)
# todo - TkOptionMenu doesn't have a way, that I know of,
# to modify the callback after instantiation. Therefore,
# for now, we need to pass the app_controller to this class
# so that it can be used in the callback when a hardware option is selected.
self.controller_menu = tk.OptionMenu(frame,
self.controller_option,
*STANDARD_CONTROLLERS.keys(),
command=application.load_controller_from_name)
self.controller_menu.grid(row=row, column=0, columnspan=3)
row += 1
self.daq_config_button = tk.Button(frame, text="Data Acquisition Config")
self.daq_config_button.grid(row=row, column=0, columnspan=3)
row += 1
self.position_controller_config_button = tk.Button(frame, text="Position Controller Config")
self.position_controller_config_button.grid(row=row, column=0, columnspan=3)
row += 1
self.config_from_yaml_button = tk.Button(frame, text="Load YAML Config")
self.config_from_yaml_button.grid(row=row, column=0, columnspan=3)
# todo -- package view settings into a separate GUI breakout window
row += 1
tk.Label(frame, text="View Settings", font='Helvetica 16').grid(row=row, column=0, pady=10)
row += 1
self.set_color_map_button = tk.Button(frame, text="Set Color")
self.set_color_map_button.grid(row=row, column=0, pady=2)
self.mpl_color_map_entry = tk.Entry(frame, width=10)
self.mpl_color_map_entry.insert(10, 'gray')
self.mpl_color_map_entry.grid(row=row, column=1, pady=2)
self.log10Button = tk.Button(frame, text="Log10")
self.log10Button.grid(row=row, column=2, pady=2)
row += 1
self.count_rate_button = tk.Button(frame, text="Toggle: Count Rate/Raw Counts")
self.count_rate_button.grid(row=row, column=0, columnspan=3, pady=2)
row += 1
self.set_raw_background_counts_button = tk.Button(frame, text="Set raw BG")
self.set_raw_background_counts_button.grid(row=row, column=0)
self.raw_background_counts_entry = tk.Entry(frame, width=10)
self.raw_background_counts_entry.insert(10, '0')
self.raw_background_counts_entry.grid(row=row, column=1, columnspan=2)
row += 1
tk.Label(frame, text="Spectral Confocal", font='Helvetica 12').grid(row=row, column=0, pady=2)
row += 1
self.set_filter_range_button = tk.Button(frame, text="Set Range")
self.set_filter_range_button.grid(row=row, column=0)
self.range_min_entry = tk.Entry(frame, width=10)
self.range_min_entry.insert(10, f'{-np.inf}')
self.range_min_entry.grid(row=row, column=1)
self.range_max_entry = tk.Entry(frame, width=10)
self.range_max_entry.insert(10, f'{np.inf}')
self.range_max_entry.grid(row=row, column=2)
row += 1
self.set_count_aggregation_button = tk.Button(frame, text="Set Aggregation")
self.set_count_aggregation_button.grid(row=row, column=0)
self.count_aggregation_option = tk.StringVar(frame)
self.count_aggregation_option.set(list(STANDARD_COUNT_AGGREGATION_METHODS.keys())[0])
self.count_aggregation_menu = tk.OptionMenu(frame,
self.count_aggregation_option,
*STANDARD_COUNT_AGGREGATION_METHODS.keys(),
)
self.count_aggregation_menu.grid(row=row, column=1, columnspan=2)
row += 1
tk.Label(frame, text='', ).grid(row=row, column=0, columnspan=3, pady=10)
def update_go_to_position(self,
x: Optional[float] = None,
y: Optional[float] = None,
z: Optional[float] = None) -> None:
if x is not None:
self.go_to_x_position_text.set(np.round(x, 4))
if y is not None:
self.go_to_y_position_text.set(np.round(y, 4))
if z is not None:
self.z_entry_text.set(np.round(z, 4))
def mpl_onclick_callback(self, mpl_event: MouseEvent, index_x: int, index_y: int) -> None:
if mpl_event.xdata and mpl_event.ydata:
self.update_go_to_position(mpl_event.xdata, mpl_event.ydata)
def set_scan_range(self, scan_range: List[float]) -> None:
self.x_min_entry.insert(10, scan_range[0])
self.x_max_entry.insert(10, scan_range[1])
self.y_min_entry.insert(10, scan_range[0])
self.y_max_entry.insert(10, scan_range[1])
class MainApplicationView:
def __init__(self, application):
frame = tk.Frame(application.root_window)
frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.scan_view = ScanImage()
self.sidepanel = SidePanel(application)
self.scan_view.set_onclick_callback(self.sidepanel.mpl_onclick_callback)
self.canvas = FigureCanvasTkAgg(self.scan_view.fig, master=frame)
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2Tk(self.canvas, frame)
toolbar.update()
self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.canvas.draw()
@property
def controller_menu(self) -> tk.OptionMenu:
return self.sidepanel.controller_menu
@property
def controller_option(self) -> tk.StringVar:
return self.sidepanel.controller_option
@controller_option.setter
def controller_option(self, value):
self.sidepanel.controller_option.set(value)
@property
def daq_config_button(self) -> tk.Button:
return self.sidepanel.daq_config_button
@property
def position_controller_config_button(self) -> tk.Button:
return self.sidepanel.position_controller_config_button
@property
def config_from_yaml_button(self) -> tk.Button:
return self.sidepanel.config_from_yaml_button
def set_scan_range(self, scan_range: List[float]) -> None:
self.sidepanel.set_scan_range(scan_range)
def show_optimization_plot(self, title: str,
old_opt_value: float,
new_opt_value: float,
x_vals: np.ndarray,
y_vals: np.ndarray,
fit_coeff: np.ndarray = None) -> None:
"""
Constructs a new window with a plot of the optimization data.
title: title of the window
old_opt_value: the old optimized value
new_opt_value: the new optimized value
x_vals: the x values of the data
y_vals: the measured y values of the data
fit_coeff: the fit coefficients of the function qt3utils.datagenerators.piezoscanner.gauss
"""
win = tk.Toplevel()
win.title(title)
fig, ax = plt.subplots()
ax.set_xlabel('position (um)')
ax.set_ylabel('count rate (Hz)')
ax.plot(x_vals, y_vals, label='data')
ax.ticklabel_format(style='sci', scilimits=(0, 3))
ax.axvline(old_opt_value, linestyle='--', color='red', label=f'old position {old_opt_value:.2f}')
ax.axvline(new_opt_value, linestyle='-', color='blue', label=f'new position {new_opt_value:.2f}')
if fit_coeff is not None:
y_fit = qt3utils.datagenerators.piezoscanner.gauss(x_vals, *fit_coeff)
ax.plot(x_vals, y_fit, label='fit', color='orange')
ax.legend()
canvas = FigureCanvasTkAgg(fig, master=win)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2Tk(canvas, win)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
canvas.draw()
class MainTkApplication:
def __init__(self, application_controller_name: str):
self.root_window = tk.Tk()
self.view = MainApplicationView(self)
self.view.controller_option = application_controller_name
self.view.sidepanel.startButton.bind("<Button>", lambda e: self.start_scan())
self.view.sidepanel.stopButton.bind("<Button>", lambda e: self.stop_scan())
self.view.sidepanel.log10Button.bind("<Button>", lambda e: self.log_scan_image())
self.view.sidepanel.count_rate_button.bind("<Button>", lambda e: self.toggle_count_rate())
self.view.sidepanel.gotoButton.bind("<Button>", lambda e: self.go_to_position())
self.view.sidepanel.go_to_z_button.bind("<Button>", lambda e: self.go_to_z())
self.view.sidepanel.saveScanButton.bind("<Button>", lambda e: self.save_scan())
self.view.sidepanel.popOutScanButton.bind("<Button>", lambda e: self.pop_out_scan())
self.view.sidepanel.loadScanButton.bind("<Button>", lambda e: self.load_scan())
self.view.sidepanel.set_color_map_button.bind("<Button>", lambda e: self.set_color_map())
self.view.sidepanel.set_raw_background_counts_button.bind("<Button>", lambda e: self.set_raw_background_counts())
self.view.sidepanel.set_filter_range_button.bind("<Button>", lambda e: self.set_filter_range())
self.view.sidepanel.set_count_aggregation_button.bind("<Button>", lambda e: self.set_count_aggregation_option())
self.view.sidepanel.optimize_x_button.bind("<Button>", lambda e: self.optimize('x'))
self.view.sidepanel.optimize_y_button.bind("<Button>", lambda e: self.optimize('y'))
self.view.sidepanel.optimize_z_button.bind("<Button>", lambda e: self.optimize('z'))
self.view.config_from_yaml_button.bind("<Button>", lambda e: self.configure_from_yaml())
self.load_controller_from_name(application_controller_name)
scan_range = [self.application_controller.position_controller.minimum_allowed_position,
self.application_controller.position_controller.maximum_allowed_position]
self.view.set_scan_range(scan_range)
self.root_window.protocol("WM_DELETE_WINDOW", self.on_closing)
self.scan_thread = None
self.optimized_position = {'x': 0, 'y': 0, 'z': -1}
self.optimized_position['z'] = self.application_controller.position_controller.get_current_position()[2]
self.optimized_position['z'] = self.optimized_position['z'] if self.optimized_position['z'] is not None else 0 # protects against None value returned by position controller when it cannot connect to hardware
self.view.sidepanel.z_entry_text.set(np.round(self.optimized_position['z'], 4))
def _open_yaml_config_for_controller(self, controller_name: str) -> dict:
with importlib.resources.path(CONTROLLER_PATH, STANDARD_CONTROLLERS[controller_name]['yaml']) as yaml_path:
logger.info(f"opening config file: {yaml_path}")
with open(yaml_path, 'r') as yaml_file:
config = yaml.safe_load(yaml_file)
return config
def _load_controller_from_dict(self, config: dict, a_protocol: Protocol) -> Any:
"""
Dynamically imports the module and instantiates the class specified in the config dictionary.
Class the class configure method if it exists.
"""
# Dynamically import the module
module = importlib.import_module(config['import_path'])
logger.debug(f"loading {config['import_path']}")
# Dynamically instantiate the class
cls = getattr(module, config['class_name'])
logger.debug(f"instantiating {config['class_name']}")
controller = cls(logger.level)
logger.debug(f"asserting {config['class_name']} of proper type {a_protocol}")
assert isinstance(controller, a_protocol)
# configure the controller
# all controllers *should* have a configure method
logger.debug(f"calling {a_protocol} configure method")
controller.configure(config['configure'])
return controller
def load_controller_from_name(self, application_controller_name: str) -> None:
"""
Loads the default yaml configuration file for the application controller.
Should be called during instantiation of this class and should be the callback
function for the support controller pull-down menu in the side panel.
When called from the pull-down menu, a popup window opens to prevent GUI freezes
due to long connection times to the devices (e.g., for spectrometers).
"""
# check if last scan was saved
if hasattr(self, 'application_controller'):
if self.application_controller.data_saved_once is False:
stored_data_shape = np.prod(np.shape(self.application_controller.scanned_count_rate))
data_shape_product = np.prod(stored_data_shape)
if data_shape_product > 0:
proceed = messagebox.askyesno("WARNING: Scan NOT SAVED",
"The previous scan was not saved. Are you sure you want to change "
"the controller? All DATA will be LOST.")
if not proceed:
self.view.controller_option.set(self.active_application_controller_option)
return
logger.info(f"loading {application_controller_name}")
config = self._open_yaml_config_for_controller(application_controller_name)
if hasattr(self, 'application_controller'): # if this is not the first initialization of the main application
make_popup_window_and_take_threaded_action( # handles potential GUI freezes
self.root_window,
f'Connecting...',
f'Connecting to {application_controller_name}. Please wait...',
lambda: self._build_controllers_from_config_dict(config, application_controller_name)
)
else: # There is no GUI, so there is no fear
self._build_controllers_from_config_dict(config, application_controller_name)
def _build_controllers_from_config_dict(self, config: dict, controller_name: str) -> None:
# load the position controller from dict
pos_config = config[CONFIG_FILE_APPLICATION_NAME][CONFIG_FILE_POSITION_CONTROLLER]
position_controller = self._load_controller_from_dict(pos_config, QT3ScanPositionControllerInterface)
# load the data acquisition model from dict
daq_config = config[CONFIG_FILE_APPLICATION_NAME][CONFIG_FILE_DAQ_CONTROLLER]
daq_controller = self._load_controller_from_dict(daq_config, QT3ScanDAQControllerInterface)
ControllerClass = STANDARD_CONTROLLERS[controller_name]['application_controller_class']
self.application_controller = ControllerClass(position_controller, daq_controller, logger.level)
# bind buttons to controllers
self.view.scan_view.set_rightclick_callback(self.application_controller.scan_image_rightclick_event)
self.view.position_controller_config_button.bind("<Button>", lambda e: self.application_controller.position_controller.configure_view(self.root_window))
self.view.daq_config_button.bind("<Button>", lambda e: self.application_controller.daq_controller.configure_view(self.root_window))
self.active_application_controller_option = self.view.controller_option.get()
def configure_from_yaml(self) -> None:
"""
This method launches a GUI window to allow the user to select a yaml file to configure the data controller.
This does instantiate a new hardware controller classes and calls configure.
"""
filetypes = (
('YAML', '*.yaml'),
)
afile = tk.filedialog.askopenfile(filetypes=filetypes, defaultextension='.yaml')
if afile is None:
return # selection was canceled.
config = yaml.safe_load(afile)
afile.close()
# Checking that we are loading files that match the current application controller
current_daq_controller_class_name = self.application_controller.daq_controller.__class__.__name__
yaml_daq_controller_class_name = config[CONFIG_FILE_APPLICATION_NAME][CONFIG_FILE_DAQ_CONTROLLER]['class_name']
if current_daq_controller_class_name != yaml_daq_controller_class_name:
logger.warning(f"The current DAQ controller class ({current_daq_controller_class_name}) "
f"does not match the DAQ controller's class name in the "
f"YAML file ({yaml_daq_controller_class_name}).\n"
f"Loading configuration from YAML was aborted.")
return
current_pos_controller_class_name = self.application_controller.position_controller.__class__.__name__
yaml_pos_controller_class_name = \
config[CONFIG_FILE_APPLICATION_NAME][CONFIG_FILE_POSITION_CONTROLLER]['class_name']
if current_pos_controller_class_name != yaml_pos_controller_class_name:
logger.warning(f"The current position controller class ({current_pos_controller_class_name}) "
f"does not match the position controller's class name in the "
f"YAML file ({yaml_pos_controller_class_name}).\n"
f"Loading configuration from YAML was aborted.")
return
make_popup_window_and_take_threaded_action( # handles potential GUI freezes
self.root_window,
f'Connecting...',
f'Connecting to {self.view.controller_option.get()}. Please wait...',
lambda: self._build_controllers_from_config_dict(config, self.view.controller_option.get())
)
def run(self) -> None:
self.root_window.title("QT3Scan: Piezo Controlled NIDAQ Digital Count Rate Scanner")
self.root_window.deiconify()
self.root_window.mainloop()
def go_to_position(self) -> None:
x = self.view.sidepanel.go_to_x_position_text.get()
y = self.view.sidepanel.go_to_y_position_text.get()
self.application_controller.position_controller.go_to_position(x, y)
self.optimized_position['x'] = x
self.optimized_position['y'] = y
self.view.scan_view.update_position_indicator(x, y)
if len(self.application_controller.scanned_count_rate) > 0:
self.view.scan_view.update(self.application_controller)
self.view.canvas.draw()
def go_to_z(self) -> None:
self.application_controller.position_controller.go_to_position(z=self.view.sidepanel.z_entry_text.get())
self.optimized_position['z'] = self.view.sidepanel.z_entry_text.get()
def set_color_map(self) -> None:
proposed_cmap = self.view.sidepanel.mpl_color_map_entry.get()
if proposed_cmap in plt.colormaps():
self.view.scan_view.cmap = proposed_cmap
else:
logger.error(f"Color map {proposed_cmap} not found in matplotlib colormaps:")
logger.error(f'{plt.colormaps()}')
if self.application_controller.still_scanning() is False:
self.view.scan_view.update(self.application_controller)
self.view.canvas.draw()
def set_raw_background_counts(self):
bg_entry_value = self.view.sidepanel.raw_background_counts_entry.get()
try:
raw_bg_counts = float(bg_entry_value)
except Exception as e:
logger.warning(f'{bg_entry_value} is invalid. Background counts did not change:{self.application_controller.raw_bg_counts}.')
return
self.application_controller.raw_bg_counts = raw_bg_counts
if self.application_controller.still_scanning() is False:
self.view.scan_view.update(self.application_controller)
self.view.canvas.draw()
def set_filter_range(self) -> None:
if not hasattr(self.application_controller, 'filter_view_range'):
logger.debug(f'Filter Range not available for this DAQ. Nothing happens.')
return
filter_min = np.float64(self.view.sidepanel.range_min_entry.get())
filter_max = np.float64(self.view.sidepanel.range_max_entry.get())
self.application_controller.filter_view_range = filter_min, filter_max
if self.application_controller.still_scanning() is False:
self.view.scan_view.update(self.application_controller)
self.view.canvas.draw()
def set_count_aggregation_option(self):
if not hasattr(self.application_controller, 'counts_aggregation_option'):
logger.debug(f'Counts aggregation option is not available for this DAQ. Nothing happens.')
return
self.application_controller.counts_aggregation_option = self.view.sidepanel.count_aggregation_option.get()
if self.application_controller.still_scanning() is False:
self.view.scan_view.update(self.application_controller)
self.view.canvas.draw()
def log_scan_image(self) -> None:
self.view.scan_view.log_data = not self.view.scan_view.log_data
if self.application_controller.still_scanning() is False:
self.view.scan_view.update(self.application_controller)
self.view.canvas.draw()
def toggle_count_rate(self):
self.view.scan_view.plot_count_rate = not self.view.scan_view.plot_count_rate
if self.application_controller.still_scanning() is False:
self.view.scan_view.update(self.application_controller)
self.view.canvas.draw()
def stop_scan(self) -> None:
self.application_controller.stop()
def pop_out_scan(self) -> None:
"""
Creates a new TKinter window with the data from the current scan. This allows researchers
to retain scan image in a separate window and run subsequent scans.
"""
win = tk.Toplevel()
win.title(f'Scan {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')
new_scan_view = ScanImage(self.view.scan_view.cmap)
new_scan_view.update(self.application_controller)
canvas = FigureCanvasTkAgg(new_scan_view.fig, master=win)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2Tk(canvas, win)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
canvas.draw()
def _scan_thread_function(self, xmin: float, xmax: float, ymin: float, ymax: float, step_size: float) -> None:
try:
self.application_controller.set_scan_range(xmin, xmax, ymin, ymax)
self.application_controller.step_size = step_size
self.application_controller.reset() # clears the data
self.application_controller.start() # starts the DAQ
self.application_controller.set_to_starting_position() # moves the stage to starting position
while self.application_controller.still_scanning():
self.application_controller.scan_x()
self.application_controller.move_y()
self.view.scan_view.update(self.application_controller)
self.view.canvas.draw()
self.application_controller.post_stop()
except nidaqmx.errors.DaqError as e:
logger.warning(e)
logger.warning('Check for other applications using resources.')
except ValueError as e:
logger.warning(e)
logger.warning('Check your configuration! You may have entered a value that is out of range')
except RuntimeError as e:
logger.warning(e)
logger.warning('Check your configuration! One or more of your devices were not properly initialized.')
finally:
self.view.sidepanel.startButton.config(state=tk.NORMAL)
self.view.sidepanel.go_to_z_button.config(state=tk.NORMAL)
self.view.sidepanel.gotoButton.config(state=tk.NORMAL)
self.view.sidepanel.saveScanButton.config(state=tk.NORMAL)
self.view.sidepanel.popOutScanButton.config(state=tk.NORMAL)
self.view.sidepanel.loadScanButton.config(state=tk.NORMAL)
self.view.sidepanel.optimize_x_button.config(state=tk.NORMAL)
self.view.sidepanel.optimize_y_button.config(state=tk.NORMAL)
self.view.sidepanel.optimize_z_button.config(state=tk.NORMAL)
self.view.sidepanel.controller_menu.config(state=tk.NORMAL)
self.view.sidepanel.daq_config_button.config(state=tk.NORMAL)
self.view.sidepanel.position_controller_config_button.config(state=tk.NORMAL)
self.view.sidepanel.config_from_yaml_button.config(state=tk.NORMAL)
if self.view.sidepanel.gotoAfterScanBoolVar.get():
self.go_to_position()
def start_scan(self) -> None:
if self.application_controller.data_saved_once is False:
stored_data_shape = np.prod(np.shape(self.application_controller.scanned_count_rate))
data_shape_product = np.prod(stored_data_shape)
if data_shape_product > 0:
proceed = messagebox.askyesno("WARNING: Scan NOT SAVED",
"The previous scan was not saved. Are you sure you want to proceed with "
"a new scan? All DATA will be LOST.")
if not proceed:
return
self.view.sidepanel.startButton.config(state=tk.DISABLED)
self.view.sidepanel.go_to_z_button.config(state=tk.DISABLED)
self.view.sidepanel.gotoButton.config(state=tk.DISABLED)
self.view.sidepanel.saveScanButton.config(state=tk.DISABLED)
self.view.sidepanel.popOutScanButton.config(state=tk.DISABLED)
self.view.sidepanel.loadScanButton.config(state=tk.DISABLED)
self.view.sidepanel.optimize_x_button.config(state=tk.DISABLED)
self.view.sidepanel.optimize_y_button.config(state=tk.DISABLED)
self.view.sidepanel.optimize_z_button.config(state=tk.DISABLED)
self.view.sidepanel.controller_menu.config(state=tk.DISABLED)
self.view.sidepanel.daq_config_button.config(state=tk.DISABLED)
self.view.sidepanel.position_controller_config_button.config(state=tk.DISABLED)
self.view.sidepanel.config_from_yaml_button.config(state=tk.DISABLED)
# clear the figure
self.view.scan_view.reset()
# get the scan settings
xmin = float(self.view.sidepanel.x_min_entry.get())
xmax = float(self.view.sidepanel.x_max_entry.get())
ymin = float(self.view.sidepanel.y_min_entry.get())
ymax = float(self.view.sidepanel.y_max_entry.get())
args = [xmin, xmax, ymin, ymax]
args.append(float(self.view.sidepanel.step_size_entry.get()))
# get this value from the DAQ controller
self.scan_thread = Thread(target=self._scan_thread_function,
args=args)
self.scan_thread.start()
def save_scan(self) -> None:
afile = tk.filedialog.asksaveasfilename(filetypes=self.application_controller.allowed_file_save_formats(),
defaultextension=self.application_controller.default_file_format())
if afile is None or afile == '':
return # selection was canceled.
logger.info(f'Saving data to {afile}')
self.application_controller.save_scan(afile)
def load_scan(self):
afile = tk.filedialog.askopenfilename(filetypes=self.application_controller.allowed_file_save_formats(),
defaultextension=self.application_controller.default_file_format())
if afile is None or afile == '':
return # selection was canceled.
logger.info(f'Loading data from {afile}')
self.application_controller.load_scan(afile)
if hasattr(self.application_controller, 'filter_view_range'):
self.view.sidepanel.range_min_entry.delete(0, tk.END)
self.view.sidepanel.range_min_entry.insert(0, str(self.application_controller.filter_view_range[0]))
self.view.sidepanel.range_max_entry.delete(0, tk.END)
self.view.sidepanel.range_max_entry.insert(0, str(self.application_controller.filter_view_range[1]))
if hasattr(self.application_controller, 'counts_aggregation_option'):
self.view.sidepanel.count_aggregation_option.set(self.application_controller.counts_aggregation_option)
if self.application_controller.still_scanning() is False:
self.view.scan_view.update(self.application_controller)
self.view.canvas.draw()
def _optimize_thread_function(self, axis: str, central: float, range: float, step_size: float) -> None:
"""
This function is called by the optimize function. It is not intended to be called directly.
"""
try:
data, axis_vals, opt_pos, coeff = self.application_controller.optimize_position(axis,
central,
range,
step_size)
self.optimized_position[axis] = opt_pos
self.application_controller.position_controller.go_to_position(**{axis: opt_pos})
self.view.show_optimization_plot(f'Optimize {axis}',
central,
self.optimized_position[axis],
axis_vals,
data,
coeff)
self.view.sidepanel.update_go_to_position(**{axis: self.optimized_position[axis]})
self.view.scan_view.update_position_indicator(self.optimized_position['x'],
self.optimized_position['y'])
self.view.canvas.draw()
except nidaqmx.errors.DaqError as e:
logger.info(e)
logger.info('Check for other applications using resources. If not, you may need to restart the application.')
except NotImplementedError as e:
logger.info(e)
finally:
self.view.sidepanel.startButton.config(state=tk.NORMAL)
self.view.sidepanel.stopButton.config(state=tk.NORMAL)
self.view.sidepanel.go_to_z_button.config(state=tk.NORMAL)
self.view.sidepanel.gotoButton.config(state=tk.NORMAL)
self.view.sidepanel.saveScanButton.config(state=tk.NORMAL)
self.view.sidepanel.popOutScanButton.config(state=tk.NORMAL)
self.view.sidepanel.loadScanButton.config(state=tk.NORMAL)
self.view.sidepanel.optimize_x_button.config(state=tk.NORMAL)
self.view.sidepanel.optimize_y_button.config(state=tk.NORMAL)
self.view.sidepanel.optimize_z_button.config(state=tk.NORMAL)
self.view.sidepanel.controller_menu.config(state=tk.NORMAL)
self.view.sidepanel.daq_config_button.config(state=tk.NORMAL)
self.view.sidepanel.position_controller_config_button.config(state=tk.NORMAL)
self.view.sidepanel.config_from_yaml_button.config(state=tk.NORMAL)
def optimize(self, axis: str) -> None:
opt_range = float(self.view.sidepanel.optimize_range_entry.get())
opt_step_size = float(self.view.sidepanel.optimize_step_size_entry.get())
old_optimized_value = self.optimized_position[axis]
self.view.sidepanel.startButton.config(state=tk.DISABLED)
self.view.sidepanel.stopButton.config(state=tk.DISABLED)
self.view.sidepanel.go_to_z_button.config(state=tk.DISABLED)
self.view.sidepanel.gotoButton.config(state=tk.DISABLED)
self.view.sidepanel.saveScanButton.config(state=tk.DISABLED)
self.view.sidepanel.popOutScanButton.config(state=tk.DISABLED)
self.view.sidepanel.loadScanButton.config(state=tk.DISABLED)
self.view.sidepanel.optimize_x_button.config(state=tk.DISABLED)
self.view.sidepanel.optimize_y_button.config(state=tk.DISABLED)
self.view.sidepanel.optimize_z_button.config(state=tk.DISABLED)
self.view.sidepanel.controller_menu.config(state=tk.DISABLED)
self.view.sidepanel.daq_config_button.config(state=tk.DISABLED)
self.view.sidepanel.position_controller_config_button.config(state=tk.DISABLED)
self.view.sidepanel.config_from_yaml_button.config(state=tk.DISABLED)
self.optimize_thread = Thread(target=self._optimize_thread_function,
args=(axis, old_optimized_value, opt_range, opt_step_size))
self.optimize_thread.start()
def on_closing(self) -> None:
try:
self.stop_scan()
except Exception as e:
logger.debug(e)
finally:
self.root_window.quit()
self.root_window.destroy()
def main():
tkapp = MainTkApplication(DEFAULT_DAQ_DEVICE_NAME)
tkapp.run()
if __name__ == '__main__':
main()