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()
def gen_trees(): n_base_leaves = 4 n_expected_extra_leaves = 1 expected_branch_length = 1 while True: yield TreeSampler.sample_tree(n_base_leaves, n_expected_extra_leaves, expected_branch_length)
def gen_trees(): n_base_leaves = 4 n_expected_extra_leaves = 1 expected_branch_length = 1 while True: yield TreeSampler.sample_tree( n_base_leaves, n_expected_extra_leaves, expected_branch_length)
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()
def main(): filename = 'counterexamples.out' fout = open(filename, 'wt') print 'Does monotonically transforming the pairwise leaf distances affect the compatibility' print 'of the split found using principal coordinate analysis?' print 'I am looking through random trees for a tree that is split incompatibly' print 'when distances are squared.' print 'Use control-c to stop the program when you get bored.' try: count = 0 ncounterexamples = 0 nerrors = 0 while True: count += 1 # get a random tree n_base_leaves = 4 n_expected_extra_leaves = 1 expected_branch_length = 1 tree = TreeSampler.sample_tree(n_base_leaves, n_expected_extra_leaves, expected_branch_length) # get the set of valid partitions implied by the tree valid_parts = TreeComparison.get_partitions(tree) ordered_tip_names = [tip.get_name() for tip in tree.gen_tips()] # assert that the partition implied by the correct formula is valid D = np.array(tree.get_distance_matrix(ordered_tip_names)) loadings = get_principal_coordinate(D) nonneg_leaf_set = frozenset( tip for tip, v in zip(ordered_tip_names, loadings) if v >= 0) neg_leaf_set = frozenset( tip for tip, v in zip(ordered_tip_names, loadings) if v < 0) part = frozenset([nonneg_leaf_set, neg_leaf_set]) if part not in valid_parts: nerrors += 1 print >> fout, 'error: a partition that was supposed to be valid was found to be invalid' print >> fout, 'tree:', NewickIO.get_newick_string(tree) print >> fout, 'invalid partition:', partition_to_string(part) print >> fout # check the validity of the partition implied by the incorrect formula Q = D * D loadings = get_principal_coordinate(Q) nonneg_leaf_set = frozenset( tip for tip, v in zip(ordered_tip_names, loadings) if v >= 0) neg_leaf_set = frozenset( tip for tip, v in zip(ordered_tip_names, loadings) if v < 0) part = frozenset([nonneg_leaf_set, neg_leaf_set]) if part not in valid_parts: ncounterexamples += 1 print >> fout, 'found a counterexample!' print >> fout, 'tree:', NewickIO.get_newick_string(tree) print >> fout, 'invalid partition:', partition_to_string(part) print >> fout except KeyboardInterrupt, e: print 'trees examined:', count print 'errors:', nerrors print 'counterexamples:', ncounterexamples
def get_response_content(fs): flags_a = get_flags_a(fs) flags_b = get_flags_b(fs) data = CheckTreeData(flags_a, flags_b) nseconds = 5 tm = time.time() while time.time() < tm + nseconds: # Sample a pair of Newick trees. true_f = TreeSampler.sample_tree(fs.nleaves, 0, 1.0) test_f = TreeSampler.sample_tree(fs.nleaves, 0, 1.0) true_s = NewickIO.get_newick_string(true_f) test_s = NewickIO.get_newick_string(test_f) true_tree = Newick.parse(true_s, Newick.NewickTree) test_tree = Newick.parse(test_s, Newick.NewickTree) # Add the pairwise check to the data borg. try: found_difference = check_tree_pair(true_tree, test_tree, data) except CheckTreeError as e: data.add_error(e) # Check to see if we should stop early. if found_difference and fs.halt_on_difference: break # make the report out = StringIO() if data.report: print >> out, 'found a difference in rejection power' print >> out print >> out, data.report print >> out else: print >> out, 'failed to find a difference in rejection power' print >> out print >> out, 'search summary:' m = data.acceptance_matrix print >> out, 'A reject, B reject:', m[0, 0] print >> out, 'A reject, B accept:', m[0, 1] print >> out, 'A accept, B reject:', m[1, 0] print >> out, 'A accept, B accept:', m[1, 1] print >> out, data.nerrors, 'tree symmetry errors' return out.getvalue()
def get_response_content(fs): """ @param fs: a FieldStorage object containing the cgi arguments @return: a (response_headers, response_text) pair """ out = StringIO() # get some samples for i in range(fs.ntrees): tree = TreeSampler.sample_tree(fs.leafbase, fs.leafmean, fs.branchmean) # write the tree print >> out, NewickIO.get_newick_string(tree) # return the response return out.getvalue()
def main(): filename = 'counterexamples.out' fout = open(filename, 'wt') print 'Does monotonically transforming the pairwise leaf distances affect the compatibility' print 'of the split found using principal coordinate analysis?' print 'I am looking through random trees for a tree that is split incompatibly' print 'when distances are squared.' print 'Use control-c to stop the program when you get bored.' try: count = 0 ncounterexamples = 0 nerrors = 0 while True: count += 1 # get a random tree n_base_leaves = 4 n_expected_extra_leaves = 1 expected_branch_length = 1 tree = TreeSampler.sample_tree(n_base_leaves, n_expected_extra_leaves, expected_branch_length) # get the set of valid partitions implied by the tree valid_parts = TreeComparison.get_partitions(tree) ordered_tip_names = [tip.get_name() for tip in tree.gen_tips()] # assert that the partition implied by the correct formula is valid D = np.array(tree.get_distance_matrix(ordered_tip_names)) loadings = get_principal_coordinate(D) nonneg_leaf_set = frozenset(tip for tip, v in zip(ordered_tip_names, loadings) if v >= 0) neg_leaf_set = frozenset(tip for tip, v in zip(ordered_tip_names, loadings) if v < 0) part = frozenset([nonneg_leaf_set, neg_leaf_set]) if part not in valid_parts: nerrors += 1 print >> fout, 'error: a partition that was supposed to be valid was found to be invalid' print >> fout, 'tree:', NewickIO.get_newick_string(tree) print >> fout, 'invalid partition:', partition_to_string(part) print >> fout # check the validity of the partition implied by the incorrect formula Q = D * D loadings = get_principal_coordinate(Q) nonneg_leaf_set = frozenset(tip for tip, v in zip(ordered_tip_names, loadings) if v >= 0) neg_leaf_set = frozenset(tip for tip, v in zip(ordered_tip_names, loadings) if v < 0) part = frozenset([nonneg_leaf_set, neg_leaf_set]) if part not in valid_parts: ncounterexamples += 1 print >> fout, 'found a counterexample!' print >> fout, 'tree:', NewickIO.get_newick_string(tree) print >> fout, 'invalid partition:', partition_to_string(part) print >> fout except KeyboardInterrupt, e: print 'trees examined:', count print 'errors:', nerrors print 'counterexamples:', ncounterexamples