-
Notifications
You must be signed in to change notification settings - Fork 7
/
swe_contrasts.m
764 lines (720 loc) · 37 KB
/
swe_contrasts.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
function [SwE] = swe_contrasts(SwE,Ic)
% This function writes statistic & p value images for parametric analyses.
% =========================================================================
% Fills in SwE.xCon and writes the following images for a parametric
% analysis:
% - swe_{vox|dpx|dat}_{T|F}stat_c{c#}.{nii|gii|mat}: T/F tatistic image
% - swe_{vox|dpx|dat}_{zT|xF}stat_c{c#}.{nii|gii|mat}: Z/Chi square equivalent
% statistic image
% - swe_{vox|dpx|dat}_{T|F}stat_lp_c{c#}.{nii|gii|mat}: -log10 P image.
% - swe_{vox|dpx|dat}_edf_c{c#}.{nii|gii|mat}: error degrees of freedom image.
% =========================================================================
% FORMAT [SwE] = SwE_contrasts(SwE,Ic)
% -------------------------------------------------------------------------
% Inputs/Outputs:
% - SwE: SwE data structure
% - Ic: Indices of xCon to compute
% =========================================================================
% Modified version of spm_contrasts adapted for the SwE toolbox
% By Bryan Guillaume
% Version Info: $Format:%ci$ $Format:%h$
% Temporary SwE variable to check for any changes to SwE. We want to avoid
% always having to save SwE.mat unless it has changed, because this is
% slow. A side benefit is one can look at results with just read
% privileges.
%--------------------------------------------------------------------------
tmpSwE = SwE;
%-Get and change to results directory
%--------------------------------------------------------------------------
try
cd(SwE.swd);
end
% For Wild Bootstrap we already made contrast images, we just need to
% record them, if we haven't already.
%--------------------------------------------------------------------------
if isfield(SwE, 'WB')
if ~isfield(SwE, 'xCon') || isempty(SwE.xCon)
SwE = swe_contrasts_WB(SwE);
end
return
end
%-Get contrast definitions (if available)
%--------------------------------------------------------------------------
try
xCon = SwE.xCon;
catch
xCon = [];
end
%-Set all contrasts by default
%--------------------------------------------------------------------------
if nargin < 2
Ic = 1:length(xCon);
end
%-Check data format
%--------------------------------------------------------------------------
file_ext = swe_get_file_extension(SwE.xY.P{1});
isMat = strcmpi(file_ext,'.mat');
isCifti = strcmpi(file_ext,'.dtseries.nii') || strcmpi(file_ext,'.dscalar.nii');
isOctave = exist('OCTAVE_VERSION','builtin');
if isCifti
metadata = {'ciftiTemplate', SwE.xY.P{1}};
file_data_type = 'dpx';
end
if isMat
file_data_type = 'dat';
end
if ~isMat && ~isCifti
isMeshData = spm_mesh_detect(SwE.xY.VY);
if isMeshData
file_ext = '.gii';
file_data_type = 'dpx';
g = SwE.xY.VY(1).private;
metadata = g.private.metadata;
name = {metadata.name};
if any(ismember(name,'SurfaceID'))
metadata = metadata(ismember(name,'SurfaceID'));
metadata = {metadata.name, metadata.value};
elseif isfield(g,'faces') && ~isempty(g.faces)
metadata = {'SurfaceID', SwE.xY.VY(1).fname};
else
metadata = {};
end
else
file_ext = spm_file_ext;
file_data_type = 'vox';
metadata = {};
end
end
%-Map parameter files
%--------------------------------------------------------------------------
%-OLS estimators and covariance estimates
%--------------------------------------------------------------------------
Vbeta = SwE.Vbeta;
Vcov_beta = SwE.Vcov_beta;
dof_type = SwE.dof.dof_type;
if dof_type == 1
Vcov_beta_g = SwE.Vcov_beta_g;
end
if dof_type>1
Vcov_vis = SwE.Vcov_vis;
end
%-Compute & store contrast parameters, contrast/ESS images, & SwE images
%==========================================================================
spm('Pointer','Watch')
XYZ = SwE.xVol.XYZ;
S=size(XYZ,2);
for i = 1:length(Ic)
%-Canonicalise contrast structure with required fields
%----------------------------------------------------------------------
ic = Ic(i);
%-Write contrast images?
%======================================================================
if isempty(xCon(ic).Vspm)
if ~isMat
Q = cumprod([1,SwE.xVol.DIM(1:2)'])*XYZ - ...
sum(cumprod(SwE.xVol.DIM(1:2)'));
end
Co=xCon(ic).c;
nBeta = size(Co,1);
nSizeCon = size(Co,2);
xCon(ic).eidf=rank(Co);
% detect the indices of the betas of interest
if nSizeCon==1
ind = find(Co ~= 0);
else
ind = find(any(Co'~=0));
end
nCov_beta = (nBeta+1)*nBeta/2;
% if ".mat" format, load data now
if isMat
beta = importdata(Vbeta);
S = size(beta,2);
end
%-Compute contrast
%------------------------------------------------------
fprintf('\t%-32s: %30s',sprintf('contrast image %2d',ic),...
'...computing'); %-#
str = 'contrast computation';
swe_progress_bar('Init',100,str,'');
% if the Co is a vector, then create Co * Beta (Vcon)
if nSizeCon==1
if ~isMat
V = Vbeta(ind);
end
cBeta = zeros(1,S);
for j=1:numel(ind)
if isMat
cBeta = cBeta + Co(ind(j)) * beta(ind(j),:);
else
cBeta = cBeta + Co(ind(j)) * swe_data_read(V(j),'xyz',XYZ);
end
swe_progress_bar('Set',100*(j/numel(ind)));
end
swe_progress_bar('Clear')
if isMat
%-save contrasted beta
%------------------------------------------------------
xCon(ic).Vcon = sprintf('swe_%s_con_c%02d%s',file_data_type,ic,file_ext);
save(xCon(ic).Vcon, 'cBeta')
else
%-Prepare handle for contrast image
%------------------------------------------------------
xCon(ic).Vcon = swe_data_hdr_write(sprintf('swe_%s_con_c%02d%s',file_data_type,ic,file_ext),...
SwE.xVol.DIM', SwE.xVol.M,...
sprintf('SwE contrast - %d: %s',ic,xCon(ic).name),...
metadata);
%-Write image
%------------------------------------------------------
tmp = zeros(SwE.xVol.DIM');
tmp(Q) = cBeta;
xCon(ic).Vcon = swe_data_write(xCon(ic).Vcon, tmp);
clear tmp
end
if ~isMat
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s', xCon(ic).Vcon.fname))
else
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s', xCon(ic).Vcon))
end
else
if ~isMat
V = Vbeta(ind);
end
cBeta = zeros(nSizeCon,S);
for j=1:numel(ind)
if isMat
cBeta = cBeta + Co(ind(j),:)' * beta(ind(j),:);
else
cBeta = cBeta + Co(ind(j),:)' * swe_data_read(V(j),'xyz',XYZ);
end
swe_progress_bar('Set',100*(j/numel(ind)));
end
swe_progress_bar('Clear')
if ~isMat
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...computed'))
else
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...computed'))
end
end
% clear beta for memory
if isMat
clear beta
end
%-Write inference SwE
%======================================================================
%-compute the contrasted beta covariances and edof for the contrast
switch(xCon(ic).STAT)
case 'T'
eSTAT = 'Z';
case 'F'
eSTAT = 'X';
end
fprintf('\t%-32s: %30s',sprintf('spm{%c} image %2d',eSTAT,ic),...
'...computing'); %-#
str = 'contrasted beta covariance computation';
swe_progress_bar('Init',100,str,'');
it = 0;
it2 = 0;
cCovBc = zeros(nSizeCon*(nSizeCon+1)/2,S);
if dof_type == 1
cCovBc_g = zeros(nSizeCon*(nSizeCon+1)/2,S,SwE.Gr.nGr);
else
indSubDesignMatrices = SwE.dof.iBeta_dof(ind);
subjectsInvolved = [];
for iIndSubDesignMatrices = indSubDesignMatrices
subjectsInvolved = [subjectsInvolved; SwE.Subj.iSubj(SwE.dof.iGr_dof == iIndSubDesignMatrices)];
end
subjectsInvolved = unique(subjectsInvolved);
indSubjInvolved = nan(length(subjectsInvolved),1);
% convert into in
for iSubjInvolved = 1:length(subjectsInvolved)
indSubjInvolved(iSubjInvolved) = find(SwE.Subj.uSubj == subjectsInvolved(iSubjInvolved));
end
xCon(ic).edf = sum(SwE.dof.edof_Subj(indSubjInvolved));
end
% load .mat file(s) if this is the format
if isMat
cov_beta = importdata(Vcov_beta);
if dof_type == 1
cov_beta_g = importdata(Vcov_beta_g);
end
end
for j = 1:nBeta
for jj = j:nBeta
it = it + 1;
if any(j == ind) && any(jj == ind)
it2 = it2+1;
weight = Co(j,:)'*Co(jj,:);
if (j~=jj) %was wrong (BG - 13/09/13)
weight = weight + weight';
end
weight = weight(tril(ones(nSizeCon))==1);
if isMat
cCovBc = cCovBc + weight * cov_beta(it,:);
else
cCovBc = cCovBc + weight * swe_data_read(Vcov_beta(it),'xyz',XYZ);
end
if dof_type == 1
for g = 1:SwE.Gr.nGr
if isMat
cCovBc_g(:,:,g) = cCovBc_g(:,:,g) + weight *...
reshape(cov_beta_g(g,it,:), 1, S);
else
cCovBc_g(:,:,g) = cCovBc_g(:,:,g) + weight *...
swe_data_read(Vcov_beta_g((g-1)*nCov_beta+it),'xyz',XYZ);
end
swe_progress_bar('Set',100*((it2-1+g/SwE.Gr.nGr)/length(ind)/(length(ind)+1)*2));
end
end
swe_progress_bar('Set',100*(it2/length(ind)/(length(ind)+1)*2));
end
end
end
swe_progress_bar('Clear')
str = 'spm computation';
swe_progress_bar('Init',100,str,'');
equivalentScore = zeros(1,S);
% add output of uncorrected p-values
uncP = zeros(1,S);
switch(xCon(ic).STAT)
case 'T' %-Compute spm{t} image
%----------------------------------------------------------
score = cBeta ./ sqrt(cCovBc);
swe_progress_bar('Set',100*(0.1));
switch dof_type
case 1
tmp = 0;
for g = 1:SwE.Gr.nGr
tmp = tmp + cCovBc_g(:,:,g).^2/SwE.dof.edof_Gr(g);
swe_progress_bar('Set',100*(g/SwE.Gr.nGr/10+0.1));
end
clear cCovBc_g
edf = cCovBc.^2 ./ tmp;
swe_progress_bar('Set',100*(0.2));
% transform into Z-scores image
if any(score>0) % avoid to run the following line when all Z are < 0 (BG - 22/08/2016)
uncP(score>0) = spm_Tcdf(-score(score>0),edf(score>0));
equivalentScore(score>0) = -swe_invNcdf(uncP(score>0));
end
if any(score<0) % avoid to run the following line when all Z are > 0(BG - 22/08/2016)
uncP(score<0) = spm_Tcdf(score(score<0),edf(score<0));
equivalentScore(score<0) = swe_invNcdf(uncP(score<0));
uncP(score<0) = 1 - uncP(score<0);
end
%Z = -log10(1-spm_Tcdf(Z,edf)); %transfo into -log10(p)
swe_progress_bar('Set',100);
case 0
% transform into Z-scores image
if any(score>0) % avoid to run the following line when all Z are < 0 (BG - 22/08/2016)
uncP(score>0) = spm_Tcdf(-score(score>0),xCon(ic).edf);
equivalentScore(score>0) = -swe_invNcdf(uncP(score>0));
end
if any(score<0) % avoid to run the following line when all Z are > 0(BG - 22/08/2016)
uncP(score<0) = spm_Tcdf(score(score<0),xCon(ic).edf);
equivalentScore(score<0) = swe_invNcdf(uncP(score<0));
uncP(score<0) = 1 - uncP(score<0);
end
% transform into -log10(p-values) image
%Z = -log10(1-spm_Tcdf(Z,xCon(ic).edf));
swe_progress_bar('Set',100);
case 2
CovcCovBc = 0;
if isMat
cov_vis = importdata(Vcov_vis);
end
for g = 1:SwE.Gr.nGr
Wg = kron(Co,Co)' * swe_duplication_matrix(nBeta) * SwE.Vis.weight(:,SwE.Vis.iGr_Cov_vis_g==g);
Wg = kron(Wg,Wg) * swe_duplication_matrix(SwE.Vis.nCov_vis_g(g));
if isMat
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(cov_vis(SwE.Vis.iGr_Cov_vis_g==g,:),SwE.dof.dofMat{g},1);
else
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(swe_data_read(Vcov_vis(SwE.Vis.iGr_Cov_vis_g==g),'xyz',XYZ),SwE.dof.dofMat{g},1);
end
swe_progress_bar('Set',100*(0.1) + g*80/SwE.Gr.nGr);
end
clear Wg cov_vis
edf = 2 * cCovBc.^2 ./ CovcCovBc - 2;
clear CovcCovBc
if any(score>0) % avoid to run the following line when all Z are < 0 (BG - 22/08/2016)
uncP(score>0) = spm_Tcdf(-score(score>0),edf(score>0));
equivalentScore(score>0) = -swe_invNcdf(uncP(score>0));
end
if any(score<0) % avoid to run the following line when all Z are > 0(BG - 22/08/2016)
uncP(score<0) = spm_Tcdf(score(score<0),edf(score<0));
equivalentScore(score<0) = swe_invNcdf(uncP(score<0));
uncP(score<0) = 1 - uncP(score<0);
end
%Z = -log10(1-spm_Tcdf(Z,edf)); %transfo into -log10(p)
swe_progress_bar('Set',100);
case 3
CovcCovBc = 0;
if isMat
cov_vis = importdata(Vcov_vis);
end
for g = 1:SwE.Gr.nGr
Wg = kron(Co,Co)' * swe_duplication_matrix(nBeta) * SwE.Vis.weight(:,SwE.Vis.iGr_Cov_vis_g==g);
Wg = kron(Wg,Wg) * swe_duplication_matrix(SwE.Vis.nCov_vis_g(g));
if isMat
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(cov_vis(SwE.Vis.iGr_Cov_vis_g==g,:),SwE.dof.dofMat{g},2);
else
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(swe_data_read(Vcov_vis(SwE.Vis.iGr_Cov_vis_g==g),'xyz',XYZ),SwE.dof.dofMat{g},2);
end
swe_progress_bar('Set',100*(0.1) + g*80/SwE.Gr.nGr);
end
clear Wg cov_vis
edf = 2 * cCovBc.^2 ./ CovcCovBc;
clear CovcCovBc
% transform into Z-scores image
if any(score>0) % avoid to run the following line when all Z are < 0 (BG - 22/08/2016)
uncP(score>0) = spm_Tcdf(-score(score>0),edf(score>0));
equivalentScore(score>0) = -swe_invNcdf(uncP(score>0));
end
if any(score<0) % avoid to run the following line when all Z are > 0(BG - 22/08/2016)
uncP(score<0) = spm_Tcdf(score(score<0),edf(score<0));
equivalentScore(score<0) = swe_invNcdf(uncP(score<0));
uncP(score<0) = 1 - uncP(score<0);
end
%Z = -log10(1-spm_Tcdf(Z,edf)); %transfo into -log10(p)
swe_progress_bar('Set',100);
end
case 'F' %-Compute spm{F} image
%---------------------------------------------------------
if nSizeCon==1
score = abs(cBeta ./ sqrt(cCovBc));
indNotNan = ~isnan(score);
swe_progress_bar('Set',100*(0.1));
switch dof_type
case 1
tmp = 0;
for g = 1:SwE.Gr.nGr
tmp = tmp + cCovBc_g(:,:,g).^2/SwE.dof.edof_Gr(g);
swe_progress_bar('Set',100*(g/SwE.Gr.nGr/10+0.1));
end
clear cCovBc_g
edf = cCovBc.^2 ./ tmp;
swe_progress_bar('Set',100*(3/4));
% transform into X-scores image
uncP(indNotNan) = spm_Tcdf(-abs(score(indNotNan)), edf(indNotNan));
equivalentScore(indNotNan) = (swe_invNcdf(uncP(indNotNan))).^2;
uncP(indNotNan) = 2 * uncP(indNotNan);
% transform into -log10(p-values) image
%Z = -log10(1-spm_Fcdf(Z,1,edf));
swe_progress_bar('Set',100);
case 0
% transform into X-scores image
uncP(indNotNan) = spm_Tcdf(-abs(score(indNotNan)),xCon(ic).edf);
equivalentScore(indNotNan) = (swe_invNcdf(uncP(indNotNan))).^2;
uncP(indNotNan) = 2 * uncP(indNotNan);
% transform into -log10(p-values) image
%Z = -log10(1-spm_Fcdf(Z,1, xCon(ic).edf));
swe_progress_bar('Set',100);
case 2
CovcCovBc = 0;
if isMat
cov_vis = importdata(Vcov_vis);
end
for g = 1:SwE.Gr.nGr
Wg = kron(Co,Co)' * swe_duplication_matrix(nBeta) * SwE.Vis.weight(:,SwE.Vis.iGr_Cov_vis_g==g);
Wg = kron(Wg,Wg) * swe_duplication_matrix(SwE.Vis.nCov_vis_g(g));
if isMat
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(cov_vis(SwE.Vis.iGr_Cov_vis_g==g,:),SwE.dof.dofMat{g},1);
else
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(swe_data_read(Vcov_vis(SwE.Vis.iGr_Cov_vis_g==g),'xyz',XYZ),SwE.dof.dofMat{g},1);
end
swe_progress_bar('Set',100*(g/SwE.Gr.nGr/10+0.1));
end
clear Wg cov_vis
edf = 2 * cCovBc.^2 ./ CovcCovBc - 2;
clear CovcCovBc
swe_progress_bar('Set',100*(3/4));
% transform into X-scores image
uncP(indNotNan) = spm_Tcdf(-abs(score(indNotNan)), edf(indNotNan));
equivalentScore(indNotNan) = (swe_invNcdf(uncP(indNotNan))).^2;
uncP(indNotNan) = 2 * uncP(indNotNan);
% transform into -log10(p-values) image
%Z = -log10(1-spm_Fcdf(Z,1,edf));
swe_progress_bar('Set',100);
case 3
CovcCovBc = 0;
if isMat
cov_vis = importdata(Vcov_vis);
end
for g = 1:SwE.Gr.nGr
Wg = kron(Co,Co)' * swe_duplication_matrix(nBeta) * SwE.Vis.weight(:,SwE.Vis.iGr_Cov_vis_g==g);
Wg = kron(Wg,Wg) * swe_duplication_matrix(SwE.Vis.nCov_vis_g(g));
if isMat
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(cov_vis(SwE.Vis.iGr_Cov_vis_g==g,:),SwE.dof.dofMat{g},2);
else
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(swe_data_read(Vcov_vis(SwE.Vis.iGr_Cov_vis_g==g),'xyz',XYZ),SwE.dof.dofMat{g},2);
end
swe_progress_bar('Set',100*(g/SwE.Gr.nGr/10+0.1));
end
clear Wg cov_vis
edf = 2 * cCovBc.^2 ./ CovcCovBc;
% transform into X-scores image
uncP(indNotNan) = spm_Tcdf(-abs(score(indNotNan)), edf(indNotNan));
equivalentScore(indNotNan) = (swe_invNcdf(uncP(indNotNan))).^2;
uncP(indNotNan) = 2 * uncP(indNotNan);
% transform into -log10(p-values) image
%Z = -log10(1-spm_Fcdf(Z,1,edf));
swe_progress_bar('Set',100);
clear CovcCovBc
end
% need to transform in F-score, not in absolute t-score
% corrected on 12/05/15 by BG
score = score.^2;
else
score = zeros(1,S);
if dof_type ~= 0
edf = zeros(1,S);
end
if dof_type == 2
CovcCovBc = 0;
if isMat
cov_vis = importdata(Vcov_vis);
end
for g = 1:SwE.Gr.nGr
Wg = kron(Co,Co)' * swe_duplication_matrix(nBeta) * SwE.Vis.weight(:,SwE.Vis.iGr_Cov_vis_g==g);
Wg = sum(kron(Wg,Wg)) * swe_duplication_matrix(SwE.Vis.nCov_vis_g(g));
if isMat
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(cov_vis(SwE.Vis.iGr_Cov_vis_g==g,:),SwE.dof.dofMat{g},1);
else
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(swe_data_read(Vcov_vis(SwE.Vis.iGr_Cov_vis_g==g),'xyz',XYZ),SwE.dof.dofMat{g},1);
end
end
clear cov_vis
edf = 2 * (sum(swe_duplication_matrix(nSizeCon)) * cCovBc).^2 ./ CovcCovBc - 2;
end
if dof_type == 3
CovcCovBc = 0;
if isMat
cov_vis = importdata(Vcov_vis);
end
tmp = eye(nSizeCon^2);
for g = 1:SwE.Gr.nGr
Wg = kron(Co,Co)' * swe_duplication_matrix(nBeta) * SwE.Vis.weight(:,SwE.Vis.iGr_Cov_vis_g==g);
% tmp is used to sum only the diagonal element
% this is useful to compute the trace as
% tr(A) = vec(I)' * vec(A)
Wg = tmp(:)' * (kron(Wg,Wg)) * swe_duplication_matrix(SwE.Vis.nCov_vis_g(g));
if isMat
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(cov_vis(SwE.Vis.iGr_Cov_vis_g==g,:),SwE.dof.dofMat{g},2);
else
CovcCovBc = CovcCovBc + Wg * swe_vechCovVechV(swe_data_read(Vcov_vis(SwE.Vis.iGr_Cov_vis_g==g),'xyz',XYZ),SwE.dof.dofMat{g},2);
end
end
clear cov_vis
% note that tr(A^2) = vec(A)' * vec(A)
tmp = eye(nSizeCon);
edf = (sum(swe_duplication_matrix(nSizeCon)) * cCovBc.^2 +...
(tmp(:)' * swe_duplication_matrix(nSizeCon) * cCovBc).^2) ./ CovcCovBc;
end
% define a parameter to tell when to update progress
% bar only 80 times
updateEvery = round(S/80);
indNotNan = ~isnan(cCovBc(1,:));
for iVox=1:S
if indNotNan(iVox)
cCovBc_vox = zeros(nSizeCon);
cCovBc_vox(tril(ones(nSizeCon))==1) = cCovBc(:,iVox);
cCovBc_vox = cCovBc_vox + cCovBc_vox' - diag(diag(cCovBc_vox));
score(iVox) = cBeta(:,iVox)' / cCovBc_vox * cBeta(:,iVox);
if (dof_type == 1)
tmp = 0;
for g = 1:SwE.Gr.nGr
cCovBc_g_vox = zeros(nSizeCon);
cCovBc_g_vox(tril(ones(nSizeCon))==1) = cCovBc_g(:,iVox,g);
cCovBc_g_vox = cCovBc_g_vox + cCovBc_g_vox' - diag(diag(cCovBc_g_vox));
tmp = tmp + (trace(cCovBc_g_vox^2) + (trace(cCovBc_g_vox))^2)/...
SwE.dof.edof_Gr(g);
end
edf(iVox)=(trace(cCovBc_vox^2) + (trace(cCovBc_vox))^2) / tmp;
end
% update progress_bar only approx 80 times
if (mod(iVox,updateEvery) == 0)
swe_progress_bar('Set',10 + 80 * (iVox/S));
end
end
end
if dof_type ~= 0
clear cCovBc_g
score = score .*(edf-xCon(ic).eidf+1)./edf/xCon(ic).eidf;
score(score < 0) = 0; % force negatif F-score to 0 (can happen for very low edf)
% transform into X-scores image
% Z2 = chi2inv(spm_Fcdf(Z,xCon(ic).eidf,edf-xCon(ic).eidf+1),1);
try % check if the user do not have the fcdf function or one with 'upper' option
uncP(score>1) = fcdf(score(score>1),xCon(ic).eidf,edf(score>1)-xCon(ic).eidf+1,'upper'); % more accurate to look this way for high scores
equivalentScore(score>1) = swe_invNcdf(uncP(score>1)/2).^2;
uncP(score<=1 & score > 0) = 1 - fcdf(score(score<=1 & score > 0),xCon(ic).eidf,edf(score<=1 & score > 0)-xCon(ic).eidf+1);
equivalentScore(score<=1 & score > 0) = swe_invNcdf(uncP(score<=1 & score > 0)/2).^2;
catch
uncP(score>0) = betainc((edf(score>0) - xCon(ic).eidf + 1)./(edf(score>0) - xCon(ic).eidf + 1 + xCon(ic).eidf * score(score>0)),(edf(score>0)-xCon(ic).eidf+1)/2, xCon(ic).eidf/2); % more accurate to look this way for high scores
equivalentScore(score>0) = swe_invNcdf(uncP(score>0)/2).^2;
% Z2 = swe_invNcdf(0.5 - spm_Fcdf(Z,xCon(ic).eidf, edf-xCon(ic).eidf+1)/2).^2;
end
uncP(score == 0) = 0;
equivalentScore(score == 0) = 0;
% transform into -log10(p-values) image
%Z = -log10(1-spm_Fcdf(Z,xCon(ic).eidf,edf));
else
score = score *(xCon(ic).edf -xCon(ic).eidf+1)/xCon(ic).edf/xCon(ic).eidf;
score(score < 0) = 0; % force negatif F-score to 0 (can happen for very low edf)
% transform into X-scores image
%Z2 = chi2inv(spm_Fcdf(Z,xCon(ic).eidf,xCon(ic).edf-xCon(ic).eidf+1),1);
try % check if the user do not have the fcdf function or one with 'upper' options
uncP(score>1) = fcdf(score(score>1),xCon(ic).eidf,xCon(ic).edf-xCon(ic).eidf+1,'upper');% more accurate to look this way for high score
equivalentScore(score>1) = swe_invNcdf(uncP(score>1)/2).^2;
uncP(score<=1 & score > 0) = 1 - fcdf(score(score<=1 & score > 0),xCon(ic).eidf,xCon(ic).edf-xCon(ic).eidf+1);
equivalentScore(score<=1 & score > 0) = swe_invNcdf(uncP(score<=1 & score > 0)/2).^2;
catch
uncP(score>0) = betainc((xCon(ic).edf - xCon(ic).eidf + 1)./(xCon(ic).edf - xCon(ic).eidf + 1 + xCon(ic).eidf * score(score>0)),(xCon(ic).edf-xCon(ic).eidf+1)/2, xCon(ic).eidf/2);
equivalentScore(score>0) = swe_invNcdf(uncP(score>0)/2).^2;
% Z2(Z>0) = swe_invNcdf(0.5 - spm_Fcdf(Z,xCon(ic).eidf,xCon(ic).edf-xCon(ic).eidf+1)/2).^2;
end
uncP(score == 0) = 0;
equivalentScore(score == 0) = 0;
% transform into -log10(p-values) image
%Z = -log10(1-spm_Fcdf(Z,xCon(ic).eidf,xCon(ic).edf));
end
swe_progress_bar('Set',100);
end
end
luncP = -log10(uncP);
swe_progress_bar('Clear')
clear cCovBc cB tmp
%-Write SwE - statistic images & edf image if needed
%------------------------------------------------------------------
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...writing'); %-#
equivalentScore (equivalentScore > realmax('single')) = realmax('single');
equivalentScore (equivalentScore < -realmax('single')) = -realmax('single');
if isMat
xCon(ic).Vspm = sprintf('swe_%s_%c%cstat_c%02d%s',file_data_type,lower(eSTAT),xCon(ic).STAT,ic, file_ext);
save(xCon(ic).Vspm, 'equivalentScore');
else
xCon(ic).Vspm = swe_data_hdr_write(sprintf('swe_%s_%c%cstat_c%02d%s',file_data_type,lower(eSTAT),xCon(ic).STAT,ic,file_ext),...
SwE.xVol.DIM', SwE.xVol.M,...
sprintf('spm{%c} - contrast %d: %s',eSTAT,ic,xCon(ic).name),...
metadata);
tmp = zeros(SwE.xVol.DIM');
tmp(Q) = equivalentScore;
xCon(ic).Vspm = swe_data_write(xCon(ic).Vspm,tmp);
end
clear tmp equivalentScore
if isMat
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s',spm_str_manip(xCon(ic).Vspm,'t')));
else
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s',spm_str_manip(xCon(ic).Vspm.fname,'t')));
end
fprintf('\t%-32s: %30s',sprintf('spm{%c} image %2d',xCon(ic).STAT,ic),...
'...writing');
if isMat
xCon(ic).Vspm2 = sprintf('swe_%s_%cstat_c%02d%s',file_data_type,xCon(ic).STAT,ic,file_ext);
save(xCon(ic).Vspm2, 'score');
else
xCon(ic).Vspm2 = struct(...
'fname', sprintf('swe_%s_%cstat_c%02d%s',file_data_type,xCon(ic).STAT,ic,file_ext),...
'dim', SwE.xVol.DIM',...
'dt', [spm_type('float32'), spm_platform('bigend')],...
'mat', SwE.xVol.M,...
'pinfo', [1,0,0]',...
'descrip',sprintf('spm{%c} - contrast %d: %s',xCon(ic).STAT,ic,xCon(ic).name),...
metadata{:});
xCon(ic).Vspm2 = swe_data_hdr_write(sprintf('swe_%s_%cstat_c%02d%s',file_data_type,xCon(ic).STAT,ic,file_ext),...
SwE.xVol.DIM', SwE.xVol.M,...
sprintf('spm{%c} - contrast %d: %s',xCon(ic).STAT,ic,xCon(ic).name),...
metadata);
tmp = zeros(SwE.xVol.DIM');
tmp(Q) = score;
xCon(ic).Vspm2 = swe_data_write(xCon(ic).Vspm2,tmp);
end
clear tmp score
if isMat
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s',spm_str_manip(xCon(ic).Vspm2,'t'))); %-#
else
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s',spm_str_manip(xCon(ic).Vspm2.fname,'t'))); %-#
end
% save raw uncorrected p-values (new on 05/11/2017)
fprintf('\t%-32s: %30s',sprintf('-log10(uncP) image %2d',ic),...
'...writing');
if isMat
xCon(ic).VspmUncP = sprintf('swe_%s_%cstat_lp_c%02d%s',file_data_type,xCon(ic).STAT,ic,file_ext);
save(xCon(ic).VspmUncP, 'luncP');
else
xCon(ic).VspmUncP = struct(...
'fname', sprintf('swe_%s_%cstat_lp_c%02d%s',file_data_type,xCon(ic).STAT,ic,file_ext),...
'dim', SwE.xVol.DIM',...
'dt', [spm_type('float32'), spm_platform('bigend')],...
'mat', SwE.xVol.M,...
'pinfo', [1,0,0]',...
'descrip',sprintf('spm{%c} - contrast %d: %s','luncP',ic,xCon(ic).name),...
metadata{:});
xCon(ic).VspmUncP = swe_data_hdr_write(sprintf('swe_%s_%cstat_lp_c%02d%s',file_data_type,xCon(ic).STAT,ic,file_ext),...
SwE.xVol.DIM', SwE.xVol.M,...
sprintf('spm{%c} - contrast %d: %s','luncP',ic,xCon(ic).name),...
metadata);
tmp = zeros(SwE.xVol.DIM');
tmp(Q) = luncP;
xCon(ic).VspmUncP = swe_data_write(xCon(ic).VspmUncP,tmp);
end
clear tmp uncP luncP
if isMat
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s',spm_str_manip(xCon(ic).VspmUncP,'t')));
else
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s',spm_str_manip(xCon(ic).VspmUncP.fname,'t')));
end
fprintf('\t%-32s: %30s',sprintf('edf image %2d',ic),...
'...writing');
if dof_type
if isMat
xCon(ic).Vedf = sprintf('swe_%s_edf_c%02d%s',file_data_type,ic,file_ext);
save(xCon(ic).Vedf, 'edf');
else
xCon(ic).Vedf = struct(...
'fname', sprintf('swe_%s_edf_c%02d%s',file_data_type,ic,file_ext),...
'dim', SwE.xVol.DIM',...
'dt', [spm_type('float32'), spm_platform('bigend')],...
'mat', SwE.xVol.M,...
'pinfo', [1,0,0]',...
'descrip',sprintf('SwE effective degrees of freedom - %d: %s',ic,xCon(ic).name),...
metadata{:});
xCon(ic).Vedf = swe_data_hdr_write(sprintf('swe_%s_edf_c%02d%s',file_data_type,ic,file_ext),...
SwE.xVol.DIM', SwE.xVol.M,...
sprintf('SwE effective degrees of freedom - %d: %s',ic,xCon(ic).name),...
metadata);
tmp = zeros(SwE.xVol.DIM');
tmp(Q) = edf;
xCon(ic).Vedf = swe_data_write(xCon(ic).Vedf,tmp);
end
clear tmp edf
if isMat
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s',spm_str_manip(xCon(ic).Vedf,'t')))%-#
else
fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),sprintf(...
'...written %s',spm_str_manip(xCon(ic).Vedf.fname,'t')))%-#
end
end
end % if isempty(xCon(ic).Vspm)
end % (for i = 1:length(Ic))
spm('Pointer','Arrow')
% place xCon back in SwE
%--------------------------------------------------------------------------
SwE.xCon = xCon;
% Check if SwE has changed. Save only if it has.
%--------------------------------------------------------------------------
if isOctave
save('SwE.mat','SwE');
elseif spm_check_version('matlab','7') >=0
save('SwE','SwE','-V6');
else
save('SwE','SwE');
end