|
| 1 | +""" |
| 2 | +==================================================================== |
| 3 | +Classification of P300 datasets from MOABB |
| 4 | +==================================================================== |
| 5 | +
|
| 6 | +It demonstrates the QuantumClassifierWithDefaultRiemannianPipeline(). This |
| 7 | +pipeline uses Riemannian Geometry, Tangent Space and a quantum SVM |
| 8 | +classifier. MOABB is used to access many EEG datasets and also for the |
| 9 | +evaluation and comparison with other classifiers. |
| 10 | +
|
| 11 | +In QuantumClassifierWithDefaultRiemannianPipeline(): |
| 12 | +If parameter "shots" is None then a classical SVM is used similar to the one |
| 13 | +in scikit learn. |
| 14 | +If "shots" is not None and IBM Qunatum token is provided with "q_account_token" |
| 15 | +then a real Quantum computer will be used. |
| 16 | +You also need to adjust the "n_components" in the PCA procedure to the number |
| 17 | +of qubits supported by the real quantum computer you are going to use. |
| 18 | +A list of real quantum computers is available in your IBM quantum account. |
| 19 | +
|
| 20 | +""" |
| 21 | +# Author: Anton Andreev |
| 22 | +# Modified from plot_classify_EEG_tangentspace.py of pyRiemann |
| 23 | +# License: BSD (3-clause) |
| 24 | + |
| 25 | +from pyriemann.estimation import XdawnCovariances |
| 26 | +from pyriemann.tangentspace import TangentSpace |
| 27 | +from sklearn.pipeline import make_pipeline |
| 28 | +from matplotlib import pyplot as plt |
| 29 | +import warnings |
| 30 | +import seaborn as sns |
| 31 | +from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA |
| 32 | +from moabb import set_log_level |
| 33 | +from moabb.datasets import bi2012 |
| 34 | +from moabb.evaluations import WithinSessionEvaluation |
| 35 | +from moabb.paradigms import P300 |
| 36 | +from pyriemann_qiskit.classification import \ |
| 37 | + QuantumClassifierWithDefaultRiemannianPipeline |
| 38 | +from sklearn.decomposition import PCA |
| 39 | + |
| 40 | +print(__doc__) |
| 41 | + |
| 42 | +############################################################################## |
| 43 | +# getting rid of the warnings about the future |
| 44 | +warnings.simplefilter(action="ignore", category=FutureWarning) |
| 45 | +warnings.simplefilter(action="ignore", category=RuntimeWarning) |
| 46 | + |
| 47 | +warnings.filterwarnings("ignore") |
| 48 | + |
| 49 | +set_log_level("info") |
| 50 | + |
| 51 | +############################################################################## |
| 52 | +# Create Pipelines |
| 53 | +# ---------------- |
| 54 | +# |
| 55 | +# Pipelines must be a dict of sklearn pipeline transformer. |
| 56 | + |
| 57 | +############################################################################## |
| 58 | +# We have to do this because the classes are called 'Target' and 'NonTarget' |
| 59 | +# but the evaluation function uses a LabelEncoder, transforming them |
| 60 | +# to 0 and 1 |
| 61 | +labels_dict = {"Target": 1, "NonTarget": 0} |
| 62 | + |
| 63 | +paradigm = P300(resample=128) |
| 64 | + |
| 65 | +datasets = [bi2012()] # MOABB provides several other P300 datasets |
| 66 | + |
| 67 | +# reduce the number of subjects, the Quantum pipeline takes a lot of time |
| 68 | +# if executed on the entire dataset |
| 69 | +n_subjects = 5 |
| 70 | +for dataset in datasets: |
| 71 | + dataset.subject_list = dataset.subject_list[0:n_subjects] |
| 72 | + |
| 73 | +overwrite = True # set to True if we want to overwrite cached results |
| 74 | + |
| 75 | +pipelines = {} |
| 76 | + |
| 77 | +# A Riemannian Quantum pipeline provided by pyRiemann-qiskit |
| 78 | +# You can choose between classical SVM and Quantum SVM. |
| 79 | +pipelines["RG+QuantumSVM"] = QuantumClassifierWithDefaultRiemannianPipeline( |
| 80 | + shots=None, # 'None' forces classic SVM |
| 81 | + nfilter=2, # default 2 |
| 82 | + # default n_components=10, a higher value renders better performance with |
| 83 | + # the non-qunatum SVM version used in qiskit |
| 84 | + # On a real Quantum computer (n_components = qubits) |
| 85 | + dim_red=PCA(n_components=5), |
| 86 | + # params={'q_account_token': '<IBM Quantum TOKEN>'} |
| 87 | + ) |
| 88 | + |
| 89 | +# Here we provide a pipeline for comparison: |
| 90 | + |
| 91 | +# This is a standard pipeline similar to |
| 92 | +# QuantumClassifierWithDefaultRiemannianPipeline, but with LDA classifier |
| 93 | +# instead. |
| 94 | +pipelines["RG+LDA"] = make_pipeline( |
| 95 | + # applies XDawn and calculates the covariance matrix, output it matrices |
| 96 | + XdawnCovariances( |
| 97 | + nfilter=2, |
| 98 | + classes=[labels_dict["Target"]], |
| 99 | + estimator="lwf", |
| 100 | + xdawn_estimator="scm" |
| 101 | + ), |
| 102 | + TangentSpace(), |
| 103 | + PCA(n_components=10), |
| 104 | + LDA(solver="lsqr", shrinkage="auto"), # you can use other classifiers |
| 105 | +) |
| 106 | + |
| 107 | +print("Total pipelines to evaluate: ", len(pipelines)) |
| 108 | + |
| 109 | +evaluation = WithinSessionEvaluation( |
| 110 | + paradigm=paradigm, |
| 111 | + datasets=datasets, |
| 112 | + suffix="examples", |
| 113 | + overwrite=overwrite |
| 114 | +) |
| 115 | + |
| 116 | +results = evaluation.process(pipelines) |
| 117 | + |
| 118 | +print("Averaging the session performance:") |
| 119 | +print(results.groupby('pipeline').mean('score')[['score', 'time']]) |
| 120 | + |
| 121 | +############################################################################## |
| 122 | +# Plot Results |
| 123 | +# ---------------- |
| 124 | +# |
| 125 | +# Here we plot the results to compare the two pipelines |
| 126 | + |
| 127 | +fig, ax = plt.subplots(facecolor="white", figsize=[8, 4]) |
| 128 | + |
| 129 | +sns.stripplot( |
| 130 | + data=results, |
| 131 | + y="score", |
| 132 | + x="pipeline", |
| 133 | + ax=ax, |
| 134 | + jitter=True, |
| 135 | + alpha=0.5, |
| 136 | + zorder=1, |
| 137 | + palette="Set1", |
| 138 | +) |
| 139 | +sns.pointplot(data=results, |
| 140 | + y="score", |
| 141 | + x="pipeline", |
| 142 | + ax=ax, zorder=1, |
| 143 | + palette="Set1") |
| 144 | + |
| 145 | +ax.set_ylabel("ROC AUC") |
| 146 | +ax.set_ylim(0.3, 1) |
| 147 | + |
| 148 | +plt.show() |
0 commit comments