forked from kingjr/meg-masc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_decoding.py
262 lines (212 loc) · 7.02 KB
/
check_decoding.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import mne
import mne_bids
import numpy as np
import pandas as pd
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.model_selection import KFold, cross_val_predict
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler, scale
from tqdm import trange
from wordfreq import zipf_frequency
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
mne.set_log_level(False)
class PATHS:
path_file = Path("./data_path.txt")
if not path_file.exists():
data = Path(input("data_path?"))
assert data.exists()
with open(path_file, "w") as f:
f.write(str(data) + "\n")
with open(path_file, "r") as f:
data = Path(f.readlines()[0].strip("\n"))
assert data.exists()
bids = data / "bids_anonym"
def segment(raw):
# preproc annotations
meta = list()
for annot in raw.annotations:
d = eval(annot.pop("description"))
for k, v in annot.items():
assert k not in d.keys()
d[k] = v
meta.append(d)
meta = pd.DataFrame(meta)
meta["intercept"] = 1.0
# compute voicing
phonemes = meta.query('kind=="phoneme"')
assert len(phonemes)
for ph, d in phonemes.groupby("phoneme"):
ph = ph.split("_")[0]
match = ph_info.query("phoneme==@ph")
assert len(match) == 1
meta.loc[d.index, "voiced"] = match.iloc[0].phonation == "v"
# compute word frquency and merge w/ phoneme
meta["is_word"] = False
words = meta.query('kind=="word"').copy()
assert len(words) > 10
# assert np.all(meta.loc[words.index + 1, "kind"] == "phoneme")
meta.loc[words.index + 1, "is_word"] = True
wfreq = lambda x: zipf_frequency(x, "en") # noqa
meta.loc[words.index + 1, "wordfreq"] = words.word.apply(wfreq).values
meta = meta.query('kind=="phoneme"')
assert len(meta.wordfreq.unique()) > 2
# segment
events = np.c_[
meta.onset * raw.info["sfreq"], np.ones((len(meta), 2))
].astype(int)
epochs = mne.Epochs(
raw,
events,
tmin=-0.200,
tmax=0.6,
decim=10,
baseline=(-0.2, 0.0),
metadata=meta,
preload=True,
event_repeated="drop",
)
# threshold
th = np.percentile(np.abs(epochs._data), 95)
epochs._data[:] = np.clip(epochs._data, -th, th)
epochs.apply_baseline()
return epochs
def decod(X, y, meta, times):
assert len(X) == len(y) == len(meta)
meta = meta.reset_index()
y = scale(y[:, None])[:, 0]
if len(set(y[:1000])) > 2:
y = y > np.nanmedian(y)
# define data
model = make_pipeline(StandardScaler(), LinearDiscriminantAnalysis())
cv = KFold(5, shuffle=True, random_state=0)
# fit predict
n, nchans, ntimes = X.shape
preds = np.zeros((n, ntimes))
for t in trange(ntimes):
preds[:, t] = cross_val_predict(
model, X[:, :, t], y, cv=cv, method="predict_proba"
)[:, 1]
# score
out = list()
for label, m in meta.groupby("label"):
Rs = correlate(y[m.index, None], preds[m.index])
for t, r in zip(times, Rs):
out.append(dict(score=r, time=t, label=label, n=len(m.index)))
return pd.DataFrame(out)
def correlate(X, Y):
if X.ndim == 1:
X = X[:, None]
if Y.ndim == 1:
Y = Y[:, None]
X = X - X.mean(0)
Y = Y - Y.mean(0)
SX2 = (X**2).sum(0) ** 0.5
SY2 = (Y**2).sum(0) ** 0.5
SXY = (X * Y).sum(0)
return SXY / (SX2 * SY2)
def plot(result):
fig, ax = plt.subplots(1, figsize=[6, 6])
sns.lineplot(x="time", y="score", data=result, hue="label", ax=ax)
ax.axhline(0, color="k")
return fig
ph_info = pd.read_csv("phoneme_info.csv")
subjects = pd.read_csv(PATHS.bids / "participants.tsv", sep="\t")
subjects = subjects.participant_id.apply(lambda x: x.split("-")[1]).values
def _get_epochs(subject):
all_epochs = list()
for session in range(2):
for task in range(4):
print(".", end="")
bids_path = mne_bids.BIDSPath(
subject=subject,
session=str(session),
task=str(task),
datatype="meg",
root=PATHS.bids,
)
try:
raw = mne_bids.read_raw_bids(bids_path)
except FileNotFoundError:
print("missing", subject, session, task)
continue
raw = raw.pick_types(
meg=True, misc=False, eeg=False, eog=False, ecg=False
)
raw.load_data().filter(0.5, 30.0, n_jobs=1)
epochs = segment(raw)
epochs.metadata["half"] = np.round(
np.linspace(0, 1.0, len(epochs))
).astype(int)
epochs.metadata["task"] = task
epochs.metadata["session"] = session
all_epochs.append(epochs)
if not len(all_epochs):
return
epochs = mne.concatenate_epochs(all_epochs)
m = epochs.metadata
label = (
"t"
+ m.task.astype(str)
+ "_s"
+ m.session.astype(str)
+ "_h"
+ m.half.astype(str)
)
epochs.metadata["label"] = label
return epochs
def _decod_one_subject(subject):
epochs = _get_epochs(subject)
if epochs is None:
return
# words
words = epochs["is_word"]
evo = words.average()
fig_evo = evo.plot(spatial_colors=True, show=False)
X = words.get_data() * 1e13
y = words.metadata["wordfreq"].values
results = decod(X, y, words.metadata, words.times)
results["subject"] = subject
results["contrast"] = "wordfreq"
fig_decod = plot(results)
# Phonemes
phonemes = epochs["not is_word"]
evo = phonemes.average()
fig_evo_ph = evo.plot(spatial_colors=True, show=False)
X = phonemes.get_data() * 1e13
y = phonemes.metadata["voiced"].values
results_ph = decod(X, y, phonemes.metadata, phonemes.times)
results_ph["subject"] = subject
results_ph["contrast"] = "voiced"
fig_decod_ph = plot(results_ph)
return fig_evo, fig_decod, results, fig_evo_ph, fig_decod_ph, results_ph
if __name__ == "__main__":
report = mne.Report()
# decoding
all_results = list()
results = list()
for subject in subjects:
print(subject)
out = _decod_one_subject(subject)
if out is None:
continue
(
fig_evo,
fig_decod,
results,
fig_evo_ph,
fig_decod_ph,
results_ph,
) = out
report.add_figure(fig_evo, subject, tags="evo_word")
report.add_figure(fig_decod, subject, tags="word")
report.add_figure(fig_evo_ph, subject, tags="evo_phoneme")
report.add_figure(fig_decod_ph, subject, tags="phoneme")
report.save("decoding.html", open_browser=False, overwrite=True)
all_results.append(results)
all_results.append(results_ph)
print("done")
pd.concat(all_results, ignore_index=True).to_csv("decoding_results.csv")