-
Notifications
You must be signed in to change notification settings - Fork 122
/
PoldiFitPeaks1D2.cpp
432 lines (339 loc) · 14.4 KB
/
PoldiFitPeaks1D2.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
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
// 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/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/TableRow.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
#include "MantidSINQ/PoldiFitPeaks1D2.h"
#include "MantidSINQ/PoldiUtilities/UncertainValue.h"
#include "MantidSINQ/PoldiUtilities/UncertainValueIO.h"
#include "MantidAPI/CompositeFunction.h"
#include <memory>
#include <boost/math/distributions/normal.hpp>
#include <functional>
namespace Mantid {
namespace Poldi {
using namespace Kernel;
using namespace API;
using namespace DataObjects;
using namespace CurveFitting;
RefinedRange::RefinedRange(const PoldiPeak_sptr &peak, double fwhmMultiples)
: m_peaks(1, peak) {
if (!peak) {
throw std::invalid_argument(
"Cannot construct RefinedRange from null-peak.");
}
if (fwhmMultiples < 0) {
throw std::invalid_argument("Cannot construct a RefinedRange of width 0.");
}
double width = peak->fwhm();
double extent = std::max(0.002, width) * fwhmMultiples;
setRangeBorders(peak->q() - extent, peak->q() + extent);
}
RefinedRange::RefinedRange(double xStart, double xEnd,
const std::vector<PoldiPeak_sptr> &peaks)
: m_peaks(peaks) {
setRangeBorders(xStart, xEnd);
}
double RefinedRange::getWidth() const { return m_width; }
bool RefinedRange::operator<(const RefinedRange &other) const {
return m_xStart < other.m_xStart;
}
bool RefinedRange::overlaps(const RefinedRange &other) const {
return overlaps(other, 0.0);
}
bool RefinedRange::overlaps(const RefinedRange &other, double fraction) const {
return getOverlapFraction(other) > fraction;
}
bool RefinedRange::contains(const RefinedRange &other) const {
return (other.m_xStart > m_xStart && other.m_xEnd < m_xEnd);
}
double RefinedRange::getOverlapFraction(const RefinedRange &other) const {
double reference = getWidth();
if (contains(other)) {
return other.getWidth() / reference;
}
if (other.contains(*this)) {
return reference / other.getWidth();
}
if (*this < other) {
return std::max(0.0, m_xEnd - other.m_xStart) / reference;
} else {
return std::max(0.0, other.m_xEnd - m_xStart) / reference;
}
}
void RefinedRange::merge(const RefinedRange &other) {
m_peaks.insert(m_peaks.end(), other.m_peaks.begin(), other.m_peaks.end());
setRangeBorders(std::min(m_xStart, other.m_xStart),
std::max(m_xEnd, other.m_xEnd));
}
void RefinedRange::setRangeBorders(double start, double end) {
if (start >= end) {
throw std::invalid_argument("Range start is larger than range end.");
}
m_xStart = start;
m_xEnd = end;
m_width = end - start;
}
bool operator<(const RefinedRange_sptr &lhs, const RefinedRange_sptr &rhs) {
return (*lhs) < (*rhs);
}
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(PoldiFitPeaks1D2)
PoldiFitPeaks1D2::PoldiFitPeaks1D2()
: m_peaks(), m_profileTemplate(),
m_fitplots(std::make_shared<WorkspaceGroup>()), m_fwhmMultiples(1.0),
m_maxRelativeFwhm(0.02) {}
/// Algorithm's name for identification. @see Algorithm::name
const std::string PoldiFitPeaks1D2::name() const { return "PoldiFitPeaks1D"; }
/// Algorithm's version for identification. @see Algorithm::version
int PoldiFitPeaks1D2::version() const { return 2; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string PoldiFitPeaks1D2::category() const { return "SINQ\\Poldi"; }
void PoldiFitPeaks1D2::init() {
declareProperty(
std::make_unique<WorkspaceProperty<Workspace2D>>("InputWorkspace", "",
Direction::Input),
"An input workspace containing a POLDI auto-correlation spectrum.");
std::shared_ptr<BoundedValidator<double>> minFwhmPerDirection =
std::make_shared<BoundedValidator<double>>();
minFwhmPerDirection->setLower(2.0);
declareProperty(
"FwhmMultiples", 2.0, minFwhmPerDirection,
"Each peak will be fitted using x times FWHM data in each direction.",
Direction::Input);
std::shared_ptr<BoundedValidator<double>> allowedOverlapFraction =
std::make_shared<BoundedValidator<double>>(0.0, 1.0);
declareProperty("AllowedOverlap", 0.25, allowedOverlapFraction,
"If a fraction larger than this value overlaps with the next "
"range, the ranges are merged.");
declareProperty("MaximumRelativeFwhm", 0.02,
"Peaks with a relative FWHM higher"
"than this value will be excluded.",
Direction::Input);
std::vector<std::string> peakFunctions =
FunctionFactory::Instance().getFunctionNames<IPeakFunction>();
std::shared_ptr<ListValidator<std::string>> peakFunctionNames(
new ListValidator<std::string>(peakFunctions));
declareProperty("PeakFunction", "Gaussian", peakFunctionNames,
"Peak function that will be fitted to all peaks.",
Direction::Input);
declareProperty(std::make_unique<WorkspaceProperty<TableWorkspace>>(
"PoldiPeakTable", "", Direction::Input),
"A table workspace containing POLDI peak data.");
declareProperty(std::make_unique<WorkspaceProperty<TableWorkspace>>(
"OutputWorkspace", "RefinedPeakTable", Direction::Output),
"Output workspace with refined peak data.");
declareProperty(std::make_unique<WorkspaceProperty<Workspace>>(
"FitPlotsWorkspace", "FitPlots", Direction::Output),
"Plots of all peak fits.");
}
void PoldiFitPeaks1D2::setPeakFunction(const std::string &peakFunction) {
m_profileTemplate = peakFunction;
}
PoldiPeakCollection_sptr PoldiFitPeaks1D2::getInitializedPeakCollection(
const DataObjects::TableWorkspace_sptr &peakTable) const {
auto peakCollection = std::make_shared<PoldiPeakCollection>(peakTable);
peakCollection->setProfileFunctionName(m_profileTemplate);
return peakCollection;
}
std::vector<RefinedRange_sptr> PoldiFitPeaks1D2::getRefinedRanges(
const PoldiPeakCollection_sptr &peaks) const {
std::vector<RefinedRange_sptr> ranges;
for (size_t i = 0; i < peaks->peakCount(); ++i) {
ranges.emplace_back(
std::make_shared<RefinedRange>(peaks->peak(i), m_fwhmMultiples));
}
return ranges;
}
std::vector<RefinedRange_sptr> PoldiFitPeaks1D2::getReducedRanges(
const std::vector<RefinedRange_sptr> &ranges) const {
std::vector<RefinedRange_sptr> workingRanges(ranges);
std::sort(workingRanges.begin(), workingRanges.end());
std::vector<RefinedRange_sptr> reducedRanges;
reducedRanges.emplace_back(
std::make_shared<RefinedRange>(*(workingRanges.front())));
double allowedOverlap = getProperty("AllowedOverlap");
for (size_t i = 1; i < workingRanges.size(); ++i) {
RefinedRange_sptr lastReduced = reducedRanges.back();
RefinedRange_sptr current = workingRanges[i];
if (!lastReduced->contains(*current) &&
!lastReduced->overlaps(*current, allowedOverlap)) {
reducedRanges.emplace_back(std::make_shared<RefinedRange>(*current));
} else {
lastReduced->merge(*current);
}
}
return reducedRanges;
}
API::IFunction_sptr
PoldiFitPeaks1D2::getRangeProfile(const RefinedRange_sptr &range, int n) const {
auto totalProfile = std::make_shared<CompositeFunction>();
totalProfile->initialize();
std::vector<PoldiPeak_sptr> peaks = range->getPeaks();
for (auto &peak : peaks) {
totalProfile->addFunction(getPeakProfile(peak));
}
totalProfile->addFunction(FunctionFactory::Instance().createInitialized(
"name=Chebyshev,n=" + std::to_string(n) +
",StartX=" + boost::lexical_cast<std::string>(range->getXStart()) +
",EndX=" + boost::lexical_cast<std::string>(range->getXEnd())));
return totalProfile;
}
IFunction_sptr
PoldiFitPeaks1D2::getPeakProfile(const PoldiPeak_sptr &poldiPeak) const {
IPeakFunction_sptr clonedProfile = std::dynamic_pointer_cast<IPeakFunction>(
FunctionFactory::Instance().createFunction(m_profileTemplate));
clonedProfile->setCentre(poldiPeak->q());
clonedProfile->setFwhm(poldiPeak->fwhm(PoldiPeak::AbsoluteQ));
clonedProfile->setHeight(poldiPeak->intensity());
return clonedProfile;
}
void PoldiFitPeaks1D2::setValuesFromProfileFunction(
const PoldiPeak_sptr &poldiPeak,
const IFunction_sptr &fittedFunction) const {
IPeakFunction_sptr peakFunction =
std::dynamic_pointer_cast<IPeakFunction>(fittedFunction);
if (peakFunction) {
poldiPeak->setIntensity(
UncertainValue(peakFunction->height(), peakFunction->getError(0)));
poldiPeak->setQ(
UncertainValue(peakFunction->centre(), peakFunction->getError(1)));
poldiPeak->setFwhm(UncertainValue(peakFunction->fwhm(),
getFwhmWidthRelation(peakFunction) *
peakFunction->getError(2)));
}
}
double PoldiFitPeaks1D2::getFwhmWidthRelation(
const IPeakFunction_sptr &peakFunction) const {
return peakFunction->fwhm() / peakFunction->getParameter(2);
}
PoldiPeakCollection_sptr
PoldiFitPeaks1D2::fitPeaks(const PoldiPeakCollection_sptr &peaks) {
g_log.information() << "Peaks to fit: " << peaks->peakCount() << '\n';
std::vector<RefinedRange_sptr> rawRanges = getRefinedRanges(peaks);
std::vector<RefinedRange_sptr> reducedRanges = getReducedRanges(rawRanges);
g_log.information() << "Ranges used for fitting: " << reducedRanges.size()
<< '\n';
Workspace2D_sptr dataWorkspace = getProperty("InputWorkspace");
m_fitplots->removeAll();
for (const auto ¤tRange : reducedRanges) {
int nMin = getBestChebyshevPolynomialDegree(dataWorkspace, currentRange);
if (nMin > -1) {
IAlgorithm_sptr fit = getFitAlgorithm(dataWorkspace, currentRange, nMin);
fit->execute();
IFunction_sptr fitFunction = fit->getProperty("Function");
CompositeFunction_sptr composite =
std::dynamic_pointer_cast<CompositeFunction>(fitFunction);
if (!composite) {
throw std::runtime_error("Not a composite function!");
}
std::vector<PoldiPeak_sptr> peaks = currentRange->getPeaks();
for (size_t i = 0; i < peaks.size(); ++i) {
setValuesFromProfileFunction(peaks[i], composite->getFunction(i));
MatrixWorkspace_sptr fpg = fit->getProperty("OutputWorkspace");
m_fitplots->addWorkspace(fpg);
}
}
}
return getReducedPeakCollection(peaks);
}
int PoldiFitPeaks1D2::getBestChebyshevPolynomialDegree(
const Workspace2D_sptr &dataWorkspace, const RefinedRange_sptr &range) {
double chiSquareMin = 1e10;
int nMin = -1;
try {
int n = 0;
while ((n < 3)) {
IAlgorithm_sptr fit = getFitAlgorithm(dataWorkspace, range, n);
bool fitSuccess = fit->execute();
if (fitSuccess) {
ITableWorkspace_sptr fitCharacteristics =
fit->getProperty("OutputParameters");
TableRow row =
fitCharacteristics->getRow(fitCharacteristics->rowCount() - 1);
double chiSquare = row.Double(1);
if (fabs(chiSquare - 1) < fabs(chiSquareMin - 1)) {
chiSquareMin = chiSquare;
nMin = n;
}
}
++n;
}
} catch (const std::runtime_error &) {
nMin = -1;
}
if (nMin == -1) {
g_log.information() << "Range [" << range->getXStart() << " - "
<< range->getXEnd() << "] is excluded.";
} else {
g_log.information() << "Chi^2 for range [" << range->getXStart() << " - "
<< range->getXEnd() << "] is minimal at n = " << nMin
<< " with Chi^2 = " << chiSquareMin << '\n';
}
return nMin;
}
PoldiPeakCollection_sptr PoldiFitPeaks1D2::getReducedPeakCollection(
const PoldiPeakCollection_sptr &peaks) const {
PoldiPeakCollection_sptr reducedPeaks =
std::make_shared<PoldiPeakCollection>();
reducedPeaks->setProfileFunctionName(peaks->getProfileFunctionName());
for (size_t i = 0; i < peaks->peakCount(); ++i) {
PoldiPeak_sptr currentPeak = peaks->peak(i);
if (peakIsAcceptable(currentPeak)) {
reducedPeaks->addPeak(currentPeak);
}
}
return reducedPeaks;
}
bool PoldiFitPeaks1D2::peakIsAcceptable(const PoldiPeak_sptr &peak) const {
return peak->intensity() > 0 &&
peak->fwhm(PoldiPeak::Relative) < m_maxRelativeFwhm &&
peak->fwhm(PoldiPeak::Relative) > 0.001;
}
void PoldiFitPeaks1D2::exec() {
setPeakFunction(getProperty("PeakFunction"));
// Number of points around the peak center to use for the fit
m_fwhmMultiples = getProperty("FwhmMultiples");
m_maxRelativeFwhm = getProperty("MaximumRelativeFwhm");
// try to construct PoldiPeakCollection from provided TableWorkspace
TableWorkspace_sptr poldiPeakTable = getProperty("PoldiPeakTable");
m_peaks = getInitializedPeakCollection(poldiPeakTable);
PoldiPeakCollection_sptr fittedPeaksNew = fitPeaks(m_peaks);
PoldiPeakCollection_sptr fittedPeaksOld = m_peaks;
int i = 0;
while (fittedPeaksNew->peakCount() < fittedPeaksOld->peakCount() || i < 1) {
fittedPeaksOld = fittedPeaksNew;
fittedPeaksNew = fitPeaks(fittedPeaksOld);
++i;
}
setProperty("OutputWorkspace", fittedPeaksNew->asTableWorkspace());
setProperty("FitPlotsWorkspace", m_fitplots);
}
IAlgorithm_sptr
PoldiFitPeaks1D2::getFitAlgorithm(const Workspace2D_sptr &dataWorkspace,
const RefinedRange_sptr &range, int n) {
IFunction_sptr rangeProfile = getRangeProfile(range, n);
IAlgorithm_sptr fitAlgorithm = createChildAlgorithm("Fit", -1, -1, false);
fitAlgorithm->setProperty("CreateOutput", true);
fitAlgorithm->setProperty("Output", "FitPeaks1D");
fitAlgorithm->setProperty("CalcErrors", true);
fitAlgorithm->setProperty("OutputCompositeMembers", true);
fitAlgorithm->setProperty("Function", rangeProfile);
fitAlgorithm->setProperty("InputWorkspace", dataWorkspace);
fitAlgorithm->setProperty("WorkspaceIndex", 0);
fitAlgorithm->setProperty("StartX", range->getXStart());
fitAlgorithm->setProperty("EndX", range->getXEnd());
return fitAlgorithm;
}
} // namespace Poldi
} // namespace Mantid