-
Notifications
You must be signed in to change notification settings - Fork 122
/
DoublePulseFit.cpp
346 lines (306 loc) · 16.7 KB
/
DoublePulseFit.cpp
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2020 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 +
#include "MantidCurveFitting/Algorithms/DoublePulseFit.h"
#include "MantidCurveFitting/CostFunctions/CostFuncFitting.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/IFuncMinimizer.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidCurveFitting/Functions/DeltaFunction.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/StartsWithValidator.h"
#include <math.h>
#include <memory>
using namespace Mantid::CurveFitting::Functions;
namespace Mantid {
namespace CurveFitting {
namespace Algorithms {
void setMultiDataProperties(const Mantid::API::IAlgorithm &fittingAlgorithm, Mantid::API::IAlgorithm &fit,
const Mantid::API::MatrixWorkspace_sptr &workspace, const std::string &suffix) {
fit.setProperty("InputWorkspace" + suffix, workspace);
int workspaceIndex = fittingAlgorithm.getProperty("WorkspaceIndex" + suffix);
fit.setProperty("WorkspaceIndex" + suffix, workspaceIndex);
double startX = fittingAlgorithm.getProperty("StartX" + suffix);
double endX = fittingAlgorithm.getProperty("EndX" + suffix);
fit.setProperty("StartX" + suffix, startX);
fit.setProperty("EndX" + suffix, endX);
std::vector<double> exclude = fittingAlgorithm.getProperty("Exclude" + suffix);
fit.setProperty("Exclude" + suffix, exclude);
}
void setMultiDataProperties(const Mantid::API::IAlgorithm &fittingAlgorithm, Mantid::API::IAlgorithm &fit,
const std::vector<Mantid::API::MatrixWorkspace_sptr> &workspaces) {
setMultiDataProperties(fittingAlgorithm, fit, workspaces[0], "");
for (auto i = 1u; i < workspaces.size(); ++i)
setMultiDataProperties(fittingAlgorithm, fit, workspaces[i], "_" + std::to_string(i));
}
Mantid::API::IFunction_sptr getDoublePulseFunction(std::shared_ptr<const API::IFunction> const &function, double offset,
double firstPulseWeight, double secondPulseWeight) {
auto clonedFunction = function->clone();
auto convolution = std::make_shared<Mantid::CurveFitting::Functions::Convolution>();
auto delta1 = std::make_shared<Mantid::CurveFitting::Functions::DeltaFunction>();
auto delta2 = std::make_shared<Mantid::CurveFitting::Functions::DeltaFunction>();
auto deltaComposite = std::make_shared<Mantid::API::CompositeFunction>();
convolution->setAttributeValue("FixResolution", false);
delta1->setParameter("Centre", -0.5 * offset);
delta1->setParameter("Height", firstPulseWeight);
delta1->fixAll();
delta2->setParameter("Centre", 0.5 * offset);
delta2->setParameter("Height", secondPulseWeight);
delta2->fixAll();
deltaComposite->addFunction(delta1);
deltaComposite->addFunction(delta2);
convolution->addFunction(clonedFunction);
convolution->addFunction(deltaComposite);
return convolution;
}
Mantid::API::IFunction_sptr
getDoublePulseMultiDomainFunction(std::shared_ptr<const API::MultiDomainFunction> const &initialFunction, double offset,
double firstPulseWeight, double secondPulseWeight) {
auto doublePulseFunc = std::make_shared<API::MultiDomainFunction>();
for (size_t domain = 0; domain < initialFunction->getNumberDomains(); domain++) {
auto twoPulseInnerFunction =
getDoublePulseFunction(initialFunction->getFunction(domain), offset, firstPulseWeight, secondPulseWeight);
doublePulseFunc->addFunction(twoPulseInnerFunction);
doublePulseFunc->setDomainIndex(domain, domain);
}
return doublePulseFunc;
}
Mantid::API::IFunction_sptr
extractInnerFunction(std::shared_ptr<const Mantid::CurveFitting::Functions::Convolution> const &convolutionFunction) {
return convolutionFunction->getFunction(0);
}
Mantid::API::IFunction_sptr
extractInnerFunction(std::shared_ptr<const API::MultiDomainFunction> const &doublePulseFunc) {
auto extractedFunction = std::make_shared<API::MultiDomainFunction>();
for (size_t domain = 0; domain < doublePulseFunc->getNumberDomains(); domain++) {
auto convFunction = std::dynamic_pointer_cast<Convolution>(doublePulseFunc->getFunction(domain));
if (convFunction) {
extractedFunction->addFunction(extractInnerFunction(convFunction));
extractedFunction->setDomainIndex(domain, domain);
} else {
throw std::runtime_error("Cannot extract from non convolution function. DoublePulseFit.cpp");
}
}
return extractedFunction;
}
// Register the class into the algorithm factory
DECLARE_ALGORITHM(DoublePulseFit)
/// Default constructor
DoublePulseFit::DoublePulseFit() : IFittingAlgorithm() {}
/** Initialisation method
*/
void DoublePulseFit::initConcrete() {
declareProperty("Ties", "", Kernel::Direction::Input);
getPointerToProperty("Ties")->setDocumentation("Math expressions defining ties between parameters of "
"the fitting function.");
declareProperty("Constraints", "", Kernel::Direction::Input);
getPointerToProperty("Constraints")->setDocumentation("List of constraints");
auto mustBePositive = std::make_shared<Kernel::BoundedValidator<int>>();
mustBePositive->setLower(0);
declareProperty("MaxIterations", 500, mustBePositive->clone(),
"Stop after this number of iterations if a good fit is not found");
declareProperty("OutputStatus", "", Kernel::Direction::Output);
getPointerToProperty("OutputStatus")->setDocumentation("Whether the fit was successful");
declareProperty("OutputChi2overDoF", 0.0, "Returns the goodness of the fit", Kernel::Direction::Output);
std::vector<std::string> minimizerOptions = API::FuncMinimizerFactory::Instance().getKeys();
Kernel::IValidator_sptr minimizerValidator = std::make_shared<Kernel::StartsWithValidator>(minimizerOptions);
declareProperty("Minimizer", "Levenberg-Marquardt", minimizerValidator, "Minimizer to use for fitting.");
std::vector<std::string> costFuncOptions = API::CostFunctionFactory::Instance().getKeys();
// select only CostFuncFitting variety
for (auto &costFuncOption : costFuncOptions) {
auto costFunc = std::dynamic_pointer_cast<CostFunctions::CostFuncFitting>(
API::CostFunctionFactory::Instance().create(costFuncOption));
if (!costFunc) {
costFuncOption = "";
}
}
Kernel::IValidator_sptr costFuncValidator = std::make_shared<Kernel::ListValidator<std::string>>(costFuncOptions);
declareProperty("CostFunction", "Least squares", costFuncValidator,
"The cost function to be used for the fit, default is Least squares", Kernel::Direction::InOut);
declareProperty("CreateOutput", false,
"Set to true to create output workspaces with the results of the fit"
"(default is false).");
declareProperty("Output", "",
"A base name for the output workspaces (if not "
"given default names will be created). The "
"default is to use the name of the original data workspace as prefix "
"followed by suffixes _Workspace, _Parameters, etc.");
declareProperty("CalcErrors", false,
"Set to true to calcuate errors when output isn't created "
"(default is false).");
declareProperty("OutputCompositeMembers", false,
"If true and CreateOutput is true then the value of each "
"member of a Composite Function is also output.");
declareProperty(std::make_unique<Kernel::PropertyWithValue<bool>>("ConvolveMembers", false),
"If true and OutputCompositeMembers is true members of any "
"Convolution are output convolved\n"
"with corresponding resolution");
declareProperty("OutputParametersOnly", false,
"Set to true to output only the parameters and not "
"workspace(s) with the calculated values\n"
"(default is false, ignored if CreateOutput is false and "
"Output is an empty string).");
declareProperty("PulseOffset", 0.0, "The time offset between the two pulses");
declareProperty("FirstPulseWeight", 0.5, "Weighting of first pulse.");
declareProperty("SecondPulseWeight", 0.5, "Weighting of second pulse.");
}
void DoublePulseFit::execConcrete() {
setOutputProperties();
declareAdditionalProperties();
Mantid::API::Algorithm_sptr fitAlg = createChildAlgorithm("Fit");
Mantid::API::IFunction_sptr function = getProperty("Function");
Mantid::API::IFunction_sptr doublePulseFunction;
auto pulseOffset = getProperty("PulseOffset");
auto firstPulseWeight = getProperty("FirstPulseWeight");
auto secondPulseWeight = getProperty("SecondPulseWeight");
if (auto multiDomainFunction = std::dynamic_pointer_cast<Mantid::API::MultiDomainFunction>(function)) {
doublePulseFunction =
getDoublePulseMultiDomainFunction(multiDomainFunction, pulseOffset, firstPulseWeight, secondPulseWeight);
} else {
doublePulseFunction = getDoublePulseFunction(function, pulseOffset, firstPulseWeight, secondPulseWeight);
}
auto newFunc = Mantid::API::FunctionFactory::Instance().createInitialized(doublePulseFunction->asString());
auto newFunc1 = Mantid::API::FunctionFactory::Instance().createInitialized(
"composite=Convolution,FixResolution=false,NumDeriv=true;name=GausOsc,A="
"0.2,Sigma=0.2,Frequency=1,Phi=0;(name=DeltaFunction,Height=1,Centre=-0."
"33,ties=(Height=1,Centre=-0.33);name=DeltaFunction,Height=1,Centre=0,"
"ties=(Height=1,Centre=0))");
runFitAlgorith(fitAlg, doublePulseFunction, getProperty("MaxIterations"));
createOutput(fitAlg, doublePulseFunction);
}
/**
* This function declares the additional output properties which are not
* present in init. It is done this way to match the behaviour of Fit which
* declares many of its output properties internally setting the names of
* output workspaces based upon the input property Output.
*/
void DoublePulseFit::declareAdditionalProperties() {
std::string baseName = getProperty("Output");
if (baseName.empty()) {
API::Workspace_const_sptr ws = getProperty("InputWorkspace");
baseName = ws->getName();
}
Mantid::API::IFunction_sptr function = getProperty("Function");
if (m_makeOutput) {
declareProperty(std::make_unique<API::WorkspaceProperty<API::ITableWorkspace>>("OutputNormalisedCovarianceMatrix",
"", Kernel::Direction::Output),
"The name of the TableWorkspace in which to store the final "
"covariance matrix");
setPropertyValue("OutputNormalisedCovarianceMatrix", baseName + "_NormalisedCovarianceMatrix");
declareProperty(std::make_unique<API::WorkspaceProperty<API::ITableWorkspace>>("OutputParameters", "",
Kernel::Direction::Output),
"The name of the TableWorkspace in which to store the "
"final fit parameters");
setPropertyValue("OutputParameters", baseName + "_Parameters");
if (m_outputFitData) {
if (m_multiDomain) {
declareProperty(std::make_unique<API::WorkspaceProperty<Mantid::API::WorkspaceGroup>>(
"OutputWorkspace", "", Kernel::Direction::Output),
"Name of the output Workspace holding resulting fitted "
"spectrum");
setPropertyValue("OutputWorkspace", baseName + "_Workspace");
} else {
declareProperty(std::make_unique<API::WorkspaceProperty<Mantid::API::MatrixWorkspace>>(
"OutputWorkspace", "", Kernel::Direction::Output),
"Name of the output Workspace holding resulting simulated "
"spectrum");
setPropertyValue("OutputWorkspace", baseName + "_Workspace");
}
}
}
}
std::vector<Mantid::API::MatrixWorkspace_sptr> DoublePulseFit::getWorkspaces() const {
std::vector<Mantid::API::MatrixWorkspace_sptr> workspaces;
workspaces.reserve(m_workspacePropertyNames.size());
for (const auto &propertyName : m_workspacePropertyNames) {
Mantid::API::Workspace_sptr workspace = getProperty(propertyName);
workspaces.emplace_back(std::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(workspace));
}
return workspaces;
}
void DoublePulseFit::setOutputProperties() {
std::string baseName = getProperty("Output");
bool makeOutput = getProperty("CreateOutput");
m_makeOutput = makeOutput || !baseName.empty();
m_outputFitData = !getProperty("OutputParametersOnly");
Mantid::API::IFunction_sptr function = getProperty("Function");
m_multiDomain = function->getNumberDomains() > 1;
}
void DoublePulseFit::runFitAlgorith(Mantid::API::IAlgorithm_sptr fitAlg, Mantid::API::IFunction_sptr function,
int maxIterations) {
const bool convolveMembers = getProperty("ConvolveMembers");
const bool ignoreInvalidData = getProperty("IgnoreInvalidData");
const bool calcErrors = getProperty("CalcErrors");
const bool createOutput = getProperty("CreateOutput");
const bool outputCompositeMembers = getProperty("OutputCompositeMembers");
const bool outputParametersOnly = getProperty("OutputParametersOnly");
fitAlg->setLogging(true);
fitAlg->setProperty("Function", function);
setMultiDataProperties(*this, *fitAlg, getWorkspaces());
fitAlg->setProperty("IgnoreInvalidData", ignoreInvalidData);
fitAlg->setProperty("DomainType", getPropertyValue("DomainType"));
fitAlg->setProperty("EvaluationType", getPropertyValue("EvaluationType"));
fitAlg->setPropertyValue("PeakRadius", getPropertyValue("PeakRadius"));
fitAlg->setProperty("Ties", getPropertyValue("Ties"));
fitAlg->setProperty("Constraints", getPropertyValue("Constraints"));
fitAlg->setProperty("MaxIterations", maxIterations);
fitAlg->setProperty("Minimizer", getPropertyValue("Minimizer"));
fitAlg->setProperty("CostFunction", getPropertyValue("CostFunction"));
fitAlg->setProperty("CalcErrors", calcErrors);
fitAlg->setProperty("OutputCompositeMembers", outputCompositeMembers);
fitAlg->setProperty("ConvolveMembers", convolveMembers);
fitAlg->setProperty("CreateOutput", createOutput);
fitAlg->setProperty("OutputParametersOnly", outputParametersOnly);
fitAlg->setProperty("Output", getPropertyValue("Output"));
fitAlg->executeAsChildAlg();
}
void DoublePulseFit::createOutput(Mantid::API::IAlgorithm_sptr fitAlg, Mantid::API::IFunction_sptr function) {
Mantid::API::IFunction_sptr extractedFunction;
if (auto convFunction = std::dynamic_pointer_cast<Mantid::CurveFitting::Functions::Convolution>(function)) {
extractedFunction = extractInnerFunction(convFunction);
} else if (auto multiDomainFunction = std::dynamic_pointer_cast<Mantid::API::MultiDomainFunction>(function)) {
extractedFunction = extractInnerFunction(multiDomainFunction);
} else {
throw std::runtime_error("Incompatible function form. DoublePulseFit.cpp");
}
double outputChi2overDoF = fitAlg->getProperty("OutputChi2overDoF");
std::string outputStatus = fitAlg->getProperty("OutputStatus");
setProperty("OutputStatus", outputStatus);
setProperty("OutputChi2overDoF", outputChi2overDoF);
if (m_makeOutput) {
Mantid::API::ITableWorkspace_sptr covarianceMatrix = fitAlg->getProperty("OutputNormalisedCovarianceMatrix");
setProperty("OutputNormalisedCovarianceMatrix", covarianceMatrix);
Mantid::API::ITableWorkspace_sptr parametersTable = fitAlg->getProperty("OutputParameters");
setProperty("OutputParameters", parametersTable);
if (m_outputFitData) {
if (m_multiDomain) {
Mantid::API::WorkspaceGroup_sptr outputWorkspace = fitAlg->getProperty("OutputWorkspace");
setProperty("OutputWorkspace", outputWorkspace);
} else {
Mantid::API::MatrixWorkspace_sptr outputWorkspace = fitAlg->getProperty("OutputWorkspace");
setProperty("OutputWorkspace", outputWorkspace);
}
}
}
// // Fit has the function property as an InOut object and modifies it in
// place.
// // To replicate this behaviour we reset the relevant pointer to the new
// // function.
// Mantid::API::IFunction_sptr inputFunction = getProperty("Function");
// inputFunction.reset(extractedFunction.get());
// extractedFunction.reset();
setProperty("Function", extractedFunction);
}
} // namespace Algorithms
} // namespace CurveFitting
} // namespace Mantid