-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimize_model.m
312 lines (266 loc) · 8.13 KB
/
optimize_model.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
function optimize_model(task_id)
% example function call:
% optimize_model('42')
% will use 42 as a seed for the random number generator and
% create a folder named '42' with optimization results for all
% recorded T5 cells. The results can be loaded and visualized with the code
% available at
% https://doi.org/10.25378/janelia.c.4771805.v1
attempts_num = 10;
err_cutoff = 3.5;
basedir ='./';
cd(basedir)
newdir = strcat(basedir,task_id);
mkdir(newdir)
rng(str2double(task_id));
lb_gauss = [ .1,.1,.1,.1,.1, 0,0,-5,-5,0,0];
ub_gauss = [ 40,40,40,40,40, 10,10,5,10,10,10];
for cell_id = 1:17
load([basedir,'data_cell_',num2str(cell_id),'_spfr.mat'],'d','p')
for n_attemps = 1:attempts_num
par0 = lb_gauss + rand(size(ub_gauss)).*(ub_gauss-lb_gauss);
q = optimize_t5(d,p,par0,lb_gauss,ub_gauss);
vm_spfr = vm_bars_and_gratings(q.param,0,p);
err_spfr = mean( (vm_spfr - d.vm_all_sb).^2 );
if(q.resnorm < err_cutoff)
break
end
end
load([basedir,'data_cell_',num2str(cell_id),'_all.mat'],'d','p')
vm_all = vm_bars_and_gratings(q.param,0,p);
if(ismember(cell_id, [2 5 6 7 9 10 13 14 15 16 17]))
err_all = mean( (vm_all -...
[d.vm_all_sb; d.vm_all_mb; d.vm_all_mm;...
d.vm_all_mg; d.vm_all_sg]).^2 );
else
err_all = mean( (vm_all -...
[d.vm_all_sb; d.vm_all_mb; d.vm_all_mm]).^2 );
end
save([newdir,'/result_cell_',num2str(cell_id),'.mat'],...
'par0','q','vm_all','err_spfr','err_all')
end
function q = optimize_t5(d,p,par0,lb,ub)
vm_all = d.vm_all_sb;
ind = false(size(vm_all));
for i = 1:length(d.ind_ref1_sb)
wid = p.width_sb(i);
if(wid==2)
ind(d.ind_ref1_sb(i):d.ind_ref2_sb(i)) = true;
end
end
fun = @(ppar) mean( (extract_rise_decay_vm(ppar, p, ind) -...
vm_all(ind) ).^2 );
options = optimoptions('fmincon','Display','iter');
[param,resnorm,residual,exitflag,output] =...
fmincon(fun,par0,[],[],[],[],lb,ub,[],options);
q.param = param;
q.resnorm = resnorm;
q.residual = residual;
q.exitflag = exitflag;
q.output = output;
function vm_ind = extract_rise_decay_vm(par, p, ind)
tmp = vm_bars_and_gratings(par, 0, p);
vm_ind = tmp(ind);
function y = vm_bars_and_gratings(param,~,q)
p.xx = q.xx;
p.tau = param(1)*10;
p.tde = param(2)*10;
p.tdi = param(3)*10;
p.tre = param(4)*10;
p.tri = param(5)*10;
ae = param(6);
ai = param(7);
mue = param(8);
mui = param(9);
sige = param(10);
sigi = param(11);
p.xae = ae * exp(-(p.xx-mue).^2 / (2 *sige^2));
p.xai = ai * exp(-(p.xx-mui).^2 / (2 *sigi^2));
p.vi = -74;
p.vl = -65;
p.ve = 0;
p.toff = 25;
p.stimmin = p.toff;
y = [];
k0 = 1; k1 = 1; k2 = 1; k3 = 1; k4 = 1;
for i = 1:length(q.protocol)
incond = [0,0,0,0];
if(q.protocol(i)==0) % single bars
p.tstimmin = p.toff;
p.tstimmax = q.duration_sb(k0) + p.toff;
p.location_sb = q.location_sb(k0);
p.width_sb = q.width_sb(k0);
[~,y0] = ode45(@run_vm_sb, q.t{i}, incond, [],p);
vm = (p.vl + p.ve*y0(:,2) + p.vi*y0(:,4))./(1 + y0(:,2) + y0(:,4));
y = [y; vm - p.vl];
k0 = k0 + 1;
end
if(q.protocol(i)==3) % moving bars
p.pos = q.pos_mb;
p.flag_dir = q.direction_mb(k3);
p.currwidth = q.width_mb(k3);
p.currdur = q.duration_mb(k3);
[~,y0] = ode45(@run_vm_mb, q.t{i}, incond, [],p);
vm = (p.vl + p.ve*y0(:,2) + p.vi*y0(:,4))./(1 + y0(:,2) + y0(:,4));
y = [y; vm - p.vl];
k3 = k3 + 1;
end
if(q.protocol(i)==4) % minimal motion
p.fb_pos = q.fb_pos_mm(k4);
p.sb_pos = q.sb_pos_mm(k4);
p.width = q.width_mm(k4);
p.fbton = q.fbton_mm + p.toff;
p.fbtoff = q.fbtoff_mm(k4) + p.toff;
p.sbton = q.sbton_mm(k4) + p.toff;
p.sbtoff = q.sbtoff_mm(k4) + p.toff;
[~,y0] = ode45(@run_vm_mm, q.t{i}, incond, [],p);
vm = (p.vl + p.ve*y0(:,2) + p.vi*y0(:,4))./(1 + y0(:,2) + y0(:,4));
y = [y; vm - p.vl];
k4 = k4 + 1;
end
if(q.protocol(i)==1) % moving grating
p.PosD_mg = q.PosD_mg;
p.currdur = q.stimdur_mg(k1);
p.phaseMat = q.phase_mg(k1,:);
[~,y0] = ode45(@run_vm_mg, q.t{i}, incond, [],p);
vm = (p.vl + p.ve*y0(:,2) + p.vi*y0(:,4))./(1 + y0(:,2) + y0(:,4));
y = [y; vm - p.vl];
k1 = k1 + 1;
end
if(q.protocol(i)==2) % static grating
p.tstimmin = p.toff;
p.tstimmax = p.toff + q.stimdur_sg(k2);
p.posD_sg1 = q.posD_sg{k2};
[~,y0] = ode45(@run_vm_sg, q.t{i}, incond, [],p);
vm = (p.vl + p.ve*y0(:,2) + p.vi*y0(:,4))./(1 + y0(:,2) + y0(:,4));
y = [y; vm - p.vl];
k2 = k2 + 1;
end
end
function dydt = run_vm_sb(t,y,p) % single bars
stim = 0;
if(t>=p.tstimmin && t<=p.tstimmax)
stim = 1;
end
ae = 0; ai = 0;
j = find(p.xx==p.location_sb);
for k = 0:(p.width_sb-1)
ae = ae + p.xae(j-k);
ai = ai + p.xai(j-k);
end
ae = ae*stim;
ai = ai*stim;
dydt = rhs_vm(ae,ai,y,p);
function dydt = run_vm_mm(t,y,p) % minimal motion
stim1 = 0;
if(t>=p.fbton && t<=p.fbtoff)
stim1 = 1;
end
stim2 = 0;
if(t>=p.sbton && t<=p.sbtoff)
stim2 = 1;
end
xflag = zeros(size(p.xae));
ae = 0; ai = 0;
if(stim1==1)
j = find(p.xx==p.fb_pos);
for k = 0:(p.width-1)
ae = ae + p.xae(j-k);
ai = ai + p.xai(j-k);
xflag(j-k) = 1;
end
end
if(stim2==1)
j = find(p.xx==p.sb_pos);
for k = 0:(p.width-1)
if(xflag(j-k)~=1)
ae = ae + p.xae(j-k);
ai = ai + p.xai(j-k);
end
end
end
dydt = rhs_vm(ae,ai,y,p);
function dydt = run_vm_mb(t,y,p) %moving bar
istim = floor((t-p.toff)/( p.currdur));
ae = 0; ai = 0;
eff_pos = p.pos;
if(p.flag_dir==1)
for i=1:(p.currwidth-1)
eff_pos = [eff_pos p.pos(end)+i] ;
end
end
if(p.flag_dir==0)
for i=1:(p.currwidth-1)
eff_pos = [p.pos(1)-i eff_pos] ;
end
eff_pos = fliplr(eff_pos);
end
if(t>p.toff && istim<length(eff_pos))
lim_width = (p.currwidth-1);
if(p.flag_dir==1)
sloc = find(p.xx==eff_pos(istim+1));
if(istim==0); lim_width = 0; end
if(istim==1); lim_width = min(1,p.currwidth-1); end
if(istim==2); lim_width = min(2,p.currwidth-1); end
if(istim>=length(p.pos))
sloc = find(p.xx==p.pos(end));
if(istim==length(p.pos)); lim_width = p.currwidth-2; end
if(istim==length(p.pos)+1); lim_width = p.currwidth-3; end
if(istim==length(p.pos)+2); lim_width = p.currwidth-4; end
end
sign_fact=-1;
end
if(p.flag_dir==0)
sloc = find(p.xx==eff_pos(istim+1));
if(istim==0); lim_width = 0; end
if(istim==1); lim_width = min(1,p.currwidth-1); end
if(istim==2); lim_width = min(2,p.currwidth-1); end
if(istim>=length(p.pos))
sloc = find(p.xx==p.pos(1));
if(istim==length(p.pos)); lim_width = p.currwidth-2; end
if(istim==length(p.pos)+1); lim_width = p.currwidth-3; end
if(istim==length(p.pos)+2); lim_width = p.currwidth-4; end
end
sign_fact=1;
end
for k = 0:lim_width
ae = ae + p.xae(sloc+ sign_fact*k);
ai = ai + p.xai(sloc+ sign_fact*k);
end
end
dydt = rhs_vm(ae,ai,y,p);
function dydt = run_vm_mg(t,y,p) % moving grating
istim = floor((t-p.toff)/( p.currdur));
ae = 0; ai = 0;
if(t>p.toff && istim<=3*8)
pos_ind = p.phaseMat( mod(istim,8) + 1 );
pos_d = p.PosD_mg{pos_ind};
for ii = 1:length(pos_d)
ae = ae + p.xae( p.xx==pos_d(ii) );
ai = ai + p.xai( p.xx==pos_d(ii) );
end
end
dydt = rhs_vm(ae,ai,y,p);
function dydt = run_vm_sg(t,y,p) %static grating
stim = 0;
ae = 0; ai = 0;
for ii = 1:length(p.posD_sg1)
ae = ae + p.xae( p.xx==p.posD_sg1(ii) );
ai = ai + p.xai( p.xx==p.posD_sg1(ii) );
end
if(t>=p.tstimmin && t<=p.tstimmax)
stim = 1;
end
ae = ae * stim;
ai = ai * stim;
dydt = rhs_vm(ae,ai,y,p);
function dydt = rhs_vm(ae,ai,y,p)
he = y(1);
ge = y(2);
hi = y(3);
gi = y(4);
dhedt = ( -he + ae ) / p.tre;
dgedt = ( -ge + he ) / p.tde;
dhidt = ( -hi + ai ) / p.tri;
dgidt = ( -gi + hi ) / p.tdi;
dydt = [dhedt; dgedt; dhidt; dgidt];