autoemxsp.core.XSp_fitter module

X-ray Spectrum Fitting Module

Created on Thu Jun 27 10:17:26 2024

@author: Andrea Giunto

This module provides classes and functions for physically-accurate modeling, fitting, and analysis of X-ray energy-dispersive spectroscopy (EDS) spectra.

Overview

The module is designed to enable robust, quantitative analysis of EDS spectra, including background continuum, detector response, and detailed peak shapes (including escape and pileup peaks, ICC effects, and low-energy tails). It is suitable for both bulk and particle samples, and supports flexible calibration and constraint schemes.

Class Structure and Interactions

The main classes are: - XSp_Fitter

The main workflow class. Given a measured spectrum, calibration information, and a list of elements, it builds the complete lmfit model (background + peaks), sets up all parameters and constraints, and runs the fit. It provides methods for plotting, reporting, and extracting fit results. This class orchestrates the use of all other classes and should be the main entry point for typical users.

  • Peaks_Model Manages the construction and parameterization of all spectral peaks (characteristic X-ray lines, escape peaks, pileup peaks, etc.). It supports advanced peak shapes (e.g., skewed/tail Gaussians, ICC convolution), constraints between related peaks, and caching for efficient repeated use. It is typically instantiated by the spectrum fitter and used to build the composite peak model.

  • Background_Model Handles the computation and parameterization of the spectral background, including physical effects such as X-ray generation, absorption, detector efficiency, and backscattering. Used to build the background component of the overall spectral model.

  • DetectorResponseFunction Provides static and class methods for handling the detector’s instrumental response, including convolution matrices for energy resolution and incomplete charge collection (ICC). This class is initialized with calibration data and is used by both background and peak models to accurately simulate detector effects.

Typical Usage

  1. Initialize the fitter: ```python fitter = XSp_Fitter(

    spectrum_vals, energy_vals, els_to_quantify=[‘Fe’, ‘Ni’], microscope_ID=’PhenomXL’

    )

  2. Fit the spectrum: ```python fit_result, fitted_lines = fitter.fit_spectrum(plot_result=True, print_result=True) )

  3. Inspect and use results: Use fit_result for detailed analysis. Plot or print results using fitter.plot_result() and fitter.print_result(). Access fitted parameters, background components, and diagnostic information.

Customization & Calibration

Detector calibration, physical constants, and peak shape calibration are handled via the calibs module and are loaded automatically based on the specified microscope and EDS mode. Advanced users may customize which peaks are freely calibrated, which are constrained, and how background/peak models are parameterized by modifying the relevant class parameters or by subclassing.

Dependencies

numpy, scipy, lmfit, matplotlib, and supporting modules for calibration and physical constants.

How the classes interact:

  • XSp_Fitter is the main user-facing class. It creates and coordinates instances of Background_Model and Peaks_Model, and uses DetectorResponseFunction to ensure all detector effects are handled consistently.

  • DetectorResponseFunction is a utility class used by both Background_Model and Peaks_Model to convolve model components with the detector response.

  • Peaks_Model and Background_Model each build their respective parts of the overall spectrum model, which are then combined by the fitter for the full fit.

In short:

Instantiate XSp_Fitter with your data and settings, then call fit_spectrum(). The module will handle background, detector response, and peak modeling for you, providing a comprehensive, physically-based EDS spectrum fit.

autoemxsp.core.XSp_fitter.patch_lmfit_fast_mode(verbose=False)[source]

Disable all covariance/uncertainty computations globally in lmfit.

Works for lmfit >=1.0. If internal methods change in future releases, prints a warning so the user knows the patch isn’t effective.

class autoemxsp.core.XSp_fitter.XSp_Fitter(spectrum_vals, energy_vals, spectrum_lims, microscope_ID, meas_mode, det_ch_offset, det_ch_width, beam_e, emergence_angle, fit_background=True, is_particle=False, els_to_quantify=None, els_substrate=None, els_w_fr=None, force_fr_total=True, tot_sp_counts=None, sp_collection_time=None, xray_quant_ref_lines=None, print_evolving_params=False, verbose=False)[source]

Bases: object

Fitter for EDS spectra.

Handles EDS spectral fitting, including background modeling, element quantification, and correction for experimental conditions.

spectrum_vals

Measured spectrum intensity values.

Type:

array-like

energy_vals

Corresponding energy values (in keV).

Type:

array-like

els_to_quantify

Elements to quantify in the sample.

Type:

list of str

els_w_fr

Elements with fixed mass fractions.

Type:

