-
Notifications
You must be signed in to change notification settings - Fork 122
/
IndirectCalibration.py
215 lines (164 loc) · 7.88 KB
/
IndirectCalibration.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
#pylint: disable=no-init,too-many-instance-attributes
from mantid.kernel import *
from mantid.simpleapi import *
from mantid.api import *
import os.path
class IndirectCalibration(DataProcessorAlgorithm):
_input_files = None
_out_ws = None
_peak_range = None
_back_range = None
_spec_range = None
_intensity_scale = None
_run_numbers = None
def category(self):
return 'Workflow\\Inelastic;Inelastic\\Calibration'
def summary(self):
return 'Creates a calibration workspace from a White-Beam Vanadium run.'
def PyInit(self):
self.declareProperty(StringArrayProperty(name='InputFiles'),
doc='Comma separated list of input files')
self.declareProperty(IntArrayProperty(name='DetectorRange',
values=[0, 1],
validator=IntArrayMandatoryValidator()),
doc='Range of detectors.')
self.declareProperty(FloatArrayProperty(name='PeakRange',
values=[0.0, 100.0],
validator=FloatArrayMandatoryValidator()),
doc='Time of flight range over the peak.')
self.declareProperty(FloatArrayProperty(name='BackgroundRange',
values=[0.0, 1000.0],
validator=FloatArrayMandatoryValidator()),
doc='Time of flight range over the background.')
self.declareProperty(name='ScaleFactor', defaultValue=1.0,
doc='Factor by which to scale the result.')
self.declareProperty(name='LoadLogFiles', defaultValue=False,
doc = 'Option to load log files.' )
self.declareProperty(WorkspaceProperty('OutputWorkspace', '',
direction=Direction.Output),
doc='Output workspace for calibration data.')
def validateInputs(self):
"""
Validates input ranges.
"""
issues = dict()
issues['DetectorRange'] = self._validate_range('DetectorRange')
issues['PeakRange'] = self._validate_range('PeakRange')
issues['BackgroundRange'] = self._validate_range('BackgroundRange')
return issues
def _validate_range(self, property_name):
"""
Validates a range property.
@param property_name Name of the property to validate
@returns String detailing error, None if no error
"""
prop_range = self.getProperty(property_name).value
if len(prop_range) == 2:
if prop_range[0] > prop_range[1]:
return 'Invalid range'
else:
return 'Incorrect number of values (should be 2)'
return None
def PyExec(self):
from IndirectCommon import get_run_number
self._setup()
runs = []
self._run_numbers = []
load_prog = Progress(self, start=0.0, end=0.30, nreports=len(self._input_files))
i = 0
for in_file in self._input_files:
(_, filename) = os.path.split(in_file)
(root, _) = os.path.splitext(filename)
try:
Load(Filename=in_file,
OutputWorkspace=root,
SpectrumMin=int(self._spec_range[0]),
SpectrumMax=int(self._spec_range[1]),
LoadLogFiles=self.getProperty('LoadLogFiles').value)
runs.append(root)
self._run_numbers.append(get_run_number(root))
except (RuntimeError,ValueError) as exc:
logger.error('Could not load raw file "%s": %s' % (in_file, str(exc)))
load_prog.report('Loading file: ' + str(i))
i += 1
calib_ws_name = 'calibration'
merge_prog = Progress(self, start=0.30, end=0.40, nreports=2)
if len(runs) > 1:
MergeRuns(InputWorkspaces=",".join(runs),
OutputWorkspace=calib_ws_name)
merge_prog.report('Workspaces merged')
factor = 1.0 / len(runs)
Scale(InputWorkspace=calib_ws_name,
OutputWorkspace=calib_ws_name,
Factor=factor)
merge_prog.report('Scaling complete')
else:
calib_ws_name = runs[0]
merge_prog.report()
workflow_prog = Progress(self, start=0.40, end=0.90, nreports=10)
workflow_prog.report('Calculating flat background')
CalculateFlatBackground(InputWorkspace=calib_ws_name,
OutputWorkspace=calib_ws_name,
StartX=self._back_range[0],
EndX=self._back_range[1],
Mode='Mean')
workflow_prog.report('Masking detectors')
number_historgrams = mtd[calib_ws_name].getNumberHistograms()
ws_mask, num_zero_spectra = FindDetectorsOutsideLimits(InputWorkspace=calib_ws_name,
OutputWorkspace='__temp_ws_mask')
DeleteWorkspace(ws_mask)
workflow_prog.report('Integrating calibration file')
Integration(InputWorkspace=calib_ws_name,
OutputWorkspace=calib_ws_name,
RangeLower=self._peak_range[0],
RangeUpper=self._peak_range[1])
workflow_prog.report('Summing Spectra')
temp_sum = SumSpectra(InputWorkspace=calib_ws_name,
OutputWorkspace='__temp_sum')
total = temp_sum.readY(0)[0]
DeleteWorkspace(temp_sum)
if self._intensity_scale is None:
self._intensity_scale = 1 / (total / (number_historgrams - num_zero_spectra))
workflow_prog.report('Scaling calibration')
Scale(InputWorkspace=calib_ws_name,
OutputWorkspace=self._out_ws,
Factor=self._intensity_scale,
Operation='Multiply')
# Remove old workspaces
if len(runs) > 1:
for run in runs:
DeleteWorkspace(Workspace=run)
workflow_prog.report('Deleting workspaces')
self._add_logs()
self.setProperty('OutputWorkspace', self._out_ws)
def _setup(self):
"""
Gets properties.
"""
self._input_files = self.getProperty('InputFiles').value
self._out_ws = self.getPropertyValue('OutputWorkspace')
self._peak_range = self.getProperty('PeakRange').value
self._back_range = self.getProperty('BackgroundRange').value
self._spec_range = self.getProperty('DetectorRange').value
self._intensity_scale = self.getProperty('ScaleFactor').value
if self._intensity_scale == 1.0:
self._intensity_scale = None
def _add_logs(self):
"""
Handles adding sample logs.
"""
# Add sample logs to output workspace
sample_logs = [('calib_peak_min', self._peak_range[0]),
('calib_peak_max', self._peak_range[1]),
('calib_back_min', self._back_range[0]),
('calib_back_max', self._back_range[1]),
('calib_run_numbers', ','.join(self._run_numbers))]
if self._intensity_scale is not None:
sample_logs.append(('calib_scale_factor', self._intensity_scale))
log_alg = self.createChildAlgorithm(name='AddSampleLogMultiple', startProgress=0.9,
endProgress=1.0, enableLogging=True)
log_alg.setProperty('Workspace', self._out_ws)
log_alg.setProperty('LogNames', [log[0] for log in sample_logs])
log_alg.setProperty('LogValues', [log[1] for log in sample_logs])
# Register algorithm with Mantid
AlgorithmFactory.subscribe(IndirectCalibration)