-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
84 lines (73 loc) · 2.89 KB
/
analysis.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
import pickle
import pandas as pd
def load_experiments(pickle_fn):
"""
Load & return the pickle output of experiment.py.
"""
with open(pickle_fn, 'rb') as f:
experiments = pickle.load(f)
return experiments
def tabulate_results(experiments):
"""
Build and return a pandas DataFrame with one row for each Experiment
performed.
"""
rows = [e.summarise_results() for e in experiments]
df = pd.DataFrame(rows)
return df
def extract_constants(df):
"""
Return a pandas Series indexed by the constant-value columns of the
supplied DataFrame `df`, giving the constant value in each case.
"""
constant_cols = []
for col in df.columns:
try:
if len(set(df[col])) == 1:
constant_cols.append(col)
except TypeError:
continue # some column values are not hashable
return df.iloc[0][constant_cols]
def _col_to_name(col):
name = col.split('_')[0].replace('nj', 'NJ')
name = name[0].upper() + name[1:]
return name
def mean_normalised_rf_to_optimum(df, gbcol):
"""
Return the mean normalised (i.e. in range [0,1]) Robinson-Foulds distance
to the tree returned by ML tree search, grouped by `gbcol`.
"""
cols = [col for col in df.columns if col.split('_')[-1] == 'rf-to-optimum']
max_rf = 2 * (df.num_leaves - 3)
nrf = df[cols].div(max_rf, axis=0).groupby(df[gbcol]).mean()
return nrf.rename(mapper=_col_to_name, axis=1)
def mean_rf_to_generating_report(df, gbcol):
"""
Return the mean RF distance between the inferred tree and the generating
tree, grouped by `gbcol`.
"""
cols = [col for col in df.columns if col.split('_')[-1] == 'rf-to-generating']
return df[cols + [gbcol]].groupby(gbcol).mean().rename(mapper=_col_to_name, axis=1)
def _mean_topological_accuracy(rfs):
"""
Return the rate at which the supplied Series (presumed to represent
Robinson-Foulds distances) is equal to zero.
"""
return (rfs == 0).mean()
def topological_accuracy_report(df, gbcol):
"""
Return the mean rate of coincidence of the inferred tree and the generating
tree, grouped by `gbcol`.
"""
cols = [col for col in df.columns if col.split('_')[-1] == 'rf-to-generating']
return df[cols + [gbcol]].groupby(gbcol).aggregate(_mean_topological_accuracy).rename(mapper=_col_to_name, axis=1)
def likelihood_report(df, gbcol, tolerance=0):
"""
Return the rate at which the likelihood of the inferred tree exceeds the
likelihood of the generating tree, grouped by `gbcol`.
"""
cols = [col for col in df.columns if col.split('_')[-1] == 'll' and col != 'generating_tree_ll']
comparisons = pd.DataFrame({col: df[col] >= (df['generating_tree_ll'] - tolerance) for col in cols}) * 1.
comparisons[gbcol] = df[gbcol]
rates = comparisons.groupby(gbcol).mean()
return rates[cols].rename(mapper=_col_to_name, axis=1)