-
Notifications
You must be signed in to change notification settings - Fork 1
/
EchidnaCalibrationUI.py
executable file
·205 lines (167 loc) · 6.92 KB
/
EchidnaCalibrationUI.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
# Script control setup area
__script__.title = 'ECH Calibration'
__script__.version = '1.0'
import sys
''' User Interface '''
from datetime import date
nowtime = date.today()
# Input
in_van_run = Par('file', '')
in_van_run.ext = '*.hdf'
in_van_show = Act('in_van_show_proc()', 'Show')
in_bkg_run = Par('file', '')
in_bkg_run.ext = '*.hdf'
in_bkg_show = Act('in_bkg_show_proc()', 'Show')
Group('Input').add(in_van_run, in_van_show, in_bkg_run, in_bkg_show)
# Output Folder
out_folder = Par('file')
out_folder.dtype = 'folder'
Group('Output Folder').add(out_folder)
# Normalization
# We link the normalisation sources to actual dataset locations right here, right now
norm_table = {'Monitor 1':'bm1_counts','Monitor 2':'bm2_counts',
'Monitor 3':'bm3_counts','Detector time':'detector_time'}
norm_apply = Par('bool' , 'True' )
norm_reference = Par('string', 'bm1 counts', options = norm_table.keys())
Group('Normalization').add(norm_apply, norm_reference)
# Vertical Tube Correction List
vtc_make = Par('bool' , 'True')
vtc_name = Par('string', "vertical_offsets_"+str(nowtime)+".txt")
vtc_algorithm = Par('string', 'Edges',
options = ['Edges','Vertically Centered Average'])
Group('Vertical Tube Correction List').add(vtc_make, vtc_algorithm, vtc_name)
# Efficiency Correction Map
eff_make = Par('bool' , 'True')
eff_name = Par('string', "eff_"+str(nowtime)+".cif")
eff_transpose = Par('bool', 'False')
eff_transpose.title = "Output transposed result also"
eff_std_range = Par('float' , '1.8' )
Group('Efficiency Correction Map').add(eff_make, eff_name, eff_std_range,eff_transpose)
''' Load Preferences '''
calibration_output_dir = __UI__.getPreference("au.gov.ansto.bragg.echidna.ui:calibration_output_dir")
normalisation_reference = __UI__.getPreference("au.gov.ansto.bragg.echidna.ui:normalisation_reference")
if calibration_output_dir:
out_folder.value = calibration_output_dir
if normalisation_reference:
if normalisation_reference == 'bm1_counts':
norm_reference.value = 'bm1 counts'
elif normalisation_reference == 'bm2_counts':
norm_reference.value = 'bm2 counts'
elif normalisation_reference == 'bm3_counts':
norm_reference.value = 'bm3 counts'
elif normalisation_reference == 'detector_counts':
norm_reference.value = 'detector counts'
''' Button Actions '''
def show_helper(filename, plot, pre_title = ''):
if filename:
ds = Dataset(str(filename))
plot.clear()
if ds.ndim == 4:
plot.set_dataset(ds[0, 0])
plot.title = ds.title + " (first frame)"
elif ds.ndim == 3:
plot.set_dataset(ds[0])
plot.title = ds.title + " (first frame)"
else:
plot.set_dataset(ds)
if pre_title:
plot.title = pre_title + plot.title
# show input Vanadium Map
def in_van_show_proc():
show_helper(in_van_run.value, Plot1, "Vanadium Map: ")
# show input Background Map
def in_bkg_show_proc():
global Plot2
show_helper(in_bkg_run.value, Plot2, "Background Map: ")
def get_norm_ref(ds, ref_name):
if ref_name == 'bm1 counts':
return ds.bm1_counts
elif ref_name == 'bm2 counts':
return ds.bm2_counts
elif ref_name == 'bm3 counts':
return ds.bm3_counts
elif ref_name == 'detector time':
return ds.detector_time
else:
raise Exception('specify normalization reference')
''' Script Actions '''
# This function is called when pushing the Run button in the control UI.
def __run_script__(fns):
from Reduction import reduction,calibrations
from os.path import join
df.datasets.clear()
# check input
if not in_van_run.value:
print 'specify vanadium run'
return
if not in_bkg_run.value:
print 'specify background run'
return
van = Dataset(str(in_van_run.value)).get_reduced()
van.location = str(in_van_run.value)
bkg = Dataset(str(in_bkg_run.value)).get_reduced()
bkg.location = str(in_bkg_run.value)
# check if input is correct
if van.ndim != 3:
raise AttributeError('van.ndim != 3')
if bkg.ndim != 3:
raise AttributeError('bkg.ndim != 3')
if van.axes[0].title != 'azimuthal_angle':
raise AttributeError('van.axes[0].title != azimuthal_angle')
if bkg.axes[0].title != 'azimuthal_angle':
raise AttributeError('bkg.axes[0].title != azimuthal_angle')
if van.shape != bkg.shape: # checks number of frames and detector pixel dimensions
raise AttributeError('van.shape != bkg.shape')
# check if input needs to be normalized
if norm_apply.value:
norm_ref = str(norm_reference.value)
# check if vertical tube correction list needs to be created
vtc_filename = join(str(out_folder.value), str(vtc_name.value))
if vtc_make.value:
if van.ndim > 2:
summed = zeros(van.shape[1:])
for step in van:
summed = summed + step
input_ds = summed
else:
input_ds = van
input_ds.location = van.location
if str(vtc_algorithm.value) == 'Vertically Centered Average':
calibrations.getVerticalCorrectionList(input_ds,
output_filename=vtc_filename)
elif str(vtc_algorithm.value) == 'Edges':
calibrations.getVerticalEdges(
input_ds,output_filename=vtc_filename)
else:
print 'Vertical offset algorithm not recognised'
if eff_make.value:
eff = calibrations.calc_eff_mark2(van, bkg,vtc_filename , norm_ref=norm_table[norm_ref])
output_filename = join(str(out_folder.value), str(eff_name.value))
# write out new efficiency file
import time
print 'Writing efficiency file at %s' % time.asctime()
calibrations.output_2d_efficiencies(eff, output_filename, comment='Created by Gumtree')
print 'Finished writing at %s' % time.asctime()
if eff_transpose.value==True:
output_filename = output_filename + '-transposed'
calibrations.output_2d_efficiencies(eff,output_filename,comment='Created by Gumtree',
transpose=True)
print 'Finished writing transposed file at %s' % time.asctime()
# dispose
def __dispose__():
global Plot1
global Plot2
global Plot3
Plot1.clear()
Plot2.clear()
Plot3.clear()
''' Quick-Fix '''
def run_action(act):
act.set_running_status()
try:
exec(act.command)
act.set_done_status()
except:
act.set_error_status()
traceback.print_exc(file = sys.stdout)
raise Exception, 'Error in running <' + act.text + '>'