moabb.pipelines.classification.SSVEP_TRCA#
- class moabb.pipelines.classification.SSVEP_TRCA(n_fbands=5, is_ensemble=True, method='original', estimator='scm')[source]#
Task-Related Component Analysis (TRCA) method for SSVEP detection [1].
TRCA is a data-driven spatial filtering approach that enhances SSVEP detection by maximizing the reproducibility of task-related EEG components across multiple trials. Unlike CCA which uses predefined sinusoidal references, TRCA learns optimal spatial filters directly from the training data.
Mathematical Formulation
Given \(N_t\) training trials \(\mathbf{X}^{(h)} \in \mathbb{R}^{N_c \times N_s}\) for a stimulus frequency, TRCA finds the optimal spatial filter \(\mathbf{w}\) that maximizes the inter-trial covariance while constraining the variance:
\[\hat{\mathbf{w}} = \arg\max_{\mathbf{w}} \frac{\mathbf{w}^T \mathbf{S} \mathbf{w}}{\mathbf{w}^T \mathbf{Q} \mathbf{w}}\]Inter-trial Covariance Matrix S
The matrix \(\mathbf{S}\) captures the covariance between different trials:
\[S_{j_1, j_2} = \sum_{h_1=1}^{N_t} \sum_{h_2=1, h_2 \neq h_1}^{N_t} \text{Cov}(x_{j_1}^{(h_1)}(t), x_{j_2}^{(h_2)}(t))\]where \(x_j^{(h)}(t)\) is the signal from channel \(j\) in trial \(h\).
Variance Constraint Matrix Q
The matrix \(\mathbf{Q}\) represents the pooled variance across all trials:
\[\mathbf{Q} = \sum_{h=1}^{N_t} \mathbf{X}^{(h)} (\mathbf{X}^{(h)})^T\]Generalized Eigenvalue Problem
The optimization is solved as a generalized eigenvalue problem:
\[\mathbf{S} \mathbf{w} = \lambda \mathbf{Q} \mathbf{w}\]The eigenvector corresponding to the largest eigenvalue gives the optimal spatial filter \(\hat{\mathbf{w}}\).
Template Construction
For each stimulus frequency \(f_n\), the template is the average of spatially filtered training trials:
\[\bar{\mathbf{X}}_n = \frac{1}{N_t} \sum_{h=1}^{N_t} \mathbf{X}_n^{(h)}\]Ensemble TRCA
The ensemble method combines spatial filters from all stimulus frequencies into a filter bank \(\mathbf{W} = [\mathbf{w}_1, \mathbf{w}_2, ..., \mathbf{w}_{N_f}]\) for improved robustness.
Filter Bank Approach
To capture harmonic components, EEG signals are decomposed into \(N_m\) sub-bands using a filter bank. The correlation coefficient for sub-band \(m\) is:
\[r_n^{(m)} = \rho\left((\mathbf{X}^{(m)})^T \mathbf{W}^{(m)}, (\bar{\mathbf{X}}_n^{(m)})^T \mathbf{W}^{(m)}\right)\]Classification Rule
The final feature combines sub-band correlations with weights \(a^{(m)} = m^{-1.25} + 0.25\):
\[\rho_n = \sum_{m=1}^{N_m} a^{(m)} \cdot (r_n^{(m)})^2\]The predicted class is: \(\hat{\tau} = \arg\max_n \rho_n\)
- Parameters:
n_fbands (int) – Number of sub-bands \(N_m\) for the filter bank decomposition. Each sub-band captures different harmonic components of the SSVEP. Defaults to
5.is_ensemble (bool) – If True, use ensemble TRCA which combines spatial filters from all stimulus frequencies for improved robustness. If False, use only the class-specific spatial filter. Defaults to
True.method (str) –
Method for computing the inter-trial covariance matrix \(\mathbf{S}\). Defaults to
'original'.'original': Euclidean mean as in the original paper [1].'riemann': Geodesic (Riemannian) mean, more robust to outliers but sensitive to ill-conditioned matrices.'logeuclid': Log-Euclidean mean, a computationally stable alternative to the Riemannian mean.
estimator (str) –
Covariance estimator for regularization. Defaults to
'scm'. Options include:'scm': Sample covariance matrix (no regularization, as in [1]).'lwf': Ledoit-Wolf shrinkage estimator.'oas': Oracle Approximating Shrinkage estimator.'schaefer': Schäfer-Strimmer shrinkage estimator.
- fb_coefs#
Sub-band weights \(a^{(m)} = m^{-1.25} + 0.25\) for filter bank fusion, of length
n_fbands.
- classes_#
Encoded class labels extracted at fit time, of shape
(n_classes,).- Type:
- templates_#
Average templates \(\bar{\mathbf{X}}_n^{(m)}\) for each class and sub-band, of shape
(n_classes, n_fbands, n_channels, n_samples).- Type:
- weights_#
Spatial filter weights \(\mathbf{w}_n^{(m)}\) for each sub-band and class, of shape
(n_fbands, n_classes, n_channels).- Type:
- peaks_#
Numeric frequency values for filter bank design, of shape
(n_classes,).- Type:
References
[1] (1,2,3,4)M. Nakanishi, Y. Wang, X. Chen, Y.-T. Wang, X. Gao, and T.-P. Jung, “Enhancing detection of SSVEPs for a high-speed brain speller using task-related component analysis”, IEEE Trans. Biomed. Eng, 65(1):104-112, 2018. https://doi.org/10.1109/TBME.2017.2694818
See also
SSVEP_CCACCA-based SSVEP classifier using sinusoidal references.
SSVEP_MsetCCAMulti-set CCA for learning optimal references from data.
Notes
Code based on the MATLAB implementation from the authors of [1]: mnakanishi/TRCA-SSVEP
Added in version 0.4.4.
Changed in version 1.1.1: TRCA implementation works with MNE Epochs object, fix labels encoding issue.
- fit(X, y)[source]#
Extract spatial filters and templates from the given calibration data.
- Parameters:
X (
mne.Epochs) – Training data. Trials are grouped by class, divided in n_fbands bands by the filterbank approach and then used to calculate weight vectors and templates for each class and band.y (numpy.ndarray) – Label vector in respect to X, of shape
(n_trials,).
- Returns:
self – Instance of classifier.
- Return type:
CCA
- predict(X)[source]#
Make predictions on unseen data.
The new data observation X will be filtered with weights previously extracted and compared to the templates to assess similarity with each of them and select a class based on the maximal value.
- Parameters:
X (
mne.Epochs) – Testing data. This will be divided in self.n_fbands using the filterbank approach, then it will be transformed by the different spatial filters and compared to the previously fit templates according to the selected method for analysis (ensemble or not). Finally, correlation scores for all sub-bands of each class will be combined, resulting on a single correlation score per class, from which the maximal one is identified as the predicted class of the data.- Returns:
y_pred – Prediction vector in respect to X, of shape
(n_trials,).- Return type:
- predict_proba(X)[source]#
Make predictions on unseen data with the associated probabilities.
The new data observation X will be filtered with weights previously extracted and compared to the templates to assess similarity with each of them and select a class based on the maximal value.
- Parameters:
X (numpy.ndarray) – Testing data, of shape
(n_trials, n_channels, n_samples). This will be divided in self.n_fbands using the filter-bank approach, then it will be transformed by the different spatial filters and compared to the previously fit templates according to the selected method for analysis (ensemble or not). Finally, correlation scores for all sub-bands of each class will be combined, resulting on a single correlation score per class, from which the maximal one is identified as the predicted class of the data.- Returns:
y_pred – Prediction vector in respect to X, of shape
(n_trials,).- Return type:
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') SSVEP_TRCA[source]#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.Added in version 1.3.