/
FindPeakBackground.cpp
360 lines (314 loc) · 13.1 KB
/
FindPeakBackground.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
// 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 "MantidAlgorithms/FindPeakBackground.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidAlgorithms/FindPeaks.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidHistogramData/EstimatePolynomial.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/Statistics.h"
#include <sstream>
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::DataObjects;
using namespace std;
namespace Mantid::Algorithms {
DECLARE_ALGORITHM(FindPeakBackground)
//----------------------------------------------------------------------------------------------
/** Define properties
*/
void FindPeakBackground::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "Anonymous", Direction::Input),
"Name of input MatrixWorkspace that contains peaks.");
declareProperty("WorkspaceIndex", EMPTY_INT(),
"workspace indices to have peak and background separated. "
"No default is taken. ");
declareProperty("SigmaConstant", 1.0,
"Multiplier of standard deviations of the variance for convergence of "
"peak elimination. Default is 1.0. ");
declareProperty(std::make_unique<ArrayProperty<double>>("FitWindow"),
"Optional: enter a comma-separated list of the minimum and "
"maximum X-positions of window to fit. "
"The window is the same for all indices in workspace. The "
"length must be exactly two.");
std::vector<std::string> bkgdtypes{"Flat", "Linear", "Quadratic"};
declareProperty("BackgroundType", "Linear", std::make_shared<StringListValidator>(bkgdtypes), "Type of Background.");
// The found peak in a table
declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>("OutputWorkspace", "", Direction::Output),
"The name of the TableWorkspace in which to store the background found "
"for each index. "
"Table contains the indices of the beginning and ending of peak "
"and the estimated background coefficients for the constant, linear, and "
"quadratic terms.");
}
void FindPeakBackground::findWindowIndex(const HistogramData::Histogram &histogram, size_t &l0, size_t &n) {
const auto &inpX = histogram.x();
const auto &inpY = histogram.y();
const size_t sizey = inpY.size(); // inpWS->y(inpwsindex).size();
// determine the fit window with their index in X (or Y)
n = sizey;
l0 = 0;
if (m_vecFitWindows.size() > 1) {
Mantid::Algorithms::FindPeaks fp;
l0 = fp.getIndex(inpX, m_vecFitWindows[0]);
n = fp.getIndex(inpX, m_vecFitWindows[1]);
if (n < sizey)
n++;
}
}
//----------------------------------------------------------------------------------------------
/** Execute body
*/
void FindPeakBackground::exec() {
// Get input and validate
processInputProperties();
auto histogram = m_inputWS->histogram(m_inputWSIndex);
size_t l0, n;
findWindowIndex(histogram, l0, n);
// m_vecFitWindows won't be used again form this point till end.
// Set up output table workspace
createOutputWorkspaces();
// 3. Get Y values
Progress prog(this, 0.0, 1.0, 1);
std::vector<size_t> peak_min_max_indexes;
std::vector<double> bkgd3;
int goodfit = findBackground(histogram, l0, n, peak_min_max_indexes, bkgd3);
if (goodfit > 0) {
size_t min_peak = peak_min_max_indexes[0];
size_t max_peak = peak_min_max_indexes[1];
double a0 = bkgd3[0];
double a1 = bkgd3[1];
double a2 = bkgd3[2];
API::TableRow t = m_outPeakTableWS->getRow(0);
t << static_cast<int>(m_inputWSIndex) << static_cast<int>(min_peak) << static_cast<int>(max_peak) << a0 << a1 << a2
<< goodfit;
}
prog.report();
// 4. Set the output
setProperty("OutputWorkspace", m_outPeakTableWS);
}
//----------------------------------------------------------------------------------------------
/**
* @brief FindPeakBackground::findBackground
* @param histogram
* @param l0
* @param n
* @param peak_min_max_indexes
* @param bkgd3
* @return
*/
int FindPeakBackground::findBackground(const HistogramData::Histogram &histogram, const size_t &l0, const size_t &n,
std::vector<size_t> &peak_min_max_indexes, std::vector<double> &bkgd3) {
const size_t sizex = histogram.x().size();
const auto &inpY = histogram.y();
const size_t sizey = inpY.size();
int goodfit(0);
// Find background
double Ymean, Yvariance, Ysigma;
MantidVec maskedY;
auto in = std::min_element(inpY.cbegin(), inpY.cend());
double bkg0 = inpY[in - inpY.begin()];
for (size_t l = l0; l < n; ++l) {
maskedY.emplace_back(inpY[l] - bkg0);
}
MantidVec mask(n - l0, 0.0);
auto xn = static_cast<double>(n - l0);
if ((0. == xn) || (0. == xn - 1.0))
throw std::runtime_error("The number of Y values in the input workspace for the "
"workspace index given, minus 'l0' or minus 'l0' minus 1, is 0. This "
"will produce a "
"divide-by-zero");
do {
Statistics stats = getStatistics(maskedY);
Ymean = stats.mean;
Yvariance = stats.standard_deviation * stats.standard_deviation;
Ysigma = std::sqrt((moment4(maskedY, static_cast<size_t>(xn), Ymean) - (xn - 3.0) / (xn - 1.0) * Yvariance) / xn);
MantidVec::const_iterator it = std::max_element(maskedY.begin(), maskedY.end());
const size_t pos = it - maskedY.begin();
maskedY[pos] = 0;
mask[pos] = 1.0;
} while (std::abs(Ymean - Yvariance) > m_sigmaConstant * Ysigma);
if (n - l0 > 5) {
// remove single outliers
if (mask[1] == mask[2] && mask[2] == mask[3])
mask[0] = mask[1];
if (mask[0] == mask[2] && mask[2] == mask[3])
mask[1] = mask[2];
for (size_t l = 2; l < n - l0 - 3; ++l) {
if (mask[l - 1] == mask[l + 1] && (mask[l - 1] == mask[l - 2] || mask[l + 1] == mask[l + 2])) {
mask[l] = mask[l + 1];
}
}
if (mask[n - l0 - 2] == mask[n - l0 - 3] && mask[n - l0 - 3] == mask[n - l0 - 4])
mask[n - l0 - 1] = mask[n - l0 - 2];
if (mask[n - l0 - 1] == mask[n - l0 - 3] && mask[n - l0 - 3] == mask[n - l0 - 4])
mask[n - l0 - 2] = mask[n - l0 - 1];
// mask regions not connected to largest region
// for loop can start > 1 for multiple peaks
vector<cont_peak> peaks;
if (mask[0] == 1) {
peaks.emplace_back();
peaks.back().start = l0;
}
for (size_t l = 1; l < n - l0; ++l) {
if (mask[l] != mask[l - 1] && mask[l] == 1) {
peaks.emplace_back();
peaks.back().start = l + l0;
} else if (!peaks.empty()) {
size_t ipeak = peaks.size() - 1;
if (mask[l] != mask[l - 1] && mask[l] == 0) {
peaks[ipeak].stop = l + l0;
}
if (inpY[l + l0] > peaks[ipeak].maxY)
peaks[ipeak].maxY = inpY[l + l0];
}
}
size_t min_peak, max_peak;
if (!peaks.empty()) {
g_log.debug() << "Peaks' size = " << peaks.size() << " -> esitmate background. \n";
if (peaks.back().stop == 0)
peaks.back().stop = n - 1;
std::sort(peaks.begin(), peaks.end(), by_len());
// save endpoints
min_peak = peaks[0].start;
// extra point for histogram input - TODO change to use Histogram better
max_peak = peaks[0].stop + sizex - sizey;
goodfit = 1;
} else {
// assume the whole thing is background
g_log.debug("Peaks' size = 0 -> whole region assumed background");
min_peak = n;
max_peak = l0;
goodfit = 2;
}
double a0 = 0., a1 = 0., a2 = 0.;
estimateBackground(histogram, l0, n, min_peak, max_peak, (!peaks.empty()), a0, a1, a2);
// Add a new row
peak_min_max_indexes.resize(2);
peak_min_max_indexes[0] = min_peak;
peak_min_max_indexes[1] = max_peak;
bkgd3.resize(3);
bkgd3[0] = a0;
bkgd3[1] = a1;
bkgd3[2] = a2;
}
return goodfit;
}
//----------------------------------------------------------------------------------------------
/** Estimate background
* @param histogram :: data to find peak background in
* @param i_min :: index of minimum in X to estimate background
* @param i_max :: index of maximum in X to estimate background
* @param p_min :: index of peak min in X to estimate background
* @param p_max :: index of peak max in X to estimate background
* @param hasPeak :: ban data in the peak range
* @param out_bg0 :: interception
* @param out_bg1 :: slope
* @param out_bg2 :: a2 = 0
*/
void FindPeakBackground::estimateBackground(const HistogramData::Histogram &histogram, const size_t i_min,
const size_t i_max, const size_t p_min, const size_t p_max,
const bool hasPeak, double &out_bg0, double &out_bg1, double &out_bg2) {
double redux_chisq;
if (hasPeak) {
HistogramData::estimateBackground(m_backgroundOrder, histogram, i_min, i_max, p_min, p_max, out_bg0, out_bg1,
out_bg2, redux_chisq);
} else {
HistogramData::estimatePolynomial(m_backgroundOrder, histogram, i_min, i_max, out_bg0, out_bg1, out_bg2,
redux_chisq);
}
g_log.information() << "Estimated background: A0 = " << out_bg0 << ", A1 = " << out_bg1 << ", A2 = " << out_bg2
<< "\n";
}
//----------------------------------------------------------------------------------------------
/** Calculate 4th moment
* @param X :: vec for X
* @param n :: length of vector
* @param mean :: mean of X
*/
double FindPeakBackground::moment4(MantidVec &X, size_t n, double mean) {
double sum = 0.0;
for (size_t i = 0; i < n; ++i) {
sum += (X[i] - mean) * (X[i] - mean) * (X[i] - mean) * (X[i] - mean);
}
sum /= static_cast<double>(n);
return sum;
}
//----------------------------------------------------------------------------------------------
void FindPeakBackground::processInputProperties() {
// process input workspace and workspace index
m_inputWS = getProperty("InputWorkspace");
int inpwsindex = getProperty("WorkspaceIndex");
if (isEmpty(inpwsindex)) {
// Default
if (m_inputWS->getNumberHistograms() == 1) {
inpwsindex = 0;
} else {
throw runtime_error("WorkspaceIndex must be given. ");
}
} else if (inpwsindex < 0 || inpwsindex >= static_cast<int>(m_inputWS->getNumberHistograms())) {
stringstream errss;
errss << "Input workspace " << m_inputWS->getName() << " has " << m_inputWS->getNumberHistograms()
<< " spectra. Input workspace index " << inpwsindex << " is out of boundary. ";
throw runtime_error(errss.str());
}
m_inputWSIndex = static_cast<size_t>(inpwsindex);
std::vector<double> fitwindow = getProperty("FitWindow");
setFitWindow(fitwindow);
// background
m_backgroundType = getPropertyValue("BackgroundType");
size_t bkgdorder = 0;
if (m_backgroundType == "Linear")
bkgdorder = 1;
else if (m_backgroundType == "Quadratic")
bkgdorder = 2;
setBackgroundOrder(bkgdorder);
// sigma constant
double k = getProperty("SigmaConstant");
setSigma(k);
}
/// set sigma constant
void FindPeakBackground::setSigma(const double &sigma) { m_sigmaConstant = sigma; }
/// set background order
void FindPeakBackground::setBackgroundOrder(size_t order) { m_backgroundOrder = order; }
//----------------------------------------------------------------------------------------------
/** set fit window
* @brief FindPeakBackground::setFitWindow
* @param fitwindow
*/
void FindPeakBackground::setFitWindow(const std::vector<double> &fitwindow) {
// validate input
if ((fitwindow.size() == 2) && fitwindow[0] >= fitwindow[1]) {
throw std::invalid_argument("Fit window has either wrong item number or "
"window value is not in ascending order.");
}
// m_vecFitWindows.resize(2);
// copy the input to class variable
m_vecFitWindows = fitwindow;
}
//----------------------------------------------------------------------------------------------
/**
* @brief FindPeakBackground::createOutputWorkspaces
*/
void FindPeakBackground::createOutputWorkspaces() {
// Set up output table workspace
m_outPeakTableWS = std::make_shared<TableWorkspace>();
m_outPeakTableWS->addColumn("int", "wksp_index");
m_outPeakTableWS->addColumn("int", "peak_min_index");
m_outPeakTableWS->addColumn("int", "peak_max_index");
m_outPeakTableWS->addColumn("double", "bkg0");
m_outPeakTableWS->addColumn("double", "bkg1");
m_outPeakTableWS->addColumn("double", "bkg2");
m_outPeakTableWS->addColumn("int", "GoodFit");
m_outPeakTableWS->appendRow();
}
} // namespace Mantid::Algorithms