Mantid
Loading...
Searching...
No Matches
FitPeaks.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11#include "MantidAPI/Axis.h"
20#include "MantidAPI/TableRow.h"
28#include "MantidHistogramData/EstimatePolynomial.h"
29#include "MantidHistogramData/Histogram.h"
30#include "MantidHistogramData/HistogramBuilder.h"
31#include "MantidHistogramData/HistogramIterator.h"
38
39#include "boost/algorithm/string.hpp"
40#include "boost/algorithm/string/trim.hpp"
41#include <limits>
42#include <utility>
43
44using namespace Mantid;
45using namespace Algorithms::PeakParameterHelper;
46using namespace Mantid::API;
47using namespace Mantid::DataObjects;
48using namespace Mantid::HistogramData;
49using namespace Mantid::Kernel;
50using namespace Mantid::Geometry;
51using Mantid::HistogramData::Histogram;
52using namespace std;
53
54namespace Mantid::Algorithms {
55
56namespace {
57namespace PropertyNames {
58const std::string INPUT_WKSP("InputWorkspace");
59const std::string OUTPUT_WKSP("OutputWorkspace");
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 COPY_LAST_GOOD_PEAK_PARAMS("CopyLastGoodPeakParameters");
82const std::string RESPECT_FIXED_PEAK_PARAMS("RespectFixedPeakParameters");
83const std::string OUTPUT_WKSP_MODEL("FittedPeaksWorkspace");
84const std::string OUTPUT_WKSP_PARAMS("OutputPeakParametersWorkspace");
85const std::string OUTPUT_WKSP_PARAM_ERRS("OutputParameterFitErrorsWorkspace");
86const std::string RAW_PARAMS("RawPeakParameters");
87const std::string PEAK_MIN_SIGNAL_TO_NOISE_RATIO("MinimumSignalToNoiseRatio");
88const std::string PEAK_MIN_TOTAL_COUNT("MinimumPeakTotalCount");
89const std::string PEAK_MIN_SIGNAL_TO_SIGMA_RATIO("MinimumSignalToSigmaRatio");
90} // namespace PropertyNames
91} // namespace
92
93namespace FitPeaksAlgorithm {
94
95//----------------------------------------------------------------------------------------------
97PeakFitResult::PeakFitResult(size_t num_peaks, size_t num_params) : m_function_parameters_number(num_params) {
98 // check input
99 if (num_peaks == 0 || num_params == 0)
100 throw std::runtime_error("No peak or no parameter error.");
101
102 //
103 m_fitted_peak_positions.resize(num_peaks, std::numeric_limits<double>::quiet_NaN());
104 m_costs.resize(num_peaks, DBL_MAX);
105 m_function_parameters_vector.resize(num_peaks);
106 m_function_errors_vector.resize(num_peaks);
107 for (size_t ipeak = 0; ipeak < num_peaks; ++ipeak) {
108 m_function_parameters_vector[ipeak].resize(num_params, std::numeric_limits<double>::quiet_NaN());
109 m_function_errors_vector[ipeak].resize(num_params, std::numeric_limits<double>::quiet_NaN());
110 }
111
112 return;
113}
114
115//----------------------------------------------------------------------------------------------
117
119
120//----------------------------------------------------------------------------------------------
127double PeakFitResult::getParameterError(size_t ipeak, size_t iparam) const {
128 return m_function_errors_vector[ipeak][iparam];
129}
130
131//----------------------------------------------------------------------------------------------
138double PeakFitResult::getParameterValue(size_t ipeak, size_t iparam) const {
139 return m_function_parameters_vector[ipeak][iparam];
140}
141
142//----------------------------------------------------------------------------------------------
143double PeakFitResult::getPeakPosition(size_t ipeak) const { return m_fitted_peak_positions[ipeak]; }
144
145//----------------------------------------------------------------------------------------------
146double PeakFitResult::getCost(size_t ipeak) const { return m_costs[ipeak]; }
147
148//----------------------------------------------------------------------------------------------
150void PeakFitResult::setRecord(size_t ipeak, const double cost, const double peak_position,
151 const FitFunction &fit_functions) {
152 // check input
153 if (ipeak >= m_costs.size())
154 throw std::runtime_error("Peak index is out of range.");
155
156 // set the values
157 m_costs[ipeak] = cost;
158
159 // set peak position
160 m_fitted_peak_positions[ipeak] = peak_position;
161
162 // transfer from peak function to vector
163 size_t peak_num_params = fit_functions.peakfunction->nParams();
164 for (size_t ipar = 0; ipar < peak_num_params; ++ipar) {
165 // peak function
166 m_function_parameters_vector[ipeak][ipar] = fit_functions.peakfunction->getParameter(ipar);
167 m_function_errors_vector[ipeak][ipar] = fit_functions.peakfunction->getError(ipar);
168 }
169 for (size_t ipar = 0; ipar < fit_functions.bkgdfunction->nParams(); ++ipar) {
170 // background function
171 m_function_parameters_vector[ipeak][ipar + peak_num_params] = fit_functions.bkgdfunction->getParameter(ipar);
172 m_function_errors_vector[ipeak][ipar + peak_num_params] = fit_functions.bkgdfunction->getError(ipar);
173 }
174}
175
176//----------------------------------------------------------------------------------------------
181void PeakFitResult::setBadRecord(size_t ipeak, const double peak_position) {
182 // check input
183 if (ipeak >= m_costs.size())
184 throw std::runtime_error("Peak index is out of range");
185 if (peak_position >= 0.)
186 throw std::runtime_error("Can only set negative postion for bad record");
187
188 // set the values
189 m_costs[ipeak] = DBL_MAX;
190
191 // set peak position
192 m_fitted_peak_positions[ipeak] = peak_position;
193
194 // transfer from peak function to vector
195 for (size_t ipar = 0; ipar < m_function_parameters_number; ++ipar) {
196 m_function_parameters_vector[ipeak][ipar] = 0.;
197 m_function_errors_vector[ipeak][ipar] = std::numeric_limits<double>::quiet_NaN();
198 }
199}
200
212
214
216
218
220
222
224
226
228 // the method should be used on an individual peak, not on a spectrum
229 assert(m_submitted_spectrum_peaks == 0);
230 assert(m_submitted_individual_peaks == 1);
231
232 // if a peak is rejected, it is rejected based on the very first check it fails
233 size_t individual_rejection_count = m_low_count_individual + m_not_enough_datapoints + m_low_snr;
234 assert(individual_rejection_count <= 1);
235
236 return individual_rejection_count == 1;
237}
238
241
242 // if no peaks were rejected by the pre-check, keep quiet
244 return "";
245
246 std::ostringstream os;
247 os << "Total number of peaks pre-checked before fitting: " << m_submitted_spectrum_peaks << "\n";
248 if (m_low_count_spectrum > 0)
249 os << m_low_count_spectrum << " peak(s) rejected: low signal count (whole spectrum).\n";
250 if (m_out_of_range > 0)
251 os << m_out_of_range << " peak(s) rejected: out of range.\n";
253 os << m_not_enough_datapoints << " peak(s) rejected: not enough X(Y) datapoints.\n";
255 os << m_low_count_individual << " peak(s) rejected: low signal count (individual peak).\n";
256 if (m_low_snr > 0)
257 os << m_low_snr << " peak(s) rejected: low signal-to-noise ratio.\n";
258
259 return os.str();
260}
261} // namespace FitPeaksAlgorithm
262
263//----------------------------------------------------------------------------------------------
265 : m_fitPeaksFromRight(true), m_fitIterations(50), m_numPeaksToFit(0), m_minPeakHeight(0.),
266 m_minSignalToNoiseRatio(0.), m_minPeakTotalCount(0.), m_peakPosTolCase234(false) {}
267
268//----------------------------------------------------------------------------------------------
273 "Name of the input workspace for peak fitting.");
276 "Name of the output workspace containing peak centers for "
277 "fitting offset."
278 "The output workspace is point data."
279 "Each workspace index corresponds to a spectrum. "
280 "Each X value ranges from 0 to N-1, where N is the number of "
281 "peaks to fit. "
282 "Each Y value is the peak position obtained by peak fitting. "
283 "Negative value is used for error signals. "
284 "-1 for data is zero; -2 for maximum value is smaller than "
285 "specified minimum value."
286 "and -3 for non-converged fitting.");
287
288 // properties about fitting range and criteria
289 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
290 mustBePositive->setLower(0);
291 declareProperty(PropertyNames::START_WKSP_INDEX, 0, mustBePositive, "Starting workspace index for fit");
293 PropertyNames::STOP_WKSP_INDEX, EMPTY_INT(),
294 "Last workspace index for fit is the smaller of this value and the workspace index of last spectrum.");
295 // properties about peak positions to fit
296 declareProperty(std::make_unique<ArrayProperty<double>>(PropertyNames::PEAK_CENTERS),
297 "List of peak centers to use as initial guess for fit.");
299 std::make_unique<WorkspaceProperty<MatrixWorkspace>>(PropertyNames::PEAK_CENTERS_WKSP, "", Direction::Input,
301 "MatrixWorkspace containing referent peak centers for each spectrum, defined at the same workspace indices.");
302
303 const std::string peakcentergrp("Peak Positions");
304 setPropertyGroup(PropertyNames::PEAK_CENTERS, peakcentergrp);
305 setPropertyGroup(PropertyNames::PEAK_CENTERS_WKSP, peakcentergrp);
306
307 // properties about peak profile
308 const std::vector<std::string> peakNames = FunctionFactory::Instance().getFunctionNames<API::IPeakFunction>();
309 declareProperty(PropertyNames::PEAK_FUNC, "Gaussian", std::make_shared<StringListValidator>(peakNames),
310 "Use of a BackToBackExponential profile is only reccomended if the "
311 "coeficients to calculate A and B are defined in the instrument "
312 "Parameters.xml file.");
313 const vector<string> bkgdtypes{"Flat", "Linear", "Quadratic"};
314 declareProperty(PropertyNames::BACK_FUNC, "Linear", std::make_shared<StringListValidator>(bkgdtypes),
315 "Type of Background.");
316
317 const std::string funcgroup("Function Types");
318 setPropertyGroup(PropertyNames::PEAK_FUNC, funcgroup);
319 setPropertyGroup(PropertyNames::BACK_FUNC, funcgroup);
320
321 // properties about peak range including fitting window and peak width
322 // (percentage)
323 declareProperty(std::make_unique<ArrayProperty<double>>(PropertyNames::FIT_WINDOW_LIST),
324 "List of boundaries of the peak fitting window corresponding to "
325 "PeakCenters.");
326
327 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(PropertyNames::FIT_WINDOW_WKSP, "",
329 "MatrixWorkspace containing peak windows for each peak center in each spectrum, defined at the same "
330 "workspace indices.");
331
332 auto min = std::make_shared<BoundedValidator<double>>();
333 min->setLower(1e-3);
334 // min->setUpper(1.); TODO make this a limit
335 declareProperty(PropertyNames::PEAK_WIDTH_PERCENT, EMPTY_DBL(), min,
336 "The estimated peak width as a "
337 "percentage of the d-spacing "
338 "of the center of the peak. Value must be less than 1.");
339
340 const std::string fitrangeegrp("Peak Range Setup");
341 setPropertyGroup(PropertyNames::PEAK_WIDTH_PERCENT, fitrangeegrp);
342 setPropertyGroup(PropertyNames::FIT_WINDOW_LIST, fitrangeegrp);
343 setPropertyGroup(PropertyNames::FIT_WINDOW_WKSP, fitrangeegrp);
344
345 // properties about peak parameters' names and value
346 declareProperty(std::make_unique<ArrayProperty<std::string>>(PropertyNames::PEAK_PARAM_NAMES),
347 "List of peak parameters' names");
348 declareProperty(std::make_unique<ArrayProperty<double>>(PropertyNames::PEAK_PARAM_VALUES),
349 "List of peak parameters' value");
350 declareProperty(std::make_unique<WorkspaceProperty<TableWorkspace>>(PropertyNames::PEAK_PARAM_TABLE, "",
352 "Name of the an optional workspace, whose each column "
353 "corresponds to given peak parameter names, "
354 "and each row corresponds to a subset of spectra.");
355
356 const std::string startvaluegrp("Starting Parameters Setup");
357 setPropertyGroup(PropertyNames::PEAK_PARAM_NAMES, startvaluegrp);
358 setPropertyGroup(PropertyNames::PEAK_PARAM_VALUES, startvaluegrp);
359 setPropertyGroup(PropertyNames::PEAK_PARAM_TABLE, startvaluegrp);
360
361 // optimization setup
362 declareProperty(PropertyNames::FIT_FROM_RIGHT, true,
363 "Flag for the order to fit peaks. If true, peaks are fitted "
364 "from rightmost;"
365 "Otherwise peaks are fitted from leftmost.");
366
367 const std::vector<std::string> minimizerOptions = API::FuncMinimizerFactory::Instance().getKeys();
368 declareProperty(PropertyNames::MINIMIZER, "Levenberg-Marquardt",
370 "Minimizer to use for fitting.");
371
372 const std::array<string, 2> costFuncOptions = {{"Least squares", "Rwp"}};
373 declareProperty(PropertyNames::COST_FUNC, "Least squares",
374 Kernel::IValidator_sptr(new Kernel::ListValidator<std::string>(costFuncOptions)), "Cost functions");
375
376 auto min_max_iter = std::make_shared<BoundedValidator<int>>();
377 min_max_iter->setLower(49);
378 declareProperty(PropertyNames::MAX_FIT_ITER, 50, min_max_iter, "Maximum number of function fitting iterations.");
379
380 const std::string optimizergrp("Optimization Setup");
381 setPropertyGroup(PropertyNames::MINIMIZER, optimizergrp);
382 setPropertyGroup(PropertyNames::COST_FUNC, optimizergrp);
383
384 // other helping information
385 std::ostringstream os;
386 os << "Deprecated property. Use " << PropertyNames::PEAK_MIN_SIGNAL_TO_NOISE_RATIO << " instead.";
387 declareProperty(PropertyNames::BACKGROUND_Z_SCORE, EMPTY_DBL(), os.str());
388
389 declareProperty(PropertyNames::HIGH_BACKGROUND, true,
390 "Flag whether the input data has high background compared to peak heights.");
391
392 declareProperty(std::make_unique<ArrayProperty<double>>(PropertyNames::POSITION_TOL),
393 "List of tolerance on fitted peak positions against given peak positions."
394 "If there is only one value given, then ");
395
396 declareProperty(PropertyNames::PEAK_MIN_HEIGHT, 0.,
397 "Used for validating peaks before and after fitting. If a peak's observed/estimated or "
398 "fitted height is under this value, the peak will be marked as error.");
399
400 declareProperty(PropertyNames::CONSTRAIN_PEAK_POS, true,
401 "If true peak position will be constrained by estimated positions "
402 "(highest Y value position) and "
403 "the peak width either estimted by observation or calculate.");
404
405 declareProperty(PropertyNames::COPY_LAST_GOOD_PEAK_PARAMS, true,
406 "If true, initial peak parameters (with the exception of peak centre) "
407 "may be copied from the last successfully fit peak in the spectra.");
408
409 declareProperty(PropertyNames::RESPECT_FIXED_PEAK_PARAMS, false,
410 "If true, peak function parameters that are marked as fixed "
411 "(e.g. parameters calculated from the instrument geometry, such as A and B "
412 "of a BackToBackExponential) remain fixed during fitting. "
413 "If false (default), such parameters are unfixed so they can be refined.");
414
415 // additional output for reviewing
416 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(PropertyNames::OUTPUT_WKSP_MODEL, "",
418 "Name of the output matrix workspace with fitted peak. "
419 "This output workspace has the same dimension as the input workspace."
420 "The Y values belonged to peaks to fit are replaced by fitted value. "
421 "Values of estimated background are used if peak fails to be fit.");
422
423 declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>(PropertyNames::OUTPUT_WKSP_PARAMS, "",
425 "Name of table workspace containing all fitted peak parameters.");
426
427 // Optional output table workspace for each individual parameter's fitting
428 // error
429 declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>(PropertyNames::OUTPUT_WKSP_PARAM_ERRS, "",
431 "Name of workspace containing all fitted peak parameters' fitting error."
432 "It must be used along with FittedPeaksWorkspace and RawPeakParameters "
433 "(True)");
434
435 declareProperty(PropertyNames::RAW_PARAMS, true,
436 "false generates table with effective centre/width/height "
437 "parameters. true generates a table with peak function "
438 "parameters");
439
441 PropertyNames::PEAK_MIN_SIGNAL_TO_NOISE_RATIO, 0.,
442 "Used for validating peaks before fitting. If the signal-to-noise ratio is under this value, "
443 "the peak will be marked as error. This does not apply to peaks for which the noise cannot be estimated.");
444
445 declareProperty(PropertyNames::PEAK_MIN_TOTAL_COUNT, EMPTY_DBL(),
446 "Used for validating peaks before fitting. If the total peak window Y-value count "
447 "is under this value, the peak will be excluded from fitting and calibration.");
448
449 declareProperty(PropertyNames::PEAK_MIN_SIGNAL_TO_SIGMA_RATIO, 0.,
450 "Used for validating peaks after fitting. If the signal-to-sigma ratio is under this value, "
451 "the peak will be excluded from fitting and calibration.");
452
453 const std::string addoutgrp("Analysis");
454 setPropertyGroup(PropertyNames::OUTPUT_WKSP_PARAMS, addoutgrp);
455 setPropertyGroup(PropertyNames::OUTPUT_WKSP_MODEL, addoutgrp);
456 setPropertyGroup(PropertyNames::OUTPUT_WKSP_PARAM_ERRS, addoutgrp);
457 setPropertyGroup(PropertyNames::RAW_PARAMS, addoutgrp);
458}
459
460//----------------------------------------------------------------------------------------------
463std::map<std::string, std::string> FitPeaks::validateInputs() {
464 map<std::string, std::string> issues;
465
466 // check that min/max spectra indices make sense - only matters if both are specified
467 if (!(isDefault(PropertyNames::START_WKSP_INDEX) && isDefault(PropertyNames::STOP_WKSP_INDEX))) {
468 const int startIndex = getProperty(PropertyNames::START_WKSP_INDEX);
469 const int stopIndex = getProperty(PropertyNames::STOP_WKSP_INDEX);
470 if (startIndex > stopIndex) {
471 const std::string msg =
472 PropertyNames::START_WKSP_INDEX + " must be less than or equal to " + PropertyNames::STOP_WKSP_INDEX;
473 issues[PropertyNames::START_WKSP_INDEX] = msg;
474 issues[PropertyNames::STOP_WKSP_INDEX] = msg;
475 }
476 }
477
478 // check that the peak parameters are in parallel properties
479 bool haveCommonPeakParameters(false);
480 std::vector<string> suppliedParameterNames = getProperty(PropertyNames::PEAK_PARAM_NAMES);
481 std::vector<double> peakParamValues = getProperty(PropertyNames::PEAK_PARAM_VALUES);
482 if ((!suppliedParameterNames.empty()) || (!peakParamValues.empty())) {
483 haveCommonPeakParameters = true;
484 if (suppliedParameterNames.size() != peakParamValues.size()) {
485 issues[PropertyNames::PEAK_PARAM_NAMES] = "must have same number of values as PeakParameterValues";
486 issues[PropertyNames::PEAK_PARAM_VALUES] = "must have same number of values as PeakParameterNames";
487 }
488 }
489
490 // get the information out of the table
491 std::string partablename = getPropertyValue(PropertyNames::PEAK_PARAM_TABLE);
492 if (!partablename.empty()) {
493 if (haveCommonPeakParameters) {
494 const std::string msg = "Parameter value table and initial parameter "
495 "name/value vectors cannot be given "
496 "simultanenously.";
497 issues[PropertyNames::PEAK_PARAM_TABLE] = msg;
498 issues[PropertyNames::PEAK_PARAM_NAMES] = msg;
499 issues[PropertyNames::PEAK_PARAM_VALUES] = msg;
500 } else {
501 m_profileStartingValueTable = getProperty(PropertyNames::PEAK_PARAM_TABLE);
502 suppliedParameterNames = m_profileStartingValueTable->getColumnNames();
503 }
504 }
505
506 // check that the suggested peak parameter names exist in the peak function
507 if (!suppliedParameterNames.empty()) {
508 std::string peakfunctiontype = getPropertyValue(PropertyNames::PEAK_FUNC);
510 std::dynamic_pointer_cast<IPeakFunction>(API::FunctionFactory::Instance().createFunction(peakfunctiontype));
511
512 // put the names in a vector
513 std::vector<string> functionParameterNames;
514 for (size_t i = 0; i < m_peakFunction->nParams(); ++i)
515 functionParameterNames.emplace_back(m_peakFunction->parameterName(i));
516 // check that the supplied names are in the function
517 // it is acceptable to be missing parameters
518 const bool failed = std::any_of(suppliedParameterNames.cbegin(), suppliedParameterNames.cend(),
519 [&functionParameterNames](const auto &parName) {
520 return std::find(functionParameterNames.begin(), functionParameterNames.end(),
521 parName) == functionParameterNames.end();
522 });
523 if (failed) {
524 std::string msg = "Specified invalid parameter for peak function";
525 if (haveCommonPeakParameters)
526 issues[PropertyNames::PEAK_PARAM_NAMES] = msg;
527 else
528 issues[PropertyNames::PEAK_PARAM_TABLE] = msg;
529 }
530 }
531
532 // check inputs for uncertainty (fitting error)
533 const std::string error_table_name = getPropertyValue(PropertyNames::OUTPUT_WKSP_PARAM_ERRS);
534 if (!error_table_name.empty()) {
535 const bool use_raw_params = getProperty(PropertyNames::RAW_PARAMS);
536 if (!use_raw_params) {
537 issues[PropertyNames::OUTPUT_WKSP_PARAM_ERRS] = "Cannot be used with " + PropertyNames::RAW_PARAMS + "=False";
538 issues[PropertyNames::RAW_PARAMS] =
539 "Cannot be False with " + PropertyNames::OUTPUT_WKSP_PARAM_ERRS + " specified";
540 }
541 }
542
543 return issues;
544}
545
546//----------------------------------------------------------------------------------------------
548 // process inputs
550
551 // create output workspace: fitted peak positions
553
554 // create output workspace: fitted peaks' parameters values
556
557 // create output workspace: calculated from fitted peak and background
559
560 // fit peaks
561 auto fit_results = fitPeaks();
562
563 // set the output workspaces to properites
564 processOutputs(fit_results);
565}
566
567//----------------------------------------------------------------------------------------------
569 // input workspaces
571
572 if (m_inputMatrixWS->getAxis(0)->unit()->unitID() == "dSpacing")
573 m_inputIsDSpace = true;
574 else
575 m_inputIsDSpace = false;
576
577 // spectra to fit
578 int start_wi = getProperty(PropertyNames::START_WKSP_INDEX);
579 m_startWorkspaceIndex = static_cast<size_t>(start_wi);
580
581 // last spectrum's workspace index, which is included
582 int stop_wi = getProperty(PropertyNames::STOP_WKSP_INDEX);
583 if (isEmpty(stop_wi))
584 m_stopWorkspaceIndex = m_inputMatrixWS->getNumberHistograms() - 1;
585 else {
586 m_stopWorkspaceIndex = static_cast<size_t>(stop_wi);
587 if (m_stopWorkspaceIndex > m_inputMatrixWS->getNumberHistograms() - 1)
588 m_stopWorkspaceIndex = m_inputMatrixWS->getNumberHistograms() - 1;
589 }
590
591 // total number of spectra to be fit
593
594 // optimizer, cost function and fitting scheme
595 m_minimizer = getPropertyValue(PropertyNames::MINIMIZER);
596 m_costFunction = getPropertyValue(PropertyNames::COST_FUNC);
597 m_fitPeaksFromRight = getProperty(PropertyNames::FIT_FROM_RIGHT);
598 m_constrainPeaksPosition = getProperty(PropertyNames::CONSTRAIN_PEAK_POS);
599 m_fitIterations = getProperty(PropertyNames::MAX_FIT_ITER);
600 m_copyLastGoodPeakParameters = getProperty(PropertyNames::COPY_LAST_GOOD_PEAK_PARAMS);
601 m_respectFixedPeakParameters = getProperty(PropertyNames::RESPECT_FIXED_PEAK_PARAMS);
602
603 // Peak centers, tolerance and fitting range
605 // check
606 if (m_numPeaksToFit == 0)
607 throw std::runtime_error("number of peaks to fit is zero.");
608 // about how to estimate the peak width
609 m_peakWidthPercentage = getProperty(PropertyNames::PEAK_WIDTH_PERCENT);
612 if (m_peakWidthPercentage >= 1.) // TODO
613 throw std::runtime_error("PeakWidthPercent must be less than 1");
614 g_log.debug() << "peak width/value = " << m_peakWidthPercentage << "\n";
615
616 // set up background
617 m_highBackground = getProperty(PropertyNames::HIGH_BACKGROUND);
618 double temp = getProperty(PropertyNames::BACKGROUND_Z_SCORE);
619 if (!isEmpty(temp)) {
620 std::ostringstream os;
621 os << "FitPeaks property \"" << PropertyNames::BACKGROUND_Z_SCORE << "\" is deprecated and will be ignored."
622 << "\n";
623 logNoOffset(4 /*warning*/, os.str());
624 }
625
626 // Set up peak and background functions
628
629 // about peak width and other peak parameter estimating method
630 if (m_peakWidthPercentage > 0.)
631 m_peakWidthEstimateApproach = EstimatePeakWidth::InstrumentResolution;
632 else if (isObservablePeakProfile((m_peakFunction->name())))
633 m_peakWidthEstimateApproach = EstimatePeakWidth::Observation;
634 else
635 m_peakWidthEstimateApproach = EstimatePeakWidth::NoEstimation;
636 // m_peakWidthEstimateApproach = EstimatePeakWidth::NoEstimation;
637 g_log.debug() << "Process inputs [3] peak type: " << m_peakFunction->name()
638 << ", background type: " << m_bkgdFunction->name() << "\n";
639
642
643 return;
644}
645
646//----------------------------------------------------------------------------------------------
650 // peak functions
651 std::string peakfunctiontype = getPropertyValue(PropertyNames::PEAK_FUNC);
653 std::dynamic_pointer_cast<IPeakFunction>(API::FunctionFactory::Instance().createFunction(peakfunctiontype));
654
655 // background functions
656 std::string bkgdfunctiontype = getPropertyValue(PropertyNames::BACK_FUNC);
657 std::string bkgdname;
658 if (bkgdfunctiontype == "Linear")
659 bkgdname = "LinearBackground";
660 else if (bkgdfunctiontype == "Flat") {
661 g_log.warning("There may be problems with Flat background");
662 bkgdname = "FlatBackground";
663 } else
664 bkgdname = bkgdfunctiontype;
666 std::dynamic_pointer_cast<IBackgroundFunction>(API::FunctionFactory::Instance().createFunction(bkgdname));
668 m_linearBackgroundFunction = std::dynamic_pointer_cast<IBackgroundFunction>(
669 API::FunctionFactory::Instance().createFunction("LinearBackground"));
670 else
672
673 // TODO check that both parameter names and values exist
674 // input peak parameters
675 std::string partablename = getPropertyValue(PropertyNames::PEAK_PARAM_TABLE);
676 m_peakParamNames = getProperty(PropertyNames::PEAK_PARAM_NAMES);
677
679 if (partablename.empty() && (!m_peakParamNames.empty())) {
680 // use uniform starting value of peak parameters
681 m_initParamValues = getProperty(PropertyNames::PEAK_PARAM_VALUES);
682 // convert the parameter name in string to parameter name in integer index
684 // m_uniformProfileStartingValue = true;
685 } else if ((!partablename.empty()) && m_peakParamNames.empty()) {
686 // use non-uniform starting value of peak parameters
688 } else if (peakfunctiontype != "Gaussian") {
689 // user specifies nothing
690 g_log.warning("Neither parameter value table nor initial "
691 "parameter name/value vectors is specified. Fitting might "
692 "not be reliable for peak profile other than Gaussian");
693 }
694
695 return;
696}
697
698//----------------------------------------------------------------------------------------------
703 // get peak fit window
704 std::vector<double> peakwindow = getProperty(PropertyNames::FIT_WINDOW_LIST);
705 std::string peakwindowname = getPropertyValue(PropertyNames::FIT_WINDOW_WKSP);
706 API::MatrixWorkspace_const_sptr peakwindowws = getProperty(PropertyNames::FIT_WINDOW_WKSP);
707
708 // in most case, calculate window by instrument resolution is False
709
710 if ((!peakwindow.empty()) && peakwindowname.empty()) {
711 // Peak windows are uniform among spectra: use vector for peak windows
712
713 // check peak positions
715 throw std::invalid_argument(
716 "Specifying peak windows with a list requires also specifying peak positions with a list.");
717 // check size
718 if (peakwindow.size() != m_numPeaksToFit * 2)
719 throw std::invalid_argument("Peak window vector must be twice as large as number of peaks.");
720
721 // set up window to m_peakWindowVector
723 for (size_t i = 0; i < m_numPeaksToFit; ++i) {
724 std::vector<double> peakranges(2);
725 peakranges[0] = peakwindow[i * 2];
726 peakranges[1] = peakwindow[i * 2 + 1];
727 // check peak window (range) against peak centers
728 if ((peakranges[0] < m_peakCenters[i]) && (m_peakCenters[i] < peakranges[1])) {
729 // pass check: set
730 m_peakWindowVector[i] = peakranges;
731 } else {
732 // failed
733 std::stringstream errss;
734 errss << "Peak " << i << ": user specifies an invalid range and peak center against " << peakranges[0] << " < "
735 << m_peakCenters[i] << " < " << peakranges[1];
736 throw std::invalid_argument(errss.str());
737 }
738 } // END-FOR
739 m_getPeakFitWindow = [this](std::size_t wi, std::size_t ipeak) -> std::pair<double, double> {
740 this->checkWorkspaceIndices(wi);
741 this->checkPeakIndices(wi, ipeak);
742 double left = this->m_peakWindowVector[ipeak][0];
743 double right = this->m_peakWindowVector[ipeak][1];
744 this->checkPeakWindowEdgeOrder(left, right);
745 return std::make_pair(left, right);
746 };
747 // END if list peak windows
748 } else if (peakwindow.empty() && peakwindowws != nullptr) {
749 // use matrix workspace for non-uniform peak windows
750 m_peakWindowWorkspace = getProperty(PropertyNames::FIT_WINDOW_WKSP);
751
752 // check each spectrum whether the window is defined with the correct size
753 for (std::size_t wi = m_startWorkspaceIndex; wi <= m_stopWorkspaceIndex; wi++) {
754 const auto &peakWindowX = m_peakWindowWorkspace->x(wi);
755 const auto &peakCenterX = m_peakCenterWorkspace->x(wi);
756 if (peakWindowX.empty()) {
757 std::stringstream errss;
758 errss << "Peak window required at workspace index " << wi << " "
759 << "which is undefined in the peak window workspace. "
760 << "Ensure workspace indices correspond in peak window workspace and input workspace "
761 << "when using start and stop indices.";
762 throw std::invalid_argument(errss.str());
763 }
764 // check size
765 if (peakWindowX.size() % 2 != 0) {
766 throw std::invalid_argument("The peak window vector must be even, with two edges for each peak center.");
767 }
768 if (peakWindowX.size() != peakCenterX.size() * 2) {
769 std::stringstream errss;
770 errss << "Peak window workspace index " << wi << " has incompatible number of fit windows "
771 << peakWindowX.size() / 2 << " with the number of peaks " << peakCenterX.size() << " to fit.";
772 throw std::invalid_argument(errss.str());
773 }
774
775 for (size_t ipeak = 0; ipeak < peakCenterX.size(); ++ipeak) {
776 double left_w_bound = peakWindowX[ipeak * 2];
777 double right_w_bound = peakWindowX[ipeak * 2 + 1];
778 double center = peakCenterX[ipeak];
779
780 if (!(left_w_bound < center && center < right_w_bound)) {
781 std::stringstream errss;
782 errss << "Workspace index " << wi << " has incompatible peak window "
783 << "(" << left_w_bound << ", " << right_w_bound << ") "
784 << "with " << ipeak << "-th expected peak's center " << center;
785 throw std::runtime_error(errss.str());
786 }
787 }
788 }
789 m_getPeakFitWindow = [this](std::size_t wi, std::size_t ipeak) -> std::pair<double, double> {
790 this->checkWorkspaceIndices(wi);
791 this->checkPeakIndices(wi, ipeak);
792 double left = m_peakWindowWorkspace->x(wi)[ipeak * 2];
793 double right = m_peakWindowWorkspace->x(wi)[ipeak * 2 + 1];
794 this->checkPeakWindowEdgeOrder(left, right);
795 return std::make_pair(left, right);
796 };
797 // END if workspace peak windows
798 } else if (peakwindow.empty()) {
799 // no peak window is defined, then the peak window will be estimated by
800 // delta(D)/D
802 // m_peakWindowMethod = PeakWindowMethod::TOLERANCE;
803 // m_calculateWindowInstrument = true;
804 m_getPeakFitWindow = [this](std::size_t wi, std::size_t ipeak) -> std::pair<double, double> {
805 this->checkWorkspaceIndices(wi);
806 this->checkPeakIndices(wi, ipeak);
807 // calcualte peak window by delta(d)/d
808 double peak_pos = this->m_getExpectedPeakPositions(wi)[ipeak];
809 // calcalate expected peak width
810 double estimate_peak_width = peak_pos * m_peakWidthPercentage;
811 // using the NUMBER THREE to estimate the peak window
812 double THREE = 3.0;
813 double left = peak_pos - estimate_peak_width * THREE;
814 double right = peak_pos + estimate_peak_width * THREE;
815 this->checkPeakWindowEdgeOrder(left, right);
816 return std::make_pair(left, right);
817 };
818 } else {
819 throw std::invalid_argument("Without definition of peak window, the "
820 "input workspace must be in unit of dSpacing "
821 "and Delta(D)/D must be given!");
822 }
823 } else {
824 // non-supported situation
825 throw std::invalid_argument("One and only one of peak window array and "
826 "peak window workspace can be specified.");
827 }
828
829 return;
830}
831
832//----------------------------------------------------------------------------------------------
842 // peak centers
843 m_peakCenters = getProperty(PropertyNames::PEAK_CENTERS);
844 API::MatrixWorkspace_const_sptr peakcenterws = getProperty(PropertyNames::PEAK_CENTERS_WKSP);
845 if (!peakcenterws)
846 g_log.notice("Peak centers are not specified by peak center workspace");
847
848 std::string peakpswsname = getPropertyValue(PropertyNames::PEAK_CENTERS_WKSP);
849 if ((!m_peakCenters.empty()) && peakcenterws == nullptr) {
850 // peak positions are uniform among all spectra
852 // number of peaks to fit!
854 m_getExpectedPeakPositions = [this](std::size_t wi) -> std::vector<double> {
855 this->checkWorkspaceIndices(wi);
856 return this->m_peakCenters;
857 };
858 } else if (m_peakCenters.empty() && peakcenterws != nullptr) {
859 // peak positions can be different among spectra
861 m_peakCenterWorkspace = getProperty(PropertyNames::PEAK_CENTERS_WKSP);
862 // number of peaks to fit must correspond to largest number of reference peaks
863 m_numPeaksToFit = 0;
864 g_log.debug() << "Input peak center workspace: " << m_peakCenterWorkspace->x(0).size() << ", "
865 << m_peakCenterWorkspace->y(0).size() << "\n";
866 for (std::size_t wi = m_startWorkspaceIndex; wi <= m_stopWorkspaceIndex; wi++) {
867 if (m_peakCenterWorkspace->x(wi).empty()) {
868 std::stringstream errss;
869 errss << "Fit peaks was asked to fit from workspace index " << m_startWorkspaceIndex << " "
870 << "until workspace index " << m_stopWorkspaceIndex << ". "
871 << "However, the peak center workspace does not have values defined "
872 << "at workspace index " << wi << ". "
873 << "Make sure the workspace indices between input and peak center workspaces correspond.";
874 g_log.error() << errss.str();
875 throw std::invalid_argument(errss.str());
876 }
877 // the number of peaks to try to fit should be the max number of peaks across spectra
878 m_numPeaksToFit = std::max(m_numPeaksToFit, m_peakCenterWorkspace->x(wi).size());
879 }
880 m_getExpectedPeakPositions = [this](std::size_t wi) -> std::vector<double> {
881 this->checkWorkspaceIndices(wi);
882 return this->m_peakCenterWorkspace->x(wi).rawData();
883 };
884 } else {
885 std::stringstream errss;
886 errss << "One and only one in 'PeakCenters' (vector) and "
887 "'PeakCentersWorkspace' shall be given. "
888 << "'PeakCenters' has size " << m_peakCenters.size() << ", and name of peak center workspace "
889 << "is " << peakpswsname;
890 throw std::invalid_argument(errss.str());
891 }
892
893 return;
894}
895
896//----------------------------------------------------------------------------------------------
903 // check code integrity
904 if (m_numPeaksToFit == 0)
905 throw std::runtime_error("ProcessInputPeakTolerance() must be called after "
906 "ProcessInputPeakCenters()");
907
908 // peak tolerance
909 m_peakPosTolerances = getProperty(PropertyNames::POSITION_TOL);
910
911 if (m_peakPosTolerances.empty()) {
912 // case 2, 3, 4
913 m_peakPosTolerances.clear();
914 m_peakPosTolCase234 = true;
915 } else if (m_peakPosTolerances.size() == 1) {
916 // only 1 uniform peak position tolerance is defined: expand to all peaks
917 double peak_tol = m_peakPosTolerances[0];
918 m_peakPosTolerances.resize(m_numPeaksToFit, peak_tol);
919 } else if (m_peakPosTolerances.size() != m_numPeaksToFit) {
920 // not uniform but number of peaks does not match
921 g_log.error() << "number of peak position tolerance " << m_peakPosTolerances.size()
922 << " is not same as number of peaks " << m_numPeaksToFit << "\n";
923 throw std::runtime_error("Number of peak position tolerances and number of "
924 "peaks to fit are inconsistent.");
925 }
926
927 // set the minimum peak height to 0 (default value) if not specified or invalid
928 m_minPeakHeight = getProperty(PropertyNames::PEAK_MIN_HEIGHT);
930 m_minPeakHeight = 0.;
931
932 // PEAK_MIN_HEIGHT used to function as both "peak height" and "total count" checker.
933 // Now the "total count" is checked by PEAK_MIN_TOTAL_COUNT, so set it accordingly.
934 m_minPeakTotalCount = getProperty(PropertyNames::PEAK_MIN_TOTAL_COUNT);
937 else {
938 // set the minimum peak total count to 0 if not specified or invalid
941 }
942
943 // set the signal-to-noise threshold to zero (default value) if not specified or invalid
944 m_minSignalToNoiseRatio = getProperty(PropertyNames::PEAK_MIN_SIGNAL_TO_NOISE_RATIO);
947
948 // set the signal-to-sigma threshold to zero (default value) if not specified or invalid
949 m_minSignalToSigmaRatio = getProperty(PropertyNames::PEAK_MIN_SIGNAL_TO_SIGMA_RATIO);
952}
953
954//----------------------------------------------------------------------------------------------
961 // get a map for peak profile parameter name and parameter index
962 std::map<std::string, size_t> parname_index_map;
963 for (size_t iparam = 0; iparam < m_peakFunction->nParams(); ++iparam)
964 parname_index_map.insert(std::make_pair(m_peakFunction->parameterName(iparam), iparam));
965
966 // define peak parameter names (class variable) if using table
969
970 // map the input parameter names to parameter indexes
971 for (const auto &paramName : m_peakParamNames) {
972 auto locator = parname_index_map.find(paramName);
973 if (locator != parname_index_map.end()) {
974 m_initParamIndexes.emplace_back(locator->second);
975 } else {
976 // a parameter name that is not defined in the peak profile function. An
977 // out-of-range index is thus set to this
978 g_log.warning() << "Given peak parameter " << paramName
979 << " is not an allowed parameter of peak "
980 "function "
981 << m_peakFunction->name() << "\n";
982 m_initParamIndexes.emplace_back(m_peakFunction->nParams() * 10);
983 }
984 }
985
986 return;
987}
988
989//----------------------------------------------------------------------------------------------
992std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> FitPeaks::fitPeaks() {
993 API::Progress prog(this, 0., 1., m_numPeaksToFit - 1);
994
997 std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> fit_result_vector(m_numSpectraToFit);
998
999 const int nThreads = FrameworkManager::Instance().getNumOMPThreads();
1000 size_t chunkSize = m_numSpectraToFit / nThreads;
1001
1002 std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> pre_check_result =
1003 std::make_shared<FitPeaksAlgorithm::PeakFitPreCheckResult>();
1004
1005 PRAGMA_OMP(parallel for schedule(dynamic, 1) )
1006 for (int ithread = 0; ithread < nThreads; ithread++) {
1008 auto iws_begin = m_startWorkspaceIndex + chunkSize * static_cast<size_t>(ithread);
1009 auto iws_end = (ithread == nThreads - 1) ? m_stopWorkspaceIndex + 1 : iws_begin + chunkSize;
1010
1011 // vector to store fit params for last good fit to each peak
1012 std::vector<std::vector<double>> lastGoodPeakParameters(m_numPeaksToFit,
1013 std::vector<double>(m_peakFunction->nParams(), 0.0));
1014 // track which spectrum index last successfully fitted each peak
1015 std::vector<size_t> lastGoodPeakSpectra(m_numPeaksToFit, 0);
1016
1017 for (auto wi = iws_begin; wi < iws_end; ++wi) {
1018 // peaks to fit
1019 std::vector<double> expected_peak_centers = m_getExpectedPeakPositions(static_cast<size_t>(wi));
1020
1021 // initialize output for this
1022 size_t numfuncparams = m_peakFunction->nParams() + m_bkgdFunction->nParams();
1023 std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> fit_result =
1024 std::make_shared<FitPeaksAlgorithm::PeakFitResult>(m_numPeaksToFit, numfuncparams);
1025
1026 std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> spectrum_pre_check_result =
1027 std::make_shared<FitPeaksAlgorithm::PeakFitPreCheckResult>();
1028
1029 fitSpectrumPeaks(static_cast<size_t>(wi), expected_peak_centers, fit_result, lastGoodPeakParameters,
1030 lastGoodPeakSpectra, spectrum_pre_check_result);
1031
1032 PARALLEL_CRITICAL(FindPeaks_WriteOutput) {
1033 writeFitResult(static_cast<size_t>(wi), expected_peak_centers, fit_result);
1034 fit_result_vector[wi - m_startWorkspaceIndex] = fit_result;
1035 *pre_check_result += *spectrum_pre_check_result;
1036 }
1037 prog.report();
1038 }
1040 }
1042 logNoOffset(5 /*notice*/, pre_check_result->getReport());
1043 return fit_result_vector;
1044}
1045
1046namespace {
1047// Forward declarations
1048bool estimateBackgroundParameters(const Histogram &histogram, const std::pair<size_t, size_t> &peak_window,
1049 const API::IBackgroundFunction_sptr &bkgd_function);
1050void reduceByBackground(const API::IBackgroundFunction_sptr &bkgd_func, const std::vector<double> &vec_x,
1051 std::vector<double> &vec_y);
1052template <typename vector_like>
1053void rangeToIndexBounds(const vector_like &vecx, const double range_left, const double range_right, size_t &left_index,
1054 size_t &right_index);
1055
1057std::vector<std::string> supported_peak_profiles{"Gaussian", "Lorentzian", "PseudoVoigt", "Voigt",
1058 "BackToBackExponential"};
1059
1060//----------------------------------------------------------------------------------------------
1065double estimateBackgroundNoise(const std::vector<double> &vec_y) {
1066 // peak window must have a certain minimum number of data points necessary to do the statistics
1067 size_t half_number_of_bkg_datapoints{5};
1068 if (vec_y.size() < 2 * half_number_of_bkg_datapoints + 3 /*a magic number*/)
1069 return DBL_MIN; // can't estimate the noise
1070
1071 // the specified number of left-most and right-most data points in the peak window are assumed to represent
1072 // background. Combine these data points into a single vector
1073 std::vector<double> vec_bkg;
1074 vec_bkg.resize(2 * half_number_of_bkg_datapoints);
1075 std::copy(vec_y.begin(), vec_y.begin() + half_number_of_bkg_datapoints, vec_bkg.begin());
1076 std::copy(vec_y.end() - half_number_of_bkg_datapoints, vec_y.end(), vec_bkg.begin() + half_number_of_bkg_datapoints);
1077
1078 // estimate the noise as the standard deviation of the combined background vector, but without outliers
1079 std::vector<double> zscore_vec = Kernel::getZscore(vec_bkg);
1080 std::vector<double> vec_bkg_no_outliers;
1081 vec_bkg_no_outliers.resize(vec_bkg.size());
1082 double zscore_crit = 3.; // using three-sigma rule
1083 for (size_t ii = 0; ii < vec_bkg.size(); ii++) {
1084 if (zscore_vec[ii] <= zscore_crit)
1085 vec_bkg_no_outliers.push_back(vec_bkg[ii]);
1086 }
1087
1088 if (vec_bkg_no_outliers.size() < half_number_of_bkg_datapoints)
1089 return DBL_MIN; // can't estimate the noise
1090
1091 auto intensityStatistics = Kernel::getStatistics(vec_bkg_no_outliers, StatOptions::CorrectedStdDev);
1092 return intensityStatistics.standard_deviation;
1093}
1094
1095//----------------------------------------------------------------------------------------------
1103template <typename vector_like>
1104void rangeToIndexBounds(const vector_like &elems, const double range_left, const double range_right, size_t &left_index,
1105 size_t &right_index) {
1106 const auto left_iter = std::lower_bound(elems.cbegin(), elems.cend(), range_left);
1107 const auto right_iter = std::upper_bound(elems.cbegin(), elems.cend(), range_right);
1108
1109 left_index = std::distance(elems.cbegin(), left_iter);
1110 right_index = std::distance(elems.cbegin(), right_iter);
1111 right_index = std::min(right_index, elems.size() - 1);
1112}
1113
1114//----------------------------------------------------------------------------------------------
1120void reduceByBackground(const API::IBackgroundFunction_sptr &bkgd_func, const std::vector<double> &vec_x,
1121 std::vector<double> &vec_y) {
1122 // calculate the background
1123 FunctionDomain1DVector vectorx(vec_x.begin(), vec_x.end());
1124 FunctionValues vector_bkgd(vectorx);
1125 bkgd_func->function(vectorx, vector_bkgd);
1126
1127 // subtract the background from the supplied data
1128 for (size_t i = 0; i < vec_y.size(); ++i) {
1129 (vec_y)[i] -= vector_bkgd[i];
1130 // Note, E is not changed here
1131 }
1132}
1133
1134//----------------------------------------------------------------------------------------------
1138class LoggingOffsetSentry {
1139public:
1140 LoggingOffsetSentry(Algorithm *const alg) : m_alg(alg) {
1143 }
1144 ~LoggingOffsetSentry() { m_alg->setLoggingOffset(m_loggingOffset); }
1145
1146private:
1149};
1150} // namespace
1151
1152//----------------------------------------------------------------------------------------------
1155void FitPeaks::fitSpectrumPeaks(size_t wi, const std::vector<double> &expected_peak_centers,
1156 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result,
1157 std::vector<std::vector<double>> &lastGoodPeakParameters,
1158 std::vector<size_t> &lastGoodPeakSpectra,
1159 const std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> &pre_check_result) {
1160 assert(fit_result->getNumberPeaks() == m_numPeaksToFit);
1161 pre_check_result->setNumberOfSubmittedSpectrumPeaks(m_numPeaksToFit);
1162 // if the whole spectrum has low count, do not fit any peaks for that spectrum
1164 for (size_t i = 0; i < m_numPeaksToFit; ++i)
1165 fit_result->setBadRecord(i, -1.);
1166 pre_check_result->setNumberOfSpectrumPeaksWithLowCount(m_numPeaksToFit);
1167 return;
1168 }
1169
1170 // Set up sub algorithm Fit for peak and background
1171 IAlgorithm_sptr peak_fitter; // both peak and background (combo)
1172 try {
1173 peak_fitter = createChildAlgorithm("Fit", -1, -1, false);
1174 } catch (Exception::NotFoundError &) {
1175 std::stringstream errss;
1176 errss << "The FitPeak algorithm requires the CurveFitting library";
1177 g_log.error(errss.str());
1178 throw std::runtime_error(errss.str());
1179 }
1180
1181 // Clone background function
1182 IBackgroundFunction_sptr bkgdfunction = std::dynamic_pointer_cast<API::IBackgroundFunction>(m_bkgdFunction->clone());
1183
1184 // set up properties of algorithm (reference) 'Fit'
1185 peak_fitter->setProperty("Minimizer", m_minimizer);
1186 peak_fitter->setProperty("CostFunction", m_costFunction);
1187 peak_fitter->setProperty("CalcErrors", true);
1188
1189 const double x0 = m_inputMatrixWS->histogram(wi).x().front();
1190 const double xf = m_inputMatrixWS->histogram(wi).x().back();
1191
1192 // index of previous peak in same spectrum (initially invalid)
1193 size_t prev_peak_index = m_numPeaksToFit;
1194 bool neighborPeakSameSpectrum = false;
1195 size_t number_of_out_of_range_peaks{0};
1196 for (size_t fit_index = 0; fit_index < m_numPeaksToFit; ++fit_index) {
1197 // convert fit index to peak index (in ascending order)
1198 size_t peak_index(fit_index);
1200 peak_index = m_numPeaksToFit - fit_index - 1;
1201
1202 // reset the background function
1203 for (size_t i = 0; i < bkgdfunction->nParams(); ++i)
1204 bkgdfunction->setParameter(i, 0.);
1205
1206 double expected_peak_pos = expected_peak_centers[peak_index];
1207
1208 // clone peak function for each peak (need to do this so can
1209 // set center and calc any parameters from xml)
1210 auto peakfunction = std::dynamic_pointer_cast<API::IPeakFunction>(m_peakFunction->clone());
1211 peakfunction->setCentre(expected_peak_pos);
1212
1213 std::pair<double, double> peak_window_i = m_getPeakFitWindow(wi, peak_index);
1214 peakfunction->setMatrixWorkspace(m_inputMatrixWS, wi, peak_window_i.first, peak_window_i.second);
1215
1216 std::map<size_t, double> keep_values;
1217 for (size_t ipar = 0; ipar < peakfunction->nParams(); ++ipar) {
1218 if (peakfunction->isFixed(ipar)) {
1219 // save value of these parameters which have just been calculated
1220 // if they were set to be fixed (e.g. for the B2Bexp this would
1221 // typically be A and B but not Sigma)
1222 keep_values[ipar] = peakfunction->getParameter(ipar);
1223 // by default let them be free to fit as these are typically refined
1224 // from a focussed bank; otherwise respect the fixed status and keep
1225 // the parameter locked at the calculated value.
1227 peakfunction->unfix(ipar);
1228 }
1229 }
1230 }
1231
1232 // Determine whether to set starting parameter from fitted value
1233 // of same peak but different spectrum
1234 bool samePeakCrossSpectrum = (lastGoodPeakParameters[peak_index].size() >
1235 static_cast<size_t>(std::count_if(lastGoodPeakParameters[peak_index].begin(),
1236 lastGoodPeakParameters[peak_index].end(),
1237 [&](auto const &val) { return val <= 1e-10; })));
1238
1239 // Check whether current spectrum's pixel (detector ID) is close to the
1240 // spectrum that last successfully fitted this peak.
1241 try {
1242 if (wi > 0 && samePeakCrossSpectrum) {
1243 size_t lastGoodWi = lastGoodPeakSpectra[peak_index];
1244 std::shared_ptr<const Geometry::Detector> pdetector =
1245 std::dynamic_pointer_cast<const Geometry::Detector>(m_inputMatrixWS->getDetector(lastGoodWi));
1246 std::shared_ptr<const Geometry::Detector> cdetector =
1247 std::dynamic_pointer_cast<const Geometry::Detector>(m_inputMatrixWS->getDetector(wi));
1248
1249 // If they do have detector ID
1250 if (pdetector && cdetector) {
1251 auto prev_id = pdetector->getID();
1252 auto curr_id = cdetector->getID();
1253 if (prev_id + 1 != curr_id)
1254 samePeakCrossSpectrum = false;
1255 } else {
1256 samePeakCrossSpectrum = false;
1257 }
1258
1259 } else {
1260 // first spectrum in the workspace: no peak's fitting result to copy
1261 // from
1262 samePeakCrossSpectrum = false;
1263 }
1264 } catch (const std::runtime_error &) {
1265 // workspace does not have detector ID set: there is no guarantee that the
1266 // adjacent spectra can have similar peak profiles
1267 samePeakCrossSpectrum = false;
1268 }
1269
1270 // Set starting values of the peak function.
1271 if (samePeakCrossSpectrum) { // somePeakFit
1272 // Get from local best result
1273 for (size_t i = 0; i < peakfunction->nParams(); ++i) {
1274 peakfunction->setParameter(i, lastGoodPeakParameters[peak_index][i]);
1275 }
1276 } else if (neighborPeakSameSpectrum && m_copyLastGoodPeakParameters) {
1277 // set the peak parameters from last good fit from ANY peak in the spectrum
1278 for (size_t i = 0; i < peakfunction->nParams(); ++i) {
1279 peakfunction->setParameter(i, lastGoodPeakParameters[prev_peak_index][i]);
1280 }
1281 }
1282
1283 // reset center though - don't know before hand which element this is
1284 peakfunction->setCentre(expected_peak_pos);
1285
1286 // reset value of parameters that were fixed (but are now free to vary)
1287 for (const auto &[ipar, value] : keep_values) {
1288 peakfunction->setParameter(ipar, value);
1289 }
1290
1291 double cost(DBL_MAX);
1292 if (expected_peak_pos <= x0 || expected_peak_pos >= xf) {
1293 // out of range and there won't be any fit
1294 peakfunction->setIntensity(0);
1295 number_of_out_of_range_peaks++;
1296 } else {
1297 // Decide whether to estimate peak width by observation
1298 // If no peaks fitted in the same or cross spectrum then the user supplied
1299 // parameters will be used if present and the width will not be estimated
1300 // (note this will overwrite parameter values caluclated from
1301 // Parameters.xml)
1302 // When CopyLastGoodPeakParameters is disabled, treat each peak as if it
1303 // has no fitted neighbour so that user-specified initial values are
1304 // re-applied as the baseline rather than falling back to function defaults.
1305 auto useUserSpecifedIfGiven =
1306 !(samePeakCrossSpectrum || (neighborPeakSameSpectrum && m_copyLastGoodPeakParameters));
1307 bool observe_peak_width = decideToEstimatePeakParams(useUserSpecifedIfGiven, peakfunction);
1308
1309 if (observe_peak_width && m_peakWidthEstimateApproach == EstimatePeakWidth::NoEstimation) {
1310 g_log.warning("Peak width can be estimated as ZERO. The result can be wrong");
1311 }
1312
1313 // do fitting with peak and background function (no analysis at this point)
1314 std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> peak_pre_check_result =
1315 std::make_shared<FitPeaksAlgorithm::PeakFitPreCheckResult>();
1316 cost = fitIndividualPeak(wi, peak_fitter, expected_peak_pos, peak_window_i, observe_peak_width, peakfunction,
1317 bkgdfunction, peak_pre_check_result);
1318 if (peak_pre_check_result->isIndividualPeakRejected())
1319 fit_result->setBadRecord(peak_index, -1.);
1320
1321 if (m_minSignalToSigmaRatio > 0) {
1322 if (calculateSignalToSigmaRatio(wi, peak_window_i, peakfunction) < m_minSignalToSigmaRatio) {
1323 fit_result->setBadRecord(peak_index, -1.);
1324 cost = DBL_MAX;
1325 }
1326 }
1327
1328 *pre_check_result += *peak_pre_check_result; // keep track of the rejection count within the spectrum
1329 }
1330 pre_check_result->setNumberOfOutOfRangePeaks(number_of_out_of_range_peaks);
1331
1332 // process fitting result
1333 FitPeaksAlgorithm::FitFunction fit_function;
1334 fit_function.peakfunction = peakfunction;
1335 fit_function.bkgdfunction = bkgdfunction;
1336
1337 auto good_fit = processSinglePeakFitResult(wi, peak_index, cost, expected_peak_centers, fit_function,
1338 fit_result); // sets the record
1339
1340 if (good_fit) {
1341 // reset the flag such that there is at a peak fit in this spectrum
1342 neighborPeakSameSpectrum = true;
1343 prev_peak_index = peak_index;
1344 // copy values and record which spectrum they came from
1345 for (size_t i = 0; i < lastGoodPeakParameters[peak_index].size(); ++i) {
1346 lastGoodPeakParameters[peak_index][i] = peakfunction->getParameter(i);
1347 }
1348 lastGoodPeakSpectra[peak_index] = wi;
1349 }
1350 }
1351
1352 return;
1353}
1354
1355//----------------------------------------------------------------------------------------------
1364bool FitPeaks::decideToEstimatePeakParams(const bool firstPeakInSpectrum,
1365 const API::IPeakFunction_sptr &peak_function) {
1366 // should observe the peak width if the user didn't supply all of the peak
1367 // function parameters
1368 bool observe_peak_shape(m_initParamIndexes.size() != peak_function->nParams());
1369
1370 if (!m_initParamIndexes.empty()) {
1371 // user specifies starting value of peak parameters
1372 if (firstPeakInSpectrum) {
1373 // set the parameter values in a vector and loop over it
1374 // first peak. using the user-specified value
1375 for (size_t i = 0; i < m_initParamIndexes.size(); ++i) {
1376 const size_t param_index = m_initParamIndexes[i];
1377 const double param_value = m_initParamValues[i];
1378 peak_function->setParameter(param_index, param_value);
1379 }
1380 } else {
1381 // using the fitted paramters from the previous fitting result
1382 // do noting
1383 }
1384 } else {
1385 // no previously defined peak parameters: observation is thus required
1386 observe_peak_shape = true;
1387 }
1388
1389 return observe_peak_shape;
1390}
1391
1392//----------------------------------------------------------------------------------------------
1404bool FitPeaks::processSinglePeakFitResult(size_t wsindex, size_t peakindex, const double cost,
1405 const std::vector<double> &expected_peak_positions,
1406 const FitPeaksAlgorithm::FitFunction &fitfunction,
1407 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result) {
1408 // determine peak position tolerance
1409 double postol(DBL_MAX);
1410 bool case23(false);
1411 if (m_peakPosTolCase234) {
1412 // peak tolerance is not defined
1413 if (m_numPeaksToFit == 1) {
1414 // case (d) one peak only
1415 postol = m_inputMatrixWS->histogram(wsindex).x().back() - m_inputMatrixWS->histogram(wsindex).x().front();
1416 } else {
1417 // case b and c: more than 1 peaks without defined peak tolerance
1418 case23 = true;
1419 }
1420 } else {
1421 // user explicitly specified
1422 if (peakindex >= m_peakPosTolerances.size())
1423 throw std::runtime_error("Peak tolerance out of index");
1424 postol = m_peakPosTolerances[peakindex];
1425 }
1426
1427 // get peak position and analyze the fitting is good or not by various
1428 // criteria
1429 auto peak_pos = fitfunction.peakfunction->centre();
1430 auto peak_fwhm = fitfunction.peakfunction->fwhm();
1431 bool good_fit(false);
1432 if ((cost < 0) || (cost >= DBL_MAX - 1.) || std::isnan(cost)) {
1433 // unphysical cost function value
1434 peak_pos = -4;
1435 } else if (fitfunction.peakfunction->height() < m_minPeakHeight) {
1436 // peak height is under minimum request
1437 peak_pos = -3;
1438 } else if (case23) {
1439 // case b and c to check peak position without defined peak tolerance
1440 std::pair<double, double> fitwindow = m_getPeakFitWindow(wsindex, peakindex);
1441 if (fitwindow.first < fitwindow.second) {
1442 // peak fit window is specified or calculated: use peak window as position
1443 // tolerance
1444 if (peak_pos < fitwindow.first || peak_pos > fitwindow.second) {
1445 // peak is out of fit window
1446 peak_pos = -2;
1447 g_log.debug() << "Peak position " << peak_pos << " is out of fit "
1448 << "window boundary " << fitwindow.first << ", " << fitwindow.second << "\n";
1449 } else if (peak_fwhm > (fitwindow.second - fitwindow.first)) {
1450 // peak is too wide or window is too small
1451 peak_pos = -2.25;
1452 g_log.debug() << "Peak position " << peak_pos << " has fwhm "
1453 << "wider than the fit window " << fitwindow.second - fitwindow.first << "\n";
1454 } else {
1455 good_fit = true;
1456 }
1457 } else {
1458 // use the 1/2 distance to neiboring peak without defined peak window
1459 double left_bound(-1);
1460 if (peakindex > 0)
1461 left_bound = 0.5 * (expected_peak_positions[peakindex] - expected_peak_positions[peakindex - 1]);
1462 double right_bound(-1);
1463 if (peakindex < m_numPeaksToFit - 1)
1464 right_bound = 0.5 * (expected_peak_positions[peakindex + 1] - expected_peak_positions[peakindex]);
1465 if (left_bound < 0)
1466 left_bound = right_bound;
1467 if (right_bound < left_bound)
1468 right_bound = left_bound;
1469 if (left_bound < 0 || right_bound < 0)
1470 throw std::runtime_error("Code logic error such that left or right "
1471 "boundary of peak position is negative.");
1472 if (peak_pos < left_bound || peak_pos > right_bound) {
1473 peak_pos = -2.5;
1474 } else if (peak_fwhm > (right_bound - left_bound)) {
1475 // peak is too wide or window is too small
1476 peak_pos = -2.75;
1477 g_log.debug() << "Peak position " << peak_pos << " has fwhm "
1478 << "wider than the fit window " << right_bound - left_bound << "\n";
1479 } else {
1480 good_fit = true;
1481 }
1482 }
1483 } else if (fabs(fitfunction.peakfunction->centre() - expected_peak_positions[peakindex]) > postol) {
1484 // peak center is not within tolerance
1485 peak_pos = -5;
1486 g_log.debug() << "Peak position difference "
1487 << fabs(fitfunction.peakfunction->centre() - expected_peak_positions[peakindex])
1488 << " is out of range of tolerance: " << postol << "\n";
1489 } else {
1490 // all criteria are passed
1491 good_fit = true;
1492 }
1493
1494 // set cost function to DBL_MAX if fitting is bad
1495 double adjust_cost(cost);
1496 if (!good_fit) {
1497 // set the cost function value to DBL_MAX
1498 adjust_cost = DBL_MAX;
1499 }
1500
1501 // reset cost
1502 if (adjust_cost > DBL_MAX - 1) {
1503 fitfunction.peakfunction->setIntensity(0);
1504 }
1505
1506 // chi2
1507 fit_result->setRecord(peakindex, adjust_cost, peak_pos, fitfunction);
1508
1509 return good_fit;
1510}
1511
1512//----------------------------------------------------------------------------------------------
1518void FitPeaks::calculateFittedPeaks(const std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> &fit_results) {
1519 // check
1520 if (!m_fittedParamTable)
1521 throw std::runtime_error("No parameters");
1522
1523 const size_t num_peakfunc_params = m_peakFunction->nParams();
1524 const size_t num_bkgdfunc_params = m_bkgdFunction->nParams();
1525
1526 // Configure the peak functions before entering the parallel region. Some peak
1527 // functions use setMatrixWorkspace() to initialise detector/workspace-derived
1528 // state.
1529 std::vector<std::vector<IPeakFunction_sptr>> peak_functions(m_numSpectraToFit,
1530 std::vector<IPeakFunction_sptr>(m_numPeaksToFit));
1531 std::vector<std::vector<IBackgroundFunction_sptr>> bkgd_functions(
1532 m_numSpectraToFit, std::vector<IBackgroundFunction_sptr>(m_numPeaksToFit));
1533
1534 for (size_t iws = m_startWorkspaceIndex; iws <= m_stopWorkspaceIndex; ++iws) {
1535 const size_t output_iws = iws - m_startWorkspaceIndex;
1536 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result_i = fit_results[output_iws];
1537 // FIXME - This is a just a pure check
1538 if (!fit_result_i)
1539 throw std::runtime_error("There is something wroing with PeakFitResult vector!");
1540
1541 for (size_t ipeak = 0; ipeak < m_numPeaksToFit; ++ipeak) {
1542 const double chi2 = fit_result_i->getCost(ipeak);
1543 if (chi2 > 10.e10)
1544 continue;
1545
1546 IPeakFunction_sptr peak_function = std::dynamic_pointer_cast<IPeakFunction>(m_peakFunction->clone());
1547 IBackgroundFunction_sptr bkgd_function = std::dynamic_pointer_cast<IBackgroundFunction>(m_bkgdFunction->clone());
1548
1549 for (size_t iparam = 0; iparam < num_peakfunc_params; ++iparam)
1550 peak_function->setParameter(iparam, fit_result_i->getParameterValue(ipeak, iparam));
1551 for (size_t iparam = 0; iparam < num_bkgdfunc_params; ++iparam)
1552 bkgd_function->setParameter(iparam, fit_result_i->getParameterValue(ipeak, num_peakfunc_params + iparam));
1553
1554 const std::pair<double, double> peakwindow = m_getPeakFitWindow(iws, ipeak);
1555 peak_function->setMatrixWorkspace(m_inputMatrixWS, iws, peakwindow.first, peakwindow.second);
1556
1557 peak_functions[output_iws][ipeak] = std::move(peak_function);
1558 bkgd_functions[output_iws][ipeak] = std::move(bkgd_function);
1559 }
1560 }
1561
1563 for (int64_t iiws = m_startWorkspaceIndex; iiws <= static_cast<int64_t>(m_stopWorkspaceIndex); ++iiws) {
1565 const std::size_t iws = static_cast<std::size_t>(iiws);
1566 const size_t output_iws = iws - m_startWorkspaceIndex;
1567
1568 for (size_t ipeak = 0; ipeak < m_numPeaksToFit; ++ipeak) {
1569 if (!peak_functions[output_iws][ipeak] || !bkgd_functions[output_iws][ipeak])
1570 continue;
1571
1572 // use domain and function to calculate
1573 // get the range of start and stop to construct a function domain
1574 const auto vec_x = m_fittedPeakWS->points(iws);
1575 const std::pair<double, double> peakwindow = m_getPeakFitWindow(iws, ipeak);
1576 auto start_x_iter = std::lower_bound(vec_x.begin(), vec_x.end(), peakwindow.first);
1577 auto stop_x_iter = std::lower_bound(vec_x.begin(), vec_x.end(), peakwindow.second);
1578
1579 if (start_x_iter == stop_x_iter)
1580 throw std::runtime_error("Range size is zero in calculateFittedPeaks");
1581
1582 FunctionDomain1DVector domain(start_x_iter, stop_x_iter);
1583 FunctionValues values(domain);
1584 CompositeFunction_sptr comp_func = std::make_shared<API::CompositeFunction>();
1585 comp_func->addFunction(std::move(peak_functions[output_iws][ipeak]));
1586 comp_func->addFunction(std::move(bkgd_functions[output_iws][ipeak]));
1587 comp_func->function(domain, values);
1588
1589 // copy over the values
1590 std::size_t istart = static_cast<size_t>(start_x_iter - vec_x.begin());
1591 std::size_t istop = static_cast<size_t>(stop_x_iter - vec_x.begin());
1592 for (std::size_t yindex = istart; yindex < istop; ++yindex) {
1593 m_fittedPeakWS->dataY(iws)[yindex] = values.getCalculated(yindex - istart);
1594 }
1595 } // END-FOR (ipeak)
1597 } // END-FOR (iws)
1599
1600 return;
1601}
1602
1603double FitPeaks::calculateSignalToSigmaRatio(const size_t &iws, const std::pair<double, double> &peakWindow,
1604 const API::IPeakFunction_sptr &peakFunction) {
1605 const auto vecX = m_inputMatrixWS->points(iws);
1606 auto startX = std::lower_bound(vecX.begin(), vecX.end(), peakWindow.first);
1607 auto stopX = std::lower_bound(vecX.begin(), vecX.end(), peakWindow.second);
1608
1609 FunctionDomain1DVector domain(startX, stopX);
1610 FunctionValues values(domain);
1611
1612 peakFunction->function(domain, values);
1613 auto peakValues = values.toVector();
1614
1615 const auto &errors = m_inputMatrixWS->readE(iws);
1616 auto startE = errors.begin() + (startX - vecX.begin());
1617 auto stopE = errors.begin() + (stopX - vecX.begin());
1618 std::vector<double> peakErrors(startE, stopE);
1619
1620 double peakSum = std::accumulate(peakValues.cbegin(), peakValues.cend(), 0.0);
1621 double sigma = sqrt(std::accumulate(peakErrors.cbegin(), peakErrors.cend(), 0.0, VectorHelper::SumSquares<double>()));
1622
1623 return peakSum / ((sigma == 0) ? 1 : sigma);
1624}
1625
1626namespace {
1627bool estimateBackgroundParameters(const Histogram &histogram, const std::pair<size_t, size_t> &peak_window,
1628 const API::IBackgroundFunction_sptr &bkgd_function) {
1629 // for estimating background parameters
1630 // 0 = constant, 1 = linear
1631 const auto POLYNOMIAL_ORDER = std::min<size_t>(1, bkgd_function->nParams());
1632
1633 if (peak_window.first >= peak_window.second)
1634 throw std::runtime_error("Invalid peak window");
1635
1636 // reset the background function
1637 const auto nParams = bkgd_function->nParams();
1638 for (size_t i = 0; i < nParams; ++i)
1639 bkgd_function->setParameter(i, 0.);
1640
1641 // 10 is a magic number that worked in a variety of situations
1642 const size_t iback_start = peak_window.first + 10;
1643 const size_t iback_stop = peak_window.second - 10;
1644
1645 // use the simple way to find linear background
1646 // there aren't enough bins in the window to try to estimate so just leave the
1647 // estimate at zero
1648 if (iback_start < iback_stop) {
1649 double bkgd_a0{0.}; // will be fit
1650 double bkgd_a1{0.}; // may be fit
1651 double bkgd_a2{0.}; // will be ignored
1652 double chisq{DBL_MAX}; // how well the fit worked
1653 HistogramData::estimateBackground(POLYNOMIAL_ORDER, histogram, peak_window.first, peak_window.second, iback_start,
1654 iback_stop, bkgd_a0, bkgd_a1, bkgd_a2, chisq);
1655 // update the background function with the result
1656 bkgd_function->setParameter(0, bkgd_a0);
1657 if (nParams > 1)
1658 bkgd_function->setParameter(1, bkgd_a1);
1659 // quadratic term is always estimated to be zero
1660
1661 // TODO: return false if chisq is too large
1662 return true;
1663 }
1664
1665 return false; // too few data points for the fit
1666}
1667} // anonymous namespace
1668
1669//----------------------------------------------------------------------------------------------
1675bool FitPeaks::isObservablePeakProfile(const std::string &peakprofile) {
1676 return (std::find(supported_peak_profiles.begin(), supported_peak_profiles.end(), peakprofile) !=
1677 supported_peak_profiles.end());
1678}
1679
1680//----------------------------------------------------------------------------------------------
1683bool FitPeaks::fitBackground(const size_t &ws_index, const std::pair<double, double> &fit_window,
1684 const double &expected_peak_pos, const API::IBackgroundFunction_sptr &bkgd_func) {
1685 constexpr size_t MIN_POINTS{10}; // TODO explain why 10
1686
1687 // find out how to fit background
1688 const auto histogram = m_inputMatrixWS->histogram(ws_index);
1689 const auto &points = histogram.points();
1690 size_t start_index = findXIndex(points.rawData(), fit_window.first);
1691 size_t expected_peak_index = findXIndex(points.rawData(), expected_peak_pos, start_index);
1692 size_t stop_index = findXIndex(points.rawData(), fit_window.second, expected_peak_index);
1693
1694 // treat 5 as a magic number - TODO explain why
1695 bool good_fit(false);
1696 if (expected_peak_index - start_index > MIN_POINTS && stop_index - expected_peak_index > MIN_POINTS) {
1697 // enough data points left for multi-domain fitting
1698 // set a smaller fit window
1699 const std::pair<double, double> vec_min{fit_window.first, points[expected_peak_index + 5]};
1700 const std::pair<double, double> vec_max{points[expected_peak_index - 5], fit_window.second};
1701
1702 // reset background function value
1703 for (size_t n = 0; n < bkgd_func->nParams(); ++n)
1704 bkgd_func->setParameter(n, 0);
1705
1706 double chi2 = fitFunctionMD(bkgd_func, m_inputMatrixWS, ws_index, vec_min, vec_max);
1707
1708 // process
1709 if (chi2 < DBL_MAX - 1) {
1710 good_fit = true;
1711 }
1712
1713 } else {
1714 // fit as a single domain function. check whether the result is good or bad
1715
1716 // TODO FROM HERE!
1717 g_log.debug() << "Don't know what to do with background fitting with single "
1718 << "domain function! " << (expected_peak_index - start_index) << " points to the left "
1719 << (stop_index - expected_peak_index) << " points to the right\n";
1720 }
1721
1722 return good_fit;
1723}
1724
1725//----------------------------------------------------------------------------------------------
1728double FitPeaks::fitIndividualPeak(size_t wi, const API::IAlgorithm_sptr &fitter, const double expected_peak_center,
1729 const std::pair<double, double> &fitwindow, const bool estimate_peak_width,
1730 const API::IPeakFunction_sptr &peakfunction,
1731 const API::IBackgroundFunction_sptr &bkgdfunc,
1732 const std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> &pre_check_result) {
1733 pre_check_result->setNumberOfSubmittedIndividualPeaks(1);
1734 double cost(DBL_MAX);
1735
1736 // make sure the number of data points satisfies the number of fitting parameters plus a magic cushion of 2.
1737 size_t min_required_datapoints{peakfunction->nParams() + bkgdfunc->nParams() + 2};
1738 size_t number_of_datapoints = histRangeToDataPointCount(wi, fitwindow);
1739 if (number_of_datapoints < min_required_datapoints) {
1740 pre_check_result->setNumberOfPeaksWithNotEnoughDataPoints(1);
1741 return cost;
1742 }
1743
1744 // check the number of counts in the peak window
1745 if (m_minPeakTotalCount >= 0.0 && numberCounts(wi, fitwindow) <= m_minPeakTotalCount) {
1746 pre_check_result->setNumberOfIndividualPeaksWithLowCount(1);
1747 return cost;
1748 }
1749
1750 // exclude a peak with a low signal-to-noise ratio
1751 if (m_minSignalToNoiseRatio > 0.0 && calculateSignalToNoiseRatio(wi, fitwindow, bkgdfunc) < m_minSignalToNoiseRatio) {
1752 pre_check_result->setNumberOfPeaksWithLowSignalToNoise(1);
1753 return cost;
1754 }
1755
1756 if (m_highBackground) {
1757 // fit peak with high background!
1758 cost = fitFunctionHighBackground(fitter, fitwindow, wi, expected_peak_center, estimate_peak_width, peakfunction,
1759 bkgdfunc);
1760 } else {
1761 // fit peak and background
1762 cost = fitFunctionSD(fitter, peakfunction, bkgdfunc, m_inputMatrixWS, wi, fitwindow, expected_peak_center,
1763 estimate_peak_width, true);
1764 }
1765
1766 return cost;
1767}
1768
1769//----------------------------------------------------------------------------------------------
1776 const API::IBackgroundFunction_sptr &bkgd_function,
1777 const API::MatrixWorkspace_sptr &dataws, size_t wsindex,
1778 const std::pair<double, double> &peak_range, const double &expected_peak_center,
1779 bool estimate_peak_width, bool estimate_background) {
1780 std::stringstream errorid;
1781 errorid << "(WorkspaceIndex=" << wsindex << " PeakCentre=" << expected_peak_center << ")";
1782
1783 // validate peak window
1784 if (peak_range.first >= peak_range.second) {
1785 std::stringstream msg;
1786 msg << "Invalid peak window: xmin>xmax (" << peak_range.first << ", " << peak_range.second << ")" << errorid.str();
1787 throw std::runtime_error(msg.str());
1788 }
1789
1790 // determine the peak window in terms of vector indexes
1791 const auto &histogram = dataws->histogram(wsindex);
1792 const auto &vector_x = histogram.points();
1793 const auto start_index = findXIndex(vector_x, peak_range.first);
1794 const auto stop_index = findXIndex(vector_x, peak_range.second, start_index);
1795 if (start_index == stop_index)
1796 throw std::runtime_error("Range size is zero in fitFunctionSD");
1797 std::pair<size_t, size_t> peak_index_window = std::make_pair(start_index, stop_index);
1798
1799 // Estimate background
1800 if (estimate_background) {
1801 if (!estimateBackgroundParameters(histogram, peak_index_window, bkgd_function)) {
1802 return DBL_MAX;
1803 }
1804 }
1805
1806 // Estimate peak profile parameter
1807 peak_function->setCentre(expected_peak_center); // set expected position first
1808 int result = estimatePeakParameters(histogram, peak_index_window, peak_function, bkgd_function, estimate_peak_width,
1810
1811 if (result != GOOD) {
1812 peak_function->setCentre(expected_peak_center);
1813 if (result == NOSIGNAL || result == LOWPEAK) {
1814 return DBL_MAX; // exit early - don't fit
1815 }
1816 }
1817
1818 // Create the composition function
1819 CompositeFunction_sptr comp_func = std::make_shared<API::CompositeFunction>();
1820 comp_func->addFunction(peak_function);
1821 comp_func->addFunction(bkgd_function);
1822 IFunction_sptr fitfunc = std::dynamic_pointer_cast<IFunction>(comp_func);
1823
1824 // Set the properties
1825 fit->setProperty("Function", fitfunc);
1826 fit->setProperty("InputWorkspace", dataws);
1827 fit->setProperty("WorkspaceIndex", static_cast<int>(wsindex));
1828 fit->setProperty("MaxIterations", m_fitIterations); // magic number
1829 fit->setProperty("StartX", peak_range.first);
1830 fit->setProperty("EndX", peak_range.second);
1831 fit->setProperty("IgnoreInvalidData", true);
1832
1834 // set up a constraint on peak position
1835 double peak_center = peak_function->centre();
1836 double peak_width = peak_function->fwhm();
1837 std::stringstream peak_center_constraint;
1838 peak_center_constraint << (peak_center - 0.5 * peak_width) << " < f0." << peak_function->getCentreParameterName()
1839 << " < " << (peak_center + 0.5 * peak_width);
1840 fit->setProperty("Constraints", peak_center_constraint.str());
1841 }
1842
1843 // Execute fit and get result of fitting background
1844 g_log.debug() << "[E1201] FitSingleDomain Before fitting, Fit function: " << fit->asString() << "\n";
1845 errorid << " starting function [" << comp_func->asString() << "]";
1846 try {
1847 fit->execute();
1848 g_log.debug() << "[E1202] FitSingleDomain After fitting, Fit function: " << fit->asString() << "\n";
1849
1850 if (!fit->isExecuted()) {
1851 g_log.warning() << "Fitting peak SD (single domain) failed to execute. " + errorid.str();
1852 return DBL_MAX;
1853 }
1854 } catch (std::invalid_argument &e) {
1855 errorid << ": " << e.what();
1856 g_log.warning() << "\nWhile fitting " + errorid.str();
1857 return DBL_MAX; // probably the wrong thing to do
1858 }
1859
1860 // Retrieve result
1861 std::string fitStatus = fit->getProperty("OutputStatus");
1862 double chi2{std::numeric_limits<double>::max()};
1863 if (fitStatus == "success") {
1864 chi2 = fit->getProperty("OutputChi2overDoF");
1865 }
1866
1867 return chi2;
1868}
1869
1870//----------------------------------------------------------------------------------------------
1872 const size_t wsindex, const std::pair<double, double> &vec_xmin,
1873 const std::pair<double, double> &vec_xmax) {
1874 // Note: after testing it is found that multi-domain Fit cannot be reused
1876 try {
1877 fit = createChildAlgorithm("Fit", -1, -1, false);
1878 } catch (Exception::NotFoundError &) {
1879 std::stringstream errss;
1880 errss << "The FitPeak algorithm requires the CurveFitting library";
1881 throw std::runtime_error(errss.str());
1882 }
1883 // set up background fit instance
1884 fit->setProperty("Minimizer", m_minimizer);
1885 fit->setProperty("CostFunction", m_costFunction);
1886 fit->setProperty("CalcErrors", true);
1887
1888 // This use multi-domain; but does not know how to set up IFunction_sptr
1889 // fitfunc,
1890 std::shared_ptr<MultiDomainFunction> md_function = std::make_shared<MultiDomainFunction>();
1891
1892 // Set function first
1893 md_function->addFunction(std::move(fit_function));
1894
1895 // set domain for function with index 0 covering both sides
1896 md_function->clearDomainIndices();
1897 md_function->setDomainIndices(0, {0, 1});
1898
1899 // Set the properties
1900 fit->setProperty("Function", std::dynamic_pointer_cast<IFunction>(md_function));
1901 fit->setProperty("InputWorkspace", dataws);
1902 fit->setProperty("WorkspaceIndex", static_cast<int>(wsindex));
1903 fit->setProperty("StartX", vec_xmin.first);
1904 fit->setProperty("EndX", vec_xmax.first);
1905 fit->setProperty("InputWorkspace_1", dataws);
1906 fit->setProperty("WorkspaceIndex_1", static_cast<int>(wsindex));
1907 fit->setProperty("StartX_1", vec_xmin.second);
1908 fit->setProperty("EndX_1", vec_xmax.second);
1909 fit->setProperty("MaxIterations", m_fitIterations);
1910 fit->setProperty("IgnoreInvalidData", true);
1911
1912 // Execute
1913 fit->execute();
1914 if (!fit->isExecuted()) {
1915 throw runtime_error("Fit is not executed on multi-domain function/data. ");
1916 }
1917
1918 // Retrieve result
1919 std::string fitStatus = fit->getProperty("OutputStatus");
1920
1921 double chi2 = DBL_MAX;
1922 if (fitStatus == "success") {
1923 chi2 = fit->getProperty("OutputChi2overDoF");
1924 }
1925
1926 return chi2;
1927}
1928
1929//----------------------------------------------------------------------------------------------
1931double FitPeaks::fitFunctionHighBackground(const IAlgorithm_sptr &fit, const std::pair<double, double> &fit_window,
1932 const size_t &ws_index, const double &expected_peak_center,
1933 bool observe_peak_shape, const API::IPeakFunction_sptr &peakfunction,
1934 const API::IBackgroundFunction_sptr &bkgdfunc) {
1936
1937 // high background to reduce
1938 API::IBackgroundFunction_sptr high_bkgd_function =
1939 std::dynamic_pointer_cast<API::IBackgroundFunction>(m_linearBackgroundFunction->clone());
1940
1941 // Fit the background first if there is enough data points
1942 fitBackground(ws_index, fit_window, expected_peak_center, high_bkgd_function);
1943
1944 // Get partial of the data
1945 std::vector<double> vec_x, vec_y, vec_e;
1946 getRangeData(ws_index, fit_window, vec_x, vec_y, vec_e);
1947
1948 // Reduce the background
1949 reduceByBackground(high_bkgd_function, vec_x, vec_y);
1950 for (std::size_t n = 0; n < bkgdfunc->nParams(); ++n)
1951 bkgdfunc->setParameter(n, 0);
1952
1953 // Create a new workspace
1954 API::MatrixWorkspace_sptr reduced_bkgd_ws = createMatrixWorkspace(vec_x, vec_y, vec_e);
1955
1956 // Fit peak with background
1957 fitFunctionSD(fit, peakfunction, bkgdfunc, reduced_bkgd_ws, 0, {vec_x.front(), vec_x.back()}, expected_peak_center,
1958 observe_peak_shape, false);
1959
1960 // add the reduced background back
1961 bkgdfunc->setParameter(0, bkgdfunc->getParameter(0) + high_bkgd_function->getParameter(0));
1962 bkgdfunc->setParameter(1, bkgdfunc->getParameter(1) + // TODO doesn't work for flat background
1963 high_bkgd_function->getParameter(1));
1964
1965 double cost = fitFunctionSD(fit, peakfunction, bkgdfunc, m_inputMatrixWS, ws_index, {vec_x.front(), vec_x.back()},
1966 expected_peak_center, false, false);
1967
1968 return cost;
1969}
1970
1971//----------------------------------------------------------------------------------------------
1974 const std::vector<double> &vec_y,
1975 const std::vector<double> &vec_e) {
1976 std::size_t size = vec_x.size();
1977 std::size_t ysize = vec_y.size();
1978
1979 HistogramBuilder builder;
1980 builder.setX(size);
1981 builder.setY(ysize);
1982 MatrixWorkspace_sptr matrix_ws = create<Workspace2D>(1, builder.build());
1983
1984 auto &dataX = matrix_ws->mutableX(0);
1985 auto &dataY = matrix_ws->mutableY(0);
1986 auto &dataE = matrix_ws->mutableE(0);
1987
1988 dataX.assign(vec_x.cbegin(), vec_x.cend());
1989 dataY.assign(vec_y.cbegin(), vec_y.cend());
1990 dataE.assign(vec_e.cbegin(), vec_e.cend());
1991 return matrix_ws;
1992}
1993
1994//----------------------------------------------------------------------------------------------
1998 // create output workspace for peak positions: can be partial spectra to input
1999 // workspace
2000 m_outputPeakPositionWorkspace = create<Workspace2D>(m_numSpectraToFit, Points(m_numPeaksToFit));
2001 // set default
2002 for (std::size_t wi = 0; wi < m_numSpectraToFit; ++wi) {
2003 // convert to workspace index of input data workspace
2004 std::size_t inp_wi = wi + m_startWorkspaceIndex;
2005 std::vector<double> expected_position = m_getExpectedPeakPositions(inp_wi);
2006 for (std::size_t ipeak = 0; ipeak < expected_position.size(); ++ipeak) {
2007 m_outputPeakPositionWorkspace->dataX(wi)[ipeak] = expected_position[ipeak];
2008 }
2009 }
2010
2011 return;
2012}
2013
2014//----------------------------------------------------------------------------------------------
2022 const std::vector<std::string> &param_names, bool with_chi2) {
2023 // add columns
2024 table_ws->addColumn("int", "wsindex");
2025 table_ws->addColumn("int", "peakindex");
2026 for (const auto &param_name : param_names)
2027 table_ws->addColumn("double", param_name);
2028 if (with_chi2)
2029 table_ws->addColumn("double", "chi2");
2030
2031 // add rows
2032 const size_t numParam = m_fittedParamTable->columnCount() - 3;
2033 for (size_t iws = m_startWorkspaceIndex; iws <= m_stopWorkspaceIndex; ++iws) {
2034 for (size_t ipeak = 0; ipeak < m_numPeaksToFit; ++ipeak) {
2035 API::TableRow newRow = table_ws->appendRow();
2036 newRow << static_cast<int>(iws); // workspace index
2037 newRow << static_cast<int>(ipeak); // peak number
2038 for (size_t iparam = 0; iparam < numParam; ++iparam)
2039 newRow << 0.; // parameters for each peak
2040 if (with_chi2)
2041 newRow << DBL_MAX; // chisq
2042 }
2043 }
2044
2045 return;
2046}
2047
2048//----------------------------------------------------------------------------------------------
2054 // peak parameter workspace
2055 m_rawPeaksTable = getProperty(PropertyNames::RAW_PARAMS);
2056
2057 // create parameters
2058 // peak
2059 std::vector<std::string> param_vec;
2060 if (m_rawPeaksTable) {
2061 param_vec = m_peakFunction->getParameterNames();
2062 } else {
2063 param_vec.emplace_back("centre");
2064 param_vec.emplace_back("width");
2065 param_vec.emplace_back("height");
2066 param_vec.emplace_back("intensity");
2067 }
2068 // background
2069 for (size_t iparam = 0; iparam < m_bkgdFunction->nParams(); ++iparam)
2070 param_vec.emplace_back(m_bkgdFunction->parameterName(iparam));
2071
2072 // parameter value table
2073 m_fittedParamTable = std::make_shared<TableWorkspace>();
2075
2076 // for error workspace
2077 std::string fiterror_table_name = getPropertyValue(PropertyNames::OUTPUT_WKSP_PARAM_ERRS);
2078 // do nothing if user does not specifiy
2079 if (fiterror_table_name.empty()) {
2080 // not specified
2081 m_fitErrorTable = nullptr;
2082 } else {
2083 // create table and set up parameter table
2084 m_fitErrorTable = std::make_shared<TableWorkspace>();
2086 }
2087
2088 return;
2089}
2090
2091//----------------------------------------------------------------------------------------------
2096 // matrix workspace contained calculated peaks from fitting
2097 std::string fit_ws_name = getPropertyValue(PropertyNames::OUTPUT_WKSP_MODEL);
2098 if (fit_ws_name.size() == 0) {
2099 // skip if user does not specify
2100 m_fittedPeakWS = nullptr;
2101 return;
2102 }
2103
2104 // create a wokspace with same size as in the input matrix workspace
2105 m_fittedPeakWS = create<Workspace2D>(*m_inputMatrixWS);
2106}
2107
2108//----------------------------------------------------------------------------------------------
2110void FitPeaks::processOutputs(std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> fit_result_vec) {
2112 setProperty(PropertyNames::OUTPUT_WKSP_PARAMS, m_fittedParamTable);
2113
2114 if (m_fitErrorTable) {
2115 g_log.warning("Output error table workspace");
2116 setProperty(PropertyNames::OUTPUT_WKSP_PARAM_ERRS, m_fitErrorTable);
2117 } else {
2118 g_log.warning("No error table output");
2119 }
2120
2121 // optional
2123 g_log.debug("about to calcualte fitted peaks");
2124 calculateFittedPeaks(std::move(fit_result_vec));
2125 setProperty(PropertyNames::OUTPUT_WKSP_MODEL, m_fittedPeakWS);
2126 }
2127}
2128
2129//----------------------------------------------------------------------------------------------
2134double FitPeaks::numberCounts(size_t iws) {
2135 const Histogram histogram = m_inputMatrixWS->histogram(iws);
2136 const auto &vec_y = histogram.y().rawData();
2137 double total = std::accumulate(vec_y.begin(), vec_y.end(), 0.);
2138 return total;
2139}
2140
2141//----------------------------------------------------------------------------------------------
2147double FitPeaks::numberCounts(size_t iws, const std::pair<double, double> &range) {
2148 // get data range
2149 std::vector<double> vec_x, vec_y, vec_e;
2150 getRangeData(iws, range, vec_x, vec_y, vec_e);
2151 // sum up all counts
2152 double total = std::accumulate(vec_y.begin(), vec_y.end(), 0.);
2153 return total;
2154}
2155
2156//----------------------------------------------------------------------------------------------
2162size_t FitPeaks::histRangeToDataPointCount(size_t iws, const std::pair<double, double> &range) {
2163 size_t left_index, right_index;
2164 histRangeToIndexBounds(iws, range, left_index, right_index);
2165 size_t number_dp = right_index - left_index + 1;
2166 if (m_inputMatrixWS->isHistogramData())
2167 number_dp -= 1;
2168 assert(number_dp > 0);
2169 return number_dp;
2170}
2171
2172//----------------------------------------------------------------------------------------------
2179void FitPeaks::histRangeToIndexBounds(size_t iws, const std::pair<double, double> &range, size_t &left_index,
2180 size_t &right_index) {
2181 const Histogram histogram = m_inputMatrixWS->histogram(iws);
2182 const auto &orig_x = histogram.x();
2183 rangeToIndexBounds(orig_x, range.first, range.second, left_index, right_index);
2184
2185 // handle an invalid range case. For the histogram point data, make sure the number of data points is non-zero as
2186 // well.
2187 if (left_index >= right_index || (m_inputMatrixWS->isHistogramData() && left_index == right_index - 1)) {
2188 std::stringstream err_ss;
2189 err_ss << "Unable to get a valid subset of histogram from given fit window. "
2190 << "Histogram X: " << orig_x.front() << "," << orig_x.back() << "; Range: " << range.first << ","
2191 << range.second;
2192 throw std::runtime_error(err_ss.str());
2193 }
2194}
2195
2196//----------------------------------------------------------------------------------------------
2204void FitPeaks::getRangeData(size_t iws, const std::pair<double, double> &range, std::vector<double> &vec_x,
2205 std::vector<double> &vec_y, std::vector<double> &vec_e) {
2206 // convert range to index boundaries
2207 size_t left_index, right_index;
2208 histRangeToIndexBounds(iws, range, left_index, right_index);
2209
2210 // copy X, Y and E
2211 size_t num_elements_x = right_index - left_index;
2212
2213 vec_x.resize(num_elements_x);
2214 const Histogram histogram = m_inputMatrixWS->histogram(iws);
2215 const auto &orig_x = histogram.x();
2216 std::copy(orig_x.begin() + left_index, orig_x.begin() + right_index, vec_x.begin());
2217
2218 size_t num_datapoints = m_inputMatrixWS->isHistogramData() ? num_elements_x - 1 : num_elements_x;
2219
2220 const auto &orig_y = histogram.y().rawData();
2221 const auto &orig_e = histogram.e().rawData();
2222 vec_y.resize(num_datapoints);
2223 vec_e.resize(num_datapoints);
2224 std::copy(orig_y.begin() + left_index, orig_y.begin() + left_index + num_datapoints, vec_y.begin());
2225 std::copy(orig_e.begin() + left_index, orig_e.begin() + left_index + num_datapoints, vec_e.begin());
2226}
2227
2228//----------------------------------------------------------------------------------------------
2235double FitPeaks::calculateSignalToNoiseRatio(size_t iws, const std::pair<double, double> &range,
2236 const API::IBackgroundFunction_sptr &bkgd_function) {
2237 // convert range to index boundaries
2238 size_t left_index, right_index;
2239 histRangeToIndexBounds(iws, range, left_index, right_index);
2240
2241 // estimate background level by Y(X) fitting
2242 if (!estimateBackgroundParameters(m_inputMatrixWS->histogram(iws), std::pair<size_t, size_t>(left_index, right_index),
2243 bkgd_function))
2244 return 0.0; // failed to estimate background parameters
2245
2246 // get X,Y,and E for the data range
2247 std::vector<double> vec_x, vec_y, vec_e;
2248 getRangeData(iws, range, vec_x, vec_y, vec_e);
2249 if (vec_x.empty())
2250 return 0.0;
2251
2252 // subtract background from Y-values
2253 reduceByBackground(bkgd_function, vec_x, vec_y);
2254
2255 // estimate the signal as the highest Y-value in the data range
2256 auto it_max = std::max_element(vec_y.begin(), vec_y.end());
2257 double signal = vec_y[it_max - vec_y.begin()];
2258 if (signal <= DBL_MIN)
2259 return 0.0;
2260
2261 // estimate noise from background. If noise is zero, or impossible to estimate, return DBL_MAX so that the peak
2262 // won't be rejected.
2263 double noise = estimateBackgroundNoise(vec_y);
2264 if (noise <= DBL_MIN)
2265 return DBL_MAX;
2266
2267 // finally, calculate the signal-to-noise ratio
2268 return signal / noise;
2269}
2270
2271//----------------------------------------------------------------------------------------------
2273
2274void FitPeaks::checkWorkspaceIndices(std::size_t const &wi) {
2275 if (wi < m_startWorkspaceIndex || wi > m_stopWorkspaceIndex) {
2276 std::stringstream errss;
2277 errss << "Workspace index " << wi << " is out of range "
2278 << "[" << m_startWorkspaceIndex << ", " << m_stopWorkspaceIndex << "]";
2279 throw std::runtime_error(errss.str());
2280 }
2281}
2282
2283void FitPeaks::checkPeakIndices(std::size_t const &wi, std::size_t const &ipeak) {
2284 // check peak index
2285 if (ipeak >= m_getExpectedPeakPositions(wi).size()) {
2286 std::stringstream errss;
2287 errss << "Peak index " << ipeak << " is out of range (" << m_numPeaksToFit << ")";
2288 throw std::runtime_error(errss.str());
2289 }
2290}
2291
2292void FitPeaks::checkPeakWindowEdgeOrder(double const &left, double const &right) {
2293 if (left >= right) {
2294 std::stringstream errss;
2295 errss << "Peak window is inappropriate for workspace index: " << left << " >= " << right;
2296 throw std::runtime_error(errss.str());
2297 }
2298}
2299
2300//----------------------------------------------------------------------------------------------
2309void FitPeaks::writeFitResult(size_t wi, const std::vector<double> &expected_positions,
2310 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result) {
2311 // convert to
2312 size_t out_wi = wi - m_startWorkspaceIndex;
2313 if (out_wi >= m_outputPeakPositionWorkspace->getNumberHistograms()) {
2314 g_log.error() << "workspace index " << wi << " is out of output peak position workspace "
2315 << "range of spectra, which contains " << m_outputPeakPositionWorkspace->getNumberHistograms()
2316 << " spectra"
2317 << "\n";
2318 throw std::runtime_error("Out of boundary to set output peak position workspace");
2319 }
2320
2321 // Fill the output peak position workspace
2322 for (size_t ipeak = 0; ipeak < m_numPeaksToFit; ++ipeak) {
2323 double exp_peak_pos(expected_positions[ipeak]);
2324 double fitted_peak_pos = fit_result->getPeakPosition(ipeak);
2325 double peak_chi2 = fit_result->getCost(ipeak);
2326
2327 m_outputPeakPositionWorkspace->mutableX(out_wi)[ipeak] = exp_peak_pos;
2328 m_outputPeakPositionWorkspace->mutableY(out_wi)[ipeak] = fitted_peak_pos;
2329 m_outputPeakPositionWorkspace->mutableE(out_wi)[ipeak] = peak_chi2;
2330 }
2331
2332 // Output the peak parameters to the table workspace
2333 // check vector size
2334
2335 // last column of the table is for chi2
2336 size_t chi2_index = m_fittedParamTable->columnCount() - 1;
2337
2338 // check TableWorkspace and given FitResult
2339 if (m_rawPeaksTable) {
2340 // duplicate from FitPeakResult to table workspace
2341 // check again with the column size versus peak parameter values
2342 if (fit_result->getNumberParameters() != m_fittedParamTable->columnCount() - 3) {
2343 g_log.error() << "Peak of type (" << m_peakFunction->name() << ") has " << fit_result->getNumberParameters()
2344 << " parameters. Parameter table shall have 3 more "
2345 "columns. But not it has "
2346 << m_fittedParamTable->columnCount() << " columns\n";
2347 throw std::runtime_error("Peak parameter vector for one peak has different sizes to output "
2348 "table workspace");
2349 }
2350 } else {
2351 // effective peak profile parameters: need to re-construct the peak function
2352 if (4 + m_bkgdFunction->nParams() != m_fittedParamTable->columnCount() - 3) {
2353
2354 std::stringstream err_ss;
2355 err_ss << "Peak has 4 effective peak parameters and " << m_bkgdFunction->nParams() << " background parameters "
2356 << ". Parameter table shall have 3 more columns. But not it has " << m_fittedParamTable->columnCount()
2357 << " columns";
2358 throw std::runtime_error(err_ss.str());
2359 }
2360 }
2361
2362 // go through each peak
2363 // get a copy of peak function and background function
2364 IPeakFunction_sptr peak_function = std::dynamic_pointer_cast<IPeakFunction>(m_peakFunction->clone());
2365 size_t num_peakfunc_params = peak_function->nParams();
2366 size_t num_bkgd_params = m_bkgdFunction->nParams();
2367
2368 for (size_t ipeak = 0; ipeak < m_numPeaksToFit; ++ipeak) {
2369 // get row number
2370 size_t row_index = out_wi * m_numPeaksToFit + ipeak;
2371
2372 // treat as different cases for writing out raw or effective parametr
2373 if (m_rawPeaksTable) {
2374 // duplicate from FitPeakResult to table workspace
2375 for (size_t iparam = 0; iparam < num_peakfunc_params + num_bkgd_params; ++iparam) {
2376 size_t col_index = iparam + 2;
2377 // fitted parameter's value
2378 m_fittedParamTable->cell<double>(row_index, col_index) = fit_result->getParameterValue(ipeak, iparam);
2379 // fitted parameter's fitting error
2380 if (m_fitErrorTable) {
2381 m_fitErrorTable->cell<double>(row_index, col_index) = fit_result->getParameterError(ipeak, iparam);
2382 }
2383
2384 } // end for (iparam)
2385 } else {
2386 // effective peak profile parameter
2387 // construct the peak function
2388 for (size_t iparam = 0; iparam < num_peakfunc_params; ++iparam)
2389 peak_function->setParameter(iparam, fit_result->getParameterValue(ipeak, iparam));
2390
2391 const std::pair<double, double> peak_window = m_getPeakFitWindow(wi, ipeak);
2392 peak_function->setMatrixWorkspace(m_inputMatrixWS, wi, peak_window.first, peak_window.second);
2393
2394 // set the effective peak parameters
2395 m_fittedParamTable->cell<double>(row_index, 2) = peak_function->centre();
2396 m_fittedParamTable->cell<double>(row_index, 3) = peak_function->fwhm();
2397 m_fittedParamTable->cell<double>(row_index, 4) = peak_function->height();
2398 m_fittedParamTable->cell<double>(row_index, 5) = peak_function->intensity();
2399
2400 // background
2401 for (size_t iparam = 0; iparam < num_bkgd_params; ++iparam)
2402 m_fittedParamTable->cell<double>(row_index, 6 + iparam) =
2403 fit_result->getParameterValue(ipeak, num_peakfunc_params + iparam);
2404 }
2405
2406 // set chi2
2407 m_fittedParamTable->cell<double>(row_index, chi2_index) = fit_result->getCost(ipeak);
2408 }
2409
2410 return;
2411}
2412
2413//----------------------------------------------------------------------------------------------
2415 std::string height_name("");
2416
2417 std::vector<std::string> peak_parameters = peak_function->getParameterNames();
2418 for (const auto &parName : peak_parameters) {
2419 if (parName == "Height") {
2420 height_name = "Height";
2421 break;
2422 } else if (parName == "I") {
2423 height_name = "I";
2424 break;
2425 } else if (parName == "Intensity") {
2426 height_name = "Intensity";
2427 break;
2428 }
2429 }
2430
2431 if (height_name.empty())
2432 throw std::runtime_error("Peak height parameter name cannot be found.");
2433
2434 return height_name;
2435}
2436
2437// A client, like PDCalibration, may set a logging offset to make FitPeaks less "chatty".
2438// This method temporarily removes the logging offset and logs the message at its priority level.
2439void FitPeaks::logNoOffset(const size_t &priority, const std::string &msg) {
2440 LoggingOffsetSentry sentry(this);
2441
2442 switch (priority) {
2443 case 4: // warning
2444 g_log.warning() << msg;
2445 break;
2446 case 5: // notice
2447 g_log.notice() << msg;
2448 break;
2449 default:
2450 assert(false); // not implemented yet
2451 }
2452}
2453
2455
2456} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:542
double value
The value of the point.
Definition FitMW.cpp:51
Algorithm *const m_alg
int m_loggingOffset
double sigma
Definition GetAllEi.cpp:156
double left
double right
#define fabs(x)
Definition Matrix.cpp:22
#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.
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:76
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
Kernel::Logger & g_log
Definition Algorithm.h:423
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.
Definition Progress.h:25
TableRow represents a row in a TableWorkspace.
Definition TableRow.h:39
A property class for workspaces.
PeakFitPreCheckResult & operator+=(const PeakFitPreCheckResult &another)
Definition FitPeaks.cpp:201
size_t m_function_parameters_number
number of function parameters
Definition FitPeaks.h:53
std::vector< std::vector< double > > m_function_parameters_vector
Definition FitPeaks.h:59
PeakFitResult(size_t num_peaks, size_t num_params)
Holds all of the fitting information for a single spectrum.
Definition FitPeaks.cpp:97
double getParameterValue(size_t ipeak, size_t iparam) const
get the fitted value of a particular parameter
Definition FitPeaks.cpp:138
std::vector< std::vector< double > > m_function_errors_vector
fitted peak and background parameters' fitting error
Definition FitPeaks.h:61
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
Definition FitPeaks.cpp:150
double getParameterError(size_t ipeak, size_t iparam) const
get the fitting error of a particular parameter
Definition FitPeaks.cpp:127
void setBadRecord(size_t ipeak, const double peak_position)
The peak postition should be negative and indicates what went wrong.
Definition FitPeaks.cpp:181
Algorithms::PeakParameterHelper::EstimatePeakWidth m_peakWidthEstimateApproach
Flag for observing peak width: there are 3 states (1) no estimation (2) from 'observation' (3) calcul...
Definition FitPeaks.h:316
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
Definition FitPeaks.h:290
void generateFittedParametersValueWorkspaces()
Generate output workspaces.
void setupParameterTableWorkspace(const API::ITableWorkspace_sptr &table_ws, const std::vector< std::string > &param_names, bool with_chi2)
Set up parameter table (parameter value or error)
std::vector< double > m_peakPosTolerances
tolerances for fitting peak positions
Definition FitPeaks.h:312
API::IPeakFunction_sptr m_peakFunction
Peak profile name.
Definition FitPeaks.h:263
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
Definition FitPeaks.h:247
API::MatrixWorkspace_sptr m_fittedPeakWS
matrix workspace contained calcalated peaks+background from fitted result it has same number of spect...
Definition FitPeaks.h:259
API::ITableWorkspace_const_sptr m_profileStartingValueTable
table workspace for profile parameters' starting value
Definition FitPeaks.h:329
std::string m_minimizer
Minimzer.
Definition FitPeaks.h:270
API::IBackgroundFunction_sptr m_linearBackgroundFunction
Linear background function for high background fitting.
Definition FitPeaks.h:267
bool m_peakPosTolCase234
peak positon tolerance case b, c and d
Definition FitPeaks.h:349
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, std::vector< size_t > &lastGoodPeakSpectra, const std::shared_ptr< FitPeaksAlgorithm::PeakFitPreCheckResult > &pre_check_result)
fit peaks in a same spectrum
std::map< std::string, std::string > validateInputs() override
Validate inputs.
Definition FitPeaks.cpp:463
double m_peakWidthPercentage
flag to estimate peak width from
Definition FitPeaks.h:302
API::IBackgroundFunction_sptr m_bkgdFunction
Background function.
Definition FitPeaks.h:265
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
Definition FitPeaks.cpp:841
std::vector< std::shared_ptr< FitPeaksAlgorithm::PeakFitResult > > fitPeaks()
suites of method to fit peaks
Definition FitPeaks.cpp:992
double m_minPeakHeight
minimum peak height without background and it also serves as the criteria for observed peak parameter
Definition FitPeaks.h:336
std::string m_costFunction
Cost function.
Definition FitPeaks.h:272
void processInputFunctions()
process inputs for peak and background functions
Definition FitPeaks.cpp:649
void processInputPeakTolerance()
process inputs about fitted peak positions' tolerance
Definition FitPeaks.cpp:902
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.
Definition FitPeaks.cpp:547
void init() override
Init.
Definition FitPeaks.cpp:271
void logNoOffset(const size_t &priority, const std::string &msg)
std::size_t m_numSpectraToFit
total number of spectra to be fit
Definition FitPeaks.h:310
std::vector< std::string > m_peakParamNames
input peak parameters' names
Definition FitPeaks.h:324
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
Definition FitPeaks.cpp:702
std::size_t m_numPeaksToFit
the number of peaks to fit in all spectra
Definition FitPeaks.h:292
std::size_t m_startWorkspaceIndex
start index
Definition FitPeaks.h:306
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)
std::string getPeakHeightParameterName(const API::IPeakFunction_const_sptr &peak_function)
Get the parameter name for peak height (I or height or etc)
API::ITableWorkspace_sptr m_fittedParamTable
output analysis workspaces table workspace for fitted parameters
Definition FitPeaks.h:250
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.
Definition FitPeaks.cpp:960
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
Definition FitPeaks.h:345
std::vector< double > m_initParamValues
input peak parameters' starting values corresponding to above peak parameter names
Definition FitPeaks.h:327
std::vector< double > m_peakCenters
Designed peak positions and tolerance.
Definition FitPeaks.h:289
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
Definition FitPeaks.h:320
std::function< std::pair< double, double >(std::size_t const &, std::size_t const &)> m_getPeakFitWindow
Definition FitPeaks.h:296
API::MatrixWorkspace_sptr m_inputMatrixWS
mandatory input and output workspaces
Definition FitPeaks.h:242
std::vector< size_t > m_initParamIndexes
input starting parameters' indexes in peak function
Definition FitPeaks.h:286
bool m_fitPeaksFromRight
Fit from right or left.
Definition FitPeaks.h:274
std::function< std::vector< double >(std::size_t const &)> m_getExpectedPeakPositions
Definition FitPeaks.h:295
bool m_rawPeaksTable
flag to show that the pamarameters in table are raw parameters or effective parameters
Definition FitPeaks.h:255
void processInputs()
process inputs (main and child algorithms)
Definition FitPeaks.cpp:568
void checkWorkspaceIndices(std::size_t const &)
Get the expected peak's position.
void checkPeakWindowEdgeOrder(double const &, double const &)
int m_fitIterations
Fit iterations.
Definition FitPeaks.h:276
double numberCounts(size_t iws)
sum up all counts in histogram
API::MatrixWorkspace_const_sptr m_peakWindowWorkspace
Definition FitPeaks.h:321
bool m_uniformProfileStartingValue
flag for profile startng value being uniform or not
Definition FitPeaks.h:331
API::ITableWorkspace_sptr m_fitErrorTable
table workspace for fitted parameters' fitting error. This is optional
Definition FitPeaks.h:252
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)
Definition FitPeaks.h:308
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.
Definition Exception.h:145
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.
Definition Logger.cpp:145
void notice(const std::string &msg)
Logs at notice level.
Definition Logger.cpp:126
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
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
Definition IFunction.h:743
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.
Definition IValidator.h:26
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.
Definition EmptyValues.h:24
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition EmptyValues.h:42
STL namespace.
API::IBackgroundFunction_sptr bkgdfunction
Definition FitPeaks.h:35
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54
Functor to accumulate a sum of squares.