-
Notifications
You must be signed in to change notification settings - Fork 122
/
SANSILLParameterScan.py
254 lines (203 loc) · 11 KB
/
SANSILLParameterScan.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
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source
# & Institut Laue - Langevin
# SPDX - License - Identifier: GPL - 3.0 +
from mantid.api import *
from mantid.kernel import IntBoundedValidator, Direction
from mantid.simpleapi import *
from SANSILLAutoProcess import needs_loading, needs_processing
class SANSILLParameterScan(DataProcessorAlgorithm):
"""
Performs treatment for scans along a parameter for D16.
"""
progress = None
reduction_type = None
sample = None
absorber = None
container = None
sensitivity = None
default_mask = None
output = None
normalise = None
output2D = None
output_joined = None
observable = None
pixel_y_min = None
pixel_y_max = None
default_mask_ws = None
sensitivity_ws = None
def category(self):
return 'ILL\\SANS;ILL\\Auto'
def summary(self):
return 'Integrate SANS scan data along a parameter'
def seeAlso(self):
return []
def name(self):
return 'SANSILLParameterScan'
def validateInputs(self):
issues = dict()
if not (self.getPropertyValue('OutputJoinedWorkspace') or self.getPropertyValue("OutputWorkspace")):
issues["OutputJoinedWorkspace"] = "Please provide either OutputJoinedWorkspace, OutputWorkspace or both."
issues["OutputWorkspace"] = "Please provide either OutputJoinedWorkspace, OutputWorkspace or both."
if self.getProperty('PixelYmin').value > self.getProperty("PixelYmax").value:
issues["PixelYMin"] = "YMin needs to be lesser than YMax"
issues["PixelYMax"] = "YMax needs to be greater than YMin"
return issues
def setUp(self):
self.sample = self.getPropertyValue('SampleRun')
self.absorber = self.getPropertyValue('AbsorberRun').replace(',', '+')
self.container = self.getPropertyValue('ContainerRun').replace(',', '+')
self.sensitivity = self.getPropertyValue('SensitivityMap')
self.default_mask = self.getPropertyValue('DefaultMaskFile')
self.normalise = self.getPropertyValue('NormaliseBy')
self.output2D = self.getPropertyValue('OutputWorkspace')
self.output_joined = self.getPropertyValue('OutputJoinedWorkspace')
self.observable = self.getPropertyValue('Observable')
self.pixel_y_min = self.getProperty('PixelYMin').value
self.pixel_y_max = self.getProperty('PixelYMax').value
self.progress = Progress(self, start=0.0, end=1.0, nreports=10)
def checkPixelY(self, height):
if self.pixel_y_max > height:
self.pixel_y_max = height
logger.warning("PixelYMax value is too high. Reduced to {0}.".format(self.pixel_y_max))
def PyInit(self):
self.declareProperty(WorkspaceProperty('OutputWorkspace', '', direction=Direction.Output,
optional=PropertyMode.Optional),
doc="The output workspace containing the 2D reduced data.")
self.declareProperty(WorkspaceProperty('OutputJoinedWorkspace', '', direction=Direction.Output,
optional=PropertyMode.Optional),
doc="The output workspace containing all the reduced data, before grouping.")
self.declareProperty(FileProperty('SampleRun', '', action=FileAction.Load, extensions=['nxs']),
doc='Sample scan file.')
self.declareProperty(FileProperty('AbsorberRun', '', action=FileAction.OptionalLoad, extensions=['nxs']),
doc='Absorber run.')
self.declareProperty(FileProperty('ContainerRun', '', action=FileAction.OptionalLoad, extensions=['nxs']),
doc='Empty container run.')
self.setPropertyGroup('SampleRun', 'Numors')
self.setPropertyGroup('AbsorberRun', 'Numors')
self.setPropertyGroup('ContainerRun', 'Numors')
self.declareProperty(FileProperty('SensitivityMap', '', action=FileAction.OptionalLoad, extensions=['nxs']),
doc='File containing the map of relative detector efficiencies.')
self.declareProperty(FileProperty('DefaultMaskFile', '', action=FileAction.OptionalLoad, extensions=['nxs']),
doc='File containing the default mask to be applied to all the detector configurations.')
self.copyProperties("SANSILLReduction", ["NormaliseBy"], version=2)
self.declareProperty('Observable', 'Omega.value',
doc='Parameter from the sample logs along which the scan is made')
self.declareProperty('PixelYMin', 3, validator=IntBoundedValidator(lower=0),
doc='Minimal y-index taken in the integration. Default is based on D16B geometry.')
self.declareProperty('PixelYMax', 189, validator=IntBoundedValidator(lower=0),
doc='Maximal y-index taken in the integration. Default is based on D16B geometry.')
self.setPropertyGroup('SensitivityMap', 'Options')
self.setPropertyGroup('DefaultMaskFile', 'Options')
self.setPropertyGroup('NormaliseBy', 'Options')
self.setPropertyGroup('Observable', 'Options')
self.setPropertyGroup('PixelYMin', 'Options')
self.setPropertyGroup('PixelYMax', 'Options')
def PyExec(self):
self.setUp()
_, load_ws_name = needs_loading(self.sample, "Load")
Load(Filename=self.sample, OutputWorkspace=load_ws_name, startProgress=0, endProgress=0.7)
sorted_data = load_ws_name + '_sorted' if not self.output_joined else self.output_joined
SortXAxis(InputWorkspace=load_ws_name, OutputWorkspace=sorted_data,
startProgress=0.75, endProgress=0.8)
DeleteWorkspace(Workspace=load_ws_name)
self.load_input_files()
self.reduce(sorted_data)
if self.observable == "Omega.value":
mtd[sorted_data].getAxis(0).setUnit("label").setLabel(self.observable, 'degrees')
self.group_detectors(sorted_data)
if not self.output_joined:
DeleteWorkspace(Workspace=sorted_data)
else:
self.setProperty('OutputJoinedWorkspace', mtd[self.output_joined])
self.progress.report("Convert axis.")
ConvertSpectrumAxis(InputWorkspace=self.output2D,
OutputWorkspace=self.output2D,
Target="SignedInPlaneTwoTheta",
startProgress=0.95,
endProgress=1)
Transpose(InputWorkspace=self.output2D, OutputWorkspace=self.output2D)
self.setProperty('OutputWorkspace', mtd[self.output2D])
def load_input_files(self):
"""
Load input files provided by the user if needed
"""
load_sensitivity, self.sensitivity_ws = needs_loading(self.sensitivity, 'Sensitivity')
if load_sensitivity:
self.progress.report(8, 'Loading sensitivity')
LoadNexusProcessed(Filename=self.sensitivity, OutputWorkspace=self.sensitivity_ws)
load_default_mask, self.default_mask_ws = needs_loading(self.default_mask, "DefaultMask")
if load_default_mask:
self.progress.report(0, 'Loading default mask')
LoadNexusProcessed(Filename=self.default_mask, OutputWorkspace=self.default_mask_ws)
def reduce(self, sorted_ws):
"""
Do the standard data reduction using SANSILLReduction
@param sorted_ws: the name of the sample workspace with X axis holding the sorted scanned parameter
"""
process_absorber, absorber_name = needs_processing(self.absorber, 'DarkCurrent')
if process_absorber:
self.progress.report(0, 'Processing dark current')
SANSILLReduction(Run=self.absorber,
ProcessAs='DarkCurrent',
NormaliseBy=self.normalise,
OutputWorkspace=absorber_name,
Version=2)
process_container, container_name = needs_processing(self.container, 'Container')
if process_container:
self.progress.report(0, 'Processing container')
SANSILLReduction(Run=self.container,
ProcessAs='EmptyContainer',
OutputWorkspace=container_name,
AbsorberInputWorkspace=absorber_name,
CacheSolidAngle=True,
DefaultMaskWorkspace=self.default_mask,
NormaliseBy=self.normalise,
Version=2)
# reduce the sample data
self.progress.report(0, "Reducing data.")
SANSILLReduction(SampleWorkspace=sorted_ws,
DarkCurrentWorkspace=absorber_name,
EmptyContainerWorkspace=container_name,
SensitivityWorkspace=self.sensitivity_ws,
DefaultMaskWorkspace=self.default_mask_ws,
NormaliseBy=self.normalise,
OutputWorkspace=sorted_ws,
startProgress=0.8,
endProgress=0.95,
Version=2)
def group_detectors(self, ws):
"""
Average each tube / wire value of the detector.
@param ws: the name of the ws to group
"""
instrument = mtd[ws].getInstrument()
detector = instrument.getComponentByName("detector")
if "detector-width" in detector.getParameterNames() and "detector-height" in detector.getParameterNames():
width = int(detector.getNumberParameter("detector-width")[0])
height = int(detector.getNumberParameter("detector-height")[0])
else:
raise RuntimeError('No width or height found for this instrument. Unable to group detectors.')
self.checkPixelY(height)
grouping = create_detector_grouping(self.pixel_y_min, self.pixel_y_max, width, height)
GroupDetectors(InputWorkspace=ws,
OutputWorkspace=self.output2D,
GroupingPattern=grouping,
Behaviour="Average")
def create_detector_grouping(y_min, y_max, detector_width, detector_height):
"""
Create the pixel grouping for the detector. Shape is assumed to be D16's.
The pixel grouping consists of the vertical columns of pixels of the detector.
:param y_min: index of the first line to take on each column.
:param y_max: index of the last line to take on each column.
:param detector_width: the total number of column of pixel on the detector.
:param detector_height: the total number of lines of pixel on the detector.
"""
grouping = []
for i in range(detector_width):
grouping.append(str(i * detector_height + y_min) + "-" + str(i * detector_height + y_max - 1))
grouping = ",".join(grouping)
return grouping
AlgorithmFactory.subscribe(SANSILLParameterScan)