brainaccess.connect package

Submodules

brainaccess.connect.P300 module

class brainaccess.connect.P300.P300(model_number: int)[source]

Bases: object

P300 BCI library

Initialize P300 model.

Parameters:

model_number (int) – Model type to load, currently available: 0 - 8 electrode Standard Kit setup, 1 repetitions 1 - 8 electrode Standard Kit setup, 3 repetition 2 - 8 electrode Standard Kit setup, 3 repetitions, “fast” - inter trial interval is 215ms 3 - O1 and O2 electrodes only, 3 repetitions, “fast” - inter trial interval is 215ms

Raises:

Exception – An error is raised if initializing failed

destroy() None[source]
predict(x: ndarray) float[source]

Predict P300

Parameters:

x (np.ndarray) – data for classifier

Returns:

probability that data was P300 event

Return type:

float

Raises:

Exception – An error is raised if prediction failed

Warning

Data sampled at 250 Hz must have these properties:

  • standardized with ewma and filtered with 1-40 Hz filter

  • (8, 176 * repetitions) shape (channels x samples),

  • each repetition 200 ms prior to stimulus onset up to 500 ms after stimulus onset

  • Channels must be in exactly this order: F3, F4, C3, C4, P3, P4, O1, O2 (8 channels) or O1, O2 (2 channels)

brainaccess.connect.SSVEP module

class brainaccess.connect.SSVEP.SSVEP(frequencies: list = [], sample_rate: float = 250)[source]

Bases: object

SSVEP BCI library

Initialize SSVEP model

Parameters:
  • frequencies (list) – list of stimulation frequencies

  • sample_rate (float) – data sampling rate

Raises:

Exception – An error is raised if initializing failed

predict(x: ndarray, frequencies: list | None = None, sample_rate: float | None = None) tuple[source]

Classify EEG SSVEP (steady state visually evoked potentials) given a set of class frequencies

Parameters:
  • x (np.ndarray) – EEG data (channels x samples) for classifier

  • frequencies (list) – list of stimulation frequencies

  • sample_rate (float) – data sampling rate

Returns:

  • float – target frequency

  • float – target threshold value

Raises:

Exception – An error is raised if prediction failed

Warning

Data must have these properties:

  • filtered with 1-90 Hz filter

  • selected channels must be from ocipital region

brainaccess.connect.processor module

brainaccess.connect.processor.cut_into_epochs(data: ndarray, sfreq: float, epoch_length: float | None = None, overlap: float = 0.5) ndarray[source]

Cut data into epochs

Parameters:
  • data – np.ndarray: (n_channels, n_times)

  • sfreq – float: sampling frequency

  • epoch_len – float: (Default value = 1.0) length of epoch in seconds

  • overlap – float: (Default value = 0.0) ratio of overlap between epochs

Returns:

np.ndarray: (n_epochs, n_channels, n_times)

Return type:

output

brainaccess.connect.processor.demean(x: ndarray) ndarray[source]

Subtract mean from each channel

Parameters:

x (np.ndarray) – data array, shape (channels, time)

Returns:

data array, shape (channels, time)

Return type:

np.ndarray

brainaccess.connect.processor.detrend(x: ndarray) ndarray[source]

Remove linear trend from each channel

Parameters:

x (np.ndarray) – data array, shape (channels, time)

Returns:

data array, shape (channels, time)

Return type:

np.ndarray

brainaccess.connect.processor.ewma(x: ndarray, alpha: float = 0.001) ndarray[source]

Exponential weighed moving average helper_function

Parameters:
  • x (np.ndarray) – data array, shape (channels, time)

  • alpha (float) – new factor

Returns:

data array, shape (channels, time)

Return type:

np.ndarray

brainaccess.connect.processor.ewma_standardize(x: ndarray, alpha: float = 0.001, epsilon: float = 0.0001) ndarray[source]

Exponential weighed moving average standardization

First-order infinite impulse response filter that applies weighting factors which decrease exponentially

Parameters:
  • x (np.ndarray) – data array, shape (channels, time)

  • alpha (float) – Represents the degree of weighting decrease, a constant smoothing factor between 0 and 1. A higher alpha discounts older observations faster.

  • epsilon (float) – Stabilizer for division by zero variance

Returns:

data array, shape (channels, time)

Return type:

np.ndarray

brainaccess.connect.processor.fft(x: ndarray, sampling_freq: float) dict[source]

Compute the discrete Fourier Transform (DFT) with the efficient Fast Fourier Transform (FFT) algorithm

Parameters:
  • x (np.ndarray) – data array, shape (channels, time)

  • sampling_freq (float) – data sampling rate

Returns:

dictionary (key: value) - freq: frequencies - mag: amplitudes - phase: phases

Return type:

dict

brainaccess.connect.processor.filter_bandpass(x: ndarray, sampling_freq: float, freq_low: float, freq_high: float) ndarray[source]

Bandpass filter

Butterworth 5th order zero phase bandpass filter

Parameters:
  • x (np.ndarray) – data array, shape (channels, time)

  • sampling_freq (float) – data sampling rate

  • freq_low (float) – frequency to filter from

  • freq_high (float) – frequency to filter to

Returns:

filtered data, shape (channels, time)

Return type:

np.ndarray

Warning

Data must be detrended or passed through ba_bci_connect_ewma_standartize before applying notch filter

brainaccess.connect.processor.filter_highpass(x: ndarray, sampling_freq: float, freq: float) ndarray[source]

High-pass filter

Butterworth 5th order zero phase high-pass filter

