metabci.brainda.algorithms.decomposition.dsp module

class metabci.brainda.algorithms.decomposition.dsp.DCPM(n_components: int = 1, transform_method: str = 'corr')[source]

Bases: DSP, ClassifierMixin

DCPM: discriminative canonical pattern matching [1]_.

Author: Junyang Wang <2144755928@qq.com>

Create on: 2022-6-26

Update log:

Parameters:
  • n_components (int) – length of the spatial filters, first k components to use, by default 1

  • transform_method (str) – method of template matching, by default ’corr‘ (pearson correlation coefficient)

  • n_rpts (int) – repetition times in a block

n_components

length of the spatial filters, first k components to use, by default 1

Type:

int

transform_method

method of template matching, by default ’corr‘ (pearson correlation coefficient)

Type:

str

n_rpts

repetition times in a block

Type:

int

classes_

number of classes

Type:

int

combinations_

combinations of two classes in all classes

Type:

list, ([int, int], …)

n_combinations

numbers of combinations

Type:

int

Ws

spatial filter, shpe(n_channels, n_components * n_combinations)

Type:

ndarray, shape(n_channels, n_components *n_combinations)

templates

templates of train data, shape(n_classes, n_components * n_combinations, n_samples)

Type:

ndarray, shape(n_classes, n_components*n_combinations, n_samples)

M

mean value of all classes and trials, i.e. common mode signals, shape(n_channels, n_samples)

Type:

ndarray, shape(n_channels, n_samples)

References

Tip

An example using DCPM
 1from brainda.algorithms.decomposition.dsp import DCPM
 2X = np.array(data.get(‘X’))     #data(n_trials, n_channels, n_times)
 3y = data.get(‘Y’)               #labels(n_trials)
 4estimator = DCPM(n_components=2,transform_method=’corr’, n_rpts=1)
 5accs = []
 6# use ‘fit’ to get the model of train data;
 7# use ‘predict’ to get the prediction labels of test data;
 8p_labels=estimator.fit(X[train_ind], y[train_ind]).predict(X[test_ind])
 9accs.append(np.mean(p_labels==y[test_ind]))
10print(np.mean(accs))

See also

pearson_features

calculate pearson correlation coefficients

fit(X: ndarray, y: ndarray)[source]

Import the train data to get a model: Ws, templates, M.

Parameters:
  • X (ndarray, shape(n_trials, n_channels, n_samples)) – train data, shape(n_trials, n_channels, n_samples)

  • y (ndarray, shape(n_trials, )) – labels of train data, shape(n_trials, )

Returns:

  • Ws (ndarray) – spatial filters of train data, shape(n_channels, n_components * n_combinations)

  • templates (ndarray) – templates of train data, shape(n_classes, n_components*n_combinations, n_samples)

  • M (ndarray) – mean of train data (common-mode signals), shape(n_channels, n_samples)

predict(X: ndarray)[source]

Import the templates and the test data to get prediction labels.

Parameters:

X (ndarray, shape(n_trials, n_channels, n_samples)) – test data, shape(n_trials, n_channels, n_samples)

Returns:

labels – prediction labels of test data, shape(n_trials, )

Return type:

ndarray, shape(n_trials, )

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') DCPM

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

transform(X: ndarray)[source]

Import the test data to get features.

Parameters:

X (ndarray, shape(n_trials, n_channels, n_samples)) – test data, shape(n_trials, n_channels, n_samples)

Returns:

feature – features of test data, shape(n_trials, n_classes)

Return type:

ndarray, shape(n_trials,n_classes)

class metabci.brainda.algorithms.decomposition.dsp.DSP(n_components: int = 1, transform_method: str = 'corr')[source]

Bases: BaseEstimator, TransformerMixin, ClassifierMixin

DSP: Discriminal Spatial Patterns

Author: Swolf <swolfforever@gmail.com>

Created on: 2021-1-07

Update log:

Parameters:
  • n_components (int) – length of the spatial filter, first k components to use, by default 1

  • transform_method (str) – method of template matching, by default ’corr‘ (pearson correlation coefficient)

  • classes (int) – number of the EEG classes

