def test_coin_sample_post(self): """Test sampling from posterior distribution""" outdir = 'test/tmp/test_hmm/test_coin_sample_post/' make_clean_dir(outdir) model = make_coin_model() # sample states and data ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) data = list(hmm.sample_hmm_data(model, states)) model.prob_emission = ( lambda pos, state: model.prob_emission_data(state, data[pos])) p = Gnuplot() p.enableOutput(False) p.plot(states, style="lines") probs = hmm.get_posterior_probs(model, len(data)) states2 = [exp(probs[i][1]) for i in xrange(len(data))] p.plot(util.vadds(states2, 1.5), style="lines", miny=-1, maxy=12) for i in range(2, 10): states2 = hmm.sample_posterior(model, ndata) self.assertTrue(stats.corr(states, states2) > .5) p.plot(util.vadds(states2, 1.5 * i), style="lines", miny=-1, maxy=12) p.enableOutput(True) p.save(outdir + 'plot.png')
def test_coin_sample_post(self): """Test sampling from posterior distribution""" outdir = 'test/tmp/test_hmm/test_coin_sample_post/' make_clean_dir(outdir) model = make_coin_model() # sample states and data ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) data = list(hmm.sample_hmm_data(model, states)) model.prob_emission = (lambda pos, state: model.prob_emission_data(state, data[pos])) p = Gnuplot() p.enableOutput(False) p.plot(states, style="lines") probs = hmm.get_posterior_probs(model, len(data)) states2 = [exp(probs[i][1]) for i in xrange(len(data))] p.plot(util.vadds(states2, 1.5), style="lines", miny=-1, maxy=12) for i in range(2, 10): states2 = hmm.sample_posterior(model, ndata) self.assertTrue(stats.corr(states, states2) > .5) p.plot(util.vadds(states2, 1.5*i), style="lines", miny=-1, maxy=12) p.enableOutput(True) p.save(outdir + 'plot.png')
def test_norecomb_plot(self): k = 50 n = 1e4 rho = 1.5e-8 * .0001 rho2 = 1.5e-8 * 10 mu = 2.5e-8 * 100 length = 1000 arg = arglib.sample_arg(k, n, rho, start=0, end=length) muts = arglib.sample_arg_mutations(arg, mu) seqs = arglib.make_alignment(arg, muts) times = arghmm.get_time_points(ntimes=20) arghmm.discretize_arg(arg, times) tree = arg.get_marginal_tree(0) treelib.draw_tree_names(tree.get_tree(), minlen=5, scale=4e-4) # get thread new_name = "n%d" % (k - 1) keep = ["n%d" % i for i in range(k - 1)] arglib.subarg_by_leaf_names(arg, keep) arg.set_ancestral() arg.prune() model = arghmm.ArgHmm(arg, seqs, new_name=new_name, times=times, rho=rho2, mu=mu) print "states", len(model.states[0]) print "muts", len(muts) # simulate a new thread states = list(islice(hmm.sample_hmm_states(model), 0, arg.end)) data = list(hmm.sample_hmm_data(model, states)) seqs[new_name] = "".join(data) #alignlib.print_align(seqs) thread = [ model.times[model.states[i][s][1]] for i, s in enumerate(states) ] p = plot(thread, style="lines") probs = arghmm.get_posterior_probs(model, length, verbose=True) print "done" high = list(arghmm.iter_posterior_times(model, probs, .75)) low = list(arghmm.iter_posterior_times(model, probs, .25)) p.plot(high, style="lines") p.plot(low, style="lines") pause()
def test_norecomb_plot(self): k = 50 n = 1e4 rho = 1.5e-8 * .0001 rho2 = 1.5e-8 * 10 mu = 2.5e-8 * 100 length = 1000 arg = arglib.sample_arg(k, n, rho, start=0, end=length) muts = arglib.sample_arg_mutations(arg, mu) seqs = arglib.make_alignment(arg, muts) times = arghmm.get_time_points(ntimes=20) arghmm.discretize_arg(arg, times) tree = arg.get_marginal_tree(0) treelib.draw_tree_names(tree.get_tree(), minlen=5, scale=4e-4) # get thread new_name = "n%d" % (k-1) keep = ["n%d" % i for i in range(k-1)] arglib.subarg_by_leaf_names(arg, keep) arg.set_ancestral() arg.prune() model = arghmm.ArgHmm(arg, seqs, new_name=new_name, times=times, rho=rho2, mu=mu) print "states", len(model.states[0]) print "muts", len(muts) # simulate a new thread states = list(islice(hmm.sample_hmm_states(model), 0, arg.end)) data = list(hmm.sample_hmm_data(model, states)) seqs[new_name] = "".join(data) #alignlib.print_align(seqs) thread = [model.times[model.states[i][s][1]] for i, s in enumerate(states)] p = plot(thread, style="lines") probs = arghmm.get_posterior_probs(model, length, verbose=True) print "done" high = list(arghmm.iter_posterior_times(model, probs, .75)) low = list(arghmm.iter_posterior_times(model, probs, .25)) p.plot(high, style="lines") p.plot(low, style="lines") pause()
def test_coin_post(self): """Test that posterior decoding.""" model = make_coin_model() # sample states and data ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) data = list(hmm.sample_hmm_data(model, states)) model.prob_emission = ( lambda pos, state: model.prob_emission_data(state, data[pos])) probs = hmm.get_posterior_probs(model, len(data)) for col in probs: p = sum(map(exp, col)) self.assertAlmostEqual(p, 1.0)
def test_coin_post(self): """Test that posterior decoding.""" model = make_coin_model() # sample states and data ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) data = list(hmm.sample_hmm_data(model, states)) model.prob_emission = (lambda pos, state: model.prob_emission_data(state, data[pos])) probs = hmm.get_posterior_probs(model, len(data)) for col in probs: p = sum(map(exp, col)) self.assertAlmostEqual(p, 1.0)
def test_coin(self): """Test that viterbi and posterior coding work well.""" outdir = 'test/tmp/test_hmm/test_coin/' make_clean_dir(outdir) model = make_coin_model() # sample states ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) p = Gnuplot() p.enableOutput(False) p.plot(states, style="lines") # sample data data = list(hmm.sample_hmm_data(model, states)) # viterbi model.prob_emission = ( lambda pos, state: model.prob_emission_data(state, data[pos])) states2 = hmm.viterbi(model, len(data)) # posterior probs = hmm.get_posterior_probs(model, len(data)) states3 = [exp(probs[i][1]) for i in xrange(len(data))] # assert that inferences correlates with true state self.assertTrue(stats.corr(states, states2) > .5) self.assertTrue(stats.corr(states, states3) > .5) # plot inference p.plot(util.vadds(states2, 1.5), style="lines", miny=-1, maxy=4) p.plot(util.vadds(states3, 2.5), style="lines", miny=-1, maxy=4) p.enableOutput(True) p.save(outdir + 'plot.png')
def test_coin(self): """Test that viterbi and posterior coding work well.""" outdir = 'test/tmp/test_hmm/test_coin/' make_clean_dir(outdir) model = make_coin_model() # sample states ndata = 100 states = list(islice(hmm.sample_hmm_states(model), ndata)) p = Gnuplot() p.enableOutput(False) p.plot(states, style="lines") # sample data data = list(hmm.sample_hmm_data(model, states)) # viterbi model.prob_emission = (lambda pos, state: model.prob_emission_data(state, data[pos])) states2 = hmm.viterbi(model, len(data)) # posterior probs = hmm.get_posterior_probs(model, len(data)) states3 = [exp(probs[i][1]) for i in xrange(len(data))] # assert that inferences correlates with true state self.assertTrue(stats.corr(states, states2) > .5) self.assertTrue(stats.corr(states, states3) > .5) # plot inference p.plot(util.vadds(states2, 1.5), style="lines", miny=-1, maxy=4) p.plot(util.vadds(states3, 2.5), style="lines", miny=-1, maxy=4) p.enableOutput(True) p.save(outdir + 'plot.png')