-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_for_paper.py
71 lines (58 loc) · 2.64 KB
/
test_for_paper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
__author__ = 'Lena Collienne'
# Functions used for ranked SPR paper
import sys
from rankedSPR_seidel import *
from rankedspr_exploration import *
from spr_path_functions import *
def get_diameter(num_leaves):
d = get_distance_matrix(num_leaves, hspr=True)
print("The diameter of HSPR space for", num_leaves, "leaves is", np.amax(d[0]))
def check_approx_alg(num_leaves):
(d, tree_dict, tree_index_dict) = get_distance_matrix(num_leaves,
hspr=True)
for i in range(0, len(d)):
tree1 = read_from_cluster(tree_index_dict[i])
for j in range(i, len(d)):
tree2 = read_from_cluster(tree_index_dict[j])
approx_path = rankedspr_path_bottom_up_hspr(tree1, tree2)
approx_dist = approx_path.num_trees - 1
if approx_dist != d[i][j]:
print("For the trees", tree_index_dict[i], "and", tree_index_dict[j],
"the approximated HSPR distance is", approx_dist)
print("The exact distance is", d[i][j])
return(False)
return(True)
def check_cluster_property(tree1_str, tree2_str, cluster):
# check if any tree in the predecessor dictionary (other than tree1 and tree2)
# contains the cluster {1,2,3}
tree1 = read_from_cluster(tree1_str)
tree2 = read_from_cluster(tree2_str)
# dictionary containing predecessors of tree2 for all paths from tree1 to tree2
pred_dict = all_shortest_paths(tree1, tree2)
cluster_found = False
for tree_str in pred_dict:
if tree_str != tree1_str and tree_str != tree2_str and cluster in tree_str:
print(tree_str + " contains the cluster " + cluster)
cluster_found = True
for pred_str in pred_dict[tree_str]:
if pred_str != tree1_str and pred_str != tree2_str and cluster in pred_str:
print(tree_str + " contains the cluster " + cluster)
cluster_found = True
if cluster_found:
break
if cluster_found:
print("The shared clusters " + cluster + " is present in a tree on a shortes path between "
+ tree1_str + " and " + tree2_str)
else:
print("The shared clusters " + cluster + " is not present in a tree on a shortes path between "
+ tree1_str + " and " + tree2_str)
def main():
num_leaves = 4
get_diameter(num_leaves)
check_approx_alg(num_leaves)
tree1_str = "[{4,5}:1,{1,2}:2,{1,2,3}:3,{1,2,3,4,5}:4]"
tree2_str = "[{1,2}:1,{4,5}:2,{1,2,3}:3,{1,2,3,4,5}:4]"
cluster = "{1,2,3}"
check_cluster_property(tree1_str, tree2_str, cluster)
if __name__ == "__main__":
main()