n_components

length of the spatial filter, first k components to use, by default 1

Type:

int

transform_method

method of template matching, by default ’corr‘ (pearson correlation coefficient)

Type:

str

classes_

number of the EEG classes

Type:

int

W_

Spatial filters, shape(n_channels, n_filters), in which n_channels = n_filters

Type:

ndarray, shape(n_channels, n_filters)

D_

eigenvalues in descending order, shape(n_filters, )

Type:

ndarray, shape(n_filters, )

M_

mean value of all classes and trials, i.e. common mode signals, shape(n_channels, n_samples)

Type:

ndarray, shape(n_channels, n_samples)

A_

spatial patterns, shape(n_channels, n_filters)

Type:

ndarray, shape(n_channels, n_filters)

templates_

templates of train data, shape(n_classes, n_filters, n_samples)

Type:

ndarray, shape(n_classes, n_filters, n_samples)

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

Import the train data to get a model.

Parameters:
  • X (ndarray) – train data, shape(n_trials, n_channels, n_samples)

  • y (ndarray) – labels of train data, shape (n_trials, )

  • Yf (ndarray) – optional parameter

Returns:

  • W_ (ndarray) – spatial filters, shape (n_channels, n_filters), in which n_channels = n_filters

  • D_ (ndarray) – eigenvalues in descending order, shape (n_filters, )

  • M_ (ndarray) – template for all classes, shape (n_channel, n_samples)

  • A_ (ndarray) – spatial patterns, shape (n_channels, n_filters)

  • templates_ (ndarray) – templates of train data, shape (n_channels, n_filters, n_samples)

predict(X: ndarray)[source]

Import the templates and the test data to get prediction labels.

Parameters:

X (ndarray) – test data, shape(n_trials, n_channels, n_samples)

Returns:

labels – prediction labels of test data, shape(n_trials,)

Return type:

ndarray

set_fit_request(*, Yf: bool | None | str = '$UNCHANGED$') DSP

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

