-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-epochs.py
197 lines (175 loc) · 6.2 KB
/
process-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
"""
Top-level trial data bandpower processing
Authored by Eric Easthope
"""
import os
import warnings
import numpy as np
import scipy.signal as sig
import resampy
import multiprocess as mp
from pathlib import Path
from utils import log_scale_bands
# Filter warnings
warnings.filterwarnings("ignore")
DATA = "data"
DERIVED = "derived"
subjects = ["EC2", "EC9", "GP31", "GP33"]
BANDS = (
log_scale_bands(12, 35, 2)
+ log_scale_bands(35, 70, 2)
+ log_scale_bands(70, 140, 2)
)
MIN_REST_LENGTH = 0.5
OVERWRITE = True
fs = 3052
nyq = fs // 2
r = int(MIN_REST_LENGTH * fs)
pad = int(0.5 * fs)
# Average consonant/vowel length, cross-session
avg_consonant_len = np.concatenate(
[
[c for c in np.load(p, allow_pickle=True) if c is not None]
for p in Path(DERIVED).glob(f"*/**/*-ee-consonants.npy")
]
).mean()
avg_vowel_len = np.concatenate(
[
[v for v in np.load(p, allow_pickle=True) if v is not None]
for p in Path(DERIVED).glob(f"*/**/*-ee-vowels.npy")
]
).mean()
for SUBJECT in subjects:
for path in [p for p in Path(DATA).glob(f"{SUBJECT}*.nwb")]:
_, SESSION = path.stem.split("_")
OUT = DERIVED + f"/{SUBJECT}/{SESSION}"
print(f"File {SUBJECT}_{SESSION}.")
if os.path.isdir(f"{OUT}/powers") and not OVERWRITE:
print("Powers exist already, skipping ...")
else:
# LOAD
cx = np.load(
f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-consonants.npy",
allow_pickle=True,
)
vx = np.load(
f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-vowels.npy",
allow_pickle=True,
)
idx = np.load(
f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-indices.npy",
allow_pickle=True,
)
goods = np.load(
f"{OUT}/{SUBJECT}_{SESSION}-good-channels.npy",
allow_pickle=True,
)
epochs = np.load(
f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-epochs.npy",
allow_pickle=True,
)
# RE-REFERENCE, subtract average signal from each channel
def rereference(s):
return (s.T - s[:, goods].mean(axis=1)).T
# NOTCH FILTER, 60/120/180 Hz line noise
def notch(s, Hz):
# NOTCH FILTER
Q = 30
b, a = sig.iirnotch(Hz, Q=Q, fs=fs)
return sig.filtfilt(b, a, s, axis=0)
# BANDPASS FILTER
def bandpass(s, band):
# BANDPASS FILTER
[l, h] = band
low = l / nyq
high = h / nyq
ORDER = 3
sos = sig.butter(ORDER, [low, high], "band", analog=False, output="sos")
return sig.sosfiltfilt(sos, s, axis=0)
# ANALYTIC SIGNAL, Hilbert transform
def analytic(s):
return sig.hilbert(s, axis=0)
# POWER
def power(s):
return np.abs(s) ** 2.0
# STRETCH, Time-warp consonant/vowel to average lengths w/ sinc interpolation
def stretch(s, i):
[pad1, irp1, con, vow, irp2, pad2] = np.split(s, i[1:-1])
[rest1, start, cv, stop, rest2] = i[1:-1]
c_scale = fs * avg_consonant_len / len(con)
v_scale = fs * avg_vowel_len / len(vow)
c_fs = np.floor(fs * c_scale).astype(int)
v_fs = np.floor(fs * v_scale).astype(int)
c_pad = np.floor(pad * c_scale).astype(int)
v_pad = np.floor(pad * v_scale).astype(int)
c_stretched = resampy.resample(
s[start - pad : cv + pad],
fs,
c_fs,
filter="sinc_window",
axis=0,
)[c_pad:-c_pad]
v_stretched = resampy.resample(
s[cv - pad : stop + pad],
fs,
v_fs,
axis=0,
filter="sinc_window",
)[v_pad:-v_pad]
return np.concatenate(
[pad1, irp1, c_stretched, v_stretched, irp2, pad2]
), np.cumsum(
[
0,
len(pad1),
len(irp1),
len(c_stretched),
len(v_stretched),
len(irp2),
len(pad2),
]
)
# Total process
def process(e, i, low, high):
return (
stretch(
power(
analytic(
bandpass(
notch(notch(notch(rereference(e), 60), 120), 180),
[low, high],
)
)
),
i,
)
if e is not None and i is not None
else (None, None)
)
# Make powers directory if it does not exist
try:
os.mkdir(f"{OUT}/powers")
except FileExistsError:
pass
for (low, high) in BANDS:
l = np.ceil(low).astype(int)
h = np.ceil(high).astype(int)
print(f"{l}-{h} Hz ...")
with mp.Pool(processes=8) as pool:
results = pool.starmap(
process,
[(epochs[i], idx[i], low, high) for i in range(len(epochs))],
)
powers, indices = zip(*results)
np.save(
f"{OUT}/powers/{SUBJECT}_{SESSION}-powers-ee-{l}-{h}.npy",
powers,
allow_pickle=True,
)
np.save(
f"{OUT}/trials/{SUBJECT}_{SESSION}-trials-ee-indices-stretched.npy",
indices,
allow_pickle=True,
)
print("Done.")
print()