-
Notifications
You must be signed in to change notification settings - Fork 4
/
triage.py
277 lines (244 loc) · 7.16 KB
/
triage.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
import numpy as np
from sklearn.neighbors import KernelDensity
import networkx as nx
from scipy.spatial import KDTree
import scipy
def weighted_knn_triage(
xyzap, feats, scales=[1, 10, 1, 15, 30, 10, 10, 10], percentile=85
):
vecs = np.c_[xyzap, feats] @ np.diag(scales)
log_ptp = xyzap[:, 4]
tree = KDTree(vecs)
dist, ind = tree.query(vecs, k=6)
dist = np.sum(dist / log_ptp[:, None], 1)
idx_keep1 = dist <= np.percentile(dist, percentile)
return idx_keep1
def run_weighted_triage_low_ptp(
x,
z,
maxptps,
scales=(1, 1, 30),
threshold=80,
ptp_low_threshold=3,
ptp_high_threshold=6,
c=1,
):
# only triage high ptp spikes
high_ptp_filter = maxptps < ptp_high_threshold
high_ptp_filter = np.flatnonzero(high_ptp_filter)
x = x[high_ptp_filter]
z = z[high_ptp_filter]
maxptps = maxptps[high_ptp_filter]
# filter by low ptp
low_ptp_filter = maxptps > ptp_low_threshold
low_ptp_filter = np.flatnonzero(low_ptp_filter)
x = x[low_ptp_filter]
z = z[low_ptp_filter]
maxptps = maxptps[low_ptp_filter]
feats = np.c_[scales[0] * x, scales[1] * z, scales[2] * np.log(maxptps)]
tree = KDTree(feats)
dist, ind = tree.query(feats, k=6)
dist = dist[:, 1:]
dist = np.sum(
c * np.log(dist) + np.log(1 / (scales[2] * np.log(maxptps)))[:, None],
1,
)
idx_keep = dist <= np.percentile(dist, threshold)
triaged_x = x[idx_keep]
triaged_z = z[idx_keep]
triaged_maxptps = maxptps[idx_keep]
return idx_keep, high_ptp_filter, low_ptp_filter
def run_weighted_triage_adaptive(
x,
z,
maxptps,
scales=(1, 1, 50),
log_c=5,
threshold=80,
ptp_low_threshold=3,
bin_size=5,
region_size=25,
):
# filter by low ptp
low_ptp_filter = maxptps > ptp_low_threshold
low_ptp_filter = np.flatnonzero(low_ptp_filter)
x = x[low_ptp_filter]
z = z[low_ptp_filter]
maxptps = maxptps[low_ptp_filter]
# get local distances
feats = np.c_[
scales[0] * x, scales[1] * z, scales[2] * np.log(log_c + maxptps)
]
tree = KDTree(feats)
dist, ind = tree.query(feats, k=6)
dist = dist[:, 1:]
dist = np.mean(dist, 1)
spike_region_dist = 1
if region_size is not None:
# bin spikes
min_z, max_z = np.min(z), np.max(z)
bins = np.arange(min_z, max_z, bin_size)
binned_z = np.digitize(z, bins, right=False)
# get mean distance in bin
binned_dist = []
spikes_in_bin_list = []
for bin_id in range(len(bins)):
spikes_in_bin = np.where(binned_z == bin_id)[0]
spikes_in_bin_list.append(len(spikes_in_bin))
if len(spikes_in_bin) == 0:
binned_mean_dist = np.max(dist)
else:
binned_mean_dist = np.mean(dist[np.where(binned_z == bin_id)])
binned_dist.append(binned_mean_dist)
# gaussian filter sigma=region_size/bin_size
region_dist = scipy.ndimage.gaussian_filter(
binned_dist, sigma=region_size / bin_size, mode="constant"
)
spike_region_dist_func = scipy.interpolate.interp1d(
bins, region_dist, fill_value="extrapolate"
)
spike_region_dist = spike_region_dist_func(z)
true_dist = (
np.log(dist)
- np.log(spike_region_dist)
+ np.log(1 / (scales[2] * np.log(maxptps)))
)
idx_keep = true_dist <= np.percentile(true_dist, threshold)
triaged_x = x[idx_keep]
triaged_z = z[idx_keep]
triaged_maxptps = maxptps[idx_keep]
return triaged_x, triaged_z, triaged_maxptps, idx_keep, low_ptp_filter
def run_weighted_triage(
x,
y,
z,
alpha,
maxptps,
pcs=None,
scales=(1, 10, 1, 15, 30, 10),
threshold=80,
ptp_threshold=3,
c=1,
mask=None,
):
ptp_filter = maxptps > ptp_threshold
ptp_filter = np.flatnonzero(ptp_filter)
x = x[ptp_filter]
y = y[ptp_filter]
z = z[ptp_filter]
alpha = alpha[ptp_filter]
maxptps = maxptps[ptp_filter]
if pcs is not None:
pcs = pcs[ptp_filter]
feats = np.c_[
scales[0] * x,
scales[1] * np.log(y),
scales[2] * z,
scales[3] * np.log(alpha),
scales[4] * np.log(maxptps),
scales[5] * pcs[:, :3],
]
else:
feats = np.c_[
scales[0] * x,
# scales[1]*np.log(y),
scales[2] * z,
# scales[3]*np.log(alpha),
scales[4] * np.log(maxptps),
]
tree = KDTree(feats)
dist, ind = tree.query(feats, k=6)
dist = dist[:, 1:]
dist = np.sum(
c * np.log(dist) + np.log(1 / (scales[4] * np.log(maxptps)))[:, None],
1,
)
idx_keep = dist <= np.percentile(dist, threshold)
triaged_x = x[idx_keep]
triaged_y = y[idx_keep]
triaged_z = z[idx_keep]
triaged_alpha = alpha[idx_keep]
triaged_maxptps = maxptps[idx_keep]
triaged_pcs = None
if pcs is not None:
triaged_pcs = pcs[idx_keep]
return (
triaged_x,
triaged_y,
triaged_z,
triaged_alpha,
triaged_maxptps,
triaged_pcs,
ptp_filter,
idx_keep,
)
def weighted_triage_ix(
x,
y,
z,
alpha,
maxptps,
pcs=None,
scales=(1, 10, 1, 15, 30, 10),
threshold=100,
ptp_threshold=3,
c=1,
):
ptp_filter = maxptps > ptp_threshold
x = x[ptp_filter]
y = y[ptp_filter]
z = z[ptp_filter]
alpha = alpha[ptp_filter]
maxptps = maxptps[ptp_filter]
if pcs is not None:
# pcs = pcs[ptp_filter]
feats = np.c_[
scales[0] * x,
scales[1] * np.log(y),
scales[2] * z,
scales[3] * np.log(alpha),
scales[4] * np.log(maxptps),
scales[5] * pcs[:, :3],
]
else:
feats = np.c_[
scales[0] * x,
# scales[1]*np.log(y),
scales[2] * z,
# scales[3]*np.log(alpha),
scales[4] * np.log(maxptps),
]
tree = KDTree(feats)
dist, ind = tree.query(feats, k=6)
print(dist.shape)
dist = dist[:, 1:]
print(np.isfinite(np.log(dist)).all(axis=1).mean())
dist = np.sum(
c * np.log(dist) + np.log(1 / (scales[4] * np.log(maxptps)))[:, None],
1,
)
idx_keep = dist <= np.percentile(dist, threshold)
idx_keep_full = ptp_filter
idx_keep_full[np.flatnonzero(idx_keep_full)[~idx_keep]] = 0
return idx_keep_full
def coarse_split(
xyzp,
feats,
scales=[1, 10, 1, 15, 30, 10, 10, 10],
):
vecs = np.c_[xyzp, feats] @ np.diag(scales)
tree = KDTree(vecs)
dist, ind = tree.query(vecs, k=6)
dist = np.mean(dist, 1)
print(dist.mean() + 6 * dist.std(), dist.mean(), dist.std())
W = tree.sparse_distance_matrix(
tree, max_distance=dist.mean() + 6 * dist.std()
)
G = nx.convert_matrix.from_scipy_sparse_matrix(W)
components = list(nx.algorithms.components.connected_components(G))
print(len(components), flush=True)
labels = np.zeros(xyzp.shape[0], dtype=np.int)
for k in range(len(components)):
mask = np.isin(np.arange(xyzp.shape[0]), list(components[k]))
labels[mask] = k
return labels