Example #1
0
 def test_symmetry(self):
     trees = itertools.product((randtree(5, repeat=3, width=2) for x in xrange(N)), repeat=2)
     for a,b in trees:
         ab = compare.distance(a,b)
         ba = compare.distance(b,a)
         #print '-----------------------------'
         #print ab, ba
         self.assertEquals(ab, ba)
Example #2
0
 def test_symmetry(self):
     trees = itertools.product(
         (randtree(5, repeat=3, width=2) for x in xrange(N)), repeat=2)
     for a, b in trees:
         ab = compare.distance(a, b)
         ba = compare.distance(b, a)
         #print '-----------------------------'
         #print ab, ba
         self.assertEquals(ab, ba)
Example #3
0
 def test_triangle_inequality(self):
     trees = itertools.product((randtree(5, repeat=3, width=2) for x in xrange(N)), (randtree(5, repeat=3, width=2) for x in xrange(N)), (randtree(5, repeat=3, width=2) for x in xrange(N)))
     for a,b,c in trees:
         #print '--------------------------------'
         ab = compare.distance(a,b)
         bc = compare.distance(b,c)
         ac = compare.distance(a,c)
         #print ab, bc, ac
         self.assertTrue(ac <= ab + bc)
Example #4
0
 def test_triangle_inequality(self):
     trees = itertools.product(
         (randtree(5, repeat=3, width=2) for x in xrange(N)),
         (randtree(5, repeat=3, width=2) for x in xrange(N)),
         (randtree(5, repeat=3, width=2) for x in xrange(N)))
     for a, b, c in trees:
         #print '--------------------------------'
         ab = compare.distance(a, b)
         bc = compare.distance(b, c)
         ac = compare.distance(a, c)
         #print ab, bc, ac
         self.assertTrue(ac <= ab + bc)
Example #5
0
def test_paper_tree():
    A = (
      Node("f")
        .addkid(Node("d")
          .addkid(Node("a"))
          .addkid(Node("c")
            .addkid(Node("b"))
          )
        )
        .addkid(Node("e"))
    )
    B = (
      Node("f")
        .addkid(Node("c")
          .addkid(Node("d")
            .addkid(Node("a"))
            .addkid(Node("b"))
          )
        )
        .addkid(Node("e"))
    )
    #print A
    #print
    #print B
    dist = compare.distance(A,B)
    assert dist == 2
Example #6
0
def test_wrong_index_regression():
    A = Node('r')
    B = (
        Node("a")
          .addkid(Node("b")
            .addkid(Node("c"))
            .addkid(Node("d"))
          )
          .addkid(Node("e")
            .addkid(Node("f"))
            .addkid(Node("g"))
          )
     )
    def insert(n):
        return ord(n.label[0])
    def remove(n):
        return ord(n.label[0])
    def update(a,b):
        return 10000*strdist(a.label, b.label)
    dist = distance(A, B,
        get_children=(lambda n: n.children),
        insert_cost=insert,
        remove_cost=remove,
        update_cost=update,
    )
    assert dist == 814
Example #7
0
def test_paper_tree():
    Node = WeirdNode
    A = (
      Node("f")
        .addkid(Node("d")
          .addkid(Node("a"))
          .addkid(Node("c")
            .addkid(Node("b"))
          )
        )
        .addkid(Node("e"))
    )
    B = (
      Node("f")
        .addkid(Node("c")
          .addkid(Node("d")
            .addkid(Node("a"))
            .addkid(Node("b"))
          )
        )
        .addkid(Node("e"))
    )
    #print A
    #print
    #print B
    dist = compare.distance(A,B,WeirdNode.get_children, WeirdNode.get_label,
        weird_dist)
    assert dist == 20
def test_wrong_index_regression():
    A = Node('r')
    B = (
        Node("a")
          .addkid(Node("b")
            .addkid(Node("c"))
            .addkid(Node("d"))
          )
          .addkid(Node("e")
            .addkid(Node("f"))
            .addkid(Node("g"))
          )
     )
    def insert(n):
        return ord(n.label[0])
    def remove(n):
        return ord(n.label[0])
    def update(a,b):
        return 10000*strdist(a.label, b.label)
    dist = distance(A, B,
        get_children=(lambda n: n.children),
        insert_cost=insert,
        remove_cost=remove,
        update_cost=update,
    )
    assert dist == 814
