\(\renewcommand\AA{\unicode{x212B}}\)

FitPeaks v1

../_images/FitPeaks-v1_dlg.png

FitPeaks dialog.

Summary

Fit one or multiple peaks in all spectra of a given workspace

Properties

Name Direction Type Default Description
InputWorkspace Input MatrixWorkspace Mandatory Name of the input workspace for peak fitting.
OutputWorkspace Output MatrixWorkspace Mandatory Name of the output workspace containing peak centers for fitting offset.The output workspace is point data.Each workspace index corresponds to a spectrum. Each X value ranges from 0 to N-1, where N is the number of peaks to fit. Each Y value is the peak position obtained by peak fitting. Negative value is used for error signals. -1 for data is zero; -2 for maximum value is smaller than specified minimum value.and -3 for non-converged fitting.
StartWorkspaceIndex Input number Optional Starting workspace index for fit
StopWorkspaceIndex Input number Optional Last workspace index to fit (which is included). If a value larger than the workspace index of last spectrum, then the workspace index of last spectrum is used.
PeakCenters Input dbl list   List of peak centers to fit against.
PeakCentersWorkspace Input MatrixWorkspace   MatrixWorkspace containing peak centers
PeakFunction Input string Gaussian Use of a BackToBackExponential profile is only reccomended if the coeficients to calculate A and B are defined in the instrument Parameters.xml file. Allowed values: [‘BackToBackExponential’, ‘Bk2BkExpConvPV’, ‘DeltaFunction’, ‘ElasticDiffRotDiscreteCircle’, ‘ElasticDiffSphere’, ‘ElasticIsoRotDiff’, ‘ExamplePeakFunction’, ‘Gaussian’, ‘IkedaCarpenterPV’, ‘Lorentzian’, ‘PseudoVoigt’, ‘Voigt’]
BackgroundType Input string Linear Type of Background. Allowed values: [‘Flat’, ‘Linear’, ‘Quadratic’]
FitWindowBoundaryList Input dbl list   List of left boundaries of the peak fitting window corresponding to PeakCenters.
FitPeakWindowWorkspace Input MatrixWorkspace   MatrixWorkspace for of peak windows
PeakWidthPercent Input number Optional The estimated peak width as a percentage of the d-spacing of the center of the peak. Value must be less than 1.
PeakParameterNames Input str list   List of peak parameters’ names
PeakParameterValues Input dbl list   List of peak parameters’ value
PeakParameterValueTable Input TableWorkspace   Name of the an optional workspace, whose each column corresponds to given peak parameter names, and each row corresponds to a subset of spectra.
FitFromRight Input boolean True Flag for the order to fit peaks. If true, peaks are fitted from rightmost;Otherwise peaks are fitted from leftmost.
Minimizer Input string Levenberg-Marquardt Minimizer to use for fitting. Allowed values: [‘BFGS’, ‘Conjugate gradient (Fletcher-Reeves imp.)’, ‘Conjugate gradient (Polak-Ribiere imp.)’, ‘Damped GaussNewton’, ‘FABADA’, ‘Levenberg-Marquardt’, ‘Levenberg-MarquardtMD’, ‘Simplex’, ‘SteepestDescent’, ‘Trust Region’]
CostFunction Input string Least squares Cost functions. Allowed values: [‘Least squares’, ‘Rwp’]
MaxFitIterations Input number 50 Maximum number of function fitting iterations.
FindBackgroundSigma Input number 1 Multiplier of standard deviations of the variance for convergence of peak elimination. Default is 1.0.
HighBackground Input boolean True Flag whether the data has high background comparing to peaks’ intensities. For example, vanadium peaks usually have high background.
PositionTolerance Input dbl list   List of tolerance on fitted peak positions against given peak positions.If there is only one value given, then
MinimumPeakHeight Input number 0 Minimum peak height such that all the fitted peaks with height under this value will be excluded.
ConstrainPeakPositions Input boolean True If true peak position will be constrained by estimated positions (highest Y value position) and the peak width either estimted by observation or calculate.
FittedPeaksWorkspace Output MatrixWorkspace   Name of the output matrix workspace with fitted peak. This output workspace have the same dimesion as the input workspace.The Y values belonged to peaks to fit are replaced by fitted value. Values of estimated background are used if peak fails to be fit.
OutputPeakParametersWorkspace Output TableWorkspace Mandatory Name of table workspace containing all fitted peak parameters.
OutputParameterFitErrorsWorkspace Output TableWorkspace   Name of workspace containing all fitted peak parameters’ fitting error.It must be used along with FittedPeaksWorkspace and RawPeakParameters (True)
RawPeakParameters Input boolean True false generates table with effective centre/width/height parameters. true generates a table with peak function parameters

