Exemplo n.º 1
0
def get_response_content(fs):
    flags = get_flags(fs)
    nseconds = 5
    tm = time.time()
    rejected_s = None
    nerrors = 0
    nchecked = 0
    while time.time() < tm + nseconds and not rejected_s:
        nchecked += 1
        # Sample a Newick tree.
        true_f = TreeSampler.sample_tree(fs.nleaves, 0, 1.0)
        true_s = NewickIO.get_newick_string(true_f)
        true_tree = Newick.parse(true_s, Newick.NewickTree)
        # Get the leaf and internal vertex ids for the true tree.
        internal_ids = set(id(x) for x in true_tree.gen_internal_nodes())
        leaf_ids = set(id(x) for x in true_tree.gen_tips())
        nleaves = len(leaf_ids)
        # Get the harmonic valuations for all vertices of the tree.
        id_to_full_val_list = [
            Harmonic.get_harmonic_valuations(true_tree, i)
            for i in range(1, nleaves)
        ]
        # Check for small valuations at the leaves.
        try:
            for id_to_full_val in id_to_full_val_list:
                for x in leaf_ids:
                    value = id_to_full_val[x]
                    if abs(value) < 1e-8:
                        raise CheckTreeError('the true tree is too symmetric')
        except CheckTreeError as e:
            nerrors += 1
            continue
        # Assign the leaf values and assign None to internal values.
        id_to_val_list = []
        for id_to_full_val in id_to_full_val_list:
            d = {}
            for x in leaf_ids:
                s = -1 if id_to_full_val[x] < 0 else 1
                d[x] = s
            for x in internal_ids:
                d[x] = None
            id_to_val_list.append(d)
        # Define the topology in a different format.
        id_to_adj = get_id_to_adj(true_tree)
        # Check the tree for self-compatibility under the given conditions.
        id_to_vals = SeekEigenLacing.rec_eigen(id_to_adj, id_to_val_list,
                                               flags)
        if not id_to_vals:
            rejected_s = true_s
    # make the report
    out = StringIO()
    if rejected_s:
        print >> out, 'rejected a true tree:'
        print >> out, rejected_s
    else:
        print >> out, 'no true tree was rejected'
    print >> out
    print >> out, nchecked, 'trees were sampled total'
    print >> out, nerrors, 'trees were too symmetric'
    return out.getvalue()
Exemplo n.º 2
0
def get_response_content(fs):
    flags = get_flags(fs)
    nseconds = 5
    tm = time.time()
    rejected_s = None
    nerrors = 0
    nchecked = 0
    while time.time() < tm + nseconds and not rejected_s:
        nchecked += 1
        # Sample a Newick tree.
        true_f = TreeSampler.sample_tree(fs.nleaves, 0, 1.0)
        true_s = NewickIO.get_newick_string(true_f)
        true_tree = Newick.parse(true_s, Newick.NewickTree)
        # Get the leaf and internal vertex ids for the true tree.
        internal_ids = set(id(x) for x in true_tree.gen_internal_nodes())
        leaf_ids = set(id(x) for x in true_tree.gen_tips())
        nleaves = len(leaf_ids)
        # Get the harmonic valuations for all vertices of the tree.
        id_to_full_val_list = [Harmonic.get_harmonic_valuations(
            true_tree, i) for i in range(1, nleaves)]
        # Check for small valuations at the leaves.
        try:
            for id_to_full_val in id_to_full_val_list:
                for x in leaf_ids:
                    value = id_to_full_val[x]
                    if abs(value) < 1e-8:
                        raise CheckTreeError('the true tree is too symmetric')
        except CheckTreeError as e:
            nerrors += 1
            continue
        # Assign the leaf values and assign None to internal values.
        id_to_val_list = []
        for id_to_full_val in id_to_full_val_list:
            d = {}
            for x in leaf_ids:
                s = -1 if id_to_full_val[x] < 0 else 1
                d[x] = s
            for x in internal_ids:
                d[x] = None
            id_to_val_list.append(d)
        # Define the topology in a different format.
        id_to_adj = get_id_to_adj(true_tree)
        # Check the tree for self-compatibility under the given conditions.
        id_to_vals = SeekEigenLacing.rec_eigen(
                id_to_adj, id_to_val_list, flags)
        if not id_to_vals:
            rejected_s = true_s
    # make the report
    out = StringIO()
    if rejected_s:
        print >> out, 'rejected a true tree:'
        print >> out, rejected_s
    else:
        print >> out, 'no true tree was rejected'
    print >> out
    print >> out, nchecked, 'trees were sampled total'
    print >> out, nerrors, 'trees were too symmetric'
    return out.getvalue()