dict

els_substrate

Elements present in the substrate.

Type:

list of str

fit_background

Whether to fit a background continuum.

Type:

bool

xray_quant_ref_lines

X-ray lines used for quantification.

Type:

tuple of str

is_particle

If True, fit considers absorption and mass effect of particles.

Type:

bool

microscope_ID

Identifier for microscope calibration and detector efficiency.

Type:

str

meas_mode

EDS acquisition mode.

Type:

str

spectrum_lims

Spectrum limits.

Type:

tuple

force_fr_total

Normalize total fitted elemental fraction to 1 if True.

Type:

bool

beam_energy

Beam energy in keV.

Type:

float

emergence_angle

X-ray emergence angle in degrees.

Type:

float

tot_sp_counts

Total spectrum counts.

Type:

int or None

sp_collection_time

Spectrum collection time in seconds.

Type:

float or None

print_evolving_params

Print evolving fit parameters (for debugging).

Type:

bool

verbose

Verbose output.

Type:

bool

escape_peaks_str = '_escSiKa'
pileup_peaks_str = '_pileup'
fit_spectrum(parameters=None, initial_par_vals=None, function_tolerance=0.001, plot_result=False, print_result=False, print_result_extended=False, n_iter=None)[source]

Fit the EDS spectrum using lmfit.

Parameters:
  • parameters (lmfit.Parameters, optional) – Parameters object to use for fitting. If None, parameters are generated internally.

  • initial_par_vals (dict, optional) – Dictionary of initial parameter values to override defaults.

  • function_tolerance (float, optional) – ftol used in scipy.optimize.leastsq (default: 1e-3).

  • plot_result (bool, optional) – Whether to plot the fitted spectrum (total fit and background).

  • print_result (bool, optional) – Whether to print the quality of the fit.

  • print_result_extended (bool, optional) – Whether to print extended fit results.

  • n_iter (int, optional) – Iteration number (for display purposes).

Returns:

  • fit_result (lmfit.ModelResult) – Contains all information about the result of the fit.

  • fitted_lines (list of str) – List of fitted X-ray lines.

plot_result()[source]

Plot the fitted EDS spectrum and its individual background components.

Displays the total fit, background components, and a residual plot.

print_result(extended=False, print_only_independent_params=False)[source]

Print a summary of the fit results.

Parameters:
  • extended (bool, optional) – If True, prints the full fit report or parameter table. Otherwise, prints only summary statistics (Reduced chi-squared and R-squared).

  • print_only_independent_params (bool, optional) – If True and extended is True, prints only parameter names and their values.

class autoemxsp.core.XSp_fitter.Peaks_Model(spectrum_vals, energy_vals, microscope_ID, meas_mode, fitting_model, fitting_pars, xray_weight_refs_dict=None, is_particle=False, free_area_el_lines=None, free_peak_shapes_els=None)[source]

Bases: object

Model for X-ray spectral peaks in EDS spectra.

Handles the construction, parameterization, and constraints of peak models for EDS spectral fitting, including area weighting, escape/pileup peaks, and peak shape calibration.

spectrum_vals

Measured spectrum values (e.g., counts).

Type:

array-like or None

energy_vals

Corresponding energy values (e.g., keV).

Type:

array-like or None

fitting_model

Composite model used for fitting.

Type:

lmfit.Model or None

fitting_params

Parameters for the current model.

Type:

lmfit.Parameters or None

xray_weight_refs_dict

Maps each secondary line to its reference line for area weighting.

Type:

dict

xray_weight_refs_lines

Unique reference lines extracted from xray_weight_refs_dict.

Type:

list

microscope_ID

Identifier for the microscope/calibration to use.

Type:

str

meas_mode

EDS mode, e.g., ‘point’, ‘map’, etc.

Type:

str

is_particle

Whether the sample is a particle (affects some constraints).

Type:

bool

free_area_el_lines

Elements/lines whose area is fitted freely (for peak intensity weight calibration).

Type:

list

free_peak_shapes_els

Elements whose peak shapes are calibrated (for shape calibration).

Type:

list

fixed_peaks_dict

Tracks dependencies for overlapping peaks.

Type:

dict

Class attributes
----------------
icc_freq_spectra

Cache for ICC convolution spectra, shared across all instances.

Type:

dict (class variable)

center_key, sigma_key, area_key, center_offset_key, sigma_broadening_key,
gamma_key, tail_fraction_key, F_loss_key, R_e_key, pileup_peaks_str,
pileup_peaks_str

Standardized parameter names for model building.