Example #9
0
 def test_nondegenercy(self):
     trees = itertools.product((randtree(5, repeat=3, width=2) for x in xrange(N)), repeat=2)
     for a,b in trees:
         d = compare.distance(a,b)
         #print '-----------------------------'
         #print d, a is b
         self.assertTrue((d == 0 and a is b) or a is not b)
Example #10
0
    def test_labelchange(self):

        for A in (randtree(5, repeat=3, width=2) for x in xrange(N*4)):
            B = copy.deepcopy(A)
            node = random.choice([n for n in B.iter()])
            old_label = str(node.label)
            node.label = 'xty'
            assert compare.distance(A, B) == compare.strdist(old_label, node.label)
Example #11
0
def zssdist(t1, t2, names):
    """
    t1: Tree, t2:Tree -> x

    Return the edit distance, as computed by zss.compare, between t2zst-compatible trees t1, t2
    """
    t1, t2 = map(lambda x: t2zst(x, names), [t1, t2])
    return zscmp.distance(t1, t2)
Example #12
0
 def test_nondegenercy(self):
     trees = itertools.product(
         (randtree(5, repeat=3, width=2) for x in xrange(N)), repeat=2)
     for a, b in trees:
         d = compare.distance(a, b)
         #print '-----------------------------'
         #print d, a is b
         self.assertTrue((d == 0 and a is b) or a is not b)
Example #13
0
    def test_labelchange(self):

        for A in (randtree(5, repeat=3, width=2) for x in xrange(N * 4)):
            B = copy.deepcopy(A)
            node = random.choice([n for n in B.iter()])
            old_label = str(node.label)
            node.label = 'xty'
            assert compare.distance(A, B) == compare.strdist(
                old_label, node.label)
Example #14
0
def test_simplelabelchange():
    A = (Node("f").addkid(
        Node("a").addkid(Node("h")).addkid(Node("c").addkid(
            Node("l")))).addkid(Node("e")))
    B = (Node("f").addkid(
        Node("a").addkid(Node("d")).addkid(Node("r").addkid(
            Node("b")))).addkid(Node("e")))
    dist = compare.distance(A, B)
    print dist
    assert dist == 3
Example #15
0
 def test_distance(self):
     trees = itertools.product([tree1(), tree2(), tree3(), tree4()], repeat=2)
     for a,b in trees:
         ab = compare.distance(a,b)
         ba = compare.distance(b,a)
         #print '-----------------------------'
         #print a
         #print '------'
         #print b
         #print '------'
         #print ab, ba
         self.assertEquals(ab,ba)
         self.assertTrue((ab == 0 and a is b) or a is not b)
         #break
     trees = itertools.product([tree1(), tree2(), tree3(), tree4()], repeat=3)
     for a,b,c in trees:
         ab = compare.distance(a,b)
         bc = compare.distance(b,c)
         ac = compare.distance(a,c)
         self.assertTrue(ac <= ab + bc)
Example #16
0
def test_paper_tree():
    A = (Node("f").addkid(
        Node("d").addkid(Node("a")).addkid(Node("c").addkid(
            Node("b")))).addkid(Node("e")))
    B = (Node("f").addkid(
        Node("c").addkid(Node("d").addkid(Node("a")).addkid(
            Node("b")))).addkid(Node("e")))
    #print A
    #print
    #print B
    dist = compare.distance(A, B)
    assert dist == 2
Example #17
0
 def test_distance(self):
     trees = itertools.product(
         [tree1(), tree2(), tree3(), tree4()], repeat=2)
     for a, b in trees:
         ab = compare.distance(a, b)
         ba = compare.distance(b, a)
         #print '-----------------------------'
         #print a
         #print '------'
         #print b
         #print '------'
         #print ab, ba
         self.assertEquals(ab, ba)
         self.assertTrue((ab == 0 and a is b) or a is not b)
         #break
     trees = itertools.product(
         [tree1(), tree2(), tree3(), tree4()], repeat=3)
     for a, b, c in trees:
         ab = compare.distance(a, b)
         bc = compare.distance(b, c)
         ac = compare.distance(a, c)
         self.assertTrue(ac <= ab + bc)
