예제 #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()
예제 #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()
예제 #3
0
파일: 20110404a.py 프로젝트: BIGtigr/xgcode
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
예제 #4
0
def get_response_content(fs):
    # get a properly formatted newick tree with branch lengths
    tree = Newick.parse(fs.tree, SpatialTree.SpatialTree)
    # get the vertex valuations
    id_to_v1 = Harmonic.get_harmonic_valuations(tree, fs.eig_idx1)
    id_to_v2 = Harmonic.get_harmonic_valuations(tree, fs.eig_idx2)
    # do the layout
    try:
        layout = FastDaylightLayout.StraightBranchLayout()
        layout.do_layout(tree)
    except RuntimeError as e:
        pass
    # draw the image
    try:
        ext = Form.g_imageformat_to_ext[fs.imageformat]
        return DrawEigenLacing.get_single_tree_image(
                tree, (640, 480), ext, id_to_v1, id_to_v2)
    except CairoUtil.CairoUtilError as e:
        raise HandlingError(e)
예제 #5
0
파일: 20110324a.py 프로젝트: BIGtigr/xgcode
def get_response_content(fs):
    # get a properly formatted newick tree with branch lengths
    tree = Newick.parse(fs.tree, SpatialTree.SpatialTree)
    # get the vertex valuations
    id_to_v1 = Harmonic.get_harmonic_valuations(tree, fs.eig_idx1)
    id_to_v2 = Harmonic.get_harmonic_valuations(tree, fs.eig_idx2)
    # do the layout
    try:
        layout = FastDaylightLayout.StraightBranchLayout()
        layout.do_layout(tree)
    except RuntimeError as e:
        pass
    # draw the image
    try:
        ext = Form.g_imageformat_to_ext[fs.imageformat]
        return DrawEigenLacing.get_single_tree_image(tree, (640, 480), ext,
                                                     id_to_v1, id_to_v2)
    except CairoUtil.CairoUtilError as e:
        raise HandlingError(e)
예제 #6
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
예제 #7
0
파일: 20110331a.py 프로젝트: BIGtigr/xgcode
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()
예제 #8
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()
예제 #9
0
def get_response_content(fs):
    # get a properly formatted newick tree with branch lengths
    tree = Newick.parse(fs.tree, SpatialTree.SpatialTree)
    # get the vertex valuations
    valuations = [Harmonic.get_harmonic_valuations(
        tree, i) for i in range(fs.first_index, fs.last_index+1)]
    # do the layout
    try:
        layout = FastDaylightLayout.StraightBranchLayout()
        layout.do_layout(tree)
    except RuntimeError as e:
        pass
    # draw the image
    try:
        ext = Form.g_imageformat_to_ext[fs.imageformat]
        return DrawEigenLacing.get_forest_image(
                tree, (640, 480), ext, valuations,
                fs.draw_background, fs.draw_vertices, fs.draw_labels)
    except CairoUtil.CairoUtilError as e:
        raise HandlingError(e)
예제 #10
0
def get_response_content(fs):
    # read the tree
    tree = Newick.parse(fs.tree, Newick.NewickTree)
    # get ids
    leaf_ids = [id(x) for x in tree.gen_tips()]
    internal_ids = [id(x) for x in tree.gen_internal_nodes()]
    nleaves = len(leaf_ids)
    ninternal = len(internal_ids)
    # get map from id to name
    id_to_name = get_id_to_name(tree)
    # Reorder the leaf and the internal node ids according to name order.
    leaf_pair = sorted(
            (id_to_name[x], x) for x in leaf_ids)
    internal_pair = sorted(
            (id_to_name[x], x) for x in internal_ids)
    leaf_ids = zip(*leaf_pair)[1]
    internal_ids = zip(*internal_pair)[1]
    # compute the harmonic extensions
    id_to_val_list = []
    eigenvalues = []
    for i in range(nleaves-1):
        index = i+1
        w, d = Harmonic.get_eigenvalue_and_harmonic_valuations(tree, index)
        id_to_val_list.append(d)
        eigenvalues.append(w)
    # write the report
    out = StringIO()
    print >> out, 'valuations:'
    print >> out
    for i, (w, id_to_val) in enumerate(zip(eigenvalues, id_to_val_list)):
        index = i+1
        print >> out, index, '(1 is Fiedler)'
        print >> out, 'eigenvalue:', w
        print >> out, 'leaves:'
        for x in leaf_ids:
            print >> out, id_to_name[x], id_to_val[x]
        print >> out, 'internal vertices:'
        for x in internal_ids:
            print >> out, id_to_name[x], id_to_val[x]
        print >> out
    return out.getvalue()
예제 #11
0
파일: 20110405a.py 프로젝트: BIGtigr/xgcode
def get_response_content(fs):
    # read the tree
    tree = Newick.parse(fs.tree, Newick.NewickTree)
    # get ids
    leaf_ids = [id(x) for x in tree.gen_tips()]
    internal_ids = [id(x) for x in tree.gen_internal_nodes()]
    nleaves = len(leaf_ids)
    ninternal = len(internal_ids)
    # get map from id to name
    id_to_name = get_id_to_name(tree)
    # Reorder the leaf and the internal node ids according to name order.
    leaf_pair = sorted((id_to_name[x], x) for x in leaf_ids)
    internal_pair = sorted((id_to_name[x], x) for x in internal_ids)
    leaf_ids = zip(*leaf_pair)[1]
    internal_ids = zip(*internal_pair)[1]
    # compute the harmonic extensions
    id_to_val_list = []
    eigenvalues = []
    for i in range(nleaves - 1):
        index = i + 1
        w, d = Harmonic.get_eigenvalue_and_harmonic_valuations(tree, index)
        id_to_val_list.append(d)
        eigenvalues.append(w)
    # write the report
    out = StringIO()
    print >> out, 'valuations:'
    print >> out
    for i, (w, id_to_val) in enumerate(zip(eigenvalues, id_to_val_list)):
        index = i + 1
        print >> out, index, '(1 is Fiedler)'
        print >> out, 'eigenvalue:', w
        print >> out, 'leaves:'
        for x in leaf_ids:
            print >> out, id_to_name[x], id_to_val[x]
        print >> out, 'internal vertices:'
        for x in internal_ids:
            print >> out, id_to_name[x], id_to_val[x]
        print >> out
    return out.getvalue()
예제 #12
0
def get_response_content(fs):
    # get a properly formatted newick tree with branch lengths
    tree = Newick.parse(fs.tree, SpatialTree.SpatialTree)
    # get the vertex valuations
    valuations = [
        Harmonic.get_harmonic_valuations(tree, i)
        for i in range(fs.first_index, fs.last_index + 1)
    ]
    # do the layout
    try:
        layout = FastDaylightLayout.StraightBranchLayout()
        layout.do_layout(tree)
    except RuntimeError as e:
        pass
    # draw the image
    try:
        ext = Form.g_imageformat_to_ext[fs.imageformat]
        physical_size = (fs.width, fs.height)
        return DrawEigenLacing.get_forest_image_revised(
            tree, physical_size, ext, valuations, fs.draw_background,
            fs.draw_labels, fs.inner_margin, fs.outer_margin, fs.reflect_trees)
    except CairoUtil.CairoUtilError as e:
        raise HandlingError(e)