Note
Go to the end to download the full example code.
Visualize distances with MDM and NCH¶
Demonstrates how to use the visualization module, to plot the distances between the classes.
# Author: Gregoire Cattan
# License: BSD (3-clause)
# import warnings
from matplotlib import pyplot as plt
from moabb import set_log_level
from moabb.datasets import BI2012
from moabb.paradigms import P300
from pyriemann.classification import MDM
from pyriemann.estimation import XdawnCovariances
from pyriemann.preprocessing import Whitening
from pyriemann.utils.viz import plot_bihist, plot_biscatter
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from pyriemann_qiskit.classification import QuanticNCH
from pyriemann_qiskit.visualization.manifold import plot_manifold
print(__doc__)
getting rid of the warnings about the future warnings.simplefilter(action=”ignore”, category=FutureWarning) warnings.simplefilter(action=”ignore”, category=RuntimeWarning)
# warnings.filterwarnings("ignore")
set_log_level("info")
Create Pipelines¶
Create a MDM and NCH pipeline, as well as an estimator for 2x2 cov matrices.
paradigm = P300(resample=128)
ds = BI2012()
# Change this to use NCH instead of MDM estimator for the distances
USE_MDM = True
mdm = make_pipeline(
# applies XDawn and calculates the covariance matrix, output it matrices
XdawnCovariances(
nfilter=3,
estimator="scm",
xdawn_estimator="lwf",
),
MDM(metric="logeuclid"),
)
nch = make_pipeline(
XdawnCovariances(
nfilter=3,
estimator="scm",
xdawn_estimator="lwf",
),
QuanticNCH(
n_hulls_per_class=3,
n_samples_per_hull=15,
n_jobs=12,
subsampling="min",
quantum=False,
),
)
estimator = mdm if USE_MDM else nch
cov2x2 = make_pipeline(
XdawnCovariances(
nfilter=3,
estimator="scm",
xdawn_estimator="lwf",
),
Whitening(dim_red={"n_components": 2}),
)
[QClass] Initializing Quantum Classifier
Data¶
Retrieve data from BI2012
X, y, _ = paradigm.get_data(ds, subjects=[1])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)
/home/docs/checkouts/readthedocs.org/user_builds/pyriemann-qiskit/envs/latest/lib/python3.12/site-packages/moabb/datasets/download.py:97: RuntimeWarning: Setting non-standard config type: "MNE_DATASETS_BRAININVADERS2012_PATH"
set_config(key, get_config("MNE_DATA"))
/home/docs/checkouts/readthedocs.org/user_builds/pyriemann-qiskit/envs/latest/lib/python3.12/site-packages/urllib3/connectionpool.py:1110: InsecureRequestWarning: Unverified HTTPS request is being made to host 'zenodo.org'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/pyriemann-qiskit/envs/latest/lib/python3.12/site-packages/urllib3/connectionpool.py:1110: InsecureRequestWarning: Unverified HTTPS request is being made to host 'zenodo.org'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
0%| | 0.00/28.7M [00:00<?, ?B/s]
0%| | 11.3k/28.7M [00:00<07:05, 67.5kB/s]
0%| | 36.9k/28.7M [00:00<03:40, 130kB/s]
0%| | 88.1k/28.7M [00:00<02:02, 234kB/s]
1%|▎ | 193k/28.7M [00:00<01:05, 434kB/s]
1%|▍ | 351k/28.7M [00:00<00:41, 680kB/s]
2%|▌ | 440k/28.7M [00:00<00:42, 667kB/s]
2%|▊ | 571k/28.7M [00:01<00:37, 759kB/s]
2%|▉ | 701k/28.7M [00:01<00:34, 818kB/s]
3%|█ | 784k/28.7M [00:01<00:37, 750kB/s]
3%|█▎ | 931k/28.7M [00:01<00:32, 845kB/s]
4%|█▎ | 1.02M/28.7M [00:01<00:35, 775kB/s]
4%|█▍ | 1.11M/28.7M [00:01<00:36, 749kB/s]
4%|█▌ | 1.19M/28.7M [00:01<00:39, 703kB/s]
5%|█▊ | 1.32M/28.7M [00:01<00:35, 774kB/s]
5%|█▉ | 1.44M/28.7M [00:02<00:34, 791kB/s]
5%|██ | 1.52M/28.7M [00:02<00:37, 731kB/s]
6%|██▏ | 1.62M/28.7M [00:02<00:37, 726kB/s]
6%|██▎ | 1.76M/28.7M [00:02<00:32, 826kB/s]
6%|██▍ | 1.85M/28.7M [00:02<00:35, 758kB/s]
7%|██▌ | 1.96M/28.7M [00:02<00:34, 777kB/s]
7%|██▋ | 2.08M/28.7M [00:02<00:33, 793kB/s]
8%|██▉ | 2.22M/28.7M [00:03<00:30, 876kB/s]
8%|███ | 2.32M/28.7M [00:03<00:32, 824kB/s]
8%|███▏ | 2.40M/28.7M [00:03<00:34, 757kB/s]
9%|███▎ | 2.52M/28.7M [00:03<00:33, 777kB/s]
9%|███▍ | 2.63M/28.7M [00:03<00:32, 793kB/s]
10%|███▌ | 2.73M/28.7M [00:03<00:33, 769kB/s]
10%|███▊ | 2.84M/28.7M [00:03<00:32, 785kB/s]
10%|███▉ | 2.99M/28.7M [00:04<00:29, 870kB/s]
11%|████ | 3.09M/28.7M [00:04<00:31, 824kB/s]
11%|████▏ | 3.19M/28.7M [00:04<00:32, 790kB/s]
12%|████▍ | 3.38M/28.7M [00:04<00:25, 980kB/s]
12%|████▋ | 3.50M/28.7M [00:04<00:26, 936kB/s]
14%|█████ | 3.89M/28.7M [00:04<00:16, 1.51MB/s]
14%|█████▏ | 4.04M/28.7M [00:04<00:17, 1.39MB/s]
15%|█████▍ | 4.19M/28.7M [00:05<00:19, 1.29MB/s]
15%|█████▌ | 4.31M/28.7M [00:05<00:20, 1.18MB/s]
15%|█████▋ | 4.43M/28.7M [00:05<00:22, 1.08MB/s]
16%|█████▊ | 4.54M/28.7M [00:05<00:24, 1.00MB/s]
16%|██████ | 4.71M/28.7M [00:05<00:22, 1.06MB/s]
17%|██████▏ | 4.84M/28.7M [00:05<00:23, 1.03MB/s]
17%|██████▍ | 4.99M/28.7M [00:05<00:22, 1.04MB/s]
18%|██████▌ | 5.12M/28.7M [00:05<00:23, 1.01MB/s]
18%|██████▉ | 5.23M/28.7M [00:06<00:24, 959kB/s]
19%|███████ | 5.38M/28.7M [00:06<00:23, 992kB/s]
19%|███████▏ | 5.48M/28.7M [00:06<00:25, 909kB/s]
20%|███████▍ | 5.61M/28.7M [00:06<00:25, 916kB/s]
20%|███████▍ | 5.80M/28.7M [00:06<00:21, 1.07MB/s]
21%|███████▊ | 5.92M/28.7M [00:06<00:22, 997kB/s]
21%|████████ | 6.05M/28.7M [00:06<00:23, 980kB/s]
21%|████████▏ | 6.15M/28.7M [00:07<00:25, 901kB/s]
22%|████████▎ | 6.26M/28.7M [00:07<00:25, 882kB/s]
22%|████████▍ | 6.38M/28.7M [00:07<00:25, 866kB/s]
23%|████████▋ | 6.56M/28.7M [00:07<00:22, 996kB/s]
23%|████████▊ | 6.67M/28.7M [00:07<00:23, 948kB/s]
24%|████████▉ | 6.80M/28.7M [00:07<00:23, 948kB/s]
24%|█████████▏ | 6.92M/28.7M [00:07<00:23, 915kB/s]
24%|█████████▎ | 7.03M/28.7M [00:08<00:24, 888kB/s]
25%|█████████▍ | 7.14M/28.7M [00:08<00:24, 871kB/s]
26%|█████████▍ | 7.36M/28.7M [00:08<00:19, 1.07MB/s]
26%|█████████▋ | 7.47M/28.7M [00:08<00:21, 1.00MB/s]
26%|██████████ | 7.59M/28.7M [00:08<00:22, 949kB/s]
27%|██████████▏ | 7.70M/28.7M [00:08<00:22, 914kB/s]
28%|██████████▏ | 7.91M/28.7M [00:08<00:18, 1.10MB/s]
28%|██████████▎ | 8.04M/28.7M [00:09<00:19, 1.06MB/s]
28%|██████████▊ | 8.15M/28.7M [00:09<00:21, 971kB/s]
29%|██████████▉ | 8.26M/28.7M [00:09<00:22, 909kB/s]
29%|██████████▉ | 8.45M/28.7M [00:09<00:19, 1.06MB/s]
30%|███████████▎ | 8.57M/28.7M [00:09<00:20, 994kB/s]
30%|███████████▍ | 8.68M/28.7M [00:09<00:21, 945kB/s]
33%|████████████▍ | 9.62M/28.7M [00:09<00:07, 2.69MB/s]
38%|█████████████▉ | 10.8M/28.7M [00:09<00:04, 4.44MB/s]
48%|█████████████████▋ | 13.7M/28.7M [00:10<00:01, 9.50MB/s]
62%|██████████████████████▉ | 17.8M/28.7M [00:10<00:00, 15.5MB/s]
76%|████████████████████████████▎ | 21.9M/28.7M [00:10<00:00, 19.4MB/s]
87%|████████████████████████████████▎ | 25.1M/28.7M [00:10<00:00, 20.5MB/s]
98%|████████████████████████████████████▎| 28.2M/28.7M [00:10<00:00, 21.0MB/s]
0%| | 0.00/28.7M [00:00<?, ?B/s]
100%|██████████████████████████████████████| 28.7M/28.7M [00:00<00:00, 148GB/s]
Plot manifold¶
Plot cov matrices in 3d cartesian space. (they form a cone)
points = cov2x2.fit(X_train, y_train).transform(X_test)
plot_manifold(points, y_test, False)

<Figure size 640x480 with 1 Axes>
Plot distances¶
Plot distances between the classes with the MDM or NCH estimator.
dists = estimator.fit(X_train, y_train).transform(X_test)
plot_biscatter(dists, y_test)
plot_bihist(dists, y_test)
plt.show()
Total running time of the script: (0 minutes 12.109 seconds)

