-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPVSWORDS_GrandAverage_Plot.py
executable file
·869 lines (582 loc) · 32.2 KB
/
FPVSWORDS_GrandAverage_Plot.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
#!/imaging/local/software/mne_python/mne1.4.0_1/bin/python
"""
Plot FPVSWORDS Grand-Mean data.
==========================================
OH, May 2023
"""
import os
from os import path as op
import numpy as np
import matplotlib
matplotlib.use('Agg') # for running graphics on cluster
from matplotlib import pyplot as plt
# needed to run on SLURM
os.environ['QT_QPA_PLATFORM'] = 'offscreen'
from mayavi import mlab
mlab.options.offscreen = True
from copy import deepcopy
from importlib import reload
import mne
from mne.report import Report
from mne.source_estimate import SourceEstimate
import config_fpvswords as config
reload(config)
import FPVS_functions as Ff
reload(Ff)
print(mne.__version__)
# sub-directory for figures per subject
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.)
# grey value for some figure backgrounds
grey_value = (0.75, 0.75, 0.75)
# Base frequencies for frequency sweep for words (not faces)
freqs_all = [str(ff) for ff in config.fpvs_freqs]
print(*freqs_all)
# separate filename prefixes for ICAed and non-ICAed data
prefix = ''
subjects_dir = config.subjects_dir
# output directory for figures
# figs_path = op.join(config.grandmean_path, figs_dir)
figs_path = '/imaging/hauk/users/olaf/MEG/GM/Figures'
# conditions
conds = config.do_conds
def grand_average_plot():
"""Plot grand-average PSDs and derivatives."""
# initialise html report for one subject
report = Report(subject='GM', title='FPVS PSDs GM')
# for STC plotting
subject = 'fsaverage'
# # get condition names and frequency names from first subject
# # assumed to be consistent across subjects
# sss_map_fname = config.sss_map_fnames[1]
# conds = [] # names of conditions
# for raw_stem_in in sss_map_fname[1][2:]:
# conds.append(raw_stem_in[:4])
# conds = np.unique(conds)
# initialise
# all psd results for evoked and STC
# individual subjects and GM
# modals = ['evo', 'stc']
# gm_modals = ['evo_gm', 'stc_gm']
modals = ['evo']
gm_modals = ['evo_gm']
# types = ['psd', 'psd_z', 'psd_sum_odd', 'psd_sum_base', 'psd_harm_odd',
# 'psd_harm_base', 'psd_harm_topos_odd', 'psd_harm_topos_base']
# evo_types = [
# 'peak_odd', 'z_peak_odd', 'harm_odd_peak_odd', 'harm_base_peak_odd',
# 'peak_base', 'z_peak_base', 'harm_odd_peak_base',
# 'harm_base_peak_base', 'peak_harm_topos_odd', 'peak_harm_topos_base']
# for evoked
types = ['psd', 'psd_z', 'psd_sum_odd', 'psd_sum_base', 'psd_harm_odd',
'psd_harm_base', 'psd_harm_topos_odd', 'psd_harm_topos_base']
# 'psd_sum_base_indiv_topos', 'psd_sum_odd_indiv_topos']
# only for evoked: data for peak channels per condition
evo_types = []
# [
# 'peak_odd', 'z_peak_odd', 'harm_odd_peak_odd',
# 'harm_base_peak_odd', 'peak_base', 'z_peak_base', 'harm_odd_peak_base',
# 'harm_base_peak_base', 'peak_harm_topos_odd', 'peak_harm_topos_base']
# for STCs
stc_types = ['psd', 'psd_sum_odd', 'psd_sum_base', 'psd_harm_odd',
'psd_harm_base', 'psd_harm_topos_odd', 'psd_harm_topos_base']
psds = {}
do_modals = modals + gm_modals
# extract label amplitudes
label_amps = {}
for ss in stc_types:
label_amps[ss] = {'lh': [], 'rh': []}
# Initialise
for modal in do_modals:
psds[modal] = {} # type of data
do_types = types
if modal[:3] == 'evo': # add other types
do_types = do_types + evo_types
for tt in do_types:
psds[modal][tt] = {} # type of processed PSD
for cond in conds:
psds[modal][tt][cond] = {} # sweep frequencies
for ev_type in config.event_ids[cond]:
psds[modal][tt][cond][ev_type] = [] # subjects
# Read Evoked GM data
# Path for grand-mean results
sbj_path = config.grandmean_path
if 'evo' in modals:
modal = 'evo' # do the evoked results here
do_types = types + evo_types
for tt in do_types:
for cond in conds: # conditions
print('###\nCondition: %s.\n###' % cond)
# if all frequencies in one evoked file
if tt in types:
for ev_type in config.event_ids[cond]:
fname_evo = op.join(sbj_path, 'AVE', 'GM_%s_%s_%s-ave.fif' %
(tt, cond, ev_type))
evokeds = mne.read_evokeds(fname=fname_evo)
print(ev_type)
psds[modal][tt][cond][ev_type] = evokeds[0]
elif tt in evo_types:
for ev_type in config.event_ids[cond]:
fname_evo = op.join(
sbj_path, 'AVE', 'GM_%s_%s_%s-ave.fif' %
(tt, cond, ev_type))
evokeds = mne.read_evokeds(fname=fname_evo)
psds[modal][tt][cond][ev_type] = evokeds[0]
print('Done reading evoked files.')
# get channel names for MEG channel selections
channel_ROIs = Ff.get_MEG_ROI_channel_names(
config.meg_selections, evokeds[0].info)
for roi in config.meg_selections:
config.channel_ROIs['Mag ' + roi] = channel_ROIs['Mag ' + roi]
config.channel_ROIs['Grad ' + roi] = channel_ROIs['Grad ' + roi]
# # average certain frequencies, to be plotted separately
# if cond != 'face': # if a word condition
# print('Averaging frequencies: ')
# print(*avg_freqs)
# evo_freqs = [] # collect evoked across frequencies
# for freq in avg_freqs:
# # topography for oddball frequency
# evoked = psds[modal][tt][cond][freq]
# evo_freqs.append(evoked)
# psds[modal][tt][cond]['avg'] =\
# Ff.grand_average_evoked_arrays(evo_freqs)
# PLOTTING ############################################################
print('Plotting.')
chtypes = ['mag', 'grad', 'eeg'] # for some topo plots
for cond in conds:
print('Condition %s.' % cond)
# # Plot topographies for individuals
# do_types = ['psd_sum_odd_indiv_topos', 'psd_sum_base_indiv_topos']
# for tt in do_types:
# for ev_type in config.event_ids[cond]:
# evoked = psds[modal][tt][cond][ev_type]
# print('Scaling topographies per sample.')
# evoked = Ff.scale_evoked_per_channel_type(evoked)
# for chtype in chtypes:
# # scaling to individual maxima per topography
# vmin, vmax = 0., 1.
# fig = evoked.plot_topomap(times=evoked.times,
# ch_type=chtype,
# vmin=vmin, vmax=vmax,
# scalings=unit_scalings[chtype],
# units='Z', show=False)
# for ax in fig.axes:
# # ax.set_xlabel(fontsize=24)
# # ax.set_ylabel(fontsize=24)
# xtl = ax.get_xticklabels()
# ax.set_xticklabels(labels=xtl,
# fontdict={'fontsize': 12})
# ytl = ax.get_yticklabels()
# ax.set_yticklabels(labels=ytl,
# fontdict={'fontsize': 12})
# fig_fname = op.join(
# figs_path, '%s_%s_%s_%s.jpg' %
# (tt, cond, ev_type, chtype))
# print('Saving individual topographies: %s.' % fig_fname)
# fig.savefig(fig_fname)
# Plot topographies for sum across harmonic for oddball and base
# frequencies
do_types = ['psd_sum_odd', 'psd_sum_base']
for tt in do_types:
for ev_type in config.event_ids[cond]:
# topography
evoked = psds[modal][tt][cond][ev_type]
times = [0.]
sec_label = '%s_%s' % (cond, ev_type)
file_label = '%s_%s_%s_%s' % (prefix, cond, tt, ev_type)
# Filename stem for figure; channel type to be added later
fname_fig = op.join(figs_path, file_label)
print('Creating figure %s.' % fname_fig)
figs = Ff.plot_evo_topomap(evoked, times, chtypes,
fname_fig)
for ax in figs[0].axes:
# ax.set_xlabel(fontsize=24)
# ax.set_ylabel(fontsize=24)
xtl = ax.get_xticklabels()
ax.set_xticklabels(labels=xtl,
fontdict={'fontsize': 12})
ytl = ax.get_yticklabels()
ax.set_yticklabels(labels=ytl,
fontdict={'fontsize': 12})
for [fig, chtype] in zip(figs, chtypes):
report.add_figure(fig, tt, section=sec_label)
# plot amplitudes across harmonics for electrode groups
print('Plotting topographies and amplitudes across harmonics.')
do_types = ['psd_harm_topos_base', 'psd_harm_topos_odd']
for tt in do_types:
for ev_type in config.event_ids[cond]:
print('Plot GM evoked for %s %s.' % (cond, ev_type))
evoked = psds[modal][tt][cond][ev_type]
# # change times for plotting to one sample per "second"
# times = evoked.times
# evoked.times = np.arange(0., len(times), 1.)
# label for condition and base frequency
sec_label = '%s_%s' % (cond, ev_type)
file_label = '%s_%s_%s_%s' % (prefix, cond, tt, ev_type)
# Plot topopraphies for all harmonics
# Filename stem for figure; channel type to be added later
fname_fig = op.join(figs_path, file_label)
print('Creating figure %s.' % fname_fig)
times = evoked.times # all harmonics
figs = Ff.plot_evo_topomap(evoked, times, chtypes,
fname_fig)
for ax in fig.axes:
# ax.set_xlabel(fontsize=24)
# ax.set_ylabel(fontsize=24)
xtl = ax.get_xticklabels()
ax.set_xticklabels(labels=xtl,
fontdict={'fontsize': 12})
ytl = ax.get_yticklabels()
ax.set_yticklabels(labels=ytl,
fontdict={'fontsize': 12})
# # plot spectra for EEG channel selections
# for roi in config.channel_ROIs:
# # determine channel type for scaling
# if roi[:3] == 'EEG':
# chtype = 'eeg'
# elif roi[:3] == 'Gra':
# chtype = 'grad'
# elif roi[:3] == 'Mag':
# chtype = 'mag'
# evoked_roi = deepcopy(evoked)
# ch_names = config.channel_ROIs[roi]
# evoked_roi.pick_channels(ch_names)
# n = len(evoked_roi.times)
# evoked_roi.times = np.arange(1., n + 1, 1.)
# # Plot for peak channels without topographies
# # add a bit more space for channel plot
# max_val = evoked_roi.data.max()
# ylim_vals = [0, evoked_roi.data.max() + 0.33 * max_val]
# fig = evoked_roi.plot(
# spatial_colors=True, picks=None,
# scalings=unit_scalings,
# ylim={chtype: ylim_vals},
# gfp=False)
# for ax in fig.axes:
# # ax.set_xlabel(fontsize=24)
# # ax.set_ylabel(fontsize=24)
# xtl = ax.get_xticklabels()
# ax.set_xticklabels(labels=xtl,
# fontdict={'fontsize': 12})
# ytl = ax.get_yticklabels()
# ax.set_yticklabels(labels=ytl,
# fontdict={'fontsize': 12})
# # grey background for better line visibility
# ax.set_facecolor(grey_value)
# fname_fig = op.join(figs_path,
# file_label + '_%s.jpg' % roi)
# print('Creating figure %s.' % fname_fig)
# fig.savefig(fname_fig)
# sec_label = sec_label + ' ' + roi
# report.add_figure(fig, sec_label, section=sec_label)
# get singular values per channel type
# don't include last harmonic because of MEG artefact
idx = np.arange(0, evoked.data.shape[1] - 1, 1)
ss = Ff.svd_per_channel_type(evoked, idx)[0]
# channel types for SVD
ch_types = ['grad', 'mag', 'eeg']
# create new pyplot figure, subplots for channel types
fig, axs = plt.subplots(len(ch_types), 1)
for [ci, cht] in enumerate(ch_types):
# turn singular values into variances
s = 100. * ss[cht]**2 / (ss[cht]**2).sum()
x = np.arange(1, len(s) + 1, 1)
# plot singular values to figure
axs[ci].plot(x, s)
axs[ci].set_title(cht)
# axs[ci].set_xlabel(fontsize=24)
# axs[ci].set_ylabel(fontsize=24)
xtl = axs[ci].get_xticklabels()
axs[ci].set_xticklabels(labels=xtl,
fontdict={'fontsize': 12})
ytl = axs[ci].get_yticklabels()
axs[ci].set_yticklabels(labels=ytl,
fontdict={'fontsize': 12})
# grey background for better line visibility
ax.set_facecolor(grey_value)
fig.tight_layout(pad=1.)
fname_fig = op.join(
figs_path, file_label + '_svd.jpg')
# save figure for this channel type
fig.savefig(fname_fig)
plt.close('all') # close pyplot figures
# plot evoked spectra and topographies (plot_joint())
do_types = ['psd', 'psd_z']
for tt in do_types:
for ev_type in config.event_ids[cond]:
print('Plot GM evoked for %s %s.' % (cond, ev_type))
evoked = psds[modal][tt][cond][ev_type]
# label for condition and base frequency
sec_label = '%s_%s' % (cond, ev_type)
file_label = '%s_%s_%s_%s' % (prefix, cond, tt, ev_type)
if tt == 'psd_z': # scale z-scores to "significance"
ylim = {'mag': [0, 2], 'grad': [0, 2], 'eeg': [0, 2]}
else:
ylim = None # scale to extrema
figs = Ff.plot_psd_as_evo(evoked, sbj_path, picks=None,
txt_label=file_label,
close_fig=close_fig,
scalings=unit_scalings,
ylim=ylim)
for [fig, chtype] in zip(figs, chtypes):
report.add_figure(fig, file_label, section=sec_label)
# # plot spectra for EEG channel selections
# for roi in config.channel_ROIs:
# evoked_roi = deepcopy(evoked)
# ch_names = config.channel_ROIs[roi]
# evoked_roi.pick_channels(ch_names)
# # CROP PSD for display
# evoked_roi.crop(tmin=config.crop_times[0],
# tmax=config.crop_times[1])
# # Plot for peak channels without topographies
# fig = evoked_roi.plot(spatial_colors=True, picks=None,
# scalings=unit_scalings,
# gfp=False, ylim=ylim)
# for ax in fig.axes:
# # ax.set_xlabel(fontsize=24)
# # ax.set_ylabel(fontsize=24)
# xtl = ax.get_xticklabels()
# ax.set_xticklabels(labels=xtl,
# fontdict={'fontsize': 12})
# ytl = ax.get_yticklabels()
# ax.set_yticklabels(labels=ytl,
# fontdict={'fontsize': 12})
# # grey background for better line visibility
# ax.set_facecolor(grey_value)
# fname_fig = op.join(figs_path,
# file_label + '_%s.jpg' % roi)
# print('Creating figure %s.' % fname_fig)
# fig.savefig(fname_fig)
# sec_label = sec_label + ' ' + roi
# report.add_figure(fig, sec_label, section=sec_label)
# plt.close('all')
# # plot evoked spectra for peak channels
# do_types = ['peak_odd', 'peak_base', 'z_peak_odd', 'z_peak_base']
# for tt in do_types:
# for ev_type in config.event_ids[cond]:
# print('Plot GM evoked for %s %s.' % (cond, ev_type))
# evoked = psds[modal][tt][cond][ev_type]
# # CROP PSD for display
# evoked.crop(tmin=config.crop_times[0],
# tmax=config.crop_times[1])
# if tt[0] == 'z':
# ylim = {'mag': [0, 2], 'grad': [0, 2], 'eeg': [0, 2]}
# else:
# ylim = None
# # Plot for peak channels without topographies
# fig = evoked.plot(spatial_colors=True, picks=None,
# scalings=unit_scalings, gfp=False)
# for ax in fig.axes:
# # ax.set_xlabel(fontsize=24)
# # ax.set_ylabel(fontsize=24)
# xtl = ax.get_xticklabels()
# ax.set_xticklabels(labels=xtl,
# fontdict={'fontsize': 12})
# ytl = ax.get_yticklabels()
# ax.set_yticklabels(labels=ytl,
# fontdict={'fontsize': 12})
# # grey background for better line visibility
# ax.set_facecolor(grey_value)
# sec_label = '%s_%s' % (cond, ev_type)
# file_label = '%s_%s_%s_%s' % (prefix, cond, tt, ev_type)
# fname_fig = op.join(figs_path, file_label + '.jpg')
# print('Creating figure %s.' % fname_fig)
# fig.savefig(fname_fig)
# report.add_figure(fig, sec_label, section=sec_label)
# plt.close('all')
# # plot amplitudes of harmonics for peak channels
# do_types = ['peak_harm_topos_odd', 'peak_harm_topos_base']
# for tt in do_types:
# for ev_type in config.event_ids[cond]:
# print('Plot GM evoked for %s %s.' % (cond, ev_type))
# evoked = psds[modal][tt][cond][ev_type]
# times = evoked.times
# evoked.times = np.arange(1., len(times) + 1, 1.)
# # Plot for peak channels without topographies
# fig = evoked.plot(spatial_colors=True, picks=None,
# scalings=unit_scalings, gfp=False,
# sphere=None)
# for ax in fig.axes:
# # ax.set_xlabel(fontsize=24)
# # ax.set_ylabel(fontsize=24)
# xtl = ax.get_xticklabels()
# ax.set_xticklabels(labels=xtl,
# fontdict={'fontsize': 14})
# ytl = ax.get_yticklabels()
# ax.set_yticklabels(labels=ytl,
# fontdict={'fontsize': 14})
# # grey background for better line visibility
# ax.set_facecolor(grey_value)
# sec_label = '%s_%s' % (cond, ev_type)
# file_label = '%s_%s_%s_%s' % (prefix, cond, tt, ev_type)
# fname_fig = op.join(figs_path, file_label + '.jpg')
# print('Creating figure %s.' % fname_fig)
# fig.savefig(fname_fig)
# # also create PDF because some edits may be needed
# fname_fig = op.join(figs_path, file_label + '.pdf')
# print('Creating figure %s.' % fname_fig)
# fig.savefig(fname_fig)
# report.add_figure(fig, sec_label, section=sec_label)
plt.close('all')
# plot spectra around target frequencies
do_types = ['psd_harm_odd', 'psd_harm_base']
for tt in do_types:
for ev_type in config.event_ids[cond]:
print('Plot GM target frequencies for %s %s.' %
(cond, ev_type))
evoked = psds[modal][tt][cond][ev_type]
fig = evoked.plot(spatial_colors=True, picks=None,
scalings=unit_scalings, gfp=False)
for ax in fig.axes:
# ax.set_xlabel(fontsize=24)
# ax.set_ylabel(fontsize=24)
xtl = ax.get_xticklabels()
ax.set_xticklabels(labels=xtl,
fontdict={'fontsize': 12})
ytl = ax.get_yticklabels()
ax.set_yticklabels(labels=ytl,
fontdict={'fontsize': 12})
# grey background for better line visibility
ax.set_facecolor(grey_value)
sec_label = '%s_%s' % (cond, ev_type)
file_label = '%s_%s_%s_%s' % (prefix, cond, tt, ev_type)
fname_fig = op.join(figs_path, file_label + '.jpg')
print('Creating figure %s.' % fname_fig)
fig.savefig(fname_fig)
report.add_figure(fig, sec_label, section=sec_label)
# # plot spectra for EEG channel selections
# for roi in config.channel_ROIs:
# evoked_roi = deepcopy(evoked)
# ch_names = config.channel_ROIs[roi]
# evoked_roi.pick_channels(ch_names)
# # Plot for peak channels without topographies
# fig = evoked_roi.plot(spatial_colors=True, picks=None,
# scalings=unit_scalings,
# gfp=False)
# for ax in fig.axes:
# # fig.axes[0].set_xlabel(fontsize=24)
# # fig.axes[0].set_ylabel(fontsize=24)
# xtl = ax.get_xticklabels()
# ax.set_xticklabels(labels=xtl,
# fontdict={'fontsize': 12})
# ytl = ax.get_yticklabels()
# ax.set_yticklabels(labels=ytl,
# fontdict={'fontsize': 12})
# # grey background for better line visibility
# ax.set_facecolor(grey_value)
# fname_fig = op.join(figs_path,
# file_label + '_%s.jpg' % roi)
# print('Creating figure %s.' % fname_fig)
# fig.savefig(fname_fig)
# sec_label = sec_label + ' ' + roi
# report.add_figure(fig, sec_label, section=sec_label)
# plt.close('all')
# # plot spectra around target frequencies for peak channels
# do_types = ['harm_odd_peak_odd', 'harm_base_peak_odd',
# 'harm_odd_peak_base', 'harm_base_peak_base']
# for tt in do_types:
# for ev_type in config.event_ids[cond]:
# print('Plot GM evoked for %s %s.' % (cond, ev_type))
# evoked = psds[modal][tt][cond][ev_type]
# # Plotting PSDs across harmonics
# fig = evoked.plot(spatial_colors=True, picks=None,
# scalings=unit_scalings, gfp=False)
# for ax in fig.axes:
# # ax.set_xlabel(fontsize=24)
# # ax.set_ylabel(fontsize=24)
# xtl = ax.get_xticklabels()
# ax.set_xticklabels(labels=xtl,
# fontdict={'fontsize': 12})
# ytl = ax.get_yticklabels()
# ax.set_yticklabels(labels=ytl,
# fontdict={'fontsize': 12})
# # grey background for better line visibility
# ax.set_facecolor(grey_value)
# sec_label = '%s_%s' % (cond, ev_type)
# file_label = '%s_%s_%s_%s' % (prefix, cond, tt,
# ev_type)
# fname_fig = op.join(figs_path, file_label + '.jpg')
# print('Creating figure %s.' % fname_fig)
# fig.savefig(fname_fig)
# report.add_figure(fig, sec_label, section=sec_label)
plt.close('all')
# Plot STCs
if 'stc' in modals:
modal = 'stc' # do source estimates here
for tt in stc_types:
for cond in conds: # conditions
print('###\nCondition: %s.\n###' % cond)
stc_evs = {}
for ev_type in config.event_ids[cond]:
fname_stc = op.join(
config.grandmean_path, 'STC',
'%s_%s_%s_%s-lh.stc' % (prefix, tt, cond, ev_type)
)
print('Reading source estimate from %s.' % fname_stc)
stc = mne.read_source_estimate(fname_stc)
stc_evs[ev_type] = stc
for ev_type in config.event_ids[cond]:
# use STC for this event type
stc = stc_evs[ev_type]
time_label = None
# index to time point 0, which will be plotted
idx0 = np.abs(stc.times).argmin()
thresh = np.abs(stc.data[:, idx0]).max()
# # get some round numbers for colour bar
# if thresh < 10:
# thresh = np.floor(thresh)
# elif thresh < 50:
# thresh = 5 * np.floor(thresh / 5.)
# else:
# thresh = 10 * np.floor(thresh / 10.)
# plot for left and right hemisphere
for hemi in ['both']: # ['lh', 'rh']:
# for some reason, 'both' only works for 'ven' but not
# for 'lat'
for view in ['ven']:
brain = stc.plot(
subject=subject, initial_time=0.,
time_label=time_label,
subjects_dir=subjects_dir,
clim=dict(kind='value',
lims=[0, thresh / 2., thresh]),
hemi=hemi, views=view
)
fname_fig = op.join(
figs_path,
'%s_%s_%s_%s_STC_%s_%s.jpg' %
(prefix, tt, cond, ev_type, hemi, view)
)
print('Saving figure to %s.' % fname_fig)
mlab.savefig(fname_fig)
mlab.close(all=True)
# plot for left and right hemisphere
for hemi in ['lh', 'rh']:
# for some reason, 'both' only works for 'ven' but not
# for 'lat'
for view in ['lat']:
# apparently 'brain' required for saving?
brain = stc.plot(
subject=subject, initial_time=0.,
time_label=time_label,
subjects_dir=subjects_dir,
clim=dict(kind='value',
lims=[0, thresh / 2., thresh]),
hemi=hemi, views=view
)
fname_fig = op.join(
figs_path,
'%s_%s_%s_%s_STC_%s_%s.jpg' %
(prefix, tt, cond, ev_type, hemi, view)
)
print('Saving figure to %s.' % fname_fig)
mlab.savefig(fname_fig)
mlab.close(all=True)
# Save HTML report
fname_report = op.join(figs_path, prefix + 'GM_report.html')
report.save(fname_report, overwrite=True, open_browser=False)
plt.close('all')
return
grand_average_plot()