Beispiel #1
0
def iterate(n_iters):
    for i in tqdm(xrange(n_iters)):
        sampler.sample()
        likelihoods.append(sampler.tree.marg_log_likelihood())

    plt.figure()
    plt.xlabel("Iterations", fontsize=fontsize)
    plt.ylabel("Data Log Likelihood", fontsize=fontsize)
    plt.plot(likelihoods)
    plt.legend(loc='best', fontsize=12)

    plt.savefig('unconstrained-likelihoods.png', bbox_inches='tight')


    final_tree = sampler.tree.copy()

    plt.figure()
    plot_tree_2d(final_tree, X, pca)

    for node in final_tree.dfs():
        if node.is_leaf():
            node.point = y[node.point]

    plt.figure()
    newick = final_tree.to_newick()
    tree = Phylo.read(StringIO(newick), 'newick')

    Phylo.draw_graphviz(tree, prog='neato')
    plt.savefig('unconstrained-tree.png', bbox_inches='tight')
    graph = Phylo.to_networkx(tree)
    with open('unconstrained-tree.nwk', 'w') as fp:
        print >>fp, newick,
    nx.write_dot(graph, 'unconstrained-tree.dot')
    plt.show()
Beispiel #2
0
def iterate(n_iters):
    for i in tqdm(xrange(n_iters)):
        sampler.sample()
        likelihoods.append(sampler.tree.marg_log_likelihood())

    plt.figure()
    plt.xlabel("Iterations", fontsize=fontsize)
    plt.ylabel("Data Log Likelihood", fontsize=fontsize)
    plt.plot(likelihoods)
    plt.legend(loc='best', fontsize=12)

    plt.savefig('unconstrained-likelihoods.png', bbox_inches='tight')

    final_tree = sampler.tree.copy()

    plt.figure()
    plot_tree_2d(final_tree, X, pca)

    for node in final_tree.dfs():
        if node.is_leaf():
            node.point = y[node.point]

    plt.figure()
    newick = final_tree.to_newick()
    tree = Phylo.read(StringIO(newick), 'newick')

    Phylo.draw_graphviz(tree, prog='neato')
    plt.savefig('unconstrained-tree.png', bbox_inches='tight')
    graph = Phylo.to_networkx(tree)
    with open('unconstrained-tree.nwk', 'w') as fp:
        print >> fp, newick,
    nx.write_dot(graph, 'unconstrained-tree.dot')
    plt.show()
Beispiel #3
0
from trees.util import plot_tree, plot_tree_2d
from trees.ddt import DirichletDiffusionTree, Inverse, GaussianLikelihoodModel
from trees.mcmc import MetropolisHastingsSampler
from tqdm import tqdm


if __name__ == "__main__":
    D = 2
    N = 100
    X = np.random.multivariate_normal(mean=np.zeros(D), cov=np.eye(D), size=N).astype(np.float32)
    df = Inverse(c=1)
    lm = GaussianLikelihoodModel(sigma=np.eye(D) / 4.0, mu0=np.zeros(D), sigma0=np.eye(D))
    ddt = DirichletDiffusionTree(df=df,
                                 likelihood_model=lm)
    mh = MetropolisHastingsSampler(ddt, X)
    mh.initialize_assignments()

    for _ in tqdm(xrange(1000)):
        mh.sample()

    plt.figure()
    plt.plot(mh.likelihoods)

    plt.figure()
    plot_tree(mh.tree)

    plt.figure()
    plot_tree_2d(mh.tree, X)

    plt.show()
plt.legend(loc='best', fontsize=12)
plt.savefig('online-scores.png', bbox_inches='tight')

plt.figure()
plt.xlim([0, n_iters + constraint_add])
plt.xlabel("Iterations", fontsize=fontsize)
plt.ylabel("Data Log Likelihood", fontsize=fontsize)
plt.plot(likelihoods)
plt.legend(loc='best', fontsize=12)

plt.savefig('online-likelihoods.png', bbox_inches='tight')

final_tree = sampler.tree.copy()

plt.figure()
plot_tree_2d(final_tree, X, pca)

for node in final_tree.dfs():
    if node.is_leaf():
        node.point = y[node.point]

newick = final_tree.to_newick()
tree = Phylo.read(StringIO(newick), 'newick')

plt.figure()
Phylo.draw_graphviz(tree, prog='neato')
plt.savefig('tree.png', bbox_inches='tight')
graph = Phylo.to_networkx(tree)
with open('tree.nwk', 'w') as fp:
    print >> fp, newick,
nx.write_dot(graph, 'tree.dot')
Beispiel #5
0
plt.legend(loc='best', fontsize=12)
plt.savefig('offline-scores.png', bbox_inches='tight')

plt.figure()
plt.xlim([0, n_iters])
# plt.ylim(ymin=-400)
plt.xlabel("Iterations", fontsize=fontsize)
plt.ylabel("Data Log Likelihood", fontsize=fontsize)
for name, likelihood in likelihoods.items():
    plt.plot(likelihood, label=name)
plt.legend(loc='best', fontsize=12)
plt.savefig('offline-likelihoods.png', bbox_inches='tight')

for type, model in models.items():
    final_tree = model.copy()

    plt.figure()
    plot_tree_2d(final_tree, X, pca)

    for node in final_tree.dfs():
        if node.is_leaf():
            node.point = y[node.point]

    newick = final_tree.to_newick()
    tree = Phylo.read(StringIO(newick), 'newick')

    Phylo.draw_graphviz(tree, prog='neato')
    plt.savefig('tree-%s.png' % type, bbox_inches='tight')
plt.show()