/
IqtFitSequential.py
247 lines (201 loc) · 11.5 KB
/
IqtFitSequential.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
from __future__ import (absolute_import, division, print_function)
from mantid import logger, AlgorithmFactory
from mantid.api import *
from mantid.kernel import *
import mantid.simpleapi as ms
class IqtFitSequential(PythonAlgorithm):
_input_ws = None
_function = None
_fit_type = None
_start_x = None
_end_x = None
_spec_min = None
_spec_max = None
_intensities_constrained = None
_minimizer = None
_max_iterations = None
_result_name = None
_result_ws = None
_parameter_name = None
_fit_group_name = None
def category(self):
return "Workflow\\MIDAS"
def summary(self):
return "Fits an \*\_iqt file generated by I(Q,t) sequentially."
def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty('InputWorkspace', '', direction=Direction.Input),
doc='The _iqt.nxs InputWorkspace used by the algorithm')
self.declareProperty(name='Function', defaultValue='',
doc='The function to use in fitting')
self.declareProperty(name='FitType', defaultValue='',
doc='The type of fit being carried out')
self.declareProperty(name='StartX', defaultValue=0.0,
validator=FloatBoundedValidator(0.0),
doc="The first value for X")
self.declareProperty(name='EndX', defaultValue=0.2,
validator=FloatBoundedValidator(0.0),
doc="The last value for X")
self.declareProperty(name='SpecMin', defaultValue=0,
validator=IntBoundedValidator(0),
doc='Minimum spectra in the workspace to fit')
self.declareProperty(name='SpecMax', defaultValue=1,
validator=IntBoundedValidator(0),
doc='Maximum spectra in the workspace to fit')
self.declareProperty(name='Minimizer', defaultValue='Levenberg-Marquardt',
doc='The minimizer to use in fitting')
self.declareProperty(name="MaxIterations", defaultValue=500,
validator=IntBoundedValidator(0),
doc="The Maximum number of iterations for the fit")
self.declareProperty(name='ConstrainIntensities', defaultValue=False,
doc="If the Intensities should be constrained during the fit")
self.declareProperty(name='ExtractMembers', defaultValue=False,
doc="If true, then each member of the fit will be extracted, into their "
"own workspace")
self.declareProperty(MatrixWorkspaceProperty('OutputResultWorkspace', '', direction=Direction.Output),
doc='The output workspace containing the results of the fit data')
self.declareProperty(ITableWorkspaceProperty('OutputParameterWorkspace', '', direction=Direction.Output),
doc='The output workspace containing the parameters for each fit')
self.declareProperty(WorkspaceGroupProperty('OutputWorkspaceGroup', '', direction=Direction.Output),
doc='The OutputWorkspace group Data, Calc and Diff, values for the fit of each spectra')
def validateInputs(self):
self._get_properties()
issues = dict()
if self._start_x >= self._end_x:
issues['StartX'] = 'StartX must be more than EndX'
return issues
def _get_properties(self):
self._input_ws = self.getProperty('InputWorkspace').value
self._function = self.getProperty('Function').value
self._fit_type = self.getProperty('FitType').value
self._start_x = self.getProperty('StartX').value
self._end_x = self.getProperty('EndX').value
self._spec_min = self.getProperty('SpecMin').value
self._spec_max = self.getProperty('SpecMax').value
self._intensities_constrained = self.getProperty('ConstrainIntensities').value
self._do_extract_members = self.getProperty('ExtractMembers').value
self._minimizer = self.getProperty('Minimizer').value
self._max_iterations = self.getProperty('MaxIterations').value
self._result_name = self.getPropertyValue('OutputResultWorkspace')
self._parameter_name = self.getPropertyValue('OutputParameterWorkspace')
self._fit_group_name = self.getPropertyValue('OutputWorkspaceGroup')
def PyExec(self):
from IndirectCommon import (getWSprefix, convertToElasticQ)
setup_prog = Progress(self, start=0.0, end=0.1, nreports=4)
self._fit_type = self._fit_type[:-2]
logger.information('Option: ' + self._fit_type)
logger.information(self._function)
setup_prog.report('Cropping workspace')
tmp_fit_name = "__IqtFit_ws"
crop_alg = self.createChildAlgorithm("CropWorkspace", enableLogging=False)
crop_alg.setProperty("InputWorkspace", self._input_ws)
crop_alg.setProperty("OutputWorkspace", tmp_fit_name)
crop_alg.setProperty("XMin", self._start_x)
crop_alg.setProperty("XMax", self._end_x)
crop_alg.execute()
num_hist = self._input_ws.getNumberHistograms()
if self._spec_max is None:
self._spec_max = num_hist - 1
# Name stem for generated workspace
output_workspace = '%sIqtFit_%s_s%d_to_%d' % (getWSprefix(self._input_ws.name()),
self._fit_type, self._spec_min,
self._spec_max)
setup_prog.report('Converting to Histogram')
convert_to_hist_alg = self.createChildAlgorithm("ConvertToHistogram", enableLogging=False)
convert_to_hist_alg.setProperty("InputWorkspace", crop_alg.getProperty("OutputWorkspace").value)
convert_to_hist_alg.setProperty("OutputWorkspace", tmp_fit_name)
convert_to_hist_alg.execute()
mtd.addOrReplace(tmp_fit_name, convert_to_hist_alg.getProperty("OutputWorkspace").value)
setup_prog.report('Convert to Elastic Q')
convertToElasticQ(tmp_fit_name)
# Build input string for PlotPeakByLogValue
input_str = [tmp_fit_name + ',i%d' % i for i in range(self._spec_min, self._spec_max + 1)]
input_str = ';'.join(input_str)
fit_prog = Progress(self, start=0.1, end=0.8, nreports=2)
fit_prog.report('Fitting...')
ms.PlotPeakByLogValue(Input=input_str,
OutputWorkspace=output_workspace,
Function=self._function,
Minimizer=self._minimizer,
MaxIterations=self._max_iterations,
StartX=self._start_x,
EndX=self._end_x,
FitType='Sequential',
CreateOutput=True,
OutputCompositeMembers=self._do_extract_members)
fit_prog.report('Fitting complete')
conclusion_prog = Progress(self, start=0.8, end=1.0, nreports=5)
# Remove unused workspaces
delete_alg = self.createChildAlgorithm("DeleteWorkspace", enableLogging=False)
delete_alg.setProperty("Workspace", output_workspace + '_NormalisedCovarianceMatrices')
delete_alg.execute()
delete_alg.setProperty("Workspace", output_workspace + '_Parameters')
delete_alg.execute()
delete_alg.setProperty("Workspace", tmp_fit_name)
delete_alg.execute()
conclusion_prog.report('Renaming workspaces')
# rename workspaces to match user input
rename_alg = self.createChildAlgorithm("RenameWorkspace", enableLogging=False)
if output_workspace + "_Workspaces" != self._fit_group_name:
rename_alg.setProperty("InputWorkspace", output_workspace + "_Workspaces")
rename_alg.setProperty("OutputWorkspace", self._fit_group_name)
rename_alg.execute()
if output_workspace != self._parameter_name:
rename_alg.setProperty("InputWorkspace", output_workspace)
rename_alg.setProperty("OutputWorkspace", self._parameter_name)
rename_alg.execute()
# Create *_Result workspace
parameter_names = 'A0,Height,Lifetime,Stretching'
conclusion_prog.report('Processing indirect fit parameters')
pifp_alg = self.createChildAlgorithm("ProcessIndirectFitParameters")
pifp_alg.setProperty("InputWorkspace", self._parameter_name)
pifp_alg.setProperty("ColumnX", "axis-1")
pifp_alg.setProperty("XAxisUnit", "MomentumTransfer")
pifp_alg.setProperty("ParameterNames", parameter_names)
pifp_alg.setProperty("OutputWorkspace", self._result_name)
pifp_alg.execute()
self._result_ws = pifp_alg.getProperty("OutputWorkspace").value
mtd.addOrReplace(self._result_name, self._result_ws)
# Process generated workspaces
wsnames = mtd[self._fit_group_name].getNames()
for i, workspace in enumerate(wsnames):
output_ws = output_workspace + '_Workspace_%d' % i
rename_alg.setProperty("InputWorkspace", workspace)
rename_alg.setProperty("OutputWorkspace", output_ws)
rename_alg.execute()
conclusion_prog.report('Copying and transferring sample logs')
self._transfer_sample_logs()
if self._do_extract_members:
ms.ExtractQENSMembers(InputWorkspace=self._input_ws,
ResultWorkspace=self._fit_group_name,
OutputWorkspace=self._fit_group_name.rsplit('_', 1)[0] + "_Members")
self.setProperty('OutputParameterWorkspace', self._parameter_name)
self.setProperty('OutputWorkspaceGroup', self._fit_group_name)
self.setProperty('OutputResultWorkspace', self._result_ws)
conclusion_prog.report('Algorithm complete')
def _transfer_sample_logs(self):
"""
Copy the sample logs from the input workspace and add them to the output workspaces
"""
sample_logs = {'start_x': self._start_x, 'end_x': self._end_x, 'fit_type': self._fit_type,
'intensities_constrained': self._intensities_constrained, 'beta_constrained': False}
copy_log_alg = self.createChildAlgorithm("CopyLogs", enableLogging=False)
copy_log_alg.setProperty("InputWorkspace", self._input_ws)
copy_log_alg.setProperty("OutputWorkspace", self._fit_group_name)
copy_log_alg.execute()
copy_log_alg.setProperty("InputWorkspace", self._input_ws)
copy_log_alg.setProperty("OutputWorkspace", self._result_ws.name())
copy_log_alg.execute()
log_names = [item for item in sample_logs]
log_values = [sample_logs[item] for item in sample_logs]
add_sample_log_multi = self.createChildAlgorithm("AddSampleLogMultiple", enableLogging=False)
add_sample_log_multi.setProperty("Workspace", self._result_ws.name())
add_sample_log_multi.setProperty("LogNames", log_names)
add_sample_log_multi.setProperty("LogValues", log_values)
add_sample_log_multi.execute()
add_sample_log_multi.setProperty("Workspace", self._fit_group_name)
add_sample_log_multi.setProperty("LogNames", log_names)
add_sample_log_multi.setProperty("LogValues", log_values)
add_sample_log_multi.execute()
def _extract_members(self):
ms.ExtractMembers()
AlgorithmFactory.subscribe(IqtFitSequential)