Skip to content

Commit 6173d98

Browse files
committed
add signal to sigma post fitting check
1 parent 78d0301 commit 6173d98

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

Framework/Algorithms/inc/MantidAlgorithms/FitPeaks.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ class MANTID_ALGORITHMS_DLL FitPeaks final : public API::Algorithm {
216216
/// calculate peak+background for fitted
217217
void calculateFittedPeaks(const std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> &fit_results);
218218

219+
double calculateSignalToSigmaRatio(const size_t &iws, const std::pair<double, double> &peakWindow,
220+
const API::IPeakFunction_sptr &peakFunction,
221+
const API::IBackgroundFunction_sptr &backgroundFunction);
222+
219223
/// Get the parameter name for peak height (I or height or etc)
220224
std::string getPeakHeightParameterName(const API::IPeakFunction_const_sptr &peak_function);
221225

@@ -329,6 +333,8 @@ class MANTID_ALGORITHMS_DLL FitPeaks final : public API::Algorithm {
329333
double m_minSignalToNoiseRatio;
330334
double m_minPeakTotalCount;
331335

336+
double m_minSignalToSigmaRatio;
337+
332338
/// flag for high background
333339
bool m_highBackground;
334340

Framework/Algorithms/src/FitPeaks.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "MantidKernel/IValidator.h"
3434
#include "MantidKernel/ListValidator.h"
3535
#include "MantidKernel/StartsWithValidator.h"
36+
#include "MantidKernel/VectorHelper.h"
3637

3738
#include "boost/algorithm/string.hpp"
3839
#include "boost/algorithm/string/trim.hpp"
@@ -82,6 +83,7 @@ const std::string OUTPUT_WKSP_PARAM_ERRS("OutputParameterFitErrorsWorkspace");
8283
const std::string RAW_PARAMS("RawPeakParameters");
8384
const std::string PEAK_MIN_SIGNAL_TO_NOISE_RATIO("MinimumSignalToNoiseRatio");
8485
const std::string PEAK_MIN_TOTAL_COUNT("MinimumPeakTotalCount");
86+
const std::string PEAK_MIN_SIGNAL_TO_SIGMA_RATIO("MinimumSignalToSigmaRatio");
8587
} // namespace PropertyNames
8688
} // namespace
8789

@@ -431,6 +433,10 @@ void FitPeaks::init() {
431433
"Used for validating peaks before fitting. If the total peak window Y-value count "
432434
"is under this value, the peak will be excluded from fitting and calibration.");
433435

