-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPVS_PSD_Source_sweep.py
executable file
·359 lines (241 loc) · 12.6 KB
/
FPVS_PSD_Source_sweep.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
352
353
354
355
356
357
358
359
#!/imaging/local/software/miniconda/envs/mne0.19/bin/python
"""
Compute PSDs in Source Space for average raw data for FPVS Frequency Sweep.
Average raw data from FPVS_get_sweeps.py.
Based on FOVS_PSD_sweep.py.
# Plot figures.
# Compute z-scores.
# Compute TFR if specified.
==========================================
OH, Possible better to add to existing function
"""
import sys
from os import path as op
import numpy as np
from copy import deepcopy
# import matplotlib
# matplotlib.use('Agg') # for running graphics on cluster ### EDIT
from matplotlib import pyplot as plt
from importlib import reload
import mne
from mne.report import Report
import config_sweep as config
reload(config)
import FPVS_functions as Ff
reload(Ff)
print(mne.__version__)
# perform TFR of raw data or not
# do_tfr = config.do_tfr
# sub-directory for figures per subject
# separate for ICAed and non-ICAed data
if 'ica' in config.raw_ICA_suff:
figs_dir = 'Figures_ICA'
else:
figs_dir = 'Figures'
close_fig = 1 # close figures only if close_fig==1
# plt.ion() # interactive plotting
# for some plots of SNRs
unit_scalings = dict(eeg=1., mag=1., grad=1.)
print('Sunshine.')
def run_PSD_raw(sbj_id):
"""Compute spectra for one subject."""
# initialise html report for one subject
report = Report(subject=str(sbj_id), title='FPVS PSDs')
subject = config.mri_subjects[sbj_id]
# path to subject's data
sbj_path = op.join(config.data_path, config.map_subjects[sbj_id][0])
inv_fname = op.join(sbj_path, subject + '_EEGMEG-inv.fif')
print('Reading EEG/MEG inverse operator: %s.' % inv_fname)
invop = mne.minimum_norm.read_inverse_operator(inv_fname)
# raw-filename mappings for this subject
sss_map_fname = config.sss_map_fnames[sbj_id]
# get condition names and frequency names
conds = [] # names of conditions
for raw_stem_in in sss_map_fname[1][2:]:
conds.append(raw_stem_in[:4])
conds = np.unique(conds)
freqs_all = [str(ff) for ff in config.fpvs_freqs]
print('Frequencies used: ')
print(freqs_all)
# initialise sum across harmonics for conditions
sum_harms = {}
for cond in conds:
sum_harms[cond] = {}
# Go through conditions and frequencies
for cond in conds: # conditions
# create list of Evoked objects for all frequencies per condition
psds_as_evo, psds_z_as_evo, sum_as_evo, psd_harm_as_evo,\
psd_harm_base_as_evo = [], [], [], [], []
if cond == 'face': # hack, no frequency sweep for faces
freqs = ['6.0']
else: # for all word condition, use all sweep frequencies
freqs = freqs_all
for freq in freqs: # frequencies
sum_harms[cond][freq] = [] # initialise for this base frequency
# input average raw data; remove dot from frequency string
fname = 'rawavg_%s_%s_%s.fif' % (cond, ''.join(freq.split('.')),
config.raw_ICA_suff)
fname_raw_in = op.join(sbj_path, fname)
print('Reading average raw data from %s:' % fname_raw_in)
raw = mne.io.read_raw_fif(fname_raw_in, preload=True)
# reduce raw data to relevant channels
raw.pick_types(meg=True, eeg=True, eog=False, ecg=False,
stim=False, misc=False, chpi=False)
# Compute PSD for raw data
n_fft = len(raw.times)
psds
# print('Computing psd_welch().')
# psds, psd_freqs = mne.time_frequency.psd_welch(raw, fmin=0.,
# fmax=40.,
# n_fft=n_fft)
# print('Frequency resolution: %f.' % (psd_freqs[1] - psd_freqs[0]))
# info = raw.info
# # To plot PSDs like Evoked, pretend sample frequency
# info['sfreq'] = 1000. / (psd_freqs[1] - psd_freqs[0])
# # convert PSD to amplitudes (rather than power)
# psds = np.sqrt(psds)
# # "BASELINE-CORRECT" PSDs with neighbouring frequency bins
# print("\nCorrecting baseline.\n")
# if config.psd_base_bins > 0:
# psds = Ff.psd_correct_baseline(psds, config.psd_base_bins,
# config.psd_n_gap)
# print('Computing SNRs.')
# psds_z = Ff.psd_convert_to_snr(psds, config.psd_snr_bins,
# config.psd_n_gap)
# # Baseline-corrected PSDs as Evoked object
# as_evo = mne.EvokedArray(psds, info,
# tmin=psd_freqs[0] / 1000.,
# comment=('PSD ' + cond + ' ' + freq))
# psds_as_evo.append(as_evo)
# # z-scored PSDs as Evoked object
# as_evo = mne.EvokedArray(psds_z, info,
# tmin=psd_freqs[0] / 1000.,
# comment=('PSD_Z ' + cond + ' ' + freq))
# psds_z_as_evo.append(as_evo)
# # Plot PSD as spectrum plus topographies (plot_joint())
# print('Plotting PSDs.')
# # label for condition and base frequency
# label_str = '%s_%s' % (cond, ''.join(freq.split('.')))
# file_label = 'PSDtopo_%s' % label_str
# figs = Ff.plot_psd_as_evo(psds_as_evo[-1], sbj_path, file_label,
# close_fig)
# chtypes = ['mag', 'grad'] # sequence of plots in plot_joint
# if len(figs) == 3:
# chtypes = chtypes + ['eeg']
# for [fig, chtype] in zip(figs, chtypes):
# sec_label = '%s_%s' % (label_str, chtype)
# report.add_figs_to_section(fig, sec_label, section=sec_label,
# scale=1)
# file_label = 'PSDtopoZ_%s' % label_str
# figs = Ff.plot_psd_as_evo(psds_z_as_evo[-1], sbj_path, file_label,
# close_fig, scalings=unit_scalings)
# for [fig, chtype] in zip(figs, chtypes):
# sec_label = '%s_%s_Z' % (label_str, chtype)
# report.add_figs_to_section(fig, sec_label, section=sec_label,
# scale=1)
# # Compute the sum across harmonics of oddball frequency for this
# # condition and base frequency
# print('Combining harmonics.')
# if cond == 'face':
# # round to make sure combine_harmonics finds the right
# # frequencies
# oddfreq = round(config.fpvs_odd_freq['faces'], 2)
# else:
# oddfreq = round(config.fpvs_odd_freq['words'], 2)
# basefreq = float(freq) # hack, float-to-string-to-float-again
# sum_harms[cond][freq] = Ff.combine_harmonics_topos(psds=psds_z,
# freqs=psd_freqs,
# basefreq=basefreq,
# oddfreq=oddfreq,
# n_harms=config.fpvs_n_harms,
# method='sum')
# # needs another dimension for Evoked object
# to_evo = np.expand_dims(sum_harms[cond][freq], 1)
# # Combined harmonics as Evoked object
# as_evo = mne.EvokedArray(to_evo, info,
# tmin=basefreq / 1000.,
# comment=('Sum ' + cond + ' ' + freq))
# sum_as_evo.append(as_evo)
# # get PSDs around harmonics
# psd_harm = Ff.psds_across_harmonics(psds=psds_z, freqs=psd_freqs,
# basefreq=basefreq,
# oddfreq=oddfreq,
# n_harms=config.fpvs_n_harms,
# n_bins=config.psd_snr_bins,
# method='sum')
# # Sanity check - do it for base frequency
# # i.e. basefreq as oddfreq here, for all its harmonics
# psd_harm_base = Ff.psds_across_harmonics(psds=psds_z, freqs=psd_freqs,
# basefreq=999.,
# oddfreq=basefreq,
# n_harms=3,
# n_bins=config.psd_snr_bins,
# method='sum')
# tmin = -config.psd_snr_bins * 0.001 # include baseline
# info['sfreq'] = 1000. # to display samples as time points
# as_evo = mne.EvokedArray(psd_harm, info, tmin=tmin,
# comment=('PSD Harm ' + cond + ' ' + freq))
# psd_harm_as_evo.append(as_evo)
# as_evo = mne.EvokedArray(psd_harm_base, info, tmin=tmin,
# comment=('PSD Harm ' + cond + ' ' + freq))
# psd_harm_base_as_evo.append(as_evo)
# # Plotting PSDs across harmonics
# fig = psd_harm_as_evo[-1].plot(spatial_colors=True,
# scalings=unit_scalings)
# # path to sub-directory for figures
# figs_path = op.join(sbj_path, figs_dir)
# fname_fig = op.join(figs_path, 'HarmPSD_%s_%s.pdf'
# % (cond, freq))
# fig.savefig(fname_fig)
# sec_label = '%s_%s_HarmPSD' % (label_str, chtype)
# report.add_figs_to_section(fig, sec_label, section=sec_label,
# scale=1)
# plt.close(fig)
# # Plotting PSDs across harmonics
# fig = psd_harm_base_as_evo[-1].plot(spatial_colors=True,
# scalings=unit_scalings)
# fname_fig = op.join(figs_path, 'HarmPSDbase_%s_%s.pdf'
# % (cond, freq))
# fig.savefig(fname_fig)
# sec_label = '%s_%s_HarmPSDbase' % (label_str, chtype)
# report.add_figs_to_section(fig, sec_label, section=sec_label,
# scale=1)
# plt.close(fig)
# times = [basefreq / 1000.]
# # Filename stem for figure; channel type to be added later
# fname_fig = op.join(figs_path, 'SumTopo_%s_%s' % (cond, freq))
# # For html report section label
# sec_label = '%s_%s_Sum' % (label_str, chtype)
# Ff.plot_evo_topomap(sum_as_evo[-1], times, chtypes, fname_fig, report,
# label_str)
# # Save HTML report
# fname_report = op.join(figs_path, str(sbj_id) +
# '_report.html')
# report.save(fname_report, overwrite=True, open_browser=False)
# # Save Evoked objects for later group stats:
# print('Saving PSD results as evoked files:')
# fname_evo = op.join(sbj_path, 'PSDtopo_%s%s' % (cond, '-ave.fif'))
# print(fname_evo)
# mne.write_evokeds(fname_evo, psds_as_evo)
# fname_evo = op.join(sbj_path, 'PSDtopoZ_%s%s' % (cond, '-ave.fif'))
# print(fname_evo)
# mne.write_evokeds(fname_evo, psds_z_as_evo)
# fname_evo = op.join(sbj_path, 'HarmPSD_%s%s' % (cond, '-ave.fif'))
# print(fname_evo)
# mne.write_evokeds(fname_evo, psd_harm_as_evo)
# fname_evo = op.join(sbj_path, 'HarmPSDbase_%s%s' % (cond, '-ave.fif'))
# print(fname_evo)
# mne.write_evokeds(fname_evo, psd_harm_base_as_evo)
# fname_evo = op.join(sbj_path, 'SumTopo_%s%s' % (cond, '-ave.fif'))
# print(fname_evo)
# mne.write_evokeds(fname_evo, sum_as_evo)
return
# 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)
run_PSD_raw(ss)