forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DSFinterp.py
129 lines (116 loc) · 6.67 KB
/
DSFinterp.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
# 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 & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
#pylint: disable=no-init,invalid-name
from mantid.api import PythonAlgorithm, AlgorithmFactory
import mantid.simpleapi
from mantid.kernel import StringListValidator, FloatArrayProperty, FloatArrayMandatoryValidator,\
StringArrayProperty, StringArrayMandatoryValidator, Direction, logger, EnabledWhenProperty, PropertyCriterion
class DSFinterp(PythonAlgorithm):
channelgroup = None
def category(self):
return "Transforms\\Smoothing"
def name(self):
return 'DSFinterp'
def summary(self):
return 'Given a set of parameter values {Ti} and corresponding structure factors S(Q,E,Ti), this algorithm '\
'interpolates S(Q,E,T) for any value of parameter T within the range spanned by the {Ti} set.'
def PyInit(self):
arrvalidator = StringArrayMandatoryValidator()
lrg='Input'
self.declareProperty(StringArrayProperty('Workspaces', values=[], validator=arrvalidator,
direction=Direction.Input), doc='list of input workspaces')
self.declareProperty('LoadErrors', True, direction=Direction.Input,
doc='Do we load error data contained in the workspaces?')
self.declareProperty(FloatArrayProperty('ParameterValues', values=[],
validator=FloatArrayMandatoryValidator(),direction=Direction.Input),
doc='list of input parameter values')
self.setPropertyGroup('Workspaces', lrg)
self.setPropertyGroup('LoadErrors', lrg)
self.setPropertyGroup('ParameterValues', lrg)
self.declareProperty('LocalRegression', True, direction=Direction.Input, doc='Perform running local-regression?')
condition = EnabledWhenProperty("LocalRegression", PropertyCriterion.IsDefault)
self.declareProperty('RegressionWindow', 6, direction=Direction.Input, doc='window size for the running local-regression')
self.setPropertySettings("RegressionWindow", condition)
regtypes = [ 'linear', 'quadratic']
self.declareProperty('RegressionType', 'quadratic', StringListValidator(regtypes),
direction=Direction.Input, doc='type of local-regression; linear and quadratic are available')
self.setPropertySettings("RegressionType", condition)
lrg = 'Running Local Regression Options'
self.setPropertyGroup('LocalRegression', lrg)
self.setPropertyGroup('RegressionWindow', lrg)
self.setPropertyGroup('RegressionType', lrg)
lrg='Output'
self.declareProperty(FloatArrayProperty('TargetParameters', values=[], ), doc="Parameters to interpolate the structure factor")
self.declareProperty(StringArrayProperty('OutputWorkspaces', values=[], validator=arrvalidator),
doc='list of output workspaces to save the interpolated structure factors')
self.setPropertyGroup('TargetParameters', lrg)
self.setPropertyGroup('OutputWorkspaces', lrg)
self.channelgroup = None
def areWorkspacesCompatible(self, a, b):
sizeA = a.blocksize() * a.getNumberHistograms()
sizeB = b.blocksize() * b.getNumberHistograms()
return sizeA == sizeB
def PyExec(self):
# Check congruence of workspaces
workspaces = self.getProperty('Workspaces').value
fvalues = self.getProperty('ParameterValues').value
if len(workspaces) != len(fvalues):
mesg = 'Number of Workspaces and ParameterValues should be the same'
#logger.error(mesg)
raise IndexError(mesg)
for workspace in workspaces[1:]:
if not self.areWorkspacesCompatible(mantid.mtd[workspaces[0]],mantid.mtd[workspace]):
mesg = 'Workspace {0} incompatible with {1}'.format(workspace, workspaces[0])
logger.error(mesg)
raise ValueError(mesg)
# Load the workspaces into a group of dynamic structure factors
from dsfinterp.dsf import Dsf
from dsfinterp.dsfgroup import DsfGroup
from dsfinterp.channelgroup import ChannelGroup
dsfgroup = DsfGroup()
for idsf in range(len(workspaces)):
dsf = Dsf()
dsf.Load( mantid.mtd[workspaces[idsf]] )
if not self.getProperty('LoadErrors').value:
dsf.errors = None # do not incorporate error data
dsf.SetFvalue( fvalues[idsf] )
dsfgroup.InsertDsf(dsf)
# Create the intepolator if not instantiated before
if not self.channelgroup:
self.channelgroup = ChannelGroup()
self.channelgroup.InitFromDsfGroup(dsfgroup)
localregression = self.getProperty('LocalRegression').value
if localregression:
regressiontype = self.getProperty('RegressionType').value
windowlength = self.getProperty('RegressionWindow').value
self.channelgroup.InitializeInterpolator(running_regr_type=regressiontype, windowlength=windowlength)
else:
self.channelgroup.InitializeInterpolator(windowlength=0)
# Invoke the interpolator and generate the output workspaces
targetfvalues = self.getProperty('TargetParameters').value
for targetfvalue in targetfvalues:
if targetfvalue < min(fvalues) or targetfvalue > max(fvalues):
mesg = 'Target parameters should lie in [{0}, {1}]'.format(min(fvalues),max(fvalues))
logger.error(mesg)
raise ValueError(mesg)
outworkspaces = self.getProperty('OutputWorkspaces').value
if len(targetfvalues) != len(outworkspaces):
mesg = 'Number of OutputWorkspaces and TargetParameters should be the same'
logger.error(mesg)
raise IndexError(mesg)
for i in range(len(targetfvalues)):
dsf = self.channelgroup( targetfvalues[i] )
outws = mantid.simpleapi.CloneWorkspace( mantid.mtd[workspaces[0]], OutputWorkspace=outworkspaces[i])
dsf.Save(outws) # overwrite dataY and dataE
#############################################################################################
#pylint: disable=unused-import
try:
import dsfinterp # noqa
AlgorithmFactory.subscribe(DSFinterp)
except ImportError:
logger.debug('Failed to subscribe algorithm DSFinterp; Python package dsfinterp'
'may be missing (https://pypi.python.org/pypi/dsfinterp)')