Description

This algorithm fits a set of specified peaks in a set of specified spectra in a MatrixWorkspace, returning a list of the successfully fitted peaks along with optional output for fitting information.

The list of candidate peaks found is passed to a fitting routine and those that are successfully fitted are kept and returned in the output workspace (and logged at information level). The output TableWorkspace contains the following columns, which reflect the fact that the peak has been fitted to a Gaussian atop a linear background: spectrum, centre, width, height, backgroundintercept & backgroundslope.

Assumption

It is assumed that the profile parameters of a peak at a given d-spacing, such as the peak width, are correlated between adjacent spectra (in index).

Required pre-knowledge

The following information are required.

  • Assumed position of each peak
  • Peak profile function
  • Background type (flat or linear)
  • Starting values of peak parameters

For better results

  • Peak fit window: this is an option
    1. specified by user
    2. figured out by FindPeakBackground, and peak FWHM estimated from the height and integrated intensity in the window assuming a Gaussian distribution (overwriting the peak profile parameters initialised from the reuslt of the previous spectrum).
    3. if the workspace is in unit dSpacing and \(\Delta d/d\)
  • Peak position tolerance: there could be three cases for how the peak position tolerance is specified.
    1. specified by user
    2. defined by peak windows
    3. half distance to the neighboring peak (if not (a) and not (b))
    4. whole range of X-axis (if there is one and only one peak in a spectrum)

Description of algorithm

For each spectrum, it is assumed that there are N peaks to fit. The fitting starts from either the peak at the smallest X value or the largest X values depending on the user’s input. The first peak will be fit with the starting value provided by the user. except background and peak center, which will be determiend by observation.

Background

Linear background is used for fitting peaks. The background parameters will be estimated via observation.

Estimation of values of peak profiles

Peak height, center, and FWHM and peak center are estimated by observing the maximum value within peak fit window and calculating the first two moments.

When using peak profiles that do not have a one-to-one mapping between effective and raw parameters, you may need to supply starting information for more of the parameters. This can be done by specifying PeakParameterNames and PeakParameterValues for some or all of the parameters (e.g. Mixing for a PseudoVoigt) which will be used for the first peak fit in each spectrum. To supply all parameters, PeakParameterNames and PeakParameterValueTable can be used. WARNING Partially specifying peak parameters such as height and position will be overwritten by observed values.

Subalgorithms used

FitPeaks uses the Fit algorithm to fit each single peak. FitPeaks uses the FindPeakBackground algorithm to estimate the background of each peak.

Inputs

The inputs tends to be general enough for various use cases.

Limitations on Partial Spectra

  • For partial spectra peak fitting, the spectra must be consecutive.

Peak positions

One of only one of the following will be taken.

  • A MatrixWorkspace
    • Number of spectra shall be the same as the number of spectra of the workspace containing peaks to fit for. Or the number of spectra is the same as the number of spectra of the input workspace.
    • X value is the index of the peak.
    • Y value is the position of the peaks to fit.
    • Peak centers are stored in m_peakCenterWorkspace.
    • Spectra can be a subset of all spectra because FitPeaks can work on partial spectra.
  • An array of double as the positions of the peaks to fit.
    • Peak centers are stored in m_peakCenters

Peaks’ positions must be given in ascending order

