def test_its_bhmm(self): BHMM = msm.bayesian_hidden_markov_model([self.double_well_data.dtraj_T100K_dt10_n6good], 2, 10) self.ck = BHMM.cktest(mlags=[0,1,10]) estref = np.array([[[ 1., 0. ], [ 0., 1. ]], [[ 0.98497185, 0.01502815], [ 0.01459256, 0.98540744]], [[ 0.88213404, 0.11786596], [ 0.11877379, 0.88122621]]]) predref = np.array([[[ 1., 0. ], [ 0., 1. ]], [[ 0.98497185, 0.01502815], [ 0.01459256, 0.98540744]], [[ 0.86824695, 0.13175305], [ 0.1279342, 0.8720658 ]]]) predLref = np.array([[[ 1. , 0. ], [ 0. , 1. ]], [[ 0.98282734, 0.01284444], [ 0.0123793 , 0.98296742]], [[ 0.8514399 , 0.11369687], [ 0.10984971, 0.85255827]]]) predRref = np.array([[[ 1. , 0. ], [ 0. , 1. ]], [[ 0.98715575, 0.01722138], [ 0.0178059 , 0.98762081]], [[ 0.8865478 , 0.14905352], [ 0.14860461, 0.89064809]]]) # rough agreement assert np.allclose(self.ck.estimates, estref, rtol=0.1, atol=10.0) assert self.ck.estimates_conf[0] is None assert self.ck.estimates_conf[1] is None assert np.allclose(self.ck.predictions, predref, rtol=0.1, atol=10.0) assert np.allclose(self.ck.predictions[0], predLref, rtol=0.1, atol=10.0) assert np.allclose(self.ck.predictions[1], predRref, rtol=0.1, atol=10.0)
def test_submodel_simple(self): # sanity check for submodel; # call should not alter self from copy import deepcopy # dtrj = np.random.randint(0, 2, size=100) # dtrj[np.random.randint(0, dtrj.shape[0], 3)] = 2 # hard-coded due to stochastic failures dtrj = [ 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 2, 0, 0, 1, 1, 2, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0 ] h = bayesian_hidden_markov_model(dtrj, 3, 2) h_original = deepcopy(h) hs = h.submodel_largest(mincount_connectivity=5) self.assertTrue(h == h_original) self.assertEqual(hs.timescales().shape[0], 1) self.assertEqual(hs.pi.shape[0], 2) self.assertEqual(hs.transition_matrix.shape, (2, 2))
def test_separate_states(self): dtrajs = [np.array([0, 1, 1, 1, 1, 1, 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1]), np.array([2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2]),] hmm_bayes = bayesian_hidden_markov_model(dtrajs, 3, lag=1, separate=[0], nsamples=100) # we expect zeros in all samples at the following indexes: pobs_zeros = [[0, 1, 2, 2, 2], [0, 0, 1, 2, 3]] for s in hmm_bayes.samples: assert np.allclose(s.observation_probabilities[pobs_zeros], 0) for strajs in hmm_bayes.sampled_trajs: assert strajs[0][0] == 2 assert strajs[0][6] == 2
def setUpClass(cls): # load observations import pyemma.datasets obs = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10 # hidden states cls.nstates = 2 # samples cls.nsamples = 100 cls.lag = 10 cls.bhmm = bayesian_hidden_markov_model([obs], cls.nstates, cls.lag, reversible=True, nsamples=cls.nsamples)
def setUpClass(cls): # load observations import pyemma.datasets obs = pyemma.datasets.load_2well_discrete().dtraj_T100K_dt10 # don't print import bhmm bhmm.config.verbose = False # hidden states cls.nstates = 2 # samples cls.nsamples = 100 cls.lag = 10 cls.sampled_hmm_lag10 = bayesian_hidden_markov_model( [obs], cls.nstates, cls.lag, reversible=True, nsamples=cls.nsamples)
mplt.plot_cktest(MSM.cktest(3)) plt.savefig('cktest_msm.png') plt.clf() plt.figure(figsize=(8, 5)) mplt.plot_free_energy(np.hstack(Y1), np.hstack(Y2), weights=np.hstack(MSM.trajectory_weights())) plt.xlabel('tic 1') plt.ylabel('tic 2') plt.savefig('reweighted_tic1_tic2.png') print('running hmm') HMM = msm.bayesian_hidden_markov_model(clkmeans.dtrajs, n_macrostates, lag=100) hmm_dist = HMM.metastable_distributions hmm_sets = HMM.metastable_sets plt.clf() plt.figure(figsize=(8, 5)) mplt.plot_free_energy(np.hstack(Y1), np.hstack(Y2), weights=np.hstack(MSM.trajectory_weights())) colors = [ 'red', 'cyan', 'blue', 'purple', 'magenta', 'brown', 'black', 'white', 'orange', 'pink', 'yellowgreen' ] for i in range(n_macrostates): plt.scatter(clkmeans.clustercenters[hmm_sets[i], 0], clkmeans.clustercenters[hmm_sets[i], 1],
import pyemma.coordinates as coor import pyemma.msm as msm import pyemma.plots as mplt Y = np.load('tica_projection.npy') Y1 = [y[:, 0] for y in Y] Y2 = [y[:, 1] for y in Y] clkmeans_clustercenters = np.load('clkmeans_clustercenters.npy') clkmeans_dtrajs = np.load('clkmeans_dtrajs.npy') clkmeans_dtrajs = clkmeans_dtrajs.tolist() HMM = msm.bayesian_hidden_markov_model(clkmeans_dtrajs, 2, lag=100) state_1_colors = HMM.metastable_distributions[0] / max( HMM.metastable_distributions[0]) state_2_colors = HMM.metastable_distributions[1] / max( HMM.metastable_distributions[1]) path_to_trajs = '../MSM-analysis/DDR1_WT_pro_plus_OG_trajectories/*.h5' from glob import glob filenames = sorted(glob(path_to_trajs)) print('These are our trajectories that flip.') print(filenames[16], filenames[42], filenames[145], filenames[149]) import seaborn as sns
system = 'WT-pro' Y = np.load('%s/tica_projection.npy' % file_paths[system]) Y1 = [y[:, 0] for y in Y] Y2 = [y[:, 1] for y in Y] clkmeans_clustercenters = np.load('%s/clkmeans_clustercenters.npy' % file_paths[system]) clkmeans_dtrajs = np.load('%s/clkmeans_dtrajs.npy' % file_paths[system]) clkmeans_dtrajs = clkmeans_dtrajs.tolist() HMM = msm.bayesian_hidden_markov_model(clkmeans_dtrajs, nstates=2, lag=100, nsamples=1000, mincount_connectivity=100) hmm_sets = HMM.metastable_sets state0_samples = [ clkmeans_clustercenters[hmm_sets[0], 0], clkmeans_clustercenters[hmm_sets[0], 1] ] state1_samples = [ clkmeans_clustercenters[hmm_sets[1], 0], clkmeans_clustercenters[hmm_sets[1], 1] ] X_HMM = np.concatenate((state0_samples, state1_samples), axis=1)