-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathembedding.py
149 lines (115 loc) · 4.34 KB
/
embedding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from virtualreality.dataset import VirtualReality
from virtualreality.utilities import get_block_repetition
import numpy as np
import mne
import moabb
import pandas as pd
import matplotlib.pyplot as plt
from moabb.paradigms import P300
from pyriemann.estimation import XdawnCovariances, ERPCovariances
from pyriemann.classification import MDM
from pyriemann.embedding import Embedding
from pyriemann.utils.mean import mean_riemann
from pyriemann.utils.distance import distance_riemann
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import KFold
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from tqdm import tqdm
"""
===============================
Spectral Embedding of the data
===============================
"""
# Authors: Pedro Rodrigues <[email protected]>
#
# License: BSD (3-clause)
import warnings
warnings.filterwarnings("ignore")
# get the paradigm
paradigm = P300()
paradigm.tmax = 1.0
# create datasets
datasets = {}
datasets['VR'] = VirtualReality()
datasets['VR'].VR = True
datasets['VR'].PC = False
datasets['PC'] = VirtualReality()
datasets['PC'].VR = False
datasets['PC'].PC = True
results = {}
for subject in VirtualReality().subject_list:
data = {}
data['VR'] = {}
data['PC'] = {}
embeddings = {}
stats = {}
stats['VR'] = {}
stats['PC'] = {}
for condition in datasets.keys():
# get the epochs and labels
X, y, meta = paradigm.get_data(datasets[condition], subjects=[subject])
y = LabelEncoder().fit_transform(y)
data[condition]['X'] = X
data[condition]['y'] = y
# estimate xDawn covs
ncomps = 4
erp = XdawnCovariances(classes=[1], estimator='lwf', nfilter=ncomps, xdawn_estimator='lwf')
#erp = ERPCovariances(classes=[1], estimator='lwf', svd=ncomps)
split = train_test_split(X, y, train_size=0.50, random_state=42)
Xtrain, Xtest, ytrain, ytest = split
covs = erp.fit(Xtrain, ytrain).transform(Xtest)
Mtarget = mean_riemann(covs[ytest == 1])
Mnontarget = mean_riemann(covs[ytest == 0])
stats[condition]['distance'] = distance_riemann(Mtarget, Mnontarget)
stats[condition]['dispersion_target'] = np.sum([distance_riemann(covi, Mtarget)**2 for covi in covs[ytest == 1]]) / len(covs[ytest == 1])
stats[condition]['dispersion_nontarget'] = np.sum([distance_riemann(covi, Mnontarget)**2 for covi in covs[ytest == 0]]) / len(covs[ytest == 0])
print('subject', subject)
print(stats)
results[subject] = stats
# covs = np.concatenate([covs, Mtarget[None,:,:], Mnontarget[None,:,:]])
# ytest = np.concatenate([ytest, [1], [0]])
# data[condition]['ytest'] = ytest
# # do the embedding
# lapl = Embedding(metric='riemann', n_components=2)
# embeddings[condition] = lapl.fit_transform(covs)
dispersion_target_list = []
dispersion_nontarget_list = []
distance_list = []
condition_list = []
subject_list = []
for subject in results.keys():
results_subj = results[subject]
for condition in ['VR', 'PC']:
subject_list.append(subject)
condition_list.append(condition)
dispersion_target_list.append(results_subj[condition]['dispersion_target'])
dispersion_nontarget_list.append(results_subj[condition]['dispersion_nontarget'])
distance_list.append(results_subj[condition]['distance'])
df = pd.DataFrame()
df['subject'] = subject_list
df['condition'] = condition_list
df['dispersion_target'] = dispersion_target_list
df['dispersion_nontarget'] = dispersion_nontarget_list
df['distance'] = distance_list
#####
# # plot
# names = {0:'NonTarget', 1:'Target'}
# colors = {0:'#2166ac', 1:'#b2182b'}
# fig, ax = plt.subplots(figsize=(16, 7.4), facecolor='white', ncols=2)
# for axi, condition in zip(ax, embeddings.keys()):
# embd = embeddings[condition]
# for label in np.unique(data[condition]['ytest']):
# idx = (data[condition]['ytest'] == label)
# axi.scatter(embd[idx, 0], embd[idx, 1], s=80, label=names[label], alpha=0.25, edgecolor='none', facecolor=colors[label])
# axi.scatter(embd[-2,0], embd[-2,1], s=150, color=colors[1])
# axi.scatter(embd[-1,0], embd[-1,1], s=150, color=colors[0])
# axi.plot([ embd[-2,0], embd[-1,0] ], [ embd[-2,1], embd[-1,1] ], c='k', lw=0.8, ls='--')
# axi.grid(False)
# axi.set_xticks([-1, -0.5, 0, +0.5, 1.0])
# axi.set_yticks([-1, -0.5, 0, +0.5, 1.0])
# axi.set_title(condition, fontsize=16)
# axi.legend()
# fig.savefig('embedding.png', format='png')
#fig.show()