-
Notifications
You must be signed in to change notification settings - Fork 4
/
spike_reassignment.py
404 lines (359 loc) · 14.5 KB
/
spike_reassignment.py
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
# %%
"""
outlier detection & soft assignment
input:
- residual bin file path
- template path
- spike train path
- geometry path
- output path
- tPCA components and mean
- number of channels: 7 by default
- number of similar units: 3 by default
output:
- reassignment.npy: N dimensional numpy array (N: number of spikes)
-1 for outlier
- soft_assignment_scores: n_sim_units x N dimensional numpy array
max norm of tpca'd spikes; scores used to reassign spikes
"""
# %%
import numpy as np
import scipy.spatial.distance as dist
from sklearn.decomposition import PCA
import torch
from tqdm.auto import tqdm
from spike_psvae import deconvolve
from spike_psvae.spikeio import read_data, read_waveforms
import os
from spike_psvae.cluster_utils import (
compute_shifted_similarity,
get_closest_clusters_hdbscan,
)
from collections import defaultdict
from spike_psvae.localization import localize_ptp
# from spike_psvae.waveform_utils import (
# get_local_geom,
# relativize_waveforms,
# channel_index_subset,
# )
from spike_psvae.pre_deconv_merge_split import (
get_proposed_pairs,
get_x_z_templates,
)
import matplotlib.pyplot as plt
import h5py
from spike_psvae import waveform_utils
# %%
def run_with_cleaned_wfs(
deconv_h5_path,
template_path,
spike_train_path,
geom,
tpca,
channel_index_h5,
n_chans=8,
n_sim_units=2,
num_sigma_outlier=4,
batch_size=2048,
output_path=None,
soft_assignment_scores=None,
):
# save file
if output_path is not None:
reassignment_file_path = os.path.join(output_path, "reassignment.npy")
reassigned_scores_path = os.path.join(
output_path, "reassignment_scores.npy"
)
soft_assignment_scores_path = os.path.join(
output_path, "soft_assignment_scores.npy"
)
# load templates
templates = np.load(template_path)
ptps = templates.ptp(1)
mcs = ptps.argmax(1)
# load spike train
spike_train = np.load(spike_train_path)
# use gpu if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# channels to be used to compute soft assignment scores
extract_channel_index = []
for c in range(geom.shape[0]):
low = max(0, c - n_chans // 2)
low = min(geom.shape[0] - n_chans, low)
extract_channel_index.append(np.arange(low, low + n_chans))
extract_channel_index = np.array(extract_channel_index)
# number of channels used for detect/subtract (hardcoded to 40 for now)
extract_channel_index_40 = []
for c in range(geom.shape[0]):
low = max(0, c - 40 // 2)
low = min(geom.shape[0] - 40, low)
extract_channel_index_40.append(np.arange(low, low + 40))
extract_channel_index_40 = np.array(extract_channel_index_40)
# get similar templates
similar_array, _ = get_similar_templates(
templates, extract_channel_index_40, n_sim_units, n_chans, geom
)
similar_array = np.hstack(
(np.arange(similar_array.shape[0])[:, None], similar_array)
) # add self as a similar template
# initialize spike reassignment array to -2
spike_reassignment = np.ones(len(spike_train)) * -2
if soft_assignment_scores is None:
# initialize soft_assignment_scores to zero
soft_assignment_scores = np.zeros((n_sim_units + 1, len(spike_train)))
for unit in tqdm(range(templates.shape[0])):
similar_units = similar_array[unit]
spike_idx = np.where(spike_train[:, 1] == unit)
n_spikes = spike_idx[0].shape[0]
if n_spikes == 0:
continue
for batch in range(0, n_spikes, batch_size):
spike_idx_batch = spike_idx[0][batch : batch + batch_size]
with h5py.File(deconv_h5_path, "r+") as h5:
cleaned_waveforms = h5["cleaned_waveforms"][spike_idx][batch : batch + batch_size]
# first_channels = h5["first_channels"][spike_idx][batch : batch + batch_size]
for i, su in enumerate(similar_units):
batch_spike_train = spike_train[spike_idx_batch]
batch_t, batch_template_idx = (
batch_spike_train[:, 0],
batch_spike_train[:, 1],
)
batch_mcs = mcs[batch_template_idx.astype('int')]
batch_extract_channel_index = channel_index_h5[
batch_mcs
]
mc = mcs[unit]
extract_channels = extract_channel_index[mc]
chan_bool = np.isin(
channel_index_h5[mc], extract_channels
)
# N, T, 20
batch_cleaned_waveforms = waveform_utils.channel_subset_by_index(
cleaned_waveforms, batch_mcs, channel_index_h5, extract_channel_index)
temp_small = templates[su][:, extract_channel_index[batch_mcs]].transpose((1, 0, 2))
residual_batch_tpca_max = batch_cleaned_waveforms - temp_small
N, T, C = residual_batch_tpca_max.shape
# max norm of tPCA'd residuals
wf = residual_batch_tpca_max.transpose(0, 2, 1).reshape(
-1, T
)
transformed_wf = tpca.inverse_transform(
tpca.fit_transform(wf)
)
transformed_wf = transformed_wf.reshape(N, C, T).transpose(
0, 2, 1
)
scores = np.abs(transformed_wf).max(axis=(1, 2))
soft_assignment_scores[i, spike_idx_batch] = scores
if output_path is not None:
np.save(
soft_assignment_scores_path, soft_assignment_scores
) # soft assignment scores
# reassign spikes to closest templates
reassigned_scores = np.zeros(len(spike_train))
assignments = soft_assignment_scores.argmin(0)
for unit in tqdm(range(templates.shape[0])):
similar_units = similar_array[unit]
idx = np.isin(spike_train[:, 1], np.ones(1) * unit)
for i, su in enumerate(similar_units):
idxx = np.where(np.logical_and(idx, assignments == i))[0]
spike_reassignment[idxx] = su
reassigned_scores[idxx] = soft_assignment_scores[i, idxx]
# outlier triaging on the reassigned spikes
for unit in tqdm(range(templates.shape[0])):
# set outlier thresholds
scores = reassigned_scores[
np.isin(spike_reassignment, np.ones(1) * unit)
]
median = np.median(scores)
mad = np.median(np.abs(scores - median))
sigma = mad / 0.6745
mu = np.median(scores) # why use median here
unit_cut_off = mu + num_sigma_outlier * sigma
outlier_idx = np.logical_and(
np.isin(spike_reassignment, np.ones(1) * unit),
reassigned_scores > unit_cut_off,
)
spike_reassignment[outlier_idx] = -1
if output_path is not None:
np.save(
reassigned_scores_path, reassigned_scores
) # soft assignment scores
np.save(
reassignment_file_path, spike_reassignment.astype(int)
) # reassignment based on soft assignment scores
return soft_assignment_scores, spike_reassignment, reassigned_scores
# %%
def run(
residual_bin_path,
template_path,
spike_train_path,
geom,
tpca,
n_chans=8,
n_sim_units=2,
num_sigma_outlier=4,
batch_size=2048,
output_path=None,
soft_assignment_scores=None,
):
# save file
if output_path is not None:
reassignment_file_path = os.path.join(output_path, "reassignment.npy")
reassigned_scores_path = os.path.join(
output_path, "reassignment_scores.npy"
)
soft_assignment_scores_path = os.path.join(
output_path, "soft_assignment_scores.npy"
)
# load templates
templates = np.load(template_path)
ptps = templates.ptp(1)
mcs = ptps.argmax()
# load spike train
spike_train = np.load(spike_train_path)
# use gpu if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# channels to be used to compute soft assignment scores
extract_channel_index = []
for c in range(geom.shape[0]):
low = max(0, c - n_chans // 2)
low = min(geom.shape[0] - n_chans, low)
extract_channel_index.append(np.arange(low, low + n_chans))
extract_channel_index = np.array(extract_channel_index)
# number of channels used for detect/subtract (hardcoded to 40 for now)
extract_channel_index_40 = []
for c in range(geom.shape[0]):
low = max(0, c - 40 // 2)
low = min(geom.shape[0] - 40, low)
extract_channel_index_40.append(np.arange(low, low + 40))
extract_channel_index_40 = np.array(extract_channel_index_40)
# get similar templates
similar_array, _ = get_similar_templates(
templates, extract_channel_index_40, n_sim_units, n_chans, geom
)
similar_array = np.hstack(
(np.arange(similar_array.shape[0])[:, None], similar_array)
) # add self as a similar template
# initialize spike reassignment array to -2
spike_reassignment = np.ones(len(spike_train)) * -2
if soft_assignment_scores is None:
# initialize soft_assignment_scores to zero
soft_assignment_scores = np.zeros((n_sim_units + 1, len(spike_train)))
for unit in tqdm(range(templates.shape[0])):
similar_units = similar_array[unit]
spike_idx = np.where(spike_train[:, 1] == unit)
n_spikes = spike_idx[0].shape[0]
if n_spikes == 0:
continue
for batch in range(0, n_spikes, batch_size):
spike_idx_batch = spike_idx[0][batch : batch + batch_size]
for i, su in enumerate(similar_units):
batch_spike_train = spike_train[spike_idx_batch]
batch_t, batch_template_idx = (
batch_spike_train[:, 0],
batch_spike_train[:, 1],
)
batch_mcs = mcs[batch_template_idx]
batch_extract_channel_index = extract_channel_index_40[
batch_mcs
]
mc = mcs[unit]
extract_channels = extract_channel_index[mc]
chan_bool = np.isin(
extract_channel_index_40[mc], extract_channels
)
# load residual batch
residual_batch, skipped_idx = read_waveforms(
batch_t, residual_bin_path, n_channels=geom.shape[0]
)
if su != unit:
# add difference in template
diff = templates[unit] - templates[su]
residual_batch += diff[None]
residual_batch_tpca_max = np.array(
list(
map(
lambda x, idx: x[:, idx][:, chan_bool],
residual_batch,
batch_extract_channel_index,
)
)
)
N, T, C = residual_batch_tpca_max.shape
# max norm of tPCA'd residuals
wf = residual_batch_tpca_max.transpose(0, 2, 1).reshape(
-1, T
)
transformed_wf = tpca.inverse_transform(
tpca.fit_transform(wf)
)
transformed_wf = transformed_wf.reshape(N, C, T).transpose(
0, 2, 1
)
scores = np.abs(transformed_wf).max(axis=(1, 2))
soft_assignment_scores[i, spike_idx_batch] = scores
if output_path is not None:
np.save(
soft_assignment_scores_path, soft_assignment_scores
) # soft assignment scores
# reassign spikes to closest templates
reassigned_scores = np.zeros(len(spike_train))
assignments = soft_assignment_scores.argmin(0)
for unit in tqdm(range(templates.shape[0])):
similar_units = similar_array[unit]
idx = np.isin(spike_train[:, 1], np.ones(1) * unit)
for i, su in enumerate(similar_units):
idxx = np.where(np.logical_and(idx, assignments == i))[0]
spike_reassignment[idxx] = su
reassigned_scores[idxx] = soft_assignment_scores[i, idxx]
# outlier triaging on the reassigned spikes
for unit in tqdm(range(templates.shape[0])):
# set outlier thresholds
scores = reassigned_scores[
np.isin(spike_reassignment, np.ones(1) * unit)
]
median = np.median(scores)
mad = np.median(np.abs(scores - median))
sigma = mad / 0.6745
mu = np.median(scores) # why use median here
unit_cut_off = mu + num_sigma_outlier * sigma
outlier_idx = np.logical_and(
np.isin(spike_reassignment, np.ones(1) * unit),
reassigned_scores > unit_cut_off,
)
spike_reassignment[outlier_idx] = -1
if output_path is not None:
np.save(
reassigned_scores_path, reassigned_scores
) # soft assignment scores
np.save(
reassignment_file_path, spike_reassignment.astype(int)
) # reassignment based on soft assignment scores
return soft_assignment_scores, spike_reassignment, reassigned_scores
# %%
def get_similar_templates(
templates, extract_channel_index_40, n_sim_units, n_chans, geom
):
n_units = templates.shape[0]
ptps = templates.ptp(1)
mcs = ptps.argmax(1)
# localize templates
x_z_templates = np.zeros((n_units, 2))
for i, template in enumerate(templates):
mc = mcs[i]
channels = extract_channel_index_40[mc]
template_x, _, template_z_rel, template_z_abs, _, _ = localize_ptp(
template.ptp(0)[channels], channels[0], mc, geom
)
x_z_templates[i, 0] = template_x
x_z_templates[i, 1] = template_z_abs
dist_argsort, dist_template = get_proposed_pairs(
templates.shape[0],
templates,
x_z_templates,
n_temp=n_sim_units,
n_channels=n_chans,
shifts=[-2, -1, 0, 1, 2], # predefined shifts
)
return dist_argsort, dist_template