-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathvptree.py
222 lines (178 loc) · 6.69 KB
/
vptree.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
""" This module contains an implementation of a Vantage Point-tree (VP-tree)."""
import bisect
import collections
import math
import statistics as stats
class VPTree:
""" VP-Tree data structure for efficient nearest neighbor search.
The VP-tree is a data structure for efficient nearest neighbor
searching and finds the nearest neighbor in O(log n)
complexity given a tree constructed of n data points. Construction
complexity is O(n log n).
Parameters
----------
points : Iterable
Construction points.
dist_fn : Callable
Function taking to point instances as arguments and returning
the distance between them.
leaf_size : int
Minimum number of points in leaves (IGNORED).
"""
def __init__(self, points, dist_fn):
self.left = None
self.right = None
self.left_min = math.inf
self.left_max = 0
self.right_min = math.inf
self.right_max = 0
self.dist_fn = dist_fn
if not len(points):
raise ValueError('Points can not be empty.')
# Vantage point is point furthest from parent vp.
self.vp = points[0]
points = points[1:]
if len(points) == 0:
return
# Choose division boundary at median of distances.
distances = [self.dist_fn(self.vp, p) for p in points]
median = stats.median(distances)
left_points = []
right_points = []
for point, distance in zip(points, distances):
if distance >= median:
self.right_min = min(distance, self.right_min)
if distance > self.right_max:
self.right_max = distance
right_points.insert(0, point) # put furthest first
else:
right_points.append(point)
else:
self.left_min = min(distance, self.left_min)
if distance > self.left_max:
self.left_max = distance
left_points.insert(0, point) # put furthest first
else:
left_points.append(point)
if len(left_points) > 0:
self.left = VPTree(points=left_points, dist_fn=self.dist_fn)
if len(right_points) > 0:
self.right = VPTree(points=right_points, dist_fn=self.dist_fn)
def _is_leaf(self):
return (self.left is None) and (self.right is None)
def get_nearest_neighbor(self, query):
""" Get single nearest neighbor.
Parameters
----------
query : Any
Query point.
Returns
-------
Any
Single nearest neighbor.
"""
return self.get_n_nearest_neighbors(query, n_neighbors=1)[0]
def get_n_nearest_neighbors(self, query, n_neighbors):
""" Get `n_neighbors` nearest neigbors to `query`
Parameters
----------
query : Any
Query point.
n_neighbors : int
Number of neighbors to fetch.
Returns
-------
list
List of `n_neighbors` nearest neighbors.
"""
if not isinstance(n_neighbors, int) or n_neighbors < 1:
raise ValueError('n_neighbors must be strictly positive integer')
neighbors = _AutoSortingList(max_size=n_neighbors)
queue = collections.deque([self])
furthest_d = math.inf
need_neighbors = True
while queue:
node = queue.popleft()
if node is None:
continue
d = self.dist_fn(query, node.vp)
if d < furthest_d or need_neighbors:
neighbors.append((d, node.vp))
furthest_d = neighbors[-1][0]
if need_neighbors:
need_neighbors = len(neighbors) < n_neighbors
if node._is_leaf():
continue
if need_neighbors:
if d < node.left_max + furthest_d:
queue.append(node.left)
if d >= node.right_min - furthest_d:
queue.append(node.right)
else:
if node.left_min - furthest_d < d < node.left_max + furthest_d:
queue.append(node.left)
if node.right_min - furthest_d <= d <= node.right_max + furthest_d:
queue.append(node.right)
return list(neighbors)
def get_all_in_range(self, query, max_distance):
""" Find all neighbours within `max_distance`.
Parameters
----------
query : Any
Query point.
max_distance : float
Threshold distance for query.
Returns
-------
neighbors : list
List of points within `max_distance`.
Notes
-----
Returned neighbors are not sorted according to distance.
"""
neighbors = list()
nodes_to_visit = [(self, 0)]
while len(nodes_to_visit) > 0:
node, d0 = nodes_to_visit.pop(0)
if node is None or d0 > max_distance:
continue
d = self.dist_fn(query, node.vp)
if d < max_distance:
neighbors.append((d, node.vp))
if node._is_leaf():
continue
if node.left_min <= d <= node.left_max:
nodes_to_visit.insert(0, (node.left, 0))
elif node.left_min - max_distance <= d <= node.left_max + max_distance:
nodes_to_visit.append((node.left,
node.left_min - d if d < node.left_min
else d - node.left_max))
if node.right_min <= d <= node.right_max:
nodes_to_visit.insert(0, (node.right, 0))
elif node.right_min - max_distance <= d <= node.right_max + max_distance:
nodes_to_visit.append((node.right,
node.right_min - d if d < node.right_min
else d - node.right_max))
return neighbors
class _AutoSortingList(list):
""" Simple auto-sorting list.
Inefficient for large sizes since the queue is sorted at
each push.
Parameters
---------
size : int, optional
Max queue size.
"""
def __init__(self, max_size=None, *args):
super(_AutoSortingList, self).__init__(*args)
self.max_size = max_size
def append(self, item):
""" insert `item` in sorted order
Parameters
----------
item : Any
Input item.
"""
self.insert(bisect.bisect_left(self, item), item)
if self.max_size is not None and len(self) > self.max_size:
self.pop()