-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayMovie.m
executable file
·313 lines (284 loc) · 8.07 KB
/
ArrayMovie.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
function ArrayMovie(Expt, ids, varargin)
%ArrayMovie(Expt, ids, varargin)
%Plot an array Expt data strcture as a movie
%Expt can be and LFP Expt or spikes
%can also be a matriz of resps, in which case ids must
%contains details
%
%Use with AllExpt2mat
timerange = [];
j = 1;
while j <= length(varargin)
if strncmpi(varargin{j},'timerange',6)
j = j+1;
timerange = varargin{j};
end
j = j+1;
end
if isfield(Expt,'Header')
tid = find(ismember([Expt.Trials.id],ids));
res.lfpresp = mean(cat(3,Expt.Trials(tid).LFP),3);
Array = GetArrayConfig(Expt.Header.loadname);
if isfield(Expt.Header,'lfptimes') % old files
res.lfptimes = Expt.Header.lfptimes;
else
res.lfptimes = Expt.Header.LFPtimes;
end
elseif isnumeric(Expt)
if isfield(ids,'sdfs') && isfield(ids.sdfs,'lfptimes') %RevCor return struct
Array = GetArrayConfig(ids.Header.loadname);
res.lfptimes = ids.sdfs.lfptim\es;
elseif isfield(ids,'X') && isfield(ids,'id')
Array = ids;
else %details from AllExpt2mat
Array = GetArrayConfig(ids.loadname);
res.lfptimes = ids.times;
if isfield(ids,'celllist')
res.spkresp = Expt;
end
end
if ~isfield(res,'spkresp')
res.lfpresp = Expt;
end
end
if ~isempty(timerange)
id = find(res.lfptimes > timerange(1) & res.lfptimes < timerange(end));
res.lfptimes = res.lfptimes(id);
res.lfpresp = res.lfpresp(id,:);
end
nx = max(Array.X);
ny = max(Array.Y);
res.arraysize = [nx ny];
res.probemap = sub2ind([nx ny],Array.X,Array.Y);
PlotMovie(res,0.01, res.lfptimes);
function NextFrame(a,b,step)
D = get(gcf,'UserData');
t = D.inow;
if step == 0
t = t+1;
if t > length(D.mtimes)
D.mtimes = [D.mtimes D.mtimes(end)+mean(diff(D.mtimes))];
t = length(D.mtimes);
end
elseif step < 0
t = t+step;
if t < 1
t = 1;
end
end
PlotTimeSlice(D,D.mtimes(t));
D.inow = t;
set(gcf,'UserData',D);
function FrameSlider(a,b,step)
D = get(gcf,'UserData');
t = get(a,'value');
it = round(t .* length(D.mtimes)./200);
if it > 0
PlotTimeSlice(D,D.mtimes(it));
end
drawnow;
D.inow = it;
set(gcf,'UserData',D);
function PlayFrames(a,b,step)
D = get(gcf,'UserData');
t = D.inow;
set(findobj(gcf,'Tag','PlayStop'),'value',0);
if step > 0
if D.inow >= length(D.mtimes)
D.inow = 1;
end
t = [D.inow:step:length(D.mtimes)];
elseif step < 0
t = [D.inow:step:1];
end
for it = t
stop = get(findobj(gcf,'Tag','PlayStop'),'value');
if stop
break;
end
PlotTimeSlice(D,D.mtimes(it));
drawnow;
end
D.inow = it;
set(gcf,'UserData',D);
function res = PlotMovie(res, delay, mtimes)
sustep = 1000;
lfpstep = 10000;
if isfield(res,'spkresps')
res.surange(1) = min(res.spkresps(:));
res.surange(2) = max(res.spkresps(:));
elseif isfield(res,'spkresp')
res.surange(1) = min(res.spkresp(:));
res.surange(2) = max(res.spkresp(:));
end
if isfield(res,'lfpresp')
res.lfprange(1) = min(res.lfpresp(:));
res.lfprange(2) = max(res.lfpresp(:));
end
res.mtimes = mtimes;
PlotTimeSlice(res, res.mtimes(1));
fb = findobj(gcf,'Tag','NextFrame');
if isempty(fb)
bp = [10 10 40 20];
uicontrol(gcf, 'style','pushbutton','string','+','Position',bp,...
'Tag','NextFrame','Callback',{@NextFrame, 0});
end
fb = findobj(gcf,'Tag','LastFrame');
if isempty(fb)
bp = [50 10 40 20];
uicontrol(gcf, 'style','pushbutton','string','-','Position',bp,...
'Tag','LastFrame','Callback',{@NextFrame, -1});
end
fb = findobj(gcf,'Tag','PlayFwd');
if isempty(fb)
bp = [90 10 40 20];
uicontrol(gcf, 'style','pushbutton','string','>>','Position',bp,...
'Tag','PlayFwd','Callback',{@PlayFrames, 1});
end
fb = findobj(gcf,'Tag','PlayBack');
if isempty(fb)
bp = [130 10 40 20];
uicontrol(gcf, 'style','pushbutton','string','<<','Position',bp,...
'Tag','PlayBack','Callback',{@PlayFrames, -1});
end
fb = findobj(gcf,'Tag','PlayStop');
if isempty(fb)
bp = [170 10 60 20];
uicontrol(gcf, 'style','check','string','Stop','Position',bp,...
'Tag','PlayStop');
bp(1) = bp(1)+bp(3);
bp(3) = 200;
res.slider = uicontrol(gcf, 'style','slider','string','t','Position',bp,...
'Tag','PlaySlider','Min',0,'max',200,'value',0,'sliderstep',[0.005 0.025],'CallBack',@FrameSlider);
else
set(fb(1),'value',0);
end
set(gcf,'UserData',res);
ofig = gcf;
subplot(2,1,1); hold off;
subplot(2,1,2); hold off;
for ti = 1:length(res.mtimes)
stop = get(findobj(gcf,'Tag','PlayStop'),'value');
if stop
ti = NaN;
else
set(0,'currentfigure',ofig);
PlotTimeSlice(res,res.mtimes(ti));
res.inow = ti;
set(gcf,'UserData',res);
drawnow;
if delay > 1
pause
else
pause(delay);
end
end
end
function PlotTimeSlice(res,t);
nplots = 1;
if isfield(res,'lfprespa')
subplot(2,1,1);
nplots = 2;
[a,b] = min(abs(t-res.lfptimes));
if res.plot
imagesc(res.x(:,1),res.probes,squeeze(res.lfprespa(b,:,:))' - squeeze(res.lfpresp(b,:,:))');
else
imagesc(res.x(:,1),res.probes,squeeze(res.lfprespa(b,:,:))');
end
caxis(res.lfprange);
if isfield(res,'titlestr')
title(sprintf('%.1f ms %s',res.lfptimes(b)./10,res.titlestr{2}));
else
title(sprintf('%.1f ms',res.lfptimes(b)./10));
end
elseif isfield(res,'spkresp')
subplot(1,1,1);
nplots = 1;
[a,b] = min(abs(t-mean(res.mtimes,1)));
if isfield(res,'probemap')
im = zeros(res.arraysize);
if b > size(res.spkresp,2)
im(res.probemap) = res.spkresp(:,end);
else
im(res.probemap) = res.spkresp(:,b);
end
imagesc(im);
else
imagesc(squeeze(res.spkresp(b,:))');
end
caxis(res.surange);
title(sprintf('%.1f ms',res.mtimes(1,b)));
slidertime = res.mtimes(b);
elseif isfield(res,'spkresps')
subplot(2,1,1);
nplots = 2;
[a,b] = min(abs(t-mean(res.times,1)));
imagesc(squeeze(res.spkresps(b,:,:))');
caxis(res.surange);
title(sprintf('%.1f ms',res.times(1,b)./10));
slidertime = res.times(b)./10;
end
if isfield(res,'lfpresp')
if nplots == 1
subplot(1,1,1);
else
subplot(2,1,2);
end
[a,b] = min(abs(t-res.lfptimes));
if isfield(res,'lfprespa') & res.plot
title(sprintf('%.1f ms',res.lfptimes(b)./10));
imagesc(squeeze(res.lfprespa(b,:,:)+res.lfpresp(b,:,:))'./2);
elseif isfield(res,'probemap')
im = zeros(res.arraysize);
im(res.probemap) = res.lfpresp(b,:);
imagesc(im);
else
imagesc(res.x(:,1),res.probes,squeeze(res.lfpresp(b,:,:))');
end
caxis(res.lfprange);
if isfield(res,'titlestr')
title(sprintf('%.1f ms %s',res.lfptimes(b)./10,res.titlestr{1}));
else
title(sprintf('%.1f ms',res.lfptimes(b)./10));
end
slidertime = res.lfptimes(b)./10;
end
if isfield(res,'slider')
if slidertime <= get(res.slider,'Max') & slidertime >= get(res.slider,'Min')
set(res.slider,'value',slidertime);
end
end
function PlotTimeCourseProbes(rc, probes, style, sumy)
showvar = 1;
[nr,nc] = Nsubplots(length(probes));
if sumy == 2
ny = ceil(length(rc.yvals)/2);
yv = rc.yvals(1:ny);
zv = rc.yvals((ny+2):length(rc.yvals));
end
cr = [min(rc.lfp(:)) max(rc.lfp(:))];
for probe = probes
subplot(nr,nc,probe);
if sumy == 1
if style == 1
plot(rc.lfptimes./10,rc.lfpresp(:,:,probe)');
if showvar
v = var(rc.lfpresp(:,:,probe),[],2);
scale = max(max(rc.lfpresp(:,:,probe)))./max(v);
hold on;
plot(rc.lfptimes./10,v.*scale,'k');
hold off;
end
else
imagesc(rc.x(:,1),rc.lfptimes./10,rc.lfpresp(:,:,probe));
caxis(cr);
end
elseif sumy == 2 %diff
if style == 1
plot(rc.lfptimes./10,sum(rc.lfp(:,:,yv,probe),3)-sum(rc.lfp(:,:,zv,probe),3));
else
imagesc(rc.x(:,1),rc.lfptimes./10,sum(rc.lfp(:,:,yv,probe),3)-sum(rc.lfp(:,:,zv,probe),3));
caxis(cr);
end
end
end