27#include "MantidHistogramData/EstimatePolynomial.h"
28#include "MantidHistogramData/Histogram.h"
29#include "MantidHistogramData/HistogramBuilder.h"
30#include "MantidHistogramData/HistogramIterator.h"
39#include "boost/algorithm/string.hpp"
40#include "boost/algorithm/string/trim.hpp"
45using namespace Algorithms::PeakParameterHelper;
51using Mantid::HistogramData::Histogram;
60const std::string START_WKSP_INDEX(
"StartWorkspaceIndex");
61const std::string STOP_WKSP_INDEX(
"StopWorkspaceIndex");
62const std::string PEAK_CENTERS(
"PeakCenters");
63const std::string PEAK_CENTERS_WKSP(
"PeakCentersWorkspace");
64const std::string PEAK_FUNC(
"PeakFunction");
65const std::string BACK_FUNC(
"BackgroundType");
66const std::string FIT_WINDOW_LIST(
"FitWindowBoundaryList");
67const std::string FIT_WINDOW_WKSP(
"FitPeakWindowWorkspace");
68const std::string PEAK_WIDTH_PERCENT(
"PeakWidthPercent");
69const std::string PEAK_PARAM_NAMES(
"PeakParameterNames");
70const std::string PEAK_PARAM_VALUES(
"PeakParameterValues");
71const std::string PEAK_PARAM_TABLE(
"PeakParameterValueTable");
72const std::string FIT_FROM_RIGHT(
"FitFromRight");
73const std::string MINIMIZER(
"Minimizer");
74const std::string COST_FUNC(
"CostFunction");
75const std::string MAX_FIT_ITER(
"MaxFitIterations");
76const std::string BACKGROUND_Z_SCORE(
"FindBackgroundSigma");
77const std::string HIGH_BACKGROUND(
"HighBackground");
78const std::string POSITION_TOL(
"PositionTolerance");
79const std::string PEAK_MIN_HEIGHT(
"MinimumPeakHeight");
80const std::string CONSTRAIN_PEAK_POS(
"ConstrainPeakPositions");
81const std::string OUTPUT_WKSP_MODEL(
"FittedPeaksWorkspace");
82const std::string OUTPUT_WKSP_PARAMS(
"OutputPeakParametersWorkspace");
83const std::string OUTPUT_WKSP_PARAM_ERRS(
"OutputParameterFitErrorsWorkspace");
84const std::string RAW_PARAMS(
"RawPeakParameters");
85const std::string PEAK_MIN_SIGNAL_TO_NOISE_RATIO(
"MinimumSignalToNoiseRatio");
86const std::string PEAK_MIN_TOTAL_COUNT(
"MinimumPeakTotalCount");
87const std::string PEAK_MIN_SIGNAL_TO_SIGMA_RATIO(
"MinimumSignalToSigmaRatio");
91namespace FitPeaksAlgorithm {
97 if (num_peaks == 0 || num_params == 0)
98 throw std::runtime_error(
"No peak or no parameter error.");
102 m_costs.resize(num_peaks, DBL_MAX);
105 for (
size_t ipeak = 0; ipeak < num_peaks; ++ipeak) {
152 throw std::runtime_error(
"Peak index is out of range.");
161 size_t peak_num_params = fit_functions.
peakfunction->nParams();
162 for (
size_t ipar = 0; ipar < peak_num_params; ++ipar) {
167 for (
size_t ipar = 0; ipar < fit_functions.
bkgdfunction->nParams(); ++ipar) {
182 throw std::runtime_error(
"Peak index is out of range");
183 if (peak_position >= 0.)
184 throw std::runtime_error(
"Can only set negative postion for bad record");
232 assert(individual_rejection_count <= 1);
234 return individual_rejection_count == 1;
244 std::ostringstream os;
255 os <<
m_low_snr <<
" peak(s) rejected: low signal-to-noise ratio.\n";
263 : m_fitPeaksFromRight(true), m_fitIterations(50), m_numPeaksToFit(0), m_minPeakHeight(0.),
264 m_minSignalToNoiseRatio(0.), m_minPeakTotalCount(0.), m_peakPosTolCase234(false) {}
271 "Name of the input workspace for peak fitting.");
274 "Name of the output workspace containing peak centers for "
276 "The output workspace is point data."
277 "Each workspace index corresponds to a spectrum. "
278 "Each X value ranges from 0 to N-1, where N is the number of "
280 "Each Y value is the peak position obtained by peak fitting. "
281 "Negative value is used for error signals. "
282 "-1 for data is zero; -2 for maximum value is smaller than "
283 "specified minimum value."
284 "and -3 for non-converged fitting.");
287 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
288 mustBePositive->setLower(0);
289 declareProperty(PropertyNames::START_WKSP_INDEX, 0, mustBePositive,
"Starting workspace index for fit");
291 PropertyNames::STOP_WKSP_INDEX,
EMPTY_INT(),
292 "Last workspace index for fit is the smaller of this value and the workspace index of last spectrum.");
295 "List of peak centers to use as initial guess for fit.");
299 "MatrixWorkspace containing referent peak centers for each spectrum, defined at the same workspace indices.");
301 const std::string peakcentergrp(
"Peak Positions");
306 const std::vector<std::string> peakNames = FunctionFactory::Instance().getFunctionNames<
API::IPeakFunction>();
307 declareProperty(PropertyNames::PEAK_FUNC,
"Gaussian", std::make_shared<StringListValidator>(peakNames),
308 "Use of a BackToBackExponential profile is only reccomended if the "
309 "coeficients to calculate A and B are defined in the instrument "
310 "Parameters.xml file.");
311 const vector<string> bkgdtypes{
"Flat",
"Linear",
"Quadratic"};
312 declareProperty(PropertyNames::BACK_FUNC,
"Linear", std::make_shared<StringListValidator>(bkgdtypes),
313 "Type of Background.");
315 const std::string funcgroup(
"Function Types");
322 "List of boundaries of the peak fitting window corresponding to "
327 "MatrixWorkspace containing peak windows for each peak center in each spectrum, defined at the same "
328 "workspace indices.");
330 auto min = std::make_shared<BoundedValidator<double>>();
334 "The estimated peak width as a "
335 "percentage of the d-spacing "
336 "of the center of the peak. Value must be less than 1.");
338 const std::string fitrangeegrp(
"Peak Range Setup");
345 "List of peak parameters' names");
347 "List of peak parameters' value");
350 "Name of the an optional workspace, whose each column "
351 "corresponds to given peak parameter names, "
352 "and each row corresponds to a subset of spectra.");
354 const std::string startvaluegrp(
"Starting Parameters Setup");
361 "Flag for the order to fit peaks. If true, peaks are fitted "
363 "Otherwise peaks are fitted from leftmost.");
365 const std::vector<std::string> minimizerOptions = API::FuncMinimizerFactory::Instance().getKeys();
368 "Minimizer to use for fitting.");
370 const std::array<string, 2> costFuncOptions = {{
"Least squares",
"Rwp"}};
374 auto min_max_iter = std::make_shared<BoundedValidator<int>>();
375 min_max_iter->setLower(49);
376 declareProperty(PropertyNames::MAX_FIT_ITER, 50, min_max_iter,
"Maximum number of function fitting iterations.");
378 const std::string optimizergrp(
"Optimization Setup");
383 std::ostringstream os;
384 os <<
"Deprecated property. Use " << PropertyNames::PEAK_MIN_SIGNAL_TO_NOISE_RATIO <<
" instead.";
388 "Flag whether the input data has high background compared to peak heights.");
391 "List of tolerance on fitted peak positions against given peak positions."
392 "If there is only one value given, then ");
395 "Used for validating peaks before and after fitting. If a peak's observed/estimated or "
396 "fitted height is under this value, the peak will be marked as error.");
399 "If true peak position will be constrained by estimated positions "
400 "(highest Y value position) and "
401 "the peak width either estimted by observation or calculate.");
406 "Name of the output matrix workspace with fitted peak. "
407 "This output workspace has the same dimension as the input workspace."
408 "The Y values belonged to peaks to fit are replaced by fitted value. "
409 "Values of estimated background are used if peak fails to be fit.");
413 "Name of table workspace containing all fitted peak parameters.");
419 "Name of workspace containing all fitted peak parameters' fitting error."
420 "It must be used along with FittedPeaksWorkspace and RawPeakParameters "
424 "false generates table with effective centre/width/height "
425 "parameters. true generates a table with peak function "
429 PropertyNames::PEAK_MIN_SIGNAL_TO_NOISE_RATIO, 0.,
430 "Used for validating peaks before fitting. If the signal-to-noise ratio is under this value, "
431 "the peak will be marked as error. This does not apply to peaks for which the noise cannot be estimated.");
434 "Used for validating peaks before fitting. If the total peak window Y-value count "
435 "is under this value, the peak will be excluded from fitting and calibration.");
438 "Used for validating peaks after fitting. If the signal-to-sigma ratio is under this value, "
439 "the peak will be excluded from fitting and calibration.");
441 const std::string addoutgrp(
"Analysis");
452 map<std::string, std::string> issues;
455 if (!(
isDefault(PropertyNames::START_WKSP_INDEX) &&
isDefault(PropertyNames::STOP_WKSP_INDEX))) {
456 const int startIndex =
getProperty(PropertyNames::START_WKSP_INDEX);
457 const int stopIndex =
getProperty(PropertyNames::STOP_WKSP_INDEX);
458 if (startIndex > stopIndex) {
459 const std::string msg =
460 PropertyNames::START_WKSP_INDEX +
" must be less than or equal to " + PropertyNames::STOP_WKSP_INDEX;
461 issues[PropertyNames::START_WKSP_INDEX] = msg;
462 issues[PropertyNames::STOP_WKSP_INDEX] = msg;
467 bool haveCommonPeakParameters(
false);
468 std::vector<string> suppliedParameterNames =
getProperty(PropertyNames::PEAK_PARAM_NAMES);
469 std::vector<double> peakParamValues =
getProperty(PropertyNames::PEAK_PARAM_VALUES);
470 if ((!suppliedParameterNames.empty()) || (!peakParamValues.empty())) {
471 haveCommonPeakParameters =
true;
472 if (suppliedParameterNames.size() != peakParamValues.size()) {
473 issues[PropertyNames::PEAK_PARAM_NAMES] =
"must have same number of values as PeakParameterValues";
474 issues[PropertyNames::PEAK_PARAM_VALUES] =
"must have same number of values as PeakParameterNames";
479 std::string partablename =
getPropertyValue(PropertyNames::PEAK_PARAM_TABLE);
480 if (!partablename.empty()) {
481 if (haveCommonPeakParameters) {
482 const std::string msg =
"Parameter value table and initial parameter "
483 "name/value vectors cannot be given "
485 issues[PropertyNames::PEAK_PARAM_TABLE] = msg;
486 issues[PropertyNames::PEAK_PARAM_NAMES] = msg;
487 issues[PropertyNames::PEAK_PARAM_VALUES] = msg;
495 if (!suppliedParameterNames.empty()) {
498 std::dynamic_pointer_cast<IPeakFunction>(API::FunctionFactory::Instance().createFunction(peakfunctiontype));
501 std::vector<string> functionParameterNames;
503 functionParameterNames.emplace_back(
m_peakFunction->parameterName(i));
506 const bool failed = std::any_of(suppliedParameterNames.cbegin(), suppliedParameterNames.cend(),
507 [&functionParameterNames](
const auto &parName) {
508 return std::find(functionParameterNames.begin(), functionParameterNames.end(),
509 parName) == functionParameterNames.end();
512 std::string msg =
"Specified invalid parameter for peak function";
513 if (haveCommonPeakParameters)
514 issues[PropertyNames::PEAK_PARAM_NAMES] = msg;
516 issues[PropertyNames::PEAK_PARAM_TABLE] = msg;
521 const std::string error_table_name =
getPropertyValue(PropertyNames::OUTPUT_WKSP_PARAM_ERRS);
522 if (!error_table_name.empty()) {
523 const bool use_raw_params =
getProperty(PropertyNames::RAW_PARAMS);
524 if (!use_raw_params) {
525 issues[PropertyNames::OUTPUT_WKSP_PARAM_ERRS] =
"Cannot be used with " + PropertyNames::RAW_PARAMS +
"=False";
526 issues[PropertyNames::RAW_PARAMS] =
527 "Cannot be False with " + PropertyNames::OUTPUT_WKSP_PARAM_ERRS +
" specified";
566 int start_wi =
getProperty(PropertyNames::START_WKSP_INDEX);
570 int stop_wi =
getProperty(PropertyNames::STOP_WKSP_INDEX);
593 throw std::runtime_error(
"number of peaks to fit is zero.");
599 throw std::runtime_error(
"PeakWidthPercent must be less than 1");
604 double temp =
getProperty(PropertyNames::BACKGROUND_Z_SCORE);
606 std::ostringstream os;
607 os <<
"FitPeaks property \"" << PropertyNames::BACKGROUND_Z_SCORE <<
"\" is deprecated and will be ignored."
639 std::dynamic_pointer_cast<IPeakFunction>(API::FunctionFactory::Instance().createFunction(peakfunctiontype));
643 std::string bkgdname;
644 if (bkgdfunctiontype ==
"Linear")
645 bkgdname =
"LinearBackground";
646 else if (bkgdfunctiontype ==
"Flat") {
647 g_log.
warning(
"There may be problems with Flat background");
648 bkgdname =
"FlatBackground";
650 bkgdname = bkgdfunctiontype;
652 std::dynamic_pointer_cast<IBackgroundFunction>(API::FunctionFactory::Instance().createFunction(bkgdname));
655 API::FunctionFactory::Instance().createFunction(
"LinearBackground"));
661 std::string partablename =
getPropertyValue(PropertyNames::PEAK_PARAM_TABLE);
674 }
else if (peakfunctiontype !=
"Gaussian") {
676 g_log.
warning(
"Neither parameter value table nor initial "
677 "parameter name/value vectors is specified. Fitting might "
678 "not be reliable for peak profile other than Gaussian");
690 std::vector<double> peakwindow =
getProperty(PropertyNames::FIT_WINDOW_LIST);
691 std::string peakwindowname =
getPropertyValue(PropertyNames::FIT_WINDOW_WKSP);
696 if ((!peakwindow.empty()) && peakwindowname.empty()) {
701 throw std::invalid_argument(
702 "Specifying peak windows with a list requires also specifying peak positions with a list.");
705 throw std::invalid_argument(
"Peak window vector must be twice as large as number of peaks.");
710 std::vector<double> peakranges(2);
711 peakranges[0] = peakwindow[i * 2];
712 peakranges[1] = peakwindow[i * 2 + 1];
719 std::stringstream errss;
720 errss <<
"Peak " << i <<
": user specifies an invalid range and peak center against " << peakranges[0] <<
" < "
722 throw std::invalid_argument(errss.str());
725 m_getPeakFitWindow = [
this](std::size_t wi, std::size_t ipeak) -> std::pair<double, double> {
734 }
else if (peakwindow.empty() && peakwindowws !=
nullptr) {
742 if (peakWindowX.empty()) {
743 std::stringstream errss;
744 errss <<
"Peak window required at workspace index " << wi <<
" "
745 <<
"which is undefined in the peak window workspace. "
746 <<
"Ensure workspace indices correspond in peak window workspace and input workspace "
747 <<
"when using start and stop indices.";
748 throw std::invalid_argument(errss.str());
751 if (peakWindowX.size() % 2 != 0) {
752 throw std::invalid_argument(
"The peak window vector must be even, with two edges for each peak center.");
754 if (peakWindowX.size() != peakCenterX.size() * 2) {
755 std::stringstream errss;
756 errss <<
"Peak window workspace index " << wi <<
" has incompatible number of fit windows "
757 << peakWindowX.size() / 2 <<
" with the number of peaks " << peakCenterX.size() <<
" to fit.";
758 throw std::invalid_argument(errss.str());
761 for (
size_t ipeak = 0; ipeak < peakCenterX.size(); ++ipeak) {
762 double left_w_bound = peakWindowX[ipeak * 2];
763 double right_w_bound = peakWindowX[ipeak * 2 + 1];
764 double center = peakCenterX[ipeak];
766 if (!(left_w_bound < center && center < right_w_bound)) {
767 std::stringstream errss;
768 errss <<
"Workspace index " << wi <<
" has incompatible peak window "
769 <<
"(" << left_w_bound <<
", " << right_w_bound <<
") "
770 <<
"with " << ipeak <<
"-th expected peak's center " << center;
771 throw std::runtime_error(errss.str());
775 m_getPeakFitWindow = [
this](std::size_t wi, std::size_t ipeak) -> std::pair<double, double> {
784 }
else if (peakwindow.empty()) {
790 m_getPeakFitWindow = [
this](std::size_t wi, std::size_t ipeak) -> std::pair<double, double> {
799 double left = peak_pos - estimate_peak_width * THREE;
800 double right = peak_pos + estimate_peak_width * THREE;
805 throw std::invalid_argument(
"Without definition of peak window, the "
806 "input workspace must be in unit of dSpacing "
807 "and Delta(D)/D must be given!");
811 throw std::invalid_argument(
"One and only one of peak window array and "
812 "peak window workspace can be specified.");
832 g_log.
notice(
"Peak centers are not specified by peak center workspace");
834 std::string peakpswsname =
getPropertyValue(PropertyNames::PEAK_CENTERS_WKSP);
844 }
else if (
m_peakCenters.empty() && peakcenterws !=
nullptr) {
854 std::stringstream errss;
857 <<
"However, the peak center workspace does not have values defined "
858 <<
"at workspace index " << wi <<
". "
859 <<
"Make sure the workspace indices between input and peak center workspaces correspond.";
861 throw std::invalid_argument(errss.str());
871 std::stringstream errss;
872 errss <<
"One and only one in 'PeakCenters' (vector) and "
873 "'PeakCentersWorkspace' shall be given. "
874 <<
"'PeakCenters' has size " <<
m_peakCenters.size() <<
", and name of peak center workspace "
875 <<
"is " << peakpswsname;
876 throw std::invalid_argument(errss.str());
891 throw std::runtime_error(
"ProcessInputPeakTolerance() must be called after "
892 "ProcessInputPeakCenters()");
909 throw std::runtime_error(
"Number of peak position tolerances and number of "
910 "peaks to fit are inconsistent.");
948 std::map<std::string, size_t> parname_index_map;
949 for (
size_t iparam = 0; iparam <
m_peakFunction->nParams(); ++iparam)
950 parname_index_map.insert(std::make_pair(
m_peakFunction->parameterName(iparam), iparam));
958 auto locator = parname_index_map.find(paramName);
959 if (locator != parname_index_map.end()) {
965 <<
" is not an allowed parameter of peak "
983 std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> fit_result_vector(
m_numSpectraToFit);
985 const int nThreads = FrameworkManager::Instance().getNumOMPThreads();
988 std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> pre_check_result =
989 std::make_shared<FitPeaksAlgorithm::PeakFitPreCheckResult>();
991 PRAGMA_OMP(parallel
for schedule(dynamic, 1) )
992 for (
int ithread = 0; ithread < nThreads; ithread++) {
998 std::vector<std::vector<double>> lastGoodPeakParameters(
m_numPeaksToFit,
1001 for (
auto wi = iws_begin; wi < iws_end; ++wi) {
1007 std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> fit_result =
1008 std::make_shared<FitPeaksAlgorithm::PeakFitResult>(
m_numPeaksToFit, numfuncparams);
1010 std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> spectrum_pre_check_result =
1011 std::make_shared<FitPeaksAlgorithm::PeakFitPreCheckResult>();
1013 fitSpectrumPeaks(
static_cast<size_t>(wi), expected_peak_centers, fit_result, lastGoodPeakParameters,
1014 spectrum_pre_check_result);
1017 writeFitResult(
static_cast<size_t>(wi), expected_peak_centers, fit_result);
1019 *pre_check_result += *spectrum_pre_check_result;
1027 return fit_result_vector;
1032bool estimateBackgroundParameters(
const Histogram &histogram,
const std::pair<size_t, size_t> &peak_window,
1035 std::vector<double> &vec_y);
1036template <
typename vector_like>
1037void rangeToIndexBounds(
const vector_like &vecx,
const double range_left,
const double range_right,
size_t &left_index,
1038 size_t &right_index);
1041std::vector<std::string> supported_peak_profiles{
"Gaussian",
"Lorentzian",
"PseudoVoigt",
"Voigt",
1042 "BackToBackExponential"};
1049double estimateBackgroundNoise(
const std::vector<double> &vec_y) {
1051 size_t half_number_of_bkg_datapoints{5};
1052 if (vec_y.size() < 2 * half_number_of_bkg_datapoints + 3 )
1057 std::vector<double> vec_bkg;
1058 vec_bkg.resize(2 * half_number_of_bkg_datapoints);
1059 std::copy(vec_y.begin(), vec_y.begin() + half_number_of_bkg_datapoints, vec_bkg.begin());
1060 std::copy(vec_y.end() - half_number_of_bkg_datapoints, vec_y.end(), vec_bkg.begin() + half_number_of_bkg_datapoints);
1064 std::vector<double> vec_bkg_no_outliers;
1065 vec_bkg_no_outliers.resize(vec_bkg.size());
1066 double zscore_crit = 3.;
1067 for (
size_t ii = 0; ii < vec_bkg.size(); ii++) {
1068 if (zscore_vec[ii] <= zscore_crit)
1069 vec_bkg_no_outliers.push_back(vec_bkg[ii]);
1072 if (vec_bkg_no_outliers.size() < half_number_of_bkg_datapoints)
1076 return intensityStatistics.standard_deviation;
1087template <
typename vector_like>
1088void rangeToIndexBounds(
const vector_like &elems,
const double range_left,
const double range_right,
size_t &left_index,
1089 size_t &right_index) {
1090 const auto left_iter = std::lower_bound(elems.cbegin(), elems.cend(), range_left);
1091 const auto right_iter = std::upper_bound(elems.cbegin(), elems.cend(), range_right);
1093 left_index = std::distance(elems.cbegin(), left_iter);
1094 right_index = std::distance(elems.cbegin(), right_iter);
1095 right_index = std::min(right_index, elems.size() - 1);
1105 std::vector<double> &vec_y) {
1109 bkgd_func->function(vectorx, vector_bkgd);
1112 for (
size_t i = 0; i < vec_y.size(); ++i) {
1113 (vec_y)[i] -= vector_bkgd[i];
1122class LoggingOffsetSentry {
1140 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result,
1141 std::vector<std::vector<double>> &lastGoodPeakParameters,
1142 const std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> &pre_check_result) {
1148 fit_result->setBadRecord(i, -1.);
1149 pre_check_result->setNumberOfSpectrumPeaksWithLowCount(
m_numPeaksToFit);
1158 std::stringstream errss;
1159 errss <<
"The FitPeak algorithm requires the CurveFitting library";
1161 throw std::runtime_error(errss.str());
1168 peak_fitter->setProperty(
"Minimizer",
m_minimizer);
1170 peak_fitter->setProperty(
"CalcErrors",
true);
1177 bool neighborPeakSameSpectrum =
false;
1178 size_t number_of_out_of_range_peaks{0};
1179 for (
size_t fit_index = 0; fit_index <
m_numPeaksToFit; ++fit_index) {
1181 size_t peak_index(fit_index);
1186 for (
size_t i = 0; i < bkgdfunction->nParams(); ++i)
1187 bkgdfunction->setParameter(i, 0.);
1189 double expected_peak_pos = expected_peak_centers[peak_index];
1193 auto peakfunction = std::dynamic_pointer_cast<API::IPeakFunction>(
m_peakFunction->clone());
1194 peakfunction->setCentre(expected_peak_pos);
1197 std::map<size_t, double> keep_values;
1198 for (
size_t ipar = 0; ipar < peakfunction->nParams(); ++ipar) {
1199 if (peakfunction->isFixed(ipar)) {
1203 keep_values[ipar] = peakfunction->getParameter(ipar);
1206 peakfunction->unfix(ipar);
1212 bool samePeakCrossSpectrum = (lastGoodPeakParameters[peak_index].size() >
1213 static_cast<size_t>(std::count_if(lastGoodPeakParameters[peak_index].begin(),
1214 lastGoodPeakParameters[peak_index].end(),
1215 [&](
auto const &val) {
return val <= 1e-10; })));
1220 if (wi > 0 && samePeakCrossSpectrum) {
1223 std::shared_ptr<const Geometry::Detector> pdetector =
1224 std::dynamic_pointer_cast<const Geometry::Detector>(
m_inputMatrixWS->getDetector(wi - 1));
1225 std::shared_ptr<const Geometry::Detector> cdetector =
1226 std::dynamic_pointer_cast<const Geometry::Detector>(
m_inputMatrixWS->getDetector(wi));
1229 if (pdetector && cdetector) {
1230 auto prev_id = pdetector->getID();
1231 auto curr_id = cdetector->getID();
1232 if (prev_id + 1 != curr_id)
1233 samePeakCrossSpectrum =
false;
1235 samePeakCrossSpectrum =
false;
1241 samePeakCrossSpectrum =
false;
1243 }
catch (
const std::runtime_error &) {
1246 samePeakCrossSpectrum =
false;
1250 if (samePeakCrossSpectrum) {
1252 for (
size_t i = 0; i < peakfunction->nParams(); ++i) {
1253 peakfunction->setParameter(i, lastGoodPeakParameters[peak_index][i]);
1255 }
else if (neighborPeakSameSpectrum) {
1257 for (
size_t i = 0; i < peakfunction->nParams(); ++i) {
1258 peakfunction->setParameter(i, lastGoodPeakParameters[prev_peak_index][i]);
1263 peakfunction->setCentre(expected_peak_pos);
1265 for (
const auto &[ipar,
value] : keep_values) {
1266 peakfunction->setParameter(ipar,
value);
1269 double cost(DBL_MAX);
1270 if (expected_peak_pos <= x0 || expected_peak_pos >= xf) {
1272 peakfunction->setIntensity(0);
1273 number_of_out_of_range_peaks++;
1283 auto useUserSpecifedIfGiven = !(samePeakCrossSpectrum || neighborPeakSameSpectrum);
1287 g_log.
warning(
"Peak width can be estimated as ZERO. The result can be wrong");
1291 std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> peak_pre_check_result =
1292 std::make_shared<FitPeaksAlgorithm::PeakFitPreCheckResult>();
1293 cost =
fitIndividualPeak(wi, peak_fitter, expected_peak_pos, peak_window_i, observe_peak_width, peakfunction,
1294 bkgdfunction, peak_pre_check_result);
1295 if (peak_pre_check_result->isIndividualPeakRejected())
1296 fit_result->setBadRecord(peak_index, -1.);
1300 fit_result->setBadRecord(peak_index, -1.);
1305 *pre_check_result += *peak_pre_check_result;
1307 pre_check_result->setNumberOfOutOfRangePeaks(number_of_out_of_range_peaks);
1319 neighborPeakSameSpectrum =
true;
1320 prev_peak_index = peak_index;
1322 for (
size_t i = 0; i < lastGoodPeakParameters[peak_index].size(); ++i) {
1323 lastGoodPeakParameters[peak_index][i] = peakfunction->getParameter(i);
1348 if (firstPeakInSpectrum) {
1354 peak_function->setParameter(param_index, param_value);
1362 observe_peak_shape =
true;
1365 return observe_peak_shape;
1381 const std::vector<double> &expected_peak_positions,
1383 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result) {
1385 double postol(DBL_MAX);
1399 throw std::runtime_error(
"Peak tolerance out of index");
1407 bool good_fit(
false);
1408 if ((cost < 0) || (cost >= DBL_MAX - 1.) || std::isnan(cost)) {
1414 }
else if (case23) {
1417 if (fitwindow.first < fitwindow.second) {
1420 if (peak_pos < fitwindow.first || peak_pos > fitwindow.second) {
1423 g_log.
debug() <<
"Peak position " << peak_pos <<
" is out of fit "
1424 <<
"window boundary " << fitwindow.first <<
", " << fitwindow.second <<
"\n";
1425 }
else if (peak_fwhm > (fitwindow.second - fitwindow.first)) {
1428 g_log.
debug() <<
"Peak position " << peak_pos <<
" has fwhm "
1429 <<
"wider than the fit window " << fitwindow.second - fitwindow.first <<
"\n";
1435 double left_bound(-1);
1437 left_bound = 0.5 * (expected_peak_positions[peakindex] - expected_peak_positions[peakindex - 1]);
1438 double right_bound(-1);
1440 right_bound = 0.5 * (expected_peak_positions[peakindex + 1] - expected_peak_positions[peakindex]);
1442 left_bound = right_bound;
1443 if (right_bound < left_bound)
1444 right_bound = left_bound;
1445 if (left_bound < 0 || right_bound < 0)
1446 throw std::runtime_error(
"Code logic error such that left or right "
1447 "boundary of peak position is negative.");
1448 if (peak_pos < left_bound || peak_pos > right_bound) {
1450 }
else if (peak_fwhm > (right_bound - left_bound)) {
1453 g_log.
debug() <<
"Peak position " << peak_pos <<
" has fwhm "
1454 <<
"wider than the fit window " << right_bound - left_bound <<
"\n";
1459 }
else if (
fabs(fitfunction.
peakfunction->centre() - expected_peak_positions[peakindex]) > postol) {
1463 <<
fabs(fitfunction.
peakfunction->centre() - expected_peak_positions[peakindex])
1464 <<
" is out of range of tolerance: " << postol <<
"\n";
1471 double adjust_cost(cost);
1474 adjust_cost = DBL_MAX;
1478 if (adjust_cost > DBL_MAX - 1) {
1483 fit_result->setRecord(peakindex, adjust_cost, peak_pos, fitfunction);
1497 throw std::runtime_error(
"No parameters");
1505 std::size_t iws =
static_cast<std::size_t
>(iiws);
1509 std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> fit_result_i = fit_results[iws -
m_startWorkspaceIndex];
1512 throw std::runtime_error(
"There is something wroing with PeakFitResult vector!");
1516 const double chi2 = fit_result_i->getCost(ipeak);
1520 for (
size_t iparam = 0; iparam < num_peakfunc_params; ++iparam)
1521 peak_function->setParameter(iparam, fit_result_i->getParameterValue(ipeak, iparam));
1522 for (
size_t iparam = 0; iparam < num_bkgdfunc_params; ++iparam)
1523 bkgd_function->setParameter(iparam, fit_result_i->getParameterValue(ipeak, num_peakfunc_params + iparam));
1528 auto start_x_iter = std::lower_bound(vec_x.begin(), vec_x.end(), peakwindow.first);
1529 auto stop_x_iter = std::lower_bound(vec_x.begin(), vec_x.end(), peakwindow.second);
1531 if (start_x_iter == stop_x_iter)
1532 throw std::runtime_error(
"Range size is zero in calculateFittedPeaks");
1537 comp_func->addFunction(peak_function);
1538 comp_func->addFunction(bkgd_function);
1539 comp_func->function(domain, values);
1542 std::size_t istart =
static_cast<size_t>(start_x_iter - vec_x.begin());
1543 std::size_t istop =
static_cast<size_t>(stop_x_iter - vec_x.begin());
1544 for (std::size_t yindex = istart; yindex < istop; ++yindex) {
1558 auto startX = std::lower_bound(vecX.begin(), vecX.end(), peakWindow.first);
1559 auto stopX = std::lower_bound(vecX.begin(), vecX.end(), peakWindow.second);
1564 peakFunction->function(domain, values);
1565 auto peakValues = values.
toVector();
1568 auto startE = errors.begin() + (startX - vecX.begin());
1569 auto stopE = errors.begin() + (stopX - vecX.begin());
1570 std::vector<double> peakErrors(startE, stopE);
1572 double peakSum = std::accumulate(peakValues.cbegin(), peakValues.cend(), 0.0);
1579bool estimateBackgroundParameters(
const Histogram &histogram,
const std::pair<size_t, size_t> &peak_window,
1583 const auto POLYNOMIAL_ORDER = std::min<size_t>(1, bkgd_function->nParams());
1585 if (peak_window.first >= peak_window.second)
1586 throw std::runtime_error(
"Invalid peak window");
1589 const auto nParams = bkgd_function->nParams();
1590 for (
size_t i = 0; i < nParams; ++i)
1591 bkgd_function->setParameter(i, 0.);
1594 const size_t iback_start = peak_window.first + 10;
1595 const size_t iback_stop = peak_window.second - 10;
1600 if (iback_start < iback_stop) {
1604 double chisq{DBL_MAX};
1605 HistogramData::estimateBackground(POLYNOMIAL_ORDER, histogram, peak_window.first, peak_window.second, iback_start,
1606 iback_stop, bkgd_a0, bkgd_a1, bkgd_a2, chisq);
1608 bkgd_function->setParameter(0, bkgd_a0);
1610 bkgd_function->setParameter(1, bkgd_a1);
1628 return (std::find(supported_peak_profiles.begin(), supported_peak_profiles.end(), peakprofile) !=
1629 supported_peak_profiles.end());
1637 constexpr size_t MIN_POINTS{10};
1641 size_t start_index =
findXIndex(points.rawData(), fit_window.first);
1642 size_t expected_peak_index =
findXIndex(points.rawData(), expected_peak_pos, start_index);
1643 size_t stop_index =
findXIndex(points.rawData(), fit_window.second, expected_peak_index);
1646 bool good_fit(
false);
1647 if (expected_peak_index - start_index > MIN_POINTS && stop_index - expected_peak_index > MIN_POINTS) {
1650 const std::pair<double, double> vec_min{fit_window.first, points[expected_peak_index + 5]};
1651 const std::pair<double, double> vec_max{points[expected_peak_index - 5], fit_window.second};
1654 for (
size_t n = 0;
n < bkgd_func->nParams(); ++
n)
1655 bkgd_func->setParameter(
n, 0);
1660 if (chi2 < DBL_MAX - 1) {
1668 g_log.
debug() <<
"Don't know what to do with background fitting with single "
1669 <<
"domain function! " << (expected_peak_index - start_index) <<
" points to the left "
1670 << (stop_index - expected_peak_index) <<
" points to the right\n";
1680 const std::pair<double, double> &fitwindow,
const bool estimate_peak_width,
1683 const std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> &pre_check_result) {
1684 pre_check_result->setNumberOfSubmittedIndividualPeaks(1);
1685 double cost(DBL_MAX);
1688 size_t min_required_datapoints{peakfunction->nParams() + bkgdfunc->nParams() + 2};
1690 if (number_of_datapoints < min_required_datapoints) {
1691 pre_check_result->setNumberOfPeaksWithNotEnoughDataPoints(1);
1697 pre_check_result->setNumberOfIndividualPeaksWithLowCount(1);
1703 pre_check_result->setNumberOfPeaksWithLowSignalToNoise(1);
1714 estimate_peak_width,
true);
1729 const std::pair<double, double> &peak_range,
const double &expected_peak_center,
1730 bool estimate_peak_width,
bool estimate_background) {
1731 std::stringstream errorid;
1732 errorid <<
"(WorkspaceIndex=" << wsindex <<
" PeakCentre=" << expected_peak_center <<
")";
1735 if (peak_range.first >= peak_range.second) {
1736 std::stringstream msg;
1737 msg <<
"Invalid peak window: xmin>xmax (" << peak_range.first <<
", " << peak_range.second <<
")" << errorid.str();
1738 throw std::runtime_error(msg.str());
1742 const auto &histogram = dataws->histogram(wsindex);
1743 const auto &vector_x = histogram.points();
1744 const auto start_index =
findXIndex(vector_x, peak_range.first);
1745 const auto stop_index =
findXIndex(vector_x, peak_range.second, start_index);
1746 if (start_index == stop_index)
1747 throw std::runtime_error(
"Range size is zero in fitFunctionSD");
1748 std::pair<size_t, size_t> peak_index_window = std::make_pair(start_index, stop_index);
1751 if (estimate_background) {
1752 if (!estimateBackgroundParameters(histogram, peak_index_window, bkgd_function)) {
1758 peak_function->setCentre(expected_peak_center);
1759 int result =
estimatePeakParameters(histogram, peak_index_window, peak_function, bkgd_function, estimate_peak_width,
1762 if (result !=
GOOD) {
1763 peak_function->setCentre(expected_peak_center);
1771 comp_func->addFunction(peak_function);
1772 comp_func->addFunction(bkgd_function);
1773 IFunction_sptr fitfunc = std::dynamic_pointer_cast<IFunction>(comp_func);
1776 fit->setProperty(
"Function", fitfunc);
1777 fit->setProperty(
"InputWorkspace", dataws);
1778 fit->setProperty(
"WorkspaceIndex",
static_cast<int>(wsindex));
1780 fit->setProperty(
"StartX", peak_range.first);
1781 fit->setProperty(
"EndX", peak_range.second);
1782 fit->setProperty(
"IgnoreInvalidData",
true);
1786 double peak_center = peak_function->centre();
1787 double peak_width = peak_function->fwhm();
1788 std::stringstream peak_center_constraint;
1789 peak_center_constraint << (peak_center - 0.5 * peak_width) <<
" < f0." << peak_function->getCentreParameterName()
1790 <<
" < " << (peak_center + 0.5 * peak_width);
1791 fit->setProperty(
"Constraints", peak_center_constraint.str());
1795 g_log.
debug() <<
"[E1201] FitSingleDomain Before fitting, Fit function: " << fit->asString() <<
"\n";
1796 errorid <<
" starting function [" << comp_func->asString() <<
"]";
1799 g_log.
debug() <<
"[E1202] FitSingleDomain After fitting, Fit function: " << fit->asString() <<
"\n";
1801 if (!fit->isExecuted()) {
1802 g_log.
warning() <<
"Fitting peak SD (single domain) failed to execute. " + errorid.str();
1805 }
catch (std::invalid_argument &e) {
1806 errorid <<
": " << e.what();
1812 std::string fitStatus = fit->getProperty(
"OutputStatus");
1813 double chi2{std::numeric_limits<double>::max()};
1814 if (fitStatus ==
"success") {
1815 chi2 = fit->getProperty(
"OutputChi2overDoF");
1823 const size_t wsindex,
const std::pair<double, double> &vec_xmin,
1824 const std::pair<double, double> &vec_xmax) {
1830 std::stringstream errss;
1831 errss <<
"The FitPeak algorithm requires the CurveFitting library";
1832 throw std::runtime_error(errss.str());
1837 fit->setProperty(
"CalcErrors",
true);
1841 std::shared_ptr<MultiDomainFunction> md_function = std::make_shared<MultiDomainFunction>();
1844 md_function->addFunction(std::move(fit_function));
1847 md_function->clearDomainIndices();
1848 md_function->setDomainIndices(0, {0, 1});
1851 fit->setProperty(
"Function", std::dynamic_pointer_cast<IFunction>(md_function));
1852 fit->setProperty(
"InputWorkspace", dataws);
1853 fit->setProperty(
"WorkspaceIndex",
static_cast<int>(wsindex));
1854 fit->setProperty(
"StartX", vec_xmin.first);
1855 fit->setProperty(
"EndX", vec_xmax.first);
1856 fit->setProperty(
"InputWorkspace_1", dataws);
1857 fit->setProperty(
"WorkspaceIndex_1",
static_cast<int>(wsindex));
1858 fit->setProperty(
"StartX_1", vec_xmin.second);
1859 fit->setProperty(
"EndX_1", vec_xmax.second);
1861 fit->setProperty(
"IgnoreInvalidData",
true);
1865 if (!fit->isExecuted()) {
1866 throw runtime_error(
"Fit is not executed on multi-domain function/data. ");
1870 std::string fitStatus = fit->getProperty(
"OutputStatus");
1872 double chi2 = DBL_MAX;
1873 if (fitStatus ==
"success") {
1874 chi2 = fit->getProperty(
"OutputChi2overDoF");
1883 const size_t &ws_index,
const double &expected_peak_center,
1893 fitBackground(ws_index, fit_window, expected_peak_center, high_bkgd_function);
1896 std::vector<double> vec_x, vec_y, vec_e;
1897 getRangeData(ws_index, fit_window, vec_x, vec_y, vec_e);
1900 reduceByBackground(high_bkgd_function, vec_x, vec_y);
1901 for (std::size_t
n = 0;
n < bkgdfunc->nParams(); ++
n)
1902 bkgdfunc->setParameter(
n, 0);
1908 fitFunctionSD(fit, peakfunction, bkgdfunc, reduced_bkgd_ws, 0, {vec_x.front(), vec_x.back()}, expected_peak_center,
1909 observe_peak_shape,
false);
1912 bkgdfunc->setParameter(0, bkgdfunc->getParameter(0) + high_bkgd_function->getParameter(0));
1913 bkgdfunc->setParameter(1, bkgdfunc->getParameter(1) +
1914 high_bkgd_function->getParameter(1));
1917 expected_peak_center,
false,
false);
1925 const std::vector<double> &vec_y,
1926 const std::vector<double> &vec_e) {
1927 std::size_t size = vec_x.size();
1928 std::size_t ysize = vec_y.size();
1930 HistogramBuilder builder;
1932 builder.setY(ysize);
1935 auto &dataX = matrix_ws->mutableX(0);
1936 auto &dataY = matrix_ws->mutableY(0);
1937 auto &dataE = matrix_ws->mutableE(0);
1939 dataX.assign(vec_x.cbegin(), vec_x.cend());
1940 dataY.assign(vec_y.cbegin(), vec_y.cend());
1941 dataE.assign(vec_e.cbegin(), vec_e.cend());
1957 for (std::size_t ipeak = 0; ipeak < expected_position.size(); ++ipeak) {
1973 const std::vector<std::string> ¶m_names,
bool with_chi2) {
1975 table_ws->addColumn(
"int",
"wsindex");
1976 table_ws->addColumn(
"int",
"peakindex");
1977 for (
const auto ¶m_name : param_names)
1978 table_ws->addColumn(
"double", param_name);
1980 table_ws->addColumn(
"double",
"chi2");
1987 newRow << static_cast<int>(iws);
1988 newRow << static_cast<int>(ipeak);
1989 for (
size_t iparam = 0; iparam < numParam; ++iparam)
2010 std::vector<std::string> param_vec;
2014 param_vec.emplace_back(
"centre");
2015 param_vec.emplace_back(
"width");
2016 param_vec.emplace_back(
"height");
2017 param_vec.emplace_back(
"intensity");
2020 for (
size_t iparam = 0; iparam <
m_bkgdFunction->nParams(); ++iparam)
2028 std::string fiterror_table_name =
getPropertyValue(PropertyNames::OUTPUT_WKSP_PARAM_ERRS);
2030 if (fiterror_table_name.empty()) {
2048 std::string fit_ws_name =
getPropertyValue(PropertyNames::OUTPUT_WKSP_MODEL);
2049 if (fit_ws_name.size() == 0) {
2074 g_log.
debug(
"about to calcualte fitted peaks");
2086 const std::vector<double> &vec_y =
m_inputMatrixWS->histogram(iws).y().rawData();
2087 double total = std::accumulate(vec_y.begin(), vec_y.end(), 0.);
2099 std::vector<double> vec_x, vec_y, vec_e;
2102 double total = std::accumulate(vec_y.begin(), vec_y.end(), 0.);
2113 size_t left_index, right_index;
2115 size_t number_dp = right_index - left_index + 1;
2118 assert(number_dp > 0);
2131void
FitPeaks::histRangeToIndexBounds(
size_t iws, const
std::pair<
double,
double> &range,
size_t &left_index,
2132 size_t &right_index) {
2133 const auto &orig_x = m_inputMatrixWS->histogram(iws).x();
2134 rangeToIndexBounds(orig_x, range.first, range.second, left_index, right_index);
2138 if (left_index >= right_index || (m_inputMatrixWS->isHistogramData() && left_index == right_index - 1)) {
2139 std::stringstream err_ss;
2140 err_ss <<
"Unable to get a valid subset of histogram from given fit window. "
2141 <<
"Histogram X: " << orig_x.front() <<
"," << orig_x.back() <<
"; Range: " << range.first <<
","
2143 throw std::runtime_error(err_ss.str());
2156 std::vector<double> &vec_y, std::vector<double> &vec_e) {
2158 size_t left_index, right_index;
2162 size_t num_elements_x = right_index - left_index;
2164 vec_x.resize(num_elements_x);
2166 std::copy(orig_x.begin() + left_index, orig_x.begin() + right_index, vec_x.begin());
2168 size_t num_datapoints =
m_inputMatrixWS->isHistogramData() ? num_elements_x - 1 : num_elements_x;
2170 const std::vector<double> orig_y =
m_inputMatrixWS->histogram(iws).y().rawData();
2171 const std::vector<double> orig_e =
m_inputMatrixWS->histogram(iws).e().rawData();
2172 vec_y.resize(num_datapoints);
2173 vec_e.resize(num_datapoints);
2174 std::copy(orig_y.begin() + left_index, orig_y.begin() + left_index + num_datapoints, vec_y.begin());
2175 std::copy(orig_e.begin() + left_index, orig_e.begin() + left_index + num_datapoints, vec_e.begin());
2187double
FitPeaks::calculateSignalToNoiseRatio(
size_t iws, const
std::pair<
double,
double> &range,
2190 size_t left_index, right_index;
2191 histRangeToIndexBounds(iws, range, left_index, right_index);
2194 if (!estimateBackgroundParameters(m_inputMatrixWS->histogram(iws), std::pair<size_t, size_t>(left_index, right_index),
2199 std::vector<double> vec_x, vec_y, vec_e;
2200 getRangeData(iws, range, vec_x, vec_y, vec_e);
2205 reduceByBackground(bkgd_function, vec_x, vec_y);
2208 auto it_max = std::max_element(vec_y.begin(), vec_y.end());
2209 double signal = vec_y[it_max - vec_y.begin()];
2210 if (signal <= DBL_MIN)
2215 double noise = estimateBackgroundNoise(vec_y);
2216 if (noise <= DBL_MIN)
2220 return signal / noise;
2228 std::stringstream errss;
2229 errss <<
"Workspace index " << wi <<
" is out of range "
2231 throw std::runtime_error(errss.str());
2238 std::stringstream errss;
2239 errss <<
"Peak index " << ipeak <<
" is out of range (" <<
m_numPeaksToFit <<
")";
2240 throw std::runtime_error(errss.str());
2246 std::stringstream errss;
2247 errss <<
"Peak window is inappropriate for workspace index: " <<
left <<
" >= " <<
right;
2248 throw std::runtime_error(errss.str());
2262 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result) {
2266 g_log.
error() <<
"workspace index " << wi <<
" is out of output peak position workspace "
2270 throw std::runtime_error(
"Out of boundary to set output peak position workspace");
2275 double exp_peak_pos(expected_positions[ipeak]);
2276 double fitted_peak_pos = fit_result->getPeakPosition(ipeak);
2277 double peak_chi2 = fit_result->getCost(ipeak);
2296 <<
" parameters. Parameter table shall have 3 more "
2297 "columns. But not it has "
2299 throw std::runtime_error(
"Peak parameter vector for one peak has different sizes to output "
2306 std::stringstream err_ss;
2307 err_ss <<
"Peak has 4 effective peak parameters and " <<
m_bkgdFunction->nParams() <<
" background parameters "
2308 <<
". Parameter table shall have 3 more columns. But not it has " <<
m_fittedParamTable->columnCount()
2310 throw std::runtime_error(err_ss.str());
2317 size_t num_peakfunc_params = peak_function->nParams();
2327 for (
size_t iparam = 0; iparam < num_peakfunc_params + num_bkgd_params; ++iparam) {
2328 size_t col_index = iparam + 2;
2330 m_fittedParamTable->cell<
double>(row_index, col_index) = fit_result->getParameterValue(ipeak, iparam);
2333 m_fitErrorTable->cell<
double>(row_index, col_index) = fit_result->getParameterError(ipeak, iparam);
2340 for (
size_t iparam = 0; iparam < num_peakfunc_params; ++iparam)
2341 peak_function->setParameter(iparam, fit_result->getParameterValue(ipeak, iparam));
2350 for (
size_t iparam = 0; iparam < num_bkgd_params; ++iparam)
2352 fit_result->getParameterValue(ipeak, num_peakfunc_params + iparam);
2356 m_fittedParamTable->cell<
double>(row_index, chi2_index) = fit_result->getCost(ipeak);
2364 std::string height_name(
"");
2366 std::vector<std::string> peak_parameters = peak_function->getParameterNames();
2367 for (
const auto &parName : peak_parameters) {
2368 if (parName ==
"Height") {
2369 height_name =
"Height";
2371 }
else if (parName ==
"I") {
2374 }
else if (parName ==
"Intensity") {
2375 height_name =
"Intensity";
2380 if (height_name.empty())
2381 throw std::runtime_error(
"Peak height parameter name cannot be found.");
2389 LoggingOffsetSentry sentry(
this);
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
#define PARALLEL_CRITICAL(name)
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PRAGMA_OMP(expression)
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Base class from which all concrete algorithm classes should be derived.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
void setLoggingOffset(const int value) override
gets the logging priority offset
int getLoggingOffset() const override
returns the logging priority offset
bool isDefault(const std::string &name) const
static bool isEmpty(const NumT toCheck)
checks that the value was not set by users, uses the value in empty double/int.
Implements FunctionDomain1D with its own storage in form of a std::vector.
A class to store values calculated by a function.
const std::vector< double > & toVector() const
Return the calculated values as a vector.
double getCalculated(size_t i) const
Get i-th calculated value.
An interface to a peak function, which extend the interface of IFunctionWithLocation by adding method...
Helper class for reporting progress from algorithms.
TableRow represents a row in a TableWorkspace.
A property class for workspaces.
size_t m_submitted_spectrum_peaks
void setNumberOfSpectrumPeaksWithLowCount(const size_t n)
std::string getReport() const
size_t m_low_count_individual
PeakFitPreCheckResult & operator+=(const PeakFitPreCheckResult &another)
size_t m_not_enough_datapoints
void setNumberOfSubmittedIndividualPeaks(const size_t n)
void setNumberOfPeaksWithNotEnoughDataPoints(const size_t n)
size_t m_low_count_spectrum
void setNumberOfOutOfRangePeaks(const size_t n)
void setNumberOfPeaksWithLowSignalToNoise(const size_t n)
size_t m_submitted_individual_peaks
void setNumberOfIndividualPeaksWithLowCount(const size_t n)
void setNumberOfSubmittedSpectrumPeaks(const size_t n)
bool isIndividualPeakRejected() const
size_t m_function_parameters_number
number of function parameters
double getPeakPosition(size_t ipeak) const
size_t getNumberPeaks() const
std::vector< std::vector< double > > m_function_parameters_vector
PeakFitResult(size_t num_peaks, size_t num_params)
Holds all of the fitting information for a single spectrum.
double getParameterValue(size_t ipeak, size_t iparam) const
get the fitted value of a particular parameter
std::vector< double > m_costs
std::vector< std::vector< double > > m_function_errors_vector
fitted peak and background parameters' fitting error
size_t getNumberParameters() const
double getCost(size_t ipeak) const
void setRecord(size_t ipeak, const double cost, const double peak_position, const FitFunction &fit_functions)
set the peak fitting record/parameter for one peak
double getParameterError(size_t ipeak, size_t iparam) const
get the fitting error of a particular parameter
void setBadRecord(size_t ipeak, const double peak_position)
The peak postition should be negative and indicates what went wrong.
std::vector< double > m_fitted_peak_positions
Algorithms::PeakParameterHelper::EstimatePeakWidth m_peakWidthEstimateApproach
Flag for observing peak width: there are 3 states (1) no estimation (2) from 'observation' (3) calcul...
void calculateFittedPeaks(const std::vector< std::shared_ptr< FitPeaksAlgorithm::PeakFitResult > > &fit_results)
calculate peak+background for fitted
double calculateSignalToNoiseRatio(size_t iws, const std::pair< double, double > &range, const API::IBackgroundFunction_sptr &bkgd_function)
calculate signal-to-noise ratio in histogram range
API::MatrixWorkspace_const_sptr m_peakCenterWorkspace
void generateFittedParametersValueWorkspaces()
Generate output workspaces.
void setupParameterTableWorkspace(const API::ITableWorkspace_sptr &table_ws, const std::vector< std::string > ¶m_names, bool with_chi2)
Set up parameter table (parameter value or error)
std::vector< double > m_peakPosTolerances
tolerances for fitting peak positions
API::IPeakFunction_sptr m_peakFunction
Peak profile name.
bool fitBackground(const size_t &ws_index, const std::pair< double, double > &fit_window, const double &expected_peak_pos, const API::IBackgroundFunction_sptr &bkgd_func)
fit background
double fitFunctionSD(const API::IAlgorithm_sptr &fit, const API::IPeakFunction_sptr &peak_function, const API::IBackgroundFunction_sptr &bkgd_function, const API::MatrixWorkspace_sptr &dataws, size_t wsindex, const std::pair< double, double > &peak_range, const double &expected_peak_center, bool estimate_peak_width, bool estimate_background)
Methods to fit functions (general)
API::MatrixWorkspace_sptr m_outputPeakPositionWorkspace
output workspace for peak positions
API::MatrixWorkspace_sptr m_fittedPeakWS
matrix workspace contained calcalated peaks+background from fitted result it has same number of spect...
API::ITableWorkspace_const_sptr m_profileStartingValueTable
table workspace for profile parameters' starting value
std::string m_minimizer
Minimzer.
API::IBackgroundFunction_sptr m_linearBackgroundFunction
Linear background function for high background fitting.
bool m_constrainPeaksPosition
bool m_peakPosTolCase234
peak positon tolerance case b, c and d
std::map< std::string, std::string > validateInputs() override
Validate inputs.
double m_peakWidthPercentage
flag to estimate peak width from
API::IBackgroundFunction_sptr m_bkgdFunction
Background function.
size_t histRangeToDataPointCount(size_t iws, const std::pair< double, double > &range)
convert a histogram range to index boundaries
void checkPeakIndices(std::size_t const &, std::size_t const &)
double fitFunctionHighBackground(const API::IAlgorithm_sptr &fit, const std::pair< double, double > &fit_window, const size_t &ws_index, const double &expected_peak_center, bool observe_peak_shape, const API::IPeakFunction_sptr &peakfunction, const API::IBackgroundFunction_sptr &bkgdfunc)
fit a single peak with high background
void processInputPeakCenters()
peak centers
std::vector< std::shared_ptr< FitPeaksAlgorithm::PeakFitResult > > fitPeaks()
suites of method to fit peaks
double m_minPeakHeight
minimum peak height without background and it also serves as the criteria for observed peak parameter
std::string m_costFunction
Cost function.
void processInputFunctions()
process inputs for peak and background functions
void processInputPeakTolerance()
process inputs about fitted peak positions' tolerance
void writeFitResult(size_t wi, const std::vector< double > &expected_positions, const std::shared_ptr< FitPeaksAlgorithm::PeakFitResult > &fit_result)
Write result of peak fit per spectrum to output analysis workspaces.
void histRangeToIndexBounds(size_t iws, const std::pair< double, double > &range, size_t &left_index, size_t &right_index)
convert a histogram range to index boundaries
void exec() override
Main exec method.
void init() override
Init.
void logNoOffset(const size_t &priority, const std::string &msg)
std::size_t m_numSpectraToFit
total number of spectra to be fit
std::vector< std::string > m_peakParamNames
input peak parameters' names
double calculateSignalToSigmaRatio(const size_t &iws, const std::pair< double, double > &peakWindow, const API::IPeakFunction_sptr &peakFunction)
bool isObservablePeakProfile(const std::string &peakprofile)
check whether FitPeaks supports observation on a certain peak profile's parameters (width!...
void processInputFitRanges()
process inputs for peak fitting range
std::size_t m_numPeaksToFit
the number of peaks to fit in all spectra
std::size_t m_startWorkspaceIndex
start index
API::MatrixWorkspace_sptr createMatrixWorkspace(const std::vector< double > &vec_x, const std::vector< double > &vec_y, const std::vector< double > &vec_e)
Create a single spectrum workspace for fitting.
void generateOutputPeakPositionWS()
main method to create output workspaces
void generateCalculatedPeaksWS()
Generate workspace for calculated values.
double fitFunctionMD(API::IFunction_sptr fit_function, const API::MatrixWorkspace_sptr &dataws, const size_t wsindex, const std::pair< double, double > &vec_xmin, const std::pair< double, double > &vec_xmax)
double m_minPeakTotalCount
std::string getPeakHeightParameterName(const API::IPeakFunction_const_sptr &peak_function)
Get the parameter name for peak height (I or height or etc)
bool m_uniformPeakPositions
void fitSpectrumPeaks(size_t wi, const std::vector< double > &expected_peak_centers, const std::shared_ptr< FitPeaksAlgorithm::PeakFitResult > &fit_result, std::vector< std::vector< double > > &lastGoodPeakParameters, const std::shared_ptr< FitPeaksAlgorithm::PeakFitPreCheckResult > &pre_check_result)
fit peaks in a same spectrum
API::ITableWorkspace_sptr m_fittedParamTable
output analysis workspaces table workspace for fitted parameters
double m_minSignalToNoiseRatio
bool decideToEstimatePeakParams(const bool firstPeakInSpectrum, const API::IPeakFunction_sptr &peak_function)
Decide whether to estimate peak parameters.
void convertParametersNameToIndex()
Convert peak function's parameter names to parameter index for fast access.
void processOutputs(std::vector< std::shared_ptr< FitPeaksAlgorithm::PeakFitResult > > fit_result_vec)
Set the workspaces and etc to output properties.
bool m_highBackground
flag for high background
std::vector< double > m_initParamValues
input peak parameters' starting values corresponding to above peak parameter names
std::vector< double > m_peakCenters
Designed peak positions and tolerance.
double fitIndividualPeak(size_t wi, const API::IAlgorithm_sptr &fitter, const double expected_peak_center, const std::pair< double, double > &fitwindow, const bool estimate_peak_width, const API::IPeakFunction_sptr &peakfunction, const API::IBackgroundFunction_sptr &bkgdfunc, const std::shared_ptr< FitPeaksAlgorithm::PeakFitPreCheckResult > &pre_check_result)
Fit an individual peak.
std::vector< std::vector< double > > m_peakWindowVector
peak windows
std::function< std::pair< double, double >(std::size_t const &, std::size_t const &)> m_getPeakFitWindow
API::MatrixWorkspace_sptr m_inputMatrixWS
mandatory input and output workspaces
std::vector< size_t > m_initParamIndexes
input starting parameters' indexes in peak function
bool m_fitPeaksFromRight
Fit from right or left.
std::function< std::vector< double >(std::size_t const &)> m_getExpectedPeakPositions
bool m_rawPeaksTable
flag to show that the pamarameters in table are raw parameters or effective parameters
void processInputs()
process inputs (main and child algorithms)
void checkWorkspaceIndices(std::size_t const &)
Get the expected peak's position.
void checkPeakWindowEdgeOrder(double const &, double const &)
int m_fitIterations
Fit iterations.
double m_minSignalToSigmaRatio
double numberCounts(size_t iws)
sum up all counts in histogram
API::MatrixWorkspace_const_sptr m_peakWindowWorkspace
bool m_uniformProfileStartingValue
flag for profile startng value being uniform or not
API::ITableWorkspace_sptr m_fitErrorTable
table workspace for fitted parameters' fitting error. This is optional
void getRangeData(size_t iws, const std::pair< double, double > &range, std::vector< double > &vec_x, std::vector< double > &vec_y, std::vector< double > &vec_e)
get vector X, Y and E in a given range
std::size_t m_stopWorkspaceIndex
stop index (workspace index of the last spectrum included)
bool processSinglePeakFitResult(size_t wsindex, size_t peakindex, const double cost, const std::vector< double > &expected_peak_positions, const FitPeaksAlgorithm::FitFunction &fitfunction, const std::shared_ptr< FitPeaksAlgorithm::PeakFitResult > &fit_result)
Process the result from fitting a single peak.
Support for a property that holds an array of values.
Exception for when an item is not found in a collection.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
ListValidator is a validator that requires the value of a property to be one of a defined list of pos...
void debug(const std::string &msg)
Logs at debug level.
void notice(const std::string &msg)
Logs at notice level.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
StartsWithValidator is a validator that requires the value of a property to start with one of the str...
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< IBackgroundFunction > IBackgroundFunction_sptr
std::shared_ptr< IPeakFunction > IPeakFunction_sptr
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< const IPeakFunction > IPeakFunction_const_sptr
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< CompositeFunction > CompositeFunction_sptr
shared pointer to the composite function base class
std::string const OUTPUT_WKSP("OutputWorkspace")
std::string const INPUT_WKSP("InputWorkspace")
MANTID_ALGORITHMS_DLL size_t findXIndex(const vector_like &vecx, const double x, const size_t startindex=0)
Get an index of a value in a sorted vector.
MANTID_ALGORITHMS_DLL int estimatePeakParameters(const HistogramData::Histogram &histogram, const std::pair< size_t, size_t > &peak_window, const API::IPeakFunction_sptr &peakfunction, const API::IBackgroundFunction_sptr &bkgdfunction, bool observe_peak_width, const EstimatePeakWidth peakWidthEstimateApproach, const double peakWidthPercentage, const double minPeakHeight)
Estimate peak parameters by 'observation'.
Statistics getStatistics(const std::vector< TYPE > &data, const unsigned int flags=StatOptions::AllStats)
Return a statistics object for the given data set.
std::vector< double > getZscore(const std::vector< TYPE > &data)
Return the Z score values for a dataset.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
const std::string OUTPUT_WKSP("OutputWorkspace")
const std::string INPUT_WKSP("InputWorkspace")
Helper class which provides the Collimation Length for SANS instruments.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
API::IPeakFunction_sptr peakfunction
API::IBackgroundFunction_sptr bkgdfunction
@ Input
An input workspace.
@ Output
An output workspace.
Functor to accumulate a sum of squares.