def test_weakly_connected_count_matrix(): count_matrix = np.array([[10, 1, 0, 0], [0, 1, 1, 0], [0, 1, 1, 1], [0, 0, 0, 1]], dtype=np.float32) assert_equal(MaximumLikelihoodMSM().fit(count_matrix).fetch_model().n_connected_msms, 3, err_msg="Count matrix not strongly connected, should decay into three sets.") # count matrix weakly connected, this should work msm = MaximumLikelihoodMSM(reversible=False).fit(count_matrix).fetch_model() assert_equal(msm.reversible, False) assert_equal(msm.n_states, 4) assert_equal(msm.lagtime, 1) assert_(msm.count_model is not None) assert_equal(msm.count_model.count_matrix, count_matrix) # last state is sink state assert_equal(msm.stationary_distribution, [0, 0, 0, 1]) assert_array_almost_equal(msm.transition_matrix, [[10. / 11, 1. / 11, 0, 0], [0, 0.5, 0.5, 0], [0, 1. / 3, 1. / 3, 1. / 3], [0, 0, 0, 1]]) assert_equal(msm.n_eigenvalues, 4) assert_equal(msm.sparse, False) msm = msm.submodel(np.array([1, 2])) assert_equal(msm.reversible, False) assert_equal(msm.n_states, 2) assert_equal(msm.count_model.state_symbols, [1, 2]) assert_equal(msm.lagtime, 1) assert_equal(msm.count_model.count_matrix, [[1, 1], [1, 1]]) assert_equal(msm.stationary_distribution, [0.5, 0.5]) assert_array_almost_equal(msm.transition_matrix, [[0.5, 0.5], [0.5, 0.5]]) assert_equal(msm.n_eigenvalues, 2) assert_equal(msm.sparse, False)
def test_strongly_connected_count_matrix(): # transitions 6->1->2->3->4->6, disconnected are 0 and 5 dtraj = np.array([0, 6, 1, 2, 3, 4, 6, 5]) counts = TransitionCountEstimator(lagtime=1, count_mode="sliding").fit(dtraj).fetch_model() assert_equal(counts.n_states, 7) sets = counts.connected_sets(directed=True) assert_equal(len(sets), 3) assert_equal(len(sets[0]), 5) with assert_raises(BaseException, msg="count matrix not strongly connected, expected failure in rev. case"): MaximumLikelihoodMSM().fit(counts) counts = counts.submodel_largest(directed=True) # now we are strongly connected # due to reversible we get 6<->1<->2<->3<->4<->6 msm = MaximumLikelihoodMSM(reversible=True).fit(counts).fetch_model() # check that the msm has symbols 1,2,3,4,6 assert_(np.all([i in msm.count_model.state_symbols for i in [1, 2, 3, 4, 6]])) assert_equal(msm.reversible, True) assert_equal(msm.n_states, 5) assert_equal(msm.lagtime, 1) assert_array_almost_equal(msm.transition_matrix, [ [0., .5, 0., 0., .5], [.5, 0., .5, 0., 0.], [0., .5, 0., .5, 0.], [0., 0., .5, 0., .5], [.5, 0., 0., .5, 0.] ]) assert_array_almost_equal(msm.stationary_distribution, [1. / 5] * 5) assert_equal(msm.n_eigenvalues, 5) assert_equal(msm.sparse, False) msm = msm.submodel(np.array([3, 4])) # states 3 and 4 correspond to symbols 4 and 6 assert_equal(msm.reversible, True) assert_equal(msm.n_states, 2) assert_equal(msm.lagtime, 1) assert_array_almost_equal(msm.transition_matrix, [[0, 1.], [1., 0]]) assert_array_almost_equal(msm.stationary_distribution, [0.5, 0.5]) assert_equal(msm.n_eigenvalues, 2) assert_equal(msm.sparse, False) assert_equal(msm.count_model.state_symbols, [4, 6])