def testconstruct(self): T = SNT(2, 1, 1, 4) p1 = Point([0], Euclidean()) p2 = Point([2], Euclidean()) p3 = Point([11], Euclidean()) p4 = Point([28], Euclidean()) T.construct([p2, p3, p4, p1], PL) self.assertEqual(T.root.getchild().point, p1) self.assertEqual(T.root.getchild().level, 5) n1 = [ch for ch in T.root.getchild().ch if ch.point == p1][0] n2 = [ch for ch in T.root.getchild().ch if ch.point == p4][0] self.assertTrue(n1.level == n2.level == 4) self.assertEqual(n2.getchild().point, p4) self.assertEqual(n2.getchild().level, 3) self.assertEqual(n2.getchild().getchild().level, float('-inf')) n3 = [ch for ch in n1.ch if ch.point == p1][0] n4 = [ch for ch in n1.ch if ch.point == p3][0] self.assertTrue(n3.level == n4.level == 3) self.assertEqual(n4.getchild().point, p3) self.assertEqual(n4.getchild().level, 2) self.assertEqual(n4.getchild().getchild().level, float('-inf')) self.assertEqual(n3.getchild().point, p1) self.assertEqual(n3.getchild().level, 2) self.assertEqual(n3.getchild().getchild().point, p1) self.assertEqual(n3.getchild().getchild().level, 1) n5 = [ch for ch in n3.getchild().getchild().ch if ch.point == p1][0] n6 = [ch for ch in n3.getchild().getchild().ch if ch.point == p2][0] self.assertTrue(n5.level == n6.level == 0) self.assertEqual(n5.getchild().point, p1) self.assertEqual(n5.getchild().level, -1) self.assertEqual(n6.getchild().point, p2) self.assertEqual(n6.getchild().level, -1) self.assertEqual(n5.getchild().getchild().level, float('-inf')) self.assertEqual(n6.getchild().getchild().level, float('-inf'))
def testfindleaves(self): T = SNT(3, 1, 1) T.root = Node(Point([0], Euclidean()), float('inf')) n1 = Node(Point([1], Euclidean()), 3) n2 = Node(Point([2], Euclidean()), 3) n3 = Node(Point([3], Euclidean()), 2) n4 = Node(Point([4], Euclidean()), 2) n5 = Node(Point([5], Euclidean()), 1) n6 = Node(Point([6], Euclidean()), 1) T.root.addch(n1) T.root.addch(n2) n1.addch(n3) n1.addch(n4) n4.addch(n5) n4.addch(n6) ver = SNTVerify(T, []) dic = dict() ver.findleaves(T.root, dic) self.assertEqual(dic[T.root], {n2, n3, n5, n6}) self.assertEqual(dic[n1], {n3, n5, n6}) self.assertEqual(dic[n2], {n2}) self.assertEqual(dic[n3], {n3}) self.assertEqual(dic[n4], {n5, n6}) self.assertEqual(dic[n5], {n5}) self.assertEqual(dic[n6], {n6})
def testaddnode(self): T = SNT(5, 1, 1) T.setroot(Point([0], Euclidean())) ploc = SNTPointLocation(T, []) a = Node(Point([1], Euclidean()), 2) ploc.addnode(a) self.assertEqual(ploc._rnn_in[a], set()) self.assertEqual(ploc._rnn_out[a], set())
def testsetroot(self): T = SNT(3, 1, 2, 2) rootpoint = Point([1, 2, 3, 4, 5], Euclidean()) T.setroot(rootpoint) self.assertEqual(T.root.point, rootpoint) self.assertEqual(T.root.level, float('inf')) self.assertEqual(T.root.getchild().point, rootpoint) self.assertEqual(T.root.getchild().level, float('-inf'))
def testsplitbelow_root(self): T = SNT(3, 1, 1) T.setroot(Point([0], Euclidean())) n = T.splitbelow(T.root, 1) self.assertEqual(n.point, T.root.point) self.assertEqual(n.level, 1) self.assertEqual(n.par, T.root) self.assertEqual(T.root.ch, {n}) self.assertTrue(n in T.root.ch) self.assertEqual(n.getchild().level, float('-inf')) self.assertEqual(n.rel, {n})
def testinit(self): T = SNT(5, 1, 1) T.setroot(Point([0], Euclidean())) points = [Point([i], Euclidean()) for i in [1, 2, 4, 8]] ploc = SNTPointLocation(T, points) for p in points: self.assertEqual(ploc._nn[p], T.root) self.assertEqual(len(ploc._rnn_in), 1) self.assertEqual(len(ploc._rnn_out), 1) self.assertEqual(ploc._rnn_in[T.root], set(points)) self.assertEqual(len(ploc._rnn_out[T.root]), 0)
def testisrel(self): T = SNT(3, 1, 1, 4) metric = Euclidean() a = Node(Point([0, 0], metric), 2) b = Node(Point([0, 35], metric), 2) c = Node(Point([0, 37], metric), 2) self.assertTrue(T.isrel(a, a)) self.assertEqual(metric.counter, 0) self.assertTrue(T.isrel(a, b)) self.assertFalse(T.isrel(a, c)) self.assertEqual(metric.counter, 2)
def testfinduncomplevels(self): T = SNT(2, 1, 1, 4) p1 = Point([0], Euclidean()) p2 = Point([2], Euclidean()) p3 = Point([11], Euclidean()) p4 = Point([28], Euclidean()) T.setroot(p1) T.insert(p2, T.root) T.insert(p3, T.root) T.insert(p4, [ch for ch in T.root.getchild().ch if ch.point == p3][0]) ver = SNTVerify(T, [p1, p2, p3, p4]) ver.finduncomplevels() self.assertEqual(len(ver.uncomplevels), 7) self.assertEqual(next(iter(ver.uncomplevels[5])).point, p1) self.assertEqual(next(iter(ver.uncomplevels[5])).level, 5) self.assertEqual({n.point for n in ver.uncomplevels[4]}, {p1, p4}) self.assertEqual(len([n.level for n in ver.uncomplevels[4] if n.level == 4]), 2) self.assertEqual({n.point for n in ver.uncomplevels[3]}, {p1, p3, p4}) self.assertEqual(len([n.level for n in ver.uncomplevels[3] if n.level == 3]), 3) self.assertEqual({n.point for n in ver.uncomplevels[2]}, {p1, p3, p4}) self.assertEqual(len([n.level for n in ver.uncomplevels[2] if n.level == 2]), 2) self.assertEqual(len([n.level for n in ver.uncomplevels[2] if n.level == 3]), 1) self.assertEqual({n.point for n in ver.uncomplevels[1]}, {p1, p3, p4}) self.assertEqual(len([n.level for n in ver.uncomplevels[1] if n.level == 1]), 1) self.assertEqual(len([n.level for n in ver.uncomplevels[1] if n.level == 2]), 1) self.assertEqual(len([n.level for n in ver.uncomplevels[1] if n.level == 3]), 1) self.assertEqual({n.point for n in ver.uncomplevels[0]}, {p1, p2, p3, p4}) self.assertEqual(len([n.level for n in ver.uncomplevels[0] if n.level == 0]), 2) self.assertEqual(len([n.level for n in ver.uncomplevels[0] if n.level == 2]), 1) self.assertEqual(len([n.level for n in ver.uncomplevels[0] if n.level == 3]), 1) self.assertEqual({n.point for n in ver.uncomplevels[-1]}, {p1, p2, p3, p4}) self.assertEqual(len([n.level for n in ver.uncomplevels[-1] if n.level == -1]), 2) self.assertEqual(len([n.level for n in ver.uncomplevels[-1] if n.level == 2]), 1) self.assertEqual(len([n.level for n in ver.uncomplevels[-1] if n.level == 3]), 1)
def testinit(self): T = SNT(5, 1, 2) self.assertEqual(T.tau, 5) self.assertEqual(T.cp, 1) self.assertEqual(T.cc, 2) self.assertEqual(T.cr, 20) self.assertTrue(hasattr(T, 'cr')) self.assertTrue(hasattr(T, 'root'))
def testrnn(self): T = SNT(3, 1, 1) T.setroot(Point([0], Euclidean())) ploc = SNTPointLocation(T, []) a = Point([1], Euclidean()) b = Point([2], Euclidean()) c = Point([3], Euclidean()) d = Point([4], Euclidean()) n = Node(Point([10], Euclidean()), 3) ploc.addnode(n) ploc._rnn_in[T.root] = {a} ploc._rnn_out[T.root] = {b} ploc._rnn_in[n] = {c} ploc._rnn_out[n] = {d} self.assertEqual(ploc.rnn_in(T.root), {a}) self.assertEqual(ploc.rnn_out(T.root), {b}) self.assertEqual(ploc.rnn_in(n), {c}) self.assertEqual(ploc.rnn_out(n), {d}) self.assertEqual(ploc.rnn_in([T.root, n]), {a, c}) self.assertEqual(ploc.rnn_out([T.root, n]), {b, d}) self.assertEqual(ploc.rnn([T.root, n]), {a, b, c, d})
def testupdateonsplit(self): T = SNT(3, 1, 1) T.tau = 2 T.cr = 3 r = Point([0], Euclidean()) T.setroot(r) a = Point([1], Euclidean()) b = Point([8], Euclidean()) c = Point([13], Euclidean()) d = Point([30], Euclidean()) e = Point([63], Euclidean()) f = Point([96], Euclidean()) ploc = SNTPointLocation(T, [a, b, c, d, e, f]) n1 = Node(r, 7) n2 = Node(r, 3) n1.addch(n2) ploc._rnn_in[n1] = {a, b, c, d} ploc._rnn_out[n1] = {e, f} ploc._nn[a] = ploc._nn[b] = ploc._nn[c] = ploc._nn[d] = ploc._nn[ e] = ploc._nn[f] = n1 ploc.updateonsplit(n2) self.assertEqual(ploc._rnn_in[n1], {d}) self.assertEqual(ploc._rnn_out[n1], {e, f}) self.assertEqual(ploc._rnn_in[n2], {a}) self.assertEqual(ploc._rnn_out[n2], {b, c})
def testsplitabove(self): T = SNT(4, 1, 1, 2) T.setroot(Point([0], Euclidean())) a = T.splitbelow(T.root, 10) b = T.splitbelow(a, 0) n = T.splitabove(b, 5) self.assertEqual(n.level, 5) self.assertEqual(n.ch, {b}) self.assertEqual(n.par, a) self.assertEqual(b.par, n) self.assertEqual(a.ch, {n})
def testrelativescorrect(self): T = SNT(2, 1, 1, 4) p1 = Point([0], Euclidean()) p2 = Point([2], Euclidean()) p3 = Point([11], Euclidean()) p4 = Point([28], Euclidean()) T.setroot(p1) T.insert(p2, T.root) T.insert(p3, T.root) T.insert(p4, [ch for ch in T.root.getchild().ch if ch.point == p3][0]) ver = SNTVerify(T, [p1, p2, p3, p4]) ver.populate() self.assertTrue(ver.relativescorrect()) [n for n in ver.uncomplevels[2] if n.point == p1][0].rel.discard([n for n in ver.uncomplevels[2] if n.point == p3][0]) self.assertFalse(ver.relativescorrect())
def testiscovered(self): T = SNT(3, 1, 1) metric = Euclidean() a = Node(Point([0, 0], metric), 2) b = Node(Point([0, 12], metric), 1) c = Node(b.point, 0) a.addch(b) b.addch(c) self.assertTrue(T.iscovered(c)) self.assertEqual(metric.counter, 0) self.assertFalse(T.iscovered(b)) T.cc = 2 self.assertTrue(T.iscovered(b))
def testsplitbelow_leaf(self): T = SNT(3, 1, 1) T.setroot(Point([0], Euclidean())) a = T.splitbelow(T.root, 10) n = T.splitbelow(a, 0) self.assertEqual(n.point, a.point) self.assertEqual(n.level, 0) self.assertEqual(n.par, a) self.assertEqual(a.ch, {n}) self.assertTrue(n in a.ch) self.assertTrue(n not in T.root.ch) self.assertEqual(n.getchild().level, float('-inf')) self.assertEqual(n.rel, {n})
def testislocalnettree(self): T = SNT(2, 1, 1, 4) p1 = Point([0], Euclidean()) p2 = Point([2], Euclidean()) p3 = Point([11], Euclidean()) p4 = Point([28], Euclidean()) T.setroot(p1) T.insert(p2, T.root) T.insert(p3, T.root) T.insert(p4, [ch for ch in T.root.getchild().ch if ch.point == p3][0]) ver = SNTVerify(T, [p1, p2, p3, p4]) ver.populate() self.assertTrue(ver.islocalnettree()) p3.coords = [8] self.assertFalse(ver.islocalnettree()) p3.coords = [17] self.assertFalse(ver.islocalnettree()) p3.coords = [11] self.assertTrue(ver.islocalnettree()) n1 = [n for n in ver.uncomplevels[3] if n.point == p1][0] n1.point = Point([1], Euclidean()) self.assertFalse(ver.islocalnettree())
def testfindminlevelrels(self): T = SNT(2, 1, 1, 4) p1 = Point([0], Euclidean()) p2 = Point([2], Euclidean()) p3 = Point([11], Euclidean()) p4 = Point([28], Euclidean()) T.setroot(p1) T.insert(p2, T.root) T.insert(p3, T.root) T.insert(p4, [ch for ch in T.root.getchild().ch if ch.point == p3][0]) ver = SNTVerify(T, [p1, p2, p3, p4]) ver.findminlevelrels() self.assertEqual(ver.minlevels[(p1, p1)], float('-inf')) self.assertEqual(ver.minlevels[(p2, p2)], float('-inf')) self.assertEqual(ver.minlevels[(p3, p3)], float('-inf')) self.assertEqual(ver.minlevels[(p4, p4)], float('-inf')) self.assertEqual(ver.minlevels[(p1, p2)], -1) self.assertEqual(ver.minlevels[(p1, p3)], 2) self.assertEqual(ver.minlevels[(p1, p4)], 3) self.assertEqual(ver.minlevels[(p2, p3)], 2) self.assertEqual(ver.minlevels[(p2, p4)], 3) self.assertEqual(ver.minlevels[(p3, p4)], 3)
def testinsert_onepointafterroot(self): T = SNT(4, 1, 1, 4) metric = Euclidean() T.setroot(Point([0, 0], metric)) T.insert(Point([6, 0], metric), T.root) # Check root got split below a = next(iter(T.root.ch)) self.assertEqual(a.point, T.root.point) self.assertEqual(a.level, 2) self.assertEqual(len(a.ch), 2) b, c = tuple(child for child in a.ch) self.assertTrue(b.point is not c.point) self.assertEqual(b.level, 1) self.assertEqual(c.level, 1) self.assertEqual(b.getchild().level, float('-inf')) self.assertEqual(c.getchild().level, float('-inf'))
def testremovenode(self): T = SNT(5, 1, 1) p1 = Point([.5], Euclidean()) p2 = Point([.25], Euclidean()) p3 = Point([2], Euclidean()) p4 = Point([5], Euclidean()) p5 = Point([30], Euclidean()) ploc = SNTPointLocation(T, [p1, p2, p3, p4, p5]) a = Node(Point([0], Euclidean()), 2) b = Node(Point([0], Euclidean()), 1) c = Node(Point([0], Euclidean()), float('-inf')) a.addch(b) b.addch(c) ploc.addnode(a) ploc._nn[p1] = ploc._nn[p2] = ploc._nn[p3] = ploc._nn[p4] = ploc._nn[ p5] = b ploc._rnn_in[b] = {p1, p2} ploc._rnn_out[b] = {p3, p4, p5} ploc.updateonremoval(b) self.assertTrue(ploc._nn[p1] == ploc._nn[p2] == ploc._nn[p3] == ploc._nn[p4] == ploc._nn[p5] == a) self.assertEqual(ploc._rnn_in[a], {p1, p2, p3}) self.assertEqual(ploc._rnn_out[a], {p4, p5})
def testall(self): points = [Point([x, 0, 1], Euclidean()) for x in [2, 11, 28, 0]] T = SNT(2, 1, 1, 4) T.construct(points, SNTPointLocation) stats = SNTStats(T) self.assertEqual(stats.levelno(), 6) self.assertEqual(stats.nodeno(), 12) self.assertEqual(stats.childno(), 12) self.assertEqual(stats.relno(), 26) self.assertEqual(stats.jumpno(), 0) points = [Point([x], Euclidean()) for x in [25, 20, 54, 30, 40, 0]] T = SNT(5, 1, 1) T.construct(points, SNTPointLocation) stats = SNTStats(T) self.assertEqual(stats.levelno(), 3) self.assertEqual(stats.nodeno(), 10) self.assertEqual(stats.childno(), 10) self.assertEqual(stats.relno(), 30) self.assertEqual(stats.jumpno(), 0) points = [Point([x, 0, 1], Euclidean()) for x in [2, 65, 69, 0]] T = SNT(2, 1, 1, 4) T.construct(points, SNTPointLocation) stats = SNTStats(T) self.assertEqual(stats.jumpno(), 2)
def testissemicompressed(self): T = SNT(2, 1, 1, 4) p1 = Point([0], Euclidean()) p2 = Point([2], Euclidean()) p3 = Point([11], Euclidean()) p4 = Point([28], Euclidean()) T.setroot(p1) T.insert(p2, T.root) T.insert(p3, T.root) T.insert(p4, [ch for ch in T.root.getchild().ch if ch.point == p3][0]) ver = SNTVerify(T, [p1, p2, p3, p4]) ver.populate() self.assertTrue(ver.issemicompressed()) n1 = [n for n in ver.uncomplevels[2] if n.point == p3][0] n2 = Node(p3, 1) n2.addch(n1.getchild()) n1.addch(n2) ver.populate() self.assertFalse(ver.issemicompressed()) p1 = Point([-9956], Euclidean()) p2 = Point([1288], Euclidean()) T = SNT(7, 1, 1, 14 / 3) T.setroot(p1) T.insert(p2, T.root) ver = SNTVerify(T, [p1, p2]) ver.populate() self.assertTrue(ver.issemicompressed())
def testconstructwithverification(self): points = [ Point([x, 0, 1], Euclidean()) for x in [8, 1, 2, 32, 64, 81, 80, 160] ] T = SNT(4, 1, 1, 4) T.construct(points, PL) ver = SNTVerify(T, points) ver.populate() self.assertTrue(ver.relativescorrect()) self.assertTrue(ver.issemicompressed()) self.assertTrue(ver.islocalnettree()) self.assertFalse(ver.isglobalnettree()) T.cc = 4 / 3 T.cp = 1 / 6 self.assertTrue(ver.isglobalnettree()) points = [Point([x], Euclidean()) for x in [7, 44, 30, 24, 76]] T = SNT(5, 1, 1) T.construct(points, PL) ver = SNTVerify(T, points) ver.populate() self.assertTrue(ver.relativescorrect()) self.assertTrue(ver.issemicompressed()) self.assertTrue(ver.islocalnettree()) T.cc = 5 / 4 T.cp = 1 / 4 self.assertTrue(ver.isglobalnettree()) points = [Point([x], Euclidean()) for x in [25, 20, 54, 30, 40, 0]] T = SNT(5, 1, 1) T.construct(points, PL) ver = SNTVerify(T, points) ver.populate() self.assertTrue(ver.relativescorrect()) self.assertTrue(ver.issemicompressed()) self.assertTrue(ver.islocalnettree()) T.cc = 5 / 4 T.cp = 1 / 4 self.assertTrue(ver.isglobalnettree()) points = [ Point([x], Euclidean()) for x in [-55, 93, -90, -14, -13, -12] ] T = SNT(7, 1, 1) T.construct(points, PL) ver = SNTVerify(T, points) ver.populate() self.assertTrue(ver.relativescorrect()) self.assertTrue(ver.islocalnettree()) self.assertTrue(ver.issemicompressed()) metric = Euclidean() points = [ Point([random.randint(-10000, 10000) for _ in range(2)], metric) for _ in range(200) ] tmp = list() for p in points: if p in tmp: print('duplicate:', p) else: tmp.append(p) points = tmp tau = 7 T = SNT(tau, 1, 1) T.construct(points, PL) ver = SNTVerify(T, points) ver.populate() self.assertTrue(ver.relativescorrect()) self.assertTrue(ver.islocalnettree()) self.assertTrue(ver.issemicompressed()) T.cc = tau / (tau - 1) T.cp = (tau - 3) / (2 * (tau - 1)) self.assertTrue(ver.isglobalnettree())
def testupdateoninsertion(self): T = SNT(3, 1, 1) T.cr = 2 p1 = Point([0], Euclidean()) p2 = Point([10], Euclidean()) p3 = Point([40], Euclidean()) p4 = Point([120], Euclidean()) p5 = Point([130], Euclidean()) p6 = Point([160], Euclidean()) p7 = Point([20], Euclidean()) p8 = Point([26], Euclidean()) p9 = Point([38], Euclidean()) p10 = Point([-60], Euclidean()) p11 = Point([70], Euclidean()) p12 = Point([300], Euclidean()) p13 = Point([80], Euclidean()) p14 = Point([105], Euclidean()) p15 = Point([140], Euclidean()) n1 = Node(p1, 4) n2 = Node(p1, 3) n3 = Node(p1, 2) n4 = Node(p3, 3) n5 = Node(p2, 2) n6 = Node(p6, 4) n7 = Node(p6, 3) n8 = Node(p5, 3) n9 = Node(p5, 2) n10 = Node(p4, 2) n1.addch(n2) n1.addch(n4) n2.addch(n3) n2.addch(n5) n6.addch(n7) n6.addch(n8) n8.addch(n9) n8.addch(n10) n1.addrel(n6) n2.addrel(n4) n3.addrel(n5) n7.addrel(n8) n9.addrel(n10) ploc = SNTPointLocation(T, [p7, p8, p9, p10, p11, p12, p13, p14, p15]) ploc._nn[p7] = ploc._nn[p8] = n5 ploc._nn[p9] = n2 ploc._nn[p10] = ploc._nn[p11] = n1 ploc._nn[p12] = n6 ploc._nn[p13] = n8 ploc._nn[p14] = n10 ploc._nn[p15] = n9 ploc._rnn_out[n1] = {p10, p11} ploc._rnn_out[n2] = {p9} ploc._rnn_out[n3] = set() ploc._rnn_out[n5] = {p7, p8} ploc._rnn_out[n6] = {p12} ploc._rnn_out[n7] = set() ploc._rnn_out[n8] = {p13} ploc._rnn_out[n9] = {p15} ploc._rnn_out[n10] = {p14} ploc._rnn_in[n1] = ploc._rnn_in[n2] = ploc._rnn_in[n3] = ploc._rnn_in[ n5] = set() ploc._rnn_in[n6] = ploc._rnn_in[n7] = ploc._rnn_in[n8] = ploc._rnn_in[ n9] = ploc._rnn_in[n10] = set() ploc.updateoninsertion(n4) self.assertEqual(ploc._rnn_in[n4], {p9}) self.assertEqual(ploc._rnn_out[n4], {p8, p11, p13}) self.assertTrue(ploc._nn[p8] == ploc._nn[p9] == ploc._nn[p11] == ploc._nn[p13] == n4) self.assertEqual(ploc._rnn_out[n1], {p10}) self.assertEqual(ploc._rnn_out[n2], set()) self.assertEqual(ploc._rnn_out[n5], {p7}) self.assertEqual(ploc._rnn_out[n6], {p12}) self.assertEqual(ploc._rnn_out[n8], set()) self.assertEqual(ploc._rnn_out[n9], {p15}) self.assertEqual(ploc._rnn_out[n10], {p14}) self.assertEqual(ploc._nn[p7], n5) self.assertEqual(ploc._nn[p10], n1) self.assertEqual(ploc._nn[p12], n6) self.assertEqual(ploc._nn[p14], n10) self.assertEqual(ploc._nn[p15], n9)