-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui_confocal_map.py
1341 lines (1057 loc) · 52.4 KB
/
gui_confocal_map.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
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Thu May 21 15:27:02 2020
A GUI for taking the maps
@author: Childresslab, Michael
"""
import numpy as np
import spinmob as sm
from spinmob import egg
import traceback
_p = traceback.print_last #Very usefull command to use for getting the last-not-printed error
import time
from converter import Converter # For converting the pattern for counting
# Debug stuff.
_debug_enabled = False
def _debug(*a):
if _debug_enabled:
s = []
for x in a: s.append(str(x))
print(', '.join(s))
class GUIMap(egg.gui.Window):
"""
GUI for mapping hte count as we change the x-y position.
We can also dive into Z.
This gui maps the counts by interfacing with the gui of the counts.
Therefore it needs the GUICounts.
"""
def __init__(self, fpga, name="Mapper", size=[1300,600]):
"""
Initialize
fpga:
"FPGA_api" object from api_fpga.py.
This is the object shared amoung the GUIs for controlling the fpga.
The session of the fpga must already be open.
"""
_debug('GUIMap:__init__')
_debug('Make each day your masterpiece. – John Wooden')
# Run the basic stuff for the initialization
egg.gui.Window.__init__(self, title=name, size=size)
# Take possession of the GUIs. Mouahahaha...
self.fpga = fpga
# Some attibute
self.is_scanning = False # Weither or not the scan is running
self.list_databox_scans =[] # Store the informations for many slices
self.nb_of_slice = 0 # Actual number of stored slice
self.nb_max_slice = 5 # Maximum number of slice to store
self.label_slice_date = '' # Label shown on the slice
# Fill up the GUI
self.initialize_GUI()
def initialize_GUI(self):
"""
Fill up the GUI
"""
_debug('GUIMap: initialize_GUI')
# Place a buttong for the scan
self.button_scan = self.place_object(egg.gui.Button())
self.button_scan.set_text('Scan :D')
self.button_scan.set_style('background-color: rgb(0, 200, 0);')
self.connect(self.button_scan.signal_clicked, self.button_scan_clicked)
# Place a buttong for selecting a subregion
self.is_selecting_subregion = False #Weither or not the button is selected or not
self.button_subregion = self.place_object(egg.gui.Button(), alignment=1)
self.button_subregion.set_text('Select a \nsubregion ;)')
self.button_subregion.set_style('background-color: rgb(0, 200, 0);')
self.connect(self.button_subregion.signal_clicked, self.button_subregion_clicked)
# Place a buttong for autosetting the region
self.button_autoregion = self.place_object(egg.gui.Button(), alignment=1)
self.button_autoregion.set_text('AutoSet region')
self.button_autoregion.set_style('background-color: rgb(0, 200, 0);')
self.connect(self.button_autoregion.signal_clicked, self.button_autoregion_clicked)
# Place a buttong for replacing the cross at the middle of the map
self.button_center_cursor = self.place_object(egg.gui.Button(), alignment=1)
self.button_center_cursor.set_text('Center Cursor')
self.button_center_cursor.set_style('background-color: rgb(0, 200, 0);')
self.connect(self.button_center_cursor.signal_clicked, self.button_center_cursor_clicked)
# Place a buttong for saving the scans
self.button_save_scans = self.place_object(egg.gui.Button(), alignment=1)
self.button_save_scans.set_text('Save scans :)')
self.connect(self.button_save_scans.signal_clicked, self.button_save_scans_clicked)
# Place a buttong for loading the scans
self.button_load_scans = self.place_object(egg.gui.Button(), alignment=1)
self.button_load_scans.set_text('Load scan :D')
self.connect(self.button_load_scans.signal_clicked, self.button_load_scans_clicked)
self.new_autorow()
# Place a progres bar
self.progress_bar = egg.pyqtgraph.Qt.QtWidgets.QProgressBar()
self.progress_bar.setRange(0, 100)
self.progress_bar.setValue(0)
self.place_object(self.progress_bar,alignment=1)
# Place a label
self.label_progress = egg.gui.Label('We have the best scanner on the market.')
self.place_object(self.label_progress, column_span=3)
self.new_autorow()
# Place the dictionay three for all the parameters
self.treeDic_settings = egg.gui.TreeDictionary(autosettings_path='setting_map')
self.place_object(self.treeDic_settings, column=6, column_span=3)
self.treeDic_settings.add_parameter('Wait_after_AOs', 1,
type='float', step=1,
bounds=[0,None], suffix='us',
tip='Time to wait after the AOs set.')
self.treeDic_settings.add_parameter('Count_time', 1,
type='float', step=1,
bounds=[0,None], suffix='ms',
tip='Count time during the scan')
self.treeDic_settings.add_parameter('AO_x', 2,
type='int', step=1,
bounds=[0,7],
tip='Which AO control x')
self.treeDic_settings.add_parameter('AO_y', 3,
type='int', step=1,
bounds=[0,7],
tip='Which AO control y')
self.treeDic_settings.add_parameter('AO_z', 4,
type='int', step=1,
bounds=[0,7],
tip='Which AO control z')
self.treeDic_settings.add_parameter('X_min', -5,
type='float', step=1,
bounds=[-10,10], suffix='V',
tip='Minimum voltage to scan in the x direction.')
self.treeDic_settings.add_parameter('X_max', 5,
type='float', step=1,
bounds=[-10,10], suffix='V',
tip='Maximum voltage to scan in the x direction.')
self.treeDic_settings.add_parameter('Nb_point_X', 100,
type='int', step=1,
bounds=[0, None],
tip='Number of point to scan in the x direction.')
self.treeDic_settings.add_parameter('Y_min', -5,
type='float', step=1,
bounds=[-10,10], suffix='V',
tip='Minimum voltage to scan in the y direction.')
self.treeDic_settings.add_parameter('Y_max', 5,
type='float', step=1,
bounds=[-10,10], suffix='V',
tip='Maximum voltage to scan in the y direction.')
self.treeDic_settings.add_parameter('Nb_point_Y', 100,
type='int', step=1,
bounds=[0, None],
tip='Number of point to scan in the y direction.')
self.treeDic_settings.add_parameter('Slice_shown', 0,
type='int',
suffix=' /%d'%(self.nb_max_slice-1),
bounds=[0, self.nb_max_slice-1],
tip='Which scan to shown')
list_colormap = PersonalColorMap().get_list_colormaps()
self.treeDic_settings.add_parameter('Colormap', 0,
type='list', values=list_colormap)
self.treeDic_settings.add_parameter('Set_aspect', False,
type='bool',
tip='Weither or not to set the axis to scale. ')
self.treeDic_settings.add_parameter('yfactor_aspect_ratio', 1,
type='float', step=0.1,
bounds=[None,None],
tip='Factor by which we stretch the yaxis')
self.list_scanning_mode = ['Sawtooth', 'Snake', 'Random', 'Diagonal_sweep', 'Spiral']
self.treeDic_settings.add_parameter('Scanning_mode', 0,
type='list',
values=self.list_scanning_mode,
tip='On which kind of path to scan. \nSee the methods "scan_row_..." for more information.')
# Some connections
self.treeDic_settings.connect_signal_changed('Slice_shown', self.update_slice_shown)
self.treeDic_settings.connect_signal_changed('Colormap', self.update_colormap)
self.treeDic_settings.connect_signal_changed('Set_aspect', self.update_image)
self.treeDic_settings.connect_signal_changed('yfactor_aspect_ratio', self.update_image)
# Create the slider for the z position before placing it, because a method needs it before,
self.slider_z_position = egg.gui.Slider(bounds=(-10,10),
autosettings_path='map_slider_z',
steps=1000)
#TODO place the initialization of the map in a separated method
# PLace the Ultimate map
# Create the ImgaeView within this.
# Prepare the container, expecially the axis.
self.plot_item = egg.pyqtgraph.PlotItem()
self.plot_image = egg.pyqtgraph.ImageView(view=self.plot_item)
self.place_object(self.plot_image, column=0, column_span=6,
row_span=5,alignment=0)
self.original_width = self.plot_image.geometry().bottom() # Record the original width for the rescaling
# Add the cross-hair lines
self.vLine = egg.pyqtgraph.InfiniteLine(angle=90, movable=True,
pen=(0,255,255))
self.hLine = egg.pyqtgraph.InfiniteLine(angle=0, movable=True,
pen=(0,255,255))
self.plot_image.addItem(self.vLine, ignoreBounds=True)
self.plot_image.addItem(self.hLine, ignoreBounds=True)
self.connect(self.vLine.sigPositionChanged, self.pos_vLine_changed)
self.connect(self.hLine.sigPositionChanged, self.pos_hLine_changed)
self.is_pos_vLine_changing = False # A usuful boolean to avoid infinite call
self.is_pos_hLine_changing = False # A usuful boolean to avoid infinite call
# Add a ROI
self.ptROI = egg.pyqtgraph.ROI((0,0),pen=(0,255,255))
self.plot_image.addItem(self.ptROI)
self.connect(self.ptROI.sigRegionChanged, self.pos_ptROI_changed)
# A label for the slice
self.label_slice_date = time.ctime(time.time())
self.textitem_slice = egg.pyqtgraph.TextItem(text=self.label_slice_date,
color=(200, 200, 255),
fill=(0, 0, 255, 100))
self.plot_image.addItem(self.textitem_slice)
# Add a dummy map
x = np.linspace(1, 20, 100)
y = np.linspace(1, 20, 100)
X,Y = np.meshgrid(x,y)
self.Z = np.cos(X*2)*np.sin(Y)*X*Y
# self.Z = -(X**2+Y**2-np.arctan(Y*0.01/X)**2)
# The following are the typical steps for making the image.
self.match_attributes_with_gui()
self.initialize_image()
self.update_image()
self.store_scan()
# Place the slider
self.new_autorow()
self.place_object(self.slider_z_position, row=10,row_span=3, column_span=3)
self.set_row_stretch(1,10)
self.slider_z_position.event_changed = self.slider_z_position_changed
def slider_z_position_changed(self, value):
"""
Update the value of the voltage on the AOs.
Also Automatically write it on the FPGA.
This is for when we change the value in the setting or with the cross.
"""
_debug('GUIMap: slider_z_position_changed')
#TODO Add the x and y voltage from the cross
AOz = self.treeDic_settings['AO_z']
Vz = value
# Prepare the fpga with the values
self.fpga.prepare_AOs([AOz], [Vz])
# Run the FPGA for updating its settings
# It gonna run also the pre-existing pulse sequence. Hopefully it's
# gonna be the counter.
self.fpga.lets_go_FPGA()
# Call the event to say "hey, stuff changed on the fpga"
self.event_fpga_change()
def update_slice_shown(self):
"""
Show the slice corresponding to the indices
"""
_debug('GUIMap: update_slice_shown ', self.treeDic_settings['Slice_shown'] )
n = self.treeDic_settings['Slice_shown']
# Ajdust the value if it exceed the stored data
if n>=self.nb_of_slice:
self.treeDic_settings['Slice_shown'] = self.nb_of_slice-1
n = self.treeDic_settings['Slice_shown']
# Get the slice
self.databox_scan = self.list_databox_scans[n]
# Extract the data
self.label_slice_date = self.databox_scan.h('date')
self.Vxmin = self.databox_scan.h('Vx_min')
self.Vxmax = self.databox_scan.h('Vx_max')
self.Nx = self.databox_scan.h('Nx')
self.Vymin = self.databox_scan.h('Vy_min')
self.Vymax = self.databox_scan.h('Vy_max')
self.Ny = self.databox_scan.h('Ny')
self.Z = np.zeros([self.Nx, self.Ny])
# Add each column
for i in range(self.Ny):
self.Z[i] = self.databox_scan['Col%d'%i]
# Update the image
self.initialize_image()
self.update_image()
def update_colormap(self):
"""
Update the color of the image to fit the settings
"""
_debug('GUIMap: update_colormap ')
name = self.treeDic_settings['Colormap']
mycmap = PersonalColorMap().get_colormap(name)
self.plot_image.setColorMap(mycmap)
def button_scan_clicked(self):
"""
Start or stop the scan
"""
_debug('GUIMap: button_scan_clicked')
if self.is_scanning == False:
# Note that we are scanning
self.button_scan.set_text('Stop :O')
self.button_scan.set_style('background-color: rgb(255, 100, 100);')
self.is_scanning = True
# Remove the subregion if there is one
if self.is_selecting_subregion:
self.button_subregion_clicked()
# Run the scan
self.run_scan()
else:
# Stop to take counts if we were taking counts
self.is_scanning = False # We stopped to scan
self.button_scan.set_text('Scan :D')
self.button_scan.set_style('background-color: rgb(0, 200, 0);')
def button_subregion_clicked(self):
"""
This button is for allowing to select a subregion.
If we click, we show a ROI for selcting the subregion to select for
the next scan.
If we click again, we remove this subregion from the view.
"""
_debug('GUIMap: button_subregion_clicked')
if self.is_selecting_subregion == False:
# Note that we are scanning
self.button_subregion.set_text('Remove the selection\nof subregion')
self.button_subregion.set_style('background-color: rgb(255, 100, 100);')
self.is_selecting_subregion = True
# Place the ROI for selection the subregion
size_x = (self.Vxmax-self.Vxmin)/2
size_y = (self.Vymax-self.Vymin)/2
self.ROI_subregion = egg.pyqtgraph.RectROI((self.vLine.getXPos()-size_x/2,
self.hLine.getYPos() -size_y/2),
pen=(50,255,50),
size=(size_x,size_y))
self.plot_image.addItem(self.ROI_subregion)
# Connect it
self.connect(self.ROI_subregion.sigRegionChanged, self.ROI_subregion_change)
else:
# Stop to take counts if we were taking counts
self.is_selecting_subregion = False # We stopped to scan
self.button_subregion.set_text('Select a \nsubregion ;)')
self.button_subregion.set_style('background-color: rgb(0, 200, 0);')
# Remove the ROI
self.plot_image.removeItem(self.ROI_subregion)
def button_autoregion_clicked(self):
"""
Auto set the region to scan.
TODO Add the option to preset the auto region ?
"""
_debug('GUIMap: button_autoregion_clicked')
# Update the settings
self.treeDic_settings['X_min'] = -10
self.treeDic_settings['Y_min'] = -10
self.treeDic_settings['X_max'] = 10
self.treeDic_settings['Y_max'] = 10
def button_center_cursor_clicked(self):
"""
Recenter the cursor at the middle of the map.
"""
_debug('GUIMap: button_center_cursor_clicked')
# Update the cross
xmin = self.treeDic_settings['X_min']
ymin = self.treeDic_settings['Y_min']
xmax = self.treeDic_settings['X_max']
ymax = self.treeDic_settings['Y_max']
self.ptROI.setPos(( (xmin+xmax)/2 , (ymin+ymax)/2) )
#TODO remove the "s" at scans in the name of the method !
def button_save_scans_clicked(self):
"""
Save the actually shown slice
"""
_debug('GUIMap: button_save_scans_clicked')
# Note the information on the actually scan
# We do not store it, for note poping the last scan.
N_col = len(self.Z) # Number of columns
self.databox_scan.insert_header('date', self.label_slice_date)
self.databox_scan.insert_header('Vx_min', self.Vxmin)
self.databox_scan.insert_header('Vx_max', self.Vxmax)
self.databox_scan.insert_header('Nx', self.Nx)
self.databox_scan.insert_header('Vy_min', self.Vymin)
self.databox_scan.insert_header('Vy_max', self.Vymax)
self.databox_scan.insert_header('Ny', self.Ny)
self.databox_scan.insert_header('N_col', N_col)
# Add all the element of the three dictionnary
for key in self.treeDic_settings.get_keys():
# Add each element of the dictionnary three
self.databox_scan.insert_header(key , self.treeDic_settings[key])
# Add each column
for i in range(N_col):
col = self.Z[i]
self.databox_scan['Col%d'%i] = col
# This will open a dialog window for saving the databox
self.databox_scan.save_file()
def button_load_scans_clicked(self):
"""
Load scans.
It load what is saved with the complement method "button_save_scans_clicked".
If there is more than one databox selected, it gonna take them as the
list of databox's
"""
_debug('GUIMap: button_load_scans_clicked')
# Get the databob
ds = sm.data.load_multiple(text='Load one or more scans')
if len(ds)>1:
# If we selected more than one databox, set them as the new list
self.list_databox_scans = ds
else:
# If we selected only one databox, overid the last scan
self.list_databox_scans[-1] = ds[0]
# # Store the image
# self.store_scan()
# Take the last element to show.
self.databox_scan = self.list_databox_scans[-1]
# Extract the data
self.label_slice_date = self.databox_scan.h('date')
self.Vxmin = self.databox_scan.h('Vx_min')
self.Vxmax = self.databox_scan.h('Vx_max')
self.Nx = self.databox_scan.h('Nx')
self.Vymin = self.databox_scan.h('Vy_min')
self.Vymax = self.databox_scan.h('Vy_max')
self.Ny = self.databox_scan.h('Ny')
# Set the tree dictionnary, in case that we would like to rescan the
# Same parameter
self.treeDic_settings['X_min'] = self.Vxmin
self.treeDic_settings['Y_min'] = self.Vymin
self.treeDic_settings['X_max'] = self.Vxmax
self.treeDic_settings['Y_max'] = self.Vymax
self.treeDic_settings['Nb_point_X'] = self.Nx
self.treeDic_settings['Nb_point_Y'] = self.Ny
self.Z = np.zeros([self.Ny, self.Nx])
# Add each column
for i in range(self.Ny):
self.Z[i] = self.databox_scan['Col%d'%i]
# Update the image
self.initialize_image()
self.update_image()
def ROI_subregion_change(self):
"""
Adjust the boundary of the subregion to be scan
"""
# _debug('GUIMap: ROI_subregion_change')
# Get the boundary
xmin = self.ROI_subregion.viewPos().x()
ymin = self.ROI_subregion.viewPos().y()
xmax = xmin + self.ROI_subregion.size()[0]
ymax = ymin + self.ROI_subregion.size()[1]
# Update the settings
self.treeDic_settings['X_min'] = xmin
self.treeDic_settings['Y_min'] = ymin
self.treeDic_settings['X_max'] = xmax
self.treeDic_settings['Y_max'] = ymax
def prepare_acquisition_pulse(self):
"""
Prepare the acquisition of counts.
It prepare the pulse pattern and set the wait time.
"""
_debug('GUICounts: prepare_acquisition')
# Create the data array from counting
# Prepare DIO1 in state 1
self.fpga.prepare_DIOs([1], [1])
# Get the actual DIOs, because there might be other DIOs open.
self.dio_states = self.fpga.get_DIO_states()
# Convert the instruction into the data array
conver = Converter() # Load the converter object.
self.count_time_ms = self.treeDic_settings['Count_time']
nb_ticks = self.count_time_ms*1e3/(conver.tickDuration)
self.data_array = conver.convert_into_int32([(nb_ticks, self.dio_states)])
# Update the wait time
wait_AO_time = self.treeDic_settings['Wait_after_AOs']
self.fpga.prepare_wait_time(wait_AO_time)
# Send the data_array to the FPGA
self.fpga.prepare_pulse(self.data_array)
def match_attributes_with_gui(self):
"""
make the attributes to match with the settings on the gui
"""
_debug('GUIMap: match_attributes_with_gui')
# Determine some parameters for the image
self.Vxmin = self.treeDic_settings['X_min']
self.Vxmax = self.treeDic_settings['X_max']
self.Nx = self.treeDic_settings['Nb_point_X']
self.Vymin = self.treeDic_settings['Y_min']
self.Vymax = self.treeDic_settings['Y_max']
self.Ny = self.treeDic_settings['Nb_point_Y']
self.Vz = self.slider_z_position.get_value()
def initialize_image(self):
"""
Prepare the image to be shown with the actual parameters.
This sets up the axis, the ROIs, etc.
TODO Reorganiz the way we initialize and update the image
"""
_debug('GUIMap: initialize_image')
# Set the axis
self.plot_item.setLabel('bottom', text='Vx')
self.plot_item.setLabel('left', text='Vy')
# Get the right scaling
self.scale_x = (self.Vxmax- self.Vxmin)/self.Nx
self.scale_y = (self.Vymax- self.Vymin)/self.Ny
# Update the ROI for the center of the cursor
self.size_ptROI_x = (self.Vxmax-self.Vxmin)/20
self.size_ptROI_y = (self.Vymax-self.Vymin)/20
self.pos_ptROI_x = self.vLine.getXPos()
self.pos_ptROI_y = self.hLine.getYPos()
x = self.pos_ptROI_x-self.size_ptROI_x/2
y = self.pos_ptROI_y-self.size_ptROI_y/2
self.ptROI.setPos((x,y))
self.ptROI.setSize( (self.size_ptROI_x,self.size_ptROI_y) )
# Update the cross-hair
self.vLine.setPos((self.Vxmin+self.Vxmax)/2)
self.hLine.setPos((self.Vymin+self.Vymax)/2)
# Update the text label
self.textitem_slice.setText(self.label_slice_date)
self.textitem_slice.setPos(self.Vxmin, self.Vymin)
def update_image(self):
"""
Update the map with the actual Z
"""
_debug('GUIMap: update_image')
# Set the ratio according to the wishes
value = self.treeDic_settings['Set_aspect']
self.plot_image.view.setAspectLocked(value) # Input True for having the scaling right.
# Multiply the y axis by the factor
yfactor = self.treeDic_settings['yfactor_aspect_ratio']
# self.transform =self.plot_image.view.transform()
# self.t1 = self.transform.shear(1,yfactor)
self.plot_image.setImage(self.Z.T,
pos=(self.Vxmin, self.Vymin),
scale =(self.scale_x, self.scale_y*yfactor))
# scale/pan the view to fit the image.
self.plot_image.autoRange()
# Update the color map
self.update_colormap()
def store_scan(self):
"""
Store the information of the actual shown scan.
"""
_debug('GUIMap: store_scan')
# Create the databox
self.databox_scan = sm.data.databox()
N_col = len(self.Z) # Number of columns
self.databox_scan.insert_header('date', self.label_slice_date)
self.databox_scan.insert_header('Vx_min', self.Vxmin)
self.databox_scan.insert_header('Vx_max', self.Vxmax)
self.databox_scan.insert_header('Nx', self.Nx)
self.databox_scan.insert_header('Vy_min', self.Vymin)
self.databox_scan.insert_header('Vy_max', self.Vymax)
self.databox_scan.insert_header('Ny', self.Ny)
self.databox_scan.insert_header('N_col', N_col)
# Add each column
for i in range(N_col):
col = self.Z[i]
self.databox_scan['Col%d'%i] = col
if self.nb_of_slice > self.nb_max_slice:
# Pop the oldest stored slice if we exceed the nb of slices
_debug('GUIMap: store_scan: poping oldest scan.')
self.list_databox_scans.pop(0)
# Append the new scan
self.list_databox_scans.append(self.databox_scan)
# Update the number of slice
self.nb_of_slice = len(self.list_databox_scans)
def run_scan(self):
"""
Ultimate function which scans !
"""
_debug('GUIMap: run_scan')
# First initiate the scan
# Prepare the pulse sequence
self.prepare_acquisition_pulse()
# Take the attributes in the gui.
self.match_attributes_with_gui()
# Prepare the image
self.initialize_image()
# Note the date of the scan
self.label_slice_date = time.ctime(time.time())
# Prepare the x and y position to scan
self.xs = np.linspace(self.Vxmin, self.Vxmax, self.Nx)
self.ys = np.linspace(self.Vymin, self.Vymax, self.Ny)
self.X,self.Y = np.meshgrid(self.xs,self.ys)
self.Z = np.zeros(np.shape(self.X))
# Note the AOs to take
self.AOx = self.treeDic_settings['AO_x']
self.AOy = self.treeDic_settings['AO_y']
self.AOz = self.treeDic_settings['AO_z']
# Prepare the progress bar
self.progress_bar.setValue(0)
# Now we can scan, according to which type of scan to do
if self.treeDic_settings['Scanning_mode'] == 'Sawtooth':
self.scan_row_sawtooth()
elif self.treeDic_settings['Scanning_mode'] == 'Snake':
self.scan_row_snake()
elif self.treeDic_settings['Scanning_mode'] == 'Spiral':
self.scan_spiral()
elif self.treeDic_settings['Scanning_mode'] == 'Diagonal_sweep':
self.scan_diagonal_sweep()
elif self.treeDic_settings['Scanning_mode'] == 'Random':
self.scan_random_points()
# At this poin the scan is completed or stopped
# Store the scan
self.store_scan()
# recenter the Cursor
self.button_center_cursor_clicked()
# Update the GUI
if self.is_scanning:
# This stop the scan if self.is_scanning = True
self.button_scan_clicked()
def scan_row_sawtooth(self):
"""
This scans each row and update the image after each row.
It is called sawtooth because, after each row, the voltage in x is coming
back to the initial x voltage.
"""
_debug('GUIMap: scan_row_sawtooth')
# Start the scan
self.row = 0
while self.is_scanning and self.row<len(self.ys):
# Note the time at which the row starts
self.time_row_start = time.time()
_debug('sawtooth Row ', self.row)
# Get the voltrage in Y
Vy = self.ys[self.row]
for i in range(len(self.xs)):
# Get the voltage in x
Vx = self.xs[i]
# Update the voltage of the AOs
self.list_AOs = [self.AOx, self.AOy, self.AOz]
self.list_Vs = [Vx, Vy, self.Vz]
self.fpga.prepare_AOs(self.list_AOs, self.list_Vs)
# Get the count, finally ;)
# Two step: runt he pulse pattern and get the counts.
self.fpga.run_pulse() # This will also write the AOs
self.counts = self.fpga.get_counts()[0]
self.counts_per_sec = 1e3*self.counts/self.count_time_ms
# Since zero is boring, let's add something
# image = self.counts + np.random.poisson( np.abs(1000*np.cos(Vx*Vy*0.5)) )
# self.Z[self.row][i]= image
self.Z[self.row][i] = self.counts_per_sec
# Update the image after each row
self.update_image()
# Note how much time it takes for the row
self.time_row_elapsed = time.time() - self.time_row_start
# Update the progress bar
progress = 100*(self.row+1)/len(self.ys)
self.progress_bar.setValue(progress)
# Update the label for the progress
nb_row_remaining = len(self.ys) - (self.row+1)
sec = self.time_row_elapsed*nb_row_remaining
self.label_progress.set_text('Time remaining: %.2f s'%sec)
# Update the row for the next iteration
self.row +=1
# Allow the GUI to update. This is important to avoid freezing of the GUI inside loops
self.process_events()
def scan_row_snake(self):
"""
This scans each row and update the image after each row.
It is called snake because, after each row, the voltage in x start at
it's previous value, instead of coming back to the other extrema like
the sawtooth.
"""
_debug('GUIMap: scan_row_snake')
# Start the scan
self.row = 0
while self.is_scanning and self.row<len(self.ys):
# Note the time at which the row starts
self.time_row_start = time.time()
_debug('Sssssssnake Row ', self.row)
# Get the voltrage in Y
Vy = self.ys[self.row]
# Note the order of the index to scan depending on which row we
# have for doing the snake scan.
if self.row%2 == 0:
i_s = range(len( self.xs ))
else:
i_s = np.flip(range(len( self.xs )))
# Scan this set of index
for i in i_s:
# Get the voltage in x
Vx = self.xs[i]
# Update the voltage of the AOs
self.list_AOs = [self.AOx, self.AOy, self.AOz]
self.list_Vs = [Vx, Vy, self.Vz]
self.fpga.prepare_AOs(self.list_AOs, self.list_Vs)
# Get the count, finally ;)
# Two step: runt he pulse pattern and get the counts.
self.fpga.run_pulse() # This will also write the AOs
self.counts = self.fpga.get_counts()[0]
self.counts_per_sec = 1e3*self.counts/self.count_time_ms
# Since zero is boring, let's add something
# image = self.counts + np.random.poisson( np.abs(1000*np.cos(Vx*Vy*0.5)) )
# self.Z[self.row][i]= image
self.Z[self.row][i] = self.counts_per_sec
# Update the image after each row
self.update_image()
# Note how much time it takes for the row
self.time_row_elapsed = time.time() - self.time_row_start
# Update the progress bar
progress = 100*(self.row+1)/len(self.ys)
self.progress_bar.setValue(progress)
# Update the label for the progress
nb_row_remaining = len(self.ys) - (self.row+1)
sec = self.time_row_elapsed*nb_row_remaining
self.label_progress.set_text('Time remaining: %.2f s'%sec)
# Update the row for the next iteration
self.row +=1
# Allow the GUI to update. This is important to avoid freezing of the GUI inside loops
self.process_events()
def scan_random_points(self):
"""
This scans random points on the map.
"""
_debug('GUIMap: scan_random_points')
self.Ntotal = self.Nx*self.Ny # Total number of point to scan
# create a list for labelling ALL the points
self.list_pts = range(self.Ntotal) # This
# Scan until all the points got scanned
self.iteration = 0
while self.is_scanning and len(self.list_pts)>0:
# Note the time at which a batch starts
self.time_row_start = time.time()
self.iteration +=1
_debug('GUIMap: scan_random_points', self.iteration)
# To speed up, do batches before updating
for i in range(self.Nx):
# Pick a random number
self.pt_choosen = np.random.choice(self.list_pts)
# Extract the corresponding row and column
self.row = int(self.pt_choosen/self.Nx)
self.column = int(self.pt_choosen - self.Nx*self.row)
# Delete this number from the list for the next pickup
self.index_to_delete = np.where(self.list_pts==self.pt_choosen)[0][0]
self.list_pts = np.delete(self.list_pts, self.index_to_delete)
# # for debugging
# print('self.index_to_delete = ',self.index_to_delete)
# print('len(self.list_pts) = ',len(self.list_pts))
# Get the voltrage in Y
Vy = self.ys[self.row]
# Get the voltage in x
Vx = self.xs[self.column]
# Update the voltage of the AOs
self.list_AOs = [self.AOx, self.AOy, self.AOz]
self.list_Vs = [Vx, Vy, self.Vz]
self.fpga.prepare_AOs(self.list_AOs, self.list_Vs)
# Get the count, finally ;)
# Two step: runt he pulse pattern and get the counts.
self.fpga.run_pulse() # This will also write the AOs
self.counts = self.fpga.get_counts()[0]
self.counts_per_sec = 1e3*self.counts/self.count_time_ms
# # Since zero is boring, let's add something
# image = 100+np.random.poisson( np.abs(10000*np.cos(Vx*Vy*0.5)) )
# self.Z[self.row][self.column]= image
self.Z[self.row][self.column] = self.counts_per_sec
# Update the image after each row
self.update_image()
# Note how much time it takes for the row
self.time_row_elapsed = time.time() - self.time_row_start
# Update the progress bar
progress = 100*(self.iteration)/self.Ny
self.progress_bar.setValue(progress)
# Update the label for the progress
nb_iter_remaining = self.Ny - self.iteration # Number of iteration remaining
sec = self.time_row_elapsed*nb_iter_remaining
self.label_progress.set_text('Time remaining: %.2f s'%sec)
# Allow the GUI to update. This is important to avoid freezing of the GUI inside loops
self.process_events()
def scan_diagonal_sweep(self):
"""
This scans by sweeping in diagonal.
#TODO complete this ! Scan the other half and deal with different
# Size for x and y
"""
_debug('GUIMap: scan_diagonal_sweep')
#Note the sizes
Nx = len(self.xs)
Ny = len(self.ys)
# Note the indices to scan
i_s = range(Nx)
j_s = range(Ny)
# Estimate the total area for the progress bar
total_area = Nx*Ny
# Note the time at which we start
self.time_start = time.time()
# Start the scan
self.diagonal = 0
# Scan each diagonal
while self.is_scanning and self.diagonal<Ny:
_debug('Diagonal :D ', self.diagonal)
# Do the diagonal !
for k in range(self.diagonal+1):
i = i_s[k]
j = j_s[self.diagonal-k]
Vx = self.xs[i]
Vy = self.ys[j]
# Update the voltage of the AOs
self.list_AOs = [self.AOx, self.AOy, self.AOz]
self.list_Vs = [Vx, Vy, self.Vz]
self.fpga.prepare_AOs(self.list_AOs, self.list_Vs)
# Get the count, finally ;)
# Two step: runt he pulse pattern and get the counts.
self.fpga.run_pulse() # This will also write the AOs
self.counts = self.fpga.get_counts()[0]
self.counts_per_sec = 1e3*self.counts/self.count_time_ms
# Since zero is boring, let's add something
# image = self.counts + np.random.poisson( np.abs(1000*np.cos(Vx*Vy*0.5)) )
# self.Z[j][i]= 100+np.random.poisson( np.abs(10000*np.cos(Vx*Vy*0.5)) )
self.Z[j][i] = self.counts_per_sec
# Update the image after each row
self.update_image()
# Note how much time it tooks so far
self.time_elapsed = time.time() - self.time_start
# Updathe the labels
# Update the progress bar
area_swept = (self.diagonal+1)**2/2
progress = 100*area_swept/total_area
self.progress_bar.setValue(progress)
# Update the label for the progress
t_tot = self.time_elapsed*total_area/area_swept
t_remaining= t_tot - self.time_elapsed
self.label_progress.set_text('Time remaining: %.2f s / %.2f'%(t_remaining,t_tot))
# Update the row for the next iteration
self.diagonal +=1
# Allow the GUI to update. This is important to avoid freezing of the GUI inside loops
self.process_events()
def scan_spiral(self):