Parameters:
  • x (np.ndarray) – data array, shape (channels, time)

  • sampling_freq (float) – data sampling rate

  • freq (float) – edge frequency

Returns:

filtered data, shape (channels, time)

Return type:

np.ndarray

Warning

Data must be detrended or passed through ba_bci_connect_ewma_standartize before applying notch filter

brainaccess.connect.processor.filter_lowpass(x: ndarray, sampling_freq: float, freq: float) ndarray[source]

Low-pass filter

Butterworth 5th order zero phase low-pass filter

Parameters:
  • x (np.ndarray) – data array, shape (channels, time)

  • sampling_freq (float) – data sampling rate

  • freq (float) – edge frequency

Returns:

filtered data, shape (channels, time)

Return type:

np.ndarray

Warning

Data must be detrended or passed through ba_bci_connect_ewma_standartize before applying notch filter

brainaccess.connect.processor.filter_notch(x: ndarray, sampling_freq: float, center_freq: float, width_freq: float) ndarray[source]

Notch filter at desired frequency

Butterworth 4th order zero phase bandpass filter

Parameters:
  • x (np.ndarray) – data array, shape (channels, time)

  • sampling_freq (float) – data sampling rate

  • center_freq (float) – notch filter center frequency

  • width_freq (float) – notch filter width

Returns:

data array, shape (channels, time)

Return type:

np.ndarray

Warning

Data must be detrended or passed through ba_bci_connect_ewma_standartize before applying notch filter

brainaccess.connect.processor.get_bands(data: ndarray, sfreq: float, epoch_length: float | None = None, overlap: float = 0.1, normalize: bool = False)[source]

EEG power in delta, theta, alpha, beta and gamma frequency bands for each channel

Parameters:
  • data – np.ndarray: (n_channels, n_times)

  • sfreq – float: sampling frequency

  • epoch_length – Optional[float]: (Default value = None) To reduce noise, data is cut into epochs and mean power values calculated If None, epoch length is 5 seconds for data longer then 15 seconds, otherwise all data is used without epoching

  • overlap – float: (Default value = 0.1) Ratio of overlap between epochs

  • normalize – bool: (Default value = False) Normalize power in each frequency band by total power

Returns:

dict:
  • delta: np.ndarray (n_channels)

  • theta: np.ndarray (n_channels)

  • alpha: np.ndarray (n_channels)

  • beta: np.ndarray (n_channels)

  • gamma: np.ndarray (n_channels)

Return type:

output

brainaccess.connect.processor.get_minmax(x: ndarray) dict[str, ndarray][source]

Calculate min and max for each channel in the data

Parameters:

x (np.ndarray) – data array, shape (channels, time)

Returns:

min and max for each channel in the same order as x

Return type:

dict

brainaccess.connect.processor.get_pow_freq_bands(data: ndarray, sfreq: float, freq_bands: ndarray = array([0.5, 4., 8., 13., 30., 100.]), normalize: bool = False) ndarray[source]

Power Spectrum (computed by frequency bands).

Parameters:
  • data – np.ndarray: (n_channels, n_times)

  • sfreq – float: sampling frequency

  • freq_bands – np.ndarray: (Default value = np.array([0.5, 4.0, 8.0, 13.0, 30.0,

  • 100.0])) – frequency intervals defining bands: delta, theta, alpha, beta, gamma (default)

  • normalize – bool: (Default value = True) normalize power in each frequency band by total power

Returns:

ndarray, shape (n_channels, (len(freq_bands)- 1),)

Return type:

output

brainaccess.connect.processor.get_signal_quality(x: ndarray) ndarray[source]

Calculate signal quality for each channel in the data This function estimates the EEG signal quality for each channel based on amplitude variation and 50/60Hz noise level. The supplied data should be unprocessed of 2-3 seconds length. If signals do not pass the quality measures of this function, then it means that they are really corrupted or the electrodes are not fitted. Eye or muscle artifacts are not evaluated by this function, signals containing theses should still pass the quality measures.

Parameters:

x (np.ndarray) – data array, shape (channels, time)

Returns:

signal quality for each channel in the same order as x * 0 - signal is bad and did not pass any quality measure * 1 - signal passed amplitude related quality measures * 2 - signal also do not contain significant amounts of 50/60Hz noise

Return type:

np.ndarray

brainaccess.connect.processor.mad(x: ndarray) ndarray[source]

Calculate median absolute deviation for each channel in the data

Parameters:

x (np.ndarray) – data array, shape (channels, time)

Return type:

np.ndarray

brainaccess.connect.processor.mean(x: ndarray) ndarray[source]

Calculate mean for each channel in the data

Parameters:

x (np.ndarray) – data array, shape (channels, time)

Returns:

means for each channel in the same order as x

Return type:

np.ndarray

brainaccess.connect.processor.median(x: ndarray) ndarray[source]

Calculate median for each channel in the data

Parameters:

x (np.ndarray) – data array, shape (channels, time)

Returns:

medians for each channel in the same order as x

Return type:

np.ndarray

brainaccess.connect.processor.standardize(x: ndarray) ndarray[source]

Data standardization

Parameters:

x (np.ndarray) – data array, shape (channels, time)

Returns:

data array, shape (channels, time)

Return type:

np.ndarray

brainaccess.connect.processor.std(x: ndarray) ndarray[source]

Calculate standard deviation for each channel in the data

Parameters:

x (np.ndarray) – data array, shape (channels, time)

Returns:

standard deviation for each channel in the same order as x

Return type:

np.ndarray

Module contents