-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_hdbscan_linkage.pyx
405 lines (337 loc) · 13.8 KB
/
_hdbscan_linkage.pyx
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
# cython: boundscheck=False
# cython: nonecheck=False
# Minimum spanning tree single linkage implementation for hdbscan
# Authors: Leland McInnes, Steve Astels
# License: 3-clause BSD
import numpy as np
cimport numpy as np
import itertools
from libc.float cimport DBL_MAX, DBL_MIN
from dist_metrics cimport DistanceMetric
from _d_heap_queue cimport HeapQueue
cpdef np.ndarray[np.double_t, ndim=2] boruvka(
np.ndarray[np.double_t, ndim=2] distance_matrix):
cdef int n, n_g, i_g, i_g1, i_g2, idx, i, j, u, v, w
cdef np.ndarray[np.double_t, ndim=2] local_matrix, sub_matrix, distance_sub_matrix, mst, contracted_mst
cdef np.ndarray[np.intp_t, ndim=1] cheapest_destination
cdef list F, e, V_g, V_g1, V_g2
cdef dict G, vertex_to_subgraph, subgraph, contracted_edges
# Number of vertices
n = distance_matrix.shape[0]
local_matrix = distance_matrix.copy()
local_matrix[np.eye(n).astype(bool)] = np.inf
# Indexes of the cheapest destination nodes
cheapest_destination = np.argmin(local_matrix, axis=1)
F = []
for u in range(cheapest_destination.shape[0]):
v = cheapest_destination[u]
if u > v:
u, v = v, u
if not [u, v] in F and not [v, u] in F:
F.append([u, v])
# Subgraphs
G = {}
vertex_to_subgraph = {}
# Add first edge
e = F.pop(0)
u, v = e
G[0] = {'V': [u, v], 'E': [e]}
vertex_to_subgraph[u] = 0
vertex_to_subgraph[v] = 0
while len(F) > 0:
u, v = F.pop(0)
if u in vertex_to_subgraph and not v in vertex_to_subgraph:
vertex_to_subgraph[v] = vertex_to_subgraph[u]
G[vertex_to_subgraph[v]]['V'].append(v)
G[vertex_to_subgraph[v]]['E'].append([u, v])
elif not u in vertex_to_subgraph and v in vertex_to_subgraph:
vertex_to_subgraph[u] = vertex_to_subgraph[v]
G[vertex_to_subgraph[u]]['V'].append(u)
G[vertex_to_subgraph[u]]['E'].append([v, u])
elif u in vertex_to_subgraph and v in vertex_to_subgraph:
# Need merging
if len(G[vertex_to_subgraph[u]]['V']) < len(G[vertex_to_subgraph[v]]['V']):
u, v = v, u
subgraph = G[vertex_to_subgraph[v]]
G[vertex_to_subgraph[u]]['V'] += subgraph['V']
G[vertex_to_subgraph[u]]['E'] += [[u, v]] + subgraph['E']
del G[vertex_to_subgraph[v]]
for w in subgraph['V']:
vertex_to_subgraph[w] = vertex_to_subgraph[u]
else:
# New graph
i = max(G.keys()) + 1
G[i] = {'V': [u, v], 'E': [[u, v]]}
vertex_to_subgraph[u] = i
vertex_to_subgraph[v] = i
# Rename graphs
subgraphs = G.values()
G = dict(zip(list(range(len(subgraphs))), subgraphs))
# Add edges to the mst
E = []
for i_g in G:
for e in G[i_g]['E']:
u, v = e
E.append([u, v, distance_matrix[u, v]])
mst = np.array(E)
# If just one graph, then it is the MST
n_g = len(G)
if n_g == 1:
return mst
contracted_edges = {}
distance_sub_matrix = np.infty * np.ones((n_g, n_g))
for i_g1, i_g2 in itertools.combinations(list(G.keys()), 2):
V_g1 = G[i_g1]['V']
V_g2 = G[i_g2]['V']
sub_matrix = local_matrix[np.ix_(V_g1, V_g2)]
idx = np.argmin(sub_matrix.ravel())
i, j = np.unravel_index(idx, (sub_matrix.shape[0], sub_matrix.shape[1]))
contracted_edges[(i_g1, i_g2)] = [V_g1[i], V_g2[j]]
distance_sub_matrix[i_g1, i_g2] = sub_matrix[i, j]
distance_sub_matrix[i_g2, i_g1] = sub_matrix[i, j]
# If just two graphs, it just link them and return
if n_g == 2:
mst = np.vstack((mst, contracted_edges[(0, 1)] + [distance_sub_matrix[0, 1]]))
return mst
# Call recursively the Boruvka algorithm for the contracted graph
contracted_mst = boruvka(distance_sub_matrix)
# Recover the original vertice ids
for i in range(contracted_mst.shape[0]):
u, v = contracted_mst[i, :2].astype(int)
if u > v:
u, v = v, u
contracted_mst[i, :2] = contracted_edges[(u, v)]
# Join forests by including the edges of the contracted MST
mst = np.concatenate((mst, contracted_mst), axis=0)
return mst
cpdef np.ndarray[np.double_t, ndim=2] prim_with_heap(
np.ndarray[np.double_t, ndim=2] distance_matrix):
cdef int n
cdef int v
cdef list vertices
cdef list adjacency_lists
cdef list adjacency_list
cdef np.ndarray[np.double_t, ndim=1] keys
cdef np.ndarray[np.intp_t, ndim=1] in_heap
cdef HeapQueue min_heap
cdef list null_edge
cdef list extracted_vertex
cdef int vertex_id
cdef int next_vertex_id
cdef float next_key
cdef np.ndarray[np.double_t, ndim=2] mst, mst_arr
# Number of vertices
n = distance_matrix.shape[0]
vertices = list(range(n))
# Set adjacency list (it is a fully connected graph)
adjacency_lists = [list(set(range(n))-set([v])) for v in vertices]
# Initialize min-heap
min_heap = HeapQueue(
element_list=[[0.0, 0]] + [[DBL_MAX, v] for v in vertices[1:]],
number_of_children=2)
keys = np.array([0.0] + (n - 1) * [DBL_MAX])
in_heap = np.array(n * [1])
# Initialize mst
mst = np.infty * np.ones((n, 3))
while min_heap.size() > 0:
extracted_vertex = min_heap.pop()
vertex_id = extracted_vertex[1]
in_heap[vertex_id] = 0
# Get adjacency list
adjacency_list = adjacency_lists[vertex_id]
for next_vertex_id in adjacency_list:
if in_heap[next_vertex_id] > 0:
next_key = distance_matrix[vertex_id, next_vertex_id]
if (keys[next_vertex_id] > next_key):
min_heap.set_key(next_vertex_id, next_key)
mst[next_vertex_id, 0] = vertex_id
mst[next_vertex_id, 1] = next_vertex_id
mst[next_vertex_id, 2] = next_key
keys[next_vertex_id] = next_key
mst_arr = mst[np.isfinite(mst[:, 0]), :]
return mst_arr
cpdef np.ndarray[np.double_t, ndim=2] mst_linkage_core(
np.ndarray[np.double_t,
ndim=2] distance_matrix):
cdef np.ndarray[np.intp_t, ndim=1] node_labels
cdef np.ndarray[np.intp_t, ndim=1] current_labels
cdef np.ndarray[np.double_t, ndim=1] current_distances
cdef np.ndarray[np.double_t, ndim=1] left
cdef np.ndarray[np.double_t, ndim=1] right
cdef np.ndarray[np.double_t, ndim=2] result
cdef np.ndarray label_filter
cdef np.intp_t current_node
cdef np.intp_t new_node_index
cdef np.intp_t new_node
cdef np.intp_t i
result = np.zeros((distance_matrix.shape[0] - 1, 3))
node_labels = np.arange(distance_matrix.shape[0], dtype=np.intp)
current_node = 0
current_distances = np.infty * np.ones(distance_matrix.shape[0])
current_labels = node_labels
for i in range(1, node_labels.shape[0]):
label_filter = current_labels != current_node
current_labels = current_labels[label_filter]
left = current_distances[label_filter]
right = distance_matrix[current_node][current_labels]
current_distances = np.where(left < right, left, right)
new_node_index = np.argmin(current_distances)
new_node = current_labels[new_node_index]
result[i - 1, 0] = <double> current_node
result[i - 1, 1] = <double> new_node
result[i - 1, 2] = current_distances[new_node_index]
current_node = new_node
return result
cpdef np.ndarray[np.double_t, ndim=2] mst_linkage_core_vector(
np.ndarray[np.double_t, ndim=2, mode='c'] raw_data,
np.ndarray[np.double_t, ndim=1, mode='c'] core_distances,
DistanceMetric dist_metric,
np.double_t alpha=1.0):
# Add a comment
cdef np.ndarray[np.double_t, ndim=1] current_distances_arr
cdef np.ndarray[np.double_t, ndim=1] current_sources_arr
cdef np.ndarray[np.int8_t, ndim=1] in_tree_arr
cdef np.ndarray[np.double_t, ndim=2] result_arr
cdef np.double_t * current_distances
cdef np.double_t * current_sources
cdef np.double_t * current_core_distances
cdef np.double_t * raw_data_ptr
cdef np.int8_t * in_tree
cdef np.double_t[:, ::1] raw_data_view
cdef np.double_t[:, ::1] result
cdef np.ndarray label_filter
cdef np.intp_t current_node
cdef np.intp_t source_node
cdef np.intp_t right_node
cdef np.intp_t left_node
cdef np.intp_t new_node
cdef np.intp_t i
cdef np.intp_t j
cdef np.intp_t dim
cdef np.intp_t num_features
cdef double current_node_core_distance
cdef double right_value
cdef double left_value
cdef double core_value
cdef double new_distance
dim = raw_data.shape[0]
num_features = raw_data.shape[1]
raw_data_view = (<np.double_t[:raw_data.shape[0], :raw_data.shape[1]:1]> (
<np.double_t *> raw_data.data))
raw_data_ptr = (<np.double_t *> &raw_data_view[0, 0])
result_arr = np.zeros((dim - 1, 3))
in_tree_arr = np.zeros(dim, dtype=np.int8)
current_node = 0
current_distances_arr = np.infty * np.ones(dim)
current_sources_arr = np.ones(dim)
result = (<np.double_t[:dim - 1, :3:1]> (<np.double_t *> result_arr.data))
in_tree = (<np.int8_t *> in_tree_arr.data)
current_distances = (<np.double_t *> current_distances_arr.data)
current_sources = (<np.double_t *> current_sources_arr.data)
current_core_distances = (<np.double_t *> core_distances.data)
for i in range(1, dim):
in_tree[current_node] = 1
current_node_core_distance = current_core_distances[current_node]
new_distance = DBL_MAX
source_node = 0
new_node = 0
for j in range(dim):
if in_tree[j]:
continue
right_value = current_distances[j]
right_source = current_sources[j]
left_value = dist_metric.dist(&raw_data_ptr[num_features *
current_node],
&raw_data_ptr[num_features * j],
num_features)
left_source = current_node
if alpha != 1.0:
left_value /= alpha
core_value = core_distances[j]
if (current_node_core_distance > right_value or
core_value > right_value or
left_value > right_value):
if right_value < new_distance:
new_distance = right_value
source_node = right_source
new_node = j
continue
if core_value > current_node_core_distance:
if core_value > left_value:
left_value = core_value
else:
if current_node_core_distance > left_value:
left_value = current_node_core_distance
if left_value < right_value:
current_distances[j] = left_value
current_sources[j] = left_source
if left_value < new_distance:
new_distance = left_value
source_node = left_source
new_node = j
else:
if right_value < new_distance:
new_distance = right_value
source_node = right_source
new_node = j
result[i - 1, 0] = <double> source_node
result[i - 1, 1] = <double> new_node
result[i - 1, 2] = new_distance
current_node = new_node
return result_arr
cdef class UnionFind (object):
cdef np.ndarray parent_arr
cdef np.ndarray size_arr
cdef np.intp_t next_label
cdef np.intp_t *parent
cdef np.intp_t *size
def __init__(self, N):
self.parent_arr = -1 * np.ones(2 * N - 1, dtype=np.intp, order='C')
self.next_label = N
self.size_arr = np.hstack((np.ones(N, dtype=np.intp),
np.zeros(N-1, dtype=np.intp)))
self.parent = (<np.intp_t *> self.parent_arr.data)
self.size = (<np.intp_t *> self.size_arr.data)
cdef void union(self, np.intp_t m, np.intp_t n):
self.size[self.next_label] = self.size[m] + self.size[n]
self.parent[m] = self.next_label
self.parent[n] = self.next_label
self.size[self.next_label] = self.size[m] + self.size[n]
self.next_label += 1
return
cdef np.intp_t fast_find(self, np.intp_t n):
cdef np.intp_t p
p = n
while self.parent_arr[n] != -1:
n = self.parent_arr[n]
# label up to the root
while self.parent_arr[p] != n:
p, self.parent_arr[p] = self.parent_arr[p], n
return n
cpdef np.ndarray[np.double_t, ndim=2] label(np.ndarray[np.double_t, ndim=2] L):
cdef np.ndarray[np.double_t, ndim=2] result_arr
cdef np.double_t[:, ::1] result
cdef np.intp_t N, a, aa, b, bb, index
cdef np.double_t delta
result_arr = np.zeros((L.shape[0], L.shape[1] + 1))
result = (<np.double_t[:L.shape[0], :4:1]> (
<np.double_t *> result_arr.data))
N = L.shape[0] + 1
U = UnionFind(N)
for index in range(L.shape[0]):
a = <np.intp_t> L[index, 0]
b = <np.intp_t> L[index, 1]
delta = L[index, 2]
aa, bb = U.fast_find(a), U.fast_find(b)
result[index][0] = aa
result[index][1] = bb
result[index][2] = delta
result[index][3] = U.size[aa] + U.size[bb]
U.union(aa, bb)
return result_arr
cpdef np.ndarray[np.double_t, ndim=2] single_linkage(distance_matrix):
cdef np.ndarray[np.double_t, ndim=2] hierarchy
cdef np.ndarray[np.double_t, ndim=2] for_labelling
hierarchy = mst_linkage_core(distance_matrix)
for_labelling = hierarchy[np.argsort(hierarchy.T[2]), :]
return label(for_labelling)