-
Notifications
You must be signed in to change notification settings - Fork 7
/
swe_getSPM.m
1538 lines (1378 loc) · 56.9 KB
/
swe_getSPM.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 [SwE,xSwE] = swe_getSPM(varargin)
% Compute specified and thresholded SwE parametric map for the SwE method.
% =========================================================================
% FORMAT [SwE,xSwE] = swe_getSPM;
% Query SwE in interactive mode.
%
% FORMAT [SwE,xSwE] = swe_getSPM(xSwE);
% -------------------------------------------------------------------------
%
% Query SwE in batch mode. See below for a description of fields that
% may be present in xSwE input. Values for missing fields will be
% queried interactively.
%
% xSwE - structure containing spm, distribution & filtering
% details
% .swd - SwE working directory - directory containing current
% SwE.mat
% .title - title for comparison (string)
% .Z - minimum of Statistics {filtered on u and k}
% .n - conjunction number <= number of contrasts
% .STAT - distribution {Z, T, X, F or P}
% .df - degrees of freedom [df{interest}, df{residual}]
% .STATstr - description string
% .Ic - indices of contrasts (in SwE.xCon)
% .Im - indices of masking contrasts (in xCon)
% .pm - p-value for masking (uncorrected)
% .Ex - flag for exclusive or inclusive masking
% .u - height threshold
% .k - extent threshold {voxels}
% .XYZ - location of voxels {voxel coords}
% .XYZmm - location of voxels {mm}
% .S - search Volume {voxels}
% .R - search Volume {resels}
% .FWHM - smoothness {voxels}
% .M - voxels -> mm matrix
% .iM - mm -> voxels matrix
% .VOX - voxel dimensions {mm} - column vector
% .DIM - image dimensions {voxels} - column vector
% .Vspm - Mapped statistic image(s)
% .Ps - uncorrected P values in searched volume (for voxel FDR)
% .Pp - uncorrected P values of peaks (for peak FDR)
% .Pc - uncorrected P values of cluster extents (for cluster FDR)
% .uc - 0.05 critical thresholds for FWEp, FDRp, FWEc, FDRc
% .thresDesc - description of height threshold (string)
%
% Required fields of SwE
%
% xVol - structure containing details of volume analysed
%
% xX - Design Matrix structure
% - (see spm_spm.m for structure)
%
% xCon - Contrast definitions structure array
% - (see also spm_FcUtil.m for structure, rules & handling)
% .name - Contrast name
% .STAT - Statistic indicator character ('T', 'F' or 'P')
% .c - Contrast weights (column vector contrasts)
% .X0 - Reduced design matrix data (spans design space under Ho)
% Stored as coordinates in the orthogonal basis of xX.X from spm_sp
% Extract using X0 = spm_FcUtil('X0',...
% .iX0 - Indicates how contrast was specified:
% If by columns for reduced design matrix then iX0 contains the
% column indices. Otherwise, it's a string containing the
% spm_FcUtil 'Set' action: Usually one of {'c','c+','X0'}
% .X1o - Remaining design space data (X1o is orthogonal to X0)
% Stored as coordinates in the orthogonal basis of xX.X from spm_sp
% Extract using X1o = spm_FcUtil('X1o',...
% .eidf - Effective interest degrees of freedom (numerator df)
% - Or effect-size threshold for Posterior probability
% .Vcon - Name of contrast (for 'T's) or ESS (for 'F's) image
% .Vspm - Name of SwE image
%
% Evaluated fields in xSwE (input)
%
% xSwE - structure containing SwE, distribution & filtering details
% .swd - SwE working directory - directory containing current SwE.mat
% .title - title for comparison (string)
% .Ic - indices of contrasts (in SwE.xCon)
% .n - conjunction number <= number of contrasts
% .Im - indices of masking contrasts (in xCon)
% .pm - p-value for masking (uncorrected)
% .Ex - flag for exclusive or inclusive masking
% .u - height threshold
% .k - extent threshold {voxels}
% .thresDesc - description of height threshold (string)
%
% In addition, the xCon structure is updated. For newly evaluated
% contrasts, SwE images (swe_{vox|dpx|dat}_{T|F}stat_c{c#}) are written, along
% with contrast (swe_{vox|dpx|dat}_beta_c{c#}) images.
%
% For a parametric analysis the following is added to the xCon
% structure:
%
% .Vspm - Name of SwE image
%
% For a non-parametric analysis the following are added to the xCon
% structure:
%
% .Vspm - Name of SwE image
% .VspmFWEP - Name of FWE P SwE image
% .VspmFDRP - Name of FDR P SwE image
% .VspmUncP - Name of Uncorrected P SwE image
% .VspmFWEP_clus - Name of FWE cluster P SwE image
% .Vedf - Name of error degrees of freedom image
%
% The contrast images are the weighted sum of the parameter images,
% where the weights are the contrast weights, and are uniquely
% estimable since contrasts are checked for estimability by the
% contrast manager. These contrast images (for appropriate contrasts)
% are suitable summary images of an effect at this level, and can be
% used as input at a higher level when effecting a random effects
% analysis. (Note that the swe_{vox|dpx|dat}_beta_c{c#} and
% swe_{vox|dpx|dat}_{T|F}stat_c{c#} images are not suitable input for a higher
% level analysis.) See spm_RandFX.man for further details.
%
%__________________________________________________________________________
%
% swe_getSPM prompts for an SwE parametric map and applies thresholds {u & k}
% to a point list of voxel values (specified with their locations {XYZ})
% This allows the SwE map be displayed and characterized in terms of regionally
% significant effects by subsequent routines.
%
% For general linear model Y = XB + E with data Y, design matrix X,
% parameter vector B, and (independent) errors E, a contrast c'B of the
% parameters (with contrast weights c) is estimated by c'b, where b are
% the parameter estimates given by b=pinv(X)*Y.
%
% For a paramertic analysis, either single contrasts can be examined
% or conjunctions of different contrasts. Contrasts are estimable
% linear combinations of the parameters, and are specified using
% the SwE contrast manager interface [swe_conman.m]. For a
% non-parametric analysis, two contrasts are recorded; activation
% and deactivation for the contrast vector specified in the batch
% window. These are recorded a priori in a seperate function
% with certain thresholds applied here [swe_contrasts_WB].
%
% SwE parametric maps are generated for the null hypotheses that
% the contrast is zero (or zero vector in the case of F-contrasts).
% See the help for the contrast manager [swe_conman.m] for a further
% details on contrasts and contrast specification.
%
% A conjunction assesses the conjoint expression of multiple effects. The
% conjunction SwE is the minimum of the component SPMs defined by the
% multiple contrasts. Inference on the minimum statistics can be
% performed in different ways. Inference on the Conjunction Null (one or
% more of the effects null) is accomplished by assessing the minimum as
% if it were a single statistic; one rejects the conjunction null in
% favor of the alternative that k=nc, that the number of active effects k
% is equal to the number of contrasts nc. No assumptions are needed on
% the dependence between the tests.
%
% Another approach is to make inference on the Global Null (all effects
% null). Rejecting the Global Null of no (u=0) effects real implies an
% alternative that k>0, that one or more effects are real. A third
% Intermediate approach, is to use a null hypothesis of no more than u
% effects are real. Rejecting the intermediate null that k<=u implies an
% alternative that k>u, that more than u of the effects are real.
%
% The Global and Intermediate nulls use results for minimum fields which
% require the SPMs to be identically distributed and independent. Thus,
% all component SwE maps must be either SwE{t}'s, or SwE{F}'s with the same
% degrees of freedom. Independence is roughly guaranteed for large
% degrees of freedom (and independent data) by ensuring that the
% contrasts are "orthogonal". Note that it is *not* the contrast weight
% vectors per se that are required to be orthogonal, but the subspaces of
% the data space implied by the null hypotheses defined by the contrasts
% (c'pinv(X)). Furthermore, this assumes that the errors are
% i.i.d. (i.e. the estimates are maximum likelihood or Gauss-Markov. This
% is the default in spm_spm).
%
% To ensure approximate independence of the component SwE maps in the case of
% the global or intermediate null, non-orthogonal contrasts are serially
% orthogonalised in the order specified, possibly generating new
% contrasts, such that the second is orthogonal to the first, the third
% to the first two, and so on. Note that significant inference on the
% global null only allows one to conclude that one or more of the effects
% are real. Significant inference on the conjunction null allows one to
% conclude that all of the effects are real.
%
% Masking simply eliminates voxels from the current contrast if they
% do not survive an uncorrected p value (based on height) in one or
% more further contrasts. No account is taken of this masking in the
% statistical inference pertaining to the masked contrast.
%
% The SwE map is subject to thresholding on the basis of height (u) and the
% number of voxels comprising its clusters {k}. The height threshold is
% specified as above in terms of an [un]corrected p value or
% statistic. Clusters can also be thresholded on the basis of their
% spatial extent. If you want to see all voxels simply enter 0. In this
% instance the 'set-level' inference can be considered an 'omnibus test'
% based on the number of clusters that obtain.
%
% BAYESIAN INFERENCE AND PPMS - POSTERIOR PROBABILITY MAPS
%
% If conditional estimates are available (and your contrast is a T
% contrast) then you are asked whether the inference should be 'Bayesian'
% or 'classical' (using GRF). If you choose Bayesian the contrasts are of
% conditional (i.e. MAP) estimators and the inference image is a
% posterior probability map (PPM). PPMs encode the probability that the
% contrast exceeds a specified threshold. This threshold is stored in
% the xCon.eidf. Subsequent plotting and tables will use the conditional
% estimates and associated posterior or conditional probabilities.
%
% see swe_results_ui.m for further details of the SwE results section.
% see also swe_contrasts.m
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Modified version of spm_getSPM
% Written by B. Guillaume
% Version Info: $Format:%ci$ $Format:%h$
%-GUI setup
%--------------------------------------------------------------------------
%spm_help('!ContextHelp',mfilename)
spm('Pointer','Arrow')
%-Select SwE.mat & note SwE results directory
%--------------------------------------------------------------------------
if nargin
xSwE = varargin{1};
end
try
swd = xSwE.swd;
catch
swd = '.';
end
%-Preliminaries...
%==========================================================================
%-Load SwE.mat
%--------------------------------------------------------------------------
try
load(fullfile(swd,'SwE.mat'));
catch
error(['Cannot read ' fullfile(swd,'SwE.mat')]);
end
SwE.swd = swd;
%-Change directory so that relative filenames are valid
%--------------------------------------------------------------------------
cd(SwE.swd);
%-Check the model has been estimated
%--------------------------------------------------------------------------
try
SwE.xVol.XYZ;
catch
%-Check the model has been estimated
%----------------------------------------------------------------------
error( 'This model has not been estimated.');
end
% check format of data
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');
if isCifti
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_data_type = 'dpx';
else
file_data_type = 'vox';
end
end
xX = SwE.xX; %-Design definition structure
XYZ = SwE.xVol.XYZ; %-XYZ coordinates
S = SwE.xVol.S; %-search Volume {voxels}
% R = SwE.xVol.R; %-search Volume {resels}
if isMat
M = SwE.xVol.M;
VOX = [];
clear xSwE
else
M = SwE.xVol.M(1:3,1:3); %-voxels to mm matrix
VOX = sqrt(diag(M'*M))'; %-voxel dimensions
end
% Tolerance for comparing real numbers for WB analyses
% Use a value < to the smallest WB p-value as it will be used to include WB p-values equal to alpha
if isfield(SwE, 'WB')
tol = 0.1 / (SwE.WB.nB + 1);
end
% check the data and other files have valid filenames
%--------------------------------------------------------------------------
%something here occurs and the paths to spm and swe toolboxes disappear????
% try, SwE.xY.VY = spm_check_filename(SwE.xY.VY); end
% try, SwE.xVol.VRpv = spm_check_filename(SwE.xVol.VRpv); end
% try, SwE.Vbeta = spm_check_filename(SwE.Vbeta); end
% try, SwE.Vcov_vis = spm_check_filename(SwE.Vcov_vis); end
% try, SwE.Vcov_beta = spm_check_filename(SwE.Vcov_beta); end
% try, SwE.Vcov_beta_g = spm_check_filename(SwE.Vcov_beta_g); end %here seems the problem
% try, SwE.VM = spm_check_filename(SwE.VM); end
%==========================================================================
% - C O N T R A S T S , S P M C O M P U T A T I O N , M A S K I N G
%==========================================================================
%-Get contrasts
%--------------------------------------------------------------------------
try
xCon = SwE.xCon;
% check if the Uncorrected p-value image is correctly set to the non-parametric version for WB (for retro-compatibility)
if isfield(SwE, 'WB') && ~exist('OCTAVE_VERSION','builtin') && ~contains(xCon(1).VspmUncP.fname, '-WB')
for i = 1:numel(xCon)
SwE.xCon(i).VspmUncP = spm_vol(sprintf('swe_%s_%cstat_lp%s_c%.2d%s', file_data_type, SwE.WB.stat, '-WB', i, file_ext));
end
% save the modified SwE.mat
if spm_check_version('matlab','7') >=0
save('SwE.mat', 'SwE', '-V6');
else
save('SwE.mat', 'SwE');
end
xCon = SwE.xCon;
end
catch
if isfield(SwE, 'WB') && ~exist('OCTAVE_VERSION','builtin')
SwE = swe_contrasts_WB(SwE);
% save SwE with xCon appended to it. This is important for future call of swe_getSPM for a specific Ic
if spm_check_version('matlab','7') >=0
save('SwE.mat', 'SwE', '-V6');
else
save('SwE.mat', 'SwE');
end
xCon = SwE.xCon;
else
xCon = {};
end
end
try
Ic = xSwE.Ic;
catch
% If we're not doing wild bootstrap and not in octave, ask for a contrast.
if ~isfield(SwE, 'WB') && ~exist('OCTAVE_VERSION','builtin')
[Ic,xCon] = swe_conman(SwE,'T&F',Inf,...
' Select contrasts...',' for conjunction',1);
% If we're in octave, assume we already have a contrast.
elseif exist('OCTAVE_VERSION','builtin')
Ic = 1;
% If we're doing WB, we already have a contrast. We just need to record it.
else
if numel(xCon) == 2
Ic = spm_input('Contrast Type','+1','b','Activation|Deactivation',[1,2],1);
else
Ic = 1;
end
end
end
if isempty(xCon)
% figure out whether new contrasts were defined, but not selected
% do this by comparing length of SwE.xCon to xCon, remember added
% indices to run spm_contrasts on them as well
try
noxCon = numel(SwE.xCon);
catch
noxCon = 0;
end
IcAdd = (noxCon+1):numel(xCon);
else
IcAdd = [];
end
nc = length(Ic); % Number of contrasts
%-Allow user to extend the null hypothesis for conjunctions
%
% n: conjunction number
% u: Null hyp is k<=u effects real; Alt hyp is k>u effects real
% (NB Here u is from Friston et al 2004 paper, not statistic thresh).
% u n
% Conjunction Null nc-1 1 | u = nc-n
% Intermediate 1..nc-2 nc-u | #effects under null <= u
% Global Null 0 nc | #effects under alt > u, >= u+1
%----------------------------------+---------------------------------------
if nc > 1
try
n = xSwE.n;
catch
if nc==2
But = 'Conjunction|Global'; Val=[1 nc];
else
But = 'Conj''n|Intermed|Global'; Val=[1 NaN nc];
end
n = spm_input('Null hyp. to assess?','+1','b',But,Val,1);
if isnan(n)
if nc == 3
n = nc - 1;
else
n = nc - spm_input('Effects under null ','0','n1','1',nc-1);
end
end
end
else
n = 1;
end
% not sure we want to do that with the SwE (commented for now)
%-Enforce orthogonality of multiple contrasts for conjunction
% (Orthogonality within subspace spanned by contrasts)
%--------------------------------------------------------------------------
% if nc > 1 && n > 1 && ~spm_FcUtil('|_?',xCon(Ic), xX.xKXs)
%
% OrthWarn = 0;
%
% %-Successively orthogonalise
% %-NB: This loop is peculiarly controlled to account for the
% % possibility that Ic may shrink if some contrasts disappear
% % on orthogonalisation (i.e. if there are colinearities)
% %----------------------------------------------------------------------
% i = 1;
% while(i < nc), i = i + 1;
%
% %-Orthogonalise (subspace spanned by) contrast i w.r.t. previous
% %------------------------------------------------------------------
% oxCon = spm_FcUtil('|_',xCon(Ic(i)), xX.xKXs, xCon(Ic(1:i-1)));
%
% %-See if this orthogonalised contrast has already been entered
% % or is colinear with a previous one. Define a new contrast if
% % neither is the case.
% %------------------------------------------------------------------
% d = spm_FcUtil('In',oxCon,xX.xKXs,xCon);
%
% if spm_FcUtil('0|[]',oxCon,xX.xKXs)
%
% %-Contrast was colinear with a previous one - drop it
% %--------------------------------------------------------------
% Ic(i) = [];
% i = i - 1;
%
% elseif any(d)
%
% %-Contrast unchanged or already defined - note index
% %--------------------------------------------------------------
% Ic(i) = min(d);
%
% else
%
% %-Define orthogonalised contrast as new contrast
% %--------------------------------------------------------------
% OrthWarn = OrthWarn + 1;
% conlst = sprintf('%d,',Ic(1:i-1));
% oxCon.name = sprintf('%s (orth. w.r.t {%s})', xCon(Ic(i)).name,...
% conlst(1:end-1));
% xCon = [xCon, oxCon];
% Ic(i) = length(xCon);
% end
%
% end % while...
%
% if OrthWarn
% warning('SwE:ConChange','%d contrasts orthogonalized',OrthWarn)
% end
%
% SwE.xCon = xCon;
% end % if nc>1...
SwE.xCon = xCon;
%-Apply masking
%--------------------------------------------------------------------------
try
Mask = ~isempty(xSwE.Im) * (isnumeric(xSwE.Im) + 2*iscellstr(xSwE.Im));
catch
% Mask = spm_input('mask with other contrast(s)','+1','y/n',[1,0],2);
if isMat
% no masking optionfor mat format
Mask = 0;
elseif isfield(SwE, 'WB')
% for now, the post-hoc masking is disabled for the WB
% It may be added later.
Mask = 0;
% Mask = spm_input('apply masking','+1','b','none|image',[0,2],1);
else
Mask = spm_input('apply masking','+1','b','none|contrast|image',[0,1,2],1);
end
end
if Mask == 1
%-Get contrasts for masking
%----------------------------------------------------------------------
try
Im = xSwE.Im;
catch
[Im,xCon] = swe_conman(SwE,'T&F',-Inf,...
'Select contrasts for masking...',' for masking',1);
end
%-Threshold for mask (uncorrected p-value)
%----------------------------------------------------------------------
try
pm = xSwE.pm;
catch
pm = spm_input('uncorrected mask p-value','+1','r',0.05,1,[0,1]);
end
%-Inclusive or exclusive masking
%----------------------------------------------------------------------
try
Ex = xSwE.Ex;
catch
Ex = spm_input('nature of mask','+1','b','inclusive|exclusive',[0,1],1);
end
elseif Mask == 2
%-Get mask images
%----------------------------------------------------------------------
try
Im = xSwE.Im;
catch
if isMat
Im = cellstr(spm_select([1 Inf],'mat','Select mask image(s)'));
else
[Im, sts] = spm_select([1 Inf],{'image','mesh'},'Select mask image(s)');
if ~sts, Im = []; else Im = cellstr(Im); end
end
end
%-Inclusive or exclusive masking
%----------------------------------------------------------------------
try
Ex = xSwE.Ex;
catch
Ex = spm_input('nature of mask','+1','b','inclusive|exclusive',[0,1],1);
end
pm = [];
else
Im = [];
pm = [];
Ex = [];
end
%-Create/Get title string for comparison
%--------------------------------------------------------------------------
if isMat
titlestr = xCon(Ic).name;
else
if nc == 1
str = xCon(Ic).name;
else
str = [sprintf('contrasts {%d',Ic(1)),sprintf(',%d',Ic(2:end)),'}'];
if n == nc
str = [str ' (global null)'];
elseif n == 1
str = [str ' (conj. null)'];
else
str = [str sprintf(' (Ha: k>=%d)',(nc-n)+1)];
end
end
if Ex
mstr = 'masked [excl.] by';
else
mstr = 'masked [incl.] by';
end
if isnumeric(Im)
if length(Im) == 1
str = sprintf('%s (%s %s at p=%g)',str,mstr,xCon(Im).name,pm);
elseif ~isempty(Im)
str = [sprintf('%s (%s {%d',str,mstr,Im(1)),...
sprintf(',%d',Im(2:end)),...
sprintf('} at p=%g)',pm)];
end
elseif iscellstr(Im) && numel(Im) > 0
[pf,nf,ef] = spm_fileparts(Im{1});
str = sprintf('%s (%s %s',str,mstr,[nf ef]);
for i=2:numel(Im)
[pf,nf,ef] = spm_fileparts(Im{i});
str =[str sprintf(', %s',[nf ef])];
end
str = [str ')'];
end
end
if ~isMat
try
titlestr = xSwE.title;
if isempty(titlestr)
titlestr = str;
end
catch
titlestr = spm_input('title for comparison','+1','s',str);
end
end
if ~isMat
% Ask whether to do additional voxelwise or clusterwise inference.
try
infType = xSwE.infType;
catch
if isfield(SwE, 'WB')
if isfield(SwE.WB, 'TFCE')
infType = spm_input('inference type','+1','b','voxelwise|clusterwise|TFCE',[0,1,2],3);
else
infType = spm_input('inference type','+1','b','voxelwise|clusterwise',[0,1],2);
end
else
infType = spm_input('inference type','+1','b','voxelwise|clusterwise',[0,1],1);
end
end
if isfield(SwE, 'WB')
% Work out the original form of inference performed. This will tell us
% which maps have already been generated. Most importantly, whether we
% can do FWE p value clusterwise inference.
if SwE.WB.voxelWise
orig_infType = 'vox';
elseif SwE.WB.clusterWise
orig_infType = 'clus';
else
orig_infType = 'tfce';
end
end
end
%-Compute & store contrast parameters, contrast/ESS images, & SwE images
%==========================================================================
SwE.xCon = xCon;
alreadyComputed = all(~cellfun(@isempty,{xCon(Ic).Vspm}));
if isnumeric(Im)
SwE = swe_contrasts(SwE, unique([Ic, Im, IcAdd]));
else
SwE = swe_contrasts(SwE, unique([Ic, IcAdd]));
end
xCon = SwE.xCon;
STAT = xCon(Ic(1)).STAT;
VspmSv = cat(1,xCon(Ic).Vspm);
%-Check conjunctions - Must be same STAT w/ same df
%--------------------------------------------------------------------------
if (nc > 1) && (any(diff(double(cat(1,xCon(Ic).STAT)))) || ...
any(abs(diff(cat(1,xCon(Ic).eidf))) > 1))
error('illegal conjunction: can only conjoin SPMs of same STAT & df');
end
%-Degrees of Freedom and STAT string describing marginal distribution
%--------------------------------------------------------------------------
%dFWHM=SwE.xVol.FWHM * SwE.xVol.M(1:3,1:3);
%df=[xCon(Ic(1)).eidf, (SwE.Subj.nSubj-SwE.xX.pB)*((1+2*(SwE.vFWHM(1)/dFWHM(1))^2)*(1+2*(SwE.vFWHM(2)/dFWHM(2))^2)*(1+2*(SwE.vFWHM(3)/dFWHM(3))^2))^0.5-xCon(Ic(1)).eidf+1]; %df=[xCon(Ic(1)).eidf nSubj-pB-xCon(Ic(1)).eidf-1]; %df = [xCon(Ic(1)).eidf xX.erdf];
if nc > 1
if n > 1
str = sprintf('^{%d \\{Ha:k\\geq%d\\}}',nc,(nc-n)+1);
else
str = sprintf('^{%d \\{Ha:k=%d\\}}',nc,(nc-n)+1);
end
else
str = '';
end
% We display the equivalent statistics.
switch STAT
case 'T'
STATstr = sprintf('%c','Z',str);
case 'F'
STATstr = sprintf('%c','X',str);
end
%-Compute (unfiltered) spm pointlist for masked conjunction requested
%==========================================================================
fprintf('\t%-32s: %30s','SPM computation','...initialising') %-#
%-Compute conjunction as minimum of SPMs
%--------------------------------------------------------------------------
Z = Inf;
for i = Ic
if isMat
load(xCon(i).Vspm);
Z = min(Z,equivalentScore);
clear equivalentScore
else
Z = min(Z, swe_data_read(xCon(i).Vspm, 'xyz', XYZ));
end
end
%-Copy of Z and XYZ before masking, for later use with FDR
%--------------------------------------------------------------------------
XYZum = XYZ;
Zum = Z;
%-Compute mask and eliminate masked voxels
%--------------------------------------------------------------------------
for i = 1:numel(Im)
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...masking') %-#
if isnumeric(Im)
if isMat
load(xCon(Im(i)).Vspm);
Mask = equivalentScore;
clear equivalentScore
else
Mask = swe_data_read(xCon(Im(i)).Vspm, 'xyz', XYZ);
end
switch xCon(Im(i)).STAT
case 'T'
um = swe_invNcdf(1-pm);
case 'F'
um = spm_invXcdf(1-pm,1);
end
if Ex
Q = Mask <= um;
else
Q = Mask > um;
end
else
if isMat
Mask = importdata(Im{i});
else
v = swe_data_hdr_read(Im{i});
Mask = swe_data_read(v, 'xyz', v.mat\SwE.xVol.M*[XYZ; ones(1,size(XYZ,2))]);
end
Q = Mask ~= 0 & ~isnan(Mask);
if Ex, Q = ~Q; end
end
if ~isMat
XYZ = XYZ(:,Q);
end
Z = Z(Q);
if isempty(Q)
fprintf('\n') %-#
warning('SwE:NoVoxels','No voxels survive masking at p=%4.2f',pm);
break
end
end
%==========================================================================
% - H E I G H T & E X T E N T T H R E S H O L D S
%==========================================================================
if ~isMat
u = -Inf; % height threshold
k = 0; % extent threshold {voxels}
clustWise = 'None';% Type of clusterwise inference to be performed
if spm_mesh_detect(xCon(Ic(1)).Vspm)
G = export(gifti(SwE.xVol.G),'patch');
end
%-Height threshold - classical inference
%--------------------------------------------------------------------------
if STAT ~= 'P'
% Get the equivalent statistic
switch STAT
case 'T'
eSTAT = 'Z';
case 'F'
eSTAT = 'X';
end
% If we are doing voxelwise inference on a parametric.
if ~isfield(SwE, 'WB') && infType == 0
%-Get height threshold
%----------------------------------------------------------------
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...height threshold') %-#
try
thresDesc = xSwE.thresDesc;
catch
% For non WB we only have FDR.
str = 'FDR|none';
thresDesc = spm_input('p value adjustment to control','+1','b',str,[],1);
end
switch thresDesc
case 'FDR' % False discovery rate
%--------------------------------------------------------
try
u = xSwE.u;
catch
u = spm_input('p value (FDR)','+0','r',0.05,1,[0,1]);
end
thresDesc = ['p<' num2str(u) ' (' thresDesc ')'];
switch STAT
case 'T'
u = swe_uc_FDR(u,Inf,'Z',n,VspmSv,0);
case 'F'
u = swe_uc_FDR(u,[1 1],'X',n,VspmSv,0);
end
case 'none' % No adjustment: p for conjunctions is p of the conjunction SwE
%--------------------------------------------------------
try
u = xSwE.u;
catch
u = spm_input(['threshold {',eSTAT,' or p value}'],'+0','r',0.001,1);
end
if u <= 1
thresDesc = ['p<' num2str(u) ' (unc.)'];
switch STAT
case 'T'
u = swe_invNcdf(1-u^(1/n));
case 'F'
u = spm_invXcdf(1-u^(1/n),1);
end
else
'';
end
otherwise
%--------------------------------------------------------------
fprintf('\n'); %-#
error('Unknown control method "%s".',thresDesc);
end % switch thresDesc
%-Compute p-values for topological and voxel-wise FDR (all search voxels)
%----------------------------------------------------------------
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...for voxelFDR') %-#
switch STAT
case 'T'
Ps = (1-spm_Ncdf(Zum)).^n;
case 'F'
Ps = (1-spm_Xcdf(Zum,1)).^n;
end
up = NaN;
Pp = NaN;
uc = NaN;
ue = NaN;
Pc = [];
uu = [];
Q = find(Z > u);
% If we are doing clusterwise inference on a parametric.
elseif ~isfield(SwE, 'WB') && infType == 1
% Record what type of clusterwise inference we are doing.
clustWise = 'Uncorr';
% No adjustment: p for conjunctions is p of the conjunction SwE
%--------------------------------------------------------------
try
u = xSwE.u;
catch
u = spm_input(['threshold {',eSTAT,' or p value}'],'+1','r',0.001,1);
end
if u <= 1
thresDesc = ['p<' num2str(u) ' (unc.)'];
switch STAT
case 'T'
u = swe_invNcdf(1-u^(1/n));
case 'F'
u = spm_invXcdf(1-u^(1/n),1);
end
else
thresDesc = '';
end
%-Compute p-values for topological and voxel-wise FDR (all search voxels)
%----------------------------------------------------------------
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...for voxelFDR') %-#
switch STAT
case 'T'
Ps = (1-spm_Ncdf(Zum)).^n;
case 'F'
Ps = (1-spm_Xcdf(Zum,1)).^n;
end
up = NaN;
Pp = NaN;
uc = NaN;
ue = NaN;
Pc = [];
uu = [];
Q = find(Z > u);
% If we are doing voxelwise inference on a WB.
elseif isfield(SwE, 'WB') && infType == 0
%-Get height threshold
%----------------------------------------------------------------------
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...height threshold') %-#
try
thresDesc = xSwE.thresDesc;
catch
% For WB we have FWE or FDR.
str = 'FWE|FDR|none';
thresDesc = spm_input('p value adjustment to control','+1','b',str,[],1);
end
switch thresDesc
case 'FWE' % Family-wise false positive rate
% This is performed on the voxelwise FWE P value map
%--------------------------------------------------------
try
pu = xSwE.u;
catch
pu = spm_input('p value (FWE)','+0','r',0.05,1,[0,1]);
end
thresDesc = ['p<=' num2str(pu) ' (' thresDesc ')'];
FWE_ps = 10.^-swe_data_read(xCon(Ic).VspmFWEP,'xyz', XYZ);
% When thresholding on WB FWER p-values, we should include those = to pu
% Here, we are using a - tol < b instead of a <= b due to numerical errors
% tol was set to 0.1/(nB+1) in order to make sure it is smaller than the smallest WB p-value
Q = find(FWE_ps - tol < pu);
% Obtain the exclusive statistic threshold. This will be the (1-pu)th
% percentile of the max. statistic distribution
if Ic == 1
maxScore = sort(SwE.WB.maxScore);
elseif Ic == 2
maxScore = sort(-SwE.WB.minScore);
else
error('Unknown contrast');
end
u = maxScore( ceil( (1-pu) * (SwE.WB.nB+1) ) );
case 'FDR' % False discovery rate
% This is performed on the FDR P value map
%--------------------------------------------------------
try
pu = xSwE.u;
catch
pu = spm_input('p value (FDR)','+0','r',0.05,1,[0,1]);
end
thresDesc = ['p<=' num2str(pu) ' (' thresDesc ')'];
% select the WB FDR p-values within the mask
FDR_ps = 10.^-swe_data_read(xCon(Ic).VspmFDRP, 'xyz', XYZ);
% Here, a parametric score threshold u would differ from voxel to voxel
% Thus, setting it to NaN
u = NaN;
% inclusive thresholding for WB
Q = find(FDR_ps - tol < pu);
case 'none' % No adjustment: p for conjunctions is p of the conjunction SwE
% This should be performed on the uncorrected WB p-values
%--------------------------------------------------------
try
pu = xSwE.u;
catch
pu = spm_input(['threshold {p value}'],'+0','r',0.001,1,[0,1]);
end
thresDesc = ['p<=' num2str(pu) ' (unc.)'];
% select the WB unc. p-values within the mask
unc_ps = 10.^-swe_data_read(xCon(Ic).VspmUncP, 'xyz', XYZ);
% Here, a parametric score threshold u would differ from voxel to voxel
% Thus, setting it to NaN
u = NaN
% inclusive thresholding for WB
Q = find(unc_ps - tol < pu);
otherwise
%--------------------------------------------------------------
fprintf('\n'); %-#
error('Unknown control method "%s".',thresDesc);
end
up = NaN;
Pp = NaN;
uc = NaN;
ue = NaN;
Pc = [];
uu = [];
% If we are doing clusterwise WB.
elseif isfield(SwE, 'WB') && infType == 1
fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...height threshold') %-#