Comparison of embeddings of covariance matrices

Comparison of several embeddings of a set of ERP covariance matrices extracted on MEG data: SE, LLE and t-SNE

Spectral Embedding (SE) is based on computing the low-dimensional representation that best preserves locality instead of local linearity in LLE [1].

Locally Linear Embedding (LLE) assumes that the local neighborhood of a matrix on the manifold can be well approximated by the affine subspace spanned by the k-nearest neighbors of the matrix and finds a low-dimensional embedding of the data based on these affine approximations.

t-SNE reduces SPD matrices into lower dimensional SPD matrices by computing conditional probabilities that represent similarities [2]. This fully Riemannian algorithm helps preserve the non-Euclidean structure of the data.

# Authors:  Pedro Rodrigues <pedro.rodrigues01@gmail.com>,
#           Gabriel Wagner vom Berg <gabriel@bccn-berlin.de>
#           Thibault de Surrel <thibault.de-surrel@lamsade.dauphine.fr>
#
# License: BSD (3-clause)

import matplotlib.pyplot as plt
import mne
from mne import io
from mne.datasets import sample
from sklearn.model_selection import train_test_split

from pyriemann.estimation import XdawnCovariances
from pyriemann.utils.viz import plot_embedding

print(__doc__)

Set parameters and read data

data_path = str(sample.data_path())
raw_fname = data_path + "/MEG/sample/sample_audvis_filt-0-40_raw.fif"
event_fname = data_path + "/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif"
tmin, tmax = -0., 1
event_id = dict(aud_l=1, aud_r=2, vis_l=3, vis_r=4)

# Setup for reading the raw data
raw = io.Raw(raw_fname, preload=True, verbose=False)
raw.filter(2, None, method="iir")  # replace baselining with high-pass
events = mne.read_events(event_fname)

raw.info["bads"] = ["MEG 2443"]  # set bad channels
picks = mne.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=False,
                       exclude="bads")

# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=False,
                    picks=picks, baseline=None, preload=True, verbose=False)

