-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze_etkinlab_sessioneffects.m
1155 lines (1000 loc) · 43.3 KB
/
analyze_etkinlab_sessioneffects.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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function results = analyze_etkinlab_sessioneffects(varargin)
% Compute and return conditional correlations, nuisance correlations, and NSR on all subjects x sites.
% import ggmClass/standardize.*
% import ggmClass/covariance.*
addpath(fullfile(getenv('HOME'),'MATLAB','ggmClass'));
% % DANA healthy controls
% tmpdata = load(['../prelim2016-resting-predicts-tms/' ...
% 'tmp_workspace_1.mat'],'obj');
% tmpnames = regexp(tmpdata.obj.subjlabels,'_','split');
% for ii=1:length(tmpnames)
% tmpdata.obj.subjlabels{ii} = tmpnames{ii}{2};
% end
studyname = 'best-eegfmri-cpac';
verbose = false;
if(nargin==0)
plotOnly = false;
elseif(nargin>=1)
plotOnly = varargin{1};
end
if(exist('brewermap'))
colormapfun = @()(flipud(brewermap(length(colormap),'RdYlBu')));
close all;
else
colormapfun = @winter;
end
% test re-test mccc
exportfun = @(filename)(print('-dpng','-r150',filename));
%fname = ['tmp' filesep datestr(now,'dd-mmm-yyyy')];
fname = ['tmp' filesep '15-Oct-2018'];
if(~exist(fname,'dir'))
mkdir(fname)
end
fname = [fname filesep studyname];
if(~exist(fname,'dir'))
mkdir(fname)
end
nmethods = 1;
methodnames = {'sn_denoised'};
switch studyname
case 'pnas'
datadir = fullfile(getenv('HOME'),'MATLAB','kggm2016-paper','data');
studydata.cpac = load(fullfile(datadir,'dataset_pnas_rfmri'));
subjlia = cellfun(@(x)(~isempty(x)),studydata.cpac.confounds);
include_subjects = zeros(1,length(subjlia));
subject_idx = find(subjlia);
fd_thresh = .2;
for subjno=1:length(subject_idx)
subject_no = subject_idx(subjno);
tmpconfounds = studydata.cpac.confounds{1};
sessionno1 = ...
find(strcmp(studydata.cpac.tms{subject_no},'rest'));
sessionno2 = ...
find(strcmp(studydata.cpac.tms{subject_no},'rest1Hzpre'));
if(...
sqrt(mean((...
tmpconfounds{sessionno1}.framewise_displacement).^2))<fd_thresh ...
| ...
sqrt(mean((...
tmpconfounds{sessionno2}.framewise_displacement).^2))<fd_thresh ...
)
include_subjects(subject_no) = 1;
end
end
if(verbose)
disp('Checking Motion Inclusion')
sum(subjlia)
sum(subjlia & include_subjects)
end
studydata.cpac.confounds = studydata.cpac.confounds(...
subjlia & include_subjects);
studydata.cpac.X = studydata.cpac.X(subjlia & include_subjects);
studydata.cpac.subjects = studydata.cpac.subjects(...
subjlia & include_subjects);
studydata.cpac.files = studydata.cpac.files(subjlia & include_subjects);
studydata.session1.compcor = [];
studydata.session2.compcor = [];
studydata.session1.motion = [];
studydata.session2.motion = [];
nsubjects = length(studydata.cpac.X)
for ii=1:nsubjects
try
sessionno = find(strcmp(studydata.cpac.tms{ii},'rest'));
studydata.session1.X{ii} = studydata.cpac.X{ii}(:,:,sessionno);
% studydata.session1.compcor(ii,:,:) = studydata.cpac.confounds{ii}{sessionno}.compcor;
% studydata.session1.motion(ii,:,:) = studydata.cpac.confounds{ii}{sessionno}.motion;
sessionno = find(strcmp(studydata.cpac.tms{ii},'rest1Hzpre'));
studydata.session2.X{ii} = studydata.cpac.X{ii}(:,:,sessionno);
% studydata.session2.compcor(ii,:,:) = studydata.cpac.confounds{ii}{sessionno}.compcor;
% studydata.session2.motion(ii,:,:) = studydata.cpac.confounds{ii}{sessionno}.motion;
catch me
studydata.cpac.subjects{ii}
disp(me)
end
end
case 'corr-bnu1'
datadir = fullfile(getenv('HOME'),'MATLAB','rcubed','data');
pipelinename = 'pipeline_global00';
atlasname = 'CC200';
fname = [fname filesep pipelinename];
if(~exist(fname,'dir'))
mkdir(fname)
end
fname = [fname filesep atlasname];
if(~exist(fname,'dir'))
mkdir(fname)
end
studydata.cpac = load(fullfile(datadir,...
['corr-bnu1_task-rest_' ...
pipelinename '_' atlasname ] ...
));
%subjlia = cellfun(@(x)(~isempty(x)),studydata.cpac.confounds);
subjlia = studydata.cpac.trt_idx;
studydata.cpac.confounds = studydata.cpac.confounds(subjlia);
studydata.cpac.X = studydata.cpac.X(subjlia);
studydata.cpac.subjects = studydata.cpac.subjects(subjlia);
studydata.cpac.files = studydata.cpac.subjects;
nsubjects = length(studydata.cpac.X)
for ii=1:nsubjects
ii
sessionno = find(strcmp(studydata.cpac.sessions,'ses-1'));
studydata.session1.X{ii} = studydata.cpac.X{ii}{sessionno};
sessionno = find(strcmp(studydata.cpac.sessions,'ses-2'));
studydata.session2.X{ii} = studydata.cpac.X{ii}{sessionno};
end
case 'best-eegfmri-cpac'
clear studydata
pipeline = 'etkinlab_analysis';
resttype = '';
n_time = 840;
fname = [fname '-' resttype 'trt-tp1.1-tp2.2' ['-t' num2str(n_time)] ];
if(~exist(fname))
mkdir(fname)
end
fname = fullfile(fname,pipeline);
if(~exist(fname))
mkdir(fname)
end
datadir = fullfile(getenv('HOME'),'MATLAB','kggm2016-paper','data');
studydata = ...
load(fullfile(datadir,'dataset_SchaeferYeo100_best_rfmri_2'));
session1_prefix = ['ses-tp1' 'run-1'];
session1_idx = 1:length(studydata.files)
studydata.session1.subjects = studydata.files
session2_prefix = ['ses-tp2' 'run-2'];
session2_idx = 1:length(studydata.files)
studydata.session2.subjects = studydata.files;
all_subnames = ...
cellfun(@num2str,num2cell(1:17),'UniformOutput',false);
for ii=1:length(all_subnames)
if(ii<=length(studydata.X) && ...
~isempty(studydata.X{ii}{1}))
studydata.session1.X{ii} = ...
squeeze(studydata.X{ii}{1}(10:850,1:100));
else
studydata.session1.X{ii} = [];
studydata.session1.subjects{ii} = [];
end
if(ii<=length(studydata.X) && length(studydata.X{ii})>2 && ...
~isempty(studydata.X{ii}{4}))
studydata.session2.X{ii} = ...
squeeze(studydata.X{ii}{4}(10:850,1:100));
else
studydata.session2.X{ii} = [];
studydata.session2.subjects{ii} = [];
end
tmp_subj_no = [];
end
both_subjects_idx = ...
~(cellfun(@isempty,studydata.session2.subjects) | ...
cellfun(@isempty,studydata.session1.subjects));
studydata.session1.X = studydata.session1.X(both_subjects_idx);
studydata.session2.X = studydata.session2.X(both_subjects_idx);
nsubjects = sum(both_subjects_idx)
studydata.roilabels.table = readtable('/Volumes/MACBOOKUSB/Datasets/Schaefer2018_LocalGlobal/Parcellations/MNI/Schaefer2018_100Parcels_7Networks_order.txt','Delimiter','\t','ReadVariableNames',0);
studydata.subjects.labels = all_subnames;
studydata.subjects.index = both_subjects_idx;
save([fname filesep 'data_' pipeline '_' ...
studyname '_rfmri.mat'],'studydata');
case 'best-eegfmri'
pipeline = 'etkinlab_analysis';
resttype = '';
n_time = 820;
fname = [fname '-' resttype '' ['-t' num2str(n_time)] ];
if(~exist(fname))
mkdir(fname)
end
fname = fullfile(fname,pipeline);
if(~exist(fname))
mkdir(fname)
end
datadir = fullfile(getenv('HOME'),'Downloads');
tmpdata = load(fullfile(datadir,'BEST_Schaefer100PlusSubcortical_BoldSignalsHomAndSNR_w1_05282018.mat'));
% tmpdata = load(fullfile(datadir,'BEST_Schaefer100PlusSubcortical_BoldSignalsHomAndSNR_w1_03152018.mat'));
subNames = ...
regexprep(tmpdata.subNames,{'.*best_','.connectivity.*'},{'',''});
session1_prefix = ['tp1_efmri_' 'rest1'];
session1_idx = ...
find(~cellfun(@isempty,strfind(subNames,session1_prefix)));
studydata.session1.subjects = subNames(session1_idx);
session2_prefix = ['tp2_efmri_' 'rest2'];
session2_idx = ...
find(~cellfun(@isempty,strfind(subNames,session2_prefix)));
studydata.session2.subjects = subNames(session2_idx);
all_subnames = ...
cellfun(@num2str,num2cell(3001:3020),'UniformOutput',false);
for ii=1:length(all_subnames)
tmp_subj_no = ...
find(~cellfun(@isempty, ...
strfind(subNames(session1_idx),all_subnames(ii))));
if(~isempty(tmp_subj_no))
studydata.session1.X{ii} = ...
squeeze(tmpdata.signals(session1_idx(tmp_subj_no),1:100,1:820))';
studydata.session1.subjects{ii} = subNames(session1_idx(tmp_subj_no));
else
studydata.session1.X{ii} = [];
studydata.session1.subjects{ii} = [];
end
tmp_subj_no = [];
tmp_subj_no = ...
find(~cellfun(@isempty, ...
strfind(subNames(session2_idx),all_subnames(ii))));
if(~isempty(tmp_subj_no))
studydata.session2.X{ii} = ...
squeeze(tmpdata.signals(session2_idx(tmp_subj_no),1:100,1:820))';
studydata.session2.subjects{ii} = ...
subNames(session2_idx(tmp_subj_no));
else
studydata.session2.X{ii} = [];
studydata.session2.subjects{ii} = [];
end
tmp_subj_no = [];
end
both_subjects_idx = ...
~(cellfun(@isempty,studydata.session2.subjects) | ...
cellfun(@isempty,studydata.session1.subjects));
studydata.session1.X = studydata.session1.X(both_subjects_idx);
studydata.session2.X = studydata.session2.X(both_subjects_idx);
nsubjects = sum(both_subjects_idx)
studydata.session1
studydata.roilabels.table = readtable('/Volumes/MACBOOKUSB/Datasets/Schaefer2018_LocalGlobal/Parcellations/MNI/Schaefer2018_100Parcels_7Networks_order.txt','Delimiter','\t','ReadVariableNames',0);
studydata.subjects.labels = all_subnames;
studydata.subjects.index = both_subjects_idx;
save([fname filesep 'data_' pipeline '_' ...
studyname '_rfmri.mat'],'studydata');
end
if(isfield(studydata,'cpac'))
communityno = 3;
ncommunities = length(unique(studydata.cpac.roilabels.Yeo7));
nodeidx = find(studydata.cpac.roilabels.Yeo7==communityno);
nodeidx = cat(1,nodeidx,find(studydata.cpac.roilabels.Yeo7==4));
nodeidx = cat(1,nodeidx,find(studydata.cpac.roilabels.Yeo7==6));
communitylabel = studydata.cpac.roilabels.Yeo7Label(nodeidx);
if(length(unique(communitylabel))>2)
communitylabel = {'AllNetworks'};
end
communitycolor = studydata.cpac.roilabels.Yeo7(nodeidx);
else
nodeidx = 1:size(studydata.session1.X{1},2);
tmp_community_parts = ...
regexp(studydata.roilabels.table.Var2,{'_'}, 'split');
for ii=1:length(tmp_community_parts)
communitylabels{ii} = tmp_community_parts{ii}{3};
end
communitylabel = grp2idx(communitylabels);
communitycolor = communitylabel;
end
results = struct();
observed_correlation = [];
denoised_correlation = [];
denoised_correlation2 = [];
nuisance_correlation = [];
nsr = [];
if(~plotOnly)
%Y = get_shared_nuisance(squeeze(mean(studydata.signalsSite1,2))');
Y = [];
for sessionno=1:2
for ii=1:nsubjects
if(verbose)
ii
['session' num2str(sessionno)]
end
studydata
X = studydata.(['session' num2str(sessionno)]).X{ii};
X = X(1:n_time,:);
%Y = mean(X,2);
%Y = squeeze(studydata.compcor(ii,:,:));
%Y = cat(2,Y,squeeze(studydata.motion(ii,:,:)));
try
observed_correlation(:,:,ii,sessionno) = ...
standard_correlation(X);
% results.Site1{ii,sessionno} = conditional_correlation(X,Y);
% denoised_correlation(:,:,ii,sessionno) = ...
% results.Site1{ii,sessionno}.corr;
% nuisance_correlation(:,:,ii,sessionno) = ...
% results.Site1{ii,sessionno}.nuisance;
% nsr(ii,sessionno) = results.Site1{ii,sessionno}.NSR;
denoised_correlation2(:,:,ii,sessionno) = ...
standard_correlation_sn(X);
catch me
disp(me)
results.Site1{ii,sessionno} = {};
disp('Session')
sessionno
disp('Subject')
ii
end
% if(ii==1)
% demo_conditional_correlation(X,Y);
% end
end
end
if(isfield(studydata,'subjects'))
subject_labels = ...
studydata.subjects.labels(studydata.subjects.index)
end
% results.denoised2 = denoised_correlation;
% results.nuisance = nuisance_correlation;
% results.nsr = nsr;
results.denoised2 = denoised_correlation2;
results.sn_denoised = denoised_correlation2;
results.observed = observed_correlation;
if(exist('subject_labels'))
results.subject_labels = subject_labels;
end
sitelabels = ones(1,2*nsubjects);
% if(exist('brewermap'))
% colormapfun = @()(flipud(brewermap(length(colormap),'RdYlBu')));
% close all;
% else
% colormapfun = @winter;
% end
%
% % test re-test mccc
% exportfun = @(filename)(print('-dpng','-r150',filename));
% fname = ['tmp' filesep datestr(now,'dd-mmm-yyyy')];
%
% if(~exist(fname,'dir'))
% mkdir(fname)
% end
%
% % upper_idx = find(reshape(triu(ones(p,p),1), [p*p 1]));
% % features = reshape(results.denoised(upper_idx),[p*p nsubjects 2]);
%
% nmethods = 2;
% methodnames = {'observed','sn_denoised'};
for methodno=1:nmethods
methodname = methodnames{methodno};
p = size(results.(methodname),1);
graphs = results.(methodname)(nodeidx,:,:,:);
results.cpac.(methodname).trt_reliability = ...
helper_node_mccc(graphs);
results.cpac.(methodname).trt_reliability.nodeidx = nodeidx;
tmp_kendallW = [];
for nodeno=1:size(graphs,1)
for subjectno=1:nsubjects
tmp_kendallW(nodeno,subjectno) = ...
reliability.kendallsW(...
cat(1,squeeze(graphs(nodeno,:,subjectno,1)),...
squeeze(graphs(nodeno,:,subjectno,2)))' ...
);
end
end
results.cpac.(methodname).trt_reliability.kendallsw = tmp_kendallW;
save([fname filesep 'trt_' studyname '_rfmri.mat'],'results');
tmp_kendallW = [];
if(~exist(fullfile(fname,methodname)))
mkdir(fullfile(fname,methodname));
end
ba_nodes = nodeidx;
trid_idx = find(reshape(triu(ones(p,p),1),[p^2 1]));
all_y_diff = [];
all_x_mean = [];
tmp_propLOA = [];
for subjectno=1:nsubjects
% [altmanplot, kendallcoef, propLOA, y_diff, x_mean] = ...
% plot_bland_altman_agreement(graphs(ba_nodes, ba_nodes,subjectno,1),...
% graphs(ba_nodes, ba_nodes,subjectno,2));
rater1 = graphs(ba_nodes,ba_nodes,subjectno,1);
rater2 = graphs(ba_nodes,ba_nodes,subjectno,2);
rater1 = rater1(trid_idx);
rater2 = rater2(trid_idx);
baStats = reliability.bland_altman_MD(rater1,rater2);
y_diff = baStats.D;
x_mean = baStats.M;
propLOA = nan;
kendallcoef = reliability.kendallsW(cat(2,rater1,rater2));
% savefilename = fullfile(fname, methodname, ...
% ['agreement_subj' num2str(subjectno)]);
% altmanplot.export('file_name', ...
% savefilename,'file_type','pdf');
tmp_kendallW(subjectno) = kendallcoef;
tmp_propLOA(subjectno) = propLOA;
all_y_diff(:,subjectno) = y_diff;
all_x_mean(:,subjectno) = x_mean;
close;
end
loa_matrix = zeros(p^2,3);
for ii=1:size(all_y_diff,1)
bastats = reliability.bland_altman_loa(...
all_x_mean(ii,:)', ...
all_y_diff(ii,:)' ...
);
loa_matrix(trid_idx(ii),1) = bastats.stats.mu;
loa_matrix(trid_idx(ii),2) = bastats.stats.ci(1); loa_matrix(trid_idx(ii),3) = bastats.stats.ci(2);
end
figure;
%scatter(mean(all_x_mean,2),mean(all_y_diff,2))
loa_int = [];
loa_int(1,1) = mean(loa_matrix(trid_idx,2));
loa_int(1,2) = mean(loa_matrix(trid_idx,3));
loa_int(2:3,1) = prctile(loa_matrix(trid_idx,2),[2.5 97.5]);
loa_int(2:3,2) = prctile(loa_matrix(trid_idx,3),[2.5 97.5]);
[altmanplot tmp_propLOA] = ...
plot_bland_altman_agreement(mean(all_x_mean,2), ...
mean(all_y_diff,2), ...
loa_int ...
);
savefilename = fullfile(fname, methodname, ...
['group_edgelevel_mdplot']);
altmanplot.export('file_name', ...
savefilename,'file_type','pdf');
results.cpac.(methodname).trt_reliability.edgeidx = trid_idx;
results.cpac.(methodname).trt_reliability.y_diff = all_y_diff;
results.cpac.(methodname).trt_reliability.x_mean = all_x_mean;
results.cpac.(methodname).trt_reliability.loa_matrix = ...
sparse(loa_matrix);
results.cpac.(methodname).trt_reliability.propLOA = tmp_propLOA;
results.cpac.(methodname).trt_reliability.global_kendallsw = tmp_kendallW;
save([fname filesep 'trt_' studyname '_rfmri.mat'],'results','-append');
end
try
mean(results.cpac.sn_denoised.trt_reliability.kendallsw-...
results.cpac.observed.trt_reliability.kendallsw,2);
catch
end
% Compare Agreement With and Without Successive Normalization
% nboot = 10;
% mccc = zeros(p,1);
% mccc_ci = zeros(p,2);
% mccc_boot = zeros(p,nboot);
% similarity = zeros(2*nsubjects,2*nsubjects);
% for nodeno=1:length(nodeidx)
% jj = nodeidx(nodeno)
% features1 = ...
% reshape(permute(squeeze(results.(methodnames{1})(jj,:,:,:)),...
% [1 3 2]),[p*2 nsubjects]);
% features2 = ...
% reshape(permute(squeeze(results.(methodnames{2})(jj,:,:,:)),...
% [1 3 2]),[p*2 nsubjects]);
% similarity = similarity + cov(cat(2,features1,features2));
% [mccc(jj),~,mccc_ci(jj,:),mccc_boot(jj,:)] = ...
% reliability.mccc(features1(1:p,:),features2(1:p,:));
% mccc(jj)
% mccc_ci(jj,:)
% end
% similarity = similarity./length(nodeidx);
% results.cpac.sn_gm_agreement.mccc = real(mccc);
% results.cpac.sn_gm_agreement.mccc_ci = real(mccc_ci);
% results.cpac.sn_gm_agreement.similarity = similarity;
% results.cpac.sn_gm_agreement.nodeidx = nodeidx;
%
% results = setfield(results,'sn_denoised',[]);
% results = setfield(results,'observed',[]);
% results = setfield(results,'denoised2',[]);
% save([fname filesep 'trt_' studyname '_rfmri.mat'],'results','-append');
else
load([fname filesep 'trt_' studyname '_rfmri.mat'],'results');
if(~isempty(results.observed))
results = setfield(results,'sn_denoised',[]);
results = setfield(results,'observed',[]);
results = setfield(results,'denoised2',[]);
save([fname filesep 'trt_' studyname ...
'_rfmri.mat'],'results','-append');
end
if(isfield(results,'subject_labels'))
subject_labels = ...
results.subject_labels;
end
end
% Plotting code only
figure;
set(gcf,'Position',[50 75 1200 600]);
for methodno=1:nmethods
methodname = methodnames{methodno};
y_methods = results.cpac.(methodname).trt_reliability.mccc;
y_lo = results.cpac.(methodname).trt_reliability.mccc_ci(:,1);
y_hi = results.cpac.(methodname).trt_reliability.mccc_ci(:,2);
grammobj(methodno,1) = plot_reliability_dotplot(y_methods, ...
y_lo, ...
y_hi, ...
methodnames, ...
communitycolor ...
);
switch methodname
case 'sn_denoised'
grammobj(methodno,1).set_title(['ICC ' 'after centering']);
case 'observed'
grammobj(methodno,1).set_title(['ICC ' 'before centering']);
otherwise
grammobj(methodno,1).set_title(['ICC for ' methodname]);
end
end
grammobj.draw();
if(strcmp(studyname,'best-eegfmri')|| strcmp(studyname,'best-eegfmri-cpac'))
savefilename = fullfile(fname, ['sessioneffects_reliability' '_' ...
'before_after_sn']);
else
savefilename = fullfile(fname, ['sessioneffects_reliability' '_' ...
'before_after_sn' '_' communitylabel{1}]);
end
savefig(savefilename);
grammobj.export('file_name', ...
savefilename,'file_type','pdf');
exportfun(savefilename);
figure;
addpath('~/MATLAB/packages/Violinplot-Matlab');
set(gcf,'Position',[25 75 400 600]);
vplotobj = violinplot(results.cpac.('sn_denoised').trt_reliability.mccc);
title('ROI ICC Distribution')
set(gcf,'Color',[.9 .9 .9]);
set(gcf,'Color',[.9 .9 .9]);
set(gca,'fontsize',20)
savefilename = fullfile(fname, ['MCCC_Summary.png']);
export_fig(savefilename,'-transparent');
pause(3)
figure;
addpath('~/MATLAB/packages/Violinplot-Matlab');
set(gcf,'Position',[25 75 400 600]);
vplotobj = violinplot(results.cpac.('sn_denoised').trt_reliability.global_kendallsw);
title('Subjects Kendall (W) Distribution')
set(gcf,'Color',[.9 .9 .9]);
set(gcf,'Color',[.9 .9 .9]);
set(gca,'fontsize',20)
savefilename = fullfile(fname, ['KendallW_Summary.png']);
export_fig(savefilename,'-transparent');
pause(3)
% Plot full mccc node distributions on the same plot
for methodno=1:nmethods
if(nmethods>1)
y_methods = cat(2,...
results.cpac.(methodnames{1}).trt_reliability.mccc,...
results.cpac.(methodnames{2}).trt_reliability.mccc ...
);
% y_methods_lo = cat(2,...
% results.cpac.(methodnames{1}).trt_reliability.mccc_ci(nodeidx,1),...
% results.cpac.(methodnames{2}).trt_reliability.mccc_ci(nodeidx,1) ...
% );
% y_methods_hi = cat(2,...
% results.cpac.(methodnames{1}).trt_reliability.mccc_ci(nodeidx,2),...
% results.cpac.(methodnames{2}).trt_reliability.mccc_ci(nodeidx,2) ...
% );
% y_groups = {};
% y_groups{1} = repmat(methodnames(1),[1 size(y_methods,1)]);
% y_groups{2} = repmat(methodnames(2),[1 size(y_methods,1)]);
%
% Plot cornerhistogram
grammobj = plot_reliability_cornerhist(y_methods, ...
{'Before centering','After centering'}, ...
communitycolor ...
);
figure;
set(gcf,'Position',[25 75 850 750]);
grammobj.draw();
if(strcmp(studyname,'best-eegfmri'))
savefilename = fullfile(fname, ['sessioneffects_reliability' '_' ...
'difference' '_' methodnames{1} '_' methodnames{2}] ...
);
else
savefilename = fullfile(fname, ...
['sessioneffects_reliability' '_' ...
'difference' '_' methodnames{1} '_' methodnames{2} ...
communitylabel{1} ...
] ...
);
end
savefig(savefilename);
grammobj.export('file_name',savefilename,'file_type','pdf');
exportfun(savefilename);
y_methods = cat(2,...
mean(results.cpac.(methodnames{1}).trt_reliability.kendallsw,2),...
mean(results.cpac.(methodnames{2}).trt_reliability.kendallsw,2) ...
);
% Plot cornerhistogram
grammobj = plot_reliability_cornerhist(y_methods, ...
{'Before centering','After centering'}, ...
communitycolor ...
);
figure;
set(gcf,'Position',[25 75 850 750]);
grammobj.draw();
savefilename = fullfile(fname, ...
['sessioneffects_concordance' '_' ...
'difference' '_' methodnames{1} '_' methodnames{2} ...
communitylabel{1} ...
] ...
);
savefig(savefilename);
grammobj.export('file_name',savefilename,'file_type','pdf');
exportfun(savefilename);
else
y_methods = ...
results.cpac.(methodnames{1}).trt_reliability.mccc;
end
end
addpath('~/MATLAB/packages/Violinplot-Matlab');
figure;
set(gcf,'Position',[25 75 1200 650]);
%vplotobj = violinplot(results.cpac.(methodname).trt_reliability.kendallsw');
vplotobj = plot_violinplot(...
results.cpac.(methodname).trt_reliability.kendallsw', ...
{methodname}, ...
communitycolor ...
);
if(iscell(communitylabel))
communitylabelname = communitylabel{1}
else
communitylabelname = ''
end
savefilename = fullfile(fname, ...
['sessioneffects_violinplot' '_' ...
methodname ...
communitylabelname ...
] ...
);
savefig(savefilename);
exportfun(savefilename);
% Export kendallsW with powers parameters into a single table.
reliability_table = table();
methodname = 'sn_denoised';
for subjno=1:nsubjects
if(isfield(studydata,'cpac') && isfield(studydata.cpac,'confounds'))
power_table = studydata.cpac.confounds{subjno}{1}.powers_params(:,...
[1 3 6]);
tmp_table = power_table;
tmp_table.Properties.VariableNames(2:3) = ...
strcat(tmp_table.Properties.VariableNames(2:3),{'_ses1'});
power_table = studydata.cpac.confounds{subjno}{2}.powers_params(:,...
[1 3 6]);
tmp_table2 = power_table;
tmp_table2.Properties.VariableNames(2:3) = ...
strcat(tmp_table2.Properties.VariableNames(2:3),{'_ses2'});
tmp_table = horzcat(tmp_table,tmp_table2(:,2:3));
flag_reliability_vs_motion = 1;
else
tmp_table = table();
try
tmp_table.subject = subjno;
tmp_table.subjectlabel = subject_labels(subjno);
catch me
disp(me)
end
end
tmp_table.kendallw = ...
[results.cpac.(methodname).trt_reliability.kendallsw(:,subjno)'];
reliability_table = vertcat(reliability_table,tmp_table);
flag_reliability_vs_motion = 0;
end
save([fname filesep 'table_kendallw_' methodname ],'reliability_table');
writetable(reliability_table, [fname filesep 'table_kendallw_' methodname '.csv'],'Delimiter','\t');
if(flag_reliability_vs_motion)
% Scatterplot
communityno = 3;
y_methods = cat(2,(reliability_table.rootMeanSquareFD_ses1+ ...
reliability_table.rootMeanSquareFD_ses2)/2, ...
mean(reliability_table.kendallw(:, ...
communitycolor==communityno),2) ...
);
grammobj = plot_reliability_glm(y_methods, ...
{'RMSE FD','Kendall W'}, ...
communityno ...
);
figure;
set(gcf,'Position',[25 75 850 750]);
grammobj.draw();
% scatter( (reliability_table.rootMeanSquareFD_ses1+ ...
% reliability_table.rootMeanSquareFD_ses2)/2, ...
% mean(reliability_table.kendallw,2), 50);
savefilename = fullfile(fname, ...
['sessioneffects_kendallw' '_' ...
methodname ...
communitylabel{1} ...
] ...
);
savefig(savefilename);
grammobj.export('file_name',savefilename,'file_type','pdf');
exportfun(savefilename);
end
% % Compare consistency of successive normalization to global signal regression
%
% y_methods = results.cpac.sn_gm_agreement.mccc(nodeidx);
% y_lo = results.cpac.sn_gm_agreement.mccc_ci(nodeidx,1);
% y_hi = results.cpac.sn_gm_agreement.mccc_ci(nodeidx,2);
% figure;
% set(gcf,'Position',[50 75 1200 600]);
% grammobj = plot_reliability_dotplot(y_methods, ...
% y_lo, ...
% y_hi, ...
% 'Before/After SN Agreement', ...
% communitycolor ...
% );
% grammobj.set_title(['ICC Method Agreement']);
% grammobj.draw();
% exportfun(fullfile(fname,['sessioneffects_reliability' ...
% '2_' communitylabel{1} ]));
%
end
function output = conditional_correlation(X,Y)
% Only uses usual column standardize (i.e. correlation)
output = struct();
n1 = size(X,1);
n2 = size(Y,1);
if(n2>n1)
Y = Y(end-n1+1:end,:);
elseif(n1>n2)
X = X(end-n2+1:end,:);
end
[Sigma results] = covariance.conditional_sample_covariance_separate(X, ...
struct('verbose',true,...
'nuisance',Y) ...
);
output.corr = results.Sigma;
output.nuisance = results.nCorr;
output.corr2 = covariance.mle_sample_covariance(results.X_perpY, ...
struct('standardize', 'cols'));
output.NSR = results.NSR;
end
function Sigma = standard_correlation(X)
% Usual standard correlation matrix
[Sigma results] = covariance.mle_sample_covariance(X, ...
struct('standardize','cols'));
end
function Sigma = standard_correlation_sn(X)
% Automatically applies row-first successive norm
%standardize.successive_normalize(X');
[Xnew] = standardize.successive_normalize(X');
[Sigma results] = corr(Xnew');
% [Sigma results] = covariance.mle_sample_covariance(X,
% struct('standardize',true));
%
end
function trt_reliability = helper_node_mccc(graphs)
%
% Inputs
% - graphs is p x p x subjects x {rater|session}
p = size(graphs,1);
nsubjects = size(graphs,3);
if(p<=50)
nodeidx = 1:p;
end
nboot = 50;
mccc = zeros(p,1);
mccc_ci = zeros(p,2);
mccc_var = zeros(p,1);
mccc_wvar = zeros(p,1);
mccc_boot = zeros(p,nboot);
similarity = zeros(2*nsubjects,2*nsubjects);
for nodeno=1:p;
jj = nodeno;
%jj = nodeidx(nodeno);
features = squeeze(graphs(jj,:,:,:));
similarity = similarity + cov(cat(2,features(:,:,1),features(:,:,2)));
end
similarity = similarity./p;
for nodeno=1:p
%jj = nodeidx(nodeno)
jj = nodeno
features = squeeze(graphs(jj,:,:,:));
[mccc(jj), MCCC, mccc_ci(jj,:), mccc_boot(jj,:)] = ...
reliability.mccc(features(:,:,1)',features(:,:,2)');
mccc_var(jj) = MCCC.normVind;
mccc_wvar(jj) = MCCC.normVdep;
disp(sprintf('MCCC: %2.4f, CI: (%2.4f,%2.4f), WithinVar: %.4f, TotalVar: %.4f', ...
mccc(jj), mccc_ci(jj,1), mccc_ci(jj,2), mccc_wvar(jj), mccc_var(jj)))
end
trt_reliability.mccc = real(mccc);
trt_reliability.mccc_tvar = mccc_var;
trt_reliability.mccc_wvar = mccc_wvar;
trt_reliability.mccc_ci = real(mccc_ci);
trt_reliability.mccc_boot = real(mccc_boot);
trt_reliability.similarity = similarity;
end
function site_effect = detect_site_effect(X,sitelabels)
corrfun = @(X)(corr(X)); %@(X)(covariance.rank_sample_covariance(X(:,1:min(50,size(X,2)))));
upper_idx = find(reshape(triu(ones(size(X,2), size(X,3)),1), [1 size(X,2)^2]));
X = reshape(X,[size(X,1) size(X,2)*size(X,3)]);
tmpSimilarity = corrfun(X(:,upper_idx)');
[hom sep mw] = compareWithinAndBetweenGroupsSim(tmpSimilarity,sitelabels);
site_effect.similarity = tmpSimilarity;
site_effect.within = hom;
site_effect.between = sep;
site_effect.stat = mw;
site_effect.ratio = (hom-sep)/(hom+sep);
end
function [nuisance] = get_shared_nuisance(Y);
% Y is time-series x subjects
Yz = zscore(Y')';
[U S V] = svd(Y);
nfactors = min(size(Y,2),5);
nuisance = U(1:nfactors,:);
nuisance = reshape(nuisance,[size(Y,1) nfactors]);
end
function output = get_influence_diagnostic(X)
output = struct();
[Sigmasb Sigmas] = covjackknife(X,[1 2 3]);
output.nansubjects = isnan(squeeze(sum(sum(Sigmas,1),2)));
Sigmasb(isnan(Sigmasb)) = 0;
Sigmas(isnan(Sigmas)) = 0;
[output.influence output.norminfluence] = influence(Sigmasb,Sigmas);
end
function vplotobj = plot_violinplot(datamatrix,methodnames,community)
addpath('~/MATLAB/packages/Violinplot-Matlab');
ncommunities = 7;
vcolormap = brewermap(ncommunities,'Spectral');
vplotobj = violinplot(datamatrix, ...
'ShowNotches');
for plotno=1:length(vplotobj)
set(vplotobj(plotno).ViolinPlot, ...
'FaceColor',vcolormap(community(plotno),:) ...
);
set(vplotobj(plotno).ScatterPlot, ...
'MarkerEdgeColor', vcolormap(community(plotno),:) ...
);
set(vplotobj(plotno).ScatterPlot, ...
'CData', vcolormap(community(plotno),:) ...
);
set(vplotobj(plotno).ScatterPlot, ...
'MarkerFaceColor', vcolormap(community(plotno),:) ...
);
set(vplotobj(plotno).MeanPlot, ...
'Color', vcolormap(community(plotno),:) ...
);
end
xlabel('ROIs','fontsize',20,'fontweight','bold');
ylabel('Reliability', 'fontsize',20,'fontweight','bold');
title(regexprep(methodnames{1},'_','-'));
xtickvals = get(gca,'XTick');
xticklabels = get(gca,'XTickLabels');
set(gca,'FontSize',18, ...
'XTick',xtickvals(1:4:end), ...
'XTickLabels',xticklabels(1:4:end) ...
)
%'XTickLabelRotation',90)
end
function grammobj = plot_reliability_dotplot(y_methods,y_lo,y_hi,methodnames,community)
geom_type = 'geom_point';
color_type = 'common';
ncommunities = 7;
alternate_color = zeros(1,length(y_methods));
switch color_type
case 'odd-even'
alternate_color(1:2:end) = 1;
alternate_color(2:2:end) = 2;
mycolormap = brewermap(length(unique(alternate_color))*...
ncommunities,...
'Paired' ...