1
+ function results = analyze_etkinlab_sess_length()
2
+
3
+ studyname = ' best-eegfmri-cpac'
4
+ addpath(fullfile(getenv(' HOME' ),' MATLAB' ,' ggmClass' ));
5
+
6
+
7
+ if (exist(' brewermap' ))
8
+ colormapfun = @()(flipud(brewermap(length(colormap ),' RdYlBu' )));
9
+ close all ;
10
+ else
11
+ colormapfun = @winter ;
12
+ end
13
+
14
+ % test re-test mccc
15
+ exportfun = @(filename )(print(' -dpng' ,' -r150' ,filename ));
16
+ % fname = ['tmp' filesep datestr(now,'dd-mmm-yyyy')];
17
+ fname = [' tmp' filesep ' 15-Oct-2018' ];
18
+ if (~exist(fname ,' dir' ))
19
+ mkdir(fname )
20
+ end
21
+ fname = [fname filesep studyname ];
22
+ if (~exist(fname ,' dir' ))
23
+ mkdir(fname )
24
+ end
25
+
26
+ nmethods = 1 ;
27
+ methodnames = {' sn_denoised' };
28
+ load([' tmp' filesep ' 15-Oct-2018' filesep ' best-eegfmri-cpac-trt-t840/etkinlab_analysis/data_etkinlab_analysis_best-eegfmri-cpac_rfmri.mat' ]);
29
+
30
+ nsubjects = length(studydata .session1 .X );
31
+ p = size(studydata.session1.X{1 },2 );
32
+
33
+
34
+ % Get community labels
35
+ nodeidx = 1 : size(studydata.session1.X{1 },2 );
36
+ tmp_community_parts = ...
37
+ regexp(studydata .roilabels .table .Var2 ,{' _' }, ' split' );
38
+ for ii= 1 : length(tmp_community_parts )
39
+ communitylabels{ii } = tmp_community_parts{ii }{3 };
40
+ end
41
+ communitylabel = grp2idx(communitylabels );
42
+ communitycolor = communitylabel ;
43
+
44
+
45
+ results = {};
46
+ results.cpac = {};
47
+ results.community = communitylabel ;
48
+ results.communitycolor = communitycolor ;
49
+
50
+
51
+ % Get network estimate in increments of 80 measurements
52
+ % (80*.7 = 56 seconds)
53
+
54
+ n_step = 80 ;
55
+ n_minutes = 10 ;
56
+ n_trials = 1 ;
57
+
58
+ for minuteno= 1 : 1 : n_minutes
59
+ tmp_kendallW = zeros(1 ,nsubjects );
60
+ tmp_mccc = zeros(p ,1 );
61
+ for trialno= 1 : n_trials
62
+ corr_mat = helper_corr_session(studydata ,minuteno * n_step );
63
+ results.networks = corr_mat ;
64
+ graphs = results .networks(nodeidx ,: ,: ,: );
65
+ tmp_kendallW = tmp_kendallW + helper_kendallW(graphs ,nodeidx );
66
+ trt_reliability = helper_node_mccc(graphs );
67
+ tmp_mccc = tmp_mccc + real(trt_reliability .mccc );
68
+ end
69
+ results .kendallW(: ,minuteno ) = tmp_kendallW / n_trials ;
70
+ results .mccc(: ,minuteno ) = tmp_mccc / n_trials ;
71
+ end
72
+
73
+ dlmwrite([fname filesep ' trt_' studyname ' _mccc.txt' ],results .mccc ,' precision' ,6 );
74
+ dlmwrite([fname filesep ' trt_' studyname ' _kendallw.txt' ],results .kendallW ,' precision' ,6 );
75
+
76
+ mccc_summary(: ,1 ) = mean(results .mccc ,1 );
77
+ mccc_summary(: ,2 ) = prctile(results .mccc ,[5 ]);
78
+ mccc_summary(: ,3 ) = prctile(results .mccc ,[95 ]);
79
+
80
+ kw_summary(: ,1 ) = mean(results .kendallW ,1 );
81
+ kw_summary(: ,2 ) = prctile(results .kendallW ,[5 ]);
82
+ kw_summary(: ,3 ) = prctile(results .kendallW ,[95 ]);
83
+
84
+ dlmwrite([fname filesep ' trt_' studyname ' _mccc_summary.txt' ],mccc_summary ,' precision' ,4 );
85
+ dlmwrite([fname filesep ' trt_' studyname ' _kendallw_summary.txt' ],kw_summary ,' precision' ,4 );
86
+
87
+
88
+ % save([fname filesep 'trt_' studyname '_rfmri.mat'],'results','-append');
89
+ % end
90
+ %
91
+ % try
92
+ % mean(results.cpac.sn_denoised.trt_reliability.kendallsw-...
93
+ % results.cpac.observed.trt_reliability.kendallsw,2);
94
+ % catch
95
+ %
96
+ % end
97
+
98
+ end
99
+
100
+
101
+
102
+ function corr_mat = helper_corr_session(studydata ,n_time )
103
+
104
+ [n p ] = size(studydata.([' session1' ]).X{1 });
105
+ nsubjects = length(studydata.([' session1' ]).X);
106
+ step_t = n_time
107
+ verbose = false ;
108
+
109
+ nsessions = 2 ;
110
+ corr_mat = zeros(p ,p ,nsubjects ,nsessions );
111
+ for sessionno= 1 : nsessions
112
+ for ii= 1 : nsubjects
113
+ if (verbose )
114
+ ii
115
+ [' session' num2str(sessionno )]
116
+ end
117
+ X = studydata.([' session' num2str(sessionno )]).X{ii };
118
+ start_idx = randi([9 max(9 ,(n - 9 - step_t ))]);
119
+ X = X(start_idx + 1 : start_idx + n_time ,: );
120
+
121
+ try
122
+ corr_mat(: ,: ,ii ,sessionno ) = ...
123
+ standard_correlation_sn(X );
124
+ catch me
125
+ disp(me )
126
+ results.Site1{ii ,sessionno } = {};
127
+ disp(' Session' )
128
+ sessionno
129
+ disp(' Subject' )
130
+ ii
131
+ end
132
+ % if(ii==1)
133
+ % demo_conditional_correlation(X,Y);
134
+ % end
135
+ end
136
+ end
137
+
138
+
139
+ end
140
+
141
+
142
+
143
+
144
+ function output = helper_kendallW(graphs ,nodeidx )
145
+
146
+
147
+ p = size(graphs ,1 );
148
+ nsubjects = size(graphs ,3 );
149
+ tmp_kendallW = nan(1 ,nsubjects );
150
+
151
+
152
+ ba_nodes = nodeidx ;
153
+ trid_idx = find(reshape(triu(ones(p ,p ),1 ),[p ^ 2 1 ]));
154
+ for subjectno= 1 : nsubjects
155
+ rater1 = graphs(ba_nodes ,ba_nodes ,subjectno ,1 );
156
+ rater2 = graphs(ba_nodes ,ba_nodes ,subjectno ,2 );
157
+ rater1 = rater1(trid_idx );
158
+ rater2 = rater2(trid_idx );
159
+
160
+ tmp_kendallW(subjectno ) = reliability .kendallsW(cat(2 ,rater1 ,rater2 ));
161
+ end
162
+
163
+ output = tmp_kendallW ;
164
+
165
+ end
166
+
167
+
168
+
169
+ function output = helper_mccc()
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+ end
178
+
179
+
180
+
181
+ function output = conditional_correlation(X ,Y )
182
+ % Only uses usual column standardize (i.e. correlation)
183
+ output = struct();
184
+
185
+ [Sigma results ] = covariance .conditional_sample_covariance_separate(X , ...
186
+ struct(' verbose' ,false ,...
187
+ ' nuisance' ,Y ) ...
188
+ );
189
+
190
+ output.corr = results .Sigma ;
191
+ output.nuisance = results .nCorr ;
192
+ output.corr2 = covariance .mle_sample_covariance(results .X_perpY , ...
193
+ struct(' standardize' , false ));
194
+ output.NSR = results .NSR ;
195
+
196
+ end
197
+
198
+
199
+ function Sigma = standard_correlation(X )
200
+ % Usual standard correlation matrix
201
+
202
+
203
+ [Sigma results ] = covariance .mle_sample_covariance(X , ...
204
+ struct(' standardize' ,false ));
205
+
206
+
207
+ end
208
+
209
+
210
+ function Sigma = standard_correlation_sn(X )
211
+ % Automatically applies row-first successive norm
212
+ % standardize.successive_normalize(X');
213
+
214
+ [Xnew ] = standardize .successive_normalize(X ' );
215
+ [Sigma results ] = corr(Xnew ' );
216
+
217
+ % [Sigma results] = covariance.mle_sample_covariance(X,
218
+ % struct('standardize',true));
219
+ %
220
+ end
221
+
222
+
223
+ function site_effect = detect_site_effect(X ,sitelabels )
224
+
225
+ corrfun = @(X )(corr(X )); % @(X)(covariance.rank_sample_covariance(X(:,1:min(50,size(X,2)))));
226
+ upper_idx = find(reshape(triu(ones(size(X ,2 ), size(X ,3 )),1 ), [1 size(X ,2 )^2 ]));
227
+ X = reshape(X ,[size(X ,1 ) size(X ,2 )*size(X ,3 )]);
228
+ tmpSimilarity = corrfun(X(: ,upper_idx )' );
229
+ [hom sep mw ] = compareWithinAndBetweenGroupsSim(tmpSimilarity ,sitelabels );
230
+
231
+ site_effect.similarity = tmpSimilarity ;
232
+ site_effect.within = hom ;
233
+ site_effect.between = sep ;
234
+ site_effect.stat = mw ;
235
+ site_effect.ratio = (hom - sep )/(hom + sep );
236
+
237
+
238
+ end
239
+
240
+ function [nuisance ] = get_shared_nuisance(Y );
241
+
242
+ % Y is time-series x subjects
243
+ Yz = zscore(Y ' )' ;
244
+ [U S V ] = svd(Y );
245
+ nfactors = min(size(Y ,2 ),5 );
246
+ nuisance = U(1 : nfactors ,: );
247
+ nuisance = reshape(nuisance ,[size(Y ,1 ) nfactors ]);
248
+
249
+ end
250
+
251
+ function output = get_influence_diagnostic(X )
252
+
253
+ output = struct();
254
+
255
+ [Sigmasb Sigmas ] = covjackknife(X ,[1 2 3 ]);
256
+ output.nansubjects = isnan(squeeze(sum(sum(Sigmas ,1 ),2 )));
257
+ Sigmasb(isnan(Sigmasb )) = 0 ;
258
+ Sigmas(isnan(Sigmas )) = 0 ;
259
+
260
+ [output .influence output .norminfluence ] = influence(Sigmasb ,Sigmas );
261
+
262
+ end
263
+
264
+ function trt_reliability = helper_node_mccc(graphs )
265
+ %
266
+ % Inputs
267
+ % - graphs is p x p x subjects x {rater|session}
268
+
269
+ p = size(graphs ,1 );
270
+ nsubjects = size(graphs ,3 );
271
+ if (p <= 50 )
272
+ nodeidx = 1 : p ;
273
+ end
274
+ verbose = false ;
275
+
276
+ nboot = 50 ;
277
+ mccc = zeros(p ,1 );
278
+ mccc_ci = zeros(p ,2 );
279
+ mccc_var = zeros(p ,1 );
280
+ mccc_wvar = zeros(p ,1 );
281
+ mccc_boot = zeros(p ,nboot );
282
+ similarity = zeros(2 * nsubjects ,2 * nsubjects );
283
+
284
+ for nodeno= 1 : p ;
285
+ jj = nodeno ;
286
+ % jj = nodeidx(nodeno);
287
+ features = squeeze(graphs(jj ,: ,: ,: ));
288
+ similarity = similarity + cov(cat(2 ,features(: ,: ,1 ),features(: ,: ,2 )));
289
+
290
+ end
291
+ similarity = similarity ./ p ;
292
+ for nodeno= 1 : p
293
+ % jj = nodeidx(nodeno)
294
+ if (verbose )
295
+ jj = nodeno
296
+ else
297
+ jj = nodeno ;
298
+ if (mod(jj ,10 )==0 )
299
+ disp(sprintf(' MCCC %2.2f Complete' ,jj / p ));
300
+ end
301
+ end
302
+ features = squeeze(graphs(jj ,: ,: ,: ));
303
+ [mccc(jj ), MCCC , mccc_ci(jj ,: ), mccc_boot(jj ,: )] = ...
304
+ reliability .mccc(features(: ,: ,1 )' ,features(: ,: ,2 )' );
305
+ mccc_var(jj ) = MCCC .normVind ;
306
+ mccc_wvar(jj ) = MCCC .normVdep ;
307
+ if (verbose )
308
+ disp(sprintf(' MCCC: %2.4f , CI: (%2.4f ,%2.4f ), WithinVar: %.4f , TotalVar: %.4f ' , ...
309
+ mccc(jj ), mccc_ci(jj ,1 ), mccc_ci(jj ,2 ), mccc_wvar(jj ), mccc_var(jj )))
310
+ end
311
+ end
312
+ trt_reliability.mccc = real(mccc );
313
+ trt_reliability.mccc_tvar = mccc_var ;
314
+ trt_reliability.mccc_wvar = mccc_wvar ;
315
+ trt_reliability.mccc_ci = real(mccc_ci );
316
+ trt_reliability.mccc_boot = real(mccc_boot );
317
+ trt_reliability.similarity = similarity ;
318
+ end
0 commit comments