def search_knn(self, point, k, dist=None): """ Return the k nearest neighbors of point and their distances point must be an actual point, not a node. k is the number of results to return. The actual results can be less (if there aren't more nodes to return) or more in case of equal distances. dist is a distance function, expecting two points and returning a distance value. Distance values can be any compareable type. The result is an ordered list of (node, distance) tuples. """ if dist is None: get_dist = lambda n: n.dist(point) else: get_dist = lambda n: dist(n.data, point) results = BoundedPriorityQueue(k) self._search_node(point, k, results, get_dist) # We sort the final result by the distance in the tuple # (<KdNode>, distance) BY_VALUE = lambda kv: kv[1] return sorted(results.items(), key=BY_VALUE)
def search_knn(self, point, k, dist=None): """ k is the number of results to return. The actual results can be less (if there aren't more nodes to return) or more in case of equal distances. dist is a distance function, expecting two points and returning a distance value. The result is an ordered list of (node, distance) tuples. """ if dist is None: get_dist = lambda n: n.dist(point) else: get_dist = lambda n: dist(n.data, point) results = BoundedPriorityQueue(k) self._search_node(point, k, results, get_dist) BY_VALUE = lambda kv: kv[1] return sorted(results.items(), key=BY_VALUE)
def get_test_bpq(self): bound = 5 bpq = BoundedPriorityQueue(bound) for n in self.get_test_nodes(): bpq.add(n) return bpq