Yf (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for Yf parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') DSP

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

transform(X: ndarray)[source]

Import the test data to get features.

Parameters:

X (ndarray) – test data, shape(n_trials, n_channels, n_samples)

Returns:

feature – correlation coefficients of templates of train data and features of test data, shape(n_trials, n_classes)

Return type:

ndarray, shape(n_trials,n_classes)

class metabci.brainda.algorithms.decomposition.dsp.FBDSP(filterbank: List[ndarray], n_components: int = 1, transform_method: str = 'corr', filterweights: ndarray | None = None, n_jobs: int | None = None)[source]

Bases: FilterBankSSVEP, ClassifierMixin

FBDSP: FilterBank DSP

Author: Swolf <swolfforever@gmail.com>

Created on: 2021-1-07

Update log:

Parameters:
  • filterbank (list) – bandpass filterbank, ([float, float],…)

  • n_components (int) – length of the spatial filters, first k components to use, by default 1

  • transform_method (str) – method of template matching, by default ’corr‘ (pearson correlation coefficient)

  • filterweights (ndarray) – filter weights, optional parameter, by default None

  • n_jobs (int) – optional parameter, by default None

filterbank

bandpass filterbank, ([float, float],…)

Type:

list[[float, float], …]

n_components

length of the spatial filters, first k components to use, by default 1

Type:

int

transform_method

method of template matching, by default ’corr‘ (pearson correlation coefficient)

Type:

str

filterweights

filter weights, optional parameter, by default None

Type:

ndarray

n_jobs

optional parameter, by default None

Type:

int

classes_

number of classes

Type:

int

W_

spatial filter, shpe(n_channels, n_filters), in which n_channels = n_filters

Type:

ndarray, shape(n_channels, n_filters)

D_

eigenvalues in descending order, shape(n_filters, )

Type:

ndarray, shape(n_filters, )

M_

mean value of all classes and trials, i.e. common mode signals, shape(n_channels, n_samples)

Type:

ndarray, shape(n_channels, n_samples)

A_

spatial patterns, shape(n_channels, n_filters)

Type:

ndarray, shape(n_channels, n_filters)

templates_

templates of train data, shape(n_classes, n_filters, n_samples)

Type:

ndarray, shape(n_classes, n_filters, n_samples)

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

Import the test data to get features.

Parameters:
  • X (ndarray, shape(n_trials, n_channels, n_samples)) – train data, shape (n_trials, n_channels, n_samples)

  • y (ndarray, shape(n_trials, )) – labels of train data, shape (n_trials, )

  • Yf (ndarray) – optional parameter,

Returns:

  • W_ (ndarray) – spatial filters, shape (n_channels, n_filters)

  • D_ (ndarray) – eigenvalues in descending order

  • M_ (ndarray) – template for all classes, shape (n_channel, n_samples)

  • A_ (ndarray) – spatial patterns, shape (n_channels, n_filters)

  • templates_ (ndarray) – templates of train data, shape (n_channels, n_filters, n_samples)

predict(X: ndarray)[source]

Import the templates and the test data to get prediction labels.

Parameters:

X (ndarray, shape(n_trials, n_channels, n_samples)) – test data, shape(n_trials, n_channels, n_samples)

Returns:

labels – prediction labels of test data, shape(n_trials, )

Return type:

ndarray, shape(n_trials, )

See also

FilterBankSSVEP()

filterbank analysis (base)

set_fit_request(*, Yf: bool | None | str = '$UNCHANGED$') FBDSP

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

Yf (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for Yf parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') FBDSP

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

metabci.brainda.algorithms.decomposition.dsp.pearson_features(X, templates)[source]

Calculate pearson correlation coefficient.

Parameters:
  • X (ndarray) – features of test data after spatial filters, shape(n_trials, n_components, n_samples)

  • templates (ndarray) – templates of train data, shape(n_classes, n_components, n_samples)

Returns:

corr – pearson correlation coefficient, shape(n_trials, n_classes)

Return type:

ndarray

metabci.brainda.algorithms.decomposition.dsp.xiang_dsp_feature(W: ndarray, M: ndarray, X: ndarray, n_components: int = 1) ndarray[source]

Return DSP features in paper [1]_.

Author: Swolf <swolfforever@gmail.com>

Created on: 2021-1-07

Update log:

Parameters:
  • W (ndarray) – spatial filters from csp_kernel, shape (n_channels, n_filters)

  • M (ndarray) – common template for all classes, shape (n_channel, n_samples)

  • X (ndarray) – eeg test data, shape (n_trials, n_channels, n_samples)

  • n_components (int, optional) – length of the spatial filters, first k components to use, by default 1

Returns:

features – features, shape (n_trials, n_components, n_samples)

Return type:

ndarray

Raises:

ValueError – n_components should less than half of the number of channels

Notes

  1. instead of meaning of filtered signals in paper [1]_., we directly return filtered signals.

References

metabci.brainda.algorithms.decomposition.dsp.xiang_dsp_kernel(X: ndarray, y: ndarray) Tuple[ndarray, ndarray, ndarray, ndarray][source]

DSP: Discriminal Spatial Patterns, only for two classes[1]_. Import train data to solve spatial filters with DSP, finds a projection matrix that maximize the between-class scatter matrix and minimize the within-class scatter matrix. Currently only support for two types of data.

Author: Swolf <swolfforever@gmail.com>

Created on: 2021-1-07

Update log:

Parameters:
  • X (ndarray) – EEG train data assuming removing mean, shape (n_trials, n_channels, n_samples)

  • y (ndarray) – labels of EEG data, shape (n_trials, )

Returns:

  • W (ndarray) – spatial filters, shape (n_channels, n_filters)

  • D (ndarray) – eigenvalues in descending order

  • M (ndarray) – mean value of all classes and trials, i.e. common mode signals, shape (n_channel, n_samples)

  • A (ndarray) – spatial patterns, shape (n_channels, n_filters)

Notes

the implementation removes regularization on within-class scatter matrix Sw.

References