metabci.brainda.algorithms.decomposition.csp module

Common Spatial Patterns and his happy little buddies!

class metabci.brainda.algorithms.decomposition.csp.CSP(n_components: int | None = None, max_components: int | None = None)[source]

Bases: BaseEstimator, TransformerMixin

common spatial pattern (CSP)

author: Swolf <swolfforever@gmail.com>

Created on:2021-1-07

update log:

2023-11-06 by Yupeng Wang <zy_wyp@tju.edu.cn>

Parameters:
  • n_component (int) – Spatial filter dimension

  • max_component (int) – The maximum dimension of the selected spatial filter does not exceed half of the number of leads

n_component

Spatial filter dimension

Type:

int

max_component

The maximum dimension of the selected spatial filter does not exceed half of the number of leads

Type:

int

classes_

number of classes.

Type:

ndarray

W

Spatial filter

Type:

ndarray, shape(n_channels, n_filters)

D

Eigenvector of spatial filter

Type:

ndarray, shape(n_filters )

A

Spatial pattern

Type:

ndarray, shape(n_channels, n_filters)

best_n_components

If the number of spatial filters is not set, the optimal number of choices is calculated automatically.

Type:

int

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

model training

Parameters:
  • X (Optional, [ndarray]) – Test signal, default is None.

  • y (Optional, [ndarray]) – Label, default is None.

transform(X: ndarray)[source]

Convert X to a feature using the arguments stored in self.

Parameters:

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

Returns:

features – Find feature model, shape(n_trials, n_components).

Return type:

ndarray

class metabci.brainda.algorithms.decomposition.csp.FBCSP(n_components: int | None = None, max_components: int | None = None, n_mutualinfo_components: int | None = None, filterbank: List[ndarray] = [])[source]

Bases: FilterBank

FBCSP.

FilterBank CSP based on paper [1]_.

author: Swolf <swolfforever@gmail.com>

Created on:2021-1-07

update log:

2023-11-06 by Yupeng Wang <zy_wyp@tju.edu.cn>

Parameters:
  • n_component (int) – Spatial filter dimension

  • max_component (int) – The maximum dimension of the selected spatial filter does not exceed half of the number of leads

  • n_mutualinfo_components (int) – Multiple classification strategy, one to many.

  • filterbank (Optional, [List,[ndarray]]) – Spatial filter band division range.

n_component

Spatial filter dimension

Type:

int

max_component

The maximum dimension of the selected spatial filter does not exceed half of the number of leads

Type:

int

n_mutualinfo_components

Multiple classification strategy, one to many.

Type:

int

filterbank

Spatial filter band division range.

Type:

Optional, [List,[ndarray]]

ajd_method

Covariance matrix joint diagonalization method

Type:

str,’uwedge’

classes_

number of classes.

Type:

ndarray

W

Spatial filter

Type:

ndarray, shape(n_channels, n_filters)

mutualinfo_values_

The selected mutual information feature vector has dimension k

Type:

ndarray, shape(k,)

A

Spatial pattern

Type:

ndarray, shape(n_channels, n_filters)

best_n_components

If the number of spatial filters is not set, the optimal number of choices is calculated automatically.

Type:

int

Raises:

ValueError – None

References

interface[C]//2008 IEEE International Joint Conference on Neural Networks (IEEE World Congress on Computational Intelligence). IEEE, 2008: 2390-2397.

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

model training

Parameters:
  • X (Optional, [ndarray]) – Test signal, default is None.

  • y (Optional, [ndarray]) – Label, default is None.

transform(X: ndarray)[source]

Convert X to a feature using the arguments stored in self.

Parameters:

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

Returns:

features – Find feature model, shape(n_trials, n_components).

Return type:

ndarray

class metabci.brainda.algorithms.decomposition.csp.FBMultiCSP(n_components: int | None = None, max_components: int | None = None, multiclass: str = 'ovr', ajd_method: str = 'uwedge', n_mutualinfo_components: int | None = None, filterbank: List[ndarray] = [])[source]

Bases: FilterBank

FBMultiCSP.

The MultiCSP method based on filter banks is a decoding algorithm formed after adding filter banks and feature selection strategies to the MultiCSP algorithm.

author: Swolf <swolfforever@gmail.com>

Created on:2021-1-07

update log:

2023-11-06 by Yupeng Wang <zy_wyp@tju.edu.cn>

Parameters:
  • n_component (int) – Spatial filter dimension

  • max_component (int) – The maximum dimension of the selected spatial filter does not exceed half of the number of leads

  • n_mutualinfo_components (int) – Multiple classification strategy, one to many.

  • filterbank (Optional, [List,[ndarray]]) – Spatial filter band division range.

n_component

Spatial filter dimension

Type:

int

max_component

The maximum dimension of the selected spatial filter does not exceed half of the number of leads

Type:

int

n_mutualinfo_components

Multiple classification strategy, one to many.

Type:

int

filterbank

Spatial filter band division range.

Type:

Optional, [List,[ndarray]]

ajd_method

Covariance matrix joint diagonalization method

Type:

str,’uwedge’

classes_

number of classes.

Type:

ndarray

W

Spatial filter

Type:

ndarray, shape(n_channels, n_filters)

mutualinfo_values_

The selected mutual information feature vector has dimension k

Type:

ndarray, shape(k,)

A

Spatial pattern

Type:

ndarray, shape(n_channels, n_filters)

best_n_components

If the number of spatial filters is not set, the optimal number of choices is calculated automatically.

Type:

int

Raises:

ValueError – None

References

interface[C]//2008 IEEE International Joint Conference on Neural Networks (IEEE World Congress on Computational Intelligence). IEEE, 2008: 2390-2397.

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

model training

Parameters:
  • X (Optional, [ndarray]) – Test signal, default is None.

  • y (Optional, [ndarray]) – Label, default is None.

transform(X: ndarray)[source]

Convert X to a feature using the arguments stored in self.

Parameters:

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

Returns:

features – Find feature model, shape(n_trials, n_components).

Return type:

ndarray

class metabci.brainda.algorithms.decomposition.csp.MultiCSP(n_components: int | None = None, max_components: int | None = None, multiclass: str = 'ovr', ajd_method: str = 'uwedge')[source]

Bases: BaseEstimator, TransformerMixin

Multi common spatial pattern (MultiCSP) [1]_.

author: Swolf <swolfforever@gmail.com>

Created on:2021-1-07

update log:

2023-11-06 by Yupeng Wang <zy_wyp@tju.edu.cn>

Parameters:
  • n_component (int) – Spatial filter dimension

  • max_component (int) – The maximum dimension of the selected spatial filter does not exceed half of the number of leads

  • multiclass (int) – Multiple classification strategy, one to many.

n_component

Spatial filter dimension

Type:

int

max_component

The maximum dimension of the selected spatial filter does not exceed half of the number of leads

Type:

int

multiclass

Multiple classification strategy, one to many.

Type:

int

ajd_method

Covariance matrix joint diagonalization method

Type:

str,’uwedge’

classes_

number of classes.

Type:

ndarray

W

Spatial filter

Type:

ndarray, shape(n_channels, n_filters)

mutualinfo_values_

The selected mutual information feature vector has dimension k

Type:

ndarray, shape(k,)

A

Spatial pattern

Type:

ndarray, shape(n_channels, n_filters)

best_n_components

If the number of spatial filters is not set, the optimal number of choices is calculated automatically.

Type:

int

Raises:

ValueError – None

References

feature extraction. “ Biomedical Engineering, IEEE Transactions on 55, no. 8 (2008): 1991-2000.

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

model training

Parameters:
  • X (Optional, [ndarray]) – Test signal, default is None.

  • y (Optional, [ndarray]) – Label, default is None.

transform(X: ndarray)[source]

Convert X to a feature using the arguments stored in self.

Parameters:

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

Returns:

features – Find feature model, shape(n_trials, n_components).

Return type:

ndarray

class metabci.brainda.algorithms.decomposition.csp.SPoC(n_components: int | None = None, max_components: int | None = None)[source]

Bases: BaseEstimator, TransformerMixin

Source Power Comodulation (SPoC).

For continuous data, not verified.

fit(X: ndarray, y: ndarray)[source]
transform(X: ndarray)[source]
metabci.brainda.algorithms.decomposition.csp.ajd(X: ndarray, method: str = 'uwedge') Tuple[ndarray, ndarray][source]

Wrapper of AJD methods.

Parameters:
  • X (ndarray) – Input covariance matrices, shape (n_trials, n_channels, n_channels)

  • method (str, optional) – AJD method (default uwedge).

Returns:

  • V (ndarray) – The diagonalizer, shape (n_channels, n_filters), usually n_filters == n_channels.

  • D (ndarray) – The mean of quasi diagonal matrices, shape (n_channels,).

metabci.brainda.algorithms.decomposition.csp.csp_feature(W: ndarray, X: ndarray, n_components: int = 2) ndarray[source]

Return CSP features in paper [1]_.

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

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

  • n_components (int, optional) – the first k components to use, usually even number, by default 2

Returns:

features of shape (n_trials, n_features)

Return type:

ndarray

Raises:

ValueError – n_components should less than the number of channels

References

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

The kernel in CSP algorithm based on paper [1]_.

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

  • y (ndarray) – labels of X, shape (n_trials,).

Returns:

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

  • D (ndarray) – Eigenvalues of spatial filters, shape (n_filters,).

  • A (ndarray) – Spatial patterns, shape (n_channels, n_patterns).

References

metabci.brainda.algorithms.decomposition.csp.gw_csp_kernel(X: ndarray, y: ndarray, ajd_method: str = 'uwedge') Tuple[ndarray, ndarray, ndarray, ndarray][source]

Grosse-Wentrup AJD method based on paper [1]_.

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

  • y (ndarray) – labels, shape (n_trials).

  • ajd_method (str, optional) – ajd methods, ‘uwedge’ ‘rjd’ and ‘ajd_pham’, by default ‘uwedge’.

Returns:

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

  • D (ndarray) – Eigenvalues of spatial filters, shape (n_filters,).

  • A (ndarray) – Spatial patterns, shape (n_channels, n_patterns).

  • mutual_info (ndarray) – Mutual informaiton values, shape (n_filters).

References

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

Source Power Comodulation (SPoC) based on paper [1]_.

It is a continous CSP-like method.

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

  • y (ndarray) – labels, shape (n_trials)

Returns:

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

  • D (ndarray) – Eigenvalues of spatial filters, shape (n_filters,).

  • A (ndarray) – Spatial patterns, shape (n_channels, n_patterns).

References