Exemplo n.º 3
0
def check_tree_pair(true_tree, test_tree, data):
    """
    Do this for every pair of sampled trees.
    The returned array has three zeros and a one.
    The position of the one shows
    whether the test tree was accepted or rejected
    with respect to the true tree
    for the pair of methods.
    @param true_tree: a true Newick tree
    @param test_tree: a test Newick tree
    @param data: keeps some running data about the search
    """
    # Get a list of maps from node id to harmonic extension.
    true_tree_leaf_ids = set(id(x) for x in true_tree.gen_tips())
    nleaves = len(true_tree_leaf_ids)
    id_to_full_val_list = [
        Harmonic.get_harmonic_valuations(true_tree, i)
        for i in range(1, nleaves)
    ]
    id_map = get_true_leaf_id_to_test_leaf_id(true_tree, test_tree)
    test_id_to_adj = get_id_to_adj(test_tree)
    test_id_to_name = get_id_to_name(test_tree)
    # Get the list of id to val maps with respect to leaf ids of the test tree.
    test_tree_internal_ids = set(id(x) for x in test_tree.gen_internal_nodes())
    test_tree_leaf_ids = set(id(x) for x in test_tree.gen_tips())
    # Fill out the acceptance pair.
    acceptance_pair = []
    for i, flags in enumerate((data.flags_a, data.flags_b)):
        id_to_val_list = []
        for id_to_full_val in id_to_full_val_list:
            d = {}
            for x in true_tree_leaf_ids:
                value = id_to_full_val[x]
                if abs(value) < 1e-8:
                    raise CheckTreeError('the true tree is too symmetric')
                elif value < 0:
                    s = -1
                else:
                    s = 1
                d[id_map[x]] = s
            for x in test_tree_internal_ids:
                d[x] = None
            id_to_val_list.append(d)
        id_to_vals = SeekEigenLacing.rec_eigen(test_id_to_adj, id_to_val_list,
                                               flags)
        acceptance_pair.append(1 if id_to_vals else 0)
    data.add_acceptances(acceptance_pair)
    found_example = acceptance_pair[0] != acceptance_pair[1]
    if found_example and (data.report is None):
        s = StringIO()
        print >> s, 'true tree:'
        print >> s, true_tree.get_newick_string()
        print >> s
        print >> s, 'test tree:'
        print >> s, test_tree.get_newick_string()
        data.report = s.getvalue()
    return found_example
Exemplo n.º 4
0
def check_tree_pair(true_tree, test_tree, data):
    """
    Do this for every pair of sampled trees.
    The returned array has three zeros and a one.
    The position of the one shows
    whether the test tree was accepted or rejected
    with respect to the true tree
    for the pair of methods.
    @param true_tree: a true Newick tree
    @param test_tree: a test Newick tree
    @param data: keeps some running data about the search
    """
    # Get a list of maps from node id to harmonic extension.
    true_tree_leaf_ids = set(id(x) for x in true_tree.gen_tips())
    nleaves = len(true_tree_leaf_ids)
    id_to_full_val_list = [Harmonic.get_harmonic_valuations(
        true_tree, i) for i in range(1, nleaves)]
    id_map =  get_true_leaf_id_to_test_leaf_id(true_tree, test_tree)
    test_id_to_adj = get_id_to_adj(test_tree)
    test_id_to_name = get_id_to_name(test_tree)
    # Get the list of id to val maps with respect to leaf ids of the test tree.
    test_tree_internal_ids = set(id(x) for x in test_tree.gen_internal_nodes())
    test_tree_leaf_ids = set(id(x) for x in test_tree.gen_tips())
    # Fill out the acceptance pair.
    acceptance_pair = []
    for i, flags in enumerate((data.flags_a, data.flags_b)):
        id_to_val_list = []
        for id_to_full_val in id_to_full_val_list:
            d = {}
            for x in true_tree_leaf_ids:
                value = id_to_full_val[x]
                if abs(value) < 1e-8:
                    raise CheckTreeError('the true tree is too symmetric')
                elif value < 0:
                    s = -1
                else:
                    s = 1
                d[id_map[x]] = s
            for x in test_tree_internal_ids:
                d[x] = None
            id_to_val_list.append(d)
        id_to_vals = SeekEigenLacing.rec_eigen(
                test_id_to_adj, id_to_val_list, flags)
        acceptance_pair.append(1 if id_to_vals else 0)
    data.add_acceptances(acceptance_pair)
    found_example = acceptance_pair[0] != acceptance_pair[1]
    if found_example and (data.report is None):
        s = StringIO()
        print >> s, 'true tree:'
        print >> s, true_tree.get_newick_string()
        print >> s
        print >> s, 'test tree:'
        print >> s, test_tree.get_newick_string()
        data.report = s.getvalue()
    return found_example
