-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-epochs.py
243 lines (208 loc) · 7.79 KB
/
make-epochs.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
"""
Top-level trial data pre-processing
Authored by Eric Easthope
"""
import os
import warnings
import numpy as np
from pathlib import Path
from pynwb import NWBHDF5IO
from utils import overlap
# Filter warnings
warnings.filterwarnings("ignore")
DATA = "data"
DERIVED = "derived"
subjects = ["EC2", "EC9", "GP31", "GP33"]
MIN_REST_LENGTH = 0.5
OVERWRITE = True
# Make derived directory if it does not exist
try:
os.mkdir(DERIVED)
except FileExistsError:
pass
for SUBJECT in subjects:
# Make subject directory if it does not exist
try:
os.mkdir(DERIVED + f"/{SUBJECT}")
except FileExistsError:
pass
for path in [p for p in Path(DATA).glob(f"{SUBJECT}*.nwb")]:
_, SESSION = path.stem.split("_")
OUT = DERIVED + f"/{SUBJECT}/{SESSION}"
if os.path.isdir(f"{OUT}/trials") and not OVERWRITE:
print("Trials exist already, skipping ...")
else:
# Make session directory if it does not exist
try:
os.mkdir(OUT)
except FileExistsError:
pass
# Make trials directory if it does not exist
try:
os.mkdir(f"{OUT}/trials")
except FileExistsError:
pass
print(f"File {SUBJECT}_{SESSION}.")
nwb = NWBHDF5IO(path, "r", load_namespaces=True).read()
# Sampling, Nyquist frequency
fs = round(nwb.acquisition["ElectricalSeries"].rate)
print("Loading ...")
data = nwb.acquisition["ElectricalSeries"].data[:].astype(np.float64)
nyq = fs // 2
# GOOD CHANNELS, opposite of bad channels
# Anatomical locations of electodes
goods = np.invert(nwb.electrodes["bad"][:])
locx = nwb.electrodes.location[:]
# EPOCHS
# Speech trials
# Start, consonant-vowel transition, stop, syllable condition, if/not spoke
trials = np.array(
[
nwb.trials.start_time[:],
nwb.trials.cv_transition_time[:],
nwb.trials.stop_time[:],
nwb.trials.condition[:],
nwb.trials.speak[:],
]
).T
# CONSONANT, VOWEL, IRP1/2
start_time, cv_time, stop_time, condition, spoke = trials.T
consonants = np.array(list(zip(start_time, cv_time)))
vowels = np.array(list(zip(cv_time, stop_time)))
rests = np.array(list(zip(stop_time[:-1], start_time[1:])))
cx = np.array(
[
consonants.ptp(axis=1)[i] if condition[i].endswith("e") else None
for i in range(len(trials))
]
)
vx = np.array(
[
vowels.ptp(axis=1)[i] if condition[i].endswith("e") else None
for i in range(len(trials))
]
)
condx = np.array(
[c if c.endswith("e") else None for c in condition], dtype="object"
)
# Q: How many session trials are there, in how many did the subject speak?
print(f"Session had {len(spoke)} trials.")
print(f"Subject spoke in {len(trials)} trials.")
# Rest-/i/-rest time triples, round to indices
# TEST: Assert epoch + padding definitely within signal
r = int(MIN_REST_LENGTH * fs)
pad = int(0.5 * fs)
times = np.concatenate(
[rests[:-1], consonants[1:-1], vowels[1:-1], rests[1:]], axis=1
).reshape((-1, 4, 2))
indices = np.round(fs * times).astype(int)
assert indices.min() - r - pad >= 0 and indices.max() + r + pad < len(data)
print("Finding rest-/i/-rest trials ...")
# Rest-/i/-rest indices
ij = [
[speech_start - r - pad, speech_stop + r + pad]
for ([_, speech_start], _, _, [speech_stop, _]) in indices
]
# Rest-/i/-rest ECoG
epochs = np.array(
[None] + [data[i:j] for (i, j) in ij] + [None],
dtype=object,
)
epochs = np.array(
[
epochs[i] if condition[i].endswith("e") else None
for i in range(len(trials))
]
)
# INDICES
idx = [None]
for i in indices:
idx.append(
np.sort(
np.append(
np.cumsum(i.ptp(axis=1)[1:-1]) + r + pad,
[
0,
pad,
pad + r,
pad + r + np.sum(i.ptp(axis=1)[1:-1]) + r,
pad + r + np.sum(i.ptp(axis=1)[1:-1]) + pad + r,
],
)
)
)
idx.append(None)
idx = [
idx[i] if condition[i].endswith("e") else None
for i in range(len(trials))
]
idx = np.array(idx, dtype=object)
# EXCLUSIONS
firstlast = np.full((len(trials),), True)
firstlast[0] = False
firstlast[-1] = False
# IRP1/2 shorter than MIN_REST_LENGTH
longenough = np.array(
[True]
+ np.all(indices.ptp(axis=2)[:, [0, -1]] >= fs // 2, axis=1).tolist()
+ [True]
).astype(bool)
# Rest-/i/-rest times
st = [
[speech_start - MIN_REST_LENGTH, speech_stop + MIN_REST_LENGTH]
for ([_, speech_start], _, _, [speech_stop, _]) in times
]
# Overlap with invalid times
valids = np.full((len(trials),), True)
if nwb.invalid_times is not None:
invalids = nwb.invalid_times[:].iloc[:, 0:2].values.tolist()
for i in range(1, len(trials) - 1):
if any(overlap(inv, st[i - 1]) for inv in invalids):
valids[i] = False
includes = firstlast * longenough * valids
# SAVE
print("Saving ...")
# Trials
def sift(xx):
return [
x if x is not None and includes[i] else None
for i, x in enumerate(xx)
]
np.save(
f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-epochs.npy",
sift(epochs),
allow_pickle=True,
)
np.save(
f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-indices.npy",
sift(idx),
allow_pickle=True,
)
np.save(
f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-consonants.npy", sift(cx)
)
np.save(f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-vowels.npy", sift(vx))
np.save(
f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-conditions.npy",
sift(condx),
allow_pickle=True,
)
# Good channels, anatomical locations
np.save(
f"{OUT}/{SUBJECT}_{SESSION}-good-channels.npy",
goods,
allow_pickle=True,
)
np.save(
f"{OUT}/{SUBJECT}_{SESSION}-locations.npy",
locx,
allow_pickle=True,
)
# Valid trials to include
np.save(
f"{OUT}/{SUBJECT}_{SESSION}-includes.npy",
includes,
allow_pickle=True,
)
print("Done.")
print()