Parameter FitFromRight deontes start fits from right most peak rather than left most peak.

Fit Window

There are two input parameters that are associated with fitting window.

  • FitWindowBoundaryList
  • FitPeakWindowWorkspace

If FitWindows is defined, then a peak’s range to fit (i.e., x-min and x-max) is confined by this window.

If FitWindows is defined, starting peak centres are NOT user’s input, but found by highest value within peak window. (Is this correct???)

Further down the road, here are the fitting setup that can be affected.

  • Peak positions are uniform among all spectra
    • Peak window information will be retrieved from m_peakWindowVector
  • Peak positions are different among spectra.
    • Peak windown information will be retrieved from m_peakWindowWorkspace

Tolerance on Fitting Peaks Positions

Tolerance will be always checked!

  • Uniform tolerance
  • Non-uniform tolerance
  • Case 2, 3 and 4

Algorithm Configurations

  • Peak profile starting value will be given as
    • an array PeakParameterValues such that the starting values are uniform among all spectra.
    • a table (workspace) PeakParameterValueTable such that the starting values are not necessary same among all spectra.

Calculation of starting value of peak profile and background parameters

FitPeaks supports estimating peak parameter names as starting values.

Workflow

  1. Call FindPeakBackground to estimate the background of peak with a numerical approach.
    • Some tests have made to show that most time FindPeakBackground failed to do a valid estimation. Therefore this feature is temporarily disabled.
  2. If FindPeakBackground fails, estimate-peak-background will be used for simple approximation.
  3. Estimate the peak parameter, estimate-peak-parameter, by using the estimated peak background obtained in either step 1 or step 2.
  4. Estimate the peak range, which is used to constrain the peak position in fitting, by using the left FWHM and right FWHM from step 3.

Estimate background

Estimate-peak-background takes peak fit window for pre-knowledge, and calculate a and b in the linear background function.

The algorithm is 1. Find the left and right N points respectively, average both x and y value 2. Use \((\bar{x}_1, \bar{y}_1)\) and \((\bar{x}_2, \bar{y}_2)\) to calculate a and b in \(y = a\cdot x + b\)

Estimate peak parameters

Estimate-peak-parameters requires background parameters being estimated.

Here is the approach to estimate the peak parameters

  1. Remove background.
  2. Find maximum Y value as the observed peak center and peak height \(H_{obs}\).
  3. Check peak height with user-specified minimum height and peak center that must be at least more than 3 data points away from the boundary of fit window.
  4. Find the left and right FWHM by searching \(x_i\) and \(x_{i+1}\) such that \(H_{obs}\) is between \(y_i\) and \(y_{i+1}\).

Estimate peak range

Estimate-peak-range requires inputs including expected peak center, fit window and estimated right and left FWHM. It will output the left and right boundary of the peak such that the background can be fit by excluding the peak.

  1. Peak range is defined as \(x_0 \pm 6 \cdot w\), where w is half of FWHM for either left or right half of peak.
  2. Check the number of background points out of peak range at the left and right side respectively. It is required to have at least 3 background points at either side, i.e., \(min(3, \frac{i_{x0} - i_{min}}{6})\) for left side.

Fit peak with high background

Step 1

Reduce the background by finding a linear function \(B_i = a\cdot x_i + b\), such that \(\sum_i (Y_i - B_i)\) is minimum while any \(Y_i - B_i\) is non-negative.

This approach is good for any background close to linear within the fit window.

Step 2

With the background reduced in step 1, it will be more reliable to estimate the peak’s FWHM via observation.

Step 3

Fit peak… ………..

Step 3

Get the peak range (by estimate-peak-range) and fit the background with FitMultiDomain to fit background.

Step 4

Remove the background and fit peak!

Outputs

Algorithm FitPeaks is designed for various purposes including but not limited to vanadium peak striping and fitting diamond peaks to calibrate detectors’ positions. On the other hand, due to the complexity in peak fitting, users prefer to check the fitting results. Therefore, FitPeaks supports various fexible and informative outputs.

