-
Notifications
You must be signed in to change notification settings - Fork 122
/
ElasticWindowMultiple.py
332 lines (253 loc) · 13.4 KB
/
ElasticWindowMultiple.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#pylint: disable=no-init,too-many-instance-attributes,too-many-branches
from mantid.simpleapi import *
from mantid.kernel import *
from mantid.api import *
def _normalize_to_lowest_temp(elt_ws_name):
"""
Normalise a workspace to the lowest temperature run.
@param elt_ws_name Name of the ELT workspace
"""
num_hist = mtd[elt_ws_name].getNumberHistograms()
# Normalize each spectrum in the workspace
for idx in range(0, num_hist):
y_vals = mtd[elt_ws_name].readY(idx)
scale = 1.0 / y_vals[0]
y_vals_scaled = scale * y_vals
mtd[elt_ws_name].setY(idx, y_vals_scaled)
class ElasticWindowMultiple(DataProcessorAlgorithm):
_sample_log_name = None
_sample_log_value = None
_input_workspaces = None
_q_workspace = None
_q2_workspace = None
_elf_workspace = None
_elt_workspace = None
_integration_range_start = None
_integration_range_end = None
_background_range_start = None
_background_range_end = None
def category(self):
return 'Workflow\\Inelastic;Inelastic\\Indirect'
def summary(self):
return 'Performs the ElasticWindow algorithm over multiple input workspaces'
def PyInit(self):
self.declareProperty(WorkspaceGroupProperty('InputWorkspaces', '', Direction.Input),
doc='Grouped input workspaces')
self.declareProperty(name='IntegrationRangeStart', defaultValue=0.0,
doc='Start of integration range in time of flight')
self.declareProperty(name='IntegrationRangeEnd', defaultValue=0.0,
doc='End of integration range in time of flight')
self.declareProperty(name='BackgroundRangeStart', defaultValue=Property.EMPTY_DBL,
doc='Start of background range in time of flight')
self.declareProperty(name='BackgroundRangeEnd', defaultValue=Property.EMPTY_DBL,
doc='End of background range in time of flight')
self.declareProperty(name='SampleEnvironmentLogName', defaultValue='sample',
doc='Name of the sample environment log entry')
sampEnvLogVal_type = ['last_value', 'average']
self.declareProperty('SampleEnvironmentLogValue', 'last_value',
StringListValidator(sampEnvLogVal_type),
doc='Value selection of the sample environment log entry')
self.declareProperty(WorkspaceProperty('OutputInQ', '', Direction.Output),
doc='Output workspace in Q')
self.declareProperty(WorkspaceProperty('OutputInQSquared', '', Direction.Output),
doc='Output workspace in Q Squared')
self.declareProperty(WorkspaceProperty('OutputELF', '', Direction.Output,
PropertyMode.Optional),
doc='Output workspace ELF')
self.declareProperty(WorkspaceProperty('OutputELT', '', Direction.Output,
PropertyMode.Optional),
doc='Output workspace ELT')
def validateInputs(self):
issues = dict()
background_range_start = self.getProperty('BackgroundRangeStart').value
background_range_end = self.getProperty('BackgroundRangeEnd').value
if background_range_start != Property.EMPTY_DBL and background_range_end == Property.EMPTY_DBL:
issues['BackgroundRangeEnd'] = 'If background range start was given and background range end must also be provided.'
if background_range_start == Property.EMPTY_DBL and background_range_end != Property.EMPTY_DBL:
issues['BackgroundRangeStart'] = 'If background range end was given and background range start must also be provided.'
return issues
def PyExec(self):
from IndirectImport import import_mantidplot
from IndirectCommon import getInstrRun
# Do setup
self._setup()
logger.debug('in_ws:'+str(type(self._input_workspaces)))
# Get input workspaces
input_workspace_names = self._input_workspaces.getNames()
# Lists of input and output workspaces
q_workspaces = list()
q2_workspaces = list()
run_numbers = list()
temperatures = list()
# Perform the ElasticWindow algorithms
for input_ws in input_workspace_names:
logger.information('Running ElasticWindow for workspace: %s' % input_ws)
q_ws = '__' + input_ws + '_q'
q2_ws = '__' + input_ws + '_q2'
if self._background_range_start != Property.EMPTY_DBL and self._background_range_end != Property.EMPTY_DBL:
ElasticWindow(InputWorkspace=input_ws,
OutputInQ=q_ws,
OutputInQSquared=q2_ws,
IntegrationRangeStart=self._integration_range_start,
IntegrationRangeEnd=self._integration_range_end,
BackgroundRangeStart=self._background_range_start,
BackgroundRangeEnd=self._background_range_end)
else:
ElasticWindow(InputWorkspace=input_ws,
OutputInQ=q_ws,
OutputInQSquared=q2_ws,
IntegrationRangeStart=self._integration_range_start,
IntegrationRangeEnd=self._integration_range_end)
Logarithm(InputWorkspace=q2_ws,
OutputWorkspace=q2_ws)
q_workspaces.append(q_ws)
q2_workspaces.append(q2_ws)
# Get the run number
run_no = getInstrRun(input_ws)[1]
run_numbers.append(run_no)
# Get the sample temperature
temp = self._get_temperature(input_ws)
if temp is not None:
temperatures.append(temp)
else:
# No need to output a tmperature workspace if there are no temperatures
self._elt_workspace = ''
logger.information('Creating Q and Q^2 workspaces')
if len(input_workspace_names) == 1:
# Just rename single workspaces
RenameWorkspace(InputWorkspace=q_workspaces[0],
OutputWorkspace=self._q_workspace)
RenameWorkspace(InputWorkspace=q2_workspaces[0],
OutputWorkspace=self._q2_workspace)
else:
# Append the spectra of the first two workspaces
AppendSpectra(InputWorkspace1=q_workspaces[0],
InputWorkspace2=q_workspaces[1],
OutputWorkspace=self._q_workspace)
AppendSpectra(InputWorkspace1=q2_workspaces[0],
InputWorkspace2=q2_workspaces[1],
OutputWorkspace=self._q2_workspace)
# Append to the spectra of each remaining workspace
for idx in range(2, len(input_workspace_names)):
AppendSpectra(InputWorkspace1=self._q_workspace,
InputWorkspace2=q_workspaces[idx],
OutputWorkspace=self._q_workspace)
AppendSpectra(InputWorkspace1=self._q2_workspace,
InputWorkspace2=q2_workspaces[idx],
OutputWorkspace=self._q2_workspace)
# Delete the output workspaces from the ElasticWindow algorithms
for q_ws in q_workspaces:
DeleteWorkspace(q_ws)
for q2_ws in q2_workspaces:
DeleteWorkspace(q2_ws)
logger.information('Setting vertical axis units and values')
# Set the verical axis units
v_axis_is_temp = len(input_workspace_names) == len(temperatures)
if v_axis_is_temp:
logger.notice('Vertical axis is in temperature')
unit = ('Temperature', 'K')
else:
logger.notice('Vertical axis is in run number')
unit = ('Run No', 'last 3 digits')
# Create a new vertical axis for the Q and Q**2 workspaces
q_ws_axis = NumericAxis.create(len(input_workspace_names))
q_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])
q2_ws_axis = NumericAxis.create(len(input_workspace_names))
q2_ws_axis.setUnit("Label").setLabel(unit[0], unit[1])
# Set the vertical axis values
for idx in range(0, len(input_workspace_names)):
if v_axis_is_temp:
q_ws_axis.setValue(idx, float(temperatures[idx]))
q2_ws_axis.setValue(idx, float(temperatures[idx]))
else:
q_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))
q2_ws_axis.setValue(idx, float(run_numbers[idx][-3:]))
# Add the new vertical axis to each workspace
mtd[self._q_workspace].replaceAxis(1, q_ws_axis)
mtd[self._q2_workspace].replaceAxis(1, q2_ws_axis)
# Process the ELF workspace
if self._elf_workspace != '':
logger.information('Creating ELF workspace')
Transpose(InputWorkspace=self._q_workspace,
OutputWorkspace=self._elf_workspace)
SortXAxis(InputWorkspace=self._elf_workspace,
OutputWorkspace=self._elf_workspace)
self.setProperty('OutputELF', self._elf_workspace)
# Do temperature normalisation
if self._elt_workspace != '':
logger.information('Creating ELT workspace')
# If the ELT workspace was not already created then create it here,
# otherwise just clone it
if self._elf_workspace == '':
Transpose(InputWorkspace=self._q_workspace,
OutputWorkspace=self._elt_workspace)
SortXAxis(InputWorkspace=self._elt_workspace,
OutputWorkspace=self._elt_workspace)
else:
CloneWorkspace(InputWorkspace=self._elf_workspace,
OutputWorkspace=self._elt_workspace)
_normalize_to_lowest_temp(self._elt_workspace)
self.setProperty('OutputELT', self._elt_workspace)
# Set the output workspace
self.setProperty('OutputInQ', self._q_workspace)
self.setProperty('OutputInQSquared', self._q2_workspace)
def _setup(self):
"""
Gets algorithm properties.
"""
self._sample_log_name = self.getPropertyValue('SampleEnvironmentLogName')
self._sample_log_value = self.getPropertyValue('SampleEnvironmentLogValue')
self._input_workspaces = self.getProperty('InputWorkspaces').value
self._q_workspace = self.getPropertyValue('OutputInQ')
self._q2_workspace = self.getPropertyValue('OutputInQSquared')
self._elf_workspace = self.getPropertyValue('OutputELF')
self._elt_workspace = self.getPropertyValue('OutputELT')
self._integration_range_start = self.getProperty('IntegrationRangeStart').value
self._integration_range_end = self.getProperty('IntegrationRangeEnd').value
self._background_range_start = self.getProperty('BackgroundRangeStart').value
self._background_range_end = self.getProperty('BackgroundRangeEnd').value
def _get_temperature(self, ws_name):
"""
Gets the sample temperature for a given workspace.
@param ws_name Name of workspace
@returns Temperature in Kelvin or None if not found
"""
from IndirectCommon import getInstrRun
instr, run_number = getInstrRun(ws_name)
facility = config.getFacility()
pad_num = facility.instrument(instr).zeroPadding(int(run_number))
zero_padding = '0' * (pad_num - len(run_number))
run_name = instr + zero_padding + run_number
log_filename = run_name.upper() + '.log'
run = mtd[ws_name].getRun()
if self._sample_log_name in run:
# Look for temperature in logs in workspace
tmp = run[self._sample_log_name].value
value_action = {'last_value': lambda x: x[len(x)-1],
'average': lambda x: x.mean()
}
temp = value_action[self._sample_log_value](tmp)
logger.debug('Temperature %d K found for run: %s' % (temp, run_name))
return temp
else:
# Logs not in workspace, try loading from file
logger.information('Log parameter not found in workspace. Searching for log file.')
log_path = FileFinder.getFullPath(log_filename)
if log_path != '':
# Get temperature from log file
LoadLog(Workspace=ws_name, Filename=log_path)
run_logs = mtd[ws_name].getRun()
if self._sample_log_name in run_logs:
tmp = run_logs[self._sample_log_name].value
temp = tmp[len(tmp) - 1]
logger.debug('Temperature %d K found for run: %s' % (temp, run_name))
return temp
else:
logger.warning('Log entry %s for run %s not found' % (self._sample_log_name, run_name))
else:
logger.warning('Log file for run %s not found' % run_name)
# Can't find log file
logger.warning('No temperature found for run: %s' % run_name)
return None
# Register algorithm with Mantid
AlgorithmFactory.subscribe(ElasticWindowMultiple)