-
Notifications
You must be signed in to change notification settings - Fork 1
/
NewRunDEPKET_2022.m
251 lines (207 loc) · 9.7 KB
/
NewRunDEPKET_2022.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
function NewRunDEPKET_2022(i)
% Top level script showing how to apply the thalamo-cortical neural mass
% model decribed in Shaw et al 2020 NeuroImage, to M/EEG data.
%
% Requires atcm (thalamo cortical modelling package) and aoptim
% (optimisation package)
%
% atcm: https://github.com/alexandershaw4/atcm
% aoptim: https://github.com/alexandershaw4/aoptim
%
% Overview of contents
%--------------------------------------------------
% - atcm. contains:
% - the equations of motion for an 8 pop thalamo-cortical
% model described by parameterised Morris-Lecar/Hodgkin-Hux
% conductance ODEs
% - numerical integration (Euler, RK, Newton-Cotes +) and spectral
% response functions
% - lots of helper functions (integration, differentiation,
% continuation, decomposition methods etc)
% - aoptim contains:
% - a second order gradient descent optimisation routine that
% includes both free energy and orther objective functions
% - n-th order numerical differentiation functions (parallelised)
%
%
% AS2020/21/22 {alexandershaw4[@]gmail.com}
% EXAMPLE ONE NODE SETUP:
%==========================================================================
clear global;
% for cubric system, add paths -
% addpath(genpath('~/spm12'));
% addpath(genpath('/home/sapas10/code/atcm/'));
% addpath(genpath('/home/sapas10/code/aoptim/'));
% Data & Design
%--------------------------------------------------------------------------
Data.Datasets = 'ketdep_list.txt';
Data.Design.X = []; % design matrix
Data.Design.name = {'undefined'}; % condition names
Data.Design.tCode = [1]; % condition codes in SPM
Data.Design.Ic = [1]; % channel indices
Data.Design.Sname = {'V1'}; % channel (node) names
Data.Prefix = 'nfinalTCM_'; % outputted DCM prefix
Data.Datasets = atcm.fun.ReadDatasets(Data.Datasets);
% Model space - T = ns x ns, where 1 = Fwd, 2 = Bkw
%--------------------------------------------------------------------------
T = [... % this is a 1-node model; nothing to put here...
0];
F = (T==1);
B = (T==2);
C = [1]'; % input(s)
L = sparse(1,1);
% Set up, over subjects
%--------------------------------------------------------------------------
for s = i;%1:length(Data.Datasets)
% Data Naming & Design Matrix
%----------------------------------------------------------------------
DCM = [];
[fp fn fe] = fileparts(Data.Datasets{s});
DCM.name = [Data.Prefix fn fe];
DCM.xY.Dfile = Data.Datasets{s}; % original spm datafile
Ns = length(F); % number of regions / modes
DCM.xU.X = Data.Design.X; % design matrix
DCM.xU.name = Data.Design.name; % condition names
tCode = Data.Design.tCode; % condition index (in SPM)
DCM.xY.Ic = Data.Design.Ic; % channel indices
DCM.Sname = Data.Design.Sname; % channel names
if exist(DCM.name);
fprintf('Skipping model %d/%d - already exists!\n( %s )\n',i,length(Data.Datasets),DCM.name);
continue;
end
% Extrinsic Connectivity - Model Space
%----------------------------------------------------------------------
DCM.A{1} = F;
DCM.A{2} = B;
DCM.A{3} = L;
DCM.B{1} = DCM.A{1} | DCM.A{2};
DCM.B(2:length(DCM.xU.X)) = DCM.B;
DCM.C = C;
% Function Handles
%----------------------------------------------------------------------
DCM.M.f = @atcm.tc_hilge; % model function handle
DCM.M.IS = @atcm.integrate_1; % Alex integrator/transfer function
DCM.options.SpecFun = @atcm.fun.Afft; % fft function for IS
% Print Progress
%----------------------------------------------------------------------
fprintf('Running Dataset %d / %d\n',s,length(Data.Datasets));
% Frequency range of interest
fq = [3 90];
% Prepare Data
%----------------------------------------------------------------------
DCM.M.U = sparse(diag(ones(Ns,1))); %... ignore [modes]
DCM.options.trials = tCode; %... trial code [GroupDataLocs]
DCM.options.Tdcm = [300 1300]; %... peristimulus time
DCM.options.Fdcm = fq; %... frequency window
DCM.options.D = 1; %... downsample
DCM.options.han = 1; %... apply hanning window
DCM.options.h = 4; %... number of confounds (DCT)
DCM.options.DoData = 1; %... leave on [custom]
%DCM.options.baseTdcm = [-200 0]; %... baseline times [new!]
DCM.options.Fltdcm = fq; %... bp filter [new!]
DCM.options.analysis = 'CSD'; %... analyse type
DCM.xY.modality = 'LFP'; %... ECD or LFP data? [LFP]
DCM.options.spatial = 'LFP'; %... spatial model [LFP]
DCM.options.model = 'tc6'; %... neural model
DCM.options.Nmodes = length(DCM.M.U); %... number of modes
% 1010 == use atcm.fun.AFFT.m
DCM.options.UseWelch = 1010;
DCM.options.FFTSmooth = 3;
%DCM.options.UseButterband = fq;
DCM.options.BeRobust = 0;
DCM.options.FrequencyStep = 1;
DCM.xY.name = DCM.Sname;
DCM = atcm.fun.prepcsd(DCM);
DCM.options.DATA = 1 ;
% Subfunctions and default priors
%----------------------------------------------------------------------
DCM = atcm.parameters(DCM,Ns);
% if using AOPTIM for inversion, invoke the linear model g(x) output by
% placing data (DCM.xY.y) in model struct - DCM.M.y
DCM.M.y = DCM.xY.y;
DCM.M.Hz = DCM.xY.Hz;
% If using DCM inversion, select whether to block graph or not
DCM.M.nograph = 0;
% Feature function for the integrator [NOT USED]
%----------------------------------------------------------------------
DCM = atcm.complete(DCM);
DCM.M.FS = @(x) x(:).^2.*(1:length(x))'.^2;
imscale = sum(spm_vec(abs(real(DCM.xY.y{:})))) ./ sum(spm_vec(abs(imag(DCM.xY.y{:}))));
DCM.M.FS = @(x) [real(x) ; imscale*imag(x) ];
% other model options
%----------------------------------------------------------------------
DCM.M.solvefixed=0; % oscillations == no fixed point search
DCM.M.x = zeros(1,8,7); % init state space: ns x np x nstates
DCM.M.x(:,:,1)=-70; % init pop membrane pot [mV]
% Set Q - a precision operator, increasing with frequency
%----------------------------------------------------------------------
y = spm_vec(DCM.xY.y{1});
w = spm_vec(DCM.xY.Hz);
Qw = diag(DCM.xY.y{:}./max(DCM.xY.y{:}));
Nf = length(w);
Q = {spm_Q(1/2,Nf,1)*diag(DCM.M.Hz)*spm_Q(1/2,Nf,1)};
Qw = Qw * Q{:};
% Newton-Cotes integration parameters
%----------------------------------------------------------------------
DCM.M.sim.dt = 1./300;
DCM.M.sim.pst = 1000*((0:DCM.M.sim.dt:(3)-DCM.M.sim.dt)');
DCM.M.burnin = 300;
DCM.M.intmethod = 44;
% Input is an ERP
DCM.M.InputType = 0;
DCM.M.pE.C = log(.01);
% only interested in real psd rn
%----------------------------------------------------------------------
DCM.xY.y{1} = real(DCM.xY.y{1});
DCM.M.y = DCM.xY.y;
ppE = DCM.M.pE;
% Optimise using AO.m
%----------------------------------------------------------------------
M = AODCM(DCM);
% Bias and feature selection
M.opts.Q = real(Qw);
M.opts.FS = @(x) [real(sqrt(x))];
M.opts.FS = @(x) [real( spm_vec(atcm.fun.Pf2VMD(x,3)) )];
% opt set 1.
M.opts.EnforcePriorProb=0; % forcibly constrain parameters to within prior dist
M.opts.ismimo=0; % compute dfdp elementwise on vector-output function
M.opts.doparallel=1; % use parfor loops when poss, incl for df/dx
M.opts.hyperparams=1; % hyperparameter tuning
M.opts.fsd = 1; % fixed-step for derivatives
M.opts.corrweight = 0; % weight log evidence by correlation
M.opts.inner_loop = 3;
M.opts.objective = 'qrmse'; % objective (error) function
M.opts.criterion = 1e-3;
%M.opts.isGaussNewton=1;
M.default_optimise([7],[28])
% afterward, use AODCM object to loop through the optimisation steps
% for a visualisation:
%-------------------------------------------------------------------
% for i = 1:28;
% dydp(i,:) = spm_vec(M.opts.fun(M.history.p{i}));
% end
Morig=M;
% Extract fit and run again
Ep = spm_unvec(M.Ep,DCM.M.pE);
DCM.M.pE = Ep;
% Optimise --- 2 1
%----------------------------------------------------------------------
M = AODCM(DCM);
% Bias and feature selection
M.opts.Q = Qw;
M.opts.FS = @(x) real(sqrt(x));
M.opts.FS = @(x) real( spm_vec(atcm.fun.Pf2VMD(x,3)) );
% opt set 1.
M.opts.EnforcePriorProb=0; % forcibly constrain parameters to within prior dist
M.opts.ismimo=0; % compute dfdp elementwise on vector-output function
M.opts.doparallel=1; % use parfor loops when poss, incl for df/dx
M.opts.hyperparams=1; % hyperparameter tuning
M.opts.fsd=0; % fixed-step for derivatives
M.opts.corrweight = 0; % weight log evidence by correlation
M.opts.objective = 'qrmse'; % objective (error) function
M.opts.criterion = 1e-3;
M.default_optimise([7],[8])
% reinstate the actual priors before saving
DCM.M.pE = ppE;
save(DCM.name); close; clear global;
end