epyr.baseline.selection

Region selection utilities for baseline correction.

This module provides functions for creating and managing baseline regions for both 1D and 2D EPR data.

Functions

combine_masks(*masks)

Combine multiple boolean masks using logical AND.

create_center_exclusion_mask_1d(x[, ...])

Create a mask that excludes the center portion of 1D data.

create_center_exclusion_mask_2d(X, Y[, ...])

Create a mask that excludes the center portion of 2D data.

create_edge_exclusion_mask_1d(x[, ...])

Create a mask that excludes points at the beginning and end of 1D data.

create_region_mask_1d(x, regions[, mode])

Boolean mask for 1D data, marking which points to keep for a fit.

create_region_mask_2d(X, Y, regions[, mode])

Boolean mask for 2D data, marking which points to keep for a fit.

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

Build a 1D baseline mask combining all exclusion criteria.

get_baseline_regions_2d(X, Y, Z[, ...])

Build a 2D baseline mask combining all exclusion criteria.

validate_regions_1d(regions, x_min, x_max)

Validate that 1D regions are within data bounds and properly formatted.

validate_regions_2d(regions, x_min, x_max, ...)

Validate that 2D regions are within data bounds and properly formatted.

epyr.baseline.selection.create_region_mask_1d(x, regions, mode='exclude')[source]

Boolean mask for 1D data, marking which points to keep for a fit.

Parameters:
  • x (np.ndarray) – Field or time axis.

  • regions (list of (float, float)) – [(x_low, x_high), ...]. Bounds are inclusive; order within a pair does not matter.

  • mode ({'exclude', 'include'}, optional) – 'exclude' masks out the listed regions (default); 'include' keeps only those regions.

Returns:

True where the point should be used for fitting.

Return type:

np.ndarray of bool

Examples

>>> import numpy as np
>>> from epyr.baseline import create_region_mask_1d
>>> x = np.arange(10)
>>> mask = create_region_mask_1d(x, [(3, 5)], mode="exclude")
>>> mask.astype(int)
array([1, 1, 1, 0, 0, 0, 1, 1, 1, 1])
epyr.baseline.selection.create_region_mask_2d(X, Y, regions, mode='exclude')[source]

Boolean mask for 2D data, marking which points to keep for a fit.

Parameters:
  • X (np.ndarray) – Coordinate meshgrids, identical shape.

  • Y (np.ndarray) – Coordinate meshgrids, identical shape.

  • regions (list of ((float, float), (float, float))) – [((x_lo, x_hi), (y_lo, y_hi)), ...]. Inclusive bounds.

  • mode ({'exclude', 'include'}, optional) – 'exclude' masks out the listed rectangles (default); 'include' keeps only those rectangles.

Returns:

Same shape as X. True where the point should be used.

Return type:

np.ndarray of bool

Examples

>>> import numpy as np
>>> from epyr.baseline import create_region_mask_2d
>>> X, Y = np.meshgrid(np.arange(5), np.arange(5))
>>> mask = create_region_mask_2d(X, Y, [((1, 3), (1, 3))], mode="exclude")
>>> int(mask.sum())  # 25 - 3*3 inner block
16
epyr.baseline.selection.create_center_exclusion_mask_1d(x, center_fraction=0.3)[source]

Create a mask that excludes the center portion of 1D data.

This is useful for CW EPR spectra where the signal is in the center and we want to fit the baseline using the wings.

Parameters:
  • x (ndarray) – X-coordinate array

  • center_fraction (float) – Fraction of the data range to exclude from center

Returns:

Boolean mask array (True = use for fitting, False = exclude)

Return type:

ndarray

epyr.baseline.selection.create_center_exclusion_mask_2d(X, Y, center_fraction=0.3)[source]

Create a mask that excludes the center portion of 2D data.

Parameters:
  • X (ndarray) – X-coordinate meshgrid

  • Y (ndarray) – Y-coordinate meshgrid

  • center_fraction (float) – Fraction of the data range to exclude from center

Returns:

Boolean mask array (True = use for fitting, False = exclude)

Return type:

ndarray

epyr.baseline.selection.create_edge_exclusion_mask_1d(x, exclude_initial=0, exclude_final=0)[source]

Create a mask that excludes points at the beginning and end of 1D data.

This is useful for time-series data where initial/final points may have artifacts or noise.

Parameters:
  • x (ndarray) – X-coordinate array

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

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

Returns:

Boolean mask array (True = use for fitting)

Return type:

ndarray

epyr.baseline.selection.combine_masks(*masks)[source]

Combine multiple boolean masks using logical AND.

Parameters:

masks (ndarray) – Variable number of boolean mask arrays

Returns:

Combined boolean mask (True where ALL masks are True)

Return type:

ndarray

epyr.baseline.selection.get_baseline_regions_1d(x, y, exclude_center=True, center_fraction=0.3, exclude_initial=0, exclude_final=0, manual_regions=None, region_mode='exclude')[source]

Build a 1D baseline mask combining all exclusion criteria.

Parameters:
  • x (ndarray) – X-coordinate array

  • y (ndarray) – Y-data array

  • exclude_center (bool) – Whether to exclude center region

  • center_fraction (float) – Fraction of center to exclude

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

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

  • manual_regions (List[Tuple[float, float]] | None) – List of manually specified regions

  • region_mode (str) – How to handle manual_regions (‘exclude’ or ‘include’)

Returns:

Boolean mask array for baseline fitting

Return type:

ndarray

epyr.baseline.selection.get_baseline_regions_2d(X, Y, Z, exclude_center=True, center_fraction=0.3, manual_regions=None, region_mode='exclude')[source]

Build a 2D baseline mask combining all exclusion criteria.

Parameters:
  • X (ndarray) – X-coordinate meshgrid

  • Y (ndarray) – Y-coordinate meshgrid

  • Z (ndarray) – Z-data array

  • exclude_center (bool) – Whether to exclude center region

  • center_fraction (float) – Fraction of center to exclude

  • manual_regions (List[Tuple[Tuple[float, float], Tuple[float, float]]] | None) – List of manually specified regions

  • region_mode (str) – How to handle manual_regions (‘exclude’ or ‘include’)

Returns:

Boolean mask array for baseline fitting

Return type:

ndarray

epyr.baseline.selection.validate_regions_1d(regions, x_min, x_max)[source]

Validate that 1D regions are within data bounds and properly formatted.

Parameters:
  • regions (List[Tuple[float, float]]) – List of region tuples

  • x_min (float) – Minimum x value in data

  • x_max (float) – Maximum x value in data

Returns:

True if all regions are valid

Raises:

ValueError – If regions are invalid

Return type:

bool

epyr.baseline.selection.validate_regions_2d(regions, x_min, x_max, y_min, y_max)[source]

Validate that 2D regions are within data bounds and properly formatted.

Parameters:
Returns:

True if all regions are valid

Raises:

ValueError – If regions are invalid

Return type:

bool