brainda.algorithms.decomposition package¶
Submodules¶
brainda.algorithms.decomposition.SKLDA module¶
@ author: OrionHan @ email: jinhan9165@gmail.com @ Created on: date (e.g.2022-02-15) version 1.0 update: Refer: [1] Blankertz, et al. “Single-trial analysis and classification of ERP components—a tutorial.”
NeuroImage 56.2 (2011): 814-825.
Application:
- class brainda.algorithms.decomposition.SKLDA.SKLDA¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixinShrinkage Linear discriminant analysis (SKLDA) for BCI.
- avg_feats1¶
mean feature vector of class 1.
- Type:
ndarray of shape (n_features,)
- avg_feats2¶
mean feature vector of class 2.
- Type:
ndarray of shape (n_features,)
- sigma_c1¶
empirical covariance matrix of class 1.
- Type:
ndarray of shape (n_features, n_features)
- sigma_c2¶
empirical covariance matrix of class 2.
- Type:
ndarray of shape (n_features, n_features)
- D¶
the dimensionality of the feature space.
- Type:
int, (=n_features)
- nu_c1¶
for sigma penalty calculation in class 1.
- Type:
float
- nu_c2¶
for sigma penalty calculation in class 2.
- Type:
float
- fit(X: ndarray, y: ndarray)¶
Fit SKLDA.
- Parameters:
X1 (ndarray of shape (n_samples, n_features)) – samples for class 1 (i.e. positive samples)
X2 (ndarray of shape (n_samples, n_features)) – samples for class 2 (i.e. negative samples)
X (array-like of shape (n_samples, n_features)) – Training data.
y (array-like of shape (n_samples,)) – Target values, {-1, 1} or {0, 1}.
- Returns:
self – Some parameters (sigma_c1, sigma_c2, D) of SKLDA.
- Return type:
object
- transform(Xtest: ndarray)¶
Project data and Get the decision values.
- Parameters:
Xtest (ndarray of shape (n_samples, n_features).) – Input test data.
- Returns:
proba – decision values of all test samples.
- Return type:
ndarray of shape (n_samples,)
Notes
Some important intermediate variables are as follows.
- sigma_c1_new: ndarray of shape (n_features, n_features)
sigma penalty (i.e new covariance) in class 1.
- sigma_c2_new: ndarray of shape (n_features, n_features)
sigma penalty (i.e new covariance) in class 2.
- Sw_new: ndarray of shape (n_features, n_features)
New common covariance.
- weight_vec: ndarray of shape (n_test_samples, n_features), n_test_samples=Xtest.shape[0]
weight vector of SKLDA.
brainda.algorithms.decomposition.STDA module¶
@ author: Jin Han @ email: jinhan9165@gmail.com @ Created on: 2022-05 version 1.0 update: Refer: [1] Zhang, Yu, et al. “Spatial-temporal discriminant analysis for ERP-based brain-computer interface.”
IEEE Transactions on Neural Systems and Rehabilitation Engineering 21.2 (2013): 233-243.
Application: Spatial-Temporal Discriminant Analysis (STDA)
- class brainda.algorithms.decomposition.STDA.STDA(L: int = 6, max_iter: int = 400, eps: float = 1e-05)¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixinSpatial-Temporal Discriminant Analysis (STDA). Note that the parameters naming are exactly the same as in the paper for convenient application.
- Parameters:
L (int) – the number of eigenvectors retained for projection matrices.
max_iter (int, default=400) – Max iteration times.
eps (float, default=1e-5, also can be 1e-10.) – Error to guarantee convergence. Error = norm2(W(n) - W(n-1)), see more details in paper[1].
- W1¶
Weight vector. Actually, D1=n_chs.
- Type:
ndarray of shape (D1, self.L)
- W2¶
Weight vector. Actually, D2=n_features.
- Type:
ndarray of shape (D2, self.L)
- iter_times¶
Iteration times of STDA.
- Type:
int
- wf¶
Weight vector of LDA after the raw features are projected by STDA.
- Type:
ndarray of shape (1, L*L)
References
- [1] Zhang, Yu, et al. “Spatial-temporal discriminant analysis for ERP-based brain-computer interface.”
IEEE Transactions on Neural Systems and Rehabilitation Engineering 21.2 (2013): 233-243.
- fit(X, y)¶
Fit Spatial-Temporal Discriminant Analysis (STDA) model.
- Parameters:
X (array-like of shape (n_samples, n_chs, n_features)) – Training data.
y (array-like of shape (n_samples,)) – Target values. {-1, 1} or {0, 1}
- Returns:
self – Fitted estimator (i.e. self.W1, self.W2).
- Return type:
object
- transform(Xtest)¶
Project data and Get the decision values.
- Parameters:
Xtest (ndarray of shape (n_samples, n_features).) – Input test data.
- Returns:
H_dv – decision values.
- Return type:
ndarray of shape (n_samples, )
- brainda.algorithms.decomposition.STDA.lda_kernel(X1: ndarray, X2: ndarray)¶
Linear Discriminant analysis kernel that is appliable to binary problems.
- Parameters:
X1 (ndarray of shape (n_samples, n_features)) – samples for class 1 (i.e. positive samples)
X2 (ndarray of shape (n_samples, n_features)) – samples for class 2 (i.e. negative samples)
- Returns:
weight_vec (ndarray of shape (1, n_features)) – weight vector.
lda_threshold (float)
Note
- The test samples should be formatted as (n_samples, n_features).
test sample is positive, if W @ test_sample.T > lda_thold. test sample is negative, if W @ test_sample.T <= lda_thold.
- brainda.algorithms.decomposition.STDA.lda_proba(test_samples: ndarray, weight_vec: ndarray, lda_threshold: float)¶
Calculate decision value.
- Parameters:
test_samples (2-D, (n_samples, n_features)) –
weight_vec (from LDA_kernel.) –
lda_threshold (from LDA_kernel.) –
- Returns:
proba
- Return type:
ndarray of shape (n_samples,)
brainda.algorithms.decomposition.base module¶
- class brainda.algorithms.decomposition.base.FilterBank(base_estimator: BaseEstimator, filterbank: List[ndarray], n_jobs: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixin- fit(X: ndarray, y: Optional[ndarray] = None, **kwargs)¶
- transform(X: ndarray, **kwargs)¶
- transform_filterbank(X: ndarray)¶
- class brainda.algorithms.decomposition.base.FilterBankSSVEP(filterbank: List[ndarray], base_estimator: BaseEstimator, filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankFilter bank analysis for SSVEP.
- transform(X: ndarray)¶
- brainda.algorithms.decomposition.base.generate_cca_references(freqs, srate, T, phases: Optional[Union[ndarray, int, float]] = None, n_harmonics: int = 1)¶
- brainda.algorithms.decomposition.base.generate_filterbank(passbands: List[Tuple[float, float]], stopbands: List[Tuple[float, float]], srate: int, order: Optional[int] = None, rp: float = 0.5)¶
- brainda.algorithms.decomposition.base.robust_pattern(W: ndarray, Cx: ndarray, Cs: ndarray) ndarray¶
Transform spatial filters to spatial patterns based on paper [1]_.
- 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
- brainda.algorithms.decomposition.base.sign_flip(u, s, vh=None)¶
Flip signs of SVD or EIG using the method in paper [1]_.
- 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
[1] https://www.sandia.gov/~tgkolda/pubs/pubfiles/SAND2007-6422.pdf
brainda.algorithms.decomposition.cca module¶
CCA and its variants.
- class brainda.algorithms.decomposition.cca.ECCA(n_components: int = 1, n_jobs: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: ndarray)¶
- predict(X: ndarray)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.FBECCA(filterbank: List[ndarray], n_components: int = 1, filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.FBItCCA(filterbank: List[ndarray], n_components: int = 1, method: str = 'itcca2', filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.FBMsCCA(filterbank: List[ndarray], n_components: int = 1, filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.FBMsetCCA(filterbank: List[ndarray], n_components: int = 1, method: str = 'msetcca2', filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.FBMsetCCAR(filterbank: List[ndarray], n_components: int = 1, filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.FBSCCA(filterbank: List[ndarray], n_components: int = 1, filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.FBTRCA(filterbank: List[ndarray], n_components: int = 1, ensemble: bool = True, filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.FBTRCAR(filterbank: List[ndarray], n_components: int = 1, ensemble: bool = True, filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.FBTtCCA(filterbank: List[ndarray], n_components: int = 1, filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None, y_sub: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.ItCCA(n_components: int = 1, method: str = 'itcca2', n_jobs: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.MsCCA(n_components: int = 1, n_jobs: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixinNote: MsCCA heavily depends on Yf, thus the phase information should be included when designs Yf.
- fit(X: ndarray, y: ndarray, Yf: ndarray)¶
- predict(X: ndarray)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.MsetCCA(n_components: int = 1, method: str = 'msetcca2', n_jobs: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.MsetCCAR(n_components: int = 1, n_jobs: Optional[int] = 1)¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: ndarray)¶
- predict(X: ndarray)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.SCCA(n_components: int = 1, n_jobs: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixin- fit(X: Optional[ndarray] = None, y: Optional[ndarray] = None, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.cca.TRCA(n_components: int = 1, ensemble: bool = True, n_jobs: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- transform(X: ndarray)¶
brainda.algorithms.decomposition.csp module¶
Common Spatial Patterns and his happy little buddies!
- class brainda.algorithms.decomposition.csp.CSP(n_components: Optional[int] = None, max_components: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixinCommon Spatial Pattern.
if n_components is None, auto finding the best number of components with gridsearch. The upper searching limit is determined by max_components, default is half of the number of channels.
- fit(X: ndarray, y: ndarray)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.csp.FBCSP(n_components: Optional[int] = None, max_components: Optional[int] = None, n_mutualinfo_components: Optional[int] = None, filterbank: List[ndarray] = [])¶
Bases:
FilterBankFBCSP.
FilterBank CSP based on paper [1]_.
References
[1] Ang K K, Chin Z Y, Zhang H, et al. Filter bank common spatial pattern (FBCSP) in brain-computer 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)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.csp.FBMultiCSP(n_components: Optional[int] = None, max_components: Optional[int] = None, multiclass: str = 'ovr', ajd_method: str = 'uwedge', n_mutualinfo_components: Optional[int] = None, filterbank: List[ndarray] = [])¶
Bases:
FilterBank- fit(X: ndarray, y: ndarray)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.csp.MultiCSP(n_components: Optional[int] = None, max_components: Optional[int] = None, multiclass: str = 'ovr', ajd_method: str = 'uwedge')¶
Bases:
BaseEstimator,TransformerMixin- fit(X: ndarray, y: ndarray)¶
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.csp.SPoC(n_components: Optional[int] = None, max_components: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixinSource Power Comodulation (SPoC).
For continuous data, not verified.
- fit(X: ndarray, y: ndarray)¶
- transform(X: ndarray)¶
- brainda.algorithms.decomposition.csp.ajd(X: ndarray, method: str = 'uwedge') Tuple[ndarray, ndarray]¶
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,).
- brainda.algorithms.decomposition.csp.csp_feature(W: ndarray, X: ndarray, n_components: int = 2) ndarray¶
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
[1] Ramoser H, Muller-Gerking J, Pfurtscheller G. Optimal spatial filtering of single trial EEG during imagined hand movement[J]. IEEE transactions on rehabilitation engineering, 2000, 8(4): 441-446.
- brainda.algorithms.decomposition.csp.csp_kernel(X: ndarray, y: ndarray) Tuple[ndarray, ndarray, ndarray]¶
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
[1] Ramoser H, Muller-Gerking J, Pfurtscheller G. Optimal spatial filtering of single trial EEG during imagined hand movement[J]. IEEE transactions on rehabilitation engineering, 2000, 8(4): 441-446.
- brainda.algorithms.decomposition.csp.gw_csp_kernel(X: ndarray, y: ndarray, ajd_method: str = 'uwedge') Tuple[ndarray, ndarray, ndarray, ndarray]¶
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
[1] Grosse-Wentrup, Moritz, and Martin Buss. “Multiclass common spatial patterns and information theoretic feature extraction.” Biomedical Engineering, IEEE Transactions on 55, no. 8 (2008): 1991-2000.
- brainda.algorithms.decomposition.csp.spoc_kernel(X: ndarray, y: ndarray) Tuple[ndarray, ndarray, ndarray]¶
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
[1] Sven Dähne, Frank C. Meinecke, Stefan Haufe, Johannes Höhne, Michael Tangermann, Klaus-Robert Müller, and Vadim V. Nikulin. SPoC: a novel framework for relating the amplitude of neuronal oscillations to behaviorally relevant parameters. NeuroImage, 86:111–122, 2014. doi:10.1016/j.neuroimage.2013.07.079.
brainda.algorithms.decomposition.dsp module¶
-- coding: utf-8 -- DSP: Discriminal Spatial Patterns Authors: Swolf <swolfforever@gmail.com>
Junyang Wang <2144755928@qq.com>
Last update date: 2022-8-11 License: MIT License
- class brainda.algorithms.decomposition.dsp.DCPM(n_components: int = 1, transform_method: str = 'corr', n_rpts: int = 1)¶
Bases:
DSP,ClassifierMixinDCPM: 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
classes (int) – number of the EEG classes
combinations (list) – combinations of two classes in all classes
n_combinations (int) – length of the combinations
References
- [1] Xu MP, Xiao XL, Wang YJ, et al. A brain-computer interface based on miniature-event-related
potentials induced by very small lateral visual stimuli[J]. IEEE Transactions on Biomedical Engineering, 2018:65(5), 1166-1175.
- fit(X: ndarray, y: ndarray)¶
import the train data to get a model: Ws, templates, M
- Parameters:
X (ndarray) – train data, shape(n_trials, n_channels, n_samples)
y (ndarray) – 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)¶
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
- transform(X: ndarray)¶
import the test data to get features
- Parameters:
X (ndarray) – test data, shape(n_trials, n_channels, n_samples)
- Returns:
feature – features of test data, shape(n_trials, n_classes)
- Return type:
ndarray
- class brainda.algorithms.decomposition.dsp.DSP(n_components: int = 1, transform_method: str = 'corr')¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixinDSP: 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
- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
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)¶
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
- transform(X: ndarray)¶
import the test data to get features
- Parameters:
X (ndarray) – test data, shape(n_trials, n_channels, n_samples)
- Returns:
feature – features of test data, shape(n_trials, n_classes)
- Return type:
ndarray
- class brainda.algorithms.decomposition.dsp.FBDSP(filterbank: List[ndarray], n_components: int = 1, transform_method: str = 'corr', filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixinFBDSP: FilterBank DSP -Author: Swolf <swolfforever@gmail.com> -Created on: 2021-1-07 -Update log:
- Parameters:
filterbank (list) – 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, (float, …)
n_jobs (int) – optional parameter,
classes (int) – number of the eeg classes
- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
import the test data to get features
- 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)
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)¶
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
- brainda.algorithms.decomposition.dsp.pearson_features(X, templates)¶
- brainda.algorithms.decomposition.dsp.xiang_dsp_feature(W: ndarray, M: ndarray, X: ndarray, n_components: int = 1) ndarray¶
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
instead of meaning of filtered signals in paper [1]_., we directly return filtered signals.
References
- [1] Liao, Xiang, et al. “Combining spatial filters for the classification of single-trial EEG in
a finger movement task.” IEEE Transactions on Biomedical Engineering 54.5 (2007): 821-831.
- brainda.algorithms.decomposition.dsp.xiang_dsp_kernel(X: ndarray, y: ndarray) Tuple[ndarray, ndarray, ndarray, ndarray]¶
DSP: Discriminal Spatial Patterns, only for two classes[1] -Author: Swolf <swolfforever@gmail.com> -Created on: 2021-1-07 -Update log:
- Parameters:
X (ndarray) – EEG 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) – template for all classes, 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
- [1] Liao, Xiang, et al. “Combining spatial filters for the classification of single-trial EEG in
a finger movement task.” IEEE Transactions on Biomedical Engineering 54.5 (2007): 821-831.
brainda.algorithms.decomposition.sscor module¶
SSCOR.
- class brainda.algorithms.decomposition.sscor.FBSSCOR(n_components: int = 1, ensemble: bool = False, n_jobs: Optional[int] = None, filterbank: List[ndarray] = [], filterweights: Optional[ndarray] = None)¶
Bases:
FilterBankFilter Bank SSCOR method in paper [1]_., [2]_.
filterbank and weights suggested in the paper.
- wp = [
[6, 90], [14, 90], [22, 90], [30, 90], [38, 90], [46, 90], [54, 90], [62, 90], [70, 90], [78, 90]
] ws = [
[4, 100], [10, 100], [16, 100], [24, 100], [32, 100], [40, 100], [48, 100], [56, 100], [64, 100], [72, 100]
]
- filterweights:
np.arange(1, 11)**(-1.25) + 0.25
References
[1] Kumar G R K, Reddy M R. Designing a sum of squared correlations framework for enhancing SSVEP-based BCIs[J]. IEEE Transactions on Neural Systems and Rehabilitation Engineering, 2019, 27(10): 2044-2050.
[2] Kumar G R K, Reddy M R. Correction to “Designing a Sum of Squared Correlations Framework for Enhancing SSVEP Based BCIs”[J]. IEEE Transactions on Neural Systems and Rehabilitation Engineering, 2020, 28(4): 1044-1045.
- transform(X: ndarray)¶
- class brainda.algorithms.decomposition.sscor.SSCOR(n_components: int = 1, transform_method: Optional[str] = None, ensemble: bool = False, n_jobs: Optional[int] = None)¶
Bases:
BaseEstimator,TransformerMixin- fit(X: ndarray, y: ndarray)¶
- transform(X: ndarray)¶
- brainda.algorithms.decomposition.sscor.sscor_feature(W: ndarray, X: ndarray, n_components: int = 1) ndarray¶
Return sscor features.
Modified from https://github.com/mnakanishi/TRCA-SSVEP/blob/master/src/test_sscor.m
- 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 1
- Returns:
features of shape (n_trials, n_components, n_samples)
- Return type:
ndarray
- Raises:
ValueError – n_components should less than half of the number of channels
- brainda.algorithms.decomposition.sscor.sscor_kernel(X: ndarray, y: Optional[ndarray] = None, n_jobs: Optional[int] = None) Tuple[ndarray, ndarray, ndarray]¶
The kernel part in SSCOR algorithm based on paper[1]_., [2]_.
Modified from https://github.com/mnakanishi/TRCA-SSVEP/blob/master/src/train_sscor.m
- Parameters:
X (ndarray) – EEG data assuming removing mean, shape (n_trials, n_channels, n_samples)
y (ndarray) – labels, shape (n_trials, ), not used here
n_jobs (int, optional) – the number of jobs to use, default None
- Returns:
W (ndarray) – filters, shape (n_channels, n_filters)
D (ndarray) – eigenvalues in descending order
A (ndarray) – spatial patterns, shape (n_channels, n_filters)
References
[1] Kumar G R K, Reddy M R. Designing a sum of squared correlations framework for enhancing SSVEP-based BCIs[J]. IEEE Transactions on Neural Systems and Rehabilitation Engineering, 2019, 27(10): 2044-2050.
[2] Kumar G R K, Reddy M R. Correction to “Designing a Sum of Squared Correlations Framework for Enhancing SSVEP Based BCIs”[J]. IEEE Transactions on Neural Systems and Rehabilitation Engineering, 2020, 28(4): 1044-1045.
brainda.algorithms.decomposition.tdca module¶
Task Decomposition Component Analysis.
- class brainda.algorithms.decomposition.tdca.FBTDCA(filterbank: List[ndarray], padding_len: int, n_components: int = 1, filterweights: Optional[ndarray] = None, n_jobs: Optional[int] = None)¶
Bases:
FilterBankSSVEP,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: Optional[ndarray] = None)¶
- predict(X: ndarray)¶
- class brainda.algorithms.decomposition.tdca.TDCA(padding_len: int, n_components: int = 1)¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixin- fit(X: ndarray, y: ndarray, Yf: ndarray)¶
- predict(X: ndarray)¶
- transform(X: ndarray)¶
- brainda.algorithms.decomposition.tdca.aug_2(X: ndarray, n_samples: int, padding_len: int, P: ndarray, training: bool = True)¶
- brainda.algorithms.decomposition.tdca.proj_ref(Yf: ndarray)¶
- brainda.algorithms.decomposition.tdca.tdca_feature(X: ndarray, templates: ndarray, W: ndarray, M: ndarray, Ps: List[ndarray], padding_len: int, n_components: int = 1, training=False)¶