-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_Hopf_meta2_LR.m
304 lines (260 loc) · 8.08 KB
/
run_Hopf_meta2_LR.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
clear all;
path2=[ '../Turbulence/Basics'];
addpath(path2);
path3=[ '../Nonequilibrium/'];
addpath(genpath(path3));
path4=[ '../Tenet/TENET/'];
addpath(genpath(path4));
NSUB=100;
LRange=50;
N=62;
LATDIM=7;
Isubdiag = find(tril(ones(N),-1));
IsubdiagL = find(tril(ones(LATDIM),-1));
indexregion=[1:31 50:80];
load results_f_diff_REST_dk62.mat;
load SC_dbs80HARDIFULL.mat;
C = SC_dbs80HARDI;
C = C/max(max(C));
C=C(indexregion,indexregion);
TR=0.72; % Repetition Time (seconds)
Tmax = 1200;
% Bandpass filter settings
fnq=1/(2*TR); % Nyquist frequency
flp = 0.008; % lowpass frequency of filter (Hz)
fhi = 0.08; % highpass
Wn=[flp/fnq fhi/fnq]; % butterworth bandpass non-dimensional frequency
k=2; % 2nd order butterworth filter
[bfilt,afilt]=butter(k,Wn); % construct the filter
sig = 0.01;
dt=0.1*TR/2;
dsig = sqrt(dt)*sig;
AA=-0.02;
a=AA*ones(N,1);
a=repmat(a,1,2);
wo = f_diff'*(2*pi);
omega = repmat(wo,1,2);
omega(:,1) = -omega(:,1);
%% SC
load SC_dbs80HARDIFULL.mat;
C = SC_dbs80HARDI;
C = C/max(max(C));
C=C(indexregion,indexregion);
%% WITHOUT LR
load cog_dk80.mat;
dk80cog=dk80cog(indexregion,:);
for i=1:N
for j=1:N
SCdist(i,j)=sqrt(sum((dk80cog(i,:)-dk80cog(j,:)).^2));
if SCdist(i,j)>LRange
SCmasklr(i,j)=0;
else
SCmasklr(i,j)=1;
end
end
end
C_noLR=C.*SCmasklr;
IsubdiagT = find(tril(ones(Tmax),-1));
%% FULL WITH LR
for trial=1:50
trial
for sub=1:NSUB
G=0.2;
G2=G-0.005+0.01*rand;
wC = G2*C;
sumC = repmat(sum(wC,2),1,2);
clear EdgesA Edges;
xs=zeros(Tmax,N);
z = 0.1*ones(N,2);
nn=0;
% discard first 2000 time steps
for t=0:dt:1000
suma = wC*z - sumC.*z; % sum(Cij*xi) - sum(Cij)*xj
zz = z(:,end:-1:1); % flipped z, because (x.*x + y.*y)
z = z + dt*(a.*z + zz.*omega - z.*(z.*z+zz.*zz) + suma) + dsig*randn(N,2);
end
% actual modeling (x=BOLD signal (Interpretation), y some other oscillation)
for t=0:dt:((Tmax-1)*TR)
suma = wC*z - sumC.*z; % sum(Cij*xi) - sum(Cij)*xj
zz = z(:,end:-1:1); % flipped z, because (x.*x + y.*y)
z = z + dt*(a.*z + zz.*omega - z.*(z.*z+zz.*zz) + suma) + dsig*randn(N,2);
if abs(mod(t,TR))<0.01
nn=nn+1;
xs(nn,:)=z(:,1)';
end
end
ts=xs';
for seed=1:N
ts(seed,:)=detrend(ts(seed,:)-mean(ts(seed,:)));
signal_filt(seed,:) =filtfilt(bfilt,afilt,ts(seed,:));
end
%% Edges
zPhi=zscore(signal_filt');
for t=1:size(zPhi,1)
fcd=zPhi(t,:)'*zPhi(t,:);
EdgesA(:,t)=fcd(Isubdiag)';
end
FCDA=(EdgesA'*EdgesA)./(vecnorm(EdgesA)'*vecnorm(EdgesA));
MetaA(sub)=0.5*(log(2*pi*var(FCDA(IsubdiagT))))+0.5;
%% Meta Diff
epsilon=400;
ts1=signal_filt;
ts=zscore(ts1,[],2);
Tm=size(ts,2);
Kmatrix=zeros(Tm,Tm);
for i=1:Tm
for j=1:Tm
dij2=sum((ts(:,i)-ts(:,j)).^2);
Kmatrix(i,j)=exp(-dij2/epsilon);
end
end
Dmatrix=diag(sum(Kmatrix,2));
Pmatrix=inv(Dmatrix)*Kmatrix;
[VV,LL]=eig(Pmatrix);
Phi=VV(:,1:LATDIM);
Phi=Phi*(LL(2:LATDIM+1,2:LATDIM+1));
zPhi=zscore(Phi);
for t=1:size(zPhi,1)
fcd=zPhi(t,:)'*zPhi(t,:);
EdgesL(:,t)=fcd(IsubdiagL)';
end
FCD=(EdgesL'*EdgesL)./(vecnorm(EdgesL)'*vecnorm(EdgesL));
Meta2(sub)=0.5*(log(2*pi*var(FCD(IsubdiagT))))+0.5;
%% Meta Q
epsilon=300;
Thorizont=3;
Kmatrix=zeros(Tm,Tm);
for i=1:Tm
for j=1:Tm
dij2=sum((ts(:,i)-ts(:,j)).^2);
Kmatrix(i,j)=exp(complex(0,1)*dij2/epsilon);
end
end
Ktr_t=Kmatrix^Thorizont;
Ptr_t=abs(Ktr_t).^2;
Dmatrix=diag(sum(Ptr_t,2));
Pmatrix=inv(Dmatrix)*Ptr_t;
[VV,LL]=eig(Pmatrix);
Phi=VV(:,1:LATDIM);
Phi=Phi*abs(LL(2:LATDIM+1,2:LATDIM+1));
zPhi=zscore(Phi);
for t=1:size(zPhi,1)
fcd=zPhi(t,:)'*zPhi(t,:);
EdgesL(:,t)=fcd(IsubdiagL)';
end
FCD=(EdgesL'*EdgesL)./(vecnorm(EdgesL)'*vecnorm(EdgesL));
MetaQ2(sub)=0.5*(log(2*pi*var(FCD(IsubdiagT))))+0.5;
%% Without LR
G=0.4;
G2=G-0.005+0.01*rand;
wC = G2*C;
sumC = repmat(sum(wC,2),1,2);
clear EdgesA Edges;
xs=zeros(Tmax,N);
z = 0.1*ones(N,2);
nn=0;
% discard first 2000 time steps
for t=0:dt:1000
suma = wC*z - sumC.*z; % sum(Cij*xi) - sum(Cij)*xj
zz = z(:,end:-1:1); % flipped z, because (x.*x + y.*y)
z = z + dt*(a.*z + zz.*omega - z.*(z.*z+zz.*zz) + suma) + dsig*randn(N,2);
end
% actual modeling (x=BOLD signal (Interpretation), y some other oscillation)
for t=0:dt:((Tmax-1)*TR)
suma = wC*z - sumC.*z; % sum(Cij*xi) - sum(Cij)*xj
zz = z(:,end:-1:1); % flipped z, because (x.*x + y.*y)
z = z + dt*(a.*z + zz.*omega - z.*(z.*z+zz.*zz) + suma) + dsig*randn(N,2);
if abs(mod(t,TR))<0.01
nn=nn+1;
xs(nn,:)=z(:,1)';
end
end
ts=xs';
for seed=1:N
ts(seed,:)=detrend(ts(seed,:)-mean(ts(seed,:)));
signal_filt(seed,:) =filtfilt(bfilt,afilt,ts(seed,:));
end
%% Edges
zPhi=zscore(signal_filt');
for t=1:size(zPhi,1)
fcd=zPhi(t,:)'*zPhi(t,:);
EdgesA(:,t)=fcd(Isubdiag)';
end
FCDA=(EdgesA'*EdgesA)./(vecnorm(EdgesA)'*vecnorm(EdgesA));
MetaA_noLR(sub)=0.5*(log(2*pi*var(FCDA(IsubdiagT))))+0.5;
%% Meta Diff
epsilon=400;
ts1=signal_filt;
ts=zscore(ts1,[],2);
Tm=size(ts,2);
Kmatrix=zeros(Tm,Tm);
for i=1:Tm
for j=1:Tm
dij2=sum((ts(:,i)-ts(:,j)).^2);
Kmatrix(i,j)=exp(-dij2/epsilon);
end
end
Dmatrix=diag(sum(Kmatrix,2));
Pmatrix=inv(Dmatrix)*Kmatrix;
[VV,LL]=eig(Pmatrix);
Phi=VV(:,1:LATDIM);
Phi=Phi*(LL(2:LATDIM+1,2:LATDIM+1));
zPhi=zscore(Phi);
for t=1:size(zPhi,1)
fcd=zPhi(t,:)'*zPhi(t,:);
EdgesL(:,t)=fcd(IsubdiagL)';
end
FCD=(EdgesL'*EdgesL)./(vecnorm(EdgesL)'*vecnorm(EdgesL));
Meta_noLR2(sub)=0.5*(log(2*pi*var(FCD(IsubdiagT))))+0.5;
%% Meta Q
epsilon=300;
Thorizont=3;
Kmatrix=zeros(Tm,Tm);
for i=1:Tm
for j=1:Tm
dij2=sum((ts(:,i)-ts(:,j)).^2);
Kmatrix(i,j)=exp(complex(0,1)*dij2/epsilon);
end
end
Ktr_t=Kmatrix^Thorizont;
Ptr_t=abs(Ktr_t).^2;
Dmatrix=diag(sum(Ptr_t,2));
Pmatrix=inv(Dmatrix)*Ptr_t;
[VV,LL]=eig(Pmatrix);
Phi=VV(:,1:LATDIM);
Phi=Phi*abs(LL(2:LATDIM+1,2:LATDIM+1));
zPhi=zscore(Phi);
for t=1:size(zPhi,1)
fcd=zPhi(t,:)'*zPhi(t,:);
EdgesL(:,t)=fcd(IsubdiagL)';
end
FCD=(EdgesL'*EdgesL)./(vecnorm(EdgesL)'*vecnorm(EdgesL));
MetaQ_noLR2(sub)=0.5*(log(2*pi*var(FCD(IsubdiagT))))+0.5;
end
Meta(trial)=corr2(MetaA,Meta2);
MetaQ(trial)=corr2(MetaA,MetaQ2);
Meta_noLR(trial)=corr2(MetaA_noLR,Meta_noLR2);
MetaQ_noLR(trial)=corr2(MetaA_noLR,MetaQ_noLR2);
end
figure(1)
subplot(1,2,1)
violinplot([Meta' Meta_noLR']);
ylim([-0.3 0.6])
subplot(1,2,2)
violinplot([MetaQ' MetaQ_noLR']);
ylim([-0.3 0.6])
diffMeta=Meta-Meta_noLR;
diffMetaQ=MetaQ-MetaQ_noLR;
figure(2)
violinplot([diffMeta' diffMetaQ'])
ylim([-0.4 0.7])
axis('square');
ranksum(diffMeta,diffMetaQ)
diffMetas=MetaQ-Meta;
diffMetas_noLR=MetaQ_noLR-Meta_noLR;
figure(3)
violinplot([diffMetas' diffMetas_noLR'])
ylim([-0.4 0.7])
axis('square');
ranksum(diffMetas,diffMetas_noLR)
save results_hopf_LR.mat Meta MetaQ Meta_noLR MetaQ_noLR;