Mantid
Loading...
Searching...
No Matches
FitPeaks.h
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#pragma once
8
16#include "MantidAlgorithms/DllConfig.h"
21
22#include <utility>
23
24namespace Mantid {
25namespace HistogramData {
26class HistogramX;
27class HistogramY;
28} // namespace HistogramData
29
30namespace Algorithms {
31
32namespace FitPeaksAlgorithm {
37
39public:
40 PeakFitResult(size_t num_peaks, size_t num_params);
41 double getPeakPosition(size_t ipeak) const;
42 double getCost(size_t ipeak) const;
43 size_t getNumberParameters() const;
44 size_t getNumberPeaks() const;
45 double getParameterValue(size_t ipeak, size_t iparam) const;
46 double getParameterError(size_t ipeak, size_t iparam) const;
47 void setRecord(size_t ipeak, const double cost, const double peak_position, const FitFunction &fit_functions);
48 void setBadRecord(size_t ipeak, const double peak_position);
49 void setFunctionParameters(size_t ipeak, std::vector<double> &param_values);
50
51private:
54 // goodness of fitting
55 std::vector<double> m_costs;
56 // fitted peak positions
57 std::vector<double> m_fitted_peak_positions;
58 // fitted peak and background parameters
59 std::vector<std::vector<double>> m_function_parameters_vector;
61 std::vector<std::vector<double>> m_function_errors_vector;
62};
63
65public:
70
71public:
72 void setNumberOfSubmittedSpectrumPeaks(const size_t n);
73 void setNumberOfSubmittedIndividualPeaks(const size_t n);
74 void setNumberOfSpectrumPeaksWithLowCount(const size_t n);
75 void setNumberOfOutOfRangePeaks(const size_t n);
78 void setNumberOfPeaksWithLowSignalToNoise(const size_t n);
79 bool isIndividualPeakRejected() const;
80 std::string getReport() const;
81
82private:
83 // number of peaks submitted for spectrum fitting
85 // number of peaks submitted for individual fitting. Since some spectra might fail a pre-check, not all peaks might
86 // make it to the individual fitting
88 // number of peaks rejected as a whole spectrum due to its low signal count
90 // number of peaks rejected individually because their predicted position is out of range
92 // number of peaks rejected individually due to low signal count
94 // number of peask rejected due to not enough data points
96 // number of peaks rejected due to low signal-to-noise ratio
97 size_t m_low_snr;
98};
99} // namespace FitPeaksAlgorithm
100
101class MANTID_ALGORITHMS_DLL FitPeaks final : public API::Algorithm {
102public:
103 FitPeaks();
104
106 const std::string name() const override { return "FitPeaks"; }
107
109 const std::string summary() const override { return "Fit one or multiple peaks in all spectra of a given workspace"; }
110
112 int version() const override { return (1); }
113
115 const std::string category() const override { return "Optimization"; }
116
117 std::map<std::string, std::string> validateInputs() override;
118
122 static bool fitStatusIsConverged(const std::string &fitStatus, const bool strict);
123
124private:
126 void init() override;
128 void exec() override;
129
131 void processInputs();
133 void processInputPeakCenters();
135 void processInputPeakTolerance();
137 void processInputFunctions();
139 void processInputFitRanges();
140
142 void generateFittedParametersValueWorkspaces();
144 void generateOutputPeakPositionWS();
146 void generateCalculatedPeaksWS();
147
150 void convertParametersNameToIndex();
151
153 std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> fitPeaks();
154
156 void fitSpectrumPeaks(size_t wi, const std::vector<double> &expected_peak_centers,
157 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result,
158 std::vector<std::vector<double>> &lastGoodPeakParameters,
159 std::vector<size_t> &lastGoodPeakSpectra,
160 const std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> &pre_check_result);
161
163 bool fitBackground(const size_t &ws_index, const std::pair<double, double> &fit_window,
164 const double &expected_peak_pos, const API::IBackgroundFunction_sptr &bkgd_func);
165
166 // Peak fitting suite
167 double fitIndividualPeak(size_t wi, const API::IAlgorithm_sptr &fitter, const double expected_peak_center,
168 const double peak_pos_tolerance, const std::pair<double, double> &fitwindow,
169 const bool estimate_peak_width, const API::IPeakFunction_sptr &peakfunction,
170 const API::IBackgroundFunction_sptr &bkgdfunc,
171 const std::shared_ptr<FitPeaksAlgorithm::PeakFitPreCheckResult> &pre_check_result);
172
174 double fitFunctionSD(const API::IAlgorithm_sptr &fit, const API::IPeakFunction_sptr &peak_function,
175 const API::IBackgroundFunction_sptr &bkgd_function, const API::MatrixWorkspace_sptr &dataws,
176 size_t wsindex, const std::pair<double, double> &peak_range, const double &expected_peak_center,
177 const double peak_pos_tolerance, bool estimate_peak_width, bool estimate_background);
178
183 void recalculateErrorsWithoutConstraint(const API::IPeakFunction_sptr &peak_function,
184 const API::IBackgroundFunction_sptr &bkgd_function,
185 const API::MatrixWorkspace_sptr &dataws, size_t wsindex,
186 const std::pair<double, double> &peak_range);
187
188 double fitFunctionMD(API::IFunction_sptr fit_function, const API::MatrixWorkspace_sptr &dataws, const size_t wsindex,
189 const std::pair<double, double> &vec_xmin, const std::pair<double, double> &vec_xmax);
190
192 double fitFunctionHighBackground(const API::IAlgorithm_sptr &fit, const std::pair<double, double> &fit_window,
193 const size_t &ws_index, const double &expected_peak_center,
194 const double peak_pos_tolerance, bool observe_peak_shape,
195 const API::IPeakFunction_sptr &peakfunction,
196 const API::IBackgroundFunction_sptr &bkgdfunc);
197
198 void setupParameterTableWorkspace(const API::ITableWorkspace_sptr &table_ws,
199 const std::vector<std::string> &param_names, bool with_chi2);
200
202 void histRangeToIndexBounds(size_t iws, const std::pair<double, double> &range, size_t &left_index,
203 size_t &right_index);
204
206 size_t histRangeToDataPointCount(size_t iws, const std::pair<double, double> &range);
207
209 void getRangeData(size_t iws, const std::pair<double, double> &range, std::vector<double> &vec_x,
210 std::vector<double> &vec_y, std::vector<double> &vec_e);
211
213 double numberCounts(size_t iws);
214
216 double numberCounts(size_t iws, const std::pair<double, double> &range);
217
219 double calculateSignalToNoiseRatio(size_t iws, const std::pair<double, double> &range,
220 const API::IBackgroundFunction_sptr &bkgd_function);
221
222 API::MatrixWorkspace_sptr createMatrixWorkspace(const std::vector<double> &vec_x, const std::vector<double> &vec_y,
223 const std::vector<double> &vec_e);
224
225 bool decideToEstimatePeakParams(const bool firstPeakInSpectrum, const size_t wsindex,
226 const API::IPeakFunction_sptr &peak_function);
227
229 bool processSinglePeakFitResult(size_t wsindex, size_t peakindex, const double cost,
230 const std::vector<double> &expected_peak_positions,
231 const FitPeaksAlgorithm::FitFunction &fitfunction,
232 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result);
233
235 void calculateFittedPeaks(const std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> &fit_results);
236
237 double calculateSignalToSigmaRatio(const size_t &iws, const std::pair<double, double> &peakWindow,
238 const API::IPeakFunction_sptr &peakFunction);
239
241 std::string getPeakHeightParameterName(const API::IPeakFunction_const_sptr &peak_function);
242
244 void processOutputs(std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> fit_result_vec);
245
247 void writeFitResult(size_t wi, const std::vector<double> &expected_positions,
248 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result);
249
252 bool isObservablePeakProfile(const std::string &peakprofile);
253
254 // log a message disregarding the current logging offset
255 void logNoOffset(const size_t &priority, const std::string &msg);
256
257 // create a Fit child alg
258 API::IAlgorithm_sptr createChildFit();
259
260 //------- Workspaces-------------------------------------
267 API::MatrixWorkspace_sptr m_outputPeakPositionWorkspace; // output workspace for peak positions
280
281 //-------- Functions ------------------------------------------------------
288
290 std::string m_minimizer;
292 std::string m_costFunction;
295 bool m_strictConvergence{true};
300 // Copy the peak parameters from the last successfully fit peak
302 // If true, parameters marked as fixed on the peak function (e.g. parameters
303 // calculated from the instrument geometry) remain fixed during fitting.
304 // If false (default), such parameters are unfixed so they can be refined.
306
307 //-------- Input param init values --------------------------------
309 std::vector<size_t> m_initParamIndexes;
310
312 std::vector<double> m_peakCenters;
315 std::size_t m_numPeaksToFit;
317
318 std::function<std::vector<double>(std::size_t const &)> m_getExpectedPeakPositions;
319 std::function<std::pair<double, double>(std::size_t const &, std::size_t const &)> m_getPeakFitWindow;
320 void checkWorkspaceIndices(std::size_t const &);
321 void checkPeakIndices(std::size_t const &, std::size_t const &);
322 void checkPeakWindowEdgeOrder(double const &, double const &);
323
326
327 //--------- Fitting range -----------------------------------------
333 std::size_t m_numSpectraToFit;
335 std::vector<double> m_peakPosTolerances;
336
344 bool m_constrainByPositionTolerance{false};
349 bool m_fractionalPositionTolerance{false};
354 bool m_calculateUnconstrainedErrors{false};
355
357 std::vector<std::vector<double>> m_peakWindowVector;
359
361 std::vector<std::string> m_peakParamNames;
364 std::vector<double> m_initParamValues;
369
370 // Criteria for fitting peaks
374
375 // Criteria for rejecting non-peaks or weak peaks from fitting
378
380
383
384 //----- Result criterias ---------------
387};
388
389} // namespace Algorithms
390} // namespace Mantid
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:76
PeakFitPreCheckResult & operator+=(const PeakFitPreCheckResult &another)
Definition FitPeaks.cpp:208
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
double getParameterValue(size_t ipeak, size_t iparam) const
get the fitted value of a particular parameter
Definition FitPeaks.cpp:145
void setFunctionParameters(size_t ipeak, std::vector< double > &param_values)
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:157
double getParameterError(size_t ipeak, size_t iparam) const
get the fitting error of a particular parameter
Definition FitPeaks.cpp:134
void setBadRecord(size_t ipeak, const double peak_position)
The peak postition should be negative and indicates what went wrong.
Definition FitPeaks.cpp:188
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:339
API::MatrixWorkspace_const_sptr m_peakCenterWorkspace
Definition FitPeaks.h:313
std::vector< double > m_peakPosTolerances
tolerances for fitting peak positions
Definition FitPeaks.h:335
API::IPeakFunction_sptr m_peakFunction
Peak profile name.
Definition FitPeaks.h:283
API::MatrixWorkspace_sptr m_outputPeakPositionWorkspace
output workspace for peak positions
Definition FitPeaks.h:267
API::MatrixWorkspace_sptr m_fittedPeakWS
matrix workspace contained calcalated peaks+background from fitted result it has same number of spect...
Definition FitPeaks.h:279
API::ITableWorkspace_const_sptr m_profileStartingValueTable
table workspace for profile parameters' starting value
Definition FitPeaks.h:366
std::string m_minimizer
Minimzer.
Definition FitPeaks.h:290
API::IBackgroundFunction_sptr m_linearBackgroundFunction
Linear background function for high background fitting.
Definition FitPeaks.h:287
bool m_peakPosTolCase234
peak positon tolerance case b, c and d
Definition FitPeaks.h:386
double m_peakWidthPercentage
flag to estimate peak width from
Definition FitPeaks.h:325
API::IBackgroundFunction_sptr m_bkgdFunction
Background function.
Definition FitPeaks.h:285
int version() const override
Algorithm's version.
Definition FitPeaks.h:112
const std::string summary() const override
Summary of algorithms purpose.
Definition FitPeaks.h:109
const std::string category() const override
Algorithm's category for identification.
Definition FitPeaks.h:115
double m_minPeakHeight
minimum peak height without background and it also serves as the criteria for observed peak parameter
Definition FitPeaks.h:373
std::string m_costFunction
Cost function.
Definition FitPeaks.h:292
std::size_t m_numSpectraToFit
total number of spectra to be fit
Definition FitPeaks.h:333
std::vector< std::string > m_peakParamNames
input peak parameters' names
Definition FitPeaks.h:361
std::size_t m_numPeaksToFit
the number of peaks to fit in all spectra
Definition FitPeaks.h:315
std::size_t m_startWorkspaceIndex
start index
Definition FitPeaks.h:329
const std::string name() const override
Algorithm's name.
Definition FitPeaks.h:106
API::ITableWorkspace_sptr m_fittedParamTable
output analysis workspaces table workspace for fitted parameters
Definition FitPeaks.h:270
DataObjects::EventWorkspace_const_sptr m_inputEventWS
event workspace for input
Definition FitPeaks.h:265
bool m_highBackground
flag for high background
Definition FitPeaks.h:382
std::vector< double > m_initParamValues
input peak parameters' starting values corresponding to above peak parameter names
Definition FitPeaks.h:364
std::vector< double > m_peakCenters
Designed peak positions and tolerance.
Definition FitPeaks.h:312
std::vector< std::vector< double > > m_peakWindowVector
peak windows
Definition FitPeaks.h:357
std::function< std::pair< double, double >(std::size_t const &, std::size_t const &)> m_getPeakFitWindow
Definition FitPeaks.h:319
API::MatrixWorkspace_sptr m_inputMatrixWS
mandatory input and output workspaces
Definition FitPeaks.h:262
std::vector< size_t > m_initParamIndexes
input starting parameters' indexes in peak function
Definition FitPeaks.h:309
bool m_fitPeaksFromRight
Fit from right or left.
Definition FitPeaks.h:297
std::function< std::vector< double >(std::size_t const &)> m_getExpectedPeakPositions
Definition FitPeaks.h:318
bool m_rawPeaksTable
flag to show that the pamarameters in table are raw parameters or effective parameters
Definition FitPeaks.h:275
int m_fitIterations
Fit iterations.
Definition FitPeaks.h:299
API::MatrixWorkspace_const_sptr m_peakWindowWorkspace
Definition FitPeaks.h:358
bool m_uniformProfileStartingValue
flag for profile startng value being uniform or not
Definition FitPeaks.h:368
API::ITableWorkspace_sptr m_fitErrorTable
table workspace for fitted parameters' fitting error. This is optional
Definition FitPeaks.h:272
std::size_t m_stopWorkspaceIndex
stop index (workspace index of the last spectrum included)
Definition FitPeaks.h:331
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 ITableWorkspace > ITableWorkspace_const_sptr
shared pointer to Mantid::API::ITableWorkspace (const version)
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:748
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
Helper class which provides the Collimation Length for SANS instruments.
API::IBackgroundFunction_sptr bkgdfunction
Definition FitPeaks.h:35