-
Notifications
You must be signed in to change notification settings - Fork 1
/
Zi².py
executable file
·1401 lines (1300 loc) · 72.7 KB
/
Zi².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
# This code has been made by Adrianos SIDIRAS GALANTE for the INL
# region LICENSE (EN)
"""
Copyright or © or Copr. Adrianos SIDIRAS GALANTE
contributor(s) : David ALBERTINI, INL CNRS UMR 5270 (31/08/2020)
[adrianos.sidiras@gmail.com]
This software is a computer program whose purpose is to be able to bypass
the limitation of using the 4 auxiliary signals of the HF2LI by taking
the signals data dirrectly from the HF2LI through the USB port instead of coaxial cables.
The application will communicate with the HF2LI API using Python to obtain
all the demodulators data streams (R, Theta, X, Y, Frequency) we need
and plot as many graphs as we want. It has more specificly been built for PFM with DFRT.
This software is governed by the [CeCILL|CeCILL-B|CeCILL-C] license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the [CeCILL|CeCILL-B|CeCILL-C]
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the [CeCILL|CeCILL-B|CeCILL-C] license and that you accept its terms.
"""
# endregion
# region Imports
import time
import numpy as np
import csv
import zhinst.utils
from zhinst.ziPython import ziListEnum
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from tkinter import *
import tkinter as tk
from tkinter import filedialog
import tkinter.font as tkFont
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from datetime import datetime
from PIL import ImageTk, Image
from gwyfile.objects import GwyContainer, GwyDataField
import os
import sys
"""
Here it's just some imports, the main ones are Zhinst, Matplotlib and Numpy ..
howerver they are all necessary if you want to run the program
"""
# endregion
# region dictionaries and variables definition
current_directory = (os.path.dirname(os.path.realpath(__file__)))
current_directory = current_directory.replace("\\", '/')
signal_paths = [] # setting up the array in which the signal adresses will be stored
frequency_index = []
data = {} # setting up data dictionary
im = {} # setting up ploted images dictionary
"""
here are the default limits of both size of the image in pixels and the nanoscope head quick axis frequency
which corresponds to the "x" axis scanning frequency of a line = trace + retrace
"""
size_min_limit = 31
size_max_limit = 4096
frequency_min_limit = 0.1
frequency_max_limit = 4.1
launch = 1
ani = None # defining ani for animation as a global variable, otherwise it won't work with Tkinter and the GUI
"""
Here are defined the two triggers we'll be using wich are DIO 0 and DIO 1
those can be found at the back of the HF2LI and are to be plugged in with a coaxial cable
"""
end_of_line = '.TrigDio0'
end_of_frame = '.TrigDio1'
entries_fieldnames=['draw','size','freq','trace_mode','color','save_after_image']
# endregion
# region GUI colors theme
# default
light_gray_color = "#333333"
dark_gray_color = "#1e1e1e"
white_color = "#f1f1f1"
accent_color = "#007acc"
connect_color = "#eaa100"
start_color = "#ff830f"
selected_color = "#252526"
#endregion
# region GUI Root declaration and config
"""
Here is the definition of the main window which is called root
we also define the background color and the icon image,
if you delete the image the program will still launch without problem
but i would not recommend it
"""
def shut_down():
try:
daq_module.finish()
daq.disconnect()
except:
pass
else:
pass
if(os.path.isfile(current_directory+'/configs/entries.csv')):
with open(current_directory+'/configs/entries.csv', 'w') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=entries_fieldnames, delimiter=',')
csv_writer.writeheader()
line = {entries_fieldnames[0]:'{}'.format(frames_to_draw_entry.get()), entries_fieldnames[1]:'{}'.format(size_entry.get()),
entries_fieldnames[2]:'{}'.format(frequency_entry.get()), entries_fieldnames[3]:'{}'.format(trace_mode_radio.get()),
entries_fieldnames[4]:'{}'.format(cmap_selected.get()), entries_fieldnames[5]:'{}'.format(save_after_image_checkbox_state.get())}
csv_writer.writerow(line)
if(os.path.isfile(current_directory+'/configs/connexion.csv')):
with open(current_directory+'/configs/connexion.csv', 'w') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=connexion_fieldnames, delimiter=',')
csv_writer.writeheader()
line = {connexion_fieldnames[0]:'{}'.format(autoconnect_checkbox_state.get()), connexion_fieldnames[1]:'{}'.format(advanced_checkbox_state.get()),
connexion_fieldnames[2]:'{}'.format(manual_sample_freq_checkbox_state.get()), connexion_fieldnames[3]:'{}'.format(save_to_gwy_checkbox_state.get()),
connexion_fieldnames[4]:'{}'.format(save_to_txt_checkbox_state.get())}
csv_writer.writerow(line)
sys.exit()
window_state=False
def toggleFullScreen(*args):
global window_state
window_state=not(window_state)
root.attributes('-fullscreen', window_state)
def quitFullScreen(*args):
root.attributes('-fullscreen', False)
root = Tk()
root.title("Zi²") # assign a title to the window
root.configure(background=dark_gray_color)
try:
root.iconphoto(False, tk.PhotoImage(
file=current_directory+'/imgs/logo.png'))
except:
pass
root.config(highlightbackground=dark_gray_color)
fontStyle = tkFont.Font(family="sans-serif", size=16)
root.protocol('WM_DELETE_WINDOW', shut_down)
if("win" in sys.platform):
root.state('zoomed')
else:
root.attributes('-zoomed', True)
root.bind("<F11>", toggleFullScreen)
root.bind("<Escape>", quitFullScreen)
# endregion
# region GUI FRAMES
"""
Here is the definition of all the frames that are in our GUI
Frames act like Window inside a bigger window, they are usefull when it
comes to make some segmentation in the GUI
"""
configuration_frame = LabelFrame(root, padx=0, pady=0, bd=0)
configuration_frame.configure(background=dark_gray_color, fg='#f1f1f1',
highlightbackground=dark_gray_color, highlightcolor=dark_gray_color)
configuration_frame.grid(row=0, column=0, sticky=N+E+W)
ploting_frame = LabelFrame(root, padx=0, pady=0, bd=0)
ploting_frame.configure(background=dark_gray_color, fg='#f1f1f1',
highlightbackground=dark_gray_color, highlightcolor=dark_gray_color)
ploting_frame.grid(row=0, column=1, sticky=N+E+W+S)
toolbar_frame = LabelFrame(ploting_frame, padx=0, pady=0, bd=0)
toolbar_frame.configure(background=accent_color, fg=accent_color,
highlightbackground=accent_color, highlightcolor=accent_color)
toolbar_frame.grid(row=1, column=0, sticky=S+W)
main_frame = LabelFrame(configuration_frame,
text="Demods", padx=0, pady=0)
main_frame.configure(background=dark_gray_color, fg=white_color,
highlightbackground=dark_gray_color, highlightcolor=dark_gray_color)
main_frame.grid(row=0, column=0, columnspan=4, sticky=N+E+W)
settings_frame = LabelFrame(
configuration_frame, text="Settings", padx=0, pady=0)
settings_frame.configure(background=dark_gray_color, fg='#f1f1f1',
highlightbackground=dark_gray_color, highlightcolor=dark_gray_color)
settings_frame.grid(row=1, column=0, columnspan=4, sticky=N+E+W)
save_frame = LabelFrame(configuration_frame, text="Save", padx=0, pady=0)
save_frame.configure(background=dark_gray_color, fg='#f1f1f1',
highlightbackground=dark_gray_color, highlightcolor=dark_gray_color)
save_frame.grid(row=2, column=0, columnspan=4, sticky=N+E+W)
connexion_frame = LabelFrame(
configuration_frame, text="Connexion & more", padx=0, pady=0)
connexion_frame.configure(background=dark_gray_color, fg='#f1f1f1',
highlightbackground=dark_gray_color, highlightcolor=dark_gray_color)
controls_frame = LabelFrame(
configuration_frame, text="Controls", padx=0, pady=0)
controls_frame.configure(background=dark_gray_color, fg='#f1f1f1',
highlightbackground=dark_gray_color, highlightcolor=dark_gray_color)
controls_frame.grid(row=4, column=0, columnspan=4, sticky=N+E+W)
# endregion
# region GUI logs
"""
Here is the logging window, in which you'll be able to see what the program does
and also have an instant feedback and explaination if an error occurs
this works like a print() statment, you can add your on text using the following instruction :
logs_text.insert('end', "your text \n")
the "\n" is not necessary but it is usefull as it works like the Enter key and thus
the text will not print on the same line
"""
def dont_close():
logs.withdraw()
def show_log():
global logs
logs.update()
logs.deiconify()
def save_logs():
save_text = logs_text.get('1.0', END)
now = datetime.now()
day_time = now.strftime("%Y-%m-%d %H-%M-%S")
filename = 'logs at {}'.format(day_time)
adress = '{}/logs/{}'.format(current_directory, filename)
with open(adress, 'w') as csv_file:
csv_file.write(save_text)
def clear_logs():
save_text = logs_text.delete('1.0', END)
logs = Toplevel()
logs.title("logs")
logs.configure(bg=light_gray_color)
logs.iconphoto(False, tk.PhotoImage(file=current_directory+'/imgs/logo2.png'))
logs.protocol('WM_DELETE_WINDOW', dont_close)
logs.withdraw()
logs_text = Text(logs, padx=15, pady=10, fg=white_color, bg=light_gray_color,
bd=0, highlightbackground=dark_gray_color, highlightcolor=selected_color)
logs_text.grid(row=0, column=0, columnspan=4, sticky=W+E+S+N)
logs_text.insert(
'end', "Credits to Adrianos SIDIRAS GALANTE - developper of this app\n")
logs_text.insert(
'end', "- Here you can see the activity and logs of the program ...\n")
save_logs_button = Button(logs, text="Save logs", padx=5, pady=0, fg=white_color, bg=light_gray_color,
bd=1, highlightbackground=dark_gray_color, highlightcolor=dark_gray_color, command=save_logs)
save_logs_button.grid(row=1, column=1, sticky=E)
clear_logs_button = Button(logs, text="Clear logs", padx=5, pady=0, fg=white_color, bg=light_gray_color,
bd=1, highlightbackground=dark_gray_color, highlightcolor=dark_gray_color, command=clear_logs)
clear_logs_button.grid(row=1, column=2, sticky=W)
spacer_text = Label(configuration_frame, text=" ", padx=0, pady=0, fg=dark_gray_color,
bg=dark_gray_color, bd=0, highlightbackground=dark_gray_color, highlightcolor=selected_color)
spacer_text.grid(row=7, column=0, columnspan=4)
show_logs_button = Button(configuration_frame, text="Show logs", padx=0, pady=0, fg=white_color, bg=light_gray_color,
bd=1, highlightbackground=dark_gray_color, highlightcolor=selected_color, command=show_log)
show_logs_button.grid(row=8, column=2, columnspan=2, sticky=W+N+E+S)
# endregion
# region GUI logs scrollbar
"""
Here is the scrollbar, it is usefull when your logs are full of text and you need to scroll down
it's just a nice addition. If it bothers you, you can just comment the three following lines using "#" and get rid of it
"""
scrollbar = Scrollbar(logs, command=logs_text.yview,
orient="vertical", bg=light_gray_color, bd=1)
scrollbar.grid(row=0, column=5, sticky=N+E+W+S)
logs_text.configure(yscrollcommand=scrollbar.set)
# endregion
# region check all folders are present
if(os.path.isfile(current_directory+'/configs/entries.csv'))and(os.path.isfile(current_directory+'/configs/configs.csv'))and(os.path.isfile(current_directory+'/configs/connexion.csv')):
logs_text.insert('end',"- all the config files are present\n")
#endregion
# region GUI graphs initialisation
"""
Here we define our Canvas and figure, without those we can't plot anything.
We also state that we want our figures to be drawn inside of our GUI insted of on an other window
"""
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
ratio=w/h
ratio=round(ratio,1)
screen_size=h/111
screen_size_small=screen_size*0.979
fig = plt.figure(figsize=(screen_size_small*ratio, screen_size), facecolor=dark_gray_color)
fig.suptitle('Zi²', fontsize=16, family="sans-serif",
color=accent_color, fontweight="bold")
canvas = FigureCanvasTkAgg(fig, master=ploting_frame)
canvas_widget = canvas.get_tk_widget()
# endregion
# region GUI Demodulators list to put in graphs
"""
Here is the definition of what is inside the Demods and Pids frame
You can manually add sample options if you need to
or Demod option if your device posseses more than 6
"""
sample_option = ["r", "theta", "x", "y", "frequency", "auxin0", "auxin1", "xiy", "df"]
demod_option = ["1", "2", "3", "4", "5", "6"]
demods_frame = LabelFrame(main_frame, bg=dark_gray_color,
fg=white_color, padx=0, pady=0, bd=0)
demods_frame.configure(background=dark_gray_color)
demods_frame.grid(row=3, column=0, columnspan=3, sticky=W)
add_count = 0
def Add_sample_fct():
global demod_selected, demod_selection, sample_selected, sample_selection, demod_checkbox_state, demod_checkbox, current_row, add_count, demod_option, sample_option
demod_selected.append(StringVar())
demod_selected[add_count].set(demod_option[0])
demod_selection.append(OptionMenu(
demods_frame, demod_selected[add_count], *demod_option))
demod_selection[add_count].configure(background=dark_gray_color, fg=accent_color,
highlightbackground=dark_gray_color, highlightcolor=selected_color, bd=0, justify='center')
demod_selection[add_count].grid(
row=current_row+add_count, column=0, sticky=W)
sample_selected.append(StringVar())
sample_selected[add_count].set(sample_option[0])
sample_selection.append(OptionMenu(
demods_frame, sample_selected[add_count], *sample_option))
sample_selection[add_count].configure(background=dark_gray_color, fg=accent_color,
highlightbackground=dark_gray_color, highlightcolor=selected_color, bd=0, justify='center')
sample_selection[add_count].grid(
row=current_row+add_count, column=1, columnspan=3, sticky=W+N+E+S)
add_count += 1
def Remove_sample_fct():
global demod_selected, demod_selection, sample_selected, sample_selection, demod_checkbox_state, demod_checkbox, current_row, add_count, demod_option, sample_option
if(add_count != 0):
add_count -= 1
demod_selected.pop(add_count)
demod_selection[add_count].destroy()
demod_selection.pop(add_count)
sample_selected.pop(add_count)
sample_selection[add_count].destroy()
sample_selection.pop(add_count)
Add_sample_button = Button(demods_frame, text=" + ", width=11, padx=0, pady=0, fg=white_color, bg=light_gray_color, bd=1,
highlightbackground=dark_gray_color, highlightcolor=selected_color, command=Add_sample_fct).grid(row=2, column=0, columnspan=2, sticky=N+S+E+W)
Remove_sample_button = Button(demods_frame, text=" - ", width=8, padx=0, pady=0, fg=white_color, bg=light_gray_color, bd=1,
highlightbackground=dark_gray_color, highlightcolor=selected_color, command=Remove_sample_fct).grid(row=2, column=2, columnspan=2, sticky=N+S+E+W)
demod_text = Label(demods_frame, text="Demod", bg=dark_gray_color,
fg=white_color, justify='center').grid(row=3, column=0, sticky=W)
sample_text = Label(demods_frame, text="Sample", bg=dark_gray_color, fg=white_color,
justify='center').grid(row=3, column=1, columnspan=3, sticky=W+E)
demod_selected = []
demod_selection = []
sample_selected = []
sample_selection = []
demod_checkbox_state = []
demod_checkbox = []
current_row = 4
demod_selected.append(StringVar())
demod_selected[add_count].set(demod_option[0])
demod_selection.append(OptionMenu(
demods_frame, demod_selected[add_count], *demod_option))
demod_selection[add_count].configure(background=dark_gray_color, fg=accent_color,
highlightbackground=dark_gray_color, highlightcolor=selected_color, bd=0, justify='center')
demod_selection[add_count].grid(row=current_row+add_count, column=0, sticky=W)
sample_selected.append(StringVar())
sample_selected[add_count].set(sample_option[0])
sample_selection.append(OptionMenu(
demods_frame, sample_selected[add_count], *sample_option))
sample_selection[add_count].configure(background=dark_gray_color, fg=accent_color,
highlightbackground=dark_gray_color, highlightcolor=selected_color, bd=0, justify='center')
sample_selection[add_count].grid(
row=current_row+add_count, column=1, columnspan=3, sticky=W+N+E+S)
add_count += 1
# endregion
# region GUI frames to draw
"""
Here we define the number of frames to draw.
Be carefull, if you input 0 than it will work in endless mode and scan with the microscope
"""
frames_to_draw_text = Label(settings_frame, text="Draw : ", bg=dark_gray_color, fg=white_color,
justify='center').grid(row=1, column=0, sticky=W) # put text on the window
frames_to_draw_entry = Entry(settings_frame, bg=light_gray_color, width=7, fg=accent_color,
justify='center', highlightbackground="#2d2d30", highlightcolor=selected_color, bd=0)
frames_to_draw_entry.grid(row=1, column=1, columnspan=2, sticky=W+E)
# endregion
# region GUI Size Text And Entry
size_text = Label(settings_frame, text="Size : ", bg=dark_gray_color, fg=white_color,
justify='center').grid(row=2, column=0, sticky=W) # put text on the window
size_entry = Entry(settings_frame, bg=light_gray_color, width=7, fg=accent_color,
justify='center', highlightbackground="#2d2d30", highlightcolor=selected_color, bd=0)
size_entry.grid(row=2, column=1, columnspan=2, sticky=W+E)
# endregion
# region GUI Frequency Text And Entry
frequency_text = Label(settings_frame, text="Freq : ", bg=dark_gray_color, fg=white_color,
justify='center').grid(row=3, column=0, sticky=W) # put text on the window
frequency_entry = Entry(settings_frame, bg=light_gray_color, width=7, fg=accent_color,
justify='center', highlightbackground="#2d2d30", highlightcolor=selected_color, bd=0)
frequency_entry.grid(row=3, column=1, columnspan=2, sticky=W+E)
# endregion
# region GUI display modes
trace_mode_radio = IntVar()
trace_mode_text = Label(settings_frame, text="Display mode :", bg=dark_gray_color,
justify='left', fg=white_color).grid(row=4, column=0, columnspan=3, sticky=W)
Radiobutton(settings_frame, text="Trace", variable=trace_mode_radio, value=0, bg=dark_gray_color, fg=accent_color, justify='left',
highlightbackground=dark_gray_color, highlightcolor=dark_gray_color).grid(row=5, column=0, columnspan=3, sticky=W)
Radiobutton(settings_frame, text="Retrace", variable=trace_mode_radio, value=1, bg=dark_gray_color, fg=accent_color, justify='left',
highlightbackground=dark_gray_color, highlightcolor=dark_gray_color).grid(row=6, column=0, columnspan=3, sticky=W)
# endregion
# region GUI save image
save_after_image_text = Label(
save_frame, text="Save image", bg=dark_gray_color, fg=white_color, justify='left')
save_after_image_checkbox_state = IntVar()
save_after_image_checkbox = Checkbutton(
save_frame, variable=save_after_image_checkbox_state)
save_after_image_checkbox.configure(background=dark_gray_color, fg=dark_gray_color,
highlightbackground=dark_gray_color, highlightcolor=selected_color, bd=0)
def check_save_default(*args):
if (not(save_to_gwy_checkbox_state.get())) and (not(save_to_txt_checkbox_state.get())):
if(save_after_image_checkbox_state.get()):
save_to_gwy_checkbox_state.set(1)
# endregion
# region GUI sample name + fieldnames
fieldname = ['custom_save_directory',
'custom_save_directory_adress', 'sample_name']
logs_text.insert(
'end', "- the current directory is : {} \n".format(current_directory))
def sample_name_fct(*args):
global fieldname, current_directory
if(os.path.isfile(current_directory+'/configs/configs.csv')):
with open(current_directory+'/configs/configs.csv', 'w') as csv_file:
csv_writer = csv.DictWriter(
csv_file, fieldnames=fieldname, delimiter=',')
csv_writer.writeheader()
line = {fieldname[0]: '{}'.format(save_to_custom_folder_checkbox_state.get()), fieldname[1]: '{}'.format(
save_to_custom_folder_entry.get()), fieldname[2]: '{}'.format(sample_name_entry.get())}
csv_writer.writerow(line)
# endregion
# region GUI save to custom directory
def custom_save(*args):
global fieldname, current_directory
if(save_to_custom_folder_checkbox_state.get() != 0):
if(os.path.isfile(current_directory+'/configs/configs.csv')):
with open(current_directory+'/configs/configs.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
custom_save_directory_adress = line['custom_save_directory_adress']
if(custom_save_directory_adress == "None"):
root.directory = filedialog.askdirectory(
title="select a directory in which all the app data will be saved") # ask directory
custom_save_directory_adress = root.directory
custom_save_directory_adress = str(
custom_save_directory_adress)
if((type(custom_save_directory_adress) == str) and (len(custom_save_directory_adress) > 2)):
save_to_custom_folder_entry.delete(first=0, last='end')
save_to_custom_folder_entry.insert(
INSERT, custom_save_directory_adress)
if(os.path.isdir(save_to_custom_folder_entry.get())):
if(os.path.isfile(current_directory+'/configs/configs.csv')):
with open(current_directory+'/configs/configs.csv', 'w') as csv_file:
csv_writer = csv.DictWriter(
csv_file, fieldnames=fieldname, delimiter=',')
csv_writer.writeheader()
line = {fieldname[0]: '{}'.format(save_to_custom_folder_checkbox_state.get()), fieldname[1]: '{}'.format(
save_to_custom_folder_entry.get()), fieldname[2]: '{}'.format(sample_name_entry.get())}
csv_writer.writerow(line)
else:
save_to_custom_folder_entry.delete(first=0, last='end')
save_to_custom_folder_entry.insert(INSERT, "None")
if(os.path.isfile(current_directory+'/configs/configs.csv')):
with open(current_directory+'/configs/configs.csv', 'w') as csv_file:
csv_writer = csv.DictWriter(
csv_file, fieldnames=fieldname, delimiter=',')
csv_writer.writeheader()
line = {fieldname[0]: '{}'.format(save_to_custom_folder_checkbox_state.get()), fieldname[1]: '{}'.format(
save_to_custom_folder_entry.get()), fieldname[2]: '{}'.format(sample_name_entry.get())}
csv_writer.writerow(line)
save_to_custom_folder_entry.configure(bg='#333333', fg=accent_color)
else:
if(os.path.isfile(current_directory+'/configs/configs.csv')):
with open(current_directory+'/configs/configs.csv', 'w') as csv_file:
csv_writer = csv.DictWriter(
csv_file, fieldnames=fieldname, delimiter=',')
csv_writer.writeheader()
line = {fieldname[0]: '{}'.format(save_to_custom_folder_checkbox_state.get()), fieldname[1]: '{}'.format(
save_to_custom_folder_entry.get()), fieldname[2]: '{}'.format(sample_name_entry.get())}
csv_writer.writerow(line)
save_to_custom_folder_entry.configure(bg='#222222', fg='#f1f1f1')
save_to_custom_folder_text = Label(
save_frame, text="Save to custom dir", bg=dark_gray_color, fg=white_color, justify='left')
save_to_custom_folder_checkbox_state = IntVar()
save_to_custom_folder_checkbox = Checkbutton(
save_frame, variable=save_to_custom_folder_checkbox_state)
save_to_custom_folder_checkbox.configure(background=dark_gray_color, fg=dark_gray_color,
highlightbackground=dark_gray_color, highlightcolor=selected_color, bd=0)
sample_name_var = StringVar()
sample_name_text = Label(save_frame, text="Sample :", bg=dark_gray_color,
fg=white_color, justify='center') # put text on the window
sample_name_entry = Entry(save_frame, width=10, bg=light_gray_color, fg=accent_color, justify='center',
highlightbackground="#2d2d30", highlightcolor=selected_color, bd=0, textvariable=sample_name_var)
save_to_custom_folder_entry = Entry(save_frame, width=19, bg=light_gray_color, fg=accent_color,
justify='center', highlightbackground="#2d2d30", highlightcolor=selected_color, bd=0)
if(os.path.isfile(current_directory+'/configs/configs.csv')):
with open(current_directory+'/configs/configs.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
custom_save_directory = line['custom_save_directory']
custom_save_directory_adress = line['custom_save_directory_adress']
sample_name = line['sample_name']
sample_name_entry.insert(INSERT, sample_name)
save_to_custom_folder_checkbox_state.set(custom_save_directory)
if(custom_save_directory_adress != '0'):
save_to_custom_folder_entry.delete(first=0, last='end')
save_to_custom_folder_entry.insert(
INSERT, custom_save_directory_adress)
logs_text.insert('end', " found custom save adress : {} \n".format(
custom_save_directory_adress))
else:
save_to_custom_folder_entry.delete(first=0, last='end')
save_to_custom_folder_entry.insert(INSERT, "None")
logs_text.insert('end',"- found in configs/configs\n custom directory = {}\n adress = {}\n sample name = {}\n".format(save_to_custom_folder_checkbox_state.get(), save_to_custom_folder_entry.get(), sample_name_entry.get()))
else:
save_to_custom_folder_checkbox_state.set(0)
save_to_custom_folder_entry.delete(first=0, last='end')
save_to_custom_folder_entry.insert(INSERT, "None")
if(custom_save_directory == '0'):
save_to_custom_folder_entry.configure(bg='#222222', fg='#444444')
else:
save_to_custom_folder_entry.configure(bg='#333333', fg='#007ACC')
save_to_custom_folder_text.grid(row=2, column=0, columnspan=2, sticky=W)
save_to_custom_folder_checkbox.grid(row=2, column=2, sticky=W)
save_to_custom_folder_entry.grid(row=3, column=0, columnspan=2, sticky=W+N+S)
save_after_image_text.grid(row=1, column=0, columnspan=2, sticky=W)
save_after_image_checkbox.grid(row=1, column=2, sticky=W)
sample_name_text.grid(row=0, column=0, sticky=W)
sample_name_entry.grid(row=0, column=1, columnspan=2, sticky=W)
sample_name_var.trace("w", sample_name_fct)
save_to_custom_folder_checkbox_state.trace("w", custom_save)
# endregion
# region GUI button to Ask directory
def askdir():
global fieldname, current_directory
root.directory = filedialog.askdirectory(
title="select a directory in which all the app data will be saved") # ask directory
custom_save_directory_adress = root.directory
custom_save_directory_adress = str(custom_save_directory_adress)
logs_text.insert(
'end', "- you selected : {} as the custom save directory\n".format(custom_save_directory_adress))
if len(custom_save_directory_adress) > 2:
pass
else:
custom_save_directory_adress = "None"
save_to_custom_folder_entry.delete(first=0, last='end')
save_to_custom_folder_entry.insert(
INSERT, str(custom_save_directory_adress))
if(os.path.isfile(current_directory+'/configs/configs.csv')):
with open(current_directory+'/configs/configs.csv', 'w') as csv_file:
csv_writer = csv.DictWriter(
csv_file, fieldnames=fieldname, delimiter=',')
csv_writer.writeheader()
line = {fieldname[0]: '{}'.format(save_to_custom_folder_checkbox_state.get()), fieldname[1]: '{}'.format(
save_to_custom_folder_entry.get()), fieldname[2]: '{}'.format(sample_name_entry.get())}
csv_writer.writerow(line)
askdir_button = Button(save_frame, text=" ... ", padx=0, pady=0, fg=white_color, bg=light_gray_color,
bd=1, highlightbackground=dark_gray_color, highlightcolor=selected_color, command=askdir)
askdir_button.grid(row=3, column=2, sticky=W+N)
# endregion
# region GUI choose the graphs color (CMAP)
img = {}
"""
Here you can add more color options, however they will not have the previsualisation
the other colors can be found here :
https://matplotlib.org/3.1.1/gallery/color/colormap_reference.html
"""
cmap_option = ["viridis", "inferno", "hot", "cool", "coolwarm", "tab20", "jet"]
default_save = '/saved_data/'
def change_image(*args):
global img
cmap = cmap_selected.get()
cmap_selection.configure(bg=dark_gray_color)
if(len(img) > 0):
try :
cmap_selection.configure(image=img[cmap], fg=accent_color, highlightbackground=dark_gray_color,highlightcolor=selected_color, bd=0, justify='center')
except :
pass
for cmap in cmap_option:
img_adress = '{}/colors/{}.png'.format(current_directory, cmap)
if os.path.isfile(img_adress):
img[cmap] = (ImageTk.PhotoImage(Image.open(img_adress)))
cmap_text = Label(settings_frame, text="color : ", bg=dark_gray_color, fg=white_color,
justify='center').grid(row=7, column=0, sticky=W) # put text on the window
cmap_selected = (StringVar())
cmap_selected.trace("w", change_image)
cmap_selection = OptionMenu(settings_frame, cmap_selected, *cmap_option)
cmap_selection.grid(row=7, column=1, columnspan=3, sticky=W)
# endregion
# region GUI display the frequency delta
frequency_delta_text = Label(toolbar_frame, text="", bg=dark_gray_color,
fg=white_color, justify='center') # put text on the window
frequency_delta_text.pack(side=RIGHT, fill=BOTH)
#endregion
# region Animation part 1
line_number = 0
frame_vertical_trace = 0 # 0 is top to bottom, 1 is bottom to top
right_to_plot = 0
frames_drawn = 0
initialized = 0
size = None
stoped = 1
trace_names = ['trace', 'retrace']
EOF = 0
min_r = {}
max_r = {}
first_range = 0
flip=0
def animate(i):
global line_number, im, signal_paths, data, size, frame_vertical_trace, frames_drawn, right_to_plot, current_directory, default_save, stoped, initialized, trace_names, end_of_line, end_of_frame, EOF, min_r, max_r, first_range, frequency_index
if(stoped == 0): # if the program is supposed tu be running
data_read = daq_module.read(True) # read the sampling information
if(frames_drawn == 0) and (line_number == 0): # if we wait for End of Frame trigger
# check if we received and EOF trigger @
EOF_trigger = daq_module.get("triggered")
EOF_dict = EOF_trigger['triggered']
EOF = EOF_dict[0]
if EOF != 0: # if we received EOF trigger
# what is the current trigger node ?
trigger_node = daq_module.get("triggernode")
trigger_node = trigger_node['triggernode'][0]
# if the trigger node is not EOL
if(trigger_node != '/{}/demods/0/sample{}'.format(device, end_of_line)):
# set triggernode to EOL
daq_module.set("triggernode", dev_adress +'/demods/0/sample'+end_of_line)
logs_text.insert('end', "- has aquired EOF trigger, switching to EOL trigger\n") # print it
first_range = 1
if (data_read) and (right_to_plot != 0): # if we have received data and we are not paused
now = datetime.now()
current_time = now.strftime("%Hh-%Mm-%Ss")
# if the data we want is in the returned signals
returned_signal_paths = [signal_path.lower()for signal_path in data_read.keys()]
graph_to_update_number = 1
sample_loss = 0
delta_text=""
sample_loss_indexs = []
for signal_path in signal_paths: # for every signal we want to plot
if signal_path.lower() in returned_signal_paths:
for index, signal_burst in enumerate(data_read[signal_path.lower()]):
value = signal_burst['value'][0, :]
for index in range(0, len(value)):
if np.isnan(value[index]):
sample_loss += 1
sample_loss_indexs.append(index)
if sample_loss != 0:
logs_text.insert('end', "there has been {} sample(s) lost in line {}\n".format(
sample_loss, line_number))
if first_range!= 0:
min_r[signal_path] = np.nanmin(value)
max_r[signal_path] = np.nanmax(value)
if signal_path==signal_paths[(len(signal_paths)-1)]:
first_range=0
else:
prev = min_r[signal_path]
min_r[signal_path] = np.nanmin(value)
if min_r[signal_path] < prev:
pass
else:
min_r[signal_path] = prev
prev = max_r[signal_path]
max_r[signal_path] = np.nanmax(value)
if max_r[signal_path] > prev:
pass
else:
max_r[signal_path] = prev
min = min_r[signal_path]
max = max_r[signal_path]
# fill the corresponding line in the matrix
data[signal_path][line_number] = value[:]
data_splited = np.hsplit(data[signal_path], 2)
data_trace = data_splited[trace_mode_radio.get()]
if trace_mode_radio.get() == 0:
data_trace = np.fliplr(data_trace)
if flip:
data_trace = np.flipud(data_trace)
if len(frequency_index):
for index in frequency_index:
if graph_to_update_number-1 == index:
delta = max-min
delta_text=str(delta_text+(" delta freq graph n°{} = {:.2f}hz ".format((index+1), delta)))
frequency_delta_text.configure(text="")
frequency_delta_text.configure(text=delta_text)
# update plot with trace or retrace
im[graph_to_update_number].set_data(data_trace)
im[graph_to_update_number].set_clim(min, max) # set the colorbar range
graph_to_update_number += 1
actual_size = size_entry.get()
if(frame_vertical_trace == 0) and ((graph_to_update_number-1) == len(signal_paths)):
frames_to_show = frames_drawn+1
line_number += 1
elif(frame_vertical_trace != 0) and ((graph_to_update_number-1) == len(signal_paths)):
line_number_to_show = int(actual_size)-line_number
frames_to_show = frames_drawn+1
line_number -= 1
else:
pass
#endregion
# region Save image
# here we save the data if we reach the bottom or top of the image
if(line_number == (size)) or (line_number < 0):
"""
Here we're saving both trace and retrace as opposed to the plotting part which only displays one
the File is called : curent time + sample name + signal path + frame number + trace or retrace
the file is saved as a Gwyddyon file and the directory can be :
- a custom set one
- the default with is current directory + saved data
- the current one
"""
frame_vertical_trace = not(frame_vertical_trace)
frames_drawn += 1
if(save_after_image_checkbox_state.get() != 0):
now = datetime.now()
day_time_sample = now.strftime("%H-%M-%S")
for signal_path in signal_paths:
data_to_save = np.hsplit(data[signal_path], 2)
if(flip):
data_to_save=np.flipud(data_to_save)
for index in range(0, 2):
saved_signal_path = signal_path.replace('/', ' ')
saved_signal_path = saved_signal_path.replace(
'.', ',')
file_name = str('{} ; {} ; {} ; frame {} {}'.format(day_time_sample, sample_name_var.get(), saved_signal_path, (frames_drawn+1), trace_names[index]))
if save_to_gwy_checkbox_state.get():
obj = GwyContainer()
obj['/0/data/title'] = file_name
data_to_gwy = data_to_save[index]
obj['/0/data'] = GwyDataField(data_to_gwy)
now = datetime.now()
day_time = now.strftime("%Y-%m-%d")
if(save_to_custom_folder_checkbox_state.get() == 0):
save_adress = current_directory+default_save
save_adress = save_adress+'/'+day_time
if not os.path.isdir(save_adress):
os.mkdir(save_adress)
if(os.path.isdir(save_adress)):
save_adress = save_adress+'/'+file_name+'.gwy'
else:
logs_text.insert(
'end', "- default path does not exist saving to current directory instead \n")
save_adress = current_directory+'/'+file_name+'.gwy'
else:
save_adress = save_to_custom_folder_entry.get()
if os.path.isdir(save_adress):
save_adress = save_adress+'/'+day_time
if not os.path.isdir(save_adress):
os.mkdir(save_adress)
if(os.path.isdir(save_adress)):
save_adress = save_adress+'/'+file_name+'.gwy'
else:
logs_text.insert(
'end', "- save path does not exist, saving to default path \n")
save_adress = current_directory+default_save
save_adress = save_adress+'/'+day_time
if not os.path.isdir(save_adress):
os.mkdir(save_adress)
if(os.path.isdir(save_adress)):
save_adress = save_adress+'/'+file_name+'.gwy'
else:
logs_text.insert(
'end', "- default path does not exist saving to current directory instead \n")
save_adress = current_directory+'/'+file_name+'.gwy'
if save_to_gwy_checkbox_state.get():
obj.tofile(save_adress)
if save_to_txt_checkbox_state.get():
data_to_txt=data_to_save[index]
save_adress=save_adress.replace('.gwy','.csv')
with open(save_adress, 'w') as csv_file:
np.savetxt(save_adress,data_to_txt, delimiter=',')
now = datetime.now()
current_time = now.strftime("%Hh-%Mm-%Ss")
logs_text.insert(
'end', "- saved data at adress {} at {}\n".format(save_adress, current_time))
#endregion
# region Animation part 2
if(frame_vertical_trace == 0):
line_number += 1
else:
line_number -= 1
if int(frames_to_draw_entry.get()) != 0:
if(frames_drawn == int(frames_to_draw_entry.get())):
stoped = 1
daq_module.finish()
now = datetime.now()
current_time = now.strftime("%Hh-%Mm-%Ss")
logs_text.insert(
'end', "- all the frames have been drawn, sampling process has been stoped at {}\n".format(current_time))
initialized = 0
frame_vertical_trace = 0
frames_drawn = 0
line_number = 0
# endregion
# region GUI start button + Checking variables befor launching
first_launch = 1
def check_fct():
global fig, size_min_limit, size_max_limit, frequency_min_limit, frequency_max_limit, launch, size, right_to_plot, initialized, ani, stoped, first_launch, connected, end_of_line, end_of_frame, frequency_index
if(initialized == 0):
now = datetime.now()
current_time = now.strftime("%Hh-%Mm-%Ss")
logs_text.insert(
'end', "- the sampling process is being initialized ... at {}\n".format(current_time))
size = int(size_entry.get())
frequency = float(frequency_entry.get())
if (size <= size_min_limit):
logs_text.insert(
'end', "- the image size is too low. The application does not support it, however you can manually change the bottom limit in the program script \n")
launch = 0
size_entry.configure(
highlightbackground="#aa2222", highlightcolor="#aa2222")
elif (size > size_max_limit):
logs_text.insert(
'end', "- The image size is too high. The application does not support it, however you an manually change the upper limit in the program script \n")
launch = 0
size_entry.configure(
highlightbackground="#aa2222", highlightcolor="#aa2222")
else:
launch = 1
size_entry.configure(highlightbackground="#2d2d30",
highlightcolor=selected_color)
if (frequency < frequency_min_limit):
logs_text.insert(
'end', "- the line sacnning frequency is too low. The application does not support it, however you an manually change the upper limit in the program script \n")
launch = 0
frequency_entry.configure(
highlightbackground="#aa2222", highlightcolor="#aa2222")
elif (frequency > frequency_max_limit):
logs_text.insert(
'end', "- The line sacnning frequency is too high. The application does not support it, however you an manually change the upper limit in the program script \n")
launch = 0
frequency_entry.configure(
highlightbackground="#aa2222", highlightcolor="#aa2222")
else:
frequency_entry.configure(
highlightbackground="#2d2d30", highlightcolor=selected_color)
if launch != 0:
launch = 1
# IF THE SETTINGS ARE CORRECT
if(launch != 0 and connected != 0):
logs_text.insert(
'end', "- all the settings are correct, launching the program \n")
# empty the current adress list befor re launching
for signal_path in range(0, len(signal_paths)):
signal_paths.pop(0)
for index in range(0, len(frequency_index)):
frequency_index.pop(0)
size_entry.configure(bg="#222222", fg=light_gray_color)
frequency_entry.configure(bg="#222222", fg=light_gray_color)
for signal_number in range(0, add_count):
demod_number = (int(demod_selected[signal_number].get()))-1
demod_adress = '/demods/{}'.format(demod_number)
sample_number = sample_selected[signal_number].get()
sample_adress = '/sample.{}'.format(sample_number)
signal_path = str(dev_adress+demod_adress+sample_adress)
if sample_number == 'frequency':
frequency_index.append(len(signal_paths))
signal_paths.append(signal_path)
signals_to_plot = len(signal_paths)
if signals_to_plot > 9:
ratio = 4
else:
ratio = 3
logs_text.insert(
'end', "- the signal paths are : {} \n".format(signal_paths))
rows = int(np.ceil(signals_to_plot/ratio))
columns = int(np.ceil(signals_to_plot/rows))
trace_mode = trace_mode_radio.get()
cmap = cmap_selected.get()
for signal_number in signal_paths:
daq_module.subscribe(signal_number)
data[signal_number] = np.empty(shape=(size, 2*size))
data[signal_number][:] = np.nan
min_r[signal_number] = 0
max_r[signal_number] = 0
# INIT
T = 1/frequency # time to draw a line (trace + retrace time)
# Time in seconds for each data burst/segment.
burst_duration = (T-1e-7)
holdoff_time = burst_duration*0.75
num_cols = int(size*2)
if(int(frames_to_draw_entry.get()) != 0):
lines_to_draw = int(int(frames_to_draw_entry.get())*size)
real_duration = lines_to_draw*T
total_duration = real_duration*2 # time to draw a frame
num_bursts = int(total_duration/burst_duration)
daq_module.set("device", device)
daq_module.set('type', 0) # 0 for continious 6 for trigged
daq_module.set("grid/mode", 2)
daq_module.set("count", num_bursts)
else:
daq_module.set("device", device)
daq_module.set('type', 6) # 0 for continious 6 for trigged
daq_module.set("grid/mode", 2)
daq_module.set("edge", 1) # falling edge
daq_module.set("endless", 1)
# trigger always on first demod @
daq_module.set("triggernode", dev_adress +'/demods/0/sample'+end_of_frame)
daq_module.set("duration", burst_duration)
daq_module.set("grid/cols", num_cols)
daq_module.set("clearhistory", 1)
daq_module.set("holdoff/time", holdoff_time)
if((manual_sample_freq_checkbox_state.get()!= 0) and (advanced_checkbox_state.get()!= 0)):
pass
else:
try:
high_rate = 230000
low_rate = 1800
daq.setDouble(dev_adress+'/demods/0/rate', high_rate)
logs_text.insert(
'end', '- demods n°0 rate has been set too {}\n'.format(high_rate))
for index in range(1, 6):
daq.setDouble(
dev_adress+'/demods/{}/rate'.format(index), low_rate)
logs_text.insert(
'end', '- demods n°{} rate has been set too {}\n'.format(index, low_rate))
except:
too_low = 0
too_high = 0
logs_text.insert(
'end', "Couldn't change demods sampling rate !!\n")
high_rate = daq.getDouble(dev_adress+'/demods/0/rate')
if high_rate < 145000:
too_low = 1
for index in range(1, 6):
low_rate = daq.getDouble(
dev_adress+'/demods/{}/rate'.format(index))
if low_rate > 9000:
too_high = 1
if(too_low or too_high):
message = ''
if too_low:
message = '. The sampling rate of Demod 1 is too low, you risk missing triggers please use 230kSa/s'
if too_high:
message = message+'. The sampling rate of the Demods 2 too 6 are too high please use 1,8kSa/s or 9kSa/s max'
MsgBox_rate = tk.messagebox.showwarning(
'Warning', 'the sampling rates are Wrong{}'.format(message), icon='warning')
initialized = 1
stoped = 0
blank_image = np.empty(shape=(size, size))
blank_image[:] = np.nan
plt.clf()
fig.tight_layout(pad=3)