Preprocessing#

brainmaze_eeg.preprocessing.detect_flat_line(x: ndarray[tuple[int, ...], dtype[float64]], fs: float, detection_window: float = 0.5, threshold: float = 5e-07)#

Detects flat-line segments in the input signal. A flat-line segment is identified when the mean absolute difference of the signal within a detection window is below a specified threshold.

Parameters:
  • x (np.ndarray) – Input signal, either 1D or 2D array.

  • fs (float) – Sampling frequency.

  • detection_window (float) – Length of the segment in seconds. Default is 0.5 seconds.

  • threshold (float) – Threshold for detecting flat-line segments. Default is 0.5e-6.

Returns:

Boolean array indicating the presence of flat-line segments for each detection window.

Return type:

np.ndarray

Raises:

ValueError – If the input signal is not 1D or 2D.

brainmaze_eeg.preprocessing.detect_outlier_noise(x: ndarray[tuple[int, ...], dtype[float64]], fs: float, detection_window: float = 0.5, threshold: float = 10)#

Detects outlier noise in the input signal based on a threshold. The function evaluates the signal’s deviation from the mean and identifies segments with excessive noise.

Parameters:
  • x (np.ndarray) – Input signal, either 1D or 2D array.

  • fs (float) – Sampling frequency.

  • detection_window (float) – Length of the segment in seconds. Default is 0.5 seconds.

  • threshold (float) – Threshold for detecting outliers. Default is 10.

Returns:

Boolean array indicating the presence of outlier noise for each segment.

Return type:

np.ndarray

Raises:

ValueError – If the input signal is not 1D or 2D.

brainmaze_eeg.preprocessing.detect_powerline(x: ndarray[tuple[int, ...], dtype[float64]], fs: float, detection_window: float = 0.5, powerline_freq: float = 60, threshold_ratio: float = 1000)#

Detects Powerline noise in the input signal using. Detection evaluates the power in the spectrum at powerline and its harmonics to the average power of the iEEG in 2 Hz - 40 Hz band. I Drops the last segment if ndarray shape is not a multiple of whole seconds.

Parameters:
  • x (np.ndarray) – Input signal, either 1D or 2D array.

  • fs (float) – Sampling frequency.

  • detection_window (float) – Length of the segment in seconds. Default is 0.5 seconds.

  • powerline_freq (float) – Frequency of the powerline noise. Default is 60 Hz.

  • threshold_ratio (float) – Threshold ratio for detection how many times the power of the powerline noise is higher than average power in 2 Hz - 40 Hz band. Default is 1000.

Returns:

Boolean array indicating the presence of powerline noise for every 1 second segment.

Return type:

np.ndarray

brainmaze_eeg.preprocessing.filter_powerline_notch(x: ndarray[tuple[int, ...], dtype[float64]], fs: float, frequency_powerline: float = 60)#

Filters powerline noise from the input signal using a notch filter. The function replaces NaN values with the median and returns nan values after filtering. This can possibly cause ringing around artifacts and edges.

brainmaze_eeg.preprocessing.mask_signal_with_nans(x: ndarray[tuple[int, ...], dtype[float64]], data_rate_threshold: float = 0.1)#

Masks signals with NaNs based on a nan rate threshold. If data rate (non-nan values) is below the threshold, the entire row is masked with NaNs.

Parameters:
  • x (np.ndarray) – Input signal, either 1D or 2D array.

  • dr_threshold (float, optional) – Drop rate threshold for masking. Default is 0.1.

Returns:

Signal with masked values replaced by NaNs.

Return type:

np.ndarray

Raises:

ValueError – If the input signal is not 1D or 2D.