Exemplo n.º 5
0
def get_response_content(fs):
    # Read the newick trees.
    true_tree = Newick.parse(fs.true_tree, Newick.NewickTree)
    test_tree = Newick.parse(fs.test_tree, Newick.NewickTree)
    flags = get_flags(fs)
    # Get a list of maps from node id to harmonic extension.
    true_tree_leaf_ids = set(id(x) for x in true_tree.gen_tips())
    nleaves = len(true_tree_leaf_ids)
    id_to_full_val_list = [
        Harmonic.get_harmonic_valuations(true_tree, i)
        for i in range(1, nleaves)
    ]
    id_map = get_true_leaf_id_to_test_leaf_id(true_tree, test_tree)
    test_id_to_adj = get_id_to_adj(test_tree)
    test_id_to_name = get_id_to_name(test_tree)
    # Get the list of id to val maps with respect to leaf ids of the test tree.
    test_tree_internal_ids = set(id(x) for x in test_tree.gen_internal_nodes())
    test_tree_leaf_ids = set(id(x) for x in test_tree.gen_tips())
    id_to_val_list = []
    for id_to_full_val in id_to_full_val_list:
        d = {}
        for x in true_tree_leaf_ids:
            value = id_to_full_val[x]
            if abs(value) < 1e-8:
                raise ValueError('the true tree is too symmetric')
            elif value < 0:
                s = -1
            else:
                s = 1
            d[id_map[x]] = s
        for x in test_tree_internal_ids:
            d[x] = None
        id_to_val_list.append(d)
    # Attempt to find a sign assignment.
    id_to_vals = SeekEigenLacing.rec_eigen(test_id_to_adj, id_to_val_list,
                                           flags)
    # Reorder the leaf and the internal node ids according to name order.
    leaf_pair = sorted((test_id_to_name[x], x) for x in test_tree_leaf_ids)
    internal_pair = sorted(
        (test_id_to_name[x], x) for x in test_tree_internal_ids)
    reordered_leaf_ids = zip(*leaf_pair)[1]
    reordered_internal_ids = zip(*internal_pair)[1]
    # Check for a failure to find a certificate.
    if not id_to_vals:
        return 'no non-rejection certificate was found'
    # Start writing the response.
    out = StringIO()
    print >> out, 'leaf sign valuations:'
    for x in reordered_leaf_ids:
        print >> out, test_id_to_name[x], get_sign_string(id_to_vals[x])
    print >> out
    print >> out, 'vertex sign compatible internal vertex valuations:'
    for x in reordered_internal_ids:
        print >> out, test_id_to_name[x], get_sign_string(id_to_vals[x])
    return out.getvalue()
Exemplo n.º 6
0
def get_response_content(fs):
    # Read the newick trees.
    true_tree = Newick.parse(fs.true_tree, Newick.NewickTree)
    test_tree = Newick.parse(fs.test_tree, Newick.NewickTree)
    flags = get_flags(fs)
    # Get a list of maps from node id to harmonic extension.
    true_tree_leaf_ids = set(id(x) for x in true_tree.gen_tips())
    nleaves = len(true_tree_leaf_ids)
    id_to_full_val_list = [Harmonic.get_harmonic_valuations(
        true_tree, i) for i in range(1, nleaves)]
    id_map =  get_true_leaf_id_to_test_leaf_id(true_tree, test_tree)
    test_id_to_adj = get_id_to_adj(test_tree)
    test_id_to_name = get_id_to_name(test_tree)
    # Get the list of id to val maps with respect to leaf ids of the test tree.
    test_tree_internal_ids = set(id(x) for x in test_tree.gen_internal_nodes())
    test_tree_leaf_ids = set(id(x) for x in test_tree.gen_tips())
    id_to_val_list = []
    for id_to_full_val in id_to_full_val_list:
        d = {}
        for x in true_tree_leaf_ids:
            value = id_to_full_val[x]
            if abs(value) < 1e-8:
                raise ValueError('the true tree is too symmetric')
            elif value < 0:
                s = -1
            else:
                s = 1
            d[id_map[x]] = s
        for x in test_tree_internal_ids:
            d[x] = None
        id_to_val_list.append(d)
    # Attempt to find a sign assignment.
    id_to_vals = SeekEigenLacing.rec_eigen(
            test_id_to_adj, id_to_val_list, flags)
    # Reorder the leaf and the internal node ids according to name order.
    leaf_pair = sorted(
            (test_id_to_name[x], x) for x in test_tree_leaf_ids)
    internal_pair = sorted(
            (test_id_to_name[x], x) for x in test_tree_internal_ids)
    reordered_leaf_ids = zip(*leaf_pair)[1]
    reordered_internal_ids = zip(*internal_pair)[1]
    # Check for a failure to find a certificate.
    if not id_to_vals:
        return 'no non-rejection certificate was found'
    # Start writing the response.
    out = StringIO()
    print >> out, 'leaf sign valuations:'
    for x in reordered_leaf_ids:
        print >> out, test_id_to_name[x], get_sign_string(id_to_vals[x])
    print >> out
    print >> out, 'vertex sign compatible internal vertex valuations:'
    for x in reordered_internal_ids:
        print >> out, test_id_to_name[x], get_sign_string(id_to_vals[x])
    return out.getvalue()