X = epochs.get_data(copy=False)
y = epochs.events[:, -1]
Using default location ~/mne_data for sample...

  0%|                                              | 0.00/1.65G [00:00<?, ?B/s]
  0%|                                      | 179k/1.65G [00:00<15:43, 1.75MB/s]
  0%|                                     | 2.27M/1.65G [00:00<02:08, 12.9MB/s]
  1%|▏                                    | 10.3M/1.65G [00:00<00:37, 43.6MB/s]
  1%|▍                                    | 19.0M/1.65G [00:00<00:27, 60.4MB/s]
  2%|▌                                    | 27.7M/1.65G [00:00<00:23, 70.0MB/s]
  2%|▊                                    | 36.3M/1.65G [00:00<00:21, 75.7MB/s]
  3%|█                                    | 45.0M/1.65G [00:00<00:20, 79.2MB/s]
  3%|█▏                                   | 53.6M/1.65G [00:00<00:19, 81.4MB/s]
  4%|█▍                                   | 62.2M/1.65G [00:00<00:19, 83.0MB/s]
  4%|█▌                                   | 70.9M/1.65G [00:01<00:18, 84.1MB/s]
  5%|█▊                                   | 79.4M/1.65G [00:01<00:18, 84.4MB/s]
  5%|█▉                                   | 88.0M/1.65G [00:01<00:18, 84.9MB/s]
  6%|██▏                                  | 96.5M/1.65G [00:01<00:18, 85.0MB/s]
  6%|██▍                                   | 105M/1.65G [00:01<00:18, 85.1MB/s]
  7%|██▌                                   | 114M/1.65G [00:01<00:18, 85.4MB/s]
  7%|██▊                                   | 122M/1.65G [00:01<00:17, 85.7MB/s]
  8%|███                                   | 131M/1.65G [00:01<00:17, 86.0MB/s]
  8%|███▏                                  | 140M/1.65G [00:01<00:17, 86.1MB/s]
  9%|███▍                                  | 148M/1.65G [00:01<00:17, 86.3MB/s]
  9%|███▌                                  | 157M/1.65G [00:02<00:17, 86.3MB/s]
 10%|███▊                                  | 166M/1.65G [00:02<00:17, 86.2MB/s]
 11%|████                                  | 174M/1.65G [00:02<00:17, 86.2MB/s]
 11%|████▏                                 | 183M/1.65G [00:02<00:17, 86.2MB/s]
 12%|████▍                                 | 191M/1.65G [00:02<00:16, 86.3MB/s]
 12%|████▌                                 | 200M/1.65G [00:02<00:16, 86.2MB/s]
 13%|████▊                                 | 209M/1.65G [00:02<00:16, 86.2MB/s]
 13%|████▉                                 | 217M/1.65G [00:02<00:16, 86.2MB/s]
 14%|█████▏                                | 226M/1.65G [00:02<00:16, 86.2MB/s]
 14%|█████▍                                | 235M/1.65G [00:02<00:16, 86.2MB/s]
 15%|█████▌                                | 243M/1.65G [00:03<00:16, 86.2MB/s]
 15%|█████▊                                | 252M/1.65G [00:03<00:16, 86.2MB/s]
 16%|█████▉                                | 260M/1.65G [00:03<00:16, 86.3MB/s]
 16%|██████▏                               | 269M/1.65G [00:03<00:16, 86.4MB/s]
 17%|██████▍                               | 278M/1.65G [00:03<00:15, 86.4MB/s]
 17%|██████▌                               | 286M/1.65G [00:03<00:15, 86.4MB/s]
 18%|██████▊                               | 295M/1.65G [00:03<00:15, 86.4MB/s]
 18%|██████▉                               | 304M/1.65G [00:03<00:15, 86.4MB/s]
 19%|███████▏                              | 312M/1.65G [00:03<00:15, 86.5MB/s]
 19%|███████▍                              | 321M/1.65G [00:03<00:15, 86.5MB/s]
 20%|███████▌                              | 330M/1.65G [00:04<00:15, 86.5MB/s]
 20%|███████▊                              | 338M/1.65G [00:04<00:15, 86.5MB/s]
 21%|███████▉                              | 347M/1.65G [00:04<00:15, 86.5MB/s]
 22%|████████▏                             | 356M/1.65G [00:04<00:14, 86.5MB/s]
 22%|████████▍                             | 364M/1.65G [00:04<00:14, 86.5MB/s]
 23%|████████▌                             | 373M/1.65G [00:04<00:14, 86.5MB/s]
 23%|████████▊                             | 382M/1.65G [00:04<00:14, 86.4MB/s]
 24%|████████▉                             | 390M/1.65G [00:04<00:14, 86.3MB/s]
 24%|█████████▏                            | 399M/1.65G [00:04<00:14, 86.4MB/s]
 25%|█████████▎                            | 408M/1.65G [00:04<00:14, 86.6MB/s]
 25%|█████████▌                            | 416M/1.65G [00:05<00:17, 70.5MB/s]
 26%|█████████▋                            | 424M/1.65G [00:05<00:16, 72.3MB/s]
 26%|█████████▉                            | 433M/1.65G [00:05<00:16, 76.0MB/s]
 27%|██████████▏                           | 441M/1.65G [00:05<00:15, 78.8MB/s]
 27%|██████████▎                           | 450M/1.65G [00:05<00:14, 80.9MB/s]
 28%|██████████▌                           | 458M/1.65G [00:05<00:14, 82.4MB/s]
 28%|██████████▋                           | 467M/1.65G [00:05<00:14, 83.5MB/s]
 29%|██████████▉                           | 476M/1.65G [00:05<00:13, 84.2MB/s]
 29%|███████████▏                          | 484M/1.65G [00:05<00:13, 84.8MB/s]
 30%|███████████▎                          | 493M/1.65G [00:05<00:13, 85.2MB/s]
 30%|███████████▌                          | 502M/1.65G [00:06<00:13, 85.6MB/s]
 31%|███████████▋                          | 510M/1.65G [00:06<00:13, 85.7MB/s]
 31%|███████████▉                          | 519M/1.65G [00:06<00:13, 85.9MB/s]
 32%|████████████▏                         | 527M/1.65G [00:06<00:13, 85.9MB/s]
 32%|████████████▎                         | 536M/1.65G [00:06<00:12, 86.0MB/s]
 33%|████████████▌                         | 545M/1.65G [00:06<00:12, 86.0MB/s]
 33%|████████████▋                         | 553M/1.65G [00:06<00:12, 86.1MB/s]
 34%|████████████▉                         | 562M/1.65G [00:06<00:12, 86.1MB/s]
 35%|█████████████                         | 570M/1.65G [00:06<00:12, 86.1MB/s]
 35%|█████████████▎                        | 579M/1.65G [00:06<00:12, 86.2MB/s]
 36%|█████████████▌                        | 588M/1.65G [00:07<00:12, 86.2MB/s]
 36%|█████████████▋                        | 596M/1.65G [00:07<00:12, 86.2MB/s]
 37%|█████████████▉                        | 605M/1.65G [00:07<00:12, 86.2MB/s]
 37%|██████████████                        | 614M/1.65G [00:07<00:12, 86.4MB/s]
 38%|██████████████▎                       | 622M/1.65G [00:07<00:11, 86.4MB/s]
 38%|██████████████▌                       | 631M/1.65G [00:07<00:11, 86.3MB/s]
 39%|██████████████▋                       | 640M/1.65G [00:07<00:11, 86.3MB/s]
 39%|██████████████▉                       | 648M/1.65G [00:07<00:11, 86.4MB/s]
 40%|███████████████                       | 657M/1.65G [00:07<00:11, 86.4MB/s]
 40%|███████████████▎                      | 666M/1.65G [00:07<00:11, 86.5MB/s]
 41%|███████████████▍                      | 674M/1.65G [00:08<00:11, 86.4MB/s]
 41%|███████████████▋                      | 683M/1.65G [00:08<00:11, 86.4MB/s]
 42%|███████████████▉                      | 691M/1.65G [00:08<00:11, 86.4MB/s]
 42%|████████████████                      | 700M/1.65G [00:08<00:11, 86.3MB/s]
 43%|████████████████▎                     | 709M/1.65G [00:08<00:10, 86.3MB/s]
 43%|████████████████▍                     | 717M/1.65G [00:08<00:10, 86.4MB/s]
 44%|████████████████▋                     | 726M/1.65G [00:08<00:10, 86.5MB/s]
 44%|████████████████▉                     | 735M/1.65G [00:08<00:10, 86.4MB/s]
 45%|█████████████████                     | 743M/1.65G [00:08<00:10, 86.5MB/s]
 45%|█████████████████▎                    | 752M/1.65G [00:08<00:10, 86.4MB/s]
 46%|█████████████████▍                    | 761M/1.65G [00:09<00:10, 86.5MB/s]
 47%|█████████████████▋                    | 769M/1.65G [00:09<00:10, 86.5MB/s]
 47%|█████████████████▉                    | 778M/1.65G [00:09<00:10, 86.4MB/s]
 48%|██████████████████                    | 787M/1.65G [00:09<00:10, 86.4MB/s]
 48%|██████████████████▎                   | 795M/1.65G [00:09<00:09, 86.4MB/s]
 49%|██████████████████▍                   | 804M/1.65G [00:09<00:09, 86.3MB/s]
 49%|██████████████████▋                   | 813M/1.65G [00:09<00:09, 86.4MB/s]
 50%|██████████████████▉                   | 821M/1.65G [00:09<00:09, 86.4MB/s]
 50%|███████████████████                   | 830M/1.65G [00:09<00:09, 86.5MB/s]
 51%|███████████████████▎                  | 838M/1.65G [00:09<00:09, 86.4MB/s]
 51%|███████████████████▍                  | 847M/1.65G [00:10<00:09, 86.4MB/s]
 52%|███████████████████▋                  | 856M/1.65G [00:10<00:09, 86.5MB/s]
 52%|███████████████████▊                  | 864M/1.65G [00:10<00:09, 86.5MB/s]
 53%|████████████████████                  | 873M/1.65G [00:10<00:09, 86.4MB/s]
 53%|████████████████████▎                 | 882M/1.65G [00:10<00:08, 86.3MB/s]
 54%|████████████████████▍                 | 890M/1.65G [00:10<00:08, 86.3MB/s]
 54%|████████████████████▋                 | 899M/1.65G [00:10<00:08, 86.3MB/s]
 55%|████████████████████▊                 | 908M/1.65G [00:10<00:08, 86.3MB/s]
 55%|█████████████████████                 | 916M/1.65G [00:10<00:08, 86.3MB/s]
 56%|█████████████████████▎                | 925M/1.65G [00:10<00:08, 86.3MB/s]
 56%|█████████████████████▍                | 934M/1.65G [00:11<00:08, 86.2MB/s]
 57%|█████████████████████▋                | 942M/1.65G [00:11<00:08, 86.3MB/s]
 58%|█████████████████████▊                | 951M/1.65G [00:11<00:08, 83.5MB/s]
 58%|██████████████████████                | 959M/1.65G [00:11<00:08, 84.3MB/s]
 59%|██████████████████████▎               | 968M/1.65G [00:11<00:08, 84.9MB/s]
 59%|██████████████████████▍               | 977M/1.65G [00:11<00:07, 85.5MB/s]
 60%|██████████████████████▋               | 985M/1.65G [00:11<00:07, 85.8MB/s]
 60%|██████████████████████▊               | 994M/1.65G [00:11<00:07, 86.0MB/s]
 61%|██████████████████████▍              | 1.00G/1.65G [00:11<00:07, 86.0MB/s]
 61%|██████████████████████▋              | 1.01G/1.65G [00:11<00:07, 86.2MB/s]
 62%|██████████████████████▊              | 1.02G/1.65G [00:12<00:07, 86.2MB/s]
 62%|███████████████████████              | 1.03G/1.65G [00:12<00:07, 86.3MB/s]
 63%|███████████████████████▏             | 1.04G/1.65G [00:12<00:07, 86.2MB/s]
 63%|███████████████████████▍             | 1.05G/1.65G [00:12<00:07, 86.3MB/s]
 64%|███████████████████████▌             | 1.05G/1.65G [00:12<00:06, 86.4MB/s]
 64%|███████████████████████▊             | 1.06G/1.65G [00:12<00:06, 86.4MB/s]
 65%|███████████████████████▉             | 1.07G/1.65G [00:12<00:06, 86.4MB/s]
 65%|████████████████████████▏            | 1.08G/1.65G [00:12<00:06, 86.2MB/s]
 66%|████████████████████████▍            | 1.09G/1.65G [00:12<00:06, 86.4MB/s]
 66%|████████████████████████▌            | 1.10G/1.65G [00:13<00:07, 70.1MB/s]
 67%|████████████████████████▊            | 1.11G/1.65G [00:13<00:07, 73.9MB/s]
 67%|████████████████████████▉            | 1.11G/1.65G [00:13<00:06, 77.2MB/s]
 68%|█████████████████████████▏           | 1.12G/1.65G [00:13<00:06, 79.7MB/s]
 68%|█████████████████████████▎           | 1.13G/1.65G [00:13<00:06, 81.5MB/s]
 69%|█████████████████████████▌           | 1.14G/1.65G [00:13<00:06, 82.7MB/s]
 70%|█████████████████████████▋           | 1.15G/1.65G [00:13<00:06, 83.7MB/s]
 70%|█████████████████████████▉           | 1.16G/1.65G [00:13<00:05, 84.3MB/s]
 71%|██████████████████████████           | 1.17G/1.65G [00:13<00:05, 84.6MB/s]
 71%|██████████████████████████▎          | 1.17G/1.65G [00:13<00:05, 84.6MB/s]
 72%|██████████████████████████▍          | 1.18G/1.65G [00:14<00:05, 84.7MB/s]
 72%|██████████████████████████▋          | 1.19G/1.65G [00:14<00:05, 84.8MB/s]
 73%|██████████████████████████▊          | 1.20G/1.65G [00:14<00:05, 84.7MB/s]
 73%|███████████████████████████          | 1.21G/1.65G [00:14<00:05, 84.8MB/s]
 74%|███████████████████████████▎         | 1.22G/1.65G [00:14<00:05, 84.7MB/s]
 74%|███████████████████████████▍         | 1.23G/1.65G [00:14<00:05, 84.5MB/s]
 75%|███████████████████████████▋         | 1.23G/1.65G [00:14<00:04, 84.5MB/s]
 75%|███████████████████████████▊         | 1.24G/1.65G [00:14<00:04, 84.5MB/s]
 76%|████████████████████████████         | 1.25G/1.65G [00:14<00:04, 84.5MB/s]
 76%|████████████████████████████▏        | 1.26G/1.65G [00:14<00:04, 84.5MB/s]
 77%|████████████████████████████▍        | 1.27G/1.65G [00:15<00:04, 84.6MB/s]
 77%|████████████████████████████▌        | 1.28G/1.65G [00:15<00:04, 84.7MB/s]
 78%|████████████████████████████▊        | 1.29G/1.65G [00:15<00:04, 84.6MB/s]
 78%|████████████████████████████▉        | 1.29G/1.65G [00:15<00:04, 84.6MB/s]
 79%|█████████████████████████████▏       | 1.30G/1.65G [00:15<00:04, 84.7MB/s]
 79%|█████████████████████████████▎       | 1.31G/1.65G [00:15<00:04, 84.7MB/s]
 80%|█████████████████████████████▌       | 1.32G/1.65G [00:15<00:03, 84.7MB/s]
 80%|█████████████████████████████▋       | 1.33G/1.65G [00:15<00:03, 84.6MB/s]
 81%|█████████████████████████████▉       | 1.34G/1.65G [00:15<00:03, 84.6MB/s]
 81%|██████████████████████████████       | 1.34G/1.65G [00:15<00:03, 84.6MB/s]
 82%|██████████████████████████████▎      | 1.35G/1.65G [00:16<00:03, 84.7MB/s]
 82%|██████████████████████████████▍      | 1.36G/1.65G [00:16<00:03, 84.8MB/s]
 83%|██████████████████████████████▋      | 1.37G/1.65G [00:16<00:03, 84.7MB/s]
 83%|██████████████████████████████▊      | 1.38G/1.65G [00:16<00:03, 84.8MB/s]
 84%|███████████████████████████████      | 1.39G/1.65G [00:16<00:03, 84.4MB/s]
 84%|███████████████████████████████▏     | 1.40G/1.65G [00:16<00:03, 84.5MB/s]
 85%|███████████████████████████████▍     | 1.40G/1.65G [00:16<00:02, 84.5MB/s]
 85%|███████████████████████████████▌     | 1.41G/1.65G [00:16<00:02, 84.4MB/s]
 86%|███████████████████████████████▊     | 1.42G/1.65G [00:16<00:02, 84.5MB/s]
 86%|███████████████████████████████▉     | 1.43G/1.65G [00:16<00:02, 84.5MB/s]
 87%|████████████████████████████████▏    | 1.44G/1.65G [00:17<00:02, 84.5MB/s]
 87%|████████████████████████████████▎    | 1.45G/1.65G [00:17<00:02, 84.4MB/s]
 88%|████████████████████████████████▌    | 1.45G/1.65G [00:17<00:02, 84.4MB/s]
 89%|████████████████████████████████▋    | 1.46G/1.65G [00:17<00:02, 84.4MB/s]
 89%|████████████████████████████████▉    | 1.47G/1.65G [00:17<00:02, 84.2MB/s]
 90%|█████████████████████████████████▏   | 1.48G/1.65G [00:17<00:02, 84.2MB/s]
 90%|█████████████████████████████████▎   | 1.49G/1.65G [00:17<00:01, 84.3MB/s]
 91%|█████████████████████████████████▌   | 1.50G/1.65G [00:17<00:01, 84.4MB/s]
 91%|█████████████████████████████████▋   | 1.51G/1.65G [00:17<00:01, 84.5MB/s]
 92%|█████████████████████████████████▉   | 1.51G/1.65G [00:17<00:01, 84.7MB/s]
 92%|██████████████████████████████████   | 1.52G/1.65G [00:18<00:01, 84.8MB/s]
 93%|██████████████████████████████████▎  | 1.53G/1.65G [00:18<00:01, 84.8MB/s]
 93%|██████████████████████████████████▍  | 1.54G/1.65G [00:18<00:01, 84.9MB/s]
 94%|██████████████████████████████████▋  | 1.55G/1.65G [00:18<00:01, 85.0MB/s]
 94%|██████████████████████████████████▊  | 1.56G/1.65G [00:18<00:01, 85.1MB/s]
 95%|███████████████████████████████████  | 1.56G/1.65G [00:18<00:01, 82.2MB/s]
 95%|███████████████████████████████████▏ | 1.57G/1.65G [00:18<00:01, 69.4MB/s]
 96%|███████████████████████████████████▍ | 1.58G/1.65G [00:18<00:00, 73.2MB/s]
 96%|███████████████████████████████████▌ | 1.59G/1.65G [00:18<00:00, 76.5MB/s]
 97%|███████████████████████████████████▊ | 1.60G/1.65G [00:19<00:00, 78.9MB/s]
 97%|███████████████████████████████████▉ | 1.61G/1.65G [00:19<00:00, 80.7MB/s]
 98%|████████████████████████████████████▏| 1.62G/1.65G [00:19<00:00, 82.0MB/s]
 98%|████████████████████████████████████▎| 1.62G/1.65G [00:19<00:00, 83.1MB/s]
 99%|████████████████████████████████████▌| 1.63G/1.65G [00:19<00:00, 83.9MB/s]
 99%|████████████████████████████████████▋| 1.64G/1.65G [00:19<00:00, 84.3MB/s]
