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(x_data, y_data[, shape_type, ...])

Fit EPR signal with specified lineshape function.

fit_multiple_shapes(x_data, y_data[, ...])

Fit EPR signal with multiple lineshape types and compare results.

Classes

FitResult(shape_type, parameters, ...[, ...])

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:
shape_type

Name of the fitted lineshape model.

Type:

str

parameters

Fitted parameter values keyed by parameter name.

Type:

dict

parameter_errors

Standard errors of fitted parameters (square root of covariance diagonal).

Type:

dict

fitted_curve

Model evaluated at the fitted points (x_fit).

Type:

np.ndarray

residuals

Data minus model at the fitted points.

Type:

np.ndarray

r_squared

Coefficient of determination R².

Type:

float

chi_squared

Reduced chi-squared: sum of squared residuals divided by degrees of freedom.

Type:

float

success

True if curve_fit converged.

Type:

bool

message

Convergence message or error description.

Type:

str

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

shape_type: str
parameters: Dict[str, float]
parameter_errors: Dict[str, float]
fitted_curve: ndarray
residuals: ndarray
r_squared: float
chi_squared: float
success: bool
message: str
covariance_matrix: ndarray | None = None
x_fit: ndarray | None = None
summary()[source]

Return a formatted string summarizing fit quality and parameters.

Return type:

str

__init__(shape_type, parameters, parameter_errors, fitted_curve, residuals, r_squared, chi_squared, success, message, covariance_matrix=None, x_fit=None)
Parameters:
Return type:

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 + b in 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:

FitResult

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:

dict