-
Notifications
You must be signed in to change notification settings - Fork 121
/
ConvolutionFit.cpp
333 lines (267 loc) · 12.9 KB
/
ConvolutionFit.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
// 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 +
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/FunctionDomain1D.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/Progress.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/TextAxis.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidCurveFitting/Algorithms/ConvolutionFit.h"
#include "MantidCurveFitting/Algorithms/QENSFitSequential.h"
#include "MantidCurveFitting/Algorithms/QENSFitSimultaneous.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/StringContainsValidator.h"
#include "MantidKernel/VectorHelper.h"
#include <algorithm>
#include <cmath>
#include <utility>
namespace {
using namespace Mantid::API;
using namespace Mantid::Kernel;
using Mantid::MantidVec;
std::size_t numberOfFunctions(const IFunction_sptr &function, const std::string &functionName);
std::size_t numberOfFunctions(const CompositeFunction_sptr &composite, const std::string &functionName) {
std::size_t count = 0;
for (auto i = 0u; i < composite->nFunctions(); ++i)
count += numberOfFunctions(composite->getFunction(i), functionName);
return count;
}
std::size_t numberOfFunctions(const IFunction_sptr &function, const std::string &functionName) {
const auto composite = std::dynamic_pointer_cast<CompositeFunction>(function);
if (composite)
return numberOfFunctions(composite, functionName);
return function->name() == functionName ? 1 : 0;
}
bool containsFunction(const IFunction_sptr &function, const std::string &functionName);
bool containsFunction(const CompositeFunction_sptr &composite, const std::string &functionName) {
for (auto i = 0u; i < composite->nFunctions(); ++i) {
if (containsFunction(composite->getFunction(i), functionName))
return true;
}
return false;
}
bool containsFunction(const IFunction_sptr &function, const std::string &functionName) {
const auto composite = std::dynamic_pointer_cast<CompositeFunction>(function);
if (function->name() == functionName)
return true;
else if (composite)
return containsFunction(composite, functionName);
return false;
}
template <typename T, typename F, typename... Ts>
std::vector<T, Ts...> transformVector(const std::vector<T, Ts...> &vec, F const &functor) {
auto target = std::vector<T, Ts...>();
target.reserve(vec.size());
std::transform(vec.begin(), vec.end(), std::back_inserter(target), functor);
return target;
}
template <typename T, typename F, typename... Ts>
std::vector<T, Ts...> combineVectors(const std::vector<T, Ts...> &vec, const std::vector<T, Ts...> &vec2,
F const &combinator) {
auto combined = std::vector<T, Ts...>();
combined.reserve(vec.size());
std::transform(vec.begin(), vec.end(), vec2.begin(), std::back_inserter(combined), combinator);
return combined;
}
template <typename T, typename... Ts>
std::vector<T, Ts...> divideVectors(const std::vector<T, Ts...> ÷nd, const std::vector<T, Ts...> &divisor) {
return combineVectors(dividend, divisor, std::divides<T>());
}
template <typename T, typename... Ts>
std::vector<T, Ts...> addVectors(const std::vector<T, Ts...> &vec, const std::vector<T, Ts...> &vec2) {
return combineVectors(vec, vec2, std::plus<T>());
}
template <typename T, typename... Ts>
std::vector<T, Ts...> multiplyVectors(const std::vector<T, Ts...> &vec, const std::vector<T, Ts...> &vec2) {
return combineVectors(vec, vec2, std::multiplies<T>());
}
template <typename T, typename... Ts> std::vector<T, Ts...> squareVector(const std::vector<T, Ts...> &vec) {
return transformVector(vec, VectorHelper::Squares<T>());
}
template <typename T, typename... Ts> std::vector<T, Ts...> squareRootVector(const std::vector<T, Ts...> &vec) {
return transformVector(vec, static_cast<T (*)(T)>(sqrt));
}
IFunction_sptr extractFirstBackground(IFunction_sptr function);
IFunction_sptr extractFirstBackground(const CompositeFunction_sptr &composite) {
for (auto i = 0u; i < composite->nFunctions(); ++i) {
auto background = extractFirstBackground(composite->getFunction(i));
if (background)
return background;
}
return nullptr;
}
IFunction_sptr extractFirstBackground(IFunction_sptr function) {
auto composite = std::dynamic_pointer_cast<CompositeFunction>(function);
if (composite)
return extractFirstBackground(composite);
else if (function->category() == "Background")
return function;
return nullptr;
}
std::string extractBackgroundType(IFunction_sptr function) {
auto background = extractFirstBackground(std::move(function));
if (!background)
return "None";
auto backgroundType = background->name();
auto position = backgroundType.rfind("Background");
if (position != std::string::npos)
backgroundType = backgroundType.substr(0, position);
if (background->isFixed(0))
backgroundType = "Fixed " + backgroundType;
else
backgroundType = "Fit " + backgroundType;
return backgroundType;
}
std::vector<std::size_t> searchForFitParameters(const std::string &suffix, const ITableWorkspace_sptr &tableWorkspace) {
auto indices = std::vector<std::size_t>();
for (auto i = 0u; i < tableWorkspace->columnCount(); ++i) {
auto name = tableWorkspace->getColumn(i)->name();
auto position = name.rfind(suffix);
if (position != std::string::npos && position + suffix.size() == name.size())
indices.emplace_back(i);
}
return indices;
}
std::pair<MantidVec, MantidVec> calculateEISFAndError(const MantidVec &height, const MantidVec &heightError,
const MantidVec &litude, const MantidVec &litudeError) {
auto total = addVectors(height, amplitude);
auto eisfY = divideVectors(height, total);
auto heightESq = squareVector(heightError);
auto ampErrSq = squareVector(amplitudeError);
auto totalErr = addVectors(heightESq, ampErrSq);
auto heightYSq = squareVector(height);
auto totalSq = squareVector(total);
auto errOverTotalSq = divideVectors(totalErr, totalSq);
auto heightESqOverYSq = divideVectors(heightESq, heightYSq);
auto sqrtESqOverYSq = squareRootVector(heightESqOverYSq);
auto eisfYSumRoot = multiplyVectors(eisfY, sqrtESqOverYSq);
return {eisfY, addVectors(eisfYSumRoot, errOverTotalSq)};
}
void addEISFToTable(ITableWorkspace_sptr &tableWs) {
// Get height data from parameter table
const auto height = searchForFitParameters("Height", tableWs).at(0);
const auto heightErr = searchForFitParameters("Height_Err", tableWs).at(0);
const auto heightY = tableWs->getColumn(height)->numeric_fill();
const auto heightE = tableWs->getColumn(heightErr)->numeric_fill();
// Get amplitude column names
const auto ampIndices = searchForFitParameters("Amplitude", tableWs);
const auto ampErrorIndices = searchForFitParameters("Amplitude_Err", tableWs);
// For each lorentzian, calculate EISF
auto maxSize = ampIndices.size();
if (ampErrorIndices.size() > maxSize)
maxSize = ampErrorIndices.size();
for (auto i = 0u; i < maxSize; ++i) {
// Get amplitude from column in table workspace
auto ampY = tableWs->getColumn(ampIndices[i])->numeric_fill();
auto ampErr = tableWs->getColumn(ampErrorIndices[i])->numeric_fill();
auto eisfAndError = calculateEISFAndError(heightY, heightE, ampY, ampErr);
// Append the calculated values to the table workspace
auto ampName = tableWs->getColumn(ampIndices[i])->name();
auto ampErrorName = tableWs->getColumn(ampErrorIndices[i])->name();
auto columnName = ampName.substr(0, (ampName.size() - std::string("Amplitude").size()));
columnName += "EISF";
auto errorColumnName = ampErrorName.substr(0, (ampErrorName.size() - std::string("Amplitude_Err").size()));
errorColumnName += "EISF_Err";
tableWs->addColumn("double", columnName);
tableWs->addColumn("double", errorColumnName);
auto maxEisf = eisfAndError.first.size();
if (eisfAndError.second.size() > maxEisf) {
maxEisf = eisfAndError.second.size();
}
auto col = tableWs->getColumn(columnName);
auto errCol = tableWs->getColumn(errorColumnName);
for (auto j = 0u; j < maxEisf; j++) {
col->cell<double>(j) = eisfAndError.first.at(j);
errCol->cell<double>(j) = eisfAndError.second.at(j);
}
}
}
} // namespace
namespace Mantid::CurveFitting::Algorithms {
using namespace API;
using namespace Kernel;
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
template <> const std::string ConvolutionFit<QENSFitSequential>::name() const { return "ConvolutionFitSequential"; }
template <> const std::string ConvolutionFit<QENSFitSimultaneous>::name() const { return "ConvolutionFitSimultaneous"; }
template <typename Base> const std::string ConvolutionFit<Base>::name() const { return "ConvolutionFit"; }
/// Algorithm's version for identification. @see Algorithm::version
template <typename Base> int ConvolutionFit<Base>::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
template <typename Base> const std::string ConvolutionFit<Base>::category() const { return "Workflow\\MIDAS"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
template <> const std::string ConvolutionFit<QENSFitSequential>::summary() const {
return "Performs a sequential fit for a convolution workspace";
}
template <> const std::string ConvolutionFit<QENSFitSimultaneous>::summary() const {
return "Performs a simultaneous fit across convolution workspaces";
}
template <typename Base> const std::string ConvolutionFit<Base>::summary() const {
return "Performs a convolution fit";
}
/// Algorithm's see also for related algorithms. @see Algorithm::seeAlso
template <> const std::vector<std::string> ConvolutionFit<QENSFitSequential>::seeAlso() const {
return {"QENSFitSequential"};
}
template <> const std::vector<std::string> ConvolutionFit<QENSFitSimultaneous>::seeAlso() const {
return {"QENSFitSimultaneous"};
}
template <typename Base> const std::vector<std::string> ConvolutionFit<Base>::seeAlso() const { return {}; }
template <typename Base> std::map<std::string, std::string> ConvolutionFit<Base>::validateInputs() {
auto errors = Base::validateInputs();
IFunction_sptr function = Base::getProperty("Function");
if (!containsFunction(function, "Convolution") || !containsFunction(function, "Resolution"))
errors["Function"] = "Function provided does not contain convolution with "
"a resolution function.";
return errors;
}
template <typename Base> bool ConvolutionFit<Base>::isFitParameter(const std::string &name) const {
bool isBackgroundParameter = name.rfind("A0") != std::string::npos || name.rfind("A1") != std::string::npos;
return name.rfind("Centre") == std::string::npos && !isBackgroundParameter;
}
template <typename Base> bool ConvolutionFit<Base>::throwIfElasticQConversionFails() const { return true; }
template <typename Base>
ITableWorkspace_sptr ConvolutionFit<Base>::processParameterTable(ITableWorkspace_sptr parameterTable) {
IFunction_sptr function = Base::getProperty("Function");
m_deltaUsed = containsFunction(function, "DeltaFunction");
if (m_deltaUsed)
addEISFToTable(parameterTable);
return parameterTable;
}
template <typename Base> std::map<std::string, std::string> ConvolutionFit<Base>::getAdditionalLogStrings() const {
IFunction_sptr function = Base::getProperty("Function");
auto logs = Base::getAdditionalLogStrings();
logs["delta_function"] = m_deltaUsed ? "true" : "false";
logs["background"] = extractBackgroundType(function);
return logs;
}
template <typename Base> std::map<std::string, std::string> ConvolutionFit<Base>::getAdditionalLogNumbers() const {
auto logs = Base::getAdditionalLogNumbers();
IFunction_sptr function = Base::getProperty("Function");
logs["lorentzians"] = boost::lexical_cast<std::string>(numberOfFunctions(function, "Lorentzian"));
return logs;
}
template <typename Base> std::vector<std::string> ConvolutionFit<Base>::getFitParameterNames() const {
auto names = Base::getFitParameterNames();
if (m_deltaUsed)
names.emplace_back("EISF");
return names;
}
// Register the algorithms into the AlgorithmFactory
template class ConvolutionFit<QENSFitSequential>;
template class ConvolutionFit<QENSFitSimultaneous>;
using ConvolutionFitSequential = ConvolutionFit<QENSFitSequential>;
using ConvolutionFitSimultaneous = ConvolutionFit<QENSFitSimultaneous>;
DECLARE_ALGORITHM(ConvolutionFitSequential)
DECLARE_ALGORITHM(ConvolutionFitSimultaneous)
} // namespace Mantid::CurveFitting::Algorithms