metabci.brainda.algorithms.decomposition.base module

class metabci.brainda.algorithms.decomposition.base.FilterBank(base_estimator: BaseEstimator, filterbank: List[ndarray], n_jobs: int | None = None)[source]

Bases: BaseEstimator, TransformerMixin

Filter bank decomposition is a bandpass filter array that divides the input signal into multiple subband components and obtains the eigenvalues of each subband component.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • base_estimator (class) – Estimator for model training and feature extraction.

  • filterbank (list[ndarray]) – A bandpass filter bank used to divide the input signal into multiple subband components.

  • n_jobs (int) – Sets the number of CPU working cores. The default is None.

References

Proceedings of the national academy of sciences, 2015, 112(44): E6058-E6067.

fit(X: ndarray, y: ndarray | None = None, **kwargs)[source]

Training model

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • X (None) – Training signal (parameters can be ignored, only used to maintain code structure).

  • y (None) – Label data (ibid., ignorable).

  • Yf (None) – Reference signal (ibid., ignorable).

transform(X: ndarray, **kwargs)[source]

The parameters stored in self are used to convert X into features, and X is filtered through the filter bank to obtain the eigenvalues of each subband component.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:

X (ndarray, shape(n_trials, n_channels, n_samples)) – Test the signal.

Returns:

feat – Feature array.

Return type:

ndarray, shape(n_trials, n_fre)

transform_filterbank(X: ndarray)[source]

The input signal is filtered through a filter bank.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:

X (ndarray, shape(n_trials, n_channels, n_samples)) – Input signal.

Returns:

Xs – Individual subband components of the input signal.

Return type:

ndarray, shape(Nfb, n_trials, n_channels, n_samples)

class metabci.brainda.algorithms.decomposition.base.FilterBankSSVEP(filterbank: List[ndarray], base_estimator: BaseEstimator, filterweights: ndarray | None = None, n_jobs: int | None = None)[source]

Bases: FilterBank

Filter bank analysis for SSVEP. The SSVEP is analyzed using filter banks, that is, multiple filters are combined to decompose the SSVEP signal into specific segments (subbands containing the original data) and obtain its characteristic data.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • filterbank (list[ndarray]) – The filter bank.

  • base_estimator (class) – Estimator for model training and feature extraction.

  • filterweights (ndarray) – Filter weight, default is None.

  • n_jobs (int) – Sets the number of CPU working cores. The default is None.

transform(X: ndarray)[source]

X is converted into features by using the parameters stored in self, and the eigenvalues of each subband component are obtained after the input signal is filtered by the filter bank.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:

X (ndarray, shape(n_trials, n_channels, n_samples)) – Test the signal.

Returns:

features – Feature array.

Return type:

ndarray, shape(n_trials, n_fre)

class metabci.brainda.algorithms.decomposition.base.TimeDecodeTool(dataset: BaseTimeEncodingDataset, feature_operation: str = 'sum')[source]

Bases: object

Decoding tool set for TDMA coding paradigm. Applicable data sets include P300 speller data set and aVEP speller data.The main functions include: dividing the trial according to the minor event, downsampling the data, and determining the target character (or instruction) according to the judgment result of the trial.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • dataset (BaseTimeEncodingDataset) – The data set to be decoded.

  • feature_operation (str) – An operation performed after feature extraction for each attempt of the same class.

decode(key: str, feature: ndarray, fold_num=6, paradigm='avep')[source]

The data is decoded according to character large label (used to determine the encoding sequence length, which can be any large label) characteristics, stimulus repetition cycles (fold_num), and normal form types.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • key (str) – Character large label.

  • feature (ndarray, shape(n_trials, n_class)) – A multidimensional array of the features of multiple attempts. The size of the array is the number of attempts x the number of template categories. Where the number of attempts is equal to the number of stimulus repeats * the length of the encoding sequence (key_encode_len).

  • fold_num (int) – The stimulation was repeated.

  • paradigm (str) – Type of paradigm.

Returns:

command – The character to be tested is predicted according to the class sequence of the test.

Return type:

str

epoch_sort(X, y)[source]

A trial-ordering method designed specifically for the classic column P300 speller. The trials are sorted in ascending order according to the trial label of a single round of characters.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • X (list) – Pre-sort data for multiple characters, where each element represents the data for all attempts of a character.

  • y (list) – A multi-character trial tag, where each element represents the label value of all the tries of a character, and the label value represents the currently blinking row or column.

