-
Notifications
You must be signed in to change notification settings - Fork 15
/
rsHRF_estimation_temporal_basis.m
executable file
·391 lines (345 loc) · 11.4 KB
/
rsHRF_estimation_temporal_basis.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
function [beta_hrf, bf, event_bold] = rsHRF_estimation_temporal_basis(data,xBF,temporal_mask,flag_parfor)
% xBF.TR = 2;
% xBF.T = 8;
% xBF.T0 = fix(xBF.T/2); (reference time bin, see slice timing)
% xBF.dt = xBF.TR/xBF.T;
% xBF.AR_lag = 1;
% xBF.thr = 1;
% xBF.len = 25;
% xBF.localK = 2;
% temporal_mask: scrubbing mask.
% By: Guo-Rong Wu (gronwu@gmail.com).
% Faculty of Psychology, Southwest University.
% History:
% - 2015-04-17 - Initial version.
% - 2018-07-18 - fix Warning: Rank deficient, copy code from regress.m
% addd AR_lag=0.
% set paramater for local peak detection.
% - 2019-08-01 - add temporal basis set (Fourier Set, Gamma function)
if nargin<4
flag_parfor = 1;
end
[N, nvar] = size(data);
if isnan(xBF.order)
para = rsHRF_global_para;
xBF.order = para.num_basis;
end
bf = wgr_spm_get_bf(xBF);
warning('off','all')
fprintf('#%d \n',nvar)
beta_hrf = cell(1,nvar);
event_bold= cell(1,nvar);
if flag_parfor
parfor i=1:nvar
[beta_hrf{1,i}, event_bold{i}] =wgr_hrf_estimation(data(:,i),xBF,N,bf,temporal_mask);
end
else
for i=1:nvar
[beta_hrf{1,i}, event_bold{i}] =wgr_hrf_estimation(data(:,i),xBF,N,bf,temporal_mask);
end
end
beta_hrf =cell2mat(beta_hrf);
warning on
return
function [bf] = wgr_spm_get_bf(xBF)
% Fill in basis function structure
% FORMAT [xBF] = spm_get_bf(xBF)
%
% xBF.dt - time bin length {seconds}
% xBF.name - description of basis functions specified
% 'Canonical HRF (with time derivative)'
% 'Canonical HRF (with time and dispersion derivatives)'
% 'Gamma functions'
% 'Fourier set'
% 'Fourier Set (Hanning)'
% xBF.T - microtime resolution (for 'Canonical HRF*')
% bf - array of basis functions
%__________________________________________________________________________
%
% spm_get_bf prompts for basis functions to model event or epoch-related
% responses. The basis functions returned are unitary and orthonormal
% when defined as a function of peri-stimulus time in time-bins.
% It is at this point that the distinction between event and epoch-related
% responses enters.
%__________________________________________________________________________
% Copyright (C) 1999-2011 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_get_bf.m 4473 2011-09-08 18:07:45Z guillaume $
% Edited by Guorong Wu. 2019-08-02
%-Length of time bin
%--------------------------------------------------------------------------
dt = xBF.dt; %'time bin for basis functions {secs}';
l = xBF.len;
h = xBF.order;
%-Create basis functions
%==========================================================================
%-Microtime resolution
%----------------------------------------------------------------------
fMRI_T = xBF.T;
%-Create basis functions
%==========================================================================
fprintf('Basis function: %s\n',xBF.name)
switch xBF.name
case {'Fourier set','Fourier set (Hanning)'}
%----------------------------------------------------------------------
pst = [0:dt:l]';
pst = pst/max(pst);
%-Hanning window
%----------------------------------------------------------------------
if strcmp(xBF.name,'Fourier set (Hanning)')
g = (1 - cos(2*pi*pst))/2;
else
g = ones(size(pst));
end
%-Zeroth and higher Fourier terms
%----------------------------------------------------------------------
bf = g;
for i = 1:h
bf = [bf g.*sin(i*2*pi*pst)];
bf = [bf g.*cos(i*2*pi*pst)];
end
case {'Gamma functions'}
%----------------------------------------------------------------------
pst = [0:dt:l]';
bf = spm_gamma_bf(pst,h);
otherwise
%-Canonical hemodynamic response function
%----------------------------------------------------------------------
[bf, p] = spm_hrf(dt,[],fMRI_T);
p(end) = xBF.len;
[bf, p] = spm_hrf(dt,p,fMRI_T);
%-Add time derivative
%----------------------------------------------------------------------
if strfind(xBF.name,'time')
dp = 1;
p(6) = p(6) + dp;
D = (bf(:,1) - spm_hrf(dt,p,fMRI_T))/dp;
bf = [bf D(:)];
p(6) = p(6) - dp;
%-Add dispersion derivative
%------------------------------------------------------------------
if strfind(xBF.name,'dispersion')
dp = 0.01;
p(3) = p(3) + dp;
D = (bf(:,1) - spm_hrf(dt,p,fMRI_T))/dp;
bf = [bf D(:)];
end
end
end
%-Orthogonalise and fill in basis function structure
%--------------------------------------------------------------------------
bf = spm_orth(bf);
return
%==========================================================================
%- S U B - F U N C T I O N S
%==========================================================================
function bf = spm_gamma_bf(u,h)
% Return basis functions (Gamma functions) used for Volterra expansion
% FORMAT bf = spm_gamma_bf(u,h);
% u - times {seconds}
% h - order
% bf - basis functions (mixture of Gammas)
%__________________________________________________________________________
u = u(:);
bf = [];
for i = 2:(1 + h)
m = 2^i;
s = sqrt(m);
bf = [bf spm_Gpdf(u,(m/s)^2,m/s^2)];
end
function [beta_hrf, u0]= wgr_hrf_estimation(dat,xBF,N,bf,temporal_mask)
%% estimate HRF
thr = xBF.thr;
if ~isfield(xBF,'localK')
if xBF.TR<=2
localK = 1;
else
localK = 2;
end
else
localK = xBF.localK;
end
u0 = rsHRF_find_event_vector(dat,thr,localK,temporal_mask);
u = [ full(u0)
zeros(xBF.T-1,N) ];
u = reshape(u,1,[]); %(microtime)
[beta, lag] = wgr_hrf_fit(dat,xBF,u,bf);
beta_hrf = beta; beta_hrf(end+1) = lag;
u0 = find(full(u0(:)));
return
function varargout = wgr_hrf_fit(dat,xBF,u,bf)
% u - BOLD event vector (microtime).
% nlag - time lag from neural event to BOLD event .
lag = xBF.lag;
AR_lag = xBF.AR_lag;
nlag = length(lag);
erm = zeros(1,nlag);
beta = zeros(size(bf,2)+1,nlag);
% fprintf('Converged after ')
for i=1:nlag
u_lag = [u(1,lag(i)+1:end) zeros(1,lag(i))]';
[erm(i), beta(:,i)] = wgr_glm_estimation(dat,u_lag,bf,xBF.T,xBF.T0,AR_lag);
end
[~, id] = rsHRF_knee_pt(erm);
if id==nlag
id = id-1;
end
varargout{1} = beta(:,id+1);
if nargout>1
varargout{2} = lag(id+1);
end
% fprintf('iterations!\n')
return
function [res_sum, Beta] = wgr_glm_estimation(dat,u,bf,T,T0,AR_lag)
% u: BOLD event vector (microtime).
nscans = size(dat,1);
x = wgr_onset_design(u,bf,T,T0,nscans);
X = [x ones(nscans,1)];
[res_sum, Beta] = wgr_glsco(X,dat,AR_lag);
return
function X = wgr_onset_design(u,bf,T,T0,nscans)
% u: BOLD event vector (microtime).
% bf: - basis set matrix
% T: - microtime resolution (number of time bins per scan)
% T0: - microtime onset (reference time bin, see slice timing)
ind = 1:length(u);
X = [];
for p = 1:size(bf,2)
x = conv(u,bf(:,p));
x = x(ind);
X = [X x];
end
%-Resample regressors at acquisition times
X = X( (0:(nscans - 1))*T + T0, :);
return
function [res_sum, Beta] = wgr_glsco(X,Y,AR_lag)
% Linear regression when disturbance terms follow AR(p)
% -----------------------------------
% Model:
% Yt = Xt * Beta + ut ,
% ut = Phi1 * u(t-1) + ... + Phip * u(t-p) + et
% where et ~ N(0,s^2)
% -----------------------------------
% Algorithm:
% Cochrane-Orcutt iterated regression (Feasible generalized least squares)
% -----------------------------------
% Usage:
% Y = dependent variable (n * 1 vector)
% X = regressors (n * k matrix)
% AR_lag = number of lags in AR process
% -----------------------------------
% Returns:
% Beta = estimator corresponding to the k regressors
if nargin < 3; AR_lag = 0;end
if nargin < 4; max_iter = 20;end
[nobs, nvar] = size(X);
% Beta = X\Y;
Beta = wgr_regress(Y,X);
resid = Y - X * Beta;
if ~AR_lag
%res_sum = sum(resid.^2);
res_sum=cov(resid);
return
end
max_tol = min(1e-6,max(abs(Beta))/1000);
for r = 1:max_iter
% fprintf('Iteration No. %d\n',r)
Beta_temp = Beta;
X_AR = zeros(nobs-2*AR_lag,AR_lag);
for m = 1:AR_lag
X_AR(:,m) = resid(AR_lag+1-m:nobs-AR_lag-m);
end
Y_AR = resid(AR_lag+1:nobs-AR_lag);
% AR_para = X_AR\Y_AR;
AR_para = wgr_regress(Y_AR,X_AR);
X_main = X(AR_lag+1:nobs,:);
Y_main = Y(AR_lag+1:nobs);
for m = 1:AR_lag
X_main = X_main-AR_para(m)*X(AR_lag+1-m:nobs-m,:);
Y_main = Y_main-AR_para(m)*Y(AR_lag+1-m:nobs-m);
end
% Beta = X_main\Y_main;
Beta = wgr_regress(Y_main,X_main);
resid = Y(AR_lag+1:nobs) - X(AR_lag+1:nobs,:)*Beta;
if max(abs(Beta-Beta_temp)) < max_tol
% fprintf('%d ,',r)
% fprintf('Converged after %d iterations!\n',r)
break
end
end
% res_sum = sum(resid.^2);
res_sum = cov(resid);
%if r == max_iter
% fprintf('Maximum %d iteration reached.\n',max_iter)
%end
return
function [hrf,p] = spm_hrf(RT,P,T)
% Haemodynamic response function
% FORMAT [hrf,p] = spm_hrf(RT,p,T)
% RT - scan repeat time
% p - parameters of the response function (two Gamma functions)
%
% defaults
% {seconds}
% p(1) - delay of response (relative to onset) 6
% p(2) - delay of undershoot (relative to onset) 16
% p(3) - dispersion of response 1
% p(4) - dispersion of undershoot 1
% p(5) - ratio of response to undershoot 6
% p(6) - onset {seconds} 0
% p(7) - length of kernel {seconds} 32
%
% T - microtime resolution [Default: 16]
%
% hrf - haemodynamic response function
% p - parameters of the response function
%__________________________________________________________________________
% Copyright (C) 1996-2015 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_hrf.m 6594 2015-11-06 18:47:05Z guillaume $
%-Parameters of the response function
%--------------------------------------------------------------------------
try
p = spm_get_defaults('stats.fmri.hrf');
catch
p = [6 16 1 1 6 0 32];
end
if nargin > 1
p(1:length(P)) = P;
end
%-Microtime resolution
%--------------------------------------------------------------------------
if nargin > 2
fMRI_T = T;
else
fMRI_T = spm_get_defaults('stats.fmri.t');
end
%-Modelled haemodynamic response function - {mixture of Gammas}
%--------------------------------------------------------------------------
dt = RT/fMRI_T;
u = [0:ceil(p(7)/dt)] - p(6)/dt;
hrf = spm_Gpdf(u,p(1)/p(3),dt/p(3)) - spm_Gpdf(u,p(2)/p(4),dt/p(4))/p(5);
hrf = hrf([0:floor(p(7)/RT)]*fMRI_T + 1);
hrf = hrf'/sum(hrf);
function b = wgr_regress(y,X)
% copy from function [b,bint,r,rint,stats] = regress(y,X,alpha)
[n,ncolX] = size(X);
% Use the rank-revealing QR to remove dependent columns of X.
[Q,R,perm] = qr(X,0);
if isempty(R)
p = 0;
elseif isvector(R)
p = double(abs(R(1))>0);
else
p = sum(abs(diag(R)) > max(n,ncolX)*eps(R(1)));
end
if p < ncolX
% warning(message('stats:regress:RankDefDesignMat'));
R = R(1:p,1:p);
Q = Q(:,1:p);
perm = perm(1:p);
end
% Compute the LS coefficients, filling in zeros in elements corresponding
% to rows of X that were thrown out.
b = zeros(ncolX,1);
b(perm) = R \ (Q'*y);