-
Notifications
You must be signed in to change notification settings - Fork 1
/
ITEM_est_1st_lvl.m
389 lines (339 loc) · 14.1 KB
/
ITEM_est_1st_lvl.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
function ITEM_est_1st_lvl(SPM, mode, nind, tmap)
% _
% Estimate First-Level (Scan-Wise) Model
% FORMAT ITEM_est_1st_lvl(SPM, mode, nind, tmap)
% SPM - a structure specifying an estimated GLM
% mode - a string indicating how to handle filter regressors
% nind - a vector indexing non-trial-wise nuisance conditions
% tmap - a logical indicating calculation of trial-wise t-maps
%
% FORMAT ITEM_est_1st_lvl(SPM, mode, nind, tmap) calculates trial-wise
% parameter estimates for a first-level (scan-wise) GLM which uses a
% trial-wise design matrix with one HRF regressor per trial, plus some
% augmentation to handle nuisance conditions and regressors of no interest.
%
% Author: Joram Soch, BCCN Berlin
% E-Mail: joram.soch@bccn-berlin.de
%
% First edit: 22/11/2018, 08:20 (V0.1)
% Last edit: 20/12/2019, 15:10 (V0.2)
%=========================================================================%
% P R E P A R A T I O N %
%=========================================================================%
% Get SPM.mat if necessary
%-------------------------------------------------------------------------%
if nargin == 0
SPM_mat = spm_select(1,'^SPM\.mat$','Select SPM.mat!');
SPM_dir = fileparts(SPM_mat); load(SPM_mat);
SPM.swd = SPM_dir;
ITEM_est_1st_lvl(SPM);
return
end;
% Estimate model if necessary
%-------------------------------------------------------------------------%
if ~isfield(SPM.xVi,'V')
SPM_mat = strcat(SPM.swd,'/','SPM.mat');
MA_GLM_AR_only(SPM_mat); load(SPM_mat);
ITEM_est_1st_lvl(SPM);
return
end;
% Set estimation mode if necessary
%-------------------------------------------------------------------------%
if nargin < 2 || isempty(mode), mode = 'KXY'; end;
% Set nuisance conditions if necessary
%-------------------------------------------------------------------------%
if nargin < 3 || isempty(nind), nind = []; end;
% Set t-statistic maps if necessary
%-------------------------------------------------------------------------%
if nargin < 4 || isempty(tmap), tmap = false; end;
% Change to SPM.swd if specified
%-------------------------------------------------------------------------%
orig_dir = pwd;
try
cd(SPM.swd);
catch
SPM.swd = pwd;
end
% Get number of sessions
%-------------------------------------------------------------------------%
s = numel(SPM.Sess);
% Init progress bar
%-------------------------------------------------------------------------%
Finter = spm('FigName','ITEM_est_1st_lvl: load');
% Load mask image
%-------------------------------------------------------------------------%
[M m_dim m_ind] = MA_load_mask(SPM);
% Load time series
%-------------------------------------------------------------------------%
Y = MA_load_data(SPM, m_ind);
v = numel(m_ind);
%=========================================================================%
% E S T I M A T I O N ( 1 ) : T R I A L - W I S E D E S I G N %
%=========================================================================%
% Init progress bar
%-------------------------------------------------------------------------%
Finter = spm('FigName','ITEM_est_1st_lvl: estimate (1)');
% Preallocate GLM structure
%-------------------------------------------------------------------------%
GLM1.n = zeros(1,s); % number of scans per session
GLM1.p = zeros(1,s); % number of regressors of interest
GLM1.r = zeros(1,s); % number of regressors of no interest
GLM1.t = zeros(1,s); % number of trials per session
% Convert standard to first-level model
%-------------------------------------------------------------------------%
for h = 1:numel(SPM.Sess)
% Preallocate onsets and durations
%---------------------------------------------------------------------%
ind = [];
ons = [];
dur = [];
T = [];
p = 0;
c = 0;
n = numel(SPM.Sess(h).row);
if ~isempty(nind)
names0 = cell(1,numel(nind));
onsets0 = cell(1,numel(nind));
durations0 = cell(1,numel(nind));
pmod0 = struct([]);
end;
% Collect trials from conditions
%---------------------------------------------------------------------%
for i = 1:numel(SPM.Sess(h).U)
if ~ismember(i,nind)
ind = [ind; i*ones(numel(SPM.Sess(h).U(i).ons),1)];
ons = [ons; SPM.Sess(h).U(i).ons];
dur = [dur; SPM.Sess(h).U(i).dur];
T_i = ones(numel(SPM.Sess(h).U(i).ons),1);
if ~strcmp(SPM.Sess(h).U(i).P(1).name,'none')
for j = 1:numel(SPM.Sess(h).U(i).P)
P_j = SPM.Sess(h).U(i).P(j).P; % get modulator
P_j = P_j - mean(P_j); % mean-centering
P_j = P_j./ max(abs(P_j)); % normalization
T_i = [T_i, P_j];
end;
end;
T = blkdiag(T, T_i);
p = p + size(T_i,2);
else % non-trial-wise nuisance condition
c = c + 1;
names0{c} = strcat('N',num2str(c));
onsets0{c} = SPM.Sess(h).U(i).ons;
durations0{c} = SPM.Sess(h).U(i).dur;
if ~strcmp(SPM.Sess(h).U(i).P(1).name,'none')
for j = 1:numel(SPM.Sess(h).U(i).P)
pmod0(s).name{j} = SPM.Sess(h).U(i).P(j).name;
pmod0(s).param{j} = SPM.Sess(h).U(i).P(j).P;
end;
end;
end;
end;
clear T_i P_j
% Convert from scans to seconds
%---------------------------------------------------------------------%
if strcmp(SPM.xBF.UNITS,'scans')
ons = ons*SPM.xY.RT;
dur = dur*SPM.xY.RT;
end;
% Sort trials by onset time
%---------------------------------------------------------------------%
[os, is] = sort(ons);
ind = ind(is);
ons = ons(is);
dur = dur(is);
T = T(is,:);
t = size(T,1);
names = cell(1,t);
onsets = cell(1,t);
durations = cell(1,t);
% Get regressors of no interest
%---------------------------------------------------------------------%
if ~isempty(SPM.Sess(h).C) % movement parameters
R = SPM.Sess(h).C.C;
end;
% Get trial-wise design matrix
%---------------------------------------------------------------------%
settings.n = n;
settings.TR = SPM.xY.RT;
for k = 1:t
names{k} = strcat('trl-',MF_int2str0(k,2));
onsets{k} = ons(k);
durations{k} = dur(k);
end;
if ~isempty(nind)
[X, L] = ITEM_get_des_mat(names, onsets, durations, [], [], [], settings);
[Xc, Lc] = ITEM_get_des_mat(names0, onsets0, durations0, pmod0, [], R, settings);
X = [X, Xc];
L = [L, Lc];
r = size(Xc,2);
else
[X, L] = ITEM_get_des_mat(names, onsets, durations, [], [], R, settings);
r = size(R,2);
end;
% Add discrete cosine set
%---------------------------------------------------------------------%
if strcmp(mode,'DCT') % temporal filter
k = size(SPM.xX.K(h).X0,2);
for l = 1:k, L = [L, {strcat('K',num2str(l))}]; end;
X = [X, SPM.xX.K(h).X0];
r = r + k;
end;
% Add constant regressor
%---------------------------------------------------------------------%
L = [L, {'const.'}]; % implicit baseline
X = [X, ones(n,1)];
r = r + 1;
% Normalize to unit range
%---------------------------------------------------------------------%
X = X ./ repmat(max(abs(X)), [n 1]);
% Store session information
%---------------------------------------------------------------------%
GLM1.n(h) = n;
GLM1.p(h) = p;
GLM1.r(h) = r;
GLM1.t(h) = t;
GLM1.pr(h)= p+r;
GLM1.tr(h)= t+r;
% first-level (scan-wise) design matrix
GLM1.Sess(h).n = SPM.Sess(h).row;
GLM1.Sess(h).X = X;
GLM1.Sess(h).L = L;
GLM1.Sess(h).K = SPM.xX.K(h);
GLM1.Sess(h).V = zeros(GLM1.n(h),GLM1.n(h));
% second-level (trial-wise) design matrix
GLM1.Sess(h).t = sum(GLM1.tr(1:h-1))+[1:GLM1.tr(h)];
GLM1.Sess(h).T = blkdiag(T, eye(r)); % augmented
GLM1.Sess(h).U = zeros(GLM1.tr(h),GLM1.tr(h));
% By now, the following relation should hold:
% X [n x (p+r)] = X_t [n x (t+r)] * T [(t+r) x (p+r)].
GLM1.Sess(h).ind = ind;
GLM1.Sess(h).ons = ons;
GLM1.Sess(h).dur = dur;
GLM1.Sess(h).is = is;
end;
% Setup SPM for ReML
%-------------------------------------------------------------------------%
SPM_ReML.xY = SPM.xY;
SPM_ReML.xM = SPM.xM;
SPM_ReML.xX.X = blkdiag(GLM1.Sess.X);
if strcmp(mode,'DCT')
SPM_ReML.xX.K = 1;
end;
if strcmp(mode,'KXY')
SPM_ReML.xX.K = SPM.xX.K;
end;
SPM_ReML.xX.iB = cumsum(GLM1.tr);
SPM_ReML.xX.iG = [];
SPM_ReML.xVi.Vi = SPM.xVi.Vi;
% Estimate non-sphericity
%-------------------------------------------------------------------------%
SPM_ReML.xVi = spm_est_non_sphericity(SPM_ReML);
for h = 1:numel(SPM.Sess)
GLM1.Sess(h).V = SPM_ReML.xVi.V(SPM.Sess(h).row,SPM.Sess(h).row);
GLM1.Sess(h).U = (GLM1.Sess(h).X'*inv(GLM1.Sess(h).V)*GLM1.Sess(h).X)^(-1);
end;
clear SPM_ReML
%=========================================================================%
% E S T I M A T I O N ( 2 ) : T R I A L - W I S E E S T I M A T E S %
%=========================================================================%
% Init progress bar
%-------------------------------------------------------------------------%
Finter = spm('FigName','ITEM_est_1st_lvl: estimate (2)');
% Preallocate gamma estimates
%-------------------------------------------------------------------------%
G = NaN(sum(GLM1.tr),prod(m_dim));
S2 = NaN(numel(SPM.Sess),prod(m_dim));
if tmap
T = NaN(sum(GLM1.tr),prod(m_dim));
end;
% Estimate parameters of first-level model
%-------------------------------------------------------------------------%
for h = 1:numel(SPM.Sess)
% Get data and design
%---------------------------------------------------------------------%
Yh = Y(GLM1.Sess(h).n,:);
Xh = GLM1.Sess(h).X;
Vh = GLM1.Sess(h).V;
% Filter data and design
%---------------------------------------------------------------------%
if strcmp(mode,'KXY')
Xh = spm_filter(GLM1.Sess(h).K, Xh);
Yh = spm_filter(GLM1.Sess(h).K, Yh);
end;
% Estimate parameters
%---------------------------------------------------------------------%
[G(GLM1.Sess(h).t,m_ind), S2(h,m_ind)] = ITEM_GLM_MLE(Yh, Xh, Vh, sprintf('Estimate trial-wise response amplitudes for session %d',h));
% Calculate statistics
%---------------------------------------------------------------------%
if tmap
spm_progress_bar('Init', 100, sprintf('Estimate trial-wise t-statistics for session %d',h), '');
for k = 1:GLM1.tr(h)
c = zeros(GLM1.tr(h),1);
c(k) = 1; % contrast
c_cov_c = c'*GLM1.Sess(h).U*c; % denonimator
con_est = c'*G(GLM1.Sess(h).t,m_ind); % numerator
T(GLM1.Sess(h).t(k),m_ind) = con_est ./ sqrt(S2(h,m_ind) * c_cov_c);
spm_progress_bar('Set', (k/GLM1.tr(h))*100);
end;
spm_progress_bar('Clear');
end;
end;
%=========================================================================%
% S A V E R E S U L T S %
%=========================================================================%
% Init progress bar
%-------------------------------------------------------------------------%
Finter = spm('FigName','ITEM_est_1st_lvl: save');
spm_progress_bar('Init',100,'Save trial-wise parameter estimates...','');
% Initialise image files
%-------------------------------------------------------------------------%
H = MA_init_header(SPM, false);
GLM1.swd = strcat(SPM.swd,'/','ITEM_est_1st_lvl','/');
if ~exist(GLM1.swd,'dir'), mkdir(GLM1.swd); end;
cd(GLM1.swd);
% Save gamma estimates
%-------------------------------------------------------------------------%
d = ceil(sum(GLM1.tr)/100);
for h = 1:numel(SPM.Sess)
for k = 1:GLM1.tr(h)
i = GLM1.Sess(h).t(k);
H.fname = strcat('gamma_',MF_int2str0(i,4),'.nii');
H.descrip = sprintf('ITEM_est_1st_lvl: parameter estimate; session %d, trial %d', h, k);
spm_write_vol(H,reshape(G(i,:),m_dim));
GLM1.Vgamma(i) = H;
if mod(i,d) == 0, spm_progress_bar('Set',(i/sum(GLM1.tr))*100); end;
end;
end;
% Save sigma^2 estimates
%-------------------------------------------------------------------------%
for h = 1:numel(SPM.Sess)
H.fname = strcat('sigma_',MF_int2str0(h,4),'.nii');
H.descrip = sprintf('ITEM_est_1st_lvl: variance estimate; session %d', h);
spm_write_vol(H,reshape(S2(h,:),m_dim));
GLM1.Vsigma(h) = H;
spm_progress_bar('Set',(h/s)*100);
end;
% Save t-statistics
%-------------------------------------------------------------------------%
if tmap
d = ceil(sum(GLM1.tr)/100);
for h = 1:numel(SPM.Sess)
for k = 1:GLM1.tr(h)
i = GLM1.Sess(h).t(k);
H.fname = strcat('tstat_',MF_int2str0(i,4),'.nii');
H.descrip = sprintf('ITEM_est_1st_lvl: t-statistic; session %d, trial %d', h, k);
spm_write_vol(H,reshape(T(i,:),m_dim));
GLM1.Vtstat(i) = H;
if mod(i,d) == 0, spm_progress_bar('Set',(i/sum(GLM1.tr))*100); end;
end;
end;
end;
% Save GLM structure
%-------------------------------------------------------------------------%
save(strcat(GLM1.swd,'GLM1.mat'),'GLM1');
% Clear progress bar
%-------------------------------------------------------------------------%
spm_progress_bar('Clear');
% Return to origin
%-------------------------------------------------------------------------%
cd(orig_dir);