Type:

str

Notes

  • icc_freq_spectra is a class variable, shared across all instances and used for static method access.

  • Requires XSp_Fitter to be initialized first for correct DetectorResponseFunction loading.

icc_freq_spectra = {}
center_key = 'center'
sigma_key = 'sigma'
area_key = 'area'
center_offset_key = 'cen_offset'
sigma_broadening_key = 'sigma_broad'
gamma_key = 'gamma'
tail_fraction_key = 'f_tail'
F_loss_key = 'F_loss'
R_e_key = 'R_e'
escape_peaks_str = '_escSiKa'
pileup_peaks_str = '_pileup'
static clear_cached_icc_spectra()[source]

Clear the class-level cache for ICC convolution spectra.

This method resets the icc_freq_spectra dictionary shared by all instances of Peaks_Model, ensuring that all cached ICC spectra are removed prior new calculations that require new computations of icc_freq_spectra.

get_peaks_mod_pars()[source]

Return the current composite peak model and its associated fit parameters.

Returns:

  • model (lmfit.Model or None) – The composite model representing all fitted peaks, or None if not yet defined.

  • pars (lmfit.Parameters) – The set of parameters for the current model.

class autoemxsp.core.XSp_fitter.Background_Model(is_particle: bool, sp_collection_time: float = None, tot_sp_counts: int = None, beam_energy: float = 15, emergence_angle: float = 28.5, els_w_fr: dict = None, meas_mode: str = 'point', energy_vals=None)[source]

Bases: object

Model for calculating and fitting X-ray background in EDS spectra.

Class Attributes

cls_beam_efloat or None

Electron beam energy in keV, accessible by all instances and fitting routines.

den_intany

Cached denominator integral for background calculation.

num_intany

Cached numerator integral for background calculation.

prev_xany

Cached previous energy values.

prev_rhoz_par_offsetany

Cached previous absolute rho-z offset.

prev_rhoz_par_slopeany

Cached previous rho-z offset slope.

prev_rhoz_limitany

Cached previous rho-z z limit.

prev_w_frsany

Cached previous weight fractions.

rhoz_valuesany

rhoz_values computed for a given rhoz_limit.

Instance Attributes

is_particlebool

If True, indicates the background is for a particle (affects fitting).

sp_collection_timefloat or None

Spectrum collection time in seconds.

tot_sp_countsint or None

Total counts in the spectrum.

emergence_anglefloat

Detector emergence angle in degrees.

energy_valsarray-like or None

Array of energy values for the spectrum.

meas_modestr

EDS mode, e.g., ‘point’ or ‘map’.

els_w_frdict

Elemental weight fractions.

cls_beam_e: float = None
den_int = None
num_int = None
prev_x = None
prev_rhoz_par_offset = None
prev_rhoz_par_slope = None
prev_rhoz_limit = None
prev_w_frs = None
rhoz_values = None
static get_average_Z(els_symbols, method='Statham')[source]

Returns a symbolic formula string for the average atomic number (Z) of a sample, using one of several literature methods.

Parameters:
  • els_symbols (list of str) – List of element symbols (e.g., [‘Si’, ‘O’]).

  • method (str, optional) – Which method to use for averaging Z. Options are: - “mass_weighted”: mass-fraction-weighted average Z (sum(Z_i * f_i)) - “Markowicz”: Markowicz & Van Grieken (1984) average Z formula - “Statham”: Statham et al. (2016) average Z formula Default is “mass_weighted”.

Returns:

formula_str – Symbolic formula for the chosen average Z, using mass fractions f_{el}.

Return type:

str

References

Markowicz AA, Van Grieken RE. Anal Chem 1984 Oct 1;56(12):2049–51. Statham P, Penman C, Duncumb P. IOP Conf Ser Mater Sci Eng. 2016;109(1):0–10.

Notes

  • This function returns a string formula; you must substitute actual fractions for f_{el} in use.

  • Only the Statham method is currently used in the main implementation.

get_stopping_power_mod_pars()[source]

Returns an lmfit Model and its Parameters for the electron stopping power correction.

Returns:

  • stopping_p_correction_m (lmfit.Model) – Model describing the stopping power correction.

  • params_stopping_p (lmfit.Parameters) – Parameters for the model.

Notes

  • The model is based on Background_Model._stopping_power.

  • The adr_sp parameter controls convolution with the detector response function.

get_abs_attenuation_mod_pars(model='phirho')[source]

Returns an lmfit Model and its Parameters for background signal attenuation due to absorption in the sample.

