def tpt_scenario(sparse_mode):
    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]])
    if sparse_mode:
        P = sparse.csr_matrix(P)
    msm = MarkovStateModel(P)
    tpt = msm.reactive_flux([0], [4])
    return msm, tpt
def test_coarse_grain(sparse_mode):
    # 16-state toy system
    P_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 = stationary_distribution(P_nonrev)
    # make reversible
    C = np.dot(np.diag(pstat2_nonrev), P_nonrev)
    Csym = C + C.T
    P = Csym / np.sum(Csym, axis=1)[:, np.newaxis]
    if sparse_mode:
        P = sparse.csr_matrix(P)
    msm = MarkovStateModel(P)
    tpt = msm.reactive_flux([0, 4], [11, 15])
    coarse_sets = [
        [2, 3, 6, 7],
        [10, 11, 14, 15],
        [0, 1, 4, 5],
        [8, 9, 12, 13],
    ]
    tpt_sets, cgRF = tpt.coarse_grain(coarse_sets)
    assert_equal(
        tpt_sets,
        [{0, 4}, {2, 3, 6, 7}, {10, 14}, {1, 5}, {8, 9, 12, 13}, {11, 15}])
    assert_equal(cgRF.source_states, [0])
    assert_equal(cgRF.intermediate_states, [1, 2, 3, 4])
    assert_equal(cgRF.target_states, [5])
    assert_array_almost_equal(
        cgRF.stationary_distribution,
        np.array([
            0.15995388, 0.18360442, 0.12990937, 0.11002342, 0.31928127,
            0.09722765
        ]))
    assert_array_almost_equal(
        cgRF.forward_committor,
        np.array([0., 0.56060272, 0.73052426, 0.19770537, 0.36514272, 1.]))
    assert_array_almost_equal(
        cgRF.backward_committor,
        np.array([1., 0.43939728, 0.26947574, 0.80229463, 0.63485728, 0.]))
    assert_array_almost_equal(
        _to_dense(cgRF.net_flux),
        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.]]))
    assert_array_almost_equal(
        _to_dense(cgRF.gross_flux),
        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.]]),
        decimal=6)
 def test_dt_model(self):
     C = TransitionCountModel(np.array([[0.1, 0.9], [0.9, 0.1]]), lagtime=5)
     msm = MarkovStateModel(C.count_matrix, count_model=C)
     tpt = msm.reactive_flux([0], [1])
     np.testing.assert_equal(msm.lagtime, 5)