Cross-Session Transfer with TSMNet#

This tutorial demonstrates how to use TSMNet for cross-session motor imagery classification with domain adaptation. TSMNet’s SPDBatchNormMeanVar layer enables adaptation to new sessions without labeled data from the target session.

Introduction#

In EEG-based BCIs, a common challenge is session-to-session variability: models trained on one day often perform poorly on another day due to changes in electrode impedance, mental state, and environment.

TSMNet [Kobler et al., 2022] addresses this through SPDBatchNormMeanVar, which:

  1. Normalizes SPD matrices using the Fréchet mean

  2. Maintains running statistics that can be updated on new data

  3. Enables Source-Free Unsupervised Domain Adaptation (SFUDA)

This means we can adapt to a new subject using only unlabeled data!

Setup and Imports#

import warnings

from typing import List, Tuple

import matplotlib.pyplot as plt
import numpy as np
import torch

from braindecode import EEGClassifier
from moabb.datasets import BNCI2014_001
from moabb.paradigms import MotorImagery
from skada import (
    CORALAdapter,
    EntropicOTMapping,
    SubspaceAlignment,
    make_da_pipeline,
)
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder, StandardScaler
from skorch.callbacks import EpochScoring, GradientNormClipping
from skorch.dataset import ValidSplit

from spd_learn.models import TSMNet


warnings.filterwarnings("ignore")

Loading the Dataset#

BNCI2014_001 contains EEG recordings from 9 subjects performing - 22 EEG channels: Standard 10-20 montage - 250 Hz sampling rate: After resampling

We’ll demonstrate cross-session transfer:

  • Source domain: Subject 1, Session 1 (training)

  • Target domain: Subject 1, Session 2 (testing/adaptation)

Cross-session transfer is a realistic BCI scenario where we want to avoid recalibration for a returning user.

dataset = BNCI2014_001()
paradigm = MotorImagery(n_classes=4)

print(f"Dataset: {dataset.code}")
print("Cross-subject transfer: Subject 1 (source) -> Subject 2 (target)")
Choosing from all possible events
Dataset: BNCI2014-001
Cross-subject transfer: Subject 1 (source) -> Subject 2 (target)

Creating the TSMNet Model#

TSMNet architecture:

  1. Temporal Conv: Learns temporal filters

  2. Spatial Conv: Learns spatial combinations

  3. CovLayer: Computes covariance matrices

  4. BiMap + ReEig: SPD dimensionality reduction

  5. SPDBatchNormMeanVar: Riemannian batch normalization (key for adaptation)

  6. LogEig: Projects to tangent space

  7. Linear: Classification head

n_chans = 22
n_outputs = 4

model = TSMNet(
    n_chans=n_chans,
    n_outputs=n_outputs,
    n_temp_filters=8,  # Temporal filters
    temp_kernel_length=50,  # ~200ms at 250Hz
    n_spatiotemp_filters=32,  # Spatiotemporal features
    n_bimap_filters=16,  # BiMap output dimension
    reeig_threshold=1e-4,  # ReEig threshold
)

print("TSMNet Architecture:")
print(model)
TSMNet Architecture:
TSMNet(
  (cnn): Sequential(
    (0): Conv2d(1, 8, kernel_size=(1, 50), stride=(1, 1), padding=same, padding_mode=reflect)
    (1): Conv2d(8, 32, kernel_size=(22, 1), stride=(1, 1))
    (2): Flatten(start_dim=2, end_dim=-1)
  )
  (covpool): CovLayer()
  (spdnet): Sequential(
    (0): ParametrizedBiMap(
      (parametrizations): ModuleDict(
        (weight): ParametrizationList(
          (0): _Orthogonal()
        )
      )
    )
    (1): ReEig()
  )
  (spdbnorm): ParametrizedSPDBatchNormMeanVar(
    (parametrizations): ModuleDict(
      (weight): ParametrizationList(
        (0): PositiveDefiniteScalar()
      )
      (bias): ParametrizationList(
        (0): SymmetricPositiveDefinite()
      )
    )
  )
  (logeig): Sequential(
    (0): LogEig()
    (1): Flatten(start_dim=1, end_dim=-1)
  )
  (head): Linear(in_features=136, out_features=4, bias=True)
)

Training on Source Domain#

First, we train TSMNet on Session 1 (source domain).

source_subject = 1
target_subject = 1  # Same subject, different session
batch_size = 32
max_epochs = 300
learning_rate = 1e-4  # Optimal learning rate from grid search
weight_decay = 1e-4  # L2 regularization for better generalization

device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"\nUsing device: {device}")

# Cache configuration
cache_config = dict(
    save_raw=True,
    save_epochs=True,
    save_array=True,
    use=True,
    overwrite_raw=False,
    overwrite_epochs=False,
    overwrite_array=False,
)

# Load data for both subjects
X, labels, meta = paradigm.get_data(
    dataset=dataset,
    subjects=[source_subject, target_subject],
    cache_config=cache_config,
)

# Encode labels
le = LabelEncoder()
y = le.fit_transform(labels)

# Split by session
# Session '0train' is the first session (source)
# Session '1test' is the second session (target)
source_idx = meta.query("session == '0train'").index.to_numpy()
target_idx = meta.query("session == '1test'").index.to_numpy()

X_source, y_source = X[source_idx], y[source_idx]
X_target, y_target = X[target_idx], y[target_idx]

print(f"\nSource domain (Session 1): {len(source_idx)} samples")
print(f"Target domain (Session 2): {len(target_idx)} samples")

# Create classifier
# Note: SPD networks benefit from gradient clipping to prevent
# divergence during training on the Riemannian manifold.
clf = EEGClassifier(
    model,
    criterion=torch.nn.CrossEntropyLoss,
    optimizer=torch.optim.AdamW,
    optimizer__lr=learning_rate,
    optimizer__weight_decay=weight_decay,
    train_split=ValidSplit(0.1, stratified=True, random_state=42),
    batch_size=batch_size,
    max_epochs=max_epochs,
    callbacks=[
        (
            "train_acc",
            EpochScoring(
                "accuracy", lower_is_better=False, on_train=True, name="train_acc"
            ),
        ),
        ("gradient_clip", GradientNormClipping(gradient_clip_value=1.0)),
    ],
    device=device,
    verbose=1,
)

# Train on source domain
print("\n" + "=" * 50)
print("Training on Source Domain")
print("=" * 50)
clf.fit(X_source, y_source)
Using device: cpu

Source domain (Session 1): 240 samples
Target domain (Session 2): 288 samples

==================================================
Training on Source Domain
==================================================
  epoch    train_acc    train_loss    valid_acc    valid_loss     dur
