-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGABEjnkWaveforms.m
492 lines (381 loc) · 9.97 KB
/
GABEjnkWaveforms.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
Exp = io.dataFactoryGratingSubspace(1);
%%
tstart = Exp.ptb2Ephys(cellfun(@(x) x.STARTCLOCKTIME, Exp.D(:)));
tstop = Exp.ptb2Ephys(cellfun(@(x) x.ENDCLOCKTIME, Exp.D(:)));
epochs = [tstop(1:end-1) tstart(2:end)];
%%
W1 = io.get_waveform_stats(Exp.osp);
W = io.get_waveform_stats(Exp.osp, 'validEpochs', epochs);
%%
figure(1); clf
NC = numel(W);
sx = ceil(sqrt(NC));
sy = round(sqrt(NC));
for cc = 1:NC
subplot(sx, sy, cc, 'align')
ix = W(cc).lags>=0;
plot(W(cc).lags(ix), W(cc).isi(ix)); hold on
plot(W1(cc).lags(ix), W1(cc).isi(ix))
set(gca, 'xscale', 'log')
end
legend({'ITI', 'ALL'})
figure, histogram(arrayfun(@(x) x.BRI, W), 100)
%%
[stim, robs, grating] = io.preprocess_grating_subspace_data(Exp);
[Xspat, Rspat, spopts] = io.preprocess_spatialmapping_data(Exp, 'ROI', [-200 -200 300 300]);
%%
X = stim{1};
X = X(:,sum(X)>0);
y = robs(:,Exp.osp.cids(2));
sta = simpleRevcorr(X, y-mean(y), 15);
figure(1); clf
imagesc(sta)
%%
cc = cc + 1;
sta = simpleRevcorr(Xspat, Rspat(:,cc)-mean(Rspat(:,cc)), 20);
figure(1); clf
imagesc(reshape(sum(sta), spopts.dims))
%% get waveform stats across all experimental sessions
W = [];
exid = [];
for iEx = 1:2; %55
tic;
Exp = io.dataFactoryGratingSubspace(iEx);
% trial starts and stops
tstart = Exp.ptb2Ephys(cellfun(@(x) x.STARTCLOCKTIME, Exp.D(:)));
tstop = Exp.ptb2Ephys(cellfun(@(x) x.ENDCLOCKTIME, Exp.D(:)));
% inter-trial intervals
epochs = [tstop(1:end-1) tstart(2:end)];
% get waveform statistics over valid epochs
W_ = io.get_waveform_stats(Exp.osp, 'validEpochs', epochs);
% append session to struct-array of units
exid = [exid; iEx*ones(numel(W_),1)];
W = [W; W_];
toc
end
%% get all waveforms and useful metrics
fprintf('%d total units\n', numel(W_keep))
% waveforms shifted to peak or trough
wf = cell2mat(arrayfun(@(x) x.shiftwaveform(:,3)', W_keep, 'uni', 0));
% ISI autocorrelation
isi = cell2mat(arrayfun(@(x) x.isi(:)', W_keep, 'uni', 0));
p2t = arrayfun(@(x) x.t2p_interp, W_keep, 'uni', 1);
% unit quality measures
isiL = arrayfun(@(x) x.isiL, W_keep); % variability around detrended isi
isiR = arrayfun(@(x) x.isiRate, W_keep); % refractory rate / expected rate
localIdx = arrayfun(@(x) x.localityIdx, W_keep); % wf amplitude ch-2 / ch center
BRI = arrayfun(@(x) x.BRIJake, W_keep); % burstiness-refractoriness index
figure(1); clf
ix = localIdx < .5 & isiR<1 & p2t > 0;
fprintf('%d/%d units are putative SU\n', sum(ix), numel(W_keep))
histogram(p2t(ix)*1e3, linspace(-1, 1, 80) );
xlabel('Peak - Trough (ms)')
figure(2); clf
wx = (wf(ix,:)); %./max(wf(ix,:),[],2));
imagesc( wx )
figure(3); clf
% normalized waveforms
wxn = (wx - min(wx,[],2))./(max(wx,[],2)- min(wx,[],2));
peaksorters = wxn(:,21) > 0;
troughsorters = wxn(:,21)==0;
p = p2t(ix)*1e3; % peak - trough (miliseconds)
bri = BRI(ix);
a = wxn(peaksorters,:);
[~, ind] = sort(p(troughsorters));
w = wxn(troughsorters,:);
b = w(ind,:);
imagesc([a; b])
plot.fixfigure(gcf, 12, [4 6], 'offsetAxes', false)
figure(4); clf
% histogram(p2t(:)*1e3, 100, 'EdgeColor', 'none', 'FaceColor', .5*[1 1 1]); hold on
h = histogram(p(:), 50, 'EdgeColor', 'none', 'FaceColor', lines(1), 'Normalization', 'pdf'); hold on
xlabel('Peak - Trough (ms)')
plot.fixfigure(gcf, 12, [4 4])
n = 5;
AIC = zeros(n,1);
BIC = zeros(n,1);
for c = 1:n
gmdist = fitgmdist(p,c);
xx = linspace(min(xlim), max(xlim), 100);
plot(xx, gmdist.pdf(xx'))
AIC(c) = gmdist.AIC;
BIC(c) = gmdist.BIC;
end
%%
figure(1); clf
plot(BIC); hold on
xlabel('# of mixing distributions')
ylabel('BIC')
[~, id] = min(BIC);
plot([1 1]*id, ylim, 'k--')
plot.fixfigure(gcf, 12, [4 4])
figure(4); clf
% histogram(p2t(:)*1e3, 100, 'EdgeColor', 'none', 'FaceColor', .5*[1 1 1]); hold on
h = histogram(p(:), 50, 'EdgeColor', 'none', 'FaceColor', lines(1), 'Normalization', 'pdf'); hold on
xlabel('Peak - Trough (ms)')
plot.fixfigure(gcf, 12, [4 4])
gmdist = fitgmdist(p,id);
xx = linspace(min(xlim), max(xlim), 100);
plot(xx, gmdist.pdf(xx'))
%%
clusts = cluster(gmdist, p);
figure(6); clf
clu = unique(clusts);
nclust = numel(clu);
cmap = lines(nclust);
cmap = [cmap .25*ones(nclust,1)];
for cc = 1 %1:nclust
ii = clusts==clu(cc);
nts = size(wxn,2);
plot(W(1).shiftlags*1e3, wxn(ii, :)', 'Color', cmap(cc,:)); hold on
text(nts, .5 + .3*cc/nclust, sprintf('n=%d', sum(ii)), 'Color', cmap(cc,:))
end
axis tight
xlabel('Milliseconds from trough')
ylabel('Amplitude (normalized)')
plot.fixfigure(gcf, 12, [4 4])
%%
figure(7); clf
ax = subplot(3,3,[1 4]);
histogram(bri, 100)
xlim(ax, [0 20])
view(-90,90)
ax = subplot(3,3,[2:3 5:6]);
for cc = 2%nclust
ii = clusts==clu(cc);
plot(p(ii), bri(ii), 'o', 'Color', cmap(cc,:), 'MarkerFaceColor', cmap(cc,1:3), 'MarkerSize', 5); hold on
end
set(gca, 'yscale', 'log')
xlabel('Peak - Trough (ms)')
ylabel('BRI')
%plot.fixfigure(gcf, 12, [4 4])
%ylim(ax,[0 20])
%xlim(ax,[0 .85])
ax = subplot(3,3,8:9);
histogram(p, 100)
xlim(ax, [0 .85])
% figure(2); clf
% histogram(
%% Get NW cells only
W_NW = W_keep(clusts==1);
%%
ac = isi(ix,:);
ac = (ac - min(ac,[],2)) ./ (max(ac,[],2) - min(ac,[],2));
wxn(isnan(wxn)) = 0;
figure(1); clf
for cc = 1:nclust
subplot(nclust, 2, (cc-1)*2 + 1, 'align')
ii = clusts==clu(cc);
wtmp = wxn(ii,:);
ptmp = p(ii);
[~, ind] = sort(ptmp);
imagesc(wtmp(ind,:))
title(sprintf('Waveforms Group %d', cc))
subplot(nclust, 2, (cc-1)*2 + 2, 'align')
atmp = ac(ii,:);
lags = W(1).lags;
atmp = atmp(:,lags>0.001);
[~, id] = max(atmp, [], 2);
[~, ind] = sort(id);
imagesc(atmp(ind,:))
title(sprintf('AutoCorrelation Group %d', cc))
end
%%
figure(5); clf
lags = W(1).lags;
[~, id] = max(ac(:,lags>0.001), [], 2);
[~, ind] = sort(id);
imagesc(ac(ind,lags>0.001))
figure(6); clf
[X, binsx, binsy] = histcounts2(p, bri, 0:.05:1.5, 0:.5:15);
h = imagesc(binsx,binsy, X'); colormap(flipud(viridis))
h.AlphaData = h.CData/max(h.CData(:));
axis xy
hold on
% f = plot(p, lx(id)', '.', 'Color', cmap(1,:));
xlabel('Peak - Trough')
ylabel('AC peak')
%%
figure(1); clf
C = corr(wxn') + corr(ac');
C = 1 - C;
imagesc(C)
%%
Z = linkage(C, 'weighted');
d = pdist([wxn isi(ix,:)]);
order = optimalleaforder(Z, d);
figure
imagesc(C(order,order))
figure
dendrogram(Z)
c = cluster(Z, 'maxclust', 10);
%%
figure(1); clf
cs = unique(c);
cmap = lines(numel(cs));
cmap = [cmap .25*ones(numel(cs),1)];
for i = 1:numel(cs)
ix = c==cs(i);
f = plot(wxn(ix, :)', 'Color', cmap(i,:)); hold on
end
%%
%%
% ac = imgaussfilt(ac, [.001 3]);
a = ac(peaksorters,:);
w = ac(troughsorters,:);
b = w(ind,:);
imagesc(([a; b]), [0 20])
plot.fixfigure(gcf, 12, [6 4])
%%
figure
plot(ac')
%%
figure(7); clf
ac = (ac - min(ac,[],2)) ./ (max(ac, [], 2) - min(ac, [], 2));
ii = p < 0 | p > 1;
n(1) = sum(ii);
nts = size(ac,2);
f = plot(ac(ii, :)', 'g'); hold on
for i = 1:numel(f)
f(i).Color(4) = .5;
end
text(nts, .7, sprintf('n=%d', n(1)), 'Color', 'g')
ii = p > 0 & p < .35;
n(2) = sum(ii);
f = plot(ac(ii, :)', 'b'); hold on
for i = 1:numel(f)
f(i).Color(4) = .5;
end
text(nts, .82, sprintf('n=%d', n(2)), 'Color', 'b')
ii = p > 0.35 & p < 1;
n(3) = sum(ii);
f = plot(ac(ii, :)', 'r');
for i = 1:numel(f)
f(i).Color(4) = .5;
end
text(nts, .88, sprintf('n=%d', n(3)), 'Color', 'r')
plot.fixfigure(gcf, 12, [6 4])
%%
x = w;
Y = tsne(x, 'NumPCAComponents', 10, 'Algorithm','barneshut',...
'Distance', 'minkowski', 'Standardize',true,'Perplexity',50);
X = Y;
d = pdist(X);
z = linkage(d, 'ward');
c = cluster(z, 'maxclust', 5);
figure(1); clf
gscatter(X(:,1), X(:,2), c)
%% clustering
wtmp = wxn;
itmp = isi(ix,:);
cinds = unique(c);
nc = numel(cinds);
cmap = lines(nc);
figure(2); clf
for ci = 1:nc
subplot(1, 2, 1)
x = wtmp(c==ci,:);
m = nanmean(x);
s = 2*nanstd(x)/sqrt(sum(c==ci));
plot.errorbarFill(1:numel(m), m,s, 'k', 'EdgeColor', 'none', 'FaceAlpha', .25, 'FaceColor', cmap(ci,:)); hold on
plot(m, 'Color', cmap(ci,:))
subplot(1, 2, 2)
plot(mean(itmp(c==ci,:))); hold on
end
%%
wi = reshape(wx, sum(ix), [], 5);
I = wi(:,:,3);
imagesc(I)
%
% plot(std(I,[],2), '.')
% w = I(std(I,[],2)>4,:);
% imagesc(w)
%
% figure(3); clf
% plot(w(:,20:80)')
%%
i = i + 1;
if i > numel(W)
i = 1;
end
figure(1); clf
subplot(121)
plot(W(i).waveform)
title(exid(i))
subplot(122)
plot(isi(i,:))
title(i)
%%
cids = Exp.osp.cids;
pairs = nchoosek(cids, 2);
%%
ipair = ipair + 1;
if ipair > size(pairs,1)
ipair = 1;
end
st1 = Exp.osp.st(Exp.osp.clu==cids(pairs(ipair,1)));
st2 = Exp.osp.st(Exp.osp.clu==cids(pairs(ipair,2)));
[xc, lags, err] = crossCorrelation(st1, st2);
figure(1); clf
plot.errorbarFill(lags, xc, err); hold on
plot(lags, xc, 'k')
%%
%%
figure(1); clf
isi = isi;
x = isi(:,1:100); x = x - mean(x,2);
s = std(x,[],2);
% ix = sum(isnan(wf),2) == 0 & uQ > 1 & isi(:,202)<0;% & isiV < .5 & s < 0.0015;
subplot(121)
imagesc(wf(ix,:))
subplot(122)
imagesc(isi(ix,:))
%%
% -mean(wf(ix,:),2)
x = wf(ix,:)-mean(wf(ix,:),2);
x = x ./ max(x,[],2);
% x = [isi(ix,:)];
Y = tsne(x, 'NumPCAComponents', 4, 'Algorithm','barneshut',...
'Distance', 'minkowski', 'Standardize',true,'Perplexity',50);
% Z = tsne(isi(ix,:)-mean(isi(ix,:),2), 'NumPCAComponents',4, 'Algorithm','exact','Standardize',true,'Perplexity',50);
X = Y;
d = pdist(X);
z = linkage(d, 'ward');
c = cluster(z, 'maxclust', 5);
figure(1); clf
gscatter(X(:,1), X(:,2), c)
%%
wtmp = wf(ix,:);
itmp = isi(ix,:);
cinds = unique(c);
nc = numel(cinds);
cmap = lines(nc);
figure(2); clf
for ci = 1:nc
subplot(1, 2, 1)
x = wtmp(c==ci,:);
m = mean(x);
s = 2*std(x)/sqrt(sum(c==ci));
plot.errorbarFill(1:numel(m), m,s, 'k', 'EdgeColor', 'none', 'FaceAlpha', .25, 'FaceColor', cmap(ci,:)); hold on
plot(m, 'Color', cmap(ci,:))
subplot(1, 2, 2)
plot(mean(itmp(c==ci,:))); hold on
end
%%
figure(1); clf
for ci = 1:nc
x = itmp(c==ci,:);
subplot(1,nc,ci)
imagesc(x)
end
%%
figure(1); clf
[~, ind] = sort(p2t(ix));
% [~, ind] = sort(itmp(:,199));
subplot(121)
imagesc(wtmp(ind,:))
subplot(122)
imagesc(itmp(ind,:))
%%
clf
plot(p2t(ix), mean(itmp(:,198:199),2), '.')