def test_pcca_3(self): P = np.array([[0.9, 0.1, 0.0, 0.0], [0.1, 0.9, 0.0, 0.0], [0.0, 0.0, 0.8, 0.2], [0.0, 0.0, 0.2, 0.8]]) # n=2 chi = pcca(P, 2) sol = np.array([[1., 0.], [1., 0.], [0., 1.], [0., 1.]]) assert_allclose(chi, sol) # n=3 chi = pcca(P, 3) sol = np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.], [0., 0., 1.]]) assert_allclose(chi, sol) # n=4 chi = pcca(P, 4) sol = np.array([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]]) assert_allclose(chi, sol)
def ALA4_msstates( T_AA ): # Define the metastable states from analysis of T_AA_mle membership=pcca(T_AA, 4) # the precise definitions were fine-tuned by hand, I should plot the PCCA states to double check! E1 = np.where(membership[:,0]>0.4)[0] E2 = np.where(membership[:,1]>0.5)[0] H = np.where(membership[:,2]>0.5)[0] I = np.where(membership[:,3]>0.5)[0] # make sure there is no overlap #print "checking the overlap of metastable states:" check = [val for val in H if val in I] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in H if val in E1] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in H if val in E2] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in I if val in E1] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in I if val in E2] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in E1 if val in E2] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') return H, I, E2, E1
def ALA4_msstates( T_AA ): # Define the metastable states from analysis of T_AA_mle membership=pcca(T_AA, 4) # the precise definitions were fine-tuned by hand, I should plot the PCCA states to double check! E1 = np.where(membership[:,0]>0.4)[0] E2 = np.where(membership[:,1]>0.75)[0] H = np.where(membership[:,2]>0.6)[0] I = np.where(membership[:,3]>0.75)[0] # make sure there is no overlap #print "checking the overlap of metastable states:" check = [val for val in H if val in I] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in H if val in E1] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in H if val in E2] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in I if val in E1] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in I if val in E2] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in E1 if val in E2] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') return H, I, E2, E1
def test_pcca_1(self): P = np.array([[1, 0], [0, 1]]) chi = pcca(P, 2) sol = np.array([[1., 0.], [0., 1.]]) assert_allclose(chi, sol)
def test_pcca_large(self): import os P = np.loadtxt(os.path.split(__file__)[0] + '/../../P_rev_251x251.dat') # n=2 chi = pcca(P, 2) assert (np.alltrue(chi >= 0)) assert (np.alltrue(chi <= 1)) # n=3 chi = pcca(P, 3) assert (np.alltrue(chi >= 0)) assert (np.alltrue(chi <= 1)) # n=4 chi = pcca(P, 4) assert (np.alltrue(chi >= 0)) assert (np.alltrue(chi <= 1))
def test_pcca_2(self): P = np.array([[0.0, 1.0, 0.0], [0.0, 0.999, 0.001], [0.0, 0.001, 0.999]]) chi = pcca(P, 2) sol = np.array([[1., 0.], [1., 0.], [0., 1.]]) assert_allclose(chi, sol)
def test_pcca_4(self): P = np.array([[0.9, 0.1, 0.0, 0.0], [0.1, 0.8, 0.1, 0.0], [0.0, 0.0, 0.8, 0.2], [0.0, 0.0, 0.2, 0.8]]) chi = pcca(P, 2) sol = np.array([[1., 0.], [1., 0.], [1., 0.], [0., 1.]]) assert_allclose(chi, sol)
def set_model_params(self, P, m): """ Parameters ---------- P : ndarray (n,n) Transition matrix. m : int Number of clusters to group to. """ # remember input from scipy.sparse import issparse if issparse(P): warnings.warn( 'PCCA is only implemented for dense matrices, ' 'converting sparse transition matrix to dense ndarray.') P = P.toarray() self.P = P self.m = m # pcca coarse-graining # -------------------- # PCCA memberships # TODO: can be improved. pcca computes stationary distribution internally, we don't need to compute it twice. from msmtools.analysis.dense.pcca import pcca self._M = pcca(P, m) # stationary distribution # TODO: in msmtools we recomputed this from P, we actually want to use pi from the msm obj, but this caused #1208 from msmtools.analysis import stationary_distribution self._pi = stationary_distribution(P) # coarse-grained stationary distribution self._pi_coarse = np.dot(self._M.T, self._pi) # HMM output matrix self._B = np.dot(np.dot(np.diag(1.0 / self._pi_coarse), self._M.T), np.diag(self._pi)) # renormalize B to make it row-stochastic self._B /= self._B.sum(axis=1)[:, None] self._B /= self._B.sum(axis=1)[:, None] # coarse-grained transition matrix W = np.linalg.inv(np.dot(self._M.T, self._M)) A = np.dot(np.dot(self._M.T, P), self._M) self._P_coarse = np.dot(W, A) # symmetrize and renormalize to eliminate numerical errors X = np.dot(np.diag(self._pi_coarse), self._P_coarse) self._P_coarse = X / X.sum(axis=1)[:, None]
def ALA3_msstates( T_AA ): # Define the metastable states from analysis of T_AA_mle membership=pcca(T_AA, 3) # the precise definitions were fine-tuned by hand, I should plot the PCCA states to double check! lH = np.where(membership[:,0]>0.60)[0] aH = np.where(membership[:,1]>0.60)[0] B = np.where(membership[:,2]>0.60)[0] # make sure there is no overlap #print "checking the overlap of metastable states:" check = [val for val in lH if val in aH] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in lH if val in B] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') check = [val for val in aH if val in B] if (len(check) != 0 ): raise ValueError('Some metastable states are overlapping!') return B, aH, lH
def test_multiple_components(self): L = np.array([[0, 1, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 0, 1, 0], [0, 0, 0, 1, 1, 0, 1], [0, 0, 0, 1, 0, 1, 0]]) transition_matrix = L / np.sum(L, 1).reshape(-1, 1) chi = pcca(transition_matrix, 2) expected = np.array([[0., 1.], [0., 1.], [0., 1.], [1., 0.], [1., 0.], [1., 0.], [1., 0.]]) np.testing.assert_equal(chi, expected)
def ALA3_msstates(T_AA): # Define the metastable states from analysis of T_AA_mle membership = pcca(T_AA, 3) # the precise definitions were fine-tuned by hand, I should plot the PCCA states to double check! lH = np.where(membership[:, 0] > 0.60)[0] aH = np.where(membership[:, 1] > 0.60)[0] B = np.where(membership[:, 2] > 0.60)[0] # make sure there is no overlap #print "checking the overlap of metastable states:" check = [val for val in lH if val in aH] if (len(check) != 0): raise ValueError('Some metastable states are overlapping!') check = [val for val in lH if val in B] if (len(check) != 0): raise ValueError('Some metastable states are overlapping!') check = [val for val in aH if val in B] if (len(check) != 0): raise ValueError('Some metastable states are overlapping!') return B, aH, lH
def test_pcca_coarsegrain(self): # fine-grained transition matrix P = np.array([[0.9, 0.1, 0.0, 0.0, 0.0], [0.1, 0.89, 0.01, 0.0, 0.0], [0.0, 0.1, 0.8, 0.1, 0.0], [0.0, 0.0, 0.01, 0.79, 0.2], [0.0, 0.0, 0.0, 0.2, 0.8]]) from msmtools.analysis import stationary_distribution pi = stationary_distribution(P) Pi = np.diag(pi) m = 3 # Susanna+Marcus' expression ------------ M = pcca(P, m) pi_c = np.dot(M.T, pi) Pi_c_inv = np.diag(1.0/pi_c) # restriction and interpolation operators R = M.T I = np.dot(np.dot(Pi, M), Pi_c_inv) # result ms1 = np.linalg.inv(np.dot(R,I)).T ms2 = np.dot(np.dot(I.T, P), R.T) Pc_ref = np.dot(ms1,ms2) # --------------------------------------- Pc = coarsegrain(P, 3) # test against Marcus+Susanna's expression assert np.max(np.abs(Pc - Pc_ref)) < 1e-10 # test mass conservation assert np.allclose(Pc.sum(axis=1), np.ones(m)) p = PCCA(P, m) # test against Marcus+Susanna's expression assert np.max(np.abs(p.coarse_grained_transition_matrix - Pc_ref)) < 1e-10 # test against the present coarse-grained stationary dist assert np.max(np.abs(p.coarse_grained_stationary_probability - pi_c)) < 1e-10 # test mass conservation assert np.allclose(p.coarse_grained_transition_matrix.sum(axis=1), np.ones(m))
def test_pcca_no_detailed_balance(self): P = np.array([[0.8, 0.1, 0.1], [0.3, 0.2, 0.5], [0.6, 0.3, 0.1]]) with self.assertRaises(ValueError): pcca(P, 2)
def test_pcca_no_transition_matrix(self): P = np.array([[1.0, 1.0], [0.1, 0.9]]) with self.assertRaises(ValueError): pcca(P, 2)