-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPVS_get_sweeps.py
executable file
·351 lines (237 loc) · 10.1 KB
/
FPVS_get_sweeps.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/imaging/local/software/miniconda/envs/mne0.20/bin/python
"""
Get data segments for individual frequency sweeps for FPVS Frequency Sweep.
Read raw data, find runs, segment into individual frequency sweeps,
average sweeps across runs, write the average as raw file
(and ascii file if desired).
Compute TFR if specified.
Needs event file from filtering step.
==========================================
OH, October 2019
"""
import sys
from os import path as op
import numpy as np
from copy import deepcopy
from importlib import reload
import mne
import config_sweep as config
reload(config)
print(mne.__version__)
# conditions
conds = config.do_conds
close_fig = 1 # close figures only if close_fig==1
ascii_eeg_edf = 0 # !=0 if EEG also output as ascii and edf files
# Code to save in EDF format
# https://gist.github.com/skjerns/bc660ef59dca0dbd53f00ed38c42f6be
if ascii_eeg_edf:
from save_edf import write_edf
# plt.ion() # interactive plotting
def run_get_sweeps(sbj_id):
"""Compute spectra for one subject."""
# path to subject's data
sbj_path = op.join(config.data_path, config.map_subjects[sbj_id][0])
# raw-filename mappings for this subject
tmp_fnames = config.sss_map_fnames[sbj_id][1]
# only use files for correct conditions
sss_map_fnames = []
for cond in conds:
for [fi, ff] in enumerate(tmp_fnames):
if cond in ff:
sss_map_fnames.append(ff)
# initialise dict for results
# one dict per condition (e.g. 'hflf'), then for frequency (e.g. '12.'),
# then list of individual sweeps
names = [] # names of conditions
for raw_stem_in in sss_map_fnames:
names.append(raw_stem_in[:4])
names = np.unique(names)
# initialise for data at different sweep frequencies
data_runs = {}
for name in names:
data_runs[name] = {}
if name == 'face':
# faces only have one frequency
data_runs[name]['6.0'] = []
else:
for freq in config.fpvs_freqs:
data_runs[name][str(freq)] = []
# Note: Skip first two files since they are rest, no events
for raw_stem_in in sss_map_fnames:
# omit "_raw" in middle of filename
raw_fname_in = op.join(sbj_path, raw_stem_in[:-4] + '_f_' +
config.raw_ICA_suff + '.fif')
print('\n###\nReading raw file %s.' % raw_fname_in)
raw_ori = mne.io.read_raw_fif(raw_fname_in, preload=True)
raw = deepcopy(raw_ori) # keep raw_ori for possible TFR analysis
# event file was written during filtering, already correcting
# projector stimulus delay
event_file = op.join(sbj_path, raw_stem_in + '_sss_f_raw-eve.fif')
print('Reading events from %s.' % event_file)
events = mne.read_events(event_file)
# Find indices of good events (onsets of runs without missing frames)
event_ids = config.fpvs_event_ids
# duration of run incl. all sweeps and lead-in time at beginning
run_duration = config.fpvs_n_sweeps * config.fpvs_sweep_duration + \
config.fpvs_leadin
# idx_good, idx_bad: lists of indices to onsets of good/bad runs
idx_good, idx_bad = find_good_events(events, event_ids=event_ids,
run_duration=run_duration,
sfreq=raw.info['sfreq'])
print('Good runs:')
print(idx_good)
print(events[idx_good, :])
if len(idx_bad) != 0:
print('Bad runs:')
print(idx_bad)
print(events[idx_bad, :])
else:
print('No bad runs.')
# go through all indices to good runs
for idx in idx_good:
# onset time (s) for this good run
# there is one second gap between trigger and stimulus onset
# note: samples don't start at 0, but times do
onset_time = (events[idx, 0] - raw.first_samp) / raw.info['sfreq'] + 1.
if raw_stem_in[:4] == 'face': # faces don't have "sweeps"
# just get one "sweep"
raw_sweep = get_sweeps_from_raw(raw, t0=onset_time,
sweep_duration=60.,
n_sweeps=1)
# for faces use whole run
data_runs[raw_stem_in[:4]]['6.0'].append(raw_sweep[0])
else: # for frequency sweeps
n_sweeps = len(config.fpvs_freqs)
print('ID: %d, idx: %d, onset time: %f.' % (events[idx, 2],
idx, onset_time))
# raw_sweeps: list of raw instances per data segments,
# one per frequency
raw_sweeps = get_sweeps_from_raw(raw, t0=onset_time,
sweep_duration=config.fpvs_sweep_duration,
n_sweeps=n_sweeps)
for [fi, ff] in enumerate(config.fpvs_freqs):
data_runs[raw_stem_in[:4]][str(ff)].append(raw_sweeps[fi])
# AVERAGE raw files per condition and frequency across runs
# write the result as raw fiff-file
for cond in data_runs.keys(): # conditions
for freq in data_runs[cond].keys(): # frequencies
# average sweeps across runs
raw_avg = average_raws(data_runs[cond][freq])
# remove dot from frequency string
fname = 'rawavg_%s_%s_%s.fif' % (cond, ''.join(freq.split('.')),
config.raw_ICA_suff)
fname_raw_out = op.join(sbj_path, fname)
print('Writing average raw data to %s:' % fname_raw_out)
raw_avg.save(fname_raw_out, overwrite=True)
# if ascii and edf data for EEG requested as well
if ascii_eeg_edf:
# reduce to EEG only
raw_avg_eeg = raw_avg.pick_types(meg=False, eeg=True)
# ASCII
fname = 'rawavg_%s_%s_eeg.asc' % (cond,
''.join(freq.split('.')))
fname_asc_out = op.join(sbj_path, fname)
data_out = raw_avg_eeg.get_data()
print('Writing average ASCII data (%d, %d) to %s:'
% (data_out.shape[0], data_out.shape[1], fname_asc_out))
np.savetxt(fname_asc_out, data_out)
# EDF
fname = 'rawavg_%s_%s_eeg.edf' % (cond, ''.join(freq.split('.')))
fname_edf_out = op.join(sbj_path, fname)
print('Writing average EDF data to %s:' % fname_edf_out)
write_edf(raw_avg_eeg, fname_edf_out, overwrite=True)
return data_runs
def find_good_events(events, event_ids, run_duration, sfreq):
"""Find the onsets of good runs in raw data.
Parameters:
events: nd-array
Events from raw data.
event_ids: list of int
Possible triggers of run onsets
run_duration: float
Duration (s) of a run within session
sfreq: float
Sampling frequency (Hz)
Returns:
idx_good: list of int
Indices to onsets of good runs.
idx_bad: list of int
List of indices to onsets of bad runs
"""
max_missed = 2 # how many frames turn a run invalid
idx_good, idx_bad = [], [] # initialise output
# number of indices for events in this run
n_idx = int(run_duration * sfreq)
# find all onsets in events based on event_ids
onsets = [ee for ee in events if (ee[2] in event_ids)]
for onset in onsets:
# find index of this event
onset_idx = np.where(events[:, 0] == onset[0])[0][0]
# get all indices for events in this run
idx_run = np.where((events[:, 0] > onset[0]) &
(events[:, 0] < onset[0] + n_idx))[0]
# get all events for this run
events_run = events[idx_run, :]
# check if missed frames present, and how many
missed_frames = np.where(events_run[:, 2] == 20)[0]
print('Missed frames:')
print(missed_frames)
# if good run found
if (len(missed_frames) == 0) or (missed_frames.shape[0] < max_missed):
idx_good.append(onset_idx)
else: # if invalid due to missing frames
idx_bad.append(onset_idx)
return idx_good, idx_bad
def get_sweeps_from_raw(raw, t0, sweep_duration, n_sweeps):
"""Get segments from raw data for individual frequency sweeps.
Parameters:
raw: instance of Raw
The raw data including frequency sweeps.
t0: float
Start time of segment in s.
sweep_duration: float
Duration of one sweep at one frequency (s).
n_sweeps: int
Number of sweeps (frequencies) per run.
Returns:
raw_sweeps: list of raw instances
Data segments per frequency.
"""
raw_sweeps = [] # initialise output
for ss in np.arange(0, n_sweeps):
# Start and end latencies of one frequency sweep
tmin = t0 + (ss * sweep_duration)
tmax = t0 + (ss + 1) * sweep_duration
raw_cp = raw.copy()
# Crop out one frequency sweep
raw_cp.crop(tmin=tmin, tmax=tmax)
raw_sweeps.append(raw_cp)
return raw_sweeps
def average_raws(raws):
"""Average data across raw files.
Parameters:
raws: list of instances of Raw
The raw data to average.
Every item of raws must have same info.
Returns:
raw_avg: instance of Raw
The average of raw data.
"""
# get data array from first file
data = raws[0].get_data()
if len(raws) > 1:
for raw in raws[1:]:
data += raw.get_data()
data = data / len(raws)
# don't understand 'copy' option, using default
raw_avg = mne.io.RawArray(data, raws[0].info, first_samp=0, copy='auto')
return raw_avg
# get all input arguments except first
if len(sys.argv) == 1:
sbj_ids = np.arange(0, len(config.map_subjects)) + 1
else:
# get list of subjects IDs to process
sbj_ids = [int(aa) for aa in sys.argv[1:]]
for ss in sbj_ids:
# raw, psds, psds_as_evo, freqs = run_PSD_raw(ss)
data_runs = run_get_sweeps(ss)