-------  -----------  ------------  -----------  ------------  ------
      1       0.1719        1.3911       0.2917        1.3904  1.5927
      2       0.2656        1.3819       0.2917        1.3832  1.5272
      3       0.3125        1.3752       0.3333        1.3764  1.5356
      4       0.3594        1.3672       0.3750        1.3708  1.5295
      5       0.4010        1.3626       0.3333        1.3656  1.5306
      6       0.4271        1.3565       0.3333        1.3615  1.5284
      7       0.4896        1.3499       0.3333        1.3572  1.5424
      8       0.4792        1.3474       0.4583        1.3533  1.5389
      9       0.5156        1.3408       0.5000        1.3490  1.5356
     10       0.5260        1.3379       0.5000        1.3456  1.5446
     11       0.5469        1.3338       0.4583        1.3423  1.5278
     12       0.5469        1.3285       0.4167        1.3385  1.5361
     13       0.5885        1.3235       0.5417        1.3336  1.5407
     14       0.6250        1.3199       0.5417        1.3294  1.5347
     15       0.5990        1.3159       0.5000        1.3258  1.5523
     16       0.6354        1.3095       0.4583        1.3218  1.4763
     17       0.6198        1.3049       0.5000        1.3179  1.5326
     18       0.6250        1.2990       0.5000        1.3142  1.5331
     19       0.6562        1.2945       0.5833        1.3104  1.5361
     20       0.6458        1.2944       0.5417        1.3066  1.5312
     21       0.6562        1.2861       0.6250        1.3026  1.5286
     22       0.6510        1.2822       0.6250        1.2987  1.5364
     23       0.6406        1.2838       0.6250        1.2953  1.5280
     24       0.6406        1.2746       0.5833        1.2917  1.5329
     25       0.6667        1.2744       0.5000        1.2891  1.5329
     26       0.6875        1.2667       0.5417        1.2863  1.3937
     27       0.7031        1.2627       0.5833        1.2825  1.0148
     28       0.7083        1.2654       0.5833        1.2793  1.0073
     29       0.6979        1.2578       0.5833        1.2756  1.0056
     30       0.7031        1.2517       0.5833        1.2726  1.0124
     31       0.7135        1.2497       0.6250        1.2689  1.0159
     32       0.6927        1.2459       0.5833        1.2656  1.0127
     33       0.6927        1.2443       0.5417        1.2624  1.0021
     34       0.7396        1.2383       0.5833        1.2589  1.0150
     35       0.7188        1.2367       0.5833        1.2558  1.0156
     36       0.7135        1.2312       0.5833        1.2525  1.0206
     37       0.7135        1.2236       0.5417        1.2500  1.3111
     38       0.7344        1.2247       0.5417        1.2468  1.1599
     39       0.7344        1.2202       0.6250        1.2431  1.0177
     40       0.7552        1.2139       0.7083        1.2404  1.0082
     41       0.7292        1.2103       0.7083        1.2376  1.0130
     42       0.7240        1.2063       0.6250        1.2333  1.0187
     43       0.7396        1.2029       0.6667        1.2303  1.0121
     44       0.7240        1.1997       0.6667        1.2264  1.0248
     45       0.7240        1.1961       0.6250        1.2238  1.0126
     46       0.7083        1.1973       0.5833        1.2208  1.0028
     47       0.7135        1.1931       0.5417        1.2177  1.0066
     48       0.7344        1.1870       0.5417        1.2153  1.0197
     49       0.6823        1.1882       0.6250        1.2127  1.0007
     50       0.7240        1.1833       0.6250        1.2095  1.0071
     51       0.7448        1.1782       0.6250        1.2064  1.0063
     52       0.7344        1.1706       0.6250        1.2041  1.0043
     53       0.7396        1.1679       0.6667        1.2021  1.0095
     54       0.7083        1.1705       0.6250        1.1987  1.0133
     55       0.7240        1.1632       0.6667        1.1956  1.0115
     56       0.7135        1.1608       0.6250        1.1931  1.0047
     57       0.6875        1.1741       0.6667        1.1891  1.0225
     58       0.7344        1.1558       0.6667        1.1872  1.0063
     59       0.7292        1.1484       0.6250        1.1849  1.0197
     60       0.7344        1.1443       0.6250        1.1822  1.0010
     61       0.7292        1.1503       0.6667        1.1788  1.0000
     62       0.7240        1.1418       0.6250        1.1763  1.0073
     63       0.7240        1.1390       0.6250        1.1737  1.0019
     64       0.7292        1.1380       0.5833        1.1716  1.0084
     65       0.7135        1.1382       0.7083        1.1686  1.0099
     66       0.7708        1.1344       0.6250        1.1663  1.0058
     67       0.7292        1.1306       0.6250        1.1635  1.0026
     68       0.7240        1.1242       0.6667        1.1620  0.9974
     69       0.7292        1.1202       0.6667        1.1599  0.9976
     70       0.7240        1.1186       0.6250        1.1573  0.9909
     71       0.7344        1.1094       0.6250        1.1551  1.0024
     72       0.7344        1.1107       0.6250        1.1524  1.0023
     73       0.7552        1.1138       0.6250        1.1487  0.9654
     74       0.7292        1.1102       0.6250        1.1458  0.9988
     75       0.7344        1.1077       0.6250        1.1432  0.9988
     76       0.7292        1.1029       0.6250        1.1397  1.0059
     77       0.7240        1.0970       0.5833        1.1394  1.0056
     78       0.7448        1.0968       0.6250        1.1368  1.0032
     79       0.7292        1.1032       0.6250        1.1339  1.0047
     80       0.7135        1.0920       0.6250        1.1318  1.0051
     81       0.6927        1.0952       0.6250        1.1293  1.0013
     82       0.7240        1.0874       0.6250        1.1286  1.0047
     83       0.7448        1.0754       0.6250        1.1261  0.9950
     84       0.7292        1.0840       0.6250        1.1225  1.0041
     85       0.7188        1.0808       0.6250        1.1205  0.9962
     86       0.7188        1.0760       0.6250        1.1175  0.9932
     87       0.7604        1.0704       0.6250        1.1150  0.9965
     88       0.7188        1.0670       0.6250        1.1121  0.9899
     89       0.7292        1.0648       0.6250        1.1110  0.9927
     90       0.7656        1.0652       0.6667        1.1089  1.0015
     91       0.7031        1.0641       0.7083        1.1075  1.0167
     92       0.7240        1.0605       0.7083        1.1047  1.0003
     93       0.7292        1.0549       0.6250        1.1020  0.9968
     94       0.7500        1.0546       0.6250        1.1014  1.0068
     95       0.7396        1.0518       0.6667        1.0988  0.9956
     96       0.7188        1.0557       0.7083        1.0977  1.0124
     97       0.7448        1.0502       0.6667        1.0955  0.9937
     98       0.7292        1.0492       0.6250        1.0927  1.0006
     99       0.7292        1.0421       0.6250        1.0902  1.0048
    100       0.7292        1.0476       0.6250        1.0868  1.0007
    101       0.7292        1.0396       0.6250        1.0853  1.0045
    102       0.7604        1.0338       0.6250        1.0840  0.9912
    103       0.7188        1.0338       0.7083        1.0815  0.9865
    104       0.7344        1.0262       0.6667        1.0803  0.9908
    105       0.7240        1.0326       0.6667        1.0787  0.9869
    106       0.7656        1.0240       0.6667        1.0762  0.9850
    107       0.7135        1.0184       0.6667        1.0737  0.9932
    108       0.7396        1.0209       0.6250        1.0718  0.9906
    109       0.7344        1.0161       0.6250        1.0692  0.9955
    110       0.7135        1.0164       0.6667        1.0656  0.9926
    111       0.7448        1.0110       0.6250        1.0649  0.9846
    112       0.7448        1.0121       0.7083        1.0626  0.9845
    113       0.7188        1.0265       0.7083        1.0596  0.9867
    114       0.7500        1.0027       0.6667        1.0587  0.9957
    115       0.7344        1.0115       0.6250        1.0571  0.9861
    116       0.7031        1.0134       0.6250        1.0552  0.9884
    117       0.7135        1.0093       0.6250        1.0524  0.9820
    118       0.7240        1.0245       0.5833        1.0499  0.9838
    119       0.7240        0.9935       0.6250        1.0484  0.9809
    120       0.7240        0.9938       0.7083        1.0473  0.9849
    121       0.7240        0.9935       0.6667        1.0461  0.9840
    122       0.7344        1.0011       0.6250        1.0453  0.9920
    123       0.7240        0.9952       0.6250        1.0440  0.9836
    124       0.7448        0.9794       0.6250        1.0432  0.9830
    125       0.7240        0.9866       0.6667        1.0400  0.9898
    126       0.7135        0.9784       0.6667        1.0390  0.9828
    127       0.7396        0.9866       0.6667        1.0357  0.9845
    128       0.7188        0.9877       0.6250        1.0334  0.9886
    129       0.7292        0.9896       0.6667        1.0309  0.9813
    130       0.7396        0.9723       0.6667        1.0303  0.9989
    131       0.7344        0.9721       0.6250        1.0297  0.9752
    132       0.7031        0.9685       0.6250        1.0275  0.9922
    133       0.7396        0.9611       0.6250        1.0256  0.9896
    134       0.7344        0.9620       0.6250        1.0215  0.9884
    135       0.7448        0.9665       0.6667        1.0202  0.9946
    136       0.7188        0.9655       0.6250        1.0180  0.9828
    137       0.7500        0.9540       0.6250        1.0167  0.9810
    138       0.7135        0.9550       0.6250        1.0157  0.9919
    139       0.6979        0.9684       0.6250        1.0134  0.9901
    140       0.7500        0.9468       0.6250        1.0132  0.9946
    141       0.7344        0.9498       0.6250        1.0113  1.0033
    142       0.7448        0.9443       0.6667        1.0096  1.0030
    143       0.7292        0.9461       0.6667        1.0065  0.9953
    144       0.7188        0.9533       0.6250        1.0050  0.9972
    145       0.7500        0.9418       0.6667        1.0061  0.9891
    146       0.7188        0.9460       0.6250        1.0042  0.9853
    147       0.7135        0.9413       0.6250        1.0014  0.9855
    148       0.7083        0.9490       0.6250        0.9986  0.9837
    149       0.7500        0.9347       0.6667        0.9971  0.9801
    150       0.7396        0.9410       0.6250        0.9952  0.9931
    151       0.6979        0.9449       0.6250        0.9919  0.9810
    152       0.7396        0.9286       0.5833        0.9922  0.9843
    153       0.7292        0.9363       0.6250        0.9885  0.9840
    154       0.7031        0.9318       0.6250        0.9868  0.9844
    155       0.7240        0.9231       0.6667        0.9880  0.9799
    156       0.7500        0.9320       0.6667        0.9881  0.9942
    157       0.7344        0.9196       0.6250        0.9845  0.9851
    158       0.7135        0.9372       0.6250        0.9828  0.9781
    159       0.7135        0.9259       0.5833        0.9817  0.9846
    160       0.7344        0.9193       0.6250        0.9791  0.9812
    161       0.7292        0.9212       0.6667        0.9758  0.9833
    162       0.7240        0.9279       0.6250        0.9754  0.9835
    163       0.7083        0.9085       0.6250        0.9763  0.9805
    164       0.7344        0.9155       0.6250        0.9736  0.9852
    165       0.7292        0.9260       0.6667        0.9713  0.9846
    166       0.7292        0.9200       0.6250        0.9682  0.9865
    167       0.7552        0.8953       0.6250        0.9695  0.9909
    168       0.7188        0.8928       0.6250        0.9691  0.9899
    169       0.7188        0.9174       0.6250        0.9646  0.9877
    170       0.7448        0.9034       0.6667        0.9648  0.9845
    171       0.7188        0.9149       0.6667        0.9643  0.9837
    172       0.7240        0.9089       0.6250        0.9631  0.9890
    173       0.7552        0.8962       0.6667        0.9617  0.9848
    174       0.7188        0.8992       0.6667        0.9591  0.9885
    175       0.7396        0.9061       0.6250        0.9575  0.9926
    176       0.7240        0.8924       0.6667        0.9560  0.9933
    177       0.7656        0.8920       0.6667        0.9583  0.9981
    178       0.7188        0.8972       0.6250        0.9555  0.9831
    179       0.7031        0.8958       0.6250        0.9551  1.0106
    180       0.7344        0.8916       0.5833        0.9560  1.0040
    181       0.7396        0.8856       0.6250        0.9532  0.9860
    182       0.7344        0.8990       0.6250        0.9488  1.0050
    183       0.7240        0.8811       0.6667        0.9481  0.9895
    184       0.7188        0.8998       0.6667        0.9474  0.9925
    185       0.6875        0.8941       0.6250        0.9450  0.9882
    186       0.7240        0.8844       0.6250        0.9446  0.9951
    187       0.7135        0.8803       0.6667        0.9440  1.0078
    188       0.7240        0.8731       0.6667        0.9438  0.9906
    189       0.7396        0.8707       0.6250        0.9422  0.9858
    190       0.7135        0.8886       0.6667        0.9414  0.9952
    191       0.7500        0.8812       0.6667        0.9412  0.9892
    192       0.7552        0.8742       0.6667        0.9422  0.9894
    193       0.7448        0.8658       0.6250        0.9378  0.9888
    194       0.7396        0.8703       0.6250        0.9339  0.9918
    195       0.7240        0.8785       0.5833        0.9335  0.9928
    196       0.7083        0.8714       0.6250        0.9343  0.9939
    197       0.7344        0.8562       0.6250        0.9314  0.9909
    198       0.7500        0.8679       0.6250        0.9298  0.9885
    199       0.7135        0.8694       0.6667        0.9296  0.9895
    200       0.7552        0.8805       0.6667        0.9290  0.9958
    201       0.7396        0.8688       0.6250        0.9287  0.9873
    202       0.7188        0.8699       0.6250        0.9267  0.9885
    203       0.7292        0.8576       0.6250        0.9289  0.9847
    204       0.7448        0.8580       0.6667        0.9277  0.9867
    205       0.7500        0.8601       0.6250        0.9238  0.9827
    206       0.7604        0.8390       0.6667        0.9217  0.9890
    207       0.7552        0.8551       0.6250        0.9214  0.9871
    208       0.7500        0.8518       0.6250        0.9208  0.9828
    209       0.7292        0.8525       0.6250        0.9188  0.9760
    210       0.7240        0.8494       0.6250        0.9194  0.9809
    211       0.7604        0.8422       0.6250        0.9175  0.9759
    212       0.7240        0.8462       0.6250        0.9167  0.9784
    213       0.7500        0.8381       0.6667        0.9170  0.9788
    214       0.7448        0.8572       0.6250        0.9147  0.9818
    215       0.7552        0.8465       0.6667        0.9133  0.9858
    216       0.7292        0.8467       0.7083        0.9136  0.9817
    217       0.7448        0.8360       0.6250        0.9131  0.9892
    218       0.7292        0.8514       0.6667        0.9073  0.9794
    219       0.7396        0.8357       0.6667        0.9064  0.9772
    220       0.7344        0.8471       0.6667        0.9086  0.9847
    221       0.7344        0.8384       0.6667        0.9067  0.9824
    222       0.7448        0.8398       0.6250        0.9028  0.9889
    223       0.7396        0.8258       0.6250        0.9019  0.9837
    224       0.7240        0.8356       0.6250        0.9011  0.9871
    225       0.7656        0.8227       0.6250        0.8992  0.9818
    226       0.7656        0.8211       0.6250        0.8991  0.9964
    227       0.7240        0.8439       0.6250        0.8967  0.9898
    228       0.7292        0.8308       0.6250        0.8975  0.9896
    229       0.7917        0.8110       0.6667        0.8984  0.9873
    230       0.7760        0.8221       0.6250        0.8966  0.9917
    231       0.7604        0.8332       0.6250        0.8938  0.9790
    232       0.7656        0.8168       0.5833        0.8951  0.9787
    233       0.7292        0.8247       0.6250        0.8981  0.9855
    234       0.7448        0.8213       0.6667        0.8908  0.9880
    235       0.7812        0.7951       0.6667        0.8939  0.9892
    236       0.7500        0.8239       0.6667        0.8925  0.9840
    237       0.7396        0.8136       0.6250        0.8915  0.9911
    238       0.7604        0.8087       0.6250        0.8873  0.9871
    239       0.7604        0.8080       0.6250        0.8866  0.9849
    240       0.7396        0.8140       0.6250        0.8834  0.9858
    241       0.7188        0.8243       0.6250        0.8809  0.9908
    242       0.7552        0.8039       0.6667        0.8823  0.9826
    243       0.7708        0.8045       0.6667        0.8836  0.9820
    244       0.7448        0.8131       0.6667        0.8800  0.9875
    245       0.7500        0.8032       0.6667        0.8782  0.9887
    246       0.7500        0.8090       0.6667        0.8781  0.9776
    247       0.7604        0.8126       0.6667        0.8788  0.9825
    248       0.7760        0.8015       0.6250        0.8774  0.9877
    249       0.7500        0.8186       0.6667        0.8732  0.9959
    250       0.7604        0.8063       0.6667        0.8742  0.9848
    251       0.7135        0.8154       0.6250        0.8731  0.9841
    252       0.7552        0.7946       0.6250        0.8736  0.9881
    253       0.7552        0.7930       0.7083        0.8744  0.9904
    254       0.7708        0.7865       0.6667        0.8768  0.9897
    255       0.7292        0.7984       0.6250        0.8753  0.9913
    256       0.7500        0.8032       0.6667        0.8720  0.9929
    257       0.7448        0.7919       0.6667        0.8697  0.9969
    258       0.7396        0.7963       0.6667        0.8681  0.9967
    259       0.7500        0.7944       0.6250        0.8662  0.9865
    260       0.7344        0.7961       0.6667        0.8682  0.9922
    261       0.7604        0.7943       0.6667        0.8675  0.9895
    262       0.7865        0.7850       0.6250        0.8670  0.9449
    263       0.7500        0.7885       0.6250        0.8633  1.0032
    264       0.7500        0.7943       0.6250        0.8608  0.9941
    265       0.7604        0.7868       0.7083        0.8636  0.9911
    266       0.7448        0.8028       0.6667        0.8670  0.9857
    267       0.7396        0.7880       0.7083        0.8652  0.9785
    268       0.7604        0.7833       0.6250        0.8610  0.9881
    269       0.7865        0.7781       0.6250        0.8612  0.9945
    270       0.7083        0.8031       0.6667        0.8573  1.0038
    271       0.7448        0.7765       0.6250        0.8572  0.9908
    272       0.7500        0.7857       0.6250        0.8570  1.0035
    273       0.7500        0.7805       0.6667        0.8571  0.8989
    274       0.7604        0.7797       0.6667        0.8567  1.0006
    275       0.7292        0.7963       0.6250        0.8558  1.0103
    276       0.7760        0.7679       0.6250        0.8542  0.9903
    277       0.7500        0.7898       0.6250        0.8508  0.9911
    278       0.7083        0.8016       0.6250        0.8504  0.9985
    279       0.7604        0.7874       0.6667        0.8506  1.0019
    280       0.7500        0.7728       0.7083        0.8521  0.9943
    281       0.7604        0.7668       0.6667        0.8487  0.9993
    282       0.7500        0.7775       0.6250        0.8483  0.9849
    283       0.7500        0.7636       0.6250        0.8504  0.9960
    284       0.7448        0.7669       0.6667        0.8480  0.9874
    285       0.7656        0.7702       0.6667        0.8454  0.9873
    286       0.7448        0.7690       0.7083        0.8446  0.9973
    287       0.7552        0.7742       0.6667        0.8436  0.9874
    288       0.7656        0.7653       0.6667        0.8412  1.0059
    289       0.7760        0.7547       0.6250        0.8426  0.9850
    290       0.7708        0.7563       0.6667        0.8397  1.0026
    291       0.7396        0.7779       0.6250        0.8359  0.9927
    292       0.7708        0.7716       0.6667        0.8349  0.9950
    293       0.7969        0.7601       0.6667        0.8348  0.9935
    294       0.7708        0.7665       0.6667        0.8362  0.9987
    295       0.7083        0.7735       0.6667        0.8351  0.9990
    296       0.7760        0.7628       0.6667        0.8314  0.9974
    297       0.7656        0.7582       0.6667        0.8308  0.9976
    298       0.7500        0.7526       0.7083        0.8364  0.9938
    299       0.7760        0.7528       0.6667        0.8334  0.9953
    300       0.7448        0.7587       0.6667        0.8342  0.9935
