-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPAPPy.py
More file actions
1185 lines (916 loc) · 50.2 KB
/
PAPPy.py
File metadata and controls
1185 lines (916 loc) · 50.2 KB
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
import tkinter as tk
import pasdatalib as pdl
import pasphysics as ph
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from os.path import basename
from numpy import array, sqrt
import analysisFunctions as anal #import analysis windows
# Original Code written July 5, 2021 at 9:30 PM - Addison Ballif
# Outline, Plans, and Ideas
#
# I think for now, I am going to only keep the data that is cropped to the annihilation peak, but
# maybe I should reconsider, because it would be easier to do deconvolution or calibration if we keep
# the other peaks in the data.
#
# I am assuming that all the data files have the same calibration, and total number of bins. This way
# I only have to store the x-data once.
# I am putting window and GUI tools in this file, and physics code in the other file. If it must be related to a GUI,
# then it goes here, but if it is a physics thing, you will find it in one of the other files. The pasdatalib file is
# for importing data and doing data manipulation work, and the pasphysics file contains background subtraction and
# s-parameter calculation.
header_font = ('Arial', 18, 'bold') #the font size of the section title
subheader_font = ('Arial', 16)
parameter_bounds_alpha = 0.2 #the alpha of the parameter bound fill color
# ----------- FILE PICKER ------------
# This is the part where you load the csv files. Any number of csv files can be loaded,
# and the reference sample is also selected.
class FilePicker(tk.Frame):
def __init__(self, parent, master, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.master = master
self.filepaths = []
self.statusText = tk.Label(self, text='Please Load a File')
self.statusText.grid(row=4, column=0, columnspan=2)
# a dropdown to select the reference file
tk.Label(self, text='1. Import a Data File', font=header_font).grid(row=0, column=0, columnspan=2)
#a file browse button
tk.Label(self, text='Import a CSV File: ').grid(row=1, column=0)
tk.Button(self, text='Browse', command=self.filebrowse).grid(column=1, row=1)
#reference sample
tk.Label(self, text='Select the Reference Sample: ').grid(row=2, column=0)
self.value_inside = tk.StringVar(self)
self.value_inside.set('Reference Sample')
self.optionMenu = tk.OptionMenu(self, self.value_inside, self.master.names, command=self.selectReference)
self.optionMenu.grid(row=2, column=1)
#listbox
self.listbox = tk.Listbox(self, height=3)
self.listbox.grid(column=0, row=3)
self.updateListBox()
tk.Button(self, text='Remove Selected Sample', command=self.removeFile).grid(row=3, column=1)
def loadFile(self, filepath):
# This function adds the filepath inputted into the list of filepaths.
try:
#check to see if they already imported it
if filepath in self.filepaths:
self.updateStatusText('File Already Loaded. ')
return
bins, energy, counts = pdl.importFile(filepath)
#just override it, because we assume its the same
self.master.bins0 = bins
self.master.energy0 = energy
#add data to the list of data
self.master.data0.append(counts)
self.filepaths.append(filepath)
self.master.names.append(basename(filepath))
self.updateStatusText('Success! ')
self.updateOptionsMenu()
self.updateListBox()
self.master.updateCroppedData()
self.master.graph.plotData()
self.master.bounds.makeGuess()
except FileNotFoundError:
self.updateStatusText('File Not Found. ')
self.filepathEntry.delete(0, 'end')
except OSError:
self.updateStatusText('File Not Found. ')
self.filepathEntry.delete(0, 'end')
def updateStatusText(self, text):
#updates the status text
self.statusText.config(text=text+f'{len(self.master.data0)} files loaded successfully.')
def updateOptionsMenu(self):
#makes the option menu
#updates the selection of options - I am just doing it by deletion and recreation
self.optionMenu.destroy()
self.value_inside = tk.StringVar(self)
try:
self.value_inside.set(self.master.names[self.master.ref_idx])
except IndexError:
self.value_inside.set('Select the Reference File')
if len(self.master.names) > 0:
self.optionMenu = tk.OptionMenu(self, self.value_inside, *self.master.names, command=self.selectReference)
else:
self.optionMenu = tk.OptionMenu(self, self.value_inside, self.master.names, command=self.selectReference)
self.optionMenu.grid(row=2, column=1)
def selectReference(self, *args):
# a function that save the index of the reference sample
idx = self.master.names.index(self.value_inside.get())
self.master.ref_idx = idx
self.master.graph.plotData()
def filebrowse(self):
#opens a file brownser
filepath_list = tk.filedialog.askopenfilenames(initialdir = "",title = "Select file",filetypes = (("csv files","*.csv"),("all files","*.*")))
for filepath in filepath_list:
self.loadFile(filepath=filepath)
def updateListBox(self):
#to update the listbox.
self.listbox.delete(0,tk.END)#clear
for filepath in self.master.names:
self.listbox.insert(tk.END, filepath)
def removeFile(self):
# to remove files which have been loaded.
idx = self.listbox.curselection()[0]
self.master.data0.remove(self.master.data0[idx])
self.master.names.remove(self.master.names[idx])
self.filepaths.remove(self.filepaths[idx])
self.updateListBox()
self.updateOptionsMenu()
self.master.updateCroppedData()
self.master.graph.plotData()
# --------------- GRAPH WINDOW ------------------
# The window where we display the data. I think for simplicity right now,
# I am only going to add the graph which is cropped to
class GraphWindow(tk.Frame):
def __init__(self, parent, master, dpi=60, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.master = master
#make the graph
self.figure = plt.Figure(figsize=(8,5.5), dpi=dpi)
self.figure.subplots_adjust(bottom=0.3, top=0.98)
self.ax = self.figure.add_subplot(111)
self.canvas = FigureCanvasTkAgg(self.figure, self) #add the figure to the window
self.canvas.get_tk_widget().grid(row=0, column=0)
#this panel contains a few options
self.optionsPanel = tk.Frame(self)
tk.Label(self.optionsPanel, text='Show All Samples: ').grid(row=0, column=0)
self.isShowingAll = True
self.allToggle = tk.Button(self.optionsPanel, text='Turn Off', command=self.setIsShowingAll)
self.allToggle.grid(row=0, column=1)
tk.Label(self.optionsPanel, text='Zoom in on Tails: ').grid(row=0, column=2)
self.isZoomed = False
self.zoomToggle = tk.Button(self.optionsPanel, text='Turn On', command=self.setIsZoomed)
self.zoomToggle.grid(row=0, column=3)
tk.Label(self.optionsPanel, text='X Units: ').grid(row=1, column=0)
self.isUsingChannels = False
self.xUnitToggle = tk.Button(self.optionsPanel, text='Show Channels', command=self.setIsUsingChannels)
self.xUnitToggle.grid(row=1, column=1)
tk.Label(self.optionsPanel, text='Show Error Bars: ').grid(row=1, column=2)
self.isShowingErrorBars = False
self.errbarToggle = tk.Button(self.optionsPanel, text='Show Error Bars', command=self.setShowErrBars)
self.errbarToggle.grid(row=1, column=3)
tk.Label(self.optionsPanel, text='Use Log Scale: ').grid(row=2, column=0)
self.isUsingLogScale = False
self.logScaleToggle = tk.Button(self.optionsPanel, text='Turn On', command=self.setUseLogScale)
self.logScaleToggle.grid(row=2, column=1)
tk.Label(self.optionsPanel, text='Zoom in on Peak: ').grid(row=3, column=0)
self.isZoomedOnCurve = False
self.zoomCurveToggle = tk.Button(self.optionsPanel, text='Turn On', command=self.setIsZoomedOnCurve)
self.zoomCurveToggle.grid(row=3, column=1)
tk.Label(self.optionsPanel, text='Show Grid Lines: ').grid(row=3, column=2)
self.hasGrid = False
self.gridToggle = tk.Button(self.optionsPanel, text='Turn On', command=self.setHasGrid)
self.gridToggle.grid(row=3, column=3)
self.optionsPanel.grid(row=1, column=0)
self.plotData()
def setHasGrid(self, **kwargs):
self.hasGrid = kwargs.get('value', not self.hasGrid)
if self.hasGrid:
self.gridToggle.config(text='Turn Off')
else:
self.gridToggle.config(text='Turn On')
self.plotData()
def setIsZoomedOnCurve(self, **kwargs):
self.isZoomedOnCurve = kwargs.get('value', not self.isZoomedOnCurve)
if self.isZoomedOnCurve:
self.zoomCurveToggle.config(text='Turn Off')
else:
self.zoomCurveToggle.config(text='Turn On')
self.plotData()
def setIsShowingAll(self, **kwargs):
self.isShowingAll = kwargs.get('value', not self.isShowingAll)
if self.isShowingAll:
self.allToggle.config(text='Turn Off')
else:
self.allToggle.config(text='Turn On')
self.plotData()
def setIsZoomed(self, **kwargs):
self.isZoomed = kwargs.get('value', not self.isZoomed)
if self.isZoomed:
self.zoomToggle.config(text='Turn Off')
else:
self.zoomToggle.config(text='Turn On')
self.updateYBounds()
def setIsUsingChannels(self, **kwargs):
#sets the units of the x axis
self.isUsingChannels = kwargs.get('value', not self.isUsingChannels)
if self.isUsingChannels:
self.xUnitToggle.config(text='Show keV')
else:
self.xUnitToggle.config(text='Show Channels')
self.plotData()
def setShowErrBars(self, **kwargs):
#toggles error bars on the graph
self.isShowingErrorBars = kwargs.get('value', not self.isShowingErrorBars)
if self.isShowingErrorBars:
self.errbarToggle.config(text='Hide Error Bars')
else:
self.errbarToggle.config(text='Show Error Bars')
self.plotData()
def setUseLogScale(self, **kwargs):
self.isUsingLogScale = kwargs.get('value', not self.isUsingLogScale)
if self.isUsingLogScale:
self.logScaleToggle.config(text='Turn Off')
else:
self.logScaleToggle.config(text='Turn On')
self.plotData()
def plotData(self):
self.ax.clear()
if len(self.master.data) < 1:
#show if no data is loaded
self.ax.text(0.5, 0.5, 'No Data Loaded', horizontalalignment='center', verticalalignment='center', transform=self.ax.transAxes, bbox=dict(facecolor='red', alpha=0.5))
#plot the data. updates the graph
if not self.isUsingChannels:
xData = self.master.energy
if self.isShowingAll:
self.plotAll(xData)
else:
self.plotReference(xData)
#plot a vlines
lowery = 0
uppery = self.ax.get_ylim()[1]*2
self.ax.axvline(self.master.center_keV, lowery, uppery, color='yellow')
self.ax.vlines([pdl.convertTokeV(self.master.lower, self.master.bins, self.master.energy),
pdl.convertTokeV(self.master.upper, self.master.bins, self.master.energy)],
lowery, uppery, color='sienna', label='Curve Bounds')
self.ax.fill_betweenx([lowery, uppery], pdl.convertTokeV(self.master.parameterWindow.lowerSBound, self.master.bins, self.master.energy),
pdl.convertTokeV(self.master.parameterWindow.upperSBound, self.master.bins, self.master.energy),
color='springgreen', alpha=parameter_bounds_alpha, label='S-Param Bounds')
self.ax.fill_betweenx([lowery, uppery], pdl.convertTokeV(self.master.parameterWindow.lowerLWBound, self.master.bins, self.master.energy),
pdl.convertTokeV(self.master.parameterWindow.upperLWBound, self.master.bins, self.master.energy),
color='skyblue', alpha=parameter_bounds_alpha, label='Left W-Param Bounds')
self.ax.fill_betweenx([lowery, uppery], pdl.convertTokeV(self.master.parameterWindow.lowerRWBound, self.master.bins, self.master.energy),
pdl.convertTokeV(self.master.parameterWindow.upperRWBound, self.master.bins, self.master.energy),
color='palevioletred', alpha=parameter_bounds_alpha, label='Right W-Param Bounds')
#uncomment this if you just want lines rather than the filled region. Gagliardi and Joey Watkins both use a filled region, So I switched to that.
# self.ax.vlines([pdl.convertTokeV(self.master.parameterWindow.lowerSBound, self.master.bins, self.master.energy),
# pdl.convertTokeV(self.master.parameterWindow.upperSBound, self.master.bins, self.master.energy)],
# lowery, uppery, color='springgreen', label='S-Param Bounds')
# self.ax.vlines([pdl.convertTokeV(self.master.parameterWindow.lowerLWBound, self.master.bins, self.master.energy),
# pdl.convertTokeV(self.master.parameterWindow.upperLWBound, self.master.bins, self.master.energy)],
# lowery, uppery, color='skyblue', label='Left W-Param Bounds')
# self.ax.vlines([pdl.convertTokeV(self.master.parameterWindow.lowerRWBound, self.master.bins, self.master.energy),
# pdl.convertTokeV(self.master.parameterWindow.upperRWBound, self.master.bins, self.master.energy)],
# lowery, uppery, color='palevioletred', label='Right W-Param Bounds')
#update xbounds to ignore vline positions
try:
if self.isZoomedOnCurve:
self.ax.set_xlim(pdl.convertTokeV(self.master.lower, self.master.bins, self.master.energy),
pdl.convertTokeV(self.master.upper, self.master.bins, self.master.energy))
else:
self.ax.set_xlim(min(self.master.energy), max(self.master.energy))
except ValueError:
pass #there isn't any data to look at yet.
self.ax.set_xlabel('Energy (keV)')
else:
#use channels as the x axis
xData = self.master.bins
if self.isShowingAll:
self.plotAll(xData)
else:
self.plotReference(xData)
#plot a vlines
lowery = 0
uppery = self.ax.get_ylim()[1]*2
self.ax.axvline(self.master.center, lowery, uppery, color='yellow')
self.ax.vlines([self.master.lower, self.master.upper, ],
lowery, uppery, color='sienna', label='Curve Bounds')
self.ax.fill_betweenx([lowery, uppery], self.master.parameterWindow.lowerSBound, self.master.parameterWindow.upperSBound,
color='springgreen', alpha=parameter_bounds_alpha, label='S-Param Bounds')
self.ax.fill_betweenx([lowery, uppery], self.master.parameterWindow.lowerLWBound, self.master.parameterWindow.upperLWBound,
color='skyblue', alpha=parameter_bounds_alpha, label='Left W-Param Bounds')
self.ax.fill_betweenx([lowery, uppery], self.master.parameterWindow.lowerRWBound, self.master.parameterWindow.upperRWBound,
color='palevioletred', alpha=parameter_bounds_alpha, label='Right W-Param Bounds')
#uncomment this if you just want lines rather than the filled region. Gagliardi and Joey Watkins both use a filled region, So I switched to that.
# self.ax.vlines([self.master.parameterWindow.lowerSBound, self.master.parameterWindow.upperSBound],
# lowery, uppery, color='mediumspringgreen', label='S-Param Bounds')
# self.ax.vlines([self.master.parameterWindow.lowerLWBound, self.master.parameterWindow.upperLWBound],
# lowery, uppery, color='skyblue', label='Left W-Param Bounds')
# self.ax.vlines([self.master.parameterWindow.lowerRWBound, self.master.parameterWindow.upperRWBound],
# lowery, uppery, color='palevioletred', label='Right W-Param Bounds')
#update xbounds to ignore vline positions
try:
if self.isZoomedOnCurve:
self.ax.set_xlim(self.master.lower, self.master.upper)
else:
self.ax.set_xlim(min(self.master.bins), max(self.master.bins))
except ValueError:
pass #there isn't any data to look at yet.
self.ax.set_xlabel('Channels')
#graph settings
self.ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), fancybox=True, shadow=True, ncol=2)
self.ax.set_ylabel('Counts')
if self.isUsingLogScale:
self.ax.set_yscale('log')
else:
self.ax.set_yscale('linear')
self.canvas.draw()
if self.hasGrid:
self.ax.grid()
self.updateYBounds()
def plotAll(self, xData):
#use this if you want to plot all files at once. It won't plot backgrounds
#plot each curve
for i in range(len(self.master.names)):
if self.isShowingErrorBars:
self.ax.errorbar(xData, self.master.data[i], label=self.master.names[i], fmt=',', yerr=self.master.data_un[i])
else:
self.ax.plot(xData, self.master.data[i], label=self.master.names[i])
def plotReference(self, xData):
#use this if you want to plot the reference and background
#plot each curve
if self.isShowingErrorBars:
self.ax.errorbar(xData, self.master.data[self.master.ref_idx], label='Reference', yerr=self.master.data_un[self.master.ref_idx], fmt=',')
self.ax.errorbar(xData, self.master.backgrounds[self.master.ref_idx], label='Background', yerr=self.master.backgrounds_un[self.master.ref_idx])
else:
self.ax.plot(xData, self.master.data[self.master.ref_idx], label='Reference')
self.ax.plot(xData, self.master.backgrounds[self.master.ref_idx], label='Background')
def updateYBounds(self):
#updates bounds based off of the reference sample
if not self.isZoomed:
try:
self.ax.set_ylim(0, 1.1*max(self.master.data[self.master.ref_idx]))
self.canvas.draw()
except IndexError:
pass #the graph probably just doesn't have anything in it.
else:
try:
self.ax.set_ylim(0, 0.1*max(self.master.data[self.master.ref_idx]))
self.canvas.draw()
except IndexError:
pass #the graph probably just doesn't have anything in it.
# -------------- BOUNDS WINDOW ----------------
# this is the window where we set the bounds of the graph, and these are the same bounds
# use for other calculations
class BoundsWindow(tk.Frame):
def __init__(self, parent, master, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.master = master
#default bounds are alread set in the
#bound selection
tk.Label(self, text='2. Update Curve Bounds', font=header_font).grid(row=0, column=0, columnspan=3)
tk.Label(self, text='Lower Curve Bound (channels): ').grid(row=1, column=0)
self.lowerEntry = tk.Entry(self)
self.lowerEntry.grid(row=1, column=1)
self.lowerEntry.insert(0, self.master.lower)
tk.Label(self, text='Upper Curve Bound (channels): ').grid(row=2, column=0)
self.upperEntry = tk.Entry(self)
self.upperEntry.grid(row=2, column=1)
self.upperEntry.insert(0, self.master.upper)
tk.Label(self, text='Background Size (channels): ').grid(row=3, column=0)
self.background_size_Entry = tk.Entry(self)
self.background_size_Entry.grid(row=3, column=1)
self.background_size_Entry.insert(0, self.master.background_size)
tk.Button(self, text='Update Bounds', command=self.updateXBounds).grid(row=3, column=2)
tk.Button(self, text='Make a Guess', command=self.makeGuess).grid(row=2, column=2)
self.lowerEntry.bind('<Return>', lambda event : self.updateXBounds())
self.upperEntry.bind('<Return>', lambda event : self.updateXBounds())
self.background_size_Entry.bind('<Return>', lambda event : self.updateXBounds())
self.centerLabel = tk.Label(self, text=f'Center: {self.master.center_keV:.3f} keV; {self.master.center}th channel')
self.centerLabel.grid(row=4, column=0)
def updateGUI (self):
#updates the labels
self.centerLabel.config(text=f'Center: {self.master.center_keV:.3f} keV; {self.master.center}th channel')
self.master.graph.plotData()
self.master.graph.updateYBounds()
def updateXBounds(self):
#updates the bounds
try:
lower = int(float(self.lowerEntry.get()))
upper = int(float(self.upperEntry.get()))
bgsize = int(float(self.background_size_Entry.get()))
center = pdl.convertTokeV(float(lower+upper)/2, self.master.bins, self.master.energy)
self.master.lower = lower
self.master.upper = upper
self.master.center_keV = center
self.master.center = int(float(upper+lower)/2)
self.master.background_size = bgsize
#update the entry so that it can remove decimals
self.lowerEntry.delete(0, tk.END)
self.lowerEntry.insert(0, self.master.lower)
self.upperEntry.delete(0, tk.END)
self.upperEntry.insert(0, self.master.upper)
self.background_size_Entry.delete(0, tk.END)
self.background_size_Entry.insert(0, self.master.background_size)
self.master.updateCroppedData()
self.updateGUI()
except ValueError:
print('Could Not Parse Bounds. ')
def makeGuess(self):
#makes a guess of the bounds
lower, upper = ph.make_curve_bound_guess(self.master.bins, self.master.data[self.master.ref_idx])
self.master.lower = int(lower)
self.master.upper = int(upper)
self.lowerEntry.delete(0, tk.END)
self.upperEntry.delete(0, tk.END)
self.lowerEntry.insert(0, self.master.lower)
self.upperEntry.insert(0, self.master.upper)
center = pdl.convertTokeV(float(lower+upper)/2, self.master.bins, self.master.energy)
self.master.center_keV = center
self.master.center = int(float(upper+lower)/2)
self.master.updateCroppedData()
self.updateGUI()
# ----------------- BACKGROUND SELECTION WINDOW --------------
class BackgroundSelectionWindow(tk.Frame):
def __init__(self, parent, master, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.master = parent
self.master = master
#make the background gui here
tk.Label(self, text='3. Background Selection and Subtraction', font=header_font).grid(row=0, column=0, columnspan=2)
tk.Label(self, text='Background Model: ').grid(row=1, column=0)
self.value_inside = tk.StringVar(self)
self.value_inside.set(ph.background_models[0])
self.optionMenu = tk.OptionMenu(self, self.value_inside, *ph.background_models)
self.optionMenu.grid(row=1, column=1)
tk.Button(self, text='Calculate Background', command=self.calculateBackground).grid(row=2, column=0)
def calculateBackground(self):
# the function that calculates and plots the background.
for i in range(len(self.master.data)):
counts = self.master.data[i]
self.master.backgrounds[i], self.master.backgrounds_un[i] = ph.find_background_shape(self.master.bins, counts, self.master.lower, self.master.upper, self.value_inside.get())
self.master.graph.setIsShowingAll(value=False)
self.master.background_type = self.value_inside.get()
# --------------- A Table for the S-Paramater Display ---------
class ParamTable(tk.Frame):
def __init__(self, parent, master, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.master = master
self.paramLabelText = 'Parameter'
#the table will have a name, s and un_s section
tk.Label(self, text='Sample').grid(row=0, column=0)
self.namesBox = tk.Listbox(self, height=3)
self.namesBox.grid(row=1, column=0)
self.paramLabel = tk.Label(self, text=self.paramLabelText)
self.paramLabel.grid(row=0, column=1)
self.paramBox = tk.Listbox(self, height=3)
self.paramBox.grid(row=1, column=1)
tk.Label(self, text='Uncertainty').grid(row=0, column=2)
self.unBox = tk.Listbox(self, height=3)
self.unBox.grid(row=1, column=2)
def populateTable(self, names, param_list, uncertainty_list):
#populates the table with the calculated s parameters
# don't give this function an empty list
height = len(names)
#set the heights
self.namesBox.config(height=height)
self.paramBox.config(height=height)
self.unBox.config(height=height)
#clear the boxes
self.namesBox.delete(0,tk.END)
self.paramBox.delete(0,tk.END)
self.unBox.delete(0,tk.END)
#add the stuff
self.namesBox.insert(tk.END, *names)
self.paramBox.insert(tk.END, *param_list)
self.unBox.insert(tk.END, *uncertainty_list)
def set_label(self, paramText):
self.paramLabelText = paramText
self.paramLabel.config(text=paramText)
def clear_table(self):
#clears the tables and deletes the parameters
self.namesBox.delete(0,tk.END)
self.paramBox.delete(0,tk.END)
self.unBox.delete(0,tk.END)
# ----- Progress Bar for Monte-Carlo Processes --------
# mostly its just a canvas, but I'm adding an update function that updates the canvas
# according to the self.progress_value
class ProgressBar(tk.Canvas):
def __init__(self, parent, *args, **kwargs):
tk.Canvas.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.progress_value = 0 # a float between 0 and 1
self.n_of_params = 0
self.completed_params = 0
def updateProgress(self, **kwargs):
#udpate the progress
self.progress_value = self.completed_params/self.n_of_params + kwargs.get('progress_value', 0)/self.n_of_params
self.create_rectangle(0, 0, self.winfo_width()*self.progress_value, self.winfo_height(), fill='blue')
self.update()
def setNumParameters(self, n_of_params):
self.n_of_params = n_of_params
def setCompletedParams(self, completed_params):
self.completed_params = completed_params
def reset_progress(self):
#resets the progress graphic
self.create_rectangle(0, 0, self.winfo_width(), self.winfo_height(), fill='white')
self.progress_value = 0
# -------------------- S-Parameter Selection Window ------------
# The s-parameter selection window contains the bounds for the s, left w, and right w parameters.
#
# The GUI is made up of entries for each of these parameters. There is also a button that makes a guess for each parameter.
# Also contained in this ParameterWindow are settings for the parameter calculation, and the parameter output table.
#
# The results from the calculation are placed in the ParameterTable as well as stored in the parameter lists which
# are contained in the MainApplication class.
#
class ParameterWindow(tk.Frame):
def __init__(self, parent, master, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.master = master
self.lowerSBound = 0
self.upperSBound = 0
self.lowerLWBound = 0
self.upperLWBound = 0
self.lowerRWBound = 0
self.upperRWBound = 0
#make the s-parameter gui here.
tk.Label(self, text='4. Finding Parameters', font=header_font).grid(row=0, column=0, columnspan=3)
#s-parameter bounds
#tk.Label(self, text='S-Parameter', font=subheader_font).grid(row=1, column=0, columnspan=3)
tk.Label(self, text='Lower S Bound (channels): ').grid(row=2, column=0)
self.lowerSEntry = tk.Entry(self)
self.lowerSEntry.grid(row=2, column=1)
tk.Label(self, text='Upper S Bound (channels): ').grid(row=3, column=0)
self.upperSEntry = tk.Entry(self)
self.upperSEntry.grid(row=3, column=1)
tk.Button(self, text='Make a Guess', command=self.makeGuessS).grid(row=3, column=2)
self.lowerSEntry.bind('<Return>', lambda event: self.updateBounds())
self.upperSEntry.bind('<Return>', lambda event: self.updateBounds())
#left w-parameter bounds
#tk.Label(self, text='Left W-Parameter', font=subheader_font).grid(row=4, column=0, columnspan=3)
tk.Label(self, text='Lower Left W Bound (channels): ').grid(row=5, column=0)
self.lowerLWEntry = tk.Entry(self)
self.lowerLWEntry.grid(row=5, column=1)
tk.Label(self, text='Upper Left W Bound (channels): ').grid(row=6, column=0)
self.upperLWEntry = tk.Entry(self)
self.upperLWEntry.grid(row=6, column=1)
tk.Button(self, text='Make a Guess', command=self.makeGuessLW).grid(row=6, column=2)
self.lowerLWEntry.bind('<Return>', lambda event: self.updateBounds())
self.upperLWEntry.bind('<Return>', lambda event: self.updateBounds())
#right w-parameter bounds
#tk.Label(self, text='Right W-Parameter', font=subheader_font).grid(row=7, column=0, columnspan=3)
tk.Label(self, text='Lower Right W Bound (channels): ').grid(row=8, column=0)
self.lowerRWEntry = tk.Entry(self)
self.lowerRWEntry.grid(row=8, column=1)
tk.Label(self, text='Upper Right W Bound (channels): ').grid(row=9, column=0)
self.upperRWEntry = tk.Entry(self)
self.upperRWEntry.grid(row=9, column=1)
tk.Button(self, text='Make a Guess', command=self.makeGuessRW).grid(row=9, column=2)
self.lowerRWEntry.bind('<Return>', lambda event: self.updateBounds())
self.upperRWEntry.bind('<Return>', lambda event: self.updateBounds())
#some settings
#tk.Label(self, text='Calculation Settings', font=subheader_font).grid(row=10, column=0, columnspan=3)
tk.Label(self, text='Use Trapezoidal Integration: ').grid(row=11, column=0)
self.isUsingTrapz = False
self.trapzToggle = tk.Button(self, text='Turn On', command=self.setIsUsingTrapz)
self.trapzToggle.grid(row=11, column=1)
tk.Label(self, text='Number of Monte-Carlo Iterations: ').grid(row=12, column=0)
self.iterationsEntry = tk.Entry(self)
self.iterationsEntry.insert(0, 500)
self.iterationsEntry.grid(row=12, column=1)
outPutRow = 13 #the row where output and calculate button starts
# the BUTTON
tk.Button(self, text='Calculate Parameters', command=self.calculateSParameters, font=('Corbel', 15, 'bold')).grid(row=outPutRow, column=0, columnspan=3)
self.progressText = tk.Label(self, text='Parameters Calculated: 0/0')
self.progressText.grid(row=outPutRow+1, column=1)
self.progressBar = ProgressBar(self, bg='white', height=20, width=200)
self.progressBar.grid(row=outPutRow+1, column=2)
#output tables
#tk.Label(self, text='S-Parameters').grid(row=outPutRow+2, column=0, columnspan=3)
self.s_table = ParamTable(self, self.master)
self.s_table.grid(row=outPutRow+3, column=0, columnspan=3)
self.s_table.set_label('S-Parameter')
#tk.Label(self, text='Left W-Parameters').grid(row=outPutRow+4, column=0, columnspan=3)
self.lw_table = ParamTable(self, self.master)
self.lw_table.grid(row=outPutRow+5, column=0, columnspan=3)
self.lw_table.set_label('Left W-Parameter')
#tk.Label(self, text='Right W-Parameters').grid(row=outPutRow+6, column=0, columnspan=3)
self.rw_table = ParamTable(self, self.master)
self.rw_table.grid(row=outPutRow+7, column=0, columnspan=3)
self.rw_table.set_label('Right W-Parameter')
def updateBounds(self):
#used to update graph and bound values as you change the entry
#s-parameter
try:
self.lowerSBound = int(self.lowerSEntry.get())
self.upperSBound = int(self.upperSEntry.get())
self.lowerSEntry.delete(0, 'end')
self.upperSEntry.delete(0, 'end')
self.lowerSEntry.insert(0, self.lowerSBound)
self.upperSEntry.insert(0, self.upperSBound)
except ValueError:
#print("Couldn't Parse S-Parameter Bound Input")
self.lowerSBound = 0
self.upperSBound = 0
self.lowerSEntry.delete(0, 'end')
self.upperSEntry.delete(0, 'end')
#left w parameter
try:
self.lowerLWBound = int(self.lowerLWEntry.get())
self.upperLWBound = int(self.upperLWEntry.get())
self.lowerLWEntry.delete(0, 'end')
self.upperLWEntry.delete(0, 'end')
self.lowerLWEntry.insert(0, self.lowerLWBound)
self.upperLWEntry.insert(0, self.upperLWBound)
except ValueError:
#print("Couldn't Parse Left W-Parameter Bound Input")
self.lowerLWBound = 0
self.upperLWBound = 0
self.lowerLWEntry.delete(0, 'end')
self.upperLWEntry.delete(0, 'end')
#right w parameter
try:
self.lowerRWBound = int(self.lowerRWEntry.get())
self.upperRWBound = int(self.upperRWEntry.get())
self.lowerRWEntry.delete(0, 'end')
self.upperRWEntry.delete(0, 'end')
self.lowerRWEntry.insert(0, self.lowerRWBound)
self.upperRWEntry.insert(0, self.upperRWBound)
except ValueError:
#print("Couldn't Parse Right W-Parameter Bound Input")
self.lowerRWBound = 0
self.upperRWBound = 0
self.lowerRWEntry.delete(0, 'end')
self.upperRWEntry.delete(0, 'end')
self.master.graph.plotData() #update display
def setIsUsingTrapz(self, **kwargs):
self.isUsingTrapz = kwargs.get('value', not self.isUsingTrapz)
if self.isUsingTrapz:
self.trapzToggle.config(text='Turn Off')
else:
self.trapzToggle.config(text='Turn On')
def makeGuessS(self):
#makes a guess for p-value bounds based off of reference sample
lower, upper = ph.make_s_parameter_bound_guess(self.master.bins, self.master.data[self.master.ref_idx])
self.lowerSBound = int(lower)
self.upperSBound = int(upper)
self.lowerSEntry.delete(0, 'end')
self.upperSEntry.delete(0, 'end')
self.lowerSEntry.insert(0, int(lower))
self.upperSEntry.insert(0, int(upper))
self.master.graph.plotData()
def makeGuessLW(self):
#makes a guess for p-value bounds based off of reference sample
lower, upper = ph.make_left_w_parameter_bound_guess(self.master.bins, self.master.data[self.master.ref_idx], (self.master.lower, self.master.upper))
self.lowerLWBound = int(lower)
self.upperLWBound = int(upper)
self.lowerLWEntry.delete(0, 'end')
self.upperLWEntry.delete(0, 'end')
self.lowerLWEntry.insert(0, int(lower))
self.upperLWEntry.insert(0, int(upper))
self.master.graph.plotData()
def makeGuessRW(self):
#makes a guess for p-value bounds based off of reference sample
lower, upper = ph.make_right_w_parameter_bound_guess(self.master.bins, self.master.data[self.master.ref_idx], (self.master.lower, self.master.upper))
self.lowerRWBound = int(lower)
self.upperRWBound = int(upper)
self.lowerRWEntry.delete(0, 'end')
self.upperRWEntry.delete(0, 'end')
self.lowerRWEntry.insert(0, int(lower))
self.upperRWEntry.insert(0, int(upper))
self.master.graph.plotData()
### CALCULATING PARAMETERS ###
def calculateSParameters(self):
self.master.backgroundWindow.calculateBackground() #calculate the background
#calculates all the s and w parameters
s_param = []
s_un = []
lw_param = []
lw_un = []
rw_param = []
rw_un = []
names = self.master.names
self.updateBounds() #this checks for empty fields
#for status text
n_of_params = 3
if self.lowerSBound == 0 and self.upperSBound == 0:
n_of_params -= 1
if self.lowerLWBound == 0 and self.upperLWBound == 0:
n_of_params -= 1
if self.lowerRWBound == 0 and self.upperRWBound == 0:
n_of_params -= 1
n_of_params *= len(names)
params_done = 0
self.progressText.config(text=f'Parameters Calculated: {0}/{n_of_params}') #update progress bar
self.progressBar.setCompletedParams(0)
self.progressBar.setNumParameters(n_of_params)
self.progressBar.reset_progress()
#get all settings and data before looping. That way if it gets changed, it won't mess up the calculation
curve_bounds = (self.master.lower, self.master.upper)
s_bounds = (self.lowerSBound, self.upperSBound)
lw_bounds = (self.lowerLWBound, self.upperLWBound)
rw_bounds = (self.lowerRWBound, self.upperRWBound)
background_type = self.master.background_type
use_trapz = self.isUsingTrapz
d_bins = self.master.bins
d_data = self.master.data
d_background = self.master.backgrounds
d_background_un = self.master.backgrounds_un
try:
n_of_iterations = int(self.iterationsEntry.get())
except ValueError:
if self.master.background_type == 'erf':
print('Could not Parse Number of Iterations, assuming 500. ')
n_of_iterations = 500
#loop through data sets
for i in range(len(names)):
if s_bounds[0] != 0 and s_bounds[1] != 0:
params_done += 1
s_parameter, s_parameter_un = ph.calculate_parameter(d_bins, d_data[i], d_background[i], d_background_un[i],
curve_bounds, s_bounds, background_type, trapz=use_trapz, progress_bar=self.progressBar, number_of_iterations=n_of_iterations)
s_param.append(s_parameter)
s_un.append(s_parameter_un)
else:
s_param.append(0)
s_un.append(0)
self.progressText.config(text=f'Parameters Calculated: {params_done}/{n_of_params}') #update progress bar
self.progressBar.setCompletedParams(params_done)
if lw_bounds[0] != 0 and lw_bounds[1] != 0:
params_done+=1
lw_parameter, lw_parameter_un = ph.calculate_parameter(d_bins, d_data[i], d_background[i], d_background_un[i],
curve_bounds, lw_bounds, background_type, trapz=use_trapz, progress_bar=self.progressBar, number_of_iterations=n_of_iterations)
lw_param.append(lw_parameter)
lw_un.append(lw_parameter_un)
else:
lw_param.append(0)
lw_un.append(0)
self.progressText.config(text=f'Parameters Calculated: {params_done}/{n_of_params}') #update progress bar
self.progressBar.setCompletedParams(params_done)
if rw_bounds[0] != 0 and rw_bounds[1] != 0:
params_done+=1
rw_parameter, rw_parameter_un = ph.calculate_parameter(d_bins, d_data[i], d_background[i], d_background_un[i],
curve_bounds, rw_bounds, background_type, trapz=use_trapz, progress_bar=self.progressBar, number_of_iterations=n_of_iterations)
rw_param.append(rw_parameter)
rw_un.append(rw_parameter_un)
else:
rw_param.append(0)
rw_un.append(0)
self.progressText.config(text=f'Parameters Calculated: {params_done}/{n_of_params}') #update progress bar
self.progressBar.setCompletedParams(params_done)
self.s_table.populateTable(names, s_param, s_un)
self.lw_table.populateTable(names, lw_param, lw_un)
self.rw_table.populateTable(names, rw_param, rw_un)
self.master.s_param = s_param
self.master.s_un = s_un
self.master.lw_param = lw_param
self.master.lw_un = lw_un
self.master.rw_param = rw_param
self.master.rw_un = rw_un
# ------------------------ PLOTS AND ADDITIONAL ANALYSIS -------
# Sometimes it is useful to do additional analysis to the parameters inside the program.
# This window contains buttons which will run various forms of additional analysis.
#
# When a button is clicked, it will open a new TopLevel window. All additional analysis GUI will
# be contained in a new window.
class AnalysisWindow(tk.Frame):
def __init__(self, parent, master, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.master = master
#additional function buttons
tk.Label(self, text='5. Plots and Analysis', font=header_font).grid(row=0, column=0, columnspan=3)
tk.Button(self, text='S vs W Plot', command=self.s_rw_plot).grid(row=1, column=0)
tk.Button(self, text='Data Plotter', command=self.data_plot).grid(row=1, column=1)
tk.Button(self, text='Export Parameters as CSV', command=self.exportParameters).grid(row=1, column=2)
def s_rw_plot(self):
#make an s vs w plot using the right w parameter
anal.SWPlotterWindow(self.master)
def data_plot(self):