Returns:

  • X_sort (list) – The sorted data of multiple characters is arranged in ascending order of the label value, where each element represents the data of all attempts of a character.

  • Y_sort (list) – After the sorting of multiple characters, each element in the ascending order of the label value represents the label value of all the tries of a character. The label value represents the current blinking row or column.

resample(x, fs_old, fs_new, axis=None)[source]

Each element is all the small labels that correspond to a character (labeled “target” and “non-target”).

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • x (ndarray) – Each element is a character corresponding to all the try labels.

  • fs_old (float) – The original sampling rate of x.

  • fs_new (float) – Sampling rate of resampling.

  • axis – Dimensions of resampling.

Returns:

x_1 – Data after resampling.

Return type:

ndarray

target_calibrate(y, key)[source]

A trial identification method specifically designed for the classic column P300 speller. According to the trial label (y) and character label (key) of the labeled column in the P300 data set, the trial label is converted into a small label that can label “target” and “non-target”.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • y (list) – Each element is a character corresponding to all the try labels.

  • key – A large label, which contains the label value (key.index) and the character corresponding to the label (key.value).

Returns:

y_tar – Each element is all the small labels corresponding to a character (labeled “target” and “non-target”).

Return type:

list

metabci.brainda.algorithms.decomposition.base.generate_cca_references(freqs: ndarray | int | float, srate, T, phases: ndarray | int | float | None = None, n_harmonics: int = 1)[source]

Construct a sine-cosine reference signal for canonical correlation analysis (CCA).

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • freqs (int or float) – Frequency.

  • srate (int) – Sampling rate.

  • T (int) – Sampling time.

  • phases (int or float) – Phase, default is None.

  • n_harmonics (int) – The number of harmonics. The default value is 1.

Returns:

Sine and cosine reference signal.

Return type:

Yf:ndarray, shape(srate*T, n_harmonics*2)

metabci.brainda.algorithms.decomposition.base.generate_filterbank(passbands: List[Tuple[float, float]], stopbands: List[Tuple[float, float]], srate: int, order: int | None = None, rp: float = 0.5)[source]

Create a filter bank, that is, obtain a bandpass filter coefficient that can divide the input signal into multiple subband components.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • passbands (list or tuple(float, float)) – Passband parameters.

  • stopbands (list or tuple(float, float)) – Stopband parameters.

  • srate (float) – Sampling rate.

  • order (int) – Filter order.

  • rp (float) – The maximum ripple allowed in the passband below the unit gain is 0.5 by default.

Returns:

Filter bank coefficient.

Return type:

Filterbank:ndarray, shape(len(passbands), N, 6)

metabci.brainda.algorithms.decomposition.base.robust_pattern(W: ndarray, Cx: ndarray, Cs: ndarray) ndarray[source]
Transform spatial filters to spatial patterns based on paper [1]_.

Referring to the method mentioned in article [1],the constructed spatial filter only shows how to combine information from different channels to extract signals of interest from EEG signals, but if our goal is neurophysiological interpretation or visualization of weights, activation patterns need to be constructed from the obtained spatial filters.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • W (ndarray) – Spatial filters, shape (n_channels, n_filters).

  • Cx (ndarray) – Covariance matrix of eeg data, shape (n_channels, n_channels).

  • Cs (ndarray) – Covariance matrix of source data, shape (n_channels, n_channels).

Returns:

A – Spatial patterns, shape (n_channels, n_patterns), each column is a spatial pattern.

Return type:

ndarray

References

metabci.brainda.algorithms.decomposition.base.sign_flip(u, s, vh=None)[source]

Flip signs of SVD or EIG using the method in paper [1]_.

update log:

2023-12-10 by Leyi Jia <18020095036@163.com>, Add code annotation

Parameters:
  • u (ndarray) – left singular vectors, shape (M, K).

  • s (ndarray) – singular values, shape (K,).

  • vh (ndarray or None) – transpose of right singular vectors, shape (K, N).

Returns:

  • u (ndarray) – corrected left singular vectors.

  • s (ndarray) – singular values.

  • vh (ndarray) – transpose of corrected right singular vectors.

References