Skip to content

Commit

Permalink
Merge pull request #26 from denbeigh2000/master
Browse files Browse the repository at this point in the history
Optimisations for knn search
  • Loading branch information
stefankoegl committed Apr 20, 2015
2 parents 51fdafa + f349c71 commit 7a3b3b2
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions kdtree.py
Expand Up @@ -470,6 +470,8 @@ def _search_node(self, point, k, results, examined, get_dist):
else:
bestNode, bestDist = sorted(results.items(), key=lambda n_d: n_d[1], reverse=True)[0]

nodesChanged = False

# If the current node is closer than the current best, then it
# becomes the current best.
nodeDist = get_dist(self)
Expand All @@ -478,18 +480,23 @@ def _search_node(self, point, k, results, examined, get_dist):
results.pop(bestNode)

results[self] = nodeDist
nodesChanged = True

# if we're equal to the current best, add it, regardless of k
elif nodeDist == bestDist:
results[self] = nodeDist
nodesChanged = True

# if we don't have k results yet, add it anyway
elif len(results) < k:
results[self] = nodeDist
nodesChanged = True

# get new best
bestNode = next(iter(sorted(results, key=get_dist, reverse=True)))
bestDist = get_dist(bestNode)
# get new best only if nodes have changed
if nodesChanged:
bestNode, bestDist = next(iter(
sorted(results.items(), key=lambda n: n[1], reverse=True)
))

# Check whether there could be any points on the other side of the
# splitting plane that are closer to the search point than the current
Expand Down

0 comments on commit 7a3b3b2

Please sign in to comment.