def testinit_with_center(self):
     M = MetricSpace([Point([2 * i, 2 * i]) for i in range(100)])
     MetricCell = Cell(MetricSpace())
     C = MetricCell(Point([99, 99]))
     for p in M:
         C.addpoint(p)
     self.assertEqual(len(C), 101)
예제 #2
0
 def test_fromstring(self):
     p = Point.fromstring("  4  2.01 999")
     self.assertEqual(len(p), 3)
     a = Point.fromstring(' 3  4  12')
     self.assertEqual(len(a), 3)
     b = Point([0, 0, 0])
     self.assertEqual(a.dist(b), 13)
 def testaddpoint(self):
     a, b, c = Point([1, 2]), Point([2, 3]), Point([3, 4])
     MetricCell = Cell(MetricSpace())
     C = MetricCell(a)
     self.assertEqual(len(C), 1)
     C.addpoint(b)
     self.assertEqual(len(C), 2)
     C.addpoint(c)
     self.assertEqual(len(C), 3)
     self.assertEqual(C.points, {a, b, c})
예제 #4
0
 def testnnsearch(self):
     coords = [
         200, -190, 57, 40, 20, 10, 170, 100, 195, -50, -30, 340, -120,
         -230, 310, 300, -300
     ]
     P = [Point([x]) for x in coords]
     T = CoverTree(P)
     for query in range(-300, 300, 1):
         q = Point([query])
         nn1 = T.nn(q)
         nn2 = min(P, key=q.dist)
         assert nn1.dist(q) == nn2.dist(q)
 def testpop(self):
     a, b, c, d = Point([0, 0]), Point([100,
                                        0]), Point([0, 50]), Point([25, 25])
     MetricCell = Cell(MetricSpace())
     C = MetricCell(a)
     C.addpoint(b)
     C.addpoint(c)
     C.addpoint(d)
     self.assertEqual(C.pop(), b)
     self.assertEqual(C.pop(), c)
     self.assertEqual(C.pop(), d)
     self.assertEqual(C.pop(), None)
 def testrebalance(self):
     a, b = Point([-1]), Point([200])
     G = NeighborGraph(MetricSpace([a, b]))
     MetricCell = Cell(MetricSpace())
     A = MetricCell(a)
     B = MetricCell(b)
     for i in range(200):
         B.addpoint(Point([i]))
     self.assertEqual(len(A), 1)
     self.assertEqual(len(B), 201)
     G.rebalance(A, B)
     self.assertEqual(len(B), 101)
     self.assertEqual(len(A), 101)
 def testaddpoint_duplicatepoint(self):
     a, b = Point([1, 2]), Point([2, 3])
     MetricCell = Cell(MetricSpace())
     C = MetricCell(a)
     self.assertEqual(len(C), 1)
     C.addpoint(b)
     self.assertEqual(len(C), 2)
     # Add b a second time.
     C.addpoint(b)
     self.assertEqual(len(C), 2)
     self.assertEqual(C.points, {a, b})
     # Add the center again.
     C.addpoint(a)
     self.assertEqual(len(C), 2)
     self.assertEqual(C.points, {a, b})
예제 #8
0
 def test_dist_1D(self):
     P = [Point([i]) for i in range(10)]
     self.assertEqual(P[1].dist(P[5]), 4)
     self.assertEqual(P[5].dist(P[1]), 4)
     self.assertEqual(P[0].dist(P[9]), 9)
     self.assertEqual(P[1].dist(P[1]), 0)
     self.assertEqual(P[6].dist(P[8]), 2)
def test_points_on_a_line():
    P = [Point([i]) for i in range(100)]
    GP = list(onehopgreedy(MetricSpace(P), P[50]))
    assert(GP[0] == P[50])
    expected = [50, 16, 83]
    n = len(expected)
    assert(GP[:n] == [P[i] for i in expected])
예제 #10
0
 def testgreedytree_randomexample(self):
     greedy = self.implementation.greedy
     root = Point([0])
     P = MetricSpace([root] +
                     [Point([x]) for x in [8, 12, 100, 40, 70, 1, 72]])
     gp = greedy(P, root, tree=True)
     self.assertEqual(next(gp), (Point([0]), None))
     self.assertEqual(next(gp), (Point([100]), 0))  # radius = 100
     self.assertEqual(next(gp), (Point([40]), 0))
     self.assertEqual(next(gp), (Point([70]), 1))
     self.assertEqual(next(gp), (Point([12]), 0))
     self.assertEqual(next(gp), (Point([8]), 4))
     self.assertEqual(next(gp), (Point([72]), 3))