Example #18
0
 def test_simplelabelchange(self):
     A = (
         Node("f")
             .addkid(Node("a")
                 .addkid(Node("h"))
                 .addkid(Node("c")
                     .addkid(Node("l"))))
             .addkid(Node("e"))
         )
     B = (
         Node("f")
             .addkid(Node("a")
                 .addkid(Node("d"))
                 .addkid(Node("c")
                     .addkid(Node("b"))))
             .addkid(Node("e"))
         )
     assert compare.distance(A, B) == 2
Example #19
0
def test_incorrect_behavior_regression():
    A = (
     Node("a")
       .addkid(Node("b")
         .addkid(Node("x"))
         .addkid(Node("y"))
       )
     )
    B = (
     Node("a")
       .addkid(Node("x"))
       .addkid(Node("b")
         .addkid(Node("y"))
       )
     )
    dist = compare.distance(A, B)
    print dist
    assert dist == 2
Example #20
0
def test_simplelabelchange():
    A = (
        Node("f")
            .addkid(Node("a")
                .addkid(Node("h"))
                .addkid(Node("c")
                    .addkid(Node("l"))))
            .addkid(Node("e"))
        )
    B = (
        Node("f")
            .addkid(Node("a")
                .addkid(Node("d"))
                .addkid(Node("r")
                    .addkid(Node("b"))))
            .addkid(Node("e"))
        )
    dist = compare.distance(A,B)
    print dist
    assert dist == 3
 trial_results = np.zeros(TRIALS, dtype=np.float64)
 trial_distances = np.zeros(TRIALS, dtype=np.float64)
 print "Trial",
 for trial in xrange(TRIALS):
     log.write("\tTrial: %d\n" % trial)
     original = Move.generate_random_move()
     mutant = original.copy()
     for x in xrange(radiation):
         mutant = mutant.mutate()
     prior_time = time.time()
     results = np.zeros(10 ** 6, dtype=np.float64)
     for i, row in enumerate(itertools.product(xrange(0, 100, 10), repeat=6)):
         s = state(row)
         results[i] = original.evaluate(s) - mutant.evaluate(s)
     # print sp.pearsonr(original_values, mutant_values)
     d = distance(original, mutant)
     deviation = np.std(results, dtype=np.float64)
     log.write("\t\tOriginal: %s\n" % (original,))
     log.write("\t\tMutant: %s\n" % (mutant,))
     log.write("\t\tDistance: %d\n" % (d,))
     log.write("\t\tNumerical Deviation %f\n" % (deviation,))
     log.write("\t\tTime: %f\n" % round(time.time() - prior_time, 2))
     distance_to_numerical_log.write("%d, %f\n" % (d, deviation))
     trial_distances[trial] = d
     trial_results[trial] = deviation
     print trial,
 print ""
 log.write("\tDistance Mean: %f\n" % np.mean(trial_distances))
 log.write("\tDistance Stdev: %f\n" % np.std(trial_distances))
 log.write("\tDeviation Mean: %f\n" % np.mean(trial_results))
 log.write("\tDeviation Stdev: %f\n" % np.std(trial_results))
Example #22
0
def test_empty_tree_distance():
    assert compare.distance(Node(''), Node('')) == 0
    assert compare.distance(Node('a'), Node('')) == 1
    assert compare.distance(Node(''), Node('b')) == 1
Example #23
0
def test_empty_tree_distance():
    assert compare.distance(Node(''), Node('')) == 0
    assert compare.distance(Node('a'), Node('')) == 1
    assert compare.distance(Node(''), Node('b')) == 1
Example #24
0
def test_incorrect_behavior_regression():
    A = (Node("a").addkid(Node("b").addkid(Node("x")).addkid(Node("y"))))
    B = (Node("a").addkid(Node("x")).addkid(Node("b").addkid(Node("y"))))
    dist = compare.distance(A, B)
    print dist
    assert dist == 2
Example #25
0
def delta_conll(a, b):
    return float(distance(a.tokens[0], b.tokens[0],
                          get_label=lambda t: t.deprel or "",
                          get_children=lambda t: t.children,
                          label_dist=strdist))
Example #26
0
def delta_tree(a, b):
    return float(distance(a, b,
                          get_label=lambda t: t.node,
                          get_children=lambda t: t,
                          label_dist=strdist))