epyr.lineshapes.fitting
EPR signal fitting module.
Fit EPR spectra with Gaussian, Lorentzian, Voigt, or pseudo-Voigt lineshapes. Supports absorption and derivative signals (orders 0-2), optional phase mixing, and affine baseline correction.
Functions
|
Fit EPR signal with specified lineshape function. |
|
Fit EPR signal with multiple lineshape types and compare results. |
Classes
|
Container for lineshape fit results. |
- class epyr.lineshapes.fitting.FitResult(shape_type, parameters, parameter_errors, fitted_curve, residuals, r_squared, chi_squared, success, message, covariance_matrix=None, x_fit=None)[source]
Container for lineshape fit results.
- Parameters:
- parameter_errors
Standard errors of fitted parameters (square root of covariance diagonal).
- Type:
- fitted_curve
Model evaluated at the fitted points (x_fit).
- Type:
np.ndarray
- residuals
Data minus model at the fitted points.
- Type:
np.ndarray
- chi_squared
Reduced chi-squared: sum of squared residuals divided by degrees of freedom.
- Type:
- covariance_matrix
Full parameter covariance matrix returned by curve_fit.
- Type:
np.ndarray or None
- x_fit
X values used for fitting, after NaN removal and masking.
- Type:
np.ndarray or None
- __init__(shape_type, parameters, parameter_errors, fitted_curve, residuals, r_squared, chi_squared, success, message, covariance_matrix=None, x_fit=None)
- epyr.lineshapes.fitting.fit_epr_signal(x_data, y_data, shape_type='gaussian', initial_params=None, bounds=None, derivative=0, fit_phase=False, fit_baseline=False, mask=None, plot=True, **fit_kwargs)[source]
Fit EPR signal with specified lineshape function.
- Parameters:
x_data (np.ndarray) – Magnetic field axis, in Gauss.
y_data (np.ndarray) – EPR signal intensity (arbitrary units).
shape_type (str, optional) – Lineshape model: ‘gaussian’, ‘lorentzian’, ‘voigt’, or ‘pseudo_voigt’ (default: ‘gaussian’).
initial_params (dict, optional) – Initial parameter guesses. Auto-estimated from data if None. Keys depend on shape_type: basic models use {‘center’, ‘width’, ‘amplitude’}; voigt uses {‘center’, ‘gaussian_width’, ‘lorentzian_width’, ‘amplitude’}; pseudo_voigt adds ‘alpha’; phase fitting adds ‘phase’; baseline fitting adds ‘baseline_slope’ and ‘baseline_offset’.
bounds (dict, optional) – Parameter bounds as {name: (lower, upper)}, overriding data-derived defaults.
derivative (int, optional) – Derivative order: 0 (absorption), 1, or 2. Fixed, not fitted (default: 0).
fit_phase (bool, optional) – Fit the phase angle controlling absorption/dispersion mixing (default: False).
fit_baseline (bool, optional) – Include an affine baseline
a*x + bin the model. Adds ‘baseline_slope’ and ‘baseline_offset’ as fitted parameters (default: False).mask (np.ndarray of bool, optional) – Boolean array of the same length as x_data. True selects a point for fitting; False excludes it. Useful to reject artefacts or solvent peaks. If None, all non-NaN points are used.
plot (bool, optional) – Display a fit plot with residuals panel (default: True).
**fit_kwargs – Additional keyword arguments passed to scipy.optimize.curve_fit.
- Returns:
Fit parameters, errors, statistics, fitted curve, and residuals. fitted_curve and residuals are defined on x_fit (masked points only).
- Return type:
Examples
>>> from epyr import eprload >>> x, y, params, filepath = eprload('data.DTA') >>> result = fit_epr_signal(x, y, 'gaussian') >>> print(result.summary()) >>> >>> # 1st derivative with phase >>> result = fit_epr_signal(x, y, 'gaussian', derivative=1, fit_phase=True) >>> >>> # Exclude a spectral artefact between 3480-3510 G >>> mask = ~((x >= 3480) & (x <= 3510)) >>> result = fit_epr_signal(x, y, 'gaussian', mask=mask) >>> >>> # 2nd derivative with explicit bounds >>> bounds = {'center': (3400, 3600), 'width': (5, 50), 'phase': (0, 3.14)} >>> result = fit_epr_signal(x, y, 'gaussian', derivative=2, ... fit_phase=True, bounds=bounds)
- epyr.lineshapes.fitting.fit_multiple_shapes(x_data, y_data, shapes=None, derivative=0, fit_phase=False, fit_baseline=False, mask=None, plot=True)[source]
Fit EPR signal with multiple lineshape types and compare results.
- Parameters:
x_data (np.ndarray) – Magnetic field axis, in Gauss.
y_data (np.ndarray) – EPR signal intensity.
shapes (list of str, optional) – Lineshape models to fit. Default: [‘gaussian’, ‘lorentzian’, ‘pseudo_voigt’].
derivative (int, optional) – Derivative order (0, 1, or 2). Fixed, not fitted (default: 0).
fit_phase (bool, optional) – Fit the phase parameter in each model (default: False).
fit_baseline (bool, optional) – Include an affine baseline in each model (default: False).
mask (np.ndarray of bool, optional) – Boolean array selecting points to include (True = include). Passed unchanged to each fit_epr_signal call.
plot (bool, optional) – Display a side-by-side comparison plot (default: True).
- Returns:
Mapping of shape name to FitResult for all attempted fits.
- Return type: