Peaks Module
Models and functions for peak fitting
@author: Dr A. Vamvakeros
- nDTomo.analysis.peaks.Cagliotti(twotheta, U, V, W)[source]
Computes the FWHM of a peak as a function of 2θ using the Cagliotti function.
- Parameters:
- Returns:
FWHM values computed using the Cagliotti function.
- Return type:
numpy.ndarray or float
- nDTomo.analysis.peaks.CagliottiLS(pars, tantth, y)[source]
Computes residuals for least-squares fitting of the Cagliotti function.
- Parameters:
pars (list or numpy.ndarray) – Parameters of the Cagliotti function: - pars[0]: U parameter. - pars[1]: V parameter. - pars[2]: W parameter.
tantth (numpy.ndarray) – Tangent of half the two-theta values.
y (numpy.ndarray) – Observed FWHM values.
- Returns:
Residuals between the observed FWHM values and the model.
- Return type:
numpy.ndarray
- nDTomo.analysis.peaks.CombGaussians(x, pars)[source]
Computes a combination of multiple Gaussian functions with a linear background.
- Parameters:
x (numpy.ndarray) – Input value(s) for the Gaussian functions.
pars (list or numpy.ndarray) – Parameter vector for the Gaussian functions and linear background: - pars[0:n_gaussians]: Amplitudes of the Gaussian peaks. - pars[n_gaussians:2*n_gaussians]: Centers of the Gaussian peaks. - pars[2*n_gaussians:3*n_gaussians]: Sigmas of the Gaussian peaks. - pars[-2]: Slope of the linear background. - pars[-1]: Intercept of the linear background.
- Returns:
Output of the combined Gaussian functions with the linear background.
- Return type:
numpy.ndarray
- nDTomo.analysis.peaks.CombGaussiansLS(pars, x, y)[source]
Computes the residuals for least-squares fitting of a combination of Gaussian functions with a linear background.
- Parameters:
pars (list or numpy.ndarray) – Parameter vector for the Gaussian functions and linear background: - pars[0:n_gaussians]: Amplitudes of the Gaussian peaks. - pars[n_gaussians:2*n_gaussians]: Centers of the Gaussian peaks. - pars[2*n_gaussians:3*n_gaussians]: Sigmas of the Gaussian peaks. - pars[-2]: Slope of the linear background. - pars[-1]: Intercept of the linear background.
x (numpy.ndarray) – Input values for the Gaussian functions.
y (numpy.ndarray) – Observed data to fit.
- Returns:
Residuals between the observed data and the model.
- Return type:
numpy.ndarray
- nDTomo.analysis.peaks.CombGaussiansLSq(pars, x, y)[source]
Computes the residuals for least-squares fitting of a combination of Gaussian functions with a quadratic background.
- Parameters:
pars (list or numpy.ndarray) – Parameter vector for the Gaussian functions and quadratic background: - pars[0:n_gaussians]: Amplitudes of the Gaussian peaks. - pars[n_gaussians:2*n_gaussians]: Centers of the Gaussian peaks. - pars[2*n_gaussians:3*n_gaussians]: Sigmas of the Gaussian peaks. - pars[-3]: Coefficient for the quadratic term in the background. - pars[-2]: Coefficient for the linear term in the background. - pars[-1]: Constant term in the background.
x (numpy.ndarray) – Input values for the Gaussian functions.
y (numpy.ndarray) – Observed data to fit.
- Returns:
Residuals between the observed data and the model.
- Return type:
numpy.ndarray
- nDTomo.analysis.peaks.CombGaussiansq(x, pars)[source]
Computes a combination of multiple Gaussian functions with a quadratic background.
- Parameters:
x (numpy.ndarray) – Input value(s) for the Gaussian functions.
pars (list or numpy.ndarray) – Parameter vector for the Gaussian functions and quadratic background: - pars[0:n_gaussians]: Amplitudes of the Gaussian peaks. - pars[n_gaussians:2*n_gaussians]: Centers of the Gaussian peaks. - pars[2*n_gaussians:3*n_gaussians]: Sigmas of the Gaussian peaks. - pars[-3]: Coefficient for the quadratic term in the background. - pars[-2]: Coefficient for the linear term in the background. - pars[-1]: Constant term in the background.
- Returns:
Output of the combined Gaussian functions with the quadratic background.
- Return type:
numpy.ndarray
- nDTomo.analysis.peaks.G(x, center=0.0, sigma=1.0)[source]
Computes a normalized Gaussian function.
This function is a special case of the Gaussian function with a default amplitude of 1.0.
- Parameters:
- Returns:
Output of the normalized Gaussian function.
- Return type:
numpy.ndarray or float
- nDTomo.analysis.peaks.Gaussian(x, amplitude=1.0, center=0.0, sigma=1.0)[source]
Computes a Gaussian function.
- Parameters:
- Returns:
Output of the Gaussian function.
- Return type:
numpy.ndarray or float
- nDTomo.analysis.peaks.GetPeaksInfo(res)[source]
Extracts peak information (areas, positions, and FWHMs) from a parameter vector.
- nDTomo.analysis.peaks.L(x, center=0.0, sigma=1.0)[source]
Computes a normalized Lorentzian function.
- Parameters:
- Returns:
Output of the normalized Lorentzian function.
- Return type:
numpy.ndarray or float
- nDTomo.analysis.peaks.Lorentzian(x, amplitude=1.0, center=0.0, sigma=1.0)[source]
Computes a Lorentzian function.
- Parameters:
- Returns:
Output of the Lorentzian function.
- Return type:
numpy.ndarray or float
- nDTomo.analysis.peaks.PVoigt(x, amplitude=1.0, center=0.0, sigma=1.0, fraction=0.5)[source]
Computes a pseudo-Voigt function as a weighted sum of Gaussian and Lorentzian functions.
- Parameters:
x (numpy.ndarray or float) – Input value(s) for the pseudo-Voigt function.
amplitude (float, optional, default=1.0) – Amplitude of the pseudo-Voigt function.
center (float, optional, default=0.0) – Center of the peak.
sigma (float, optional, default=1.0) – Width parameter shared by both Gaussian and Lorentzian components.
fraction (float, optional, default=0.5) – Fractional contribution of the Lorentzian component. The Gaussian contribution is (1 - fraction).
- Returns:
Output of the pseudo-Voigt function.
- Return type:
numpy.ndarray or float
- nDTomo.analysis.peaks.Quadratic(x, a=0, b=0, c=0)[source]
Computes a quadratic function.
- Parameters:
- Returns:
Output of the quadratic function.
- Return type:
numpy.ndarray or float
- nDTomo.analysis.peaks.atand(x)
- nDTomo.analysis.peaks.fit_gaussian_map(data, x_axis, threshold=0.001, sigma_bounds=(0.01, 1.0))[source]
Fits a Gaussian function to the spectral dimension of a 3D dataset for every pixel.
Iterates through the spatial dimensions (x, y) of the input data and fits a Gaussian peak to the signal along the last axis. Pixels with maximum intensity below the specified threshold are skipped.
- Parameters:
data (numpy.ndarray) – Input 3D array (e.g., [x, y, channels]) containing the spectral data to be fitted.
x_axis (numpy.ndarray) – 1D array representing the x-axis values (e.g., 2-theta or energy) corresponding to the last dimension of data.
threshold (float, optional, default=1e-3) – Intensity threshold. Pixels where the maximum signal is below this value will be skipped (parameters remain 0).
sigma_bounds (tuple, optional, default=(0.01, 1.0)) – A tuple of (min, max) defining the lower and upper bounds for the sigma (width) parameter of the Gaussian.
- Returns:
A 3D array of shape (x, y, 3) containing the fitted parameters for each pixel. The last axis corresponds to [Amplitude, Mean (mu), Sigma]. Returns zeros for pixels where the fit failed or signal was below threshold.
- Return type:
numpy.ndarray
- nDTomo.analysis.peaks.loadpeakfits(fn, peaks)[source]
Loads peak fitting results from an HDF5 file.
This function reads peak areas, positions, and FWHMs, as well as background parameters from an HDF5 file. The function assumes that the data is stored in a format compatible with the savepeakfits function.
- Parameters:
- Returns:
A tuple containing: - Areas (dict): Peak areas, where keys correspond to peak indices. - Pos (dict): Peak positions, where keys correspond to peak indices. - Sigma (dict): Peak FWHMs, where keys correspond to peak indices. - Bkga (numpy.ndarray): Background parameter a. - Bkgb (numpy.ndarray): Background parameter b. - Bkgc (numpy.ndarray or None): Background parameter c (if present, otherwise None).
- Return type:
Notes
Background parameter Bkgc is optional. If not present in the file, its value in the return tuple will be None.
- nDTomo.analysis.peaks.savepeakfits(fn, Areas, Pos, Sigma)[source]
Saves peak fitting results to an HDF5 file.
- Parameters:
fn (str) – Filename for the HDF5 file (use .h5 or .hdf5 extension).
Areas (dict) – Dictionary containing peak areas, with keys corresponding to peak indices.
Pos (dict) – Dictionary containing peak positions, with keys corresponding to peak indices.
Sigma (dict) – Dictionary containing peak FWHMs, with keys corresponding to peak indices.
Notes
Each peak’s area, position, and FWHM is saved as a separate dataset in the HDF5 file.
The function overwrites any existing file with the same name.
- nDTomo.analysis.peaks.subtract_linear_background(data_roi, tth_roi)[source]
Subtracts a linear background from the last axis of a 3D array.
The background is estimated by defining a straight line connecting the first and last intensity values of each spectrum along the last axis.
- Parameters:
data_roi (numpy.ndarray) – Input 3D array containing the spectral data. The function assumes the spectral channel (energy/tth) is along the last axis (e.g., shape [dim1, dim2, channels]).
tth_roi (numpy.ndarray) – 1D array representing the x-axis values (e.g., 2-theta angles) corresponding to the last dimension of data_roi.
- Returns:
The background-subtracted data, with the same shape as data_roi.
- Return type:
numpy.ndarray
- nDTomo.analysis.peaks.tand(x)