def __init__(self): from pkg_resources import resource_filename filename = resource_filename('pyemma.datasets', 'double_well_discrete.npz') datafile = np.load(filename) self._dtraj_T100K_dt10 = datafile['dtraj'] self._P = datafile['P'] self._msm = markov_model(self._P)
def calculate_macro_TPT_and_MFPT_basedon_micro_MSM(microTPM, mapping, source_state, sink_state, lagtime, time_unit): P = pyemma_msm.markov_model(microTPM) #TPM is row-normalized A = np.where(mapping == source_state)[0] B = np.where(mapping == sink_state)[0] print('MFPT from state %d to state %d = %f %s' % (source_state, sink_state, msmtools.analysis.mfpt(microTPM, A, B) * lagtime, time_unit)) #get TPT tpt = pyemma_msm.tpt(P, A, B) (paths, pathfluxes) = tpt.pathways() cumflux = 0 print("summarizing the pathways in macrostate form") temp_file = open('tpt_temp.log', 'w') for i in range(len(paths)): cumflux += pathfluxes[i] temp_file.write("%16.9f\t%16.9f\t%16.9f\t%s\n" % (pathfluxes[i], 100.0 * pathfluxes[i] / tpt.total_flux, 100.0 * cumflux / tpt.total_flux, mapping[paths[i]])) temp_file.close() os.system('bash lump.sh') #may do this in bash for line in open('tpt_pathlump.log'): print(line.strip()) print('############################################################') print("more details about the paths in the microstate form") print("Path flux\t\t%path\t%of total\tpaths") for i in range(len(paths)): cumflux += pathfluxes[i] print(pathfluxes[i], '\t', '%3.1f' % (100.0 * pathfluxes[i] / tpt.total_flux), '%\t', '%3.1f' % (100.0 * cumflux / tpt.total_flux), '%\t', paths[i])
def its_oom(Ct, C2t, rank, reversible=False, nits=2): Xi, omega, sigma, l = oom_transformations(Ct, C2t, rank) # Compute corrected transition matrix: Tt = TransitionMatrix(Xi, omega, sigma, reversible=reversible) # Build reference model: rmsm = markov_model(Tt) return rmsm.timescales(nits)
def train_markov_chain(transition_matrix): mm = msm.markov_model(transition_matrix) #mm = msm.MSM(transition_matrix,neig=13) #https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6910584/ # Use this to check if reversible mm.is_reversible return mm
def evaluate_dominant_paths(TPM, lagtime, source_state, sink_state, time_unit): M = pyemma_msm.markov_model(TPM) tpt = pyemma_msm.tpt(M, [source_state], [sink_state]) (paths,pathfluxes) = tpt.pathways() cumflux = 0 print("Dominant pathways from state %d to state %d:"%(source_state, sink_state)) print("path\t percentage") for i in range(len(paths)): print(paths[i], '\t','%3.1f'%(100.0*pathfluxes[i]/tpt.total_flux)) print('MFPT from state %d to state %d = %f %s'% (source_state, sink_state, M.mfpt(source_state, sink_state)*lagtime, time_unit))
def pcca(self, m): """ Uses PyEMMA's PCCA to give metastable sets """ try: import pyemma.msm as mm except ImportError: print "Error: PyEMMA must be installed." return 0 model = mm.markov_model(self.T) model.pcca(m) return model.metastable_memberships
def load_markov_state_models(): os.chdir("msm") with open("dtrajs.pkl", "rb") as fhandle: dtrajs_info = pickle.load(fhandle) dirs = dtrajs_info["dirs"] dtrajs = [ dtrajs_info[x] for x in dirs ] with open("msm.pkl", "rb") as fhandle: msm_info = pickle.load(fhandle) lagtimes = msm_info["lagtimes"] models = [] for i in range(len(lagtimes)): models.append(msm.markov_model(msm_info[str(lagtimes[i])])) os.chdir("..") return dirs, dtrajs, lagtimes, models
if len(transition) > 1: transitions.extend(transition) topic_list = data.topic.unique().tolist() topic_list = sorted(topic_list) transition_matrix = np.zeros([len(topic_list), len(topic_list)]).astype(int) for element in transitions: state1 = element[0] state2 = element[1] state1_index = topic_list.index(state1) state2_index = topic_list.index(state2) transition_matrix[state1_index, state2_index] = transition_matrix[state1_index, state2_index] + 1 transition_matrix_scaled = (transition_matrix.T / transition_matrix.sum(axis=1)).T mm = msm.markov_model(transition_matrix_scaled) for element in mm.eigenvalues().argsort()[::-1]: print(topic_list[element]) transition_matrix_scaled = sparse.csr_matrix(transition_matrix_scaled) flux = tpt(transition_matrix_scaled, [1], [2]) pdb.set_trace() chain = MarkovChain(transition_matrix=transition_matrix, states=topic_list) pdb.set_trace()
from pyemma import msm, plots from pyemma import plots as mplt import numpy as np import pdb import matplotlib.pyplot as plt P = np.array([[0.8, 0.15, 0.05, 0.0, 0.0], [0.1, 0.75, 0.05, 0.05, 0.05], [0.05, 0.1, 0.8, 0.0, 0.05], [0.0, 0.2, 0.0, 0.8, 0.0], [0.0, 0.02, 0.02, 0.0, 0.96]]) M = msm.markov_model(P) pos = np.array([[2.0, -1.5], [1, 0], [2.0, 1.5], [0.0, -1.5], [0.0, 1.5]]) pl = mplt.plot_markov_model(M, pos=pos) #pl[0].show() A = [0] B = [4] tpt = msm.tpt(M, A, B) # get tpt gross flux F = tpt.gross_flux print('**Flux matrix**:') print(F) print('**forward committor**:') print(tpt.committor) print('**backward committor**:') print(tpt.backward_committor) # we position states along the y-axis according to the commitor tptpos = np.array([tpt.committor, [0, 0, 0.5, -0.5, 0]]).transpose() print('\n**Gross flux illustration**: ')
def setUp(self): # 5-state toy system self.P = np.array([[0.8, 0.15, 0.05, 0.0, 0.0], [0.1, 0.75, 0.05, 0.05, 0.05], [0.05, 0.1, 0.8, 0.0, 0.05], [0.0, 0.2, 0.0, 0.8, 0.0], [0.0, 0.02, 0.02, 0.0, 0.96]]) self.A = [0] self.B = [4] self.I = [1, 2, 3] # REFERENCE SOLUTION FOR PATH DECOMP self.ref_committor = np.array( [0., 0.35714286, 0.42857143, 0.35714286, 1.]) self.ref_backwardcommittor = np.array( [1., 0.65384615, 0.53125, 0.65384615, 0.]) self.ref_grossflux = np.array( [[0., 0.00771792, 0.00308717, 0., 0.], [0., 0., 0.00308717, 0.00257264, 0.00720339], [0., 0.00257264, 0., 0., 0.00360169], [0., 0.00257264, 0., 0., 0.], [0., 0., 0., 0., 0.]]) self.ref_netflux = np.array([[ 0.00000000e+00, 7.71791768e-03, 3.08716707e-03, 0.00000000e+00, 0.00000000e+00 ], [ 0.00000000e+00, 0.00000000e+00, 5.14527845e-04, 0.00000000e+00, 7.20338983e-03 ], [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 3.60169492e-03 ], [ 0.00000000e+00, 4.33680869e-19, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00 ], [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00 ]]) self.ref_totalflux = 0.0108050847458 self.ref_kAB = 0.0272727272727 self.ref_mfptAB = 36.6666666667 self.ref_paths = [[0, 1, 4], [0, 2, 4], [0, 1, 2, 4]] self.ref_pathfluxes = np.array( [0.00720338983051, 0.00308716707022, 0.000514527845036]) self.ref_paths_99percent = [[0, 1, 4], [0, 2, 4]] self.ref_pathfluxes_99percent = np.array( [0.00720338983051, 0.00308716707022]) self.ref_majorflux_99percent = np.array( [[0., 0.00720339, 0.00308717, 0., 0.], [0., 0., 0., 0., 0.00720339], [0., 0., 0., 0., 0.00308717], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]]) msmobj = markov_model(self.P) msmobj.mu = msmana.statdist(self.P) msmobj.estimated = True msmobj1 = msmobj # Testing: # self.tpt1 = tpt(self.P, self.A, self.B) self.tpt1 = tpt(msmobj1, self.A, self.B) # 16-state toy system P2_nonrev = np.array([[ 0.5, 0.2, 0.0, 0.0, 0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.2, 0.5, 0.1, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.1, 0.5, 0.2, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.1, 0.5, 0.0, 0.0, 0.0, 0.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.3, 0.0, 0.0, 0.0, 0.5, 0.1, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.1, 0.0, 0.0, 0.2, 0.5, 0.1, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.1, 0.0, 0.0, 0.1, 0.5, 0.2, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.3, 0.5, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.5, 0.1, 0.0, 0.0, 0.3, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.2, 0.5, 0.1, 0.0, 0.0, 0.1, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.1, 0.5, 0.1, 0.0, 0.0, 0.2, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.2, 0.5, 0.0, 0.0, 0.0, 0.2 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 0.0, 0.0, 0.0, 0.5, 0.2, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.3, 0.5, 0.1, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.1, 0.5, 0.2 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 0.0, 0.0, 0.2, 0.5 ]]) pstat2_nonrev = msmana.statdist(P2_nonrev) # make reversible C = np.dot(np.diag(pstat2_nonrev), P2_nonrev) Csym = C + C.T self.P2 = Csym / np.sum(Csym, axis=1)[:, np.newaxis] pstat2 = msmana.statdist(self.P2) self.A2 = [0, 4] self.B2 = [11, 15] self.coarsesets2 = [ [2, 3, 6, 7], [10, 11, 14, 15], [0, 1, 4, 5], [8, 9, 12, 13], ] # REFERENCE SOLUTION CG self.ref2_tpt_sets = [ set([0, 4]), set([2, 3, 6, 7]), set([10, 14]), set([1, 5]), set([8, 9, 12, 13]), set([11, 15]) ] self.ref2_cgA = [0] self.ref2_cgI = [1, 2, 3, 4] self.ref2_cgB = [5] self.ref2_cgpstat = np.array([ 0.15995388, 0.18360442, 0.12990937, 0.11002342, 0.31928127, 0.09722765 ]) self.ref2_cgcommittor = np.array( [0., 0.56060272, 0.73052426, 0.19770537, 0.36514272, 1.]) self.ref2_cgbackwardcommittor = np.array( [1., 0.43939728, 0.26947574, 0.80229463, 0.63485728, 0.]) self.ref2_cggrossflux = np.array( [[0., 0., 0., 0.00427986, 0.00282259, 0.], [0., 0, 0.00234578, 0.00104307, 0., 0.00201899], [0., 0.00113892, 0, 0., 0.00142583, 0.00508346], [0., 0.00426892, 0., 0, 0.00190226, 0.], [0., 0., 0.00530243, 0.00084825, 0, 0.], [0., 0., 0., 0., 0., 0.]]) self.ref2_cgnetflux = np.array( [[0., 0., 0., 0.00427986, 0.00282259, 0.], [0., 0., 0.00120686, 0., 0., 0.00201899], [0., 0., 0., 0., 0., 0.00508346], [0., 0.00322585, 0., 0., 0.00105401, 0.], [0., 0., 0.0038766, 0., 0., 0.], [0., 0., 0., 0., 0., 0.]]) """Dummy dtraj to trick trick constructor of MSM""" dtraj = [0, 0] tau = 1 msmobj = markov_model(self.P2) msmobj.mu = msmana.statdist(self.P2) msmobj.estimated = True msmobj2 = msmobj # Testing self.tpt2 = tpt(msmobj2, self.A2, self.B2)
1.94053514e-02, 0.00000000e+00 ], [ 9.75896673e-03, 0.00000000e+00, -3.56068076e-02, 1.53539843e-02, 1.04938566e-02 ], [ 4.98873182e-05, 6.06203157e-03, 3.28301032e-03, -9.39492921e-03, 0.00000000e+00 ], [ 1.35496766e-02, 0.00000000e+00, 1.17575223e-02, 0.00000000e+00, -2.53071990e-02 ]]) tau = 1.E-2 T = expm(K * tau) # NB TPT results should be invariant with tau my_msm = msm.markov_model(T) A = [0] B = [4] my_msm_tpt = msm.tpt(my_msm, A, B) print("Stationary distribution:") print(my_msm_tpt.mu) print("Forward committor:") print(my_msm_tpt.committor) print("Backward committor:") print(my_msm_tpt.backward_committor)
print(its) print(diffusion_model.its[1:4]) hist_mem = np.empty([diffusion_model.center_list.shape[0], state_num]) for i in range(state_num): hist_mem[:, i] = np.histogram( sample_mem[:, i], bins=grid_num, range=(lb, ub), density=True, )[0] hist_mem[:, i] /= hist_mem[:, i].sum() transition_density = pp.dot(hist_mem.T) model = markov_model(K) stationary_density = model.stationary_distribution.dot(hist_mem.T) transition_density_0_mem[kk] = transition_density stationary_density_0_mem[kk] = stationary_density np.save('data/ed/partition_mem', partition_mem) np.save('data/ed/K_0_mem', K_0_mem) np.save('data/ed/its_0_mem', its_0_mem) np.save('data/ed/transition_density_0_mem', transition_density_0_mem) np.save('data/ed/stationary_density_0_mem', stationary_density_0_mem) for kk in range(3): plt.figure() plt.plot(partition_mem[kk]) plt.figure()
# is the reason why we have to do the inverse instead of the simpler operation here count_matrix_fuzzy = dtraj_fuzzy[:-1].T @ dtraj_fuzzy[1:] transition_matrix_fuzzy = np.linalg.inv( dtraj_fuzzy[:-1].T @ dtraj_fuzzy[:-1]) @ count_matrix_fuzzy transition_matrix_fuzzy[transition_matrix_fuzzy < 0] = 0 transition_matrix_fuzzy = preprocessing.normalize(transition_matrix_fuzzy, axis=1, norm="l1") #transition_matrix_fuzzy = transition_matrix_fuzzy / transition_matrix_fuzzy.sum(axis=1) #transition_matrix_fuzzy = transition_matrix_fuzzy.astype(np.float16) assert np.allclose(transition_matrix_fuzzy.sum(axis=1), 1) mm = msm.markov_model(transition_matrix_fuzzy) mm.is_reversible #Print the stationary distributions of the top states results = [] for i, element in enumerate(mm.pi.argsort()[::-1]): print(i) print(topic_labels[element]) print(mm.pi[element]) print('\n') results.append({ 'topic_name': topic_labels[element], 'stationary_prob': mm.pi[element] }) if i == 12:
assignment_array.append( numpy.loadtxt("%s" % (traj_files_array[i]), dtype='int')) #####having read microstate assignment #lag_time_msm=200 ## lag time to build MSM, in the unit of frame interval ## set parameters to build MSM msm_model = MarkovStateModel(lag_time=options.lag_time_msm, reversible_type='mle', sliding_window=1, ergodic_cutoff='on', prior_counts=0, verbose=True) msm_model.fit(assignment_array) print msm_model.transmat_ TPM = msm.markov_model(msm_model.transmat_) ########################using pyemma to get transition paths set1 = numpy.where(mapping == source)[0] set2 = numpy.where(mapping == sink)[0] #print set1 #print set2 tpt = msm.tpt(TPM, set1, set2) ## get tpt gross flux #print "grpss flux matrix" #print tpt.gross_flux #print "forward committor matrix" #print tpt.committor #print "backward committor matrix" #print tpt.backward_committor_matrix