-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmerge_posthoc6.m
447 lines (342 loc) · 11.6 KB
/
merge_posthoc6.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
function rez = merge_posthoc6(rez)
fpThresh = 0.01;
ops = rez.ops;
Nfilt = rez.ops.Nfilt;
tfi = rez.iNeigh;
tf = rez.cProj;
clusterIDs = rez.st3(:,2);
nSpikes = size(rez.st3,1);
fmax = zeros(nSpikes,1, 'single');
ppairs = cell(Nfilt,1); % potential pairs
fpRate = ones(Nfilt,1);
nspikes = zeros(Nfilt,1);
depths = rez.ypos(:);
refDur = 30;
temps = reshape(rez.dWU, [], Nfilt);
for testID = 1:Nfilt
spikesTest = clusterIDs==testID;
pp = tfi(:, testID);
pp(pp==testID) = [];
ppairs{testID} = pp;
[~, isame] = min( abs(tfi(:, testID)-testID));
fmax(spikesTest, 1) = tf(spikesTest, isame);
nspikes(testID) = sum(spikesTest);
if nspikes(testID)==0
continue
end
fpRate(testID) = ISIViolations(rez.st3(spikesTest,1), 0, refDur);
end
%% Step 1: only merge clusters with no fp violations
% clear iMegaC
iMegaC = {};
new_spikes = [];
picked = zeros(Nfilt,1);
picked(fpRate > fpThresh) = true; % eliminate false positive violations
inewclust = 0;
valid = find(~picked);
[~, order] = sort(nspikes(valid), 'descend');
clustList = valid(order);
for iseed = 1:numel(clustList)
fprintf('New Cluster Total: %d\n', inewclust)
this = clustList(iseed);
if picked(this)
continue
end
run_list = this;
do_not_consider = [];
all_possible = setdiff(tfi(:, find(sum(tfi==this))), this);
all_possible = union(ppairs{this}, all_possible(:));
all_possible = intersect(all_possible, valid);
if exist('iMegaC', 'var')
all_possible = setdiff(all_possible, unique(cell2mat(iMegaC)));
end
pair_list = all_possible(abs(depths(all_possible)-depths(this)) < 1);
consider = pair_list;
% consider = pair_list(fpRate(pair_list) < fpThresh);
% do_not_consider = [do_not_consider; setdiff(all_possible, consider)];
strun = find(clusterIDs==this);
% npairs to consider merging
nc = numel(consider);
%% first to consider
for iconsider = 1:nc
ipair = consider(iconsider);
imm = ismember(tfi(:, ipair), run_list);
if nspikes(ipair) < 100 % don't merge new units that don't spike
do_not_consider = [do_not_consider; ipair];
continue
end
rho = corr(temps(:, [run_list ipair]));
rho = mean(rho(~triu(rho)));
if rho < .5
do_not_consider = [do_not_consider; ipair];
continue
end
if ~any(imm)
do_not_consider = [do_not_consider; ipair];
continue
end
new_spikes = find(clusterIDs==ipair);
f1new = max(tf(new_spikes, imm), [], 2);
f2new = fmax(new_spikes);
f1old = fmax(strun);
f2old = NaN * ones(numel(f1old), 1, 'single');
if ~ismember(tfi(:, run_list), ipair)
feats = intersect(tfi(:,ipair), tfi(:,run_list));
else
feats = ipair;
end
i0 = 0;
for j = 1:length(run_list)
ifeat = find(ismember(tfi(:, run_list(j)), setdiff(feats, run_list(j))),1);
if ~isempty(ifeat)
ix = clusterIDs == run_list(j);
f2old(i0 + (1:nspikes(run_list(j)))) = tf(ix, ifeat);
i0 = i0 + nspikes(run_list(j));
end
end
f1old(isnan(f2old))=[];
f2old(isnan(f2old))=[];
figure(2); clf
plot(f1old, f2old, '.')
hold on
plot(f1new, f2new, '.')
drawnow
if isempty(f1old)
do_not_consider = [do_not_consider; ipair];
continue
end
mo = merging_score(f1old - f2old, f1new-f2new, ops.fracse);
if mo > 3
do_not_consider = [do_not_consider; ipair];
continue
end
% fprintf('Checking xcorr\n')
% % for xcorr analysis
% binSize = 30;
% nlags = 100;
% s1 = rez.st3(strun,1);
% s1 = ceil(s1/binSize);
%
%
% s2 = rez.st3(new_spikes,1);
% s2 = ceil(s2/binSize);
%
% num = max(max(s1), max(s2));
%
% sp1 = full(sparse(s1, ones(numel(s1),1), ones(numel(s1),1), num, 1));
% sp2 = full(sparse(s2, ones(numel(s2),1), ones(numel(s2),1), num, 1));
%
% xc3 = xcorr(sp1, sp2, nlags, 'unbiased');
% if sum(sp1) > sum(sp2)
% xc1 = xcorr(sp1, nlags, 'unbiased');
% else
% xc1 = xcorr(sp2, nlags, 'unbiased');
% end
%
% xc1(nlags+1) = 0;
% xc1 = smooth(xc1, 5);
% xc3 = smooth(xc3, 5);
%
% nfun = @(x) x/norm(x);
% xc1 = nfun(xc1);
% xc3 = nfun(xc3);
%
% figure(1); clf
% lags = -nlags:nlags;
% plot(lags,xc1, lags, xc3)
% xproj = corr([xc1 xc3]);
% title(xproj(2))
% drawnow
%
%
% if xproj(2) > .7
fprintf('Merging %d\n', ipair)
strun = cat(1, strun, new_spikes);
run_list(end+1) = ipair;
picked(ipair) = 1;
% pair_list = unique(cat(1, pair_list, ppairs{ipair}));
% pair_list(ismember(pair_list, run_list)) = [];
% end
end
if numel(run_list) > 1
inewclust = inewclust + 1;
iMegaC{inewclust} = run_list;
end
end
%% now merge high FP rate units
picked = zeros(Nfilt,1);
picked(unique(cell2mat(iMegaC))) = 1;
picked(fpRate < .2) = 1; % eliminate low fp clusters
valid = find(~picked);
[~, order] = sort(nspikes(valid), 'descend');
clustList = valid(order);
for iseed = 1:numel(clustList)
fprintf('New Cluster Total: %d\n', inewclust)
this = clustList(iseed);
if picked(this)
continue
end
run_list = this;
do_not_consider = [];
all_possible = setdiff(tfi(:, find(sum(tfi==this))), this);
all_possible = union(ppairs{this}, all_possible(:));
all_possible = intersect(all_possible, valid);
all_possible = setdiff(all_possible, unique(cell2mat(iMegaC)));
pair_list = all_possible(abs(depths(all_possible)-depths(this)) < 1);
consider = pair_list;
% strun = find(clusterIDs==this);
% npairs to consider merging
nc = numel(consider);
%% first to consider
for iconsider = 1:nc
strun = find(ismember(clusterIDs, run_list));
ipair = consider(iconsider);
imm = ismember(tfi(:, ipair), run_list);
rho = corr(temps(:, [run_list ipair]));
rho = mean(rho(~triu(rho)));
if rho > .5
fprintf('Merging %d\n', ipair)
strun = cat(1, strun, new_spikes);
run_list(end+1) = ipair;
picked(ipair) = 1;
pair_list = unique(cat(1, pair_list, ppairs{ipair}));
pair_list(ismember(pair_list, run_list)) = [];
continue
end
if ~any(imm)
do_not_consider = [do_not_consider; ipair];
continue
end
new_spikes = find(clusterIDs==ipair);
f1new = max(tf(new_spikes, imm), [], 2);
f2new = fmax(new_spikes);
f1old = fmax(strun);
f2old = NaN * ones(numel(f1old), 1, 'single');
if ~ismember(tfi(:, run_list), ipair)
feats = intersect(tfi(:,ipair), tfi(:,run_list));
else
feats = ipair;
end
i0 = 0;
for j = 1:length(run_list)
ifeat = find(ismember(tfi(:, run_list(j)), setdiff(feats, run_list(j))),1);
if ~isempty(ifeat)
ix = clusterIDs == run_list(j);
f2old(i0 + (1:nspikes(run_list(j)))) = tf(ix, ifeat);
i0 = i0 + nspikes(run_list(j));
end
end
f2old = f2old(1:numel(f1old));
f1old(isnan(f2old))=[];
f2old(isnan(f2old))=[];
figure(2); clf
plot(f1old, f2old, '.')
hold on
plot(f1new, f2new, '.')
drawnow
if isempty(f1old)
do_not_consider = [do_not_consider; ipair];
continue
end
mo = merging_score(f1old - f2old, f1new-f2new, ops.fracse);
if mo > 3
do_not_consider = [do_not_consider; ipair];
continue
end
% fprintf('Checking xcorr\n')
% % for xcorr analysis
% binSize = 30;
% nlags = 100;
% s1 = rez.st3(strun,1);
% s1 = ceil(s1/binSize);
%
%
% s2 = rez.st3(new_spikes,1);
% s2 = ceil(s2/binSize);
%
% num = max(max(s1), max(s2));
%
% sp1 = full(sparse(s1, ones(numel(s1),1), ones(numel(s1),1), num, 1));
% sp2 = full(sparse(s2, ones(numel(s2),1), ones(numel(s2),1), num, 1));
%
% xc3 = xcorr(sp1, sp2, nlags, 'unbiased');
% if sum(sp1) > sum(sp2)
% xc1 = xcorr(sp1, nlags, 'unbiased');
% else
% xc1 = xcorr(sp2, nlags, 'unbiased');
% end
%
% xc1(nlags+1) = 0;
% xc1 = smooth(xc1, 5);
% xc3 = smooth(xc3, 5);
%
% nfun = @(x) x/norm(x);
% xc1 = nfun(xc1);
% xc3 = nfun(xc3);
%
% figure(1); clf
% lags = -nlags:nlags;
% plot(lags,xc1, lags, xc3)
% xproj = corr([xc1 xc3]);
% title(xproj(2))
% drawnow
%
%
% if xproj(2) > .7
fprintf('Merging %d\n', ipair)
strun = cat(1, strun, new_spikes);
run_list(end+1) = ipair;
picked(ipair) = 1;
pair_list = unique(cat(1, pair_list, ppairs{ipair}));
pair_list(ismember(pair_list, run_list)) = [];
% end
end
if numel(run_list) > 1
inewclust = inewclust + 1;
iMegaC{inewclust} = run_list;
end
end
%%
% grouped = unique(cell2mat(iMegaC));
% notgrouped = setdiff(1:Nfilt, grouped);
%
% nMC = numel(iMegaC);
% notgroupedid = (1:numel(notgrouped)) + nMC;
fprintf('Removing Collisions from merge\n')
noiseCluster = iMegaC{end}(end);
refDur = 30;
iMega = zeros(Nfilt, 1);
for i = 1:length(iMegaC)
fprintf('%d/%d\n', i, length(iMegaC))
iMega(iMegaC{i}) = iMegaC{i}(1);
pairs = nchoosek(iMegaC{i},2);
if all(fpRate(iMegaC{i}) < fpThresh)
removeIx = [];
for j = 1:size(pairs,1)
% check for collisions upon merge
% only take one spike when both spiked at the same time
ix1 = find(rez.st3(:,2)==pairs(j,1));
ix2 = find(rez.st3(:,2)==pairs(j,2));
t1 = rez.st3(ix1,1);
t2 = rez.st3(ix2,1);
n1 = numel(t1);
n2 = numel(t2);
removeix = [];
if n1 < n2
for jt = 1:n1
removeix = [removeix; ix2(abs(t1(jt) - t2) < refDur)];
% t2(abs(t1(jt) - t2) < refDur) = inf;
end
else
for jt = 1:n2
removeix = [removeix; ix1(abs(t2(jt) - t1) < refDur)];
end
end
end
% asign collisions to noise Cluster
rez.st(removeix,2) = noiseCluster;
end
end
rez.iMega = iMega;
rez.iMegaC = iMegaC;
rez.st3(:,5) = iMega(rez.st3(:,2));