forked from JoramSoch/MACS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMA_GLM_AR_only.m
114 lines (99 loc) · 3.87 KB
/
MA_GLM_AR_only.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
function MA_GLM_AR_only(SPM_mat)
% _
% General Linear Model with AR Component Estimation only
% FORMAT MA_GLM_AR_only(SPM_mat)
% SPM_mat - a string indicating a specified GLM
%
% FORMAT MA_GLM_AR_only(SPM_mat) estimates a general linear model and
% saves only the auto-regressive components used for non-sphericity
% correction. These are estimated from the residual auto-correlations using
% a restricted maximum likelihood (ReML) approach and saved into SPM.xVi.V.
%
% Author: Joram Soch, BCCN Berlin
% E-Mail: joram.soch@bccn-berlin.de
%
% First edit: 18/08/2015, 04:10 (V0.3/V11)
% Last edit: 18/08/2015, 08:30 (V0.3/V11)
% Get SPM.mat if necessary
%-------------------------------------------------------------------------%
if nargin == 0
SPM_mat = spm_select(1,'^SPM\.mat$','Select SPM.mat!');
end;
% Load SPM.mat if available
%-------------------------------------------------------------------------%
orig_dir = pwd;
try
load(SPM_mat);
SPM.swd = fileparts(SPM_mat);
cd(SPM.swd);
catch
MA_GLM_AR_only;
return
end;
% If current version is SPM8
%-------------------------------------------------------------------------%
if strcmp(spm('Ver'),'SPM8')
% Estimate general linear model
%---------------------------------------------------------------------%
SPM = spm_spm(SPM);
% Delete beta images
%---------------------------------------------------------------------%
for i = 1:length(SPM.Vbeta)
filename = strcat(SPM.swd,'/',SPM.Vbeta(i).fname);
[SPM_dir, name, ext] = fileparts(filename);
if strcmp(ext,'.img') % SPM8 or earlier
spm_unlink(strcat(SPM.swd,'/',name,'.hdr'));
spm_unlink(strcat(SPM.swd,'/',name,'.img'));
end;
if strcmp(ext,'.nii') % SPM12 or later
spm_unlink(strcat(SPM.swd,'/',name,'.nii'));
end;
end;
% Delete contrast maps
%---------------------------------------------------------------------%
if ~isempty(SPM.xCon)
for i = 1:length(SPM.xCon)
for j = 1:2
if j == 1, filename = strcat(SPM.swd,'/',SPM.xCon(i).Vcon.fname); end;
if j == 2, filename = strcat(SPM.swd,'/',SPM.xCon(i).Vspm.fname); end;
[SPM_dir, name, ext] = fileparts(filename);
if strcmp(ext,'.img') % SPM8 or earlier
spm_unlink(strcat(SPM.swd,'/',name,'.hdr'));
spm_unlink(strcat(SPM.swd,'/',name,'.img'));
end;
if strcmp(ext,'.nii') % SPM12 or later
spm_unlink(strcat(SPM.swd,'/',name,'.nii'));
end;
end;
end;
end;
end;
% If current version is SPM12
%-------------------------------------------------------------------------%
if strcmp(spm('Ver'),'SPM12')
% Estimate non-sphericity
%---------------------------------------------------------------------%
[xVi, M] = spm_est_non_sphericity(SPM);
% Insert non-sphericity
%---------------------------------------------------------------------%
W = spm_sqrtm(spm_inv(xVi.V));
W = W.*(abs(W) > 1e-6);
SPM.xX.W = W;
SPM.xVi = xVi;
% Insert masking image
%---------------------------------------------------------------------%
H = SPM.xY.VY(1);
H.fname = 'mask.nii';
H.dt = [spm_type('uint8') spm_platform('bigend')];
H.pinfo = [1 0 0]';
H.descrip = 'MA_GLM_AR_only: masking image';
H = spm_data_hdr_write(H);
spm_write_vol(H,double(M));
SPM.VM = H;
% Save SPM.mat
%---------------------------------------------------------------------%
save(SPM_mat,'SPM');
end;
% Return to origin
%-------------------------------------------------------------------------%
cd(orig_dir);