-
Notifications
You must be signed in to change notification settings - Fork 17
/
pop_roi_connectplot.m
903 lines (820 loc) · 37.7 KB
/
pop_roi_connectplot.m
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
% pop_roi_connectplot - plot results of connectivity analysis computed
% by roi_connect.
% Usage:
% pop_roi_connectplot(EEG, 'key', 'val', ...);
%
% Inputs:
% EEG - EEGLAB dataset
%
% Required inputs:
% 'headmodel' - [string] head model file in MNI space
% 'sourcemodel' - [string] source model file
%
% Optional inputs:
% 'measure' - ['psd'|'roipsd'|'trgc'|'crossspecimag'|'crossspecpow'|'mic'|'mim']
% 'psd' : Source power spectrum
% 'psdroi': ROI based power spectrum
% 'TRGC' : Time-reversed granger causality
% 'GC' : Granger causality
% 'crossspecimag': Imaginary part of coherence from cross-spectrum
% 'crossspecpow' : Average cross-spectrum power for each ROI
% 'aCOH': Coherence
% 'iCOH': Absolute value of the imaginary part of Coherency
% 'MIC' : Maximized Imaginary Coherency for each ROI
% 'MIM' : Multivariate Interaction Measure for each ROI
% 'pac' : Phase-amplitude coupling for a certain frequency (band) combination based on bicoherence
% 'pac_anti': Phase-amplitude coupling for a certain frequency (band) combination based on the antisymmetrized bicoherence
% 'freqrange' - [min max] frequency range or [integer] single frequency in Hz. Default is to plot broadband power.
% 'smooth' - [float] smoothing factor for cortex surface plotting
% 'plotcortex' - ['on'|'off'] plot results on smooth cortex. Default is 'on'
% 'plotcortexparams' - [cell] ...
% 'plotcortexseedregion' - [string] plot seed voxel on cortex. Takes name of seed region as input.
% 'plot3d' - ['on'|'off'] ... Default is 'off'
% 'plot3dparams' - [cell] optional parameters for the generation of brain movies. Check the related documentation in roi_plotbrainmovie.m
% 'plotmatrix' - ['on'|'off'] plot results as ROI to ROI matrix. Default is 'off'
% 'plotbarplot' - ['on'|'off'] plot ROI based power spectrum as barplot. Default is 'off'
% 'hemisphere' - ['all'|'left'|'right'] hemisphere options for ROI to ROI matrix. Default is 'all'
% 'grouphemispheres' - ['on'|'off'] group ROIs by hemispheres (left hemisphere, then right hemisphere). Default is 'off'
% 'region' - ['all'|'cingulate'|'prefrontal'|'frontal'|'temporal'|'parietal'|'central'|'occipital'] region selection for ROI to ROI matrix. Default is 'all'
% 'largeplot' - ['on'|'off'] plot MIM, TRGC and Power in a single large plot. Default is 'off'
% 'plotpsd' - ['on'|'off'] plot PSD (for 'crossspecpow' only). Default is 'off'
% 'noplot' - ['on'|'off'] when 'on', disable all plotting. Default is 'off'
%
% Output
% matnet - connectivity matrix (nROI x nROI)
%
% Author: Stefan Haufe and Arnaud Delorme, 2019
%
% Example:
% % Requires prior call to pop_roi_connect
% matnet = pop_roi_connectplot(EEG, 'measure', 'ROIPSD');
%
% See also: STD_ROI_CONNECTPLOT, ROI_ACTIVITY, POP_ROI_ACTIVITY, POP_LEADFIELD
% Copyright (C) Arnaud Delorme, [email protected]
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
function [matrix, com] = pop_roi_connectplot(EEG, varargin)
matrix = [];
com = '';
if nargin < 1
help pop_roi_connectplot;
return
end
if ~isfield(EEG, 'roi')
error('Compute connectivity first');
end
if ~exist('roi_plotbrainmovie')
fprintf(2, 'To plot connectivity in 3-D, install the brainmovie plugin\n');
plot3dFlag = -1;
else
plot3dFlag = 0;
end
% if ~isfield(EEG.dipfit, 'hdmfile')
% error('You need to select a head model file using DIPFIT settings first');
% end
%
% if ~isequal(EEG.dipfit.coordformat, 'MNI')
% error('You can only use this function with MNI coordinates - change head model');
% end
cortexFlag = isfield(EEG.roi.cortex, 'Faces')*2-1;
areaNames = [ 'All' { EEG.roi.atlas(1).Scouts.Label } ];
splot = [];
% splot(end+1).label = 'Source power spectrum'; % we do not save that information anymore
% splot(end ).acronym = 'PSD';
% splot(end ).unit = '?'; % not used yet
% splot(end ).cortex = cortexFlag;
% splot(end ).matrix = -1;
% splot(end ).psd = -1;
if isfield(EEG.roi, 'source_roi_power')
splot(end+1).label = 'ROI based power spectrum';
splot(end ).labelshort = 'ROI PSD';
splot(end ).acronym = 'ROIPSD';
splot(end ).unit = 'Power (dB)'; % not used yet
splot(end ).cortex = cortexFlag;
splot(end ).matrix = -1;
splot(end ).psd = -1;
splot(end ).plot3d = -1;
end
if isfield(EEG.roi, 'CS')
splot(end+1).label = 'ROI to ROI cross-spectrum';
splot(end ).labelshort = 'Cross-spectrum';
splot(end ).acronym = 'crossspecpow';
splot(end ).unit = 'Power (dB)';
splot(end ).cortex = cortexFlag;
splot(end ).matrix = -1;
splot(end ).psd = 0;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'CS')
splot(end+1).label = 'ROI to ROI imaginary part of cross-spectrum';
splot(end ).labelshort = 'Img. part of cross-spectrum';
splot(end ).acronym = 'crossspecimag';
splot(end ).unit = 'net |iCOH|';
splot(end ).cortex = cortexFlag;
splot(end ).matrix = 1;
splot(end ).psd = -1;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'aCOH')
splot(end+1).label = 'ROI to ROI coherence';
splot(end ).labelshort = 'Coherence';
splot(end ).acronym = 'aCOH';
splot(end ).unit = 'aCOH';
splot(end ).cortex = cortexFlag;
splot(end ).matrix = 1;
splot(end ).psd = -1;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'cCOH')
splot(end+1).label = 'ROI to ROI coherency';
splot(end ).labelshort = 'Coherency';
splot(end ).acronym = 'cCoh';
splot(end ).unit = 'cCOH';
splot(end ).cortex = cortexFlag;
splot(end ).matrix = 1;
splot(end ).psd = -1;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'iCOH')
splot(end+1).label = 'ROI to ROI absolute value of the imaginary part of coherency';
splot(end ).labelshort = 'Img. part of Coherency';
splot(end ).acronym = 'iCOH';
splot(end ).unit = 'iCOH';
splot(end ).cortex = cortexFlag;
splot(end ).matrix = 1;
splot(end ).psd = -1;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'GC')
splot(end+1).label = 'ROI to ROI granger causality';
splot(end ).labelshort = 'Granger Causality';
splot(end ).acronym = 'GC';
splot(end ).unit = 'GC';
splot(end ).cortex = cortexFlag;
splot(end ).matrix = 1;
splot(end ).psd = -1;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'TRGC')
splot(end+1).label = 'ROI to ROI time-reversed granger causality';
splot(end ).labelshort = 'Time-rev. Granger Causality';
splot(end ).acronym = 'TRGC';
splot(end ).unit = 'TRGC'; % not used yet
splot(end ).cortex = cortexFlag;
splot(end ).matrix = 1;
splot(end ).psd = -1;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'MIC')
splot(end+1).label = 'ROI to ROI Maximized Imag. Coh.';
splot(end ).labelshort = 'Maximized Imag. Coh.';
splot(end ).acronym = 'MIC';
splot(end ).unit = 'MIC'; % not used yet
splot(end ).cortex = cortexFlag;
splot(end ).matrix = -1;
splot(end ).psd = 0;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'MIM')
splot(end+1).label = 'ROI to ROI Multivariate Interaction Measure';
splot(end ).labelshort = 'Multivariate Interaction Measure';
splot(end ).acronym = 'MIM';
splot(end ).unit = 'MIM'; % not used yet
splot(end ).cortex = cortexFlag;
splot(end ).matrix = 1;
splot(end ).psd = 0;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'PAC')
splot(end+1).label = 'ROI to ROI Phase-amplitude coupling';
splot(end ).labelshort = 'Phase-amplitude coupling';
splot(end ).acronym = 'PAC'; % PAC based on bicoherence
splot(end ).unit = 'PAC'; % not used yet
splot(end ).cortex = cortexFlag;
splot(end ).matrix = 1;
splot(end ).psd = 0;
splot(end ).plot3d = plot3dFlag;
end
if isfield(EEG.roi, 'PAC')
splot(end+1).label = 'ROI to ROI Phase-amplitude coupling';
splot(end ).labelshort = 'Phase-amplitude coupling';
splot(end ).acronym = 'PAC_anti'; % PAC based on antisymmetrized bicoherence
splot(end ).unit = 'PAC'; % not used yet
splot(end ).cortex = cortexFlag;
splot(end ).matrix = 1;
splot(end ).psd = 0;
splot(end ).plot3d = plot3dFlag;
end
if nargin < 2
cb_select = [ 'usrdat = get(gcf, ''userdata'');' ...
'usrdat = usrdat(get(findobj(gcf, ''tag'', ''selection''), ''value''));' ...
'fieldTmp = { ''cortex'' ''matrix'' ''psd'' ''plot3d'' };' ...
'for iField = 1:length(fieldTmp),' ...
' if usrdat.(fieldTmp{iField}) == 1,' ...
' set(findobj(gcf, ''tag'', fieldTmp{iField}), ''enable'', ''on'', ''value'', 1);' ...
' elseif usrdat.(fieldTmp{iField}) == 0,' ...
' set(findobj(gcf, ''tag'', fieldTmp{iField}), ''enable'', ''on'', ''value'', 0);' ...
' else,' ...
' set(findobj(gcf, ''tag'', fieldTmp{iField}), ''enable'', ''off'', ''value'', 0);' ...
' end;' ...
'end;' ...
'clear iField fieldTmp usrdat;' ];
fcregions = {'all', 'cingulate', 'prefrontal', 'frontal', 'temporal', 'parietal', 'central', 'occipital'};
plotrow = [1 1];
uigeom = { [1 1] [1 1] [1 0.5 0.2] 1 [1 1] plotrow [1] [3.3 1.5 1.1 1.2] plotrow };
uilist = {{ 'style' 'text' 'string' 'Select a measure to plot' 'fontweight' 'bold'} ...
{ 'style' 'popupmenu' 'string' {splot.label} 'callback' cb_select 'value' 1 'tag' 'selection' } ...
{ 'style' 'text' 'string' 'Only from/to region' } ...
{ 'style' 'popupmenu' 'string' areaNames 'tag' 'seed_region'} ...
{ 'style' 'text' 'string' 'Frequency range in Hz [min max]:'} ...
{ 'style' 'edit' 'string' '' 'tag' 'freqs'} {} ...
{} ...
{ 'style' 'text' 'string' 'Measure to plot' 'fontweight' 'bold' } ...
{ 'style' 'text' 'string' 'Measure parameters' 'fontweight' 'bold' } ...
...
{ 'style' 'checkbox' 'string' '3d static brainmovie' 'tag' 'plot3d' 'value' 1 } ...
{ 'style' 'edit' 'string' '''thresholdper'', 0.2' 'tag' 'plot3dparams' } ...
...
{ 'style' 'checkbox' 'string' 'Connectivity on cortex (requires surface atlas)' 'tag' 'cortex' 'value' 1 } ...
...
{ 'style' 'checkbox' 'string' 'Matrix representation' 'tag' 'matrix' 'enable' 'off' } ...
{ 'style' 'popupmenu' 'string' fcregions 'callback' cb_select 'value' 1 'tag' 'region' } ....
{ 'style' 'checkbox' 'string' 'left' 'tag' 'hemisphere_left' 'value' 1 } ...
{ 'style' 'checkbox' 'string' 'right' 'tag' 'hemisphere_right' 'value' 1 } ...
...
{ 'style' 'checkbox' 'string' 'Power spectral density' 'tag' 'psd' 'enable' 'off' } {} ...
};
[result,~,~,outs] = inputgui('geometry', uigeom, 'uilist', uilist, 'helpcom', 'pophelp(''pop_loadbv'')', ...
'title', 'ROI connectivity', 'userdata', splot, 'eval', cb_select);
if isempty(result), return, end
options = {};
options = { options{:} 'measure' splot(result{1}).acronym };
options = { options{:} 'freqrange' eval( [ '[' outs.freqs ']' ] ) };
options = { options{:} 'plotcortex' fastif(outs.cortex, 'on', 'off') };
options = { options{:} 'plotcortexparams' {} };
options = { options{:} 'plotcortexseedregion' outs.seed_region-1 };
options = { options{:} 'plotmatrix' fastif(outs.matrix, 'on', 'off') };
options = { options{:} 'plotpsd' fastif(outs.psd , 'on', 'off') };
options = { options{:} 'plot3d' fastif(outs.plot3d, 'on', 'off') };
options = { options{:} 'plot3dparams' eval( [ '{' outs.plot3dparams '}' ] ) };
options = { options{:} 'region' fcregions{outs.region} };
% choose which hemisphere to plot
if outs.hemisphere_left == 1 && outs.hemisphere_right == 0
options = { options{:} 'hemisphere' 'left' };
elseif outs.hemisphere_left == 0 && outs.hemisphere_right == 1
options = { options{:} 'hemisphere' 'right' };
else
options = { options{:} 'hemisphere' 'all' };
end
else
options = varargin;
end
% decode input parameters
% -----------------------
g = finputcheck(options, { 'measure' 'string' {splot.acronym} '';
'freqrange' 'real' { } [];
'smooth' 'real' { } 0.35;
'plotcortex' 'string' { 'on' 'off' } 'on';
'plotcortexparams' 'cell' { } {};
'plotcortexseedregion' 'integer' { } [];
'plot3d' 'string' { 'on' 'off' } 'off';
'plot3dparams' 'cell' { } {};
'plotmatrix' 'string' { 'on' 'off' } 'off';
'noplot' 'string' { 'on' 'off' } 'off';
'plotbarplot' 'string' { 'on' 'off'} 'off';
'hemisphere' 'string' {'all' 'left' 'right'} 'all';
'grouphemispheres' 'string' { 'on' 'off'} 'off';
'region' 'string' { 'all', 'cingulate', 'prefrontal', 'frontal', 'temporal', 'parietal', 'central', 'occipital' } 'all';
'largeplot', 'string' { 'on' 'off' } 'off';
'plotpsd', 'string' { 'on' 'off' } 'off' }, 'pop_roi_connectplot');
if ischar(g), error(g); end
if isequal(g.plotcortexseedregion, 0)
g.plotcortexseedregion = [];
end
S = EEG.roi;
if isempty(g.measure)
error('You must define a measure to plot');
end
if isfield(S, 'roi_selection')
if ~isempty(S.roi_selection)
warning("Plotting options ('region', 'hemisphere', 'grouphemispheres') are disabled when ROIs were explicitely selected.")
g.region = 'all';
g.hemisphere = 'all';
g.grouphemispheres = 'all';
end
end
% colormap
load cm17;
load cm18;
% replace low-resolution with high-resolution cortex
load cortex;
% extract frequency indices
if ~isempty(g.freqrange)
if length(g.freqrange) == 1
frq_inds = find(S.freqs == g.freqrange(1));
titleStr = sprintf('%1.1f Hz', g.freqrange(1));
else
frq_inds = find(S.freqs >= g.freqrange(1) & S.freqs < g.freqrange(2));
titleStr = sprintf('%1.1f-%1.1f Hz frequency band', g.freqrange(1), g.freqrange(2));
end
else
frq_inds = 1:length(S.freqs);
titleStr = 'broadband';
end
% plotting options
allMeasures = { splot.acronym };
pos = strmatch( g.measure, allMeasures, 'exact');
plotOpt = splot(pos);
% either plot large plot with MIM, TRGC and power or only individual plots
if strcmpi(g.largeplot, 'on')
source_roi_power_norm_dB = 10*log10( mean(EEG.roi.source_roi_power(frq_inds,:)) ); % roipsd
% TRGCnet = S.TRGC;
% TRGCnet = TRGCnet - permute(TRGCnet, [1 3 2]);
% TRGCnet = TRGCnet(:,:);
TRGC_matrix = squeeze(mean(S.TRGC(frq_inds, :, :)));
MIM_matrix = squeeze(mean(S.MIM(frq_inds, :, :)));
roi_largeplot(EEG, MIM_matrix, TRGC_matrix, source_roi_power_norm_dB, titleStr)
else
matrix = [];
switch lower(g.measure)
case { 'psd' 'roipsd' }
if strcmpi(g.measure, 'psd')
% plot poower of individual voxels
% we would need to save the power in roi_activity. The function below can plot power
% allplots_cortex_BS(S.cortex, P_dB, [min(P_dB) max(P_dB)], cm17a, 'power [dB]', g.smooth);
error('This option is obsolete');
end
if strcmpi(g.plotcortex, 'on') && strcmpi(lower(g.measure), 'roipsd')
cortexPlot = 10*log10( mean(EEG.roi.source_roi_power(frq_inds,:)) );
end
if strcmpi(g.plotbarplot, 'on') && ~strcmpi(g.noplot, 'on')
source_roi_power_norm_dB = 10*log10( mean(EEG.roi.source_roi_power(frq_inds,:)) );
roi_plotpower(EEG, source_roi_power_norm_dB, titleStr);
end
case { 'trgc' 'gc' }
% calculation of net TRGC scores (i->j minus j->i), recommended procedure
% new way to compute net scores
if strcmpi(g.measure, 'GC')
% TRGCnet = S.GC(:, :, 1) - S.GC(:, :, 2);
TRGC = S.GC;
else
% TRGCnet = S.TRGC(:, :, 1) - S.TRGC(:, :, 2);
TRGC = S.TRGC;
end
matrix = squeeze(mean(TRGC(frq_inds, :, :)));
cortexPlot = mean(matrix, 2);
cortexTitle = [ upper(g.measure) ' (' titleStr '); Red = net sender; Blue = net receiver' ];
case { 'mim' 'mic' }
if strcmpi(g.measure, 'MIC')
% MI = S.MIC(:, :);
MI = S.MIC;
else
% MI = S.MIM(:, :);
MI = S.MIM;
end
matrix = squeeze(mean(MI(frq_inds, :, :),1));
cortexPlot = mean(matrix, 2);
case { 'acoh' 'ccoh' 'icoh'}
if strcmpi(g.measure, 'aCOH')
matrix = squeeze(mean(S.aCOH(frq_inds, :, :), 1));
elseif strcmpi(g.measure, 'cCOH')
error(['Complex values are not supported. To plot the absolute values, compute "aCOH", ' ...
'to plot the imaginary part, compute "iCOH".'])
else
matrix = squeeze(mean(S.iCOH(frq_inds, :, :), 1));
end
cortexPlot = mean(matrix, 2);
case { 'crossspecpow' 'coh' 'crossspecimag' }
if strcmpi(g.measure, 'coh')
PS = abs(S.COH); % do not know what to do here
PSarea2area = squeeze(mean(PS(frq_inds, :, :)));
cortexPlot = mean(PSarea2area, 2);
elseif strcmpi(g.measure, 'crossspecimag')
PS = abs(imag(cs2coh(S.CS)));
PSarea2area = squeeze(mean(PS(frq_inds, :, :)));
cortexPlot = mean(PSarea2area, 2);
else
PS = cs2psd(S.CS);
apow = squeeze(sum(sum(reshape(PS(frq_inds, :), [], S.nROI), 1), 2)).*S.source_roi_power_norm';
cortexPlot = 10*log10(apow);
PSarea2area = [];
end
plotPSDFreq = S.freqs(frq_inds);
plotPSD = PS(frq_inds, :);
matrix = PSarea2area;
case {'pac'}
if isfield(S.PAC, 'b_orig_norm')
matrix = S.PAC.b_orig_norm;
elseif isfield(S.PAC, 'b_orig')
matrix = S.PAC.b_orig;
else
error('PAC (original bicoherence) cannot be plotted, field is missing.')
end
cortexPlot = mean(matrix, 2);
case {'pac_anti'}
if isfield(S.PAC, 'b_anti_norm')
matrix = S.PAC.b_anti_norm;
elseif isfield(S.PAC, 'b_anti')
matrix = S.PAC.b_anti;
else
error('PAC (antisymmetrized bicoherence) cannot be plotted, field is missing.')
end
cortexPlot = mean(matrix, 2);
end
% get seed
if ~isempty(g.plotcortexseedregion)
[coordinate, seed_idx] = get_seedregion_coordinate(EEG.roi.atlas.Scouts, g.plotcortexseedregion, EEG.roi.cortex.Vertices);
seedMask = zeros(size(matrix));
seedMask(seed_idx, :) = 1;
seedMask(:,seed_idx) = 1;
else
seedMask = ones(size(matrix));
end
% frequency plot
if strcmpi(g.plotpsd, 'on')
figure; semilogy(plotPSDFreq, plotPSD); grid on
h = textsc(plotOpt.label, 'title');
set(h, 'fontsize', 20);
end
% plot on matrix
if strcmpi(g.plotmatrix, 'on') && ~isempty(matrix)
matrix = matrix.*seedMask;
roi_plotcoloredlobes(EEG, matrix, titleStr, g.measure, g.hemisphere, g.grouphemispheres, g.region);
% try
% roi_plotcoloredlobes(EEG, matrix, titleStr, g.measure, g.hemisphere, g.grouphemispheres, g.region);
% catch
% warning('Functionalities only available for the Desikan-Killiany atlas (68 ROIs).')
% figure; imagesc(matrix);
% end
end
% plot on cortical surface
if strcmpi(g.plotcortex, 'on') && cortexFlag ~= -1
cortexTitle = [ plotOpt.labelshort ' (' titleStr ')' ];
if isempty(g.plotcortexseedregion)
allplots_cortex_BS(cortex_highres, cortexPlot, [min(cortexPlot) max(cortexPlot)], cm17a, upper(g.measure), g.smooth);
% allplots_cortex_BS(cortex_highres, cortexPlot, [min(cortexPlot) max(cortexPlot)], cm17a, upper(splot.unit), g.smooth);
% allplots_cortex_BS(S.cortex, cortexPlot, [min(cortexPlot) max(cortexPlot)], cm17a, plotOpt.unit, g.smooth);
else
cortexTitle = [ cortexTitle ' for area ' int2str(seed_idx)];
cortexPlot = squeeze(matrix(seed_idx,:));
allplots_cortex_BS(S.cortex, cortexPlot, [min(cortexPlot) max(cortexPlot)], cm17a, upper(g.measure), g.smooth, [], {coordinate});
% allplots_cortex_BS(cortex_highres, cortexPlot, [min(cortexPlot) max(cortexPlot)], cm17a, upper(g.measure), g.smooth, [], {coordinate});
% allplots_cortex_BS(S.cortex, cortexPlot, [min(cortexPlot) max(cortexPlot)], cm17a, plotOpt.unit, g.smooth, [], {coordinate});
end
h = textsc(cortexTitle, 'title');
set(h, 'fontsize', 20);
elseif strcmpi(g.plotcortex, 'on') && cortexFlag == -1
warning('EEG.roi.cortex does not contain the field "Faces" required to plot surface topographies.')
end
% plot 3D
if strcmpi(g.plot3d, 'on') && ~isempty(matrix)
PSarea2area = matrix.*seedMask;
roi_plotbrainmovie(matrix, 'cortex', EEG.roi.cortex, 'atlas', EEG.roi.atlas, g.plot3dparams{:});
end
end
if nargin < 2
com = sprintf('pop_roi_connectplot(EEG, %s);', vararg2str( options ));
end
end
function [coordinate, seed_idx] = get_seedregion_coordinate(scouts, seed_idx, vc)
% determine voxel of selected seed region, if needed
% assign region index to selected seed region (passed as index)
if ~isempty(seed_idx)
% ball not visible for these regions when plotting the mean voxel
manual_region_idxs = [2, 16, 18, 25, 26, 31, 32, 45, 49, 50, 55, 56, 59, 60, 61, 64];
pos_idx = scouts(seed_idx).Vertices;
pos = vc(pos_idx,:);
if seed_idx == 1
coordinate = vc(736,:);
elseif ismember(seed_idx, manual_region_idxs)
pos_sorted = sortrows(pos, 3, 'descend'); % sort by descending Z-coordinate
coordinate = pos_sorted(1,:);
else
mid_point = mean(pos,1);
[~,closest_pos_idx] = min(eucl(mid_point, pos)); % determine mean voxel
coordinate = pos(closest_pos_idx,:);
end
else
error('Selected region not in cortex')
end
end
function labels = get_labels(EEG)
% retrieve labels from atlas
labels = strings(1,length(EEG.roi.atlas.Scouts));
for i = 1:length(labels)
scout = struct2cell(EEG.roi.atlas.Scouts(i));
labels(i) = char(scout(1));
end
labels = cellstr(labels);
% remove region labels that were not selected
if isfield(EEG.roi, 'roi_selection')
if ~isempty(EEG.roi.roi_selection)
labels = labels(cell2mat(EEG.roi.roi_selection));
end
end
end
function new_labels = replace_underscores(labels)
% remove underscores in label names to avoid bug
new_labels = strrep(labels, '_', ' ');
end
function [colors, color_idxx, roi_idxx, labels_sorted, roi_loc] = get_colored_labels(EEG)
labels = get_labels(EEG);
colors = {[0,0,0]/255, [163, 107, 64]/255, [171, 163, 71]/255, [217, 37, 88]/255, [113, 15, 82]/255,[35, 103, 81]/255,[2, 45, 126]/255,};
% assign labels to colors
roi_loc ={'LT';'RT';'LL';'RL';'LF';'RF';'LO';'RO';'LT';'RT';'LPF';'RPF';'LT';'RT';'LP';'RP';'LT';'RT';'LT';'RT';'LL';'RL';'LO';'RO';'LPF';'RPF';'LO';'RO';'LPF';'RPF';'LT';'RT';'LC';'RC';'LT';'RT';'LF';'RF';'LPF';'RPF';'LF';'RF';'LO';'RO';'LC';'RC';'LL';'RL';'LC';'RC';'LP';'RP';'LL';'RL';'LF';'RF';'LF';'RF';'LP';'RP';'LT';'RT';'LP';'RP';'LT';'RT';'LT';'RT'};
roi_loc = string(roi_loc);
roi_loc = strrep(roi_loc, 'PF', '2');
roi_loc = strrep(roi_loc, 'F', '3');
roi_loc = strrep(roi_loc, 'T', '4');
roi_loc = strrep(roi_loc, 'P', '5');
roi_loc = strrep(roi_loc, 'C', '6');
roi_loc = strrep(roi_loc, 'O', '7');
roi_loc = strrep(roi_loc, 'LL', 'L1');
roi_loc = strrep(roi_loc, 'RL', 'R1');
roi_loc = strrep(roi_loc, 'L', '');
roi_loc = strrep(roi_loc, 'R', '');
% remove regions that were not selected
if isfield(EEG.roi, 'roi_selection')
if ~isempty(EEG.roi.roi_selection)
roi_loc = roi_loc(cell2mat(EEG.roi.roi_selection));
end
end
try
[color_idxx,roi_idxx] = sort(str2double(roi_loc));
labels_sorted = labels(roi_idxx);
catch
roi_idxx = 1:length(labels);
color_idxx = mod(roi_idxx, length(colors))+1;
labels_sorted = labels;
end
end
function roi_plotpower(EEG, source_roi_power_norm_dB, titleStr)
[colors, color_idxx, roi_idxx, labels_sorted, ~] = get_colored_labels(EEG);
n_roi_labels = size(labels_sorted, 2);
barh(source_roi_power_norm_dB(roi_idxx));
set(gca, 'YDir', 'reverse');
set(gca,'ytick',[1:n_roi_labels],'yticklabel',labels_sorted(1:end), 'fontweight','bold','fontsize', 9, 'TickLength',[0.015, 0.02], 'LineWidth',0.7);
h = title([ 'ROI source power' ' (' titleStr ')' ]);
set(h, 'fontsize', 16);
ylabel('power [dB]')
ax = gca;
for i=1:numel(roi_idxx)
ax.YTickLabel{ceil(i)} = sprintf('\\color[rgb]{%f,%f,%f}%s', colors{color_idxx(i)}, ax.YTickLabel{ceil(i)});
end
pos = get(gcf, 'Position');
set(gcf, 'Position', [pos(1) pos(2) pos(3) pos(4)*1.8])
movegui(gcf, 'south') % remove after
end
function roi_plotcoloredlobes( EEG, matrix, titleStr, measure, hemisphere, grouphems, region)
% check if Desikan-Killiany atlas is used
if EEG.roi.nROI == 68
isDKatlas = true;
else
isDKatlas = false;
end
if ~strcmpi(region, 'all') && isDKatlas == 0
error('Region plotting is only supported for the Desikan-Killiany atlas.');
end
% plot matrix with colored labels
load cm18
switch lower(measure)
case {'mim', 'mic', 'coh'}
cmap = cm18a;
otherwise
cmap = cm18;
end
clim_min = min(matrix, [], 'all');
clim_max = max(matrix, [], 'all');
% hemisphere parameters to determine which labels to use
last_char = EEG.roi.atlas.Scouts(1).Label(end);
if strcmpi(hemisphere, 'left')
if strcmpi(last_char, 'R')
hem_idx = {2 2 2}; % use labels 2:2:end (first two values), only use 1/2 of the labels (3rd value)
else
hem_idx = {1 2 2}; % use labels 1:2:end (first two values), only use 1/2 of the labels (3rd value)
end
elseif strcmpi(hemisphere, 'right')
if strcmpi(last_char, 'L')
hem_idx = {1 2 2};
else
hem_idx = {2 2 2};
end
else
hem_idx = {1 1 1};
end
% sort matrix according to color scheme
% reduce matrix to only keep components corresponding to selected region
if isDKatlas == 1
[colors, color_idxx, roi_idxx, labels_sorted, roi_loc] = get_colored_labels(EEG);
labels = labels_sorted;
% assign region input to an index
[GC, ~] = groupcounts(roi_loc);
switch lower(region)
case 'cingulate'
region_idx = 1;
case 'prefrontal'
region_idx = 2;
case 'frontal'
region_idx = 3;
case 'temporal'
region_idx = 4;
case 'parietal'
region_idx = 5;
case 'central'
region_idx = 6;
case 'occipital'
region_idx = 7;
otherwise
region_idx = 99;
end
matrix = matrix(roi_idxx, roi_idxx);
if not(region_idx == 99)
if region_idx == 1
start_idx = 1;
else
start_idx = 1 + sum(GC(1:region_idx-1));
end
end_idx = start_idx + GC(region_idx) - 1;
matrix = matrix(start_idx:end_idx, start_idx:end_idx);
labels = labels(start_idx:end_idx);
color_idxx = color_idxx(start_idx:end_idx);
end
else
labels = get_labels(EEG);
end
n_roi_labels = size(matrix, 1);
% remove underscores in labels to avoid plotting bug
labels = replace_underscores(labels);
% create dummy plot and add custom legend
f = figure();
%f.WindowState = 'maximized';
hold on
n_dummy_labels = 7;
x = 1:10;
% labels on dummy plot for positioning
xlim([0 n_roi_labels])
ylim([0 n_roi_labels])
set(gca,'xtick',1:n_roi_labels,'xticklabel',labels(hem_idx{1}:hem_idx{2}:n_roi_labels));%, 'TickLabelInterpreter','none');
ax = gca;
if isDKatlas == 1
for k=1:n_dummy_labels
plot(x, x*k, '-', 'LineWidth', 9, 'Color', colors{k});
end
for i=hem_idx{1}:hem_idx{2}:n_roi_labels
ax.XTickLabel{ceil(i/hem_idx{3})} = sprintf('\\color[rgb]{%f,%f,%f}%s', colors{color_idxx(i)}, ax.XTickLabel{ceil(i/2)});
end
legend('Cingulate', 'Prefrontal', 'Frontal', 'Temporal', 'Parietal', 'Central', 'Occipital', 'Location', 'southeastoutside'); % modify legend position
end
xtickangle(90)
pos = get(gca, 'Position');
set(gca, 'Position', pos, 'DataAspectRatio',[1 1 1], 'visible', 'off')
axes('pos', [pos(1) pos(2) pos(3) pos(4)]) % plot matrix over the dummy plot and keep the legend
% group by hemispheres (left first, then right)
if strcmp(grouphems, 'on')
% sort matrix
mat_left_row = matrix(1:2:end,:);
mat_right_row = matrix(2:2:end,:);
matrix = [mat_left_row; mat_right_row]; % sort rows
mat_left_col = matrix(:,1:2:end);
mat_right_col = matrix(:,2:2:end);
matrix = horzcat(mat_left_col, mat_right_col); % sort columns
% sort labels
try % if color can be assigned
lc = {labels; transpose(color_idxx)};
for i = 1:length(lc)
left = lc{i}(1:2:end);
right = lc{i}(2:2:end);
lc{i} = [left right];
end
labels = lc{1};
color_idxx = transpose(lc{2});
catch
left = labels(1:2:end);
right = labels(2:2:end);
labels = [left right];
end
end
% reduce matrix to keep only one hemisphere
if strcmp(hemisphere, 'left')
matrix = matrix(hem_idx{1}:hem_idx{2}:n_roi_labels,:);
matrix(:,2:2:end) = [];
imagesc(matrix); colormap(cmap);
elseif strcmp(hemisphere, 'right')
matrix = matrix(hem_idx{1}:hem_idx{2}:n_roi_labels,:);
matrix(:,1:2:end) = [];
imagesc(matrix); colormap(cmap);
else
imagesc(matrix); colormap(cmap);
end
cb = colorbar;
% tf = isMATLABReleaseOlderThan("R2022a");
% if tf
% caxis([clim_min clim_max])
% else
% clim([clim_min clim_max])
% end
try
caxis([clim_min clim_max])
catch
clim([clim_min clim_max])
end
if isDKatlas == 1
set(cb, 'Location', 'southoutside')
else
set(cb, 'Location', 'eastoutside')
end
set(gca, 'Position', pos, 'DataAspectRatio',[1 1 1], 'visible', 'on')
% add colored labels with display option
ax = gca;
set(gca,'xtick',1:n_roi_labels,'xticklabel',labels(hem_idx{1}:hem_idx{2}:n_roi_labels));
if isDKatlas == 1
set(gca,'ytick',1:n_roi_labels,'yticklabel',labels(hem_idx{1}:hem_idx{2}:n_roi_labels), 'fontsize', 9, 'TickLength',[0.015, 0.02], 'LineWidth',0.75);
for i=hem_idx{1}:hem_idx{2}:n_roi_labels
ax.XTickLabel{ceil(i/hem_idx{3})} = sprintf('\\color[rgb]{%f,%f,%f}%s', colors{color_idxx(i)}, ax.XTickLabel{ceil(i/hem_idx{3})});
ax.YTickLabel{ceil(i/hem_idx{3})} = sprintf('\\color[rgb]{%f,%f,%f}%s', colors{color_idxx(i)}, ax.YTickLabel{ceil(i/hem_idx{3})});
end
else
set(gca,'ytick',1:n_roi_labels,'yticklabel',labels(hem_idx{1}:hem_idx{2}:n_roi_labels), 'fontsize', 7, 'TickLength',[0.015, 0.02], 'LineWidth',0.75);
end
h = title([ 'ROI to ROI ' upper(replace_underscores(measure)) ' (' titleStr ')' ]);
set(h, 'fontsize', 16);
xtickangle(90)
end
function roi_largeplot(EEG, mim, trgc, roipsd, titleStr)
% plot MIM, TRGC and power (barplot) in a single large figure
load cm18
[colors, color_idxx, roi_idxx, labels_sorted, ~] = get_colored_labels(EEG);
n_roi_labels = size(labels_sorted, 2);
f = figure();
f.WindowState = 'maximized';
fc_matrices = cell(1,2);
fc_matrices{1,1} = mim;
fc_matrices{1,2} = trgc;
fc_names = ["MIM", "TRGC"];
for k = 1:2
plt(k) = subplot(1,3,k);
fc = fc_matrices{k};
img = squeeze(fc)';
img_sorted = img(roi_idxx, roi_idxx);
imagesc(img_sorted)
set(gca,'ytick',[1:n_roi_labels],'yticklabel',labels_sorted(1:end), 'fontsize', 5, 'TickLength',[0.015, 0.02], 'LineWidth',0.75);
set(gca,'xtick',[1:n_roi_labels],'xticklabel',labels_sorted(1:end));
h = title([ 'ROI to ROI ' fc_names{k} ' (' titleStr ')' ]);
set(h, 'fontsize', 16);
hcb = colorbar;
hcb.Label.FontSize = 10;
set(gca,'DataAspectRatio',[1 1 1])
xtickangle(90)
ax = gca;
for i=1:numel(roi_idxx)
ax.XTickLabel{ceil(i)} = sprintf('\\color[rgb]{%f,%f,%f}%s', colors{color_idxx(i)}, ax.XTickLabel{ceil(i)});
ax.YTickLabel{ceil(i)} = sprintf('\\color[rgb]{%f,%f,%f}%s', colors{color_idxx(i)}, ax.YTickLabel{ceil(i)});
end
end
colormap(plt(1), cm18a)
colormap(plt(2), cm18)
% power
subplot(1,3,3);
barh(roipsd(roi_idxx));
set(gca, 'YDir', 'reverse');
set(gca,'ytick',[1:n_roi_labels],'yticklabel',labels_sorted(1:end), 'fontsize', 9, 'TickLength',[0.015, 0.02], 'LineWidth',0.7);
h = title([ 'ROI source power' ' (' titleStr ')' ]);
set(h, 'fontsize', 16);
ylabel('power [dB]')
ax = gca;
for i=1:numel(roi_idxx)
ax.YTickLabel{ceil(i)} = sprintf('\\color[rgb]{%f,%f,%f}%s', colors{color_idxx(i)}, ax.YTickLabel{ceil(i)});
end
% invisible plot to add legend
hold on
n_labels = 7;
h = zeros(n_labels, 1);
for k=1:numel(h)
h(k) = plot(NaN, NaN, '-', 'LineWidth', 8, 'Color', colors{k});
end
if n_roi_labels == 68
lgd = legend(h, 'Cingulate', 'Prefrontal', 'Frontal', 'Temporal', 'Parietal', 'Central', 'Occipital');
lgd.FontSize = 10;
set(lgd, 'Position', [0.44 0.06 0.25 0.25]);
end
end