OutputWorkspaces

OutputWorkspace

It is a MatrixWorkspace containing the peak positions expected and fitted.

  • The output workspace has N spectra corresponding to the spectra that are specified by user via MinimumWorkspaceIndex and MaximumWorkspaceIndex.
  • If there are m peaks that are required to fit for, then each spectrum in the output workspace has m data points.
  • In each spectrum, x(i) is the expected position of i-th peak; y(i) is the fitted position of i-th peak; and e(i) is the cost from fitting.
  • There are several cases that the fitting could fail. A negative peak position y(i) combined with e(i) equal to DBL_MAX denote such failure.
  • Cause of fitting failure is denoted by different negative value of y(i) - -1: empty spectrum - -2: spectrum with too few counts - -3: peak is low - -4: TODO : find out the answer - -5: TODO : find out the answer

OutputPeakParametersWorkspace

It is an output MatrixWorkspace containing function parameters’ fitted values for all peaks that are specified to fit. The order of the peaks will be exactly the sequence of peaks as the order of the given positions of peaks.

If user specifies a subset of spectra to fit, this TableWorksapce will only contain function parameters’ value that corresponds to the spectra that are fitted.

OutputParameterFitErrorsWorkspace

It is an optional output MatrixWorkspace containing function parameters’ fitting error, i.e., uncertainties, for all peaks that are specified to fit. The order of the peaks will be exactly the sequence of peaks as the order of the given positions of peaks.

If user specifies a subset of spectra to fit, this TableWorksapce will only contain function parameters’ uncertainties that corresponds to the spectra that are fitted.

It has one less column than OutputPeakParametersWorkspace, which is chi2.

FittedPeaksWorkspace

It is an optional output MatrixWorkspace containing the peaks calculated from fitting result.

FittingCostWorkspace

It is a MatrixWorkspace recording the cost of each peak that is fitted. It is in the exactly same order as the given positions of peaks to fit. Its X values store the fitted peak positions and Y values are for \(\chi^2\).

If a peak’s fitting is bad, then the peak position will be its proposed peak position, while its \(\chi^2\) shall be some special value.

FittedPeaksWorkspace

It is an optional output MatrixWorkspace.

For each spectrum, in each fit window, the Y values will be replaced by the calculated peak and background value. If fitting is bad, then only background is calculated.

Usage

Example - Find a single peak:

ws = CreateSampleWorkspace(Function="User Defined",
                           UserDefinedFunction="name=LinearBackground, A0=0.3;name=Gaussian, PeakCentre=5, Height=10, Sigma=0.7",
                           NumBanks=1, BankPixelWidth=1, XMin=0, XMax=10, BinWidth=0.1)

FitPeaks(InputWorkspace='ws', PeakCenters='5.1', FitWindowBoundaryList='0,10', OutputPeakParametersWorkspace='fitted_params',
         BackgroundType='Linear', FittedPeaksWorkspace='fitted', OutputWorkspace='peakpositions')

peakposws = mtd['peakpositions']
param_ws = mtd['fitted_params']
row = param_ws.row(0)

# output
print ('Fitted peak position: {0:.5f}'.format(peakposws.readY(0)[0]))
print ("Peak 0  Centre: {0:.5f}, width: {1:.5f}, height: {2:.5f}".format(row["PeakCentre"], row["Sigma"], row["Height"]))

# clean up workspaces
DeleteWorkspace(Workspace='fitted')
DeleteWorkspace(Workspace='fitted_params')
DeleteWorkspace(Workspace='ws')
DeleteWorkspace(Workspace='peakpositions')

Output:

Fitted peak position: 5.05000
Peak 0  Centre: 5.05000, width: 0.70000, height: 10.00000

Example - Fit peaks on high background (vanadium):

# load a 4 spectra workspace
ws = Load("PG3_733.nxs")