436+
declareProperty(PropertyNames::PEAK_MIN_SIGNAL_TO_SIGMA_RATIO, 0.,
437+
"Used for validating peaks before fitting. If the signal-to-sigma ratio is under this value, "
438+
"the peak will be excluded from fitting and calibration.");
439+
434440
const std::string addoutgrp("Analysis");
435441
setPropertyGroup(PropertyNames::OUTPUT_WKSP_PARAMS, addoutgrp);
436442
setPropertyGroup(PropertyNames::OUTPUT_WKSP_MODEL, addoutgrp);
@@ -445,7 +451,7 @@ std::map<std::string, std::string> FitPeaks::validateInputs() {
445451
map<std::string, std::string> issues;
446452

447453
// check that min/max spectra indices make sense - only matters if both are specified
448-
if (!(isDefault(PropertyNames::START_WKSP_INDEX) && isDefault(PropertyNames::START_WKSP_INDEX))) {
454+
if (!(isDefault(PropertyNames::START_WKSP_INDEX) && isDefault(PropertyNames::STOP_WKSP_INDEX))) {
449455
const int startIndex = getProperty(PropertyNames::START_WKSP_INDEX);
450456
const int stopIndex = getProperty(PropertyNames::STOP_WKSP_INDEX);
451457
if (startIndex > stopIndex) {
@@ -922,6 +928,11 @@ void FitPeaks::processInputPeakTolerance() {
922928
m_minSignalToNoiseRatio = getProperty(PropertyNames::PEAK_MIN_SIGNAL_TO_NOISE_RATIO);
923929
if (isEmpty(m_minSignalToNoiseRatio) || m_minSignalToNoiseRatio < 0.)
924930
m_minSignalToNoiseRatio = 0.;
931+
932+
// set the signal-to-sigma threshold to zero (default value) if not specified or invalid
933+
m_minSignalToSigmaRatio = getProperty(PropertyNames::PEAK_MIN_SIGNAL_TO_SIGMA_RATIO);
934+
if (isEmpty(m_minSignalToSigmaRatio) || m_minSignalToSigmaRatio < 0.)
935+
m_minSignalToSigmaRatio = 0.;
925936
}
926937

927938
//----------------------------------------------------------------------------------------------
@@ -1281,6 +1292,13 @@ void FitPeaks::fitSpectrumPeaks(size_t wi, const std::vector<double> &expected_p
12811292
bkgdfunction, peak_pre_check_result);
12821293
if (peak_pre_check_result->isIndividualPeakRejected())
12831294
fit_result->setBadRecord(peak_index, -1.);
1295+
1296+
auto signalToSigma = calculateSignalToSigmaRatio(wi, peak_window_i, peakfunction, bkgdfunction);
1297+
if (m_minSignalToSigmaRatio > 0 && signalToSigma < m_minSignalToSigmaRatio) {
1298+
fit_result->setBadRecord(peak_index, -1.);
1299+
cost = DBL_MAX;
1300+
}
1301+
12841302
*pre_check_result += *peak_pre_check_result; // keep track of the rejection count within the spectrum
12851303
}
12861304
pre_check_result->setNumberOfOutOfRangePeaks(number_of_out_of_range_peaks);
@@ -1530,6 +1548,38 @@ void FitPeaks::calculateFittedPeaks(const std::vector<std::shared_ptr<FitPeaksAl
15301548

15311549
return;
15321550
}
1551+
1552+
double FitPeaks::calculateSignalToSigmaRatio(const size_t &iws, const std::pair<double, double> &peakWindow,
1553+
const API::IPeakFunction_sptr &peakFunction,
1554+
const API::IBackgroundFunction_sptr &backgroundFunction) {
1555+
const auto &vecX = m_fittedPeakWS->points(iws);
1556+
auto startX = std::lower_bound(vecX.begin(), vecX.end(), peakWindow.first);
1557+
auto stopX = std::lower_bound(vecX.begin(), vecX.end(), peakWindow.second);
1558+
1559+
FunctionDomain1DVector domain(startX, stopX);
1560+
FunctionValues values(domain);
1561+
CompositeFunction_sptr compFunc = std::make_shared<API::CompositeFunction>();
1562+
1563+
compFunc->addFunction(backgroundFunction);
1564+
compFunc->function(domain, values);
1565+
auto bkgdValues = values.toVector();
1566+
1567+
compFunc->addFunction(peakFunction);
1568+
compFunc->function(domain, values);
1569+
auto fittedValues = values.toVector();
1570+
1571+
const auto &errors = m_fittedPeakWS->readE(iws);
1572+
auto startE = std::lower_bound(errors.begin(), errors.end(), peakWindow.first);
1573+
auto stopE = std::lower_bound(errors.begin(), errors.end(), peakWindow.second);
1574+
std::vector<double> peakErrors(startE, stopE);
1575+
1576+
double fittedSum = std::accumulate(fittedValues.cbegin(), fittedValues.cend(), 0.0);
1577+
double bkgdSum = std::accumulate(bkgdValues.cbegin(), bkgdValues.cend(), 0.0);
1578+
double sigma = sqrt(std::accumulate(peakErrors.cbegin(), peakErrors.cend(), 0.0, VectorHelper::SumSquares<double>()));
1579+
1580+
return (fittedSum - bkgdSum) / ((sigma == 0) ? 1 : sigma);
1581+
}
1582+
15331583
namespace {
15341584
bool estimateBackgroundParameters(const Histogram &histogram, const std::pair<size_t, size_t> &peak_window,
15351585
const API::IBackgroundFunction_sptr &bkgd_function) {

Framework/Algorithms/src/PDCalibration.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ DECLARE_ALGORITHM(PDCalibration)
7373

7474
namespace { // anonymous
7575
const auto isNonZero = [](const double value) { return value != 0.; };
76-
}
76+
} // namespace
7777

7878
/// private inner class
7979
class PDCalibration::FittedPeaks {
@@ -315,6 +315,10 @@ void PDCalibration::init() {
315315
"Used for validating peaks before fitting. If the total peak window Y-value count "
316316
"is under this value, the peak will be excluded from fitting and calibration.");
317317

318+
declareProperty("MinimumSignalToSigmaRatio", 0.,
319+
"Used for validating peaks before fitting. If the signal-to-sigma ratio is under this value, "
320+
"the peak will be excluded from fitting and calibration.");
321+
318322
// make group for Input properties
319323
std::string inputGroup("Input Options");
320324
setPropertyGroup("InputWorkspace", inputGroup);
@@ -336,6 +340,7 @@ void PDCalibration::init() {
336340
setPropertyGroup("MinimumPeakHeight", fitPeaksGroup);
337341
setPropertyGroup("MinimumSignalToNoiseRatio", fitPeaksGroup);
338342
setPropertyGroup("MinimumPeakTotalCount", fitPeaksGroup);
343+
setPropertyGroup("MinimumSignalToSigmaRatio", fitPeaksGroup);
339344
setPropertyGroup("HighBackground", fitPeaksGroup);
340345
setPropertyGroup("MaxChiSq", fitPeaksGroup);
341346
setPropertyGroup("ConstrainPeakPositions", fitPeaksGroup);
@@ -489,6 +494,7 @@ void PDCalibration::exec() {
489494
const double minPeakHeight = getProperty("MinimumPeakHeight");
490495
const double minPeakTotalCount = getProperty("MinimumPeakTotalCount");
491496
const double minSignalToNoiseRatio = getProperty("MinimumSignalToNoiseRatio");
497+
const double minSignalToSigmaRatio = getProperty("MinimumSignalToSigmaRatio");
492498
const double maxChiSquared = getProperty("MaxChiSq");
493499

494500
const std::string calParams = getPropertyValue("CalibrationParameters");
@@ -578,6 +584,7 @@ void PDCalibration::exec() {
578584
algFitPeaks->setProperty("MinimumPeakHeight", minPeakHeight);
579585
algFitPeaks->setProperty("MinimumPeakTotalCount", minPeakTotalCount);
580586
algFitPeaks->setProperty("MinimumSignalToNoiseRatio", minSignalToNoiseRatio);
587+
algFitPeaks->setProperty("MinimumSignalToSigmaRatio", minSignalToSigmaRatio);
581588
// some fitting strategy
582589
algFitPeaks->setProperty("FitFromRight", true);
583590
const bool highBackground = getProperty("HighBackground");

buildconfig/CMake/CppCheck_Suppressions.txt.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ knownConditionTrueFalse:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/FindPeaks.c
256256
unreadVariable:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/InterpolatingRebin.cpp:69
257257
constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/GetEi2.cpp:213
258258
missingOverride:${CMAKE_SOURCE_DIR}/Framework/Algorithms/inc/MantidAlgorithms/GetAllEi.h:32
259-
duplicateExpression:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/FitPeaks.cpp:448
260259
passedByValue:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/GetTimeSeriesLogInformation.cpp:277
261260
passedByValue:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/GetTimeSeriesLogInformation.cpp:501
262261
constVariableReference:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/GetTimeSeriesLogInformation.cpp:248

0 commit comments

Comments
 (0)