-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunLaplaceTCM_ExampleScript.m
256 lines (202 loc) · 8.76 KB
/
RunLaplaceTCM_ExampleScript.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
function RunLaplaceTCM_ExampleScript(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.
%
% This version using a linearisation and transfer function (numerical
% Laplace) rather than brute numerical integration.
%
% 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:
%==========================================================================
% Data & Design
%--------------------------------------------------------------------------
Data.Datasets = 'NewSZ.txt';%'MeanSZDatasets.txt';%'AllSZNoMerge.txt'; % textfile list of LFP SPM datasets (.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 = 'LaplaceTCM_'; % 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];
% from
% v1 v2
% T = [0 2 % v1 to
% 1 0];%;v2
F = (T==1);
B = (T==2);
C = [1]'; % input(s)
L = sparse(1,1);
[p]=fileparts(which('atcm.integrate_1'));p=strrep(p,'+atcm','');addpath(p);
% Set up, over subjects
%--------------------------------------------------------------------------
for i = i;%1:length(Data.Datasets)
% Data Naming & Design Matrix
%----------------------------------------------------------------------
DCM = [];
[fp fn fe] = fileparts(Data.Datasets{i});
DCM.name = [Data.Prefix fn fe];
DCM.xY.Dfile = Data.Datasets{i}; % 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.TCM2024; % model function handle
DCM.M.IS = @atcm.fun.alex_LapTF_NoDelay; % Alex integrator/transfer function
DCM.options.SpecFun = @atcm.fun.Afft; % fft function for IS
% Print Progress
%----------------------------------------------------------------------
fprintf('Running Dataset %d / %d\n',i,length(Data.Datasets));
% Frequency range of interest
fq = [1 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.UseButterband = fq;
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
DCM.options.UseWelch = 1010;
DCM.options.FFTSmooth = 0;
DCM.options.BeRobust = 0;
DCM.options.FrequencyStep = 1;
DCM.xY.name = DCM.Sname;
DCM = atcm.fun.prepcsd(DCM);
DCM.options.DATA = 1 ;
DCM.xY.y{:} = atcm.fun.agauss_smooth(abs(DCM.xY.y{:}),1)';
% Subfunctions and default priors
%----------------------------------------------------------------------
DCM = atcm.parameters(DCM,Ns);
% other model options
%----------------------------------------------------------------------
DCM.M.solvefixed=0; %
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]
load([p '/newpoints3.mat'],'pE','pC')
pE = spm_unvec(spm_vec(pE)*0,pE);
pC.ID = pC.ID * 0;
pC.T = pC.T *0;
pE.J = pE.J-1000;
pE.J(1:8) = log([.6 .8 .4 .6 .4 .6 .4 .4]);
%pC.ID = pC.ID + 1/8;
pE.L = 0;
pC.a = pC.a*0;
pE.Gb = pE.H;
pC.Gb = [1 0 0 0 0 0 0 0;
0 1 1 0 0 0 0 0;
0 0 1 0 0 0 0 0;
0 0 0 1 1 0 0 0;
0 0 0 0 1 0 0 0;
0 0 0 0 1 1 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 1 0]/64;
pC.J(1:8)=1/8;
pC.d(1) = 1/8;
% Make changes here;
%-----------------------------------------------------------
DCM.M.pE = pE;
DCM.M.pC = pC;
% Optimise using AO.m -- a Newton scheme with add-ons and multiple
% objective functions built in, including free energy
%----------------------------------------------------------------------
w = DCM.xY.Hz;
Y = DCM.xY.y{:};
DCM.M.y = DCM.xY.y;
DCM.M.Hz = DCM.xY.Hz;
ppE = DCM.M.pE;
ppC = DCM.M.pC;
fprintf('--------------- STATE ESTIMATION ---------------\n');
fprintf('Search for a stable fixed point\n');
xx = load([p '/newx.mat']); DCM.M.x = spm_unvec(xx.x,DCM.M.x);
load('init_14dec','x');
DCM.M.x = spm_unvec(x,DCM.M.x);
x = atcm.fun.alexfixed(DCM.M.pE,DCM.M,1e-10);
DCM.M.x = spm_unvec(x,DCM.M.x);
norm(DCM.M.f(DCM.M.x,0,DCM.M.pE,DCM.M))
fprintf('Finished...\n');
% precompute J and put in J which flags for the rhs to compute the
% *delayed* update step
[dx,J] = DCM.M.f(DCM.M.x,0,DCM.M.pE,DCM.M);
DCM.M.J = J;
%newp = load('NewPriors','Ep');
%DCM.M.pE = newp.Ep;
fprintf('--------------- PARAM ESTIMATION ---------------\n');
%fprintf('iteration %d\n',j);
% Alex's version of the Levenberg-Marquard routine
%M = AODCM(DCM);
[Qp,Cp,Eh,F] = spm_nlsi_GN(DCM.M,DCM.xU,DCM.xY);
%M.alex_lm;
%M.compute_free_energy(M.Ep);
%DCM.M.nograph = 0;
%[Qp,Cp,Eh,F] = spm_nlsi_GN(DCM.M,DCM.xU,DCM.xY);
% save in DCM structures after optim
%----------------------------------------------------------------------
DCM.M.pE = ppE;
DCM.Ep = Qp;%spm_unvec(M.Ep,DCM.M.pE);
DCM.Cp = Cp;
DCM.M.sim.dt = 1./600;
DCM.M.sim.pst = 1000*((0:DCM.M.sim.dt:(2)-DCM.M.sim.dt)');
[y,w,G,s] = feval(DCM.M.IS,DCM.Ep,DCM.M,DCM.xU);
DCM.pred = y;
DCM.w = w;
DCM.G = G;
DCM.series = s;
%DCM.Cp = atcm.fun.reembedreducedcovariancematrix(DCM,M.CP);
%DCM.Cp = makeposdef(DCM.Cp);
DCM.F = F;%M.FreeEnergyF;
%DCM.Cp = M.CP;
save(DCM.name); close all; clear global;
end
end