van_peak_centers = "0.5044,0.5191,0.5350,0.5526,0.5936,0.6178,0.6453,0.6768,0.7134,0.7566,0.8089,0.8737,0.9571,1.0701,1.2356,1.5133,2.1401"
FitPeaks(InputWorkspace=ws, StartWorkspaceIndex=0, StopWorkspaceIndex=3,PeakCenters=van_peak_centers,
           FitFromRight=True,HighBackground=True,
           BackgroundType='Quadratic',
           PeakWidthPercent=0.008,
           OutputWorkspace='PG3_733_peak_positions',OutputPeakParametersWorkspace='PG3_733_peak_params',
           FittedPeaksWorkspace='PG3_733_fitted_peaks')

PG3_733_peak_positions = mtd["PG3_733_peak_positions"]
ep0 = PG3_733_peak_positions.readX(0)[-1]
ep1 = PG3_733_peak_positions.readX(0)[-2]
ep2 = PG3_733_peak_positions.readX(0)[-3]
fp0 = PG3_733_peak_positions.readY(0)[-1]
fp1 = PG3_733_peak_positions.readY(0)[-2]
fp2 = PG3_733_peak_positions.readY(0)[-3]

# print data
print ('Spectrum 1: Expected right most 3 peaks at {0:.7f}, {1:.7f}, {2:.7f}'.format(ep0, ep1, ep2))
print ('Spectrum 1: Found    right most 3 peaks at {0:.7f}, {1:.7f}, {2:.7f}'.format(fp0, fp1, fp2))

# delete workspaces
DeleteWorkspace(Workspace='PG3_733_peak_positions')
DeleteWorkspace(Workspace='PG3_733_fitted_peaks')
DeleteWorkspace(Workspace='PG3_733_peak_params')
DeleteWorkspace(Workspace='ws')

Output:

Spectrum 1: Expected right most 3 peaks at 2.1401000, 1.5133000, 1.2356000
Spectrum 1: Found    right most 3 peaks at 2.1485553, 1.5190662, 1.2404027

Example - Fit back-to-back exponential peaks (Vulcan diamond):

# load data
Load(Filename="vulcan_diamond.nxs", OutputWorkspace="diamond_3peaks")

FitPeaks(InputWorkspace="diamond_3peaks", StartWorkspaceIndex=0, StopWorkspaceIndex=5,
       PeakCenters="0.6867, 0.728299, 0.89198, 1.0758",
       PeakFunction="BackToBackExponential", BackgroundType="Linear",
       FitWindowBoundaryList="0.67, 0.709, 0.71, 0.76, 0.87, 0.92, 1.05, 1.1",
       PeakParameterNames="I, A, B, X0, S",
       PeakParameterValues="2.5e+06, 5400, 1700, 1.07, 0.000355",
       FitFromRight=True,
       HighBackground=False,
       OutputWorkspace="diamond_peaks_centers",
       OutputPeakParametersWorkspace="PeakParametersWS2",
       FittedPeaksWorkspace="FittedPeaksWS2")

fitted_peak_pos_ws = mtd['diamond_peaks_centers']

# print result
for ws_index in range(0, 1):
    print ('Spectrum {0}:'.format(ws_index+1))
    for peak_index in range(2, 4):
        exp_pos = fitted_peak_pos_ws.readX(ws_index)[peak_index]
        fit_pos = fitted_peak_pos_ws.readY(ws_index)[peak_index]
        print ('Expected @ {0:.3f}  Fitted @ {1:.3f}'.format(exp_pos, fit_pos))

# clean
DeleteWorkspace(Workspace='diamond_3peaks')
DeleteWorkspace(Workspace='diamond_peaks_centers')
DeleteWorkspace(Workspace='FittedPeaksWS2')
DeleteWorkspace(Workspace='PeakParametersWS2')

Output:

Spectrum 1:
Expected @ 0.892  Fitted @ 0.892
Expected @ 1.076  Fitted @ 1.075

Categories: AlgorithmIndex | Optimization

Source

C++ header: FitPeaks.h (last modified: 2021-03-31)

C++ source: FitPeaks.cpp (last modified: 2021-03-31)