def test_mfpt_match(): assignments = np.random.randint(10, size=(10, 2000)) msm = MarkovStateModel(lag_time=1) msm.fit(assignments) # these two do different things mfpts0 = np.vstack([tpt.mfpts(msm, i) for i in range(10)]).T mfpts1 = tpt.mfpts(msm) npt.assert_array_almost_equal(mfpts0, mfpts1)
def test_mfpt_match(): assignments = np.random.randint(10, size=(10, 2000)) msm = MarkovStateModel(lag_time=1) msm.fit(assignments) # these two do different things mfpts0 = np.vstack([tpt.mfpts(msm, i) for i in xrange(10)]).T mfpts1 = tpt.mfpts(msm) # print(mfpts0) # print(mfpts1) npt.assert_array_almost_equal(mfpts0, mfpts1)
def _analyze_timescales(self, mm): mfpts = tpt.mfpts(mm) * self.lagtime # mean first passage times logger.info( "Means first passage time from last to first state and vice versa: %s, %s", mfpts[0, -1], mfpts[-1, 0]) if len(mfpts) > 0: logger.info("Max first passage time: %s", mfpts.max()) timescales = mm.timescales_ timescales[np.isnan(timescales)] = -1 timescales = timescales * self.lagtime if len(timescales) > 0: logger.info("Max relaxation time for the system %s", timescales.max())
def test_mfpt2(): tprob = np.array([[0.90, 0.10], [0.22, 0.78]]) pi0 = 1 pi1 = pi0 * tprob[0, 1] / tprob[1, 0] pops = np.array([pi0, pi1]) / (pi0 + pi1) msm = MarkovStateModel(lag_time=1) msm.transmat_ = tprob msm.n_states_ = 2 msm.populations_ = pops mfpts = np.vstack([tpt.mfpts(msm, i) for i in range(2)]).T # since it's a 2x2 the mfpt from 0 -> 1 is the # same as the escape time of 0 npt.assert_almost_equal(1 / (1 - tprob[0, 0]), mfpts[0, 1]) npt.assert_almost_equal(1 / (1 - tprob[1, 1]), mfpts[1, 0])
def do_tpt(ev_id): plt.figure(figsize=(15, 10)) # TPT "FROM": sources = [7] # TPT "To": sinks = [10] net_flux = tpt.net_fluxes(sources, sinks, msm, for_committors=None) np.savetxt('net_flux.txt', net_flux) pfold = tpt.committors(sources, sinks, msm) np.savetxt('pfold.txt', pfold) paths = tpt.paths(sources, sinks, net_flux, remove_path='subtract', flux_cutoff=0.9999999999) mfpts = tpt.mfpts( msm, sinks=None, lag_time=1.0 ) # Default is (1) which is in units of the lag time of the MSM. print "mfpts:", mfpts np.savetxt('mfpts_from_i_to_j.txt', np.array(mfpts)) total_flux = np.sum(paths[1]) print "total_flux:", total_flux sort = np.argsort(pfold) total_line_width = np.sum(paths[1][0:5]) # top 5 paths, to get all paths set this number higher for j in range(5): print "path:", paths[0][j] print "flux:", paths[1][j], paths[1][j] / float(np.sum(paths[1])) x = [] for k in range(len(paths[0][j])): x.extend(np.where(np.arange(100)[sort] == paths[0][j][k])[0]) plt.plot(x, pfold[paths[0][j]], linewidth=np.log(paths[1][j] / float(total_line_width))) plt.legend(['%1.8f' % i for i in paths[1][0:5]], fontsize=18, loc='upper left') for i in range(len(pfold)): plt.plot(i, pfold[sort[i]], 'o') plt.text(i, pfold[sort[i]], sort[i]) plt.savefig('pfold_ev%d_0.01.png' % ev_id)
def test_mfpt2(): tprob = np.array([[0.90, 0.10], [0.22, 0.78]]) pi0 = 1 # pi1 T[1, 0] = pi0 T[0, 1] pi1 = pi0 * tprob[0, 1] / tprob[1, 0] pops = np.array([pi0, pi1]) / (pi0 + pi1) msm = MarkovStateModel(lag_time=1) msm.transmat_ = tprob msm.n_states_ = 2 msm.populations_ = pops mfpts = np.vstack([tpt.mfpts(msm, i) for i in xrange(2)]).T #print(1 / (1 - tprob[0, 0]), mfpts[0, 1]) #print(1 / (1 - tprob[1, 1]), mfpts[1, 0]) # since it's a 2x2 the mfpt from 0 -> 1 is the # same as the escape time of 0 npt.assert_almost_equal(1 / (1 - tprob[0, 0]), mfpts[0, 1]) npt.assert_almost_equal(1 / (1 - tprob[1, 1]), mfpts[1, 0])
# clustering from msmbuilder.cluster import MiniBatchKMeans clusterer = MiniBatchKMeans(n_clusters=num_clusters) clustered_trajs = tica_trajs.fit_transform_with( clusterer, 'kmeans/', fmt='dir-npy' ) # msm builder from msmbuilder.msm import MarkovStateModel from msmbuilder.utils import dump msm = MarkovStateModel(lag_time=20, n_timescales=20, ergodic_cutoff='on') msm.fit(clustered_trajs) # Get MFPT from msmbuilder.tpt import mfpts mfpt_matrix = mfpts(msm) # Get flux matrix Pi = np.diag(msm.populations_) Pi = scipy.linalg.fractional_matrix_power(Pi, 1) Pi_L = scipy.linalg.fractional_matrix_power(Pi, 0.5) Pi_R = scipy.linalg.fractional_matrix_power(Pi, -0.5)!! T = msm.transmat_ flux = np.linalg.multi_dot([Pi_L,T,Pi_R]) # Concatenate the trajectories in cluster indices cluster_indices = np.concatenate(clustered_trajs) # Combine all trajectories into a trajectory "bag" temp = xyz[0] _, num_atoms, num_axis = temp.xyz.shape