<class 'braindecode.classifier.EEGClassifier'>[initialized](
  module_=TSMNet(
    (cnn): Sequential(
      (0): Conv2d(1, 8, kernel_size=(1, 50), stride=(1, 1), padding=same, padding_mode=reflect)
      (1): Conv2d(8, 32, kernel_size=(22, 1), stride=(1, 1))
      (2): Flatten(start_dim=2, end_dim=-1)
    )
    (covpool): CovLayer()
    (spdnet): Sequential(
      (0): ParametrizedBiMap(
        (parametrizations): ModuleDict(
          (weight): ParametrizationList(
            (0): _Orthogonal()
          )
        )
      )
      (1): ReEig()
    )
    (spdbnorm): ParametrizedSPDBatchNormMeanVar(
      (parametrizations): ModuleDict(
        (weight): ParametrizationList(
          (0): PositiveDefiniteScalar()
        )
        (bias): ParametrizationList(
          (0): SymmetricPositiveDefinite()
        )
      )
    )
    (logeig): Sequential(
      (0): LogEig()
      (1): Flatten(start_dim=1, end_dim=-1)
    )
    (head): Linear(in_features=136, out_features=4, bias=True)
  ),
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


Evaluating Without Adaptation#

Let’s first see how the model performs on the target domain WITHOUT any adaptation.

# Evaluate on source (should be high)
y_pred_source = clf.predict(X_source)
source_acc = accuracy_score(y_source, y_pred_source)

# Evaluate on target WITHOUT adaptation
y_pred_target_no_adapt = clf.predict(X_target)
target_acc_no_adapt = accuracy_score(y_target, y_pred_target_no_adapt)

print(f"\n{'=' * 50}")
print("Results WITHOUT Domain Adaptation")
print(f"{'=' * 50}")
print(f"Source Domain Accuracy: {source_acc * 100:.2f}%")
print(f"Target Domain Accuracy: {target_acc_no_adapt * 100:.2f}%")
print(f"Performance Drop: {(source_acc - target_acc_no_adapt) * 100:.2f}%")
==================================================
Results WITHOUT Domain Adaptation
==================================================
Source Domain Accuracy: 77.50%
Target Domain Accuracy: 68.75%
Performance Drop: 8.75%

Domain Adaptation via SPDBatchNormMeanVar#

Now we perform Source-Free Unsupervised Domain Adaptation (SFUDA):

  1. Put the model in eval mode (freeze all parameters)

  2. Put SPDBatchNormMeanVar in train mode (update running statistics)

  3. Pass target domain data through the model (no labels needed!)

  4. The running mean adapts to the target domain distribution

def adapt_spdbn(
    model,
    X_target,
    n_passes=10,
    reset_stats=False,
    adapt_momentum=0.8,
    batch_size=64,
):
    """Adapt SPDBatchNormMeanVar statistics to target domain.

    Parameters
    ----------
    model : nn.Module
        TSMNet model with SPDBatchNormMeanVar layer.
    X_target : array
        Target domain data (unlabeled).
    n_passes : int
        Number of passes through the data for statistics update.
    reset_stats : bool
        If True, reset running statistics before adaptation.
        Default is False to preserve source domain knowledge.
    adapt_momentum : float
        Momentum to use during adaptation (higher = faster adaptation).
    batch_size : int
        Batch size for adaptation (larger = more stable statistics).

    Returns
    -------
    model : nn.Module
        The adapted model with updated SPDBatchNormMeanVar statistics.
    """
    model.eval()  # Freeze other layers

    # Find SPDBatchNormMeanVar layers and configure for adaptation
    spdbn_modules = []
    original_momentums = []
    for module in model.modules():
        class_name = module.__class__.__name__
        if "SPDBatchNormMeanVar" in class_name:
            spdbn_modules.append(module)
            original_momentums.append(module.momentum)

            if reset_stats:
                # Reset to identity mean and unit variance for fresh adaptation
                module.reset_running_stats()

            module.train()  # Enable running stats update

    print(f"Found {len(spdbn_modules)} SPDBatchNormMeanVar layer(s) to adapt")

    # Convert to tensor
    X_tensor = torch.tensor(X_target, dtype=torch.float32)
    if next(model.parameters()).is_cuda:
        X_tensor = X_tensor.cuda()

    # Pass data through model multiple times to update statistics
    with torch.no_grad():
        for pass_idx in range(n_passes):
            # Set momentum for this pass
            for module in spdbn_modules:
                module.momentum = adapt_momentum

            # Reshuffle each pass for better statistics
            perm = torch.randperm(len(X_tensor))
            X_shuffled = X_tensor[perm]

            # Process in batches
            for i in range(0, len(X_shuffled), batch_size):
                batch = X_shuffled[i : i + batch_size]
                _ = model(batch)

            if (pass_idx + 1) % 10 == 0 or pass_idx == 0:
                print(f"  Adaptation pass {pass_idx + 1}/{n_passes}")

    # Restore original momentum values
    for module, orig_momentum in zip(spdbn_modules, original_momentums):
        module.momentum = orig_momentum

    model.eval()  # Set everything back to eval
    return model


def predict_with_domain_specific_bn(model, X_data):
    """Predict using domain-specific batch normalization (SPDDSMBN approach).

    This implements the key idea from Kobler et al. (NeurIPS 2022):
    Compute domain-specific statistics on the target domain and use them
    directly for normalization. This is different from standard adaptation
    which tries to blend source and target statistics.

    Parameters
    ----------
    model : nn.Module
        TSMNet model with SPDBatchNormMeanVar layer.
    X_data : array
        Target domain data to predict on.

    Returns
    -------
    predictions : array
        Predicted class labels.
    """
    # Find SPDBatchNormMeanVar layers
    spdbn_modules = []
    for module in model.modules():
        class_name = module.__class__.__name__
        if "SPDBatchNormMeanVar" in class_name:
            spdbn_modules.append(module)

    model.eval()

    # Convert to tensor
    X_tensor = torch.tensor(X_data, dtype=torch.float32)
    if next(model.parameters()).is_cuda:
        X_tensor = X_tensor.cuda()

    # Key insight from SPDDSMBN: Compute domain-specific statistics
    # by processing ALL target data with momentum=1.0 (full batch stats)
    # This estimates the target domain's Fréchet mean and variance
    for module in spdbn_modules:
        module.reset_running_stats()  # Start fresh for target domain
        module.momentum = 1.0  # Use full batch statistics
        module.train()  # Enable running stats update

    # Single pass to compute target domain statistics using all data
    with torch.no_grad():
        _ = model(X_tensor)  # This updates running_mean and running_var

    # Now predict using the target domain statistics
    model.eval()  # Back to eval mode - uses the updated running stats

    with torch.no_grad():
        logits = model(X_tensor)
        predictions = logits.argmax(dim=1).cpu().numpy()

    return predictions


def extract_features_from_tsmnet(model, X, batch_size=32):
    """Extract tangent space features from TSMNet (before classification head).

    This extracts the Euclidean features from the tangent space projection,
    which can be used with standard domain adaptation methods like CORAL,
    Subspace Alignment, and Optimal Transport.

    Parameters
    ----------
    model : nn.Module
        TSMNet model.
    X : array
        Input EEG data of shape (n_samples, n_channels, n_times).
    batch_size : int
        Batch size for processing.

    Returns
    -------
    features : np.ndarray
        Tangent space features of shape (n_samples, n_features).
    """
    model.eval()
    X_tensor = torch.tensor(X, dtype=torch.float32)
    device = next(model.parameters()).device
    X_tensor = X_tensor.to(device)

    features_list = []
    with torch.no_grad():
        for i in range(0, len(X_tensor), batch_size):
            batch = X_tensor[i : i + batch_size]
            # Process through TSMNet layers up to LogEig (before classification head)
            x = batch[:, None, ...]  # Add channel dim for CNN
            x = model.cnn(x)
            x = model.covpool(x)
            x = model.spdnet(x)
            x = model.spdbnorm(x)
            x = model.logeig(x)  # Tangent space features
            features_list.append(x.cpu())

    return torch.cat(features_list, dim=0).numpy()


def plot_domain_shift_comprehensive(
    features_source: np.ndarray,
    features_target: np.ndarray,
    y_source: np.ndarray,
    y_target: np.ndarray,
    class_names: List[str],
    title: str = "Domain Shift Visualization",
    figsize: Tuple[int, int] = (16, 12),
) -> plt.Figure:
    """Comprehensive visualization of domain shift.

    Creates a 2x3 grid showing:
    - Domain distributions (PCA)
    - Source and target by class
    - Feature histograms per domain
    - Class-conditional distributions

    Parameters
    ----------
    features_source : np.ndarray
        Source domain features.
    features_target : np.ndarray
        Target domain features.
    y_source : np.ndarray
        Source domain labels.
    y_target : np.ndarray
        Target domain labels.
    class_names : List[str]
        Names of the classes.
    title : str
        Overall figure title.
    figsize : Tuple[int, int]
        Figure size.

    Returns
    -------
    plt.Figure
        The matplotlib figure.
    """
    # Combine features for PCA
    features_all = np.vstack([features_source, features_target])
    pca = PCA(n_components=2)
    features_2d = pca.fit_transform(features_all)

    n_source = len(features_source)
    source_2d = features_2d[:n_source]
    target_2d = features_2d[n_source:]

    fig, axes = plt.subplots(2, 3, figsize=figsize)

    # --- Row 1 ---
    # Plot 1: All data by domain
    ax1 = axes[0, 0]
    ax1.scatter(
        source_2d[:, 0],
        source_2d[:, 1],
        c="blue",
        alpha=0.5,
        label="Source",
        marker="o",
        s=40,
    )
    ax1.scatter(
        target_2d[:, 0],
        target_2d[:, 1],
        c="red",
        alpha=0.5,
        label="Target",
        marker="s",
        s=40,
    )
    ax1.set_xlabel(f"PC1 ({pca.explained_variance_ratio_[0] * 100:.1f}%)")
    ax1.set_ylabel(f"PC2 ({pca.explained_variance_ratio_[1] * 100:.1f}%)")
    ax1.set_title("Domain Distribution", fontweight="bold")
    ax1.legend()
    ax1.grid(True, alpha=0.3)

    # Plot 2: Source domain by class
    ax2 = axes[0, 1]
    n_classes = len(np.unique(y_source))
    colors = plt.cm.tab10(np.linspace(0, 1, max(n_classes, 4)))[:n_classes]
    for label_idx, label in enumerate(np.unique(y_source)):
        mask = y_source == label
        ax2.scatter(
            source_2d[mask, 0],
            source_2d[mask, 1],
            c=colors[label_idx],
            alpha=0.6,
            label=f"{class_names[label_idx]}",
            marker="o",
            s=40,
        )
    ax2.set_xlabel(f"PC1 ({pca.explained_variance_ratio_[0] * 100:.1f}%)")
    ax2.set_ylabel(f"PC2 ({pca.explained_variance_ratio_[1] * 100:.1f}%)")
    ax2.set_title("Source Domain (by class)", fontweight="bold")
    ax2.legend()
    ax2.grid(True, alpha=0.3)

    # Plot 3: Target domain by class
    ax3 = axes[0, 2]
    for label_idx, label in enumerate(np.unique(y_target)):
        mask = y_target == label
        ax3.scatter(
            target_2d[mask, 0],
            target_2d[mask, 1],
            c=colors[label_idx],
            alpha=0.6,
            label=f"{class_names[label_idx]}",
            marker="s",
            s=40,
        )
    ax3.set_xlabel(f"PC1 ({pca.explained_variance_ratio_[0] * 100:.1f}%)")
    ax3.set_ylabel(f"PC2 ({pca.explained_variance_ratio_[1] * 100:.1f}%)")
    ax3.set_title("Target Domain (by class)", fontweight="bold")
    ax3.legend()
    ax3.grid(True, alpha=0.3)

    # --- Row 2 ---
    # Plot 4: Feature histogram (first 3 features)
    ax4 = axes[1, 0]
    for feat_idx in range(min(3, features_source.shape[1])):
        ax4.hist(
            features_source[:, feat_idx],
            bins=20,
            alpha=0.5,
            label=f"Source feat {feat_idx}",
            density=True,
        )
        ax4.hist(
            features_target[:, feat_idx],
            bins=20,
            alpha=0.5,
            label=f"Target feat {feat_idx}",
            density=True,
            linestyle="--",
        )
    ax4.set_xlabel("Feature Value")
    ax4.set_ylabel("Density")
    ax4.set_title("Feature Distribution (first 3)", fontweight="bold")
    ax4.legend(fontsize=8)
    ax4.grid(True, alpha=0.3)

    # Plot 5: Class-conditional distributions (Source)
    ax5 = axes[1, 1]
    for label_idx, label in enumerate(np.unique(y_source)):
        mask_s = y_source == label
        ax5.hist(
            source_2d[mask_s, 0],
            bins=15,
            alpha=0.5,
            label=f"Source-{class_names[label_idx]}",
            color=colors[label_idx],
            density=True,
        )
    ax5.set_xlabel("PC1")
    ax5.set_ylabel("Density")
    ax5.set_title("Class Distribution (Source)", fontweight="bold")
    ax5.legend()
    ax5.grid(True, alpha=0.3)

    # Plot 6: Class-conditional distributions (Target)
    ax6 = axes[1, 2]
    for label_idx, label in enumerate(np.unique(y_target)):
        mask_t = y_target == label
        ax6.hist(
            target_2d[mask_t, 0],
            bins=15,
            alpha=0.5,
            label=f"Target-{class_names[label_idx]}",
            color=colors[label_idx],
            density=True,
        )
    ax6.set_xlabel("PC1")
    ax6.set_ylabel("Density")
    ax6.set_title("Class Distribution (Target)", fontweight="bold")
    ax6.legend()
    ax6.grid(True, alpha=0.3)

    plt.suptitle(title, fontsize=14, fontweight="bold")
    plt.tight_layout()
    return fig


# Get the underlying model from the classifier
underlying_model = clf.module_

# Use Domain-Specific Batch Normalization (SPDDSMBN approach from Kobler et al.)
# This computes target-domain-specific statistics and uses them for normalization
print("\n" + "=" * 50)
print("Using Domain-Specific Batch Normalization (SPDDSMBN)")
print("=" * 50)
print("Computing target domain statistics (Fréchet mean and variance)")
==================================================
Using Domain-Specific Batch Normalization (SPDDSMBN)
==================================================
Computing target domain statistics (Fréchet mean and variance)

Evaluating After Adaptation#

Note

Cross-session transfer typically shows distribution shifts that SPDBatchNormMeanVar can correct. The improvement depends on:

  • Non-stationarity between sessions

  • Training convergence on the source session

  • How well the learned features generalize

Typical improvements range from 3-10% for cross-session transfer.

# Predict using domain-specific batch normalization
# Key insight: Use target domain statistics directly (not blended with source)
y_pred_target_adapted = predict_with_domain_specific_bn(underlying_model, X_target)

target_acc_adapted = accuracy_score(y_target, y_pred_target_adapted)
improvement = target_acc_adapted - target_acc_no_adapt

print(f"\n{'=' * 50}")
print("Results WITH Domain Adaptation")
print(f"{'=' * 50}")
print(f"Target Accuracy (No Adaptation):   {target_acc_no_adapt * 100:.2f}%")
print(f"Target Accuracy (With Adaptation): {target_acc_adapted * 100:.2f}%")
if improvement >= 0:
    print(f"Improvement: +{improvement * 100:.2f}%")
else:
    print(f"Improvement: {improvement * 100:.2f}%")
==================================================
Results WITH Domain Adaptation
==================================================
Target Accuracy (No Adaptation):   68.75%
Target Accuracy (With Adaptation): 69.10%
Improvement: +0.35%

Domain Adaptation with SKADA#

Now we compare SPDBatchNormMeanVar/TTBN with domain adaptation methods from skada (scikit-learn domain adaptation). These methods operate on the Euclidean tangent space features extracted from TSMNet.

Methods compared:

  • CORAL: Correlation Alignment - aligns second-order statistics

  • Subspace Alignment: Linear subspace mapping between domains

  • Entropic Optimal Transport: Sample-to-sample mapping

print("\n" + "=" * 50)
print("SKADA Domain Adaptation Methods")
print("=" * 50)

# Extract tangent space features for SKADA methods
features_source = extract_features_from_tsmnet(underlying_model, X_source)
features_target = extract_features_from_tsmnet(underlying_model, X_target)

print(f"Source features shape: {features_source.shape}")
print(f"Target features shape: {features_target.shape}")
==================================================
SKADA Domain Adaptation Methods
==================================================
Source features shape: (240, 136)
Target features shape: (288, 136)

Visualizing Domain Shift#

Before applying domain adaptation, let’s visualize the distribution shift between source and target sessions using PCA projection.

class_names = [str(c) for c in le.classes_]
fig = plot_domain_shift_comprehensive(
    features_source,
    features_target,
    y_source,
    y_target,
    class_names=class_names,
    title="Riemannian Feature Space - Cross-Session Distribution",
)
plt.show()

# Prepare data in SKADA format
# SKADA uses sample_domain to distinguish domains:
# - Positive values (1): Source domain
# - Negative values (-1): Target domain
X_combined = np.vstack([features_source, features_target])
y_combined = np.concatenate([y_source, -np.ones(len(y_target))])
sample_domain = np.concatenate(
    [np.ones(len(features_source)), -np.ones(len(features_target))]
)

# Initialize results dictionary
results = {
    "No Adaptation": target_acc_no_adapt,
    "SPDBatchNormMeanVar (TTBN)": target_acc_adapted,
}
Riemannian Feature Space - Cross-Session Distribution, Domain Distribution, Source Domain (by class), Target Domain (by class), Feature Distribution (first 3), Class Distribution (Source), Class Distribution (Target)

CORAL (Correlation Alignment)#

CORAL aligns the second-order statistics (covariance) of source and target feature distributions. This is particularly suitable for SPD-derived features since they already capture covariance structure.

print("\n" + "-" * 50)
print("CORAL (Correlation Alignment)")
print("-" * 50)

coral_pipeline = make_da_pipeline(
    StandardScaler(),
    CORALAdapter(reg=1e-3),
    LogisticRegression(max_iter=1000),
)
coral_pipeline.fit(X_combined, y_combined, sample_domain=sample_domain)
y_pred_coral = coral_pipeline.predict(features_target)
coral_acc = accuracy_score(y_target, y_pred_coral)

print(f"CORAL Accuracy: {coral_acc * 100:.2f}%")
print(f"Improvement over baseline: {(coral_acc - target_acc_no_adapt) * 100:+.2f}%")
results["CORAL"] = coral_acc
--------------------------------------------------
CORAL (Correlation Alignment)
--------------------------------------------------
CORAL Accuracy: 76.04%
Improvement over baseline: +7.29%

Subspace Alignment#

Subspace Alignment learns a linear transformation that aligns the principal subspaces of source and target domains.

print("\n" + "-" * 50)
print("Subspace Alignment")
print("-" * 50)

sa_clf = SubspaceAlignment(
    base_estimator=LogisticRegression(max_iter=1000),
    n_components=min(10, features_source.shape[1]),
)
sa_clf.fit(X_combined, y_combined, sample_domain=sample_domain)
y_pred_sa = sa_clf.predict(features_target)
sa_acc = accuracy_score(y_target, y_pred_sa)

print(f"Subspace Alignment Accuracy: {sa_acc * 100:.2f}%")
print(f"Improvement over baseline: {(sa_acc - target_acc_no_adapt) * 100:+.2f}%")
results["Subspace Alignment"] = sa_acc
--------------------------------------------------
Subspace Alignment
--------------------------------------------------
Subspace Alignment Accuracy: 74.65%
Improvement over baseline: +5.90%

Entropic Optimal Transport#

Optimal Transport finds the minimum cost mapping between source and target distributions. Entropic regularization makes the optimization tractable and provides smoother mappings.

print("\n" + "-" * 50)
print("Entropic Optimal Transport")
print("-" * 50)

try:
    ot_clf = EntropicOTMapping(
        base_estimator=LogisticRegression(max_iter=1000),
        reg_e=1.0,
    )
    ot_clf.fit(X_combined, y_combined, sample_domain=sample_domain)
    y_pred_ot = ot_clf.predict(features_target)
    ot_acc = accuracy_score(y_target, y_pred_ot)

    print(f"Entropic OT Accuracy: {ot_acc * 100:.2f}%")
    print(f"Improvement over baseline: {(ot_acc - target_acc_no_adapt) * 100:+.2f}%")
    results["Entropic OT"] = ot_acc
except Exception as e:
    print(f"Entropic OT failed: {e}")
--------------------------------------------------
Entropic Optimal Transport
--------------------------------------------------
Entropic OT Accuracy: 71.18%
Improvement over baseline: +2.43%

Results Summary#

print("\n" + "=" * 60)
print("Domain Adaptation Results Summary")
print("=" * 60)
print(f"{'Method':<25} {'Accuracy':>12} {'vs Baseline':>14}")
print("-" * 55)
for method, acc in results.items():
    if method == "No Adaptation":
        print(f"{method:<25} {acc * 100:>10.2f}% {'-':>14}")
    else:
        imp = acc - target_acc_no_adapt
        print(f"{method:<25} {acc * 100:>10.2f}% {imp * 100:>+12.2f}%")
print("-" * 55)
print("Chance level: 25.00% (4 classes)")

# Find best method
best_method = max(results.keys(), key=lambda k: results[k])
print(f"\nBest method: {best_method} ({results[best_method] * 100:.2f}%)")
============================================================
Domain Adaptation Results Summary
============================================================
Method                        Accuracy    vs Baseline
-------------------------------------------------------
No Adaptation                  68.75%              -
SPDBatchNormMeanVar (TTBN)      69.10%        +0.35%
CORAL                          76.04%        +7.29%
Subspace Alignment             74.65%        +5.90%
Entropic OT                    71.18%        +2.43%
-------------------------------------------------------
Chance level: 25.00% (4 classes)

Best method: CORAL (76.04%)

Visualizing Results#

fig, axes = plt.subplots(1, 3, figsize=(16, 5))

# 1. Domain Adaptation Methods Comparison
ax1 = axes[0]
methods = list(results.keys())
accuracies = [results[m] * 100 for m in methods]
colors = ["#e74c3c", "#3498db", "#2ecc71", "#9b59b6", "#f39c12"][: len(methods)]
bars = ax1.bar(methods, accuracies, color=colors, edgecolor="black", linewidth=1.5)
ax1.set_ylabel("Accuracy (%)", fontsize=12)
ax1.set_title("Domain Adaptation Comparison", fontsize=14)
ax1.set_ylim([0, 100])
ax1.axhline(y=25, color="gray", linestyle="--", alpha=0.5, label="Chance (25%)")
ax1.axhline(
    y=source_acc * 100,
    color="blue",
    linestyle=":",
    alpha=0.5,
    label=f"Source ({source_acc * 100:.1f}%)",
)

# Add value labels
for bar, acc in zip(bars, accuracies):
    ax1.text(
        bar.get_x() + bar.get_width() / 2,
        bar.get_height() + 2,
        f"{acc:.1f}%",
        ha="center",
        va="bottom",
        fontsize=9,
        fontweight="bold",
    )

ax1.legend(loc="lower right", fontsize=8)
plt.setp(ax1.xaxis.get_majorticklabels(), rotation=30, ha="right")

# 2. Training history
ax2 = axes[1]
history = clf.history
epochs_hist = range(1, len(history) + 1)
ax2.plot(epochs_hist, history[:, "train_loss"], "b-", label="Train Loss", linewidth=2)
ax2.plot(epochs_hist, history[:, "valid_loss"], "r--", label="Valid Loss", linewidth=2)
ax2.set_xlabel("Epoch", fontsize=12)
ax2.set_ylabel("Loss", fontsize=12)
ax2.set_title("Training History", fontsize=14)
ax2.legend(fontsize=10)
ax2.grid(True, alpha=0.3)

# 3. Feature space PCA visualization
ax3 = axes[2]
pca = PCA(n_components=2)
features_all = np.vstack([features_source, features_target])
features_2d = pca.fit_transform(features_all)
n_source = len(features_source)

ax3.scatter(
    features_2d[:n_source, 0],
    features_2d[:n_source, 1],
    c="blue",
    alpha=0.5,
    label="Source",
    marker="o",
    s=30,
)
ax3.scatter(
    features_2d[n_source:, 0],
    features_2d[n_source:, 1],
    c="red",
    alpha=0.5,
    label="Target",
    marker="s",
    s=30,
)
ax3.set_xlabel(f"PC1 ({pca.explained_variance_ratio_[0] * 100:.1f}%)", fontsize=12)
ax3.set_ylabel(f"PC2 ({pca.explained_variance_ratio_[1] * 100:.1f}%)", fontsize=12)
ax3.set_title("Feature Space (PCA)", fontsize=14)
ax3.legend(fontsize=10)
ax3.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()
Domain Adaptation Comparison, Training History, Feature Space (PCA)

Understanding SPDBatchNormMeanVar Adaptation#

The key insight is that session variability manifests as a shift in the distribution of SPD matrices. SPDBatchNormMeanVar counters this by:

  1. Centering: Removes the batch mean (Fréchet mean on SPD manifold)

    \[\tilde{P}_i = G^{-1/2} P_i G^{-1/2}\]
  2. Scaling: Normalizes dispersion

    \[\hat{P}_i = \tilde{P}_i^{w/\sqrt{\sigma^2 + \varepsilon}}\]

When we adapt, we update the running mean \(G\) and variance \(\sigma^2\) to match the target domain, aligning the distributions without any labeled data.

Summary#

In this tutorial, we demonstrated:

  1. Training TSMNet on source session

  2. Observing performance drop on target session

  3. Adapting using SPDBatchNormMeanVar (Test-Time Batch Normalization)

  4. Comparing with SKADA domain adaptation methods:

    • CORAL: Correlation Alignment

    • Subspace Alignment: Linear subspace mapping

    • Entropic Optimal Transport: Sample-to-sample mapping

Key insights:

  • SPDBatchNormMeanVar provides a native Riemannian approach that operates directly on SPD matrices

  • SKADA methods operate on Euclidean tangent space features and can complement or outperform SPDBatchNormMeanVar depending on the domain shift

  • Combining multiple approaches allows practitioners to select the best method for their specific use case

Cross-session non-stationarity is a key challenge in BCI. Domain adaptation methods compensate for these shifts by aligning feature distributions between source and target domains.

This unsupervised domain adaptation is particularly valuable in BCI applications where:

  • Calibration time should be minimized

  • Users return for multiple sessions

  • Signal properties drift over time

Total running time of the script: (5 minutes 24.299 seconds)