100%|████████████████████████████████████▉| 1.65G/1.65G [00:19<00:00, 84.7MB/s]
  0%|                                              | 0.00/1.65G [00:00<?, ?B/s]
100%|█████████████████████████████████████| 1.65G/1.65G [00:00<00:00, 7.34TB/s]
Download complete in 52s (1576.2 MB)
Filtering raw data in 1 contiguous segment
Setting up high-pass filter at 2 Hz

IIR filter parameters
---------------------
Butterworth highpass zero-phase (two-pass forward and reverse) non-causal filter:
- Filter order 8 (effective, after forward-backward)
- Cutoff at 2.00 Hz: -6.02 dB

Extract Xdawn covariance matrices

nfilter = 4
xdwn = XdawnCovariances(estimator="scm", nfilter=nfilter)
split = train_test_split(X, y, train_size=0.25, random_state=42)
Xtrain, Xtest, ytrain, ytest = split
covs = xdwn.fit(Xtrain, ytrain).transform(Xtest)

Spectral Embedding (SE)

plot_embedding(covs, ytest, metric="riemann", embd_type="Spectral",
               normalize=True)
plt.show()
Spectral Embedding of SPD matrices

Locally Linear Embedding (LLE)

plot_embedding(covs, ytest, metric="riemann", embd_type="LocallyLinear",
               normalize=False)
plt.show()
LocallyLinear Embedding of SPD matrices

TNSE

plot_embedding(covs, ytest, metric="riemann", embd_type="TSNE",
               normalize=False, max_iter=50)
plt.show()
TSNE Embedding of SPD matrices
/home/docs/checkouts/readthedocs.org/user_builds/pyriemann/checkouts/v0.10/pyriemann/optimization/positive_definite.py:284: UserWarning: Convergence not reached. Try increasing max_iter.
  warnings.warn("Convergence not reached. Try increasing max_iter.")

References

Total running time of the script: (1 minutes 0.360 seconds)

Gallery generated by Sphinx-Gallery