-
Notifications
You must be signed in to change notification settings - Fork 121
/
AlignComponents.py
501 lines (426 loc) · 23.6 KB
/
AlignComponents.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#pylint: disable=no-init, no-name-in-module
from __future__ import (absolute_import, division, print_function)
import math
import numpy as np
from scipy.stats import chisquare
from mantid.api import PythonAlgorithm, AlgorithmFactory, MatrixWorkspaceProperty, PropertyMode, \
ITableWorkspaceProperty, FileAction, FileProperty, WorkspaceProperty, InstrumentValidator, Progress
from mantid.kernel import Direction, FloatBoundedValidator, PropertyCriterion, EnabledWhenProperty, \
logger, Quat, V3D, StringArrayProperty, StringListValidator
import mantid.simpleapi as api
class AlignComponents(PythonAlgorithm):
"""
Class to align components
"""
_optionsList = ["Xposition", "Yposition", "Zposition", "AlphaRotation", "BetaRotation", "GammaRotation"]
_optionsDict = {}
_initialPos = None
_move = False
_rotate = False
_masking = False
_eulerConvention = None
def category(self):
"""
Mantid required
"""
return "Diffraction"
def name(self):
"""
Mantid required
"""
return "AlignComponents"
def summary(self):
"""
Mantid required
"""
return "Align a component by minimising difference to an offset workspace"
#pylint: disable=too-many-locals
def PyInit(self):
self.declareProperty(ITableWorkspaceProperty("CalibrationTable", "",
optional=PropertyMode.Mandatory,
direction=Direction.Input),
doc="Calibration table, currently only uses difc")
self.declareProperty(MatrixWorkspaceProperty("MaskWorkspace", "",
optional=PropertyMode.Optional,
direction=Direction.Input),
doc="Mask workspace")
self.declareProperty(FileProperty(name="InstrumentFilename",
defaultValue="",
action=FileAction.OptionalLoad,
extensions=[".xml"]),
doc="Instrument filename")
self.declareProperty(WorkspaceProperty("Workspace", "",
validator=InstrumentValidator(),
optional=PropertyMode.Optional,
direction=Direction.Input),
doc="Workspace containing the instrument to be calibrated.")
# Source
self.declareProperty(name="FitSourcePosition", defaultValue=False,
doc="Fit the source position, changes L1 (source to sample) distance."
"Uses entire instrument. Occurs before Components are Aligned.")
# Sample
self.declareProperty(name="FitSamplePosition", defaultValue=False,
doc="Fit the sample position, changes L1 (source to sample) and L2 (sample to detector) distance."
"Uses entire instrument. Occurs before Components are Aligned.")
# List of components
self.declareProperty(StringArrayProperty("ComponentList",
direction=Direction.Input),
doc="Comma separated list on instrument components to refine.")
# X position
self.declareProperty(name="Xposition", defaultValue=False,
doc="Refine Xposition")
condition = EnabledWhenProperty("Xposition", PropertyCriterion.IsNotDefault)
self.declareProperty(name="MinXposition", defaultValue=-0.1,
validator=FloatBoundedValidator(-10.0, 10.0),
doc="Minimum relative X bound (m)")
self.setPropertySettings("MinXposition", condition)
self.declareProperty(name="MaxXposition", defaultValue=0.1,
validator=FloatBoundedValidator(-10.0, 10.0),
doc="Maximum relative X bound (m)")
self.setPropertySettings("MaxXposition", condition)
# Y position
self.declareProperty(name="Yposition", defaultValue=False,
doc="Refine Yposition")
condition = EnabledWhenProperty("Yposition", PropertyCriterion.IsNotDefault)
self.declareProperty(name="MinYposition", defaultValue=-0.1,
validator=FloatBoundedValidator(-10.0, 10.0),
doc="Minimum relative Y bound (m)")
self.setPropertySettings("MinYposition", condition)
self.declareProperty(name="MaxYposition", defaultValue=0.1,
validator=FloatBoundedValidator(-10.0, 10.0),
doc="Maximum relative Y bound (m)")
self.setPropertySettings("MaxYposition", condition)
# Z position
self.declareProperty(name="Zposition", defaultValue=False,
doc="Refine Zposition")
condition = EnabledWhenProperty("Zposition", PropertyCriterion.IsNotDefault)
self.declareProperty(name="MinZposition", defaultValue=-0.1,
validator=FloatBoundedValidator(-10.0, 10.0),
doc="Minimum relative Z bound (m)")
self.setPropertySettings("MinZposition", condition)
self.declareProperty(name="MaxZposition", defaultValue=0.1,
validator=FloatBoundedValidator(-10.0, 10.0),
doc="Maximum relative Z bound (m)")
self.setPropertySettings("MaxZposition", condition)
# euler angles convention
eulerConventions = ["ZXZ", "XYX", "YZY", "ZYZ", "XZX", "YXY", "XYZ", "YZX", "ZXY", "XZY", "ZYX", "YXZ"]
self.declareProperty(name="EulerConvention", defaultValue="YZX",
validator=StringListValidator(eulerConventions),
doc="Euler angles convention used when calculating and displaying angles,"
"eg XYZ corresponding to alpha beta gamma.")
# alpha rotation
self.declareProperty(name="AlphaRotation", defaultValue=False,
doc="Refine rotation around first axis, alpha")
condition = EnabledWhenProperty("AlphaRotation", PropertyCriterion.IsNotDefault)
self.declareProperty(name="MinAlphaRotation", defaultValue=-10.0,
validator=FloatBoundedValidator(-90, 90),
doc="Minimum relative alpha rotation (deg)")
self.setPropertySettings("MinAlphaRotation", condition)
self.declareProperty(name="MaxAlphaRotation", defaultValue=10.0,
validator=FloatBoundedValidator(-90, 90),
doc="Maximum relative alpha rotation (deg)")
self.setPropertySettings("MaxAlphaRotation", condition)
# beta rotation
self.declareProperty(name="BetaRotation", defaultValue=False,
doc="Refine rotation around seconds axis, beta")
condition = EnabledWhenProperty("BetaRotation", PropertyCriterion.IsNotDefault)
self.declareProperty(name="MinBetaRotation", defaultValue=-10.0,
validator=FloatBoundedValidator(-90, 90),
doc="Minimum relative beta rotation (deg)")
self.setPropertySettings("MinBetaRotation", condition)
self.declareProperty(name="MaxBetaRotation", defaultValue=10.0,
validator=FloatBoundedValidator(-90, 90),
doc="Maximum relative beta rotation (deg)")
self.setPropertySettings("MaxBetaRotation", condition)
# gamma rotation
self.declareProperty(name="GammaRotation", defaultValue=False,
doc="Refine rotation around third axis, gamma")
condition = EnabledWhenProperty("GammaRotation", PropertyCriterion.IsNotDefault)
self.declareProperty(name="MinGammaRotation", defaultValue=-10.0,
validator=FloatBoundedValidator(-90, 90),
doc="Minimum relative gamma rotation (deg)")
self.setPropertySettings("MinGammaRotation", condition)
self.declareProperty(name="MaxGammaRotation", defaultValue=10.0,
validator=FloatBoundedValidator(-90, 90),
doc="Maximum relative gamma rotation (deg)")
self.setPropertySettings("MaxGammaRotation", condition)
# Translation
self.setPropertyGroup("Xposition","Translation")
self.setPropertyGroup("MinXposition","Translation")
self.setPropertyGroup("MaxXposition","Translation")
self.setPropertyGroup("Yposition","Translation")
self.setPropertyGroup("MinYposition","Translation")
self.setPropertyGroup("MaxYposition","Translation")
self.setPropertyGroup("Zposition","Translation")
self.setPropertyGroup("MinZposition","Translation")
self.setPropertyGroup("MaxZposition","Translation")
# Rotation
self.setPropertyGroup("EulerConvention","Rotation")
self.setPropertyGroup("AlphaRotation","Rotation")
self.setPropertyGroup("MinAlphaRotation","Rotation")
self.setPropertyGroup("MaxAlphaRotation","Rotation")
self.setPropertyGroup("BetaRotation","Rotation")
self.setPropertyGroup("MinBetaRotation","Rotation")
self.setPropertyGroup("MaxBetaRotation","Rotation")
self.setPropertyGroup("GammaRotation","Rotation")
self.setPropertyGroup("MinGammaRotation","Rotation")
self.setPropertyGroup("MaxGammaRotation","Rotation")
def validateInputs(self):
"""
Does basic validation for inputs
"""
issues = dict()
calWS = self.getProperty('CalibrationTable').value
if 'difc' not in calWS.getColumnNames() or 'detid' not in calWS.getColumnNames():
issues['CalibrationTable'] = "Calibration table requires detid and difc"
maskWS = self.getProperty("MaskWorkspace").value
if maskWS is not None and maskWS.id() != 'MaskWorkspace':
issues['MaskWorkspace'] = "MaskWorkspace must be empty or of type \"MaskWorkspace\""
# Need to get instrument in order to check components are valid
if self.getProperty("Workspace").value is not None:
wks_name = self.getProperty("Workspace").value.name()
else:
inputFilename = self.getProperty("InstrumentFilename").value
if inputFilename == "":
issues["Workspace"] = "A Workspace or InstrumentFilename must be defined"
return issues
else:
api.LoadEmptyInstrument(Filename=inputFilename,
OutputWorkspace="alignedWorkspace")
wks_name = "alignedWorkspace"
# Check if each component listed is defined in the instrument
components = self.getProperty("ComponentList").value
if len(components) <= 0 and not self.getProperty("FitSourcePosition").value and not self.getProperty("FitSamplePosition").value:
issues['ComponentList'] = "Must supply components"
else:
components = [component for component in components
if api.mtd[wks_name].getInstrument().getComponentByName(component) is None]
if len(components) > 0:
issues['ComponentList'] = "Instrument has no component \"" \
+ ','.join(components) + "\""
# This checks that something will actually be refined,
if not (self.getProperty("Xposition").value or
self.getProperty("Yposition").value or
self.getProperty("Zposition").value or
self.getProperty("AlphaRotation").value or
self.getProperty("BetaRotation").value or
self.getProperty("GammaRotation").value):
issues["Xposition"] = "You must calibrate at least one parameter."
# Check that a position refinement is selected for sample/source
if ((self.getProperty("FitSourcePosition").value or
self.getProperty("FitSamplePosition").value) and
not (self.getProperty("Xposition").value or
self.getProperty("Yposition").value or
self.getProperty("Zposition").value)):
issues["Xposition"] = "If fitting source or sample, you must calibrate at least one position parameter."
return issues
#pylint: disable=too-many-branches
def PyExec(self):
self._eulerConvention=self.getProperty('EulerConvention').value
calWS = self.getProperty('CalibrationTable').value
calWS = api.SortTableWorkspace(calWS, Columns='detid')
maskWS = self.getProperty("MaskWorkspace").value
difc = calWS.column('difc')
if maskWS is not None:
self._masking = True
mask = maskWS.extractY().flatten()
difc = np.ma.masked_array(difc, mask)
detID = calWS.column('detid')
if self.getProperty("Workspace").value is not None:
wks_name = self.getProperty("Workspace").value.name()
else:
wks_name = "alignedWorkspace"
api.LoadEmptyInstrument(Filename=self.getProperty("InstrumentFilename").value,
OutputWorkspace=wks_name)
# Make a dictionary of what options are being refined for sample/source. No rotation.
for opt in self._optionsList[:3]:
self._optionsDict[opt] = self.getProperty(opt).value
for opt in self._optionsList[3:]:
self._optionsDict[opt] = False
# First fit L1 if selected for Source and/or Sample
for component in "Source", "Sample":
if self.getProperty("Fit"+component+"Position").value:
self._move = True
if component == "Sample":
comp = api.mtd[wks_name].getInstrument().getSample()
else:
comp = api.mtd[wks_name].getInstrument().getSource()
componentName = comp.getFullName()
logger.notice("Working on " + componentName +
" Starting position is " + str(comp.getPos()))
firstIndex = 0
lastIndex = len(difc)
if self._masking:
mask_out = mask[firstIndex:lastIndex + 1]
else:
mask_out = None
self._initialPos = [comp.getPos().getX(),
comp.getPos().getY(),
comp.getPos().getZ(),
0, 0, 0]
# Set up x0 and bounds lists
x0List = []
boundsList = []
for iopt,opt in enumerate(self._optionsList[:3]):
if self._optionsDict[opt]:
x0List.append(self._initialPos[iopt])
boundsList.append((self._initialPos[iopt] + self.getProperty("Min"+opt).value,
self._initialPos[iopt] + self.getProperty("Max"+opt).value))
results = minimize(self._minimisation_func, x0=x0List,
method='L-BFGS-B',
args=(wks_name,
componentName,
firstIndex,
lastIndex,
difc[firstIndex:lastIndex + 1],
mask_out),
bounds=boundsList)
# Apply the results to the output workspace
xmap = self._mapOptions(results.x)
# Need to grab the component again, as things have changed
api.MoveInstrumentComponent(wks_name, componentName,
X=xmap[0],
Y=xmap[1],
Z=xmap[2],
RelativePosition=False)
comp = api.mtd[wks_name].getInstrument().getComponentByName(componentName)
logger.notice("Finished " + componentName +
" Final position is " + str(comp.getPos()))
self._move = False
# Now fit all the components if any
components = self.getProperty("ComponentList").value
# Make a dictionary of what options are being refined.
for opt in self._optionsList:
self._optionsDict[opt] = self.getProperty(opt).value
self._move = (self._optionsDict["Xposition"] or self._optionsDict["Yposition"] or self._optionsDict["Zposition"])
self._rotate = (self._optionsDict["AlphaRotation"] or self._optionsDict["BetaRotation"] or self._optionsDict["GammaRotation"])
prog = Progress(self, start=0, end=1, nreports=len(components))
for component in components:
comp = api.mtd[wks_name].getInstrument().getComponentByName(component)
firstDetID = self._getFirstDetID(comp)
firstIndex = detID.index(firstDetID)
lastDetID = self._getLastDetID(comp)
lastIndex = detID.index(lastDetID)
if lastDetID - firstDetID != lastIndex - firstIndex:
raise RuntimeError("Calibration detid doesn't match instrument")
eulerAngles = comp.getRotation().getEulerAngles(self._eulerConvention)
logger.notice("Working on " + comp.getFullName() +
" Starting position is " + str(comp.getPos()) +
" Starting rotation is " + str(eulerAngles))
x0List = []
self._initialPos = [comp.getPos().getX(), comp.getPos().getY(), comp.getPos().getZ(),
eulerAngles[0], eulerAngles[1], eulerAngles[2]]
boundsList = []
if self._masking:
mask_out = mask[firstIndex:lastIndex + 1]
if mask_out.sum() == mask_out.size:
self.log().warning("All pixels in '%s' are masked. Skipping calibration." % component)
continue
else:
mask_out = None
for iopt,opt in enumerate(self._optionsList):
if self._optionsDict[opt]:
x0List.append(self._initialPos[iopt])
boundsList.append((self._initialPos[iopt] + self.getProperty("Min"+opt).value,
self._initialPos[iopt] + self.getProperty("Max"+opt).value))
results = minimize(self._minimisation_func, x0=x0List,
method='L-BFGS-B',
args=(wks_name,
component,
firstIndex,
lastIndex,
difc[firstIndex:lastIndex + 1],
mask_out),
bounds=boundsList)
# Apply the results to the output workspace
xmap = self._mapOptions(results.x)
if self._move:
api.MoveInstrumentComponent(wks_name, component, X=xmap[0], Y=xmap[1], Z=xmap[2],
RelativePosition=False)
if self._rotate:
(rotw, rotx, roty, rotz) = self._eulerToAngleAxis(xmap[3], xmap[4], xmap[5], self._eulerConvention)
api.RotateInstrumentComponent(wks_name, component, X=rotx, Y=roty, Z=rotz, Angle=rotw,
RelativeRotation=False)
# Need to grab the component again, as things have changed
comp = api.mtd[wks_name].getInstrument().getComponentByName(component)
logger.notice("Finshed " + comp.getFullName() +
" Final position is " + str(comp.getPos()) +
" Final rotation is " + str(comp.getRotation().getEulerAngles(self._eulerConvention)))
prog.report()
logger.notice("Results applied to workspace "+wks_name)
#pylint: disable=too-many-arguments
def _minimisation_func(self, x_0, wks_name, component, firstIndex, lastIndex, difc, mask):
"""
Basic minimization function used. Returns the chisquared difference between the expected
difc and the new difc after the component has been moved or rotated.
"""
xmap = self._mapOptions(x_0)
if self._move:
api.MoveInstrumentComponent(wks_name, component, X=xmap[0], Y=xmap[1], Z=xmap[2], RelativePosition=False)
if self._rotate:
(rotw, rotx, roty, rotz) = self._eulerToAngleAxis(xmap[3], xmap[4], xmap[5], self._eulerConvention) # YZX
api.RotateInstrumentComponent(wks_name, component, X=rotx, Y=roty, Z=rotz, Angle=rotw,
RelativeRotation=False)
api.CalculateDIFC(InputWorkspace=wks_name, OutputWorkspace=wks_name)
difc_new = api.mtd[wks_name].extractY().flatten()[firstIndex:lastIndex + 1]
if self._masking:
difc_new = np.ma.masked_array(difc_new, mask)
return chisquare(f_obs=difc, f_exp=difc_new)[0]
def _getFirstDetID(self, component):
"""
recursive search to find first detID of a component
"""
if component.type() == 'DetectorComponent' or component.type() == 'RectangularDetectorPixel':
return component.getID()
else:
return self._getFirstDetID(component[0])
def _getLastDetID(self, component):
"""
recursive search to find last detID of a component
"""
if component.type() == 'DetectorComponent' or component.type() == 'RectangularDetectorPixel':
return component.getID()
else:
return self._getLastDetID(component[component.nelements() - 1])
def _mapOptions(self, inX):
"""
Creates an array combining the refining and constant variables
This is required because scipy.optimise.minimise expect a constant
number of variable, so need to be able to maps any number of
inputs to six outputs.
"""
x0_index = 0
out = []
for opt in self._optionsList:
if self._optionsDict[opt]:
out.append(inX[x0_index])
x0_index += 1
else:
out.append(self._initialPos[self._optionsList.index(opt)])
return out
def _eulerToQuat(self, alpha, beta, gamma, convention):
"""
Convert Euler angles to a quaternion
"""
getV3D = {'X': V3D(1, 0, 0), 'Y': V3D(0, 1, 0), 'Z': V3D(0, 0, 1)}
return (Quat(alpha, getV3D[convention[0]]) * Quat(beta, getV3D[convention[1]]) *
Quat(gamma, getV3D[convention[2]]))
def _eulerToAngleAxis(self, alpha, beta, gamma, convention):
"""
Convert Euler angles to a angle rotation around an axis
"""
quat = self._eulerToQuat(alpha, beta, gamma, convention)
if quat[0] == 1:
return 0, 0, 0, 1
deg = math.acos(quat[0])
scale = math.sin(deg)
deg *= 360.0 / math.pi
ax0 = quat[1] / scale
ax1 = quat[2] / scale
ax2 = quat[3] / scale
return deg, ax0, ax1, ax2
try:
from scipy.optimize import minimize
AlgorithmFactory.subscribe(AlignComponents)
except ImportError:
logger.debug('Failed to subscribe algorithm AlignComponets; cannot import minimize from scipy.optimize')