예제 #11
0
 def test_smallinstance(self):
     k = 11
     # S = set(range(0, 100, 4))
     S = set(range(0, 100, 4)) | set(range(100, 200, 10))
     P = [Point([c]) for c in S]
     M = MetricSpace(P)
     output = list(knnsample(M, k, P[0]))
     print(len(output), output)
     self.assertTrue(len(output) >= len(S) / k)
예제 #12
0
 def testgreedy_exponential_example(self):
     greedy = self.implementation.greedy
     P = [Point([(-3)**i]) for i in range(100)]
     # print([str(p) for p in P])
     M = MetricSpace(P)
     gp = greedy(M, P[0])
     self.assertEqual(next(gp), P[0])
     self.assertEqual(next(gp), P[99])
     for i in range(98, 0, -1):
         self.assertEqual(next(gp), P[i])
예제 #13
0
 def testgreedytree_example2(self):
     greedy = self.implementation.greedy
     P = [Point([c]) for c in [0, 100, 49, 25, 60, 12, 81]]
     M = MetricSpace(P)
     root = P[0]
     gt = list(greedy(M, root, tree=True))
     gp = [p for p, i in gt]
     ch = defaultdict(list)
     for p, i in greedy(M, root, tree=True):
         if i is not None:
             ch[gp[i]].append(p)
     self.assertEqual(gp, [P[i] for i in [0, 1, 2, 3, 6, 5, 4]])
예제 #14
0
 def test_dist_with_different_dimensions(self):
     """If two points are in different dimensions, the higher dimensional
     point should be projected to the lower dimensional space.
     """
     p, q = Point([3, 4]), Point([0])
     self.assertEqual(p.dist(q), 3)
     r = Point([0, 0, 100])
     self.assertEqual(p.dist(r), 5)
예제 #15
0
    def testgreedytree_bigexample(self):
        greedy = self.implementation.greedy
        n = 600
        coords = set()
        while len(coords) < n:
            coords.add((randrange(100), randrange(100), randrange(100)))
        P = [Point(c) for c in coords]
        M = MetricSpace(P)

        GP = list(greedy(M, P[0], tree=True))
        radii = [p.dist(GP[i][0]) for p, i in GP if i is not None]
        # Check that the insertion radii are nonincreasing.
        for i in range(n - 2):
            self.assertTrue(radii[i] >= radii[i + 1],
                            str([i, radii[i], radii[i + 1]]))
예제 #16
0
    def testconstruction(self):
        """
                            a = 0
                          /       \
                    c = -19       b = 20
                                 /
                               d = 15

        """
        a, b, c, d = [Point([x]) for x in [0, 20, -19, 15]]
        T = CoverTree([a, b, c, d])
        self.assertEqual(T.root, a)
        self.assertTrue(b in T.children(a, 5))
        self.assertTrue(c in T.children(a, 5))
        self.assertTrue(d not in T.children(a, 5))
        self.assertTrue(c not in T.children(a, 4))
        self.assertEqual(set(T.ch), {(a, 5), (b, 3)})
예제 #17
0
    def testgreedytree_example3(self):
        greedy = self.implementation.greedy
        P = [Point([c]) for c in [0, 1, 3, 5, 20, 30]]
        M = MetricSpace(P)
        gt = list(greedy(M, P[0], tree=True))
        gp = [p for p, i in gt]
        ch = defaultdict(set)
        for p, i in gt:
            if i is not None:
                ch[gp[i]].add(p)

        self.assertEqual(gp, [P[0], P[5], P[4], P[3], P[2], P[1]])
        self.assertEqual(ch[P[0]], {P[5], P[3], P[1]})
        self.assertEqual(ch[P[1]], set())
        self.assertEqual(ch[P[2]], set())
        self.assertEqual(ch[P[3]], {P[2]})
        self.assertEqual(ch[P[4]], set())
        self.assertEqual(ch[P[5]], {P[4]})
예제 #18
0
 def test_hash(self):
     a = Point([30000000000, 4])
     b = Point([30000000000.0, 4.0])
     self.assertEqual(hash(a), hash(b))
예제 #19
0
 def testinsertduplicate(self):
     coords = [0, 200, 300, 200]
     P = [Point([x]) for x in coords]
     T = CoverTree(P)
예제 #20
0
 def testgreedy(self):
     greedy = self.implementation.greedy
     P = [Point([i]) for i in range(3)]
     M = MetricSpace(P)
     gp = list(greedy(M, P[0]))
     self.assertEqual(gp, [P[0], P[2], P[1]])
 def testbasicusage(self):
     G = NeighborGraph(MetricSpace([Point([i, i]) for i in range(100)]))
 def testdist(self):
     MetricCell = Cell(MetricSpace())
     A = MetricCell(Point([2, 3]))
     self.assertEqual(A.dist(Point([7, 3])), 5)
     self.assertEqual(A.dist(A.center), 0)
     self.assertEqual(A.dist(Point([7, 15])), 13)
예제 #23
0
    def testgreedytree_transportplan(self):
        """
        This test determines that greedy() computes the correct transportation plan
        """
        greedy = self.implementation.greedy
        root = Point([0])
        P = MetricSpace([root] + [Point([x]) for x in [9, 3, 5, 18]])
        #gp = greedy(P, root, tree=False, gettransportplan = True)
        gp = greedy(P, root, tree=False, gettransportplan=True)

        # A case-wise assertion was needed because the way clarksongreedy.greedy works,
        # it looks into all neighbors which might have been changed when a new cell was
        # created. So the transportation plan has entries in the dictionary with 0 mass
        # moved. On the other hand quadraticgreedy.greedy stores points in the
        # transportation plan only if their reverse nearest neighbor changed.
        # So there are no 0 value keys here.
        if self.implementation == clarksongreedy:
            self.assertEqual(next(gp), (Point([0]), {Point([0]): 5}))
            self.assertEqual(next(gp), (Point([18]), {
                Point([0]): -1,
                Point([18]): 1
            }))
            self.assertEqual(next(gp), (Point([9]), {
                Point([9]): 2,
                Point([0]): -2,
                Point([18]): 0
            }))
            self.assertEqual(next(gp), (Point([5]), {
                Point([9]): -1,
                Point([0]): -1,
                Point([5]): 2
            }))
            self.assertEqual(next(gp), (Point([3]), {
                Point([9]): 0,
                Point([3]): 1,
                Point([5]): -1
            }))
        else:
            self.assertEqual(next(gp), (Point([0]), {Point([0]): 5}))
            self.assertEqual(next(gp), (Point([18]), {
                Point([0]): -1,
                Point([18]): 1
            }))
            self.assertEqual(next(gp), (Point([9]), {
                Point([9]): 2,
                Point([0]): -2
            }))
            self.assertEqual(next(gp), (Point([5]), {
                Point([9]): -1,
                Point([0]): -1,
                Point([5]): 2
            }))
            self.assertEqual(next(gp), (Point([3]), {
                Point([3]): 1,
                Point([5]): -1
            }))
예제 #24
0
 def testlen(self):
     P = [Point([1]), Point([1, 2]), Point([1, 2, 3])]
     self.assertEqual(len(P[0]), 1)
     self.assertEqual(len(P[1]), 2)
     self.assertEqual(len(P[2]), 3)
 def testupdateradius_empty_cell(self):
     MetricCell = Cell(MetricSpace())
     C = MetricCell(Point([1, 2, 3]))
     C.updateradius()
     self.assertEqual(C.radius, 0)
예제 #26
0
 def test_init_with_other_iterables(self):
     p = Point(range(100))
     self.assertEqual(len(p), 100)
예제 #27
0
 def test_dist_2D(self):
     a = Point([1, 3])
     b = Point([13, 8])
     self.assertEqual(a.dist(b), 13)
예제 #28
0
 def test_iter(self):
     p = Point(3 * i for i in range(4))
     for i, c in enumerate(p):
         self.assertEqual(c, 3 * i)
예제 #29
0
 def test_eq(self):
     a = Point([3, 4])
     b = Point([3.0, 4.0])
     self.assertTrue(a == b)
예제 #30
0
 def test_init_2D_points(self):
     # Just checking that init doesn't crash
     Point([0, 0])
     Point([-1, 2])
     Point([-1.1, 2.99])