Newer
Older
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
"""
SANS-specific implementation of the Reducer. The SANSReducer class implements
a predefined set of reduction steps to be followed. The actual ReductionStep objects
executed for each of those steps can be modified.
"""
from reduction import Reducer
from reduction import ReductionStep
import sans_reduction_steps
from mantidsimple import *
## Version number
__version__ = '0.0'
class SANSReducer(Reducer):
"""
SANS-specific implementation of the Reducer
"""
## Normalization options
#TODO: those also correspond to the timer and monitor spectra -> store this in instr conf instead
NORMALIZATION_NONE = None
NORMALIZATION_TIME = 1
NORMALIZATION_MONITOR = 0
## Beam center finder ReductionStep object
_beam_finder = None
## Normalization option
_normalizer = None
## Dark current data file
_dark_current_subtracter = None
## Sensitivity correction ReductionStep object
_sensitivity_correcter = None
## Solid angle correcter
_solid_angle_correcter = None
## Azimuthal averaging
_azimuthal_averager = None
## Transmission calculator
_transmission_calculator = None
## Masking step
_mask = None
## Output saving step
_save_iq = None
## Background subtracter
_background_subtracter = None
## Data loader
_data_loader = None
## Extra data files
_extra_files = {}
geometry_correcter = None
def __init__(self):
super(SANSReducer, self).__init__()
# Default beam finder
self._beam_finder = sans_reduction_steps.BaseBeamFinder(0,0)
# Default normalization
self._normalizer = sans_reduction_steps.Normalize(SANSReducer.NORMALIZATION_TIME)
# Default data loader
self._data_loader = sans_reduction_steps.LoadRun()
# Default mask object
self._mask = sans_reduction_steps.Mask()
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
def set_normalizer(self, option):
"""
Set normalization option (time or monitor)
@param option: normalization option
"""
if option not in (None, SANSReducer.NORMALIZATION_TIME,
SANSReducer.NORMALIZATION_MONITOR):
raise RuntimeError, "SANSReducer.set_normalization: unrecognized normalization option."
if option is None:
self._normalizer = None
else:
self._normalizer = sans_reduction_steps.Normalize(option)
def set_geometry_correcter(self, correcter):
"""
Set the ReductionStep object that takes care of the geometry correction
@param subtracter: ReductionStep object
"""
if issubclass(correcter.__class__, ReductionStep) or correcter is None:
self.geometry_correcter = correcter
else:
raise RuntimeError, "Reducer.set_geometry_correcter expects an object of class ReductionStep"
def set_transmission(self, trans):
"""
Set the reduction step that will apply the transmission correction
@param trans: ReductionStep object
"""
if issubclass(trans.__class__, sans_reduction_steps.BaseTransmission) or trans is None:
self._transmission_calculator = trans
else:
raise RuntimeError, "Reducer.set_transmission expects an object of class ReductionStep"
def set_mask(self, mask):
"""
Set the reduction step that will apply the mask
@param mask: ReductionStep object
"""
if issubclass(mask.__class__, ReductionStep) or mask is None:
self._mask = mask
else:
raise RuntimeError, "Reducer.set_mask expects an object of class ReductionStep"
def get_mask(self): return self._mask
def get_beam_center(self):
"""
Return the beam center position
"""
return self._beam_finder.get_beam_center()
def set_beam_finder(self, finder):
"""
Set the ReductionStep object that finds the beam center
@param finder: BaseBeamFinder object
"""
if issubclass(finder.__class__, sans_reduction_steps.BaseBeamFinder) or finder is None:
self._beam_finder = finder
else:
raise RuntimeError, "Reducer.set_beam_finder expects an object of class ReductionStep"
def set_data_loader(self, loader):
"""
Set the loader for all data files
@param loader: ReductionStep object
"""
if issubclass(loader.__class__, ReductionStep):
self._data_loader = loader
else:
raise RuntimeError, "Reducer.set_data_loader expects an object of class ReductionStep"
def get_data_loader(self): return self._data_loader
def set_sensitivity_correcter(self, correcter):
"""
Set the ReductionStep object that applies the sensitivity correction.
The ReductionStep object stores the sensitivity, so that the object
can be re-used on multiple data sets and the sensitivity will not be
recalculated.
@param correcter: ReductionStep object
"""
if issubclass(correcter.__class__, ReductionStep) or correcter is None:
self._sensitivity_correcter = correcter
else:
raise RuntimeError, "Reducer.set_sensitivity_correcter expects an object of class ReductionStep"
def set_dark_current_subtracter(self, subtracter):
"""
Set the ReductionStep object that subtracts the dark current from the data.
The loaded dark current is stored by the ReductionStep object so that the
subtraction can be applied to multiple data sets without reloading it.
@param subtracter: ReductionStep object
"""
if issubclass(subtracter.__class__, ReductionStep) or subtracter is None:
self._dark_current_subtracter = subtracter
else:
raise RuntimeError, "Reducer.set_dark_current expects an object of class ReductionStep"
def set_solid_angle_correcter(self, correcter):
"""
Set the ReductionStep object that performs the solid angle correction.
@param subtracter: ReductionStep object
"""
if issubclass(correcter.__class__, ReductionStep) or correcter is None:
self._solid_angle_correcter = correcter
else:
raise RuntimeError, "Reducer.set_solid_angle_correcter expects an object of class ReductionStep"
def set_azimuthal_averager(self, averager):
"""
Set the ReductionStep object that performs azimuthal averaging
and transforms the 2D reduced data set into I(Q).
@param averager: ReductionStep object
"""
if issubclass(averager.__class__, ReductionStep) or averager is None:
self._azimuthal_averager = averager
else:
raise RuntimeError, "Reducer.set_azimuthal_averager expects an object of class ReductionStep"
def set_save_Iq(self, save_iq):
"""
Set the ReductionStep object that saves the I(q) output
@param averager: ReductionStep object
"""
if issubclass(save_iq.__class__, ReductionStep) or save_iq is None:
self._save_iq = save_iq
else:
raise RuntimeError, "Reducer.set_save_Iq expects an object of class ReductionStep"
def set_background(self, data_file=None):
"""
Sets the background data to be subtracted from sample data files
@param data_file: Name of the background file
"""
if data_file is None:
self._background_subtracter = None
else:
# Check that the file exists
self._full_file_path(data_file)
self._background_subtracter = sans_reduction_steps.SubtractBackground(data_file)
def set_bck_transmission(self, trans):
"""
Set the reduction step that will apply the transmission correction
@param trans: ReductionStep object
"""
if issubclass(trans.__class__, sans_reduction_steps.BaseTransmission) or trans is None:
self._background_subtracter.set_transmission(trans)
else:
raise RuntimeError, "Reducer.set_bck_transmission expects an object of class ReductionStep"
def pre_process(self):
"""
Reduction steps that are meant to be executed only once per set
of data files. After this is executed, all files will go through
the list of reduction steps.
"""
if self._beam_finder is not None:
result = self._beam_finder.execute(self)
self.log_text += "%s\n" % str(result)
# Create the list of reduction steps
self._to_steps()
def post_process(self): return NotImplemented
def _2D_steps(self):
"""
Creates a list of reduction steps to be applied to
each data set, including the background file.
Only the steps applied to a data set
before azimuthal averaging are included.
"""
reduction_steps = []
# Load file
reduction_steps.append(self._data_loader)
# Normalize
if self._normalizer is not None:
reduction_steps.append(self._normalizer)
# Dark current subtraction
if self._dark_current_subtracter is not None:
reduction_steps.append(self._dark_current_subtracter)
# Mask
if self._mask is not None:
reduction_steps.append(self._mask)
# Sensitivity correction
if self._sensitivity_correcter is not None:
reduction_steps.append(self._sensitivity_correcter)
# Solid angle correction
if self._solid_angle_correcter is not None:
reduction_steps.append(self._solid_angle_correcter)
return reduction_steps
def _to_steps(self):
"""
Creates a list of reduction steps for each data set
following a predefined reduction approach. For each
predefined step, we check that a ReductionStep object
exists to take of it. If one does, we append it to the
list of steps to be executed.
"""
# Get the basic 2D steps
self._reduction_steps = self._2D_steps()
# Apply transmission correction
if self._transmission_calculator is not None:
self._reduction_steps.append(self._transmission_calculator)
# Subtract the background
if self._background_subtracter is not None:
self.append_step(self._background_subtracter)
# Perform azimuthal averaging
if self._azimuthal_averager is not None:
self.append_step(self._azimuthal_averager)
# Save output to file
if self._save_iq is not None:
self.append_step(self._save_iq)
if self.geometry_correcter is not None:
self.append_step(self.geometry_correcter)