Parameters:

model (str, optional) –

Which absorption model to use. Options: - ‘phirho’ (default): depth-distribution-based attenuation

(Packwood & Brown, 1981; Riveros et al., 1992; Del Giorgio et al., 1990)

  • ’Philibert’: modified Philibert equation

    (Heinrich & Yakowitz, 1975)

Returns:

  • absorption_attenuation_m (lmfit.Model) – Model function describing the attenuation of the background signal.

  • params_abs_att (lmfit.Parameters) – Parameters for the model.

References

  • Packwood, R.H., & Brown, J.D. (1981). A Gaussian expression to describe ϕ(ρz) curves for quantitative electron probe microanalysis. X-Ray Spectrom. 10, 138–146. https://doi.org/10.1002/xrs.1300100311

  • Riveros, J.A., Castellano, G.E., & Trincavelli, J.C. (1992). Comparison of Φ (ρz) Curve Models in EPMA. In: Microbeam Analysis, pp. 99–105. https://doi.org/10.1007/978-3-7091-6679-6_7

  • Del Giorgio, M., Trincavelli, J., & Riveros, J.A. (1990). Spectral distribution of backscattered electrons: Application to electron probe microanalysis. X-Ray Spectrom. 19, 261–267. https://doi.org/10.1002/xrs.1300190603

  • Heinrich, K.F.J. & Yakowitz, H. (1975). Absorption of Primary X Rays in Electron Probe Microanalysis. Anal. Chem. 47, 2408–2411. https://doi.org/10.1021/ac60364a018

Raises:

ValueError – If an unknown model is requested.

get_backscattering_correction_mod_pars()[source]

Returns an lmfit Model and its Parameters for the correction of background signal loss due to electron backscattering.

Returns:

  • bs_correction_m (lmfit.Model) – Model describing the correction for loss of generated background signal due to electron backscattering.

  • params_bs_cor (lmfit.Parameters) – Parameters for the model.

Notes

  • The model is based on the implementation in Background_Model._backscattering_correction.

  • The adr_bcksctr parameter controls convolution with the detector response function.

static get_beta_expr(beta_expr, els_symbols)[source]

Returns a symbolic formula string for the beta parameter from the modified Duncumb’s continuum generation model. beta depends on atomic number Z, mass-fraction-averaged over a compound or mixture.

Parameters:
  • beta_expr (sympy expression) – A sympy expression involving the symbol ‘Z’, e.g. 0.5 * Z**1.2.

  • els_symbols (list of str) – List of element symbols (e.g., [‘Si’, ‘O’]).

Returns:

beta_expr_full – Symbolic formula for the compound parameter, mass-fraction-averaged over elements.

Return type:

str

Example

If beta_expr = 0.5 * Z**1.2 and els_symbols = [‘Si’, ‘O’], returns:

“14.054 * f_Si + 4.594 * f_O”

get_generated_background_mod_pars(fr_pars, is_calibration=False, model='DuncumbMod')[source]

Returns an lmfit Model and its Parameters for the continuum X-ray background generated within the sample.

Parameters:
  • fr_pars (dict) – Dictionary of element mass fraction parameters (e.g., {‘f_Si’: 0.5, ‘f_O’: 0.5}).

  • is_calibration (bool, optional) – If True, use calibration mode with free parameters. Default is False.

  • model (str, optional) –

    Background model to use. Options:
    • ’Castellano2004’

    • ’Trincavelli1998’

    • ’DuncumbMod’ (default)

    • ’Duncumb2001’

Returns:

  • bckgrnd_m (lmfit.Model) – Model describing the continuum X-ray signal generated within the sample.

  • params_bckgrnd (lmfit.Parameters) – Parameters for the model.

Notes

  • For Castellano2004 and Trincavelli1998, the mass-weighted average Z is used.

  • For DuncumbMod and Duncumb2001, Statham’s average Z is used.

  • Duncumb2001 is identical to DuncumbMod but with beta fixed to 0 (not varying).

  • Parameter logic adapts to calibration and particle mode.

  • See references for model details.

References

