-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildSpkRecord.m
executable file
·314 lines (282 loc) · 8.73 KB
/
BuildSpkRecord.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
314
function [res, details] = BuildSpikeRecord(path, expts, varargin)
%Plots Block ON/Off and Trials for Expts
%Not really working at the moment - needs store on/off events that
%APlaySpkFile doesn't produce yet
Expts = {};
Clusters = {};
EM = [];
LFP = [];
showspks = [];
state.showcells = [];
state.marksaccades = 0;
state.plotmu = 0;
state.showsacsdf=0;
state.sdftype = 'switch';
state.getlfp = 0;
state.eyetrigger = 1;
j = 1;
while j <= length(varargin)
if iscell(varargin{j}) && isfield(varargin{j}{1},'Trials')
Expts = varargin{j};
elseif iscell(varargin{j}) && isfield(varargin{j}{1},'mahal')
Clusters = varargin{j};
elseif isstruct(varargin{j}) && isfield(varargin{j},'Trials')
if isfield(varargin{j}.Trials,'LFP')
LFP = varargin{j};
else
EM = varargin{j};
end
elseif isstruct(varargin{j}) && isfield(varargin{j},'blklen')
FullVData = varargin{j};
elseif isnumeric(varargin{j}) && ndims(varargin{j}) == 3
CellList = varargin{j};
elseif strncmpi(varargin{j},'lfp',3) %make SDF for stim start with no nonimal stim change
state.getlfp = 1;
elseif strncmpi(varargin{j},'noswitch',4) %make SDF for stim start with no nonimal stim change
state.sdftype = 'noswitch';
elseif strncmpi(varargin{j},'nomu',4)
state.plotmu = 0;
elseif strncmpi(varargin{j},'plotmu',6)
state.plotmu = 1;
elseif strncmpi(varargin{j},'triglfp',5)
state.lfptrigger = 1;
state.eyetrigger = 0;
elseif strncmpi(varargin{j},'sacsdf',5)
state.showsacsdf = 1;
elseif strncmpi(varargin{j},'showspks',5)
j = j+1;
showspks = varargin{j};
elseif strncmpi(varargin{j},'showcells',5)
j = j+1;
state.showcells = varargin{j};
plotmu = 1;
end
j = j+1;
end
if isstruct(path) %plot results
if isempty(state.showcells)
state.showcells = 1:length(path.spktimes);
end
PlotBlock(path, [], state);
return;
end
if length(expts) > 1
for j = 1:length(expts)
GetFigure(sprintf('Expt%d',expts(j)));
[res{j}, details{j}] = BuildSpkRecord(path, expts(j), varargin{:});
end
return;
end
[a,b] = fileparts(path);
monkey = GetMonkeyName(path);
prefix = [monkey b];
eid = expts(1);
res.exptno = expts;
ename = sprintf('%s/%s.%d.mat',path, prefix,eid);
eaname = sprintf('%s/%sA.%d.mat',path, prefix,eid);
emname = strrep(ename,'.mat','.em.mat');
lfpname = strrep(ename,'.mat','.lfp.mat');
cellname = sprintf('%s/CellList.mat',path );
cname = sprintf('%s/Expt%dClusterTimes.mat',path, eid);
if ~exist(ename) && isempty(Expts)
fprintf('Can''t find %s\n',ename);
return;
end
if ~exist(cname) && isempty(Clusters)
fprintf('Can''t find %s\n',cname);
return;
end
if exist(cellname)
load(cellname);
end
if isempty(Expts)
[a, Expts] = APlaySpkFile(ename,'bysuffix');
end
if isempty(Clusters)
[Clusters, FullVData] = LoadClusters(path,eid);
end
if isempty(showspks) && isempty(state.showcells)
showspks = 1:length(Clusters);
end
if exist(emname) && isempty(EM)
load(emname);
EM = CheckSaccades(Expt,'allsz');
end
Expt = Expts{1};
ibs = [Expt.Trials.IB];
new = find(abs(diff(ibs)) > 0)+1;
noswitchid = setdiff(1:length(ibs),new);
stimtime = [Expt.Trials(new).Start]./10000;
res.stimids = ibs(new);
res.starttime = [Expt.Trials(noswitchid).Start]./10000;
ns = length(stimtime);
details.Expt = Expt;
res.spktimes = {};
for j = 1:length(FullVData.blkstart)
res.blocktimes(1,j) = FullVData.blkstart(j);
res.blocktimes(2,j) = FullVData.blkstart(j)+FullVData.blklen(j).*FullVData.samper;
end
if exist('CellList','var')
e = find(CellDetails.exptids == eid);
doneprobes = zeros(1,size(CellList,2));
for j = 1:length(state.showcells)
[pid, cid] = find(squeeze(CellList(e,:,:)) == state.showcells(j));
if length(pid)
if cid == 1
res.spktimes{j} = Clusters{pid}.times;
else
if ~isfield(Clusters{pid}.next{cid-1},'times')
Clusters{pid}.next{cid-1}.times = Clusters{pid}.t(Clusters{pid}.clst == cid+1);
end
res.spktimes{j} = Clusters{pid}.next{cid-1}.times;
end
if plotmu
row = pid;
else
row = j;
end
doneprobes(pid) = 1;
res.suprobes(j) = pid;
res.cellids(j) = state.showcells(j);
end
end
else
res.spktimes = {};
doneprobes = zeros(1, length(Clusters));
end
id = find(doneprobes ==0);
res.muprobes = id;
for j = 1:length(id)
res.mutimes{j} = Clusters{id(j)}.times;
end
endtime = [Expt.Trials(new).End];
IBid = [Expt.Trials(new).Start];
%PlotTrialOnOff(stimtime,endtime);
res.stimtime = stimtime;
details.nsdf = PlotBlock(res, EM, state, LFP);
function nsdf = PlotBlock(B, EM, state, varargin)
nsdf = [];
LFP = [];
j = 1;
while j <= length(varargin)
if isstruct(varargin{j}) && isfield(varargin{j},'Trials')
LFP = varargin{j};
end
j = j+1;
end
hold off;
for j = 1:length(B.stimtime)
plot([B.stimtime(j) B.stimtime(j)],[0 1]);
hold on;
text(B.stimtime(j),0.5,sprintf('%d',B.stimids(j)));
end
for j = 2:size(B.blocktimes,2)
x(1) = B.blocktimes(2,j-1);
x(2) = B.blocktimes(1,j);
y = [0 1];
plot(x,y,'r-','linewidth',3);
y = [1 0];
plot(x,y,'r-','linewidth',3);
end
for j = 1:length(state.showcells)
k = find(B.cellids == state.showcells(j));
if length(k) == 1
plot(B.spktimes{k},B.suprobes(k),'r.');
end
end
if state.plotmu
for j = 1:length(B.mutimes)
plot(B.mutimes{j},B.muprobes(j),'k.');
end
end
if isfield(EM,'Trials')
for j = 1:length(EM.Trials);
t = EM.Trials(j).Start./10000 + [1:length(EM.Trials(j).Eyevals.rh)].*EM.Header.CRrates(1);
plot(t,EM.Trials(j).Eyevals.rh);
end
end
oldfig = gcf;
GetFigure('sdfs');
hold off;
colors = mycolors;
Trials.Trigger = B.stimtime .* 10000;
if state.showsacsdf && isfield(EM,'Trials')
Trials.Trigger = [];
ts = -5000:10:5000;
for j = 1:length(EM.Trials)
id = find([EM.Trials(j).Saccades.size] > 0.2);
Trials.Trigger = cat(2, Trials.Trigger, EM.Trials(j).Start + [EM.Trials(j).Saccades(id).start]);
end
else
ts = -10000:10:5000;
Trials.Trigger = B.stimtime.*10000;
if strmatch(state.sdftype,'noswitch')
Trials.Trigger = B.starttime.*10000;
end
end
if state.plotmu
spktimes = [B.spktimes(:) B.mutimes(:)];
else
spktimes = B.spktimes;
end
a = minmax(Trials.Trigger);
for j = 1:length(spktimes)
Trials.Spikes = (spktimes{j} .*10000)';
sdf = trigsdfa(Trials,100,ts,'freetimes');
nsdf(j,:) = sdf./mean(sdf);
plot(ts,sdf,'color',colors{j});
hold on;
end
details.nsdf = nsdf;
if state.eyetrigger
GetFigure('EyeSpeed');
hold off;
tsmp = -1200:1200;
for j = 1:length(EM.Trials)
id = find(Trials.Trigger > EM.Trials(j).Start & Trials.Trigger < EM.Trials(j).End)
if length(id)
sid = round((Trials.Trigger(id) - EM.Trials(j).Start)./(EM.Header.CRrates(1).*10000));
speed = sqrt(sum(diff(EM.Trials(j).EyeData)'.^2));
tval = repmat(tsmp',1,length(sid))+ repmat(sid,length(tsmp),1);
tval(tval < 1) = 1;
tval(tval > length(speed)) = length(speed);
ms(j,:) = mean(speed(tval),2);
end
end
plot(tsmp.*EM.Header.CRrates(1),mean(ms,1));
details.eyespeed = mean(ms,1);
elseif state.lfptrigger
tsmp = 0:200;
for j = 1:length(LFP.Trials)
id = find(Trials.Trigger > LFP.Trials(j).Start & Trials.Trigger < LFP.Trials(j).End);
if length(id)
sid = round((Trials.Trigger(id) - LFP.Trials(j).Start)./(LFP.Header.CRsamplerate.*10000));
tval = repmat(tsmp',1,length(sid))+ repmat(sid,length(tsmp),1);
tval(tval < 1) = 1;
tval(tval > size(LFP.Trials(j).LFP,1)) = size(LFP.Trials(j).LFP,1);
for p = 1:size(LFP.Trials(j).LFP,2)
x = LFP.Trials(j).LFP(:,p);
ms(j,p,:) = mean(x(tval),2);
end
end
end
details.triglfp = squeeze(mean(ms,1));
imagesc(tsmp.*LFP.Header.CRsamplerate,1:size(ms,2),details.triglfp);
end
figure(oldfig);
if state.marksaccades
for j = 1:length(Trials.Trigger)
plot([Trials.Trigger(j) Trials.Trigger(j)]./10000,[-10 1],'g:');
end
end
function PlotTrialOnOff(S, E)
ns = length(S);
x(1:4:ns*4) = S;
x(2:4:ns*4) = S;
y(1:4:ns*4) = 0;
y(2:4:ns*4) = 1;
x(3:4:ns*4) = E;
x(4:4:ns*4) = E;
y(3:4:ns*4) = 1;
y(4:4:ns*4) = 0;
plot(x,y);