calculators

This module provides classes for background calculation and peak finding in diffraction patterns.

class fpfunctions.calculators.BackgroundCalculator

Bases: object

Class for calculating background in diffraction patterns.

This class provides methods to calculate background using different approaches: - Asymmetric Least Squares (ASLS) method - Manual interpolation between user-defined points

calculate_background_asls(data: numpy.typing.NDArray.numpy.float64, log_lambda: float, log_p: float) numpy.typing.NDArray.numpy.float64

Calculate background using Asymmetric Least Squares (ASLS) method.

This method implements the algorithm described by Eilers and Boelens (2005), which uses asymmetric weighting in the least squares fitting to separate background from peaks. The algorithm iteratively refines the background estimation by penalizing values above the estimated background.

We thank the PRISMA app built by E. Flores (https://doi.org/10.1002/cmtd.202100094)

Parameters:
  • data (npt.NDArray[np.float64]) – 2D array containing x-values (scattering variable) in first column and y-values (intensity) in second column

  • log_lambda (float) – Logarithm (base 10) of the smoothing parameter lambda. Controls the smoothness of the background

  • log_p (float) – Logarithm (base 10) of the penalty parameter.

Returns:

Calculated background values

Return type:

npt.NDArray[np.float64]

calculate_background_manual(data: numpy.typing.NDArray.numpy.float64, points: numpy.typing.NDArray.numpy.float64) numpy.typing.NDArray.numpy.float64

Calculate background by interpolating between manually selected points.

This method performs linear interpolation between user-defined points to estimate the background across the entire data range.

Parameters:
  • data (npt.NDArray[np.float64]) – 2D array containing x-values (2theta) in first column and y-values (intensity) in second column

  • points (npt.NDArray[np.float64]) – 2D array containing manually selected background points, with x-values in first column and y-values in second column

Returns:

Interpolated background values for each x-value in the input data

Return type:

npt.NDArray[np.float64]

class fpfunctions.calculators.PeakFindCalculator

Bases: object

Class for finding and processing peaks in diffraction patterns.

This class provides methods to identify peaks in diffraction data and process them to handle Ka1-Ka2 doublets.

Yb

Background values at peak positions

Type:

Optional[npt.NDArray[np.float64]]

Yr

Relative intensities at peak positions

Type:

Optional[npt.NDArray[np.float64]]

peaks_idx

Indices of detected peaks in the original data array

Type:

Optional[npt.NDArray[np.int64]]

peaks

2D array containing peak positions (2theta) in first column and intensities in second column

Type:

Optional[npt.NDArray[np.float64]]

__init__() None

Initialize the PeakFindCalculator with empty attributes.

All attributes are initialized to None and will be populated when peak finding methods are called.

process_peaks(wavelength: Tuple[float, float, float], threshold: float = 0.005) None

Process detected peaks to handle Ka1-Ka2 doublets.

After detecting peaks, if Ka1-Ka2 doublets are present, this method identifies and processes them. If a doublet is already detected (e.g., at large 2theta values), it compares peaks and accepts them within a certain threshold.

Parameters:
  • wavelength (Tuple[float, float, float]) – Tuple containing (Ka1, Ka2, intensity ratio) wavelength information

  • threshold (float, optional) – Threshold for accepting peaks as doublets, by default 0.005

Returns:

The method modifies the peaks attribute in-place

Return type:

None

Notes

This method should be called after calculate_peaks_intens to process the detected peaks. It uses Bragg’s law to identify potential Kα1-Kα2 doublets based on the sin(θ) ratio, which should match the wavelength ratio.

calculate_peaks_intens(data: numpy.typing.NDArray.numpy.float64, background: numpy.typing.NDArray.numpy.float64, width: int, alpha: float) numpy.typing.NDArray.numpy.float64

Find peaks in diffraction data and calculate their intensities.

This method subtracts the background from the data, finds peaks using scipy’s find_peaks function, and calculates various properties of the detected peaks.

Parameters:
  • data (npt.NDArray[np.float64]) – 2D array containing x-values (2theta) in first column and y-values (intensity) in second column

  • background (npt.NDArray[np.float64]) – Background values for each x-value in the input data

  • width (int) – Minimum width of peaks to be detected (in data points)

  • alpha (float) – Factor to multiply the standard deviation of relative intensities to determine the prominence threshold for peak detection

Returns:

2D array containing peak positions (scattering variable) in first column and intensities in second column

Return type:

npt.NDArray[np.float64]

Notes

This method also updates the following attributes: - Yr: Relative intensities at peak positions - Yb: Background values at peak positions - peaks_idx: Indices of detected peaks in the original data array - peaks: 2D array of peak positions and intensities

The prominence parameter for peak detection is calculated as: prominence = alpha * std(relative_intensities)