Castellano, G., Osán, J., & Trincavelli, J. (2004). Analytical model for the bremsstrahlung spectrum in the 0.25–20 keV photon energy range. Spectrochimica Acta Part B: Atomic Spectroscopy, 59(3), 313–319. https://doi.org/10.1016/j.sab.2003.11.008 Trincavelli, J., Castellano, G., & Riveros, J. A. (1998). Model for the bremsstrahlung spectrum in EPMA. Application to standardless quantification. X-Ray Spectrometry, 27(2), 81–86. https://doi.org/10.1002/(SICI)1097-4539(199803/04)27:2<81::AID-XRS253>3.0.CO;2-R Statham, P., Penman, C., & Duncumb, P. (2016). Improved spectrum simulation for validating SEM-EDS analysis. IOP Conf Ser Mater Sci Eng, 109(1):0–10. Duncumb, P., Barkshire, I. R., & Statham, P. J. (2001). Improved X-ray Spectrum Simulation for Electron Microprobe Analysis. Microscopy and Microanalysis, 7(4), 341–355. https://doi.org/10.1007/S10005-001-0010-6

get_detector_efficiency_mod_pars()[source]

Returns an lmfit Model and its Parameters for the EDS detector efficiency correction.

Returns:

  • detector_efficiency_m (lmfit.Model) – Model describing the loss of signal due to the EDS detector efficiency. This model has no adjustable parameters, as we use the efficiency provided by the EDS manufacturer.

  • detector_efficiency_pars (lmfit.Parameters) – Parameters for the model.

Notes

  • The model is based on Background_Model._det_efficiency.

  • The adr_det_eff parameter controls convolution with the detector response function.

get_det_zero_peak_model_pars(amplitude_val)[source]

Returns an lmfit GaussianModel and its Parameters for modeling the detector zero (strobe) peak.

Parameters:

amplitude_val (float) – Initial amplitude estimate for the zero peak.

Returns:

  • det_zero_peak_model (lmfit.models.GaussianModel) – Gaussian model for the detector zero (strobe) peak.

  • params_det_zero_peak (lmfit.Parameters) – Parameters for the model.

Notes

  • The amplitude limits are set wider for particles than for bulk samples.

  • The sigma (width) of the zero peak is taken from calibration for the current EDS mode.

get_full_background_mod_pars(fr_pars)[source]

Constructs the full background model and its parameters for spectral fitting.

Parameters:

fr_pars (dict) – Dictionary of element mass fraction parameters (e.g., {‘f_Si’: 0.5, ‘f_O’: 0.5}).

Returns:

  • background_mod (lmfit.Model) – The full composite model for the background spectrum, including all physical corrections and the detector zero (strobe) peak.

  • background_pars (lmfit.Parameters) – Combined parameters for the full background model.

Notes

  • The model includes generated background, absorption attenuation, detector efficiency, backscattering correction, stopping power correction, and the detector strobe peak.

  • All sub-models and their parameters are constructed and combined automatically.

class autoemxsp.core.XSp_fitter.DetectorResponseFunction[source]

Bases: object

det_res_conv_matrix = None
icc_conv_matrix = None
energy_vals_padding = 30
classmethod setup_detector_response_vars(det_ch_offset, det_ch_width, spectrum_lims, microscope_ID, verbose=True)[source]

Initialize detector response variables for the EDS system.

Loads detector efficiency and convolution matrices for the specified microscope. If convolution matrices for the given channel settings do not exist, they are calculated and saved.

Parameters:
  • det_ch_offset (float) – Energy offset for detector channels (in keV).

  • det_ch_width (float) – Channel width (in keV).

  • spectrum_lims (tuple of int) – (low, high) indices for the usable spectrum region.

  • microscope_ID (str) – Identifier for the microscope/calibration directory.

  • verbose (bool, optional) – If True, print status messages.

  • Attributes (Sets Class)

  • ---------------------

  • det_eff_energy_vals (np.ndarray) – Energy values for detector efficiency.

  • det_eff_vals (np.ndarray) – Detector efficiency values.

  • det_res_conv_matrix (np.ndarray) – Detector response convolution matrix (cropped to spectrum_lims).

  • icc_conv_matrix (np.ndarray) – ICC convolution matrix (cropped to spectrum_lims).

Notes

  • Convolution matrices are detector-dependent and cached in JSON for efficiency.

  • If multiple EDS detectors are used, this code should be adapted.

static get_icc_spectrum(energy_vals, line_en, R_e=5e-06, F_loss=0.27)[source]

Generate the ICC (Incomplete Charge Collection) smearing function for a given line energy.

Parameters:
  • energy_vals (array-like) – Energy axis (in keV) of detector channels.

  • line_en (float) – X-ray line energy (in keV).

  • R_e (float, optional) – Effective recombination parameter (cm).

  • F_loss (float, optional) – Fractional charge loss parameter.

Returns:

icc_n_vals_distr – ICC distribution, mapped to detector channel energies.

Return type:

list