-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathACRParameterOptimisationMPPAJCTest.py
executable file
·153 lines (133 loc) · 7.97 KB
/
ACRParameterOptimisationMPPAJCTest.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
import unittest
import numpy as np
import pandas as pd
from pastml.models import SCALING_FACTOR
from pastml.models.JCModel import JC
from pastml.tree import read_tree
from pastml import get_personalized_feature_name, STATES
from pastml.acr import acr
from pastml.ml import LH, LH_SF, MPPA, LOG_LIKELIHOOD, RESTRICTED_LOG_LIKELIHOOD_FORMAT_STR, MARGINAL_PROBABILITIES, \
MODEL
DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
TREE_NWK = os.path.join(DATA_DIR, 'Albanian.tree.152tax.tre')
STATES_INPUT = os.path.join(DATA_DIR, 'data.txt')
def reroot_tree_randomly():
rerooted_tree = read_tree(TREE_NWK)
new_root = np.random.choice([_ for _ in rerooted_tree.traverse()
if not _.is_root() and not _.up.is_root() and _.dist])
old_root_child = rerooted_tree.children[0]
old_root_child_dist = old_root_child.dist
other_children = list(rerooted_tree.children[1:])
old_root_child.up = None
for child in other_children:
child.up = None
old_root_child.add_child(child, dist=old_root_child_dist + child.dist)
old_root_child.set_outgroup(new_root)
new_root = new_root.up
for _ in new_root.traverse():
if not _.name:
_.name = 'unknown'
return new_root
feature = 'Country'
df = pd.read_csv(STATES_INPUT, index_col=0, header=0)[[feature]]
tree = read_tree(TREE_NWK)
acr_result = acr(tree, df, prediction_method=MPPA, model=JC)[0]
class ACRParameterOptimisationMPPAJCTest(unittest.TestCase):
def test_rerooted_values_are_the_same(self):
for _ in range(5):
rerooted_tree = reroot_tree_randomly()
rerooted_acr_result = acr(rerooted_tree, df, prediction_method=MPPA, model=JC)[0]
for (state, freq, refreq) in zip(acr_result[STATES], acr_result[MODEL].frequencies,
rerooted_acr_result[MODEL].frequencies):
self.assertAlmostEqual(freq, refreq, places=2,
msg='Frequency of {} for the original tree and rerooted tree '
'were supposed to be the same, '
'got {:.3f} vs {:3f}'
.format(state, freq, refreq))
value = acr_result[LOG_LIKELIHOOD]
rerooted_value = rerooted_acr_result[LOG_LIKELIHOOD]
self.assertAlmostEqual(value, rerooted_value, places=2,
msg='{} for the original tree and rerooted tree were supposed to be the same, '
'got {:.3f} vs {:3f}'
.format(LOG_LIKELIHOOD, value, rerooted_value))
value = acr_result[MODEL].sf
rerooted_value = rerooted_acr_result[MODEL].sf
self.assertAlmostEqual(value, rerooted_value, places=2,
msg='{} for the original tree and rerooted tree were supposed to be the same, '
'got {:.3f} vs {:3f}'
.format(SCALING_FACTOR, value, rerooted_value))
mps = acr_result[MARGINAL_PROBABILITIES]
remps = rerooted_acr_result[MARGINAL_PROBABILITIES]
for node_name in ('node_4', '02ALAY1660'):
for loc in acr_result[STATES]:
value = mps.loc[node_name, loc]
revalue = remps.loc[node_name, loc]
self.assertAlmostEqual(value, revalue, places=2,
msg='{}: Marginal probability of {} for the original tree and rerooted tree '
'were supposed to be the same, got {:.3f} vs {:3f}'
.format(node_name, loc, value, revalue))
def test_likelihood(self):
self.assertAlmostEqual(-121.873, acr_result[LOG_LIKELIHOOD], places=3,
msg='Likelihood was supposed to be the {:.3f}, got {:3f}'
.format(-121.873, acr_result[LOG_LIKELIHOOD]))
def test_restricted_likelihood(self):
self.assertAlmostEqual(-123.421, acr_result[RESTRICTED_LOG_LIKELIHOOD_FORMAT_STR.format(MPPA)], places=3,
msg='Restricted likelihood was supposed to be the {:.3f}, got {:3f}'
.format(-123.421, acr_result[RESTRICTED_LOG_LIKELIHOOD_FORMAT_STR.format(MPPA)]))
def test_sf(self):
self.assertAlmostEqual(4.951, acr_result[MODEL].sf, places=3,
msg='Scaling factor was supposed to be the {:.3f}, got {:3f}'
.format(4.951, acr_result[MODEL].sf))
def test_frequencies(self):
value = acr_result[MODEL].frequencies
expected_value = np.ones(len(value), np.float64) / len(value)
self.assertListEqual(value.tolist(), expected_value.tolist(),
msg='Frequencies were supposed to be the {}, got {}'.format(expected_value, value))
def test_frequencies_sum_to_1(self):
value = acr_result[MODEL].frequencies.sum()
self.assertAlmostEqual(value, 1, places=3,
msg='Frequencies were supposed to sum to 1, not to {:3f}'.format(value))
def test_likelihood_same_for_all_nodes(self):
"""
Tests if marginal likelihoods were correctly calculated
by comparing the likelihoods of all the nodes (should be all the same).
"""
lh_feature = get_personalized_feature_name(feature, LH)
lh_sf_feature = get_personalized_feature_name(feature, LH_SF)
for node in tree.traverse():
if not node.is_root() and not (node.is_leaf() and node.dist == 0):
node_loglh = np.log10(getattr(node, lh_feature).sum()) - getattr(node, lh_sf_feature)
parent_loglh = np.log10(getattr(node.up, lh_feature).sum()) - getattr(node.up, lh_sf_feature)
self.assertAlmostEqual(node_loglh, parent_loglh, places=2,
msg='Likelihoods of {} and {} were supposed to be the same.'
.format(node.name, node.up.name))
def test_marginal_probs_root(self):
expected_values = {'Africa': 0.819, 'Albania': 0.020, 'EastEurope': 0.045,
'Greece': 0.020, 'WestEurope': 0.095}
node_name = 'ROOT'
mps = acr_result[MARGINAL_PROBABILITIES]
for loc, expected_value in expected_values.items():
value = mps.loc[node_name, loc]
self.assertAlmostEqual(value, expected_value, places=3,
msg='{}: Marginal probability of {} was supposed to be the {:.3f}, got {:3f}'
.format(node_name, loc, expected_value, value))
def test_marginal_probs_internal_node(self):
expected_values = {'Africa': 0.686, 'Albania': 0.002, 'EastEurope': 0.003,
'Greece': 0.002, 'WestEurope': 0.307}
node_name = 'node_4'
mps = acr_result[MARGINAL_PROBABILITIES]
for loc, expected_value in expected_values.items():
value = mps.loc[node_name, loc]
self.assertAlmostEqual(value, expected_value, places=3,
msg='{}: Marginal probability of {} was supposed to be the {:.3f}, got {:3f}'
.format(node_name, loc, expected_value, value))
def test_marginal_probs_tip(self):
expected_values = {'Africa': 0, 'Albania': 1, 'EastEurope': 0, 'Greece': 0, 'WestEurope': 0}
node_name = '02ALAY1660'
mps = acr_result[MARGINAL_PROBABILITIES]
for loc, expected_value in expected_values.items():
value = mps.loc[node_name, loc]
self.assertAlmostEqual(value, expected_value, places=3,
msg='{}: Marginal probability of {} was supposed to be the {:.3f}, got {:3f}'
.format(node_name, loc, expected_value, value))