Given Two Lists Of 2d Points, How To Find The Closest Point In The 2nd List For Every Point In The 1st List?
Solution 1:
I think there are several options. I ginned up a small comparison test to explore a few. The first couple of these only go as far as finding how many points are mutually within radius of each other to make sure I was getting consistent results on the main part of the problem. It does not answer the mail on the part of your problem about finding the closest, which I think would be just a bit more work on a few of them--did it for the last option, see bottom of post. The driver of the problem is doing all of the comparisons, and I think you can make some hay by some sorting (last notion here) to limit comparisons.
Naive Python
Use brute force point-to-point comparison. Clearly O(n^2).
Scipy's cdist
module
Works great & fastest for "small" data. With large data, this starts to blow up because of size of matrix output in memory. Probably infeasible for 1M x 1M application.
Scipy's KDTree
module
From other solution. Fast, but not as fast as cdist
or "sectioning" (below). Perhaps there is a different way to employ KDTree for this task... I'm not very experienced with it. This approach (below) seemed logical.
Sectioning the compare-to array
This works very well because you are not interested in all of the distances, you just want ones that are within a radius. So, by sorting the target array and only looking within a rectangular window around it for "contenders" you can get very fast performance w/ native python and no "memory explosion." Probably still a bit "left on the table" here for enhancement maybe by embedding cdist
within this implementation or (gulp) trying to multithread it.
Other ideas...
This is a tight "mathy" loop so trying something in cython or splitting up one of the arrays and multi-threading it would be novel. And pickling the result so you don't have to run this often seems prudent.
I think any of these you could augment the tuples with the index within the array pretty easily to get a list of the matches.
My older iMac does 100K x 100K in 90 seconds via sectioning, so that does not bode well for 1M x 1M
Comparison:
# distance checkerfrom random import uniform
import time
import numpy as np
from scipy.spatial import distance, KDTree
from bisect import bisect
from operator import itemgetter
import sys
from matplotlib import pyplot as plt
sizes = [100, 500, 1000, 2000, 5000, 10000, 20000]
#sizes = [20_000, 30_000, 40_000, 50_000, 60_000] # for the playoffs. :)
naive_times = []
cdist_times = []
kdtree_times = []
sectioned_times = []
delta = 0.1for size in sizes:
print(f'\n *** running test with vectors of size {size} ***')
r = 20# radius to match
r_squared = r**2
A = [(uniform(-1000,1000), uniform(-1000,1000)) for t inrange(size)]
B = [(uniform(-1000,1000), uniform(-1000,1000)) for t inrange(size)]
# naive pythonprint('naive python')
tic = time.time()
matches = [(p1, p2) for p1 in A
for p2 in B
if (p1[0] - p2[0])**2 + (p1[1] - p2[1])**2 <= r_squared]
toc = time.time()
print(f'found: {len(matches)}')
naive_times.append(toc-tic)
print(toc-tic)
print()
# using cdist moduleprint('cdist')
tic = time.time()
dist_matrix = distance.cdist(A, B, 'euclidean')
result = np.count_nonzero(dist_matrix<=r)
toc = time.time()
print(f'found: {result}')
cdist_times.append(toc-tic)
print(toc-tic)
print()
# KDTreeprint('KDTree')
tic = time.time()
my_tree = KDTree(A)
results = my_tree.query_ball_point(B, r=r)
# for count, r in enumerate(results):# for t in r:# print(count, A[t])
result = sum(len(lis) for lis in results)
toc = time.time()
print(f'found: {result}')
kdtree_times.append(toc-tic)
print(toc-tic)
print()
# python with sort and sectioningprint('with sort and sectioning')
result = 0
tic = time.time()
B.sort()
for point in A:
# gather the neighborhood in x-dimension within x-r <= x <= x+r+1# if this has any merit, we could "do it again" for y-coord....
contenders = B[bisect(B,(point[0]-r-delta, 0)) : bisect(B,(point[0]+r+delta, 0))]
# further chop down to the y-neighborhood# flip the coordinate to support bisection by y-value
contenders = list(map(lambda p: (p[1], p[0]), contenders))
contenders.sort()
contenders = contenders[bisect(contenders,(point[1]-r-delta, 0)) :
bisect(contenders,(point[1]+r+delta, 0))]
# note (x, y) in contenders is still inverted, so need to index properly
matches = [(point, p2) for p2 in contenders if (point[0] - p2[1])**2 + (point[1] - p2[0])**2 <= r_squared]
result += len(matches)
toc = time.time()
print(f'found: {result}')
sectioned_times.append(toc-tic)
print(toc-tic)
print('complete.')
plt.plot(sizes, naive_times, label = 'naive')
plt.plot(sizes, cdist_times, label = 'cdist')
plt.plot(sizes, kdtree_times, label = 'kdtree')
plt.plot(sizes, sectioned_times, label = 'sectioning')
plt.legend()
plt.show()
Results for one of the sizes and plots:
*** running test with vectors of size 20000 ***
naive python
found:124425101.40657806396484
cdist
found:1244252.9293079376220703
KDTree
found:12442518.166933059692383with sort and sectioning
found:1244252.3414530754089355
complete.
Note: In first plot, cdist
overlays the sectioning
. Playoffs are shown in second plot.
The "playoffs"
Modified sectioning code
This code finds the minimum within the points within radius. Runtime is equivalent to the sectioning code above.
print('with sort and sectioning, and min finding')
result = 0
pairings = {}
tic = time.time()
B.sort()
defdist_squared(a, b):
# note (x, y) in point b will be inverted (below), so need to index properlyreturn (a[0] - b[1])**2 + (a[1] - b[0])**2for idx, point inenumerate(A):
# gather the neighborhood in x-dimension within x-r <= x <= x+r+1# if this has any merit, we could "do it again" for y-coord....
contenders = B[bisect(B,(point[0]-r-delta, 0)) : bisect(B,(point[0]+r+delta, 0))]
# further chop down to the y-neighborhood# flip the coordinate to support bisection by y-value
contenders = list(map(lambda p: (p[1], p[0]), contenders))
contenders.sort()
contenders = contenders[bisect(contenders,(point[1]-r-delta, 0)) :
bisect(contenders,(point[1]+r+delta, 0))]
matches = [(dist_squared(point, p2), point, p2) for p2 in contenders
if dist_squared(point, p2) <= r_squared]
if matches:
pairings[idx] = min(matches)[1] # pair the closest point in B with the point in A
toc = time.time()
print(toc-tic)
Solution 2:
What you probably want is KDTrees (which are slow in high dimensions, but should be blazingly fast for your problem. The python implementation even implements the radius bound.
Post a Comment for "Given Two Lists Of 2d Points, How To Find The Closest Point In The 2nd List For Every Point In The 1st List?"