def test_find_neighbor_and_sdist(self, PointSet): ps = PointSet fp = FastPair().build(ps) rando = rand_tuple(len(ps[0])) neigh = fp._find_neighbor(rando) # Abusing find_neighbor! dist = fp.dist(rando, neigh["neigh"]) assert abs(dist - neigh["dist"]) < 1e-8 assert len(fp) == len(ps) # Make sure we didn't add a point... l = [(fp.dist(a, b), b) for a, b in zip(cycle([rando]), ps)] res = min(l, key=itemgetter(0)) assert abs(res[0] - neigh["dist"]) < 1e-8 assert res[1] == neigh["neigh"] res = min(fp.sdist(rando), key=itemgetter(0)) assert abs(neigh["dist"] - res[0]) < 1e-8 assert neigh["neigh"] == res[1]
def test_find_neighbor_and_sdist(self): ps = PointSet() fp = FastPair().build(ps) rando = rand_tuple(len(ps[0])) neigh = fp._find_neighbor(rando) # Abusing find_neighbor! dist = fp.dist(rando, neigh["neigh"]) assert abs(dist - neigh["dist"]) < 1e-8 assert len(fp) == len(ps) # Make sure we didn't add a point... l = [(fp.dist(a, b), b) for a, b in zip(cycle([rando]), ps)] res = min(l, key=itemgetter(0)) assert abs(res[0] - neigh["dist"]) < 1e-8 assert res[1] == neigh["neigh"] res = min(fp.sdist(rando), key=itemgetter(0)) assert abs(neigh["dist"] - res[0]) < 1e-8 assert neigh["neigh"] == res[1]
def test_all_closest_pairs(self): ps = PointSet() fp = FastPair().build(ps) cp = fp.closest_pair() bf = fp.closest_pair_brute_force() # Ordering should be the same # dc = fp.closest_pair_divide_conquer() # Maybe different ordering assert abs(cp[0] - bf[0]) < 1e-8 assert cp[1] == bf[1] # Tuple comparison test = min([(fp.dist(a, b), (a, b)) for a, b in combinations(ps, r=2)], key=itemgetter(0)) assert abs(cp[0] - test[0]) < 1e-8 assert sorted(cp[1]) == sorted(test[1]) # Tuple comparison
def test_update_point(self, PointSet): # Still failing sometimes... ps = PointSet fp = FastPair().build(ps) assert len(fp) == len(ps) old = ps[0] # Just grab the first point... new = rand_tuple(len(ps[0])) res = fp._update_point(old, new) assert old not in fp assert new in fp assert len(fp) == len(ps) # Size shouldn't change l = [(fp.dist(a, b), b) for a, b in zip(cycle([new]), ps)] res = min(l, key=itemgetter(0)) neigh = fp.neighbors[new]
def test_all_closest_pairs(self, PointSet): ps = PointSet fp = FastPair().build(ps) cp = fp.closest_pair() bf = fp.closest_pair_brute_force() # Ordering should be the same # dc = fp.closest_pair_divide_conquer() # Maybe different ordering assert abs(cp[0] - bf[0]) < 1e-8 assert cp[1] == bf[1] # Tuple comparison test = min( [(fp.dist(a, b), (a, b)) for a, b in combinations(ps, r=2)], key=itemgetter(0), ) assert abs(cp[0] - test[0]) < 1e-8 assert sorted(cp[1]) == sorted(test[1]) # Tuple comparison
def test_update_point(self): # Still failing sometimes... ps = PointSet() fp = FastPair().build(ps) assert len(fp) == len(ps) old = ps[0] # Just grab the first point... new = rand_tuple(len(ps[0])) res = fp._update_point(old, new) assert old not in fp assert new in fp assert len(fp) == len(ps) # Size shouldn't change l = [(fp.dist(a, b), b) for a, b in zip(cycle([new]), ps)] res = min(l, key=itemgetter(0)) neigh = fp.neighbors[new] assert abs(res[0] - neigh["dist"]) < 1e-8 assert res[1] == neigh["neigh"]
def test_cluster(self): ps = PointSet() fp = FastPair().build(ps) for i in range(len(fp)-1): # Version one dist, (a, b) = fp.closest_pair() c = interact(a, b) fp -= b # Drop b fp -= a fp += c # Order gets reversed here... d, (e, f) = min([(fp.dist(i, j), (i, j)) for i, j in combinations(ps, r=2)], key=itemgetter(0)) g = interact(e, f) assert abs(d - dist) < 1e-8 assert (a == e or b == e) and (b == f or a == f) assert c == g ps.remove(e) ps.remove(f) ps.append(g) assert contains_same(fp.points, ps) assert len(fp.points) == len(ps) == 1
def test_cluster(self, PointSet): ps = PointSet fp = FastPair().build(ps) for i in range(len(fp) - 1): # Version one dist, (a, b) = fp.closest_pair() c = interact(a, b) fp -= b # Drop b fp -= a fp += c # Order gets reversed here... d, (e, f) = min( [(fp.dist(i, j), (i, j)) for i, j in combinations(ps, r=2)], key=itemgetter(0), ) g = interact(e, f) assert abs(d - dist) < 1e-8 assert (a == e or b == e) and (b == f or a == f) assert c == g ps.remove(e) ps.remove(f) ps.append(g) assert contains_same(fp.points, ps) assert len(fp.points) == len(ps) == 1