epyr.baseline.auto

Automatic baseline model selection for EPR data.

This module provides intelligent automatic selection of the best baseline correction model using statistical criteria (AIC, BIC, R²).

Functions

auto_baseline_with_recommendations(x, y[, ...])

Automatic baseline correction, restricted to models that fit the experiment.

baseline_auto_1d(x, y[, params, models, ...])

Automatic baseline model selection and correction for 1D EPR data.

compare_models_detailed(x, y[, params, models])

Fit every candidate baseline model and return detailed metrics.

get_model_recommendations([data_type, ...])

Suggest baseline models given the experiment type.

epyr.baseline.auto.baseline_auto_1d(x, y, params=None, models=None, selection_criterion='aic', use_real_part=True, exclude_initial=0, exclude_final=0, manual_regions=None, region_mode='exclude', interactive=False, verbose=True)[source]

Automatic baseline model selection and correction for 1D EPR data.

This function tests multiple baseline models and automatically selects the best one based on information criteria (AIC/BIC) or other metrics.

Parameters:
  • x (ndarray | None) – X-axis data from eprload (can be None)

  • y (ndarray) – 1D spectral data array from eprload

  • params (Dict[str, Any] | None) – Parameter dictionary from eprload (optional)

  • models (List[str] | None) – List of models to test. Options: ‘polynomial’, ‘stretched_exponential’, ‘bi_exponential’

  • selection_criterion (str) – ‘aic’ (Akaike), ‘bic’ (Bayesian), or ‘r2’ (R-squared)

  • use_real_part (bool) – If True, fit only real part of complex data

  • exclude_initial (int) – Number of initial points to exclude from fitting

  • exclude_final (int) – Number of final points to exclude from fitting

  • manual_regions (List[Tuple[float, float]] | None) – List of manually selected regions as [(x1, x2), …]

  • region_mode (str) – ‘exclude’ to exclude manual_regions, ‘include’ to use only manual_regions

  • interactive (bool) – If True, open interactive region selector

  • verbose (bool) – If True, print detailed model comparison

Returns:

(corrected_data, best_baseline,

model_info) where model_info contains: {‘best_model’: str, ‘criteria’: dict, ‘parameters’: dict}

Return type:

tuple

Examples

# Automatic model selection corrected, baseline, info = baseline_auto_1d(

x, y, params)

print(f”Best model: {info[‘best_model’]}”)

# Restrict to specific models corrected, baseline, info = baseline_auto_1d(

x, y, params, models=[‘polynomial’,

‘stretched_exponential’])

# Use BIC for model selection corrected, baseline, info = baseline_auto_1d(

x, y, params, selection_criterion=’bic’)

epyr.baseline.auto.compare_models_detailed(x, y, params=None, models=None, **kwargs)[source]

Fit every candidate baseline model and return detailed metrics.

Parameters:
  • x (np.ndarray or None) – Axis from epyr.eprload(). Falls back to indices if None.

  • y (np.ndarray) – 1D signal.

  • params (dict, optional) – Parameter dictionary from epyr.eprload().

  • models (list of str, optional) – Subset of ['polynomial', 'stretched_exponential', 'bi_exponential']. Default: all three.

  • **kwargs – Passed through to the underlying baseline_* functions (use_real_part, exclude_initial, manual_regions, etc.).

Returns:

{model_name: {'aic': ..., 'r_squared': ..., 'parameters': ...}} for each model that converged.

Return type:

dict

Examples

>>> from epyr import eprload
>>> from epyr.baseline import compare_models_detailed
>>> path = "examples/data/ESEdecay_2D_rotgon_035_07.3K_h80_9.73687GHz_B3.DSC"
>>> x, y, params, _ = eprload(path)
>>> results = compare_models_detailed(x[0], y[0], params)
>>> set(results) <= {"polynomial", "stretched_exponential", "bi_exponential"}
True
epyr.baseline.auto.get_model_recommendations(data_type=None, experiment_type=None)[source]

Suggest baseline models given the experiment type.

Parameters:
  • data_type ({'cw', 'pulsed'}, optional) – Continuous-wave or pulsed EPR.

  • experiment_type (str, optional) – Sub-type when data_type='pulsed'. Known values: 't1', 't2', 'echo', 'rabi', 'nutation'.

Returns:

Recommended model names, in order of preference.

Return type:

list of str

Examples

>>> from epyr.baseline import get_model_recommendations
>>> get_model_recommendations("cw")
['polynomial']
>>> get_model_recommendations("pulsed", "t2")
['stretched_exponential', 'bi_exponential', 'polynomial']
epyr.baseline.auto.auto_baseline_with_recommendations(x, y, params=None, data_type=None, experiment_type=None, **kwargs)[source]

Automatic baseline correction, restricted to models that fit the experiment.

Wraps baseline_auto_1d() after pruning the candidate-model list via get_model_recommendations().

Parameters:
  • x (np.ndarray or None) – Axis from epyr.eprload().

  • y (np.ndarray) – 1D signal.

  • params (dict, optional) – Parameter dictionary from epyr.eprload().

  • data_type ({'cw', 'pulsed'}, optional) – Used to filter the candidate models.

  • experiment_type (str, optional) – Refines the filter for pulsed experiments.

  • **kwargs – Passed through to baseline_auto_1d().

Returns:

  • corrected (np.ndarray)

  • baseline (np.ndarray)

  • info (dict) – Best-model metadata (name, score, parameters, fit metrics).

Return type:

Tuple[ndarray, ndarray, Dict[str, Any]]

Examples

>>> from epyr import eprload
>>> from epyr.baseline import auto_baseline_with_recommendations
>>> x, y, params, _ = eprload(
...     "examples/data/ESEdecay_2D_rotgon_035_07.3K_h80_9.73687GHz_B3.DSC"
... )
>>> corrected, baseline, info = auto_baseline_with_recommendations(
...     x[0], y[0], params, data_type="pulsed", experiment_type="t2", verbose=False,
... )
>>> info["best_model"] in {"polynomial", "stretched_exponential", "bi_exponential"}
True