-
Notifications
You must be signed in to change notification settings - Fork 122
/
ReflectometryILLSumForeground.py
317 lines (279 loc) · 14.9 KB
/
ReflectometryILLSumForeground.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
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function)
from mantid.api import (AlgorithmFactory, DataProcessorAlgorithm, MatrixWorkspaceProperty, PropertyMode, WorkspaceUnitValidator)
from mantid.kernel import (CompositeValidator, Direction, FloatArrayBoundedValidator, FloatArrayProperty,
IntArrayBoundedValidator, IntArrayLengthValidator, IntArrayProperty, Property, StringListValidator)
from mantid.simpleapi import (AddSampleLog, CreateWorkspace, CropWorkspace, Divide, ExtractSingleSpectrum,
Multiply, RebinToWorkspace, ReflectometrySumInQ)
import numpy
import ReflectometryILL_common as common
from scipy import constants
class Sample:
AUTO = 'Sample Flatness AUTO' # To be used in the future.
BENT = 'Bent Sample'
FLAT = 'Flat Sample'
class Prop:
CLEANUP = 'Cleanup'
DIRECT_FOREGROUND_WS = 'DirectForegroundWorkspace'
FLAT_SAMPLE = 'FlatSample'
FOREGROUND_INDICES = 'Foreground'
INPUT_WS = 'InputWorkspace'
OUTPUT_WS = 'OutputWorkspace'
SUBALG_LOGGING = 'SubalgorithmLogging'
SUM_TYPE = 'SummationType'
WAVELENGTH_RANGE = 'WavelengthRange'
class SumType:
IN_LAMBDA = 'SumInLambda'
IN_Q = 'SumInQ'
class SubalgLogging:
OFF = 'Logging OFF'
ON = 'Logging ON'
class ReflectometryILLSumForeground(DataProcessorAlgorithm):
def category(self):
"""Return algorithm's categories."""
return 'ILL\\Reflectometry;Workflow\\Reflectometry'
def name(self):
"""Return the name of the algorithm."""
return 'ReflectometryILLSumForeground'
def summary(self):
"""Return a summary of the algorithm."""
return 'Sums foreground pixels in selected summation mode, optionally converting to reflectivity.'
def seeAlso(self):
"""Return a list of related algorithm names."""
return ['ReflectometryILLConvertToQ', 'ReflectometryILLPolarizationCor', 'ReflectometryILLPreprocess']
def version(self):
"""Return the version of the algorithm."""
return 1
def PyExec(self):
"""Execute the algorithm."""
self._subalgLogging = self.getProperty(Prop.SUBALG_LOGGING).value == SubalgLogging.ON
cleanupMode = self.getProperty(Prop.CLEANUP).value
self._cleanup = common.WSCleanup(cleanupMode, self._subalgLogging)
wsPrefix = self.getPropertyValue(Prop.OUTPUT_WS)
self._names = common.WSNameSource(wsPrefix, cleanupMode)
ws = self._inputWS()
sumType = self._sumType()
if sumType == SumType.IN_LAMBDA:
ws = self._sumForegroundInLambda(ws)
ws = self._divideByDirect(ws)
self._addSumTypeToLogs(ws, SumType.IN_LAMBDA)
else:
ws = self._divideByDirect(ws)
ws = self._sumForegroundInQ(ws)
self._addSumTypeToLogs(ws, SumType.IN_Q)
ws = self._applyWavelengthRange(ws)
self._finalize(ws)
def PyInit(self):
"""Initialize the input and output properties of the algorithm."""
threeNonnegativeInts = CompositeValidator()
threeNonnegativeInts.add(IntArrayLengthValidator(3))
nonnegativeInts = IntArrayBoundedValidator()
nonnegativeInts.setLower(0)
threeNonnegativeInts.add(nonnegativeInts)
nonnegativeFloatArray = FloatArrayBoundedValidator()
nonnegativeFloatArray.setLower(0.)
self.declareProperty(MatrixWorkspaceProperty(Prop.INPUT_WS,
defaultValue='',
direction=Direction.Input,
validator=WorkspaceUnitValidator('Wavelength')),
doc='An input workspace (units wavelength) to be integrated.')
self.declareProperty(MatrixWorkspaceProperty(Prop.OUTPUT_WS,
defaultValue='',
direction=Direction.Output),
doc='The integrated foreground divided by the summed direct beam.')
self.declareProperty(Prop.SUBALG_LOGGING,
defaultValue=SubalgLogging.OFF,
validator=StringListValidator([SubalgLogging.OFF, SubalgLogging.ON]),
doc='Enable or disable child algorithm logging.')
self.declareProperty(Prop.CLEANUP,
defaultValue=common.WSCleanup.ON,
validator=StringListValidator([common.WSCleanup.ON, common.WSCleanup.OFF]),
doc='Enable or disable intermediate workspace cleanup.')
self.declareProperty(Prop.SUM_TYPE,
defaultValue=SumType.IN_LAMBDA,
validator=StringListValidator([SumType.IN_LAMBDA, SumType.IN_Q]),
doc='Type of summation to perform.')
self.declareProperty(Prop.FLAT_SAMPLE,
defaultValue=Sample.FLAT,
validator=StringListValidator([Sample.FLAT, Sample.BENT]),
doc='For SumInQ option, determines if the summation should be done for a flat or bent sample.')
self.declareProperty(MatrixWorkspaceProperty(Prop.DIRECT_FOREGROUND_WS,
defaultValue='',
direction=Direction.Input,
optional=PropertyMode.Optional,
validator=WorkspaceUnitValidator('Wavelength')),
doc='Summed direct beam workspace if output in reflectivity is required.')
self.declareProperty(IntArrayProperty(Prop.FOREGROUND_INDICES,
values=[Property.EMPTY_INT, Property.EMPTY_INT, Property.EMPTY_INT],
validator=threeNonnegativeInts),
doc='A three element array of foreground start, centre and end workspace indices.')
self.declareProperty(FloatArrayProperty(Prop.WAVELENGTH_RANGE,
values=[0.],
validator=nonnegativeFloatArray),
doc='The wavelength bounds when summing in Q.')
def validateInputs(self):
"""Validate the algorithm's input properties."""
issues = dict()
if self.getProperty(Prop.SUM_TYPE).value == SumType.IN_Q:
if self.getProperty(Prop.DIRECT_FOREGROUND_WS).isDefault:
issues[Prop.DIRECT_FOREGROUND_WS] = 'Direct foreground workspace is needed for summing in Q.'
if not self.getProperty(Prop.DIRECT_FOREGROUND_WS).isDefault:
directWS = self.getProperty(Prop.DIRECT_FOREGROUND_WS).value
if directWS.getNumberHistograms() != 1:
issues[Prop.DIRECT_FOREGROUND_WS] = 'The workspace has histograms != 1. Was foreground summation forgotten?'
wRange = self.getProperty(Prop.WAVELENGTH_RANGE).value
if len(wRange) == 2 and wRange[0] >= wRange[1]:
issues[Prop.WAVELENGTH_RANGE] = 'Upper limit is smaller than the lower limit.'
if len(wRange) > 2:
issues[Prop.WAVELENGTH_RANGE] = 'The range should be in the form [min] or [min, max].'
return issues
def _addSumTypeToLogs(self, ws, sumType):
"""Add a sum type entry to sample logs."""
AddSampleLog(
Workspace=ws,
LogName=common.SampleLogs.SUM_TYPE,
LogText=sumType,
LogType='String',
EnableLogging=self._subalgLogging)
def _applyWavelengthRange(self, ws):
"""Cut wavelengths outside the wavelength range from a TOF workspace."""
if self.getProperty(Prop.WAVELENGTH_RANGE).isDefault:
return ws
wRange = self.getProperty(Prop.WAVELENGTH_RANGE).value
rangeProp = {'XMin': wRange[0]}
if len(wRange) == 2:
rangeProp['XMax'] = wRange[1]
croppedWSName = self._names.withSuffix('cropped')
croppedWS = CropWorkspace(InputWorkspace=ws,
OutputWorkspace=croppedWSName,
EnableLogging=self._subalgLogging,
**rangeProp)
self._cleanup.cleanup(ws)
return croppedWS
def _checkIfFlatSample(self):
"""Returns true if sample is deemed 'flat' for SumInQ."""
flatness = self.getProperty(Prop.FLAT_SAMPLE).value
# The 'Sample Flatness AUTO' option should calculate the answer here someday.
return flatness == Sample.FLAT
def _correctForChopperOpenings(self, ws, directWS):
"""Correct reflectivity values if chopper openings between RB and DB differ."""
def opening(instrumentName, logs, Xs):
chopperGap = common.chopperPairDistance(logs, instrumentName)
chopperPeriod = 60. / common.chopperSpeed(logs, instrumentName)
openingAngle = common.chopperOpeningAngle(logs, instrumentName)
return chopperGap * constants.m_n / constants.h / chopperPeriod * Xs + openingAngle / 360.
instrumentName = ws.getInstrument().getName()
Xs = ws.readX(0)
if ws.isHistogramData():
Xs = (Xs[:-1] + Xs[1:]) / 2.
reflectedOpening = opening(instrumentName, ws.run(), Xs)
directOpening = opening(instrumentName, directWS.run(), Xs)
corFactorWSName = self._names.withSuffix('chopper_opening_correction_factors')
corFactorWS = CreateWorkspace(
OutputWorkspace=corFactorWSName,
DataX=ws.readX(0),
DataY=reflectedOpening / directOpening,
UnitX=ws.getAxis(0).getUnit().unitID(),
ParentWorkspace=ws,
EnableLogging=self._subalgLogging)
correctedWSName = self._names.withSuffix('corrected_by_chopper_opening')
correctedWS = Multiply(
LHSWorkspace=ws,
RHSWorkspace=corFactorWS,
OutputWorkspace=correctedWSName,
EnableLogging=self._subalgLogging)
self._cleanup.cleanup(corFactorWS)
self._cleanup.cleanup(ws)
return correctedWS
def _divideByDirect(self, ws):
"Divide ws by the direct beam."
if self.getProperty(Prop.DIRECT_FOREGROUND_WS).isDefault:
return ws
directWS = self.getProperty(Prop.DIRECT_FOREGROUND_WS).value
rebinnedWSName = self._names.withSuffix('rebinned')
rebinnedWS = RebinToWorkspace(WorkspaceToRebin=ws,
WorkspaceToMatch=directWS,
OutputWorkspace=rebinnedWSName,
EnableLogging=self._subalgLogging)
self._cleanup.cleanup(ws)
reflectivityWSName = self._names.withSuffix('reflectivity')
reflectivityWS = Divide(LHSWorkspace=rebinnedWS,
RHSWorkspace=directWS,
OutputWorkspace=reflectivityWSName,
EnableLogging=self._subalgLogging)
self._cleanup.cleanup(rebinnedWS)
reflectivityWS = self._correctForChopperOpenings(reflectivityWS, directWS)
reflectivityWS.setYUnit('Reflectivity')
reflectivityWS.setYUnitLabel('Reflectivity')
return reflectivityWS
def _finalize(self, ws):
"""Set OutputWorkspace to ws and clean up."""
self.setProperty(Prop.OUTPUT_WS, ws)
self._cleanup.cleanup(ws)
self._cleanup.finalCleanup()
def _foregroundIndices(self, ws):
"""Return a three-element list of foreground start, center and end workspace indices."""
foregroundProp = self.getProperty(Prop.FOREGROUND_INDICES)
if not foregroundProp.isDefault:
return foregroundProp.value
logs = ws.run()
if not logs.hasProperty(common.SampleLogs.FOREGROUND_START):
raise RuntimeError("The sample logs are missing the '" + common.SampleLogs.FOREGROUND_START + "' entry.")
start = logs.getProperty(common.SampleLogs.FOREGROUND_START).value
if not logs.hasProperty(common.SampleLogs.FOREGROUND_CENTRE):
raise RuntimeError("The sample logs are missing the '" + common.SampleLogs.FOREGROUND_CENTRE + "' entry.")
centre = logs.getProperty(common.SampleLogs.FOREGROUND_CENTRE).value
if not logs.hasProperty(common.SampleLogs.FOREGROUND_END):
raise RuntimeError("The sample logs are missing the '" + common.SampleLogs.FOREGROUND_END + "' entry.")
end = logs.getProperty(common.SampleLogs.FOREGROUND_END).value
return [start, centre, end]
def _inputWS(self):
"Return the input workspace."
ws = self.getProperty(Prop.INPUT_WS).value
self._cleanup.protect(ws)
return ws
def _sumForegroundInLambda(self, ws):
"""Sum the foreground region into a single histogram."""
foreground = self._foregroundIndices(ws)
sumIndices = [i for i in range(foreground[0], foreground[2] + 1)]
beamPosIndex = foreground[1]
foregroundWSName = self._names.withSuffix('foreground_grouped')
foregroundWS = ExtractSingleSpectrum(InputWorkspace=ws,
OutputWorkspace=foregroundWSName,
WorkspaceIndex=beamPosIndex,
EnableLogging=self._subalgLogging)
maxIndex = ws.getNumberHistograms() - 1
foregroundYs = foregroundWS.dataY(0)
foregroundEs = foregroundWS.dataE(0)
numpy.square(foregroundEs, out=foregroundEs)
for i in sumIndices:
if i == beamPosIndex:
continue
if i < 0 or i > maxIndex:
self.log().warning('Foreground partially out of the workspace.')
ys = ws.readY(i)
foregroundYs += ys
es = ws.readE(i)
foregroundEs += es**2
numpy.sqrt(foregroundEs, out=foregroundEs)
self._cleanup.cleanup(ws)
return foregroundWS
def _sumForegroundInQ(self, ws):
"""Sum the foreground region into a single histogram using the coherent method."""
foreground = self._foregroundIndices(ws)
sumIndices = [i for i in range(foreground[0], foreground[2] + 1)]
beamPosIndex = foreground[1]
isFlatSample = self._checkIfFlatSample()
sumWSName = self._names.withSuffix('summed_in_Q')
sumWS = ReflectometrySumInQ(
InputWorkspace=ws,
OutputWorkspace=sumWSName,
InputWorkspaceIndexSet=sumIndices,
BeamCentre=beamPosIndex,
FlatSample=isFlatSample,
EnableLogging=self._subalgLogging)
self._cleanup.cleanup(ws)
return sumWS
def _sumType(self):
return self.getProperty(Prop.SUM_TYPE).value
AlgorithmFactory.subscribe(ReflectometryILLSumForeground)