Ejemplo n.º 1
0
    def setUp(self):
        p = np.zeros(10)
        q = np.zeros(10)
        p[0:-1] = 0.5
        q[1:] = 0.5
        p[4] = 0.01
        q[6] = 0.1

        self.A = [0, 1]
        self.B = [8, 9]
        self.a = 1
        self.b = 8

        self.bdc = birth_death_chain(q, p)
        self.T = self.bdc.transition_matrix

        """Compute mu, qminus, qplus in constructor"""
        self.tpt = reactive_flux(self.T, self.A, self.B)

        """Use precomputed mu, qminus, qplus"""
        self.mu = self.bdc.stationary_distribution
        self.qminus = self.bdc.committor_backward(self.a, self.b)
        self.qplus = self.bdc.committor_forward(self.a, self.b)
        self.tpt_fast = reactive_flux(self.T, self.A, self.B, stationary_distribution=self.mu,
                                      qminus=self.qminus, qplus=self.qplus)
Ejemplo n.º 2
0
 def test_invalid_inputs(self):
     with np.testing.assert_raises(ValueError):
         self.P.reactive_flux(source_states=[], target_states=[1])
     with np.testing.assert_raises(ValueError):
         self.P.reactive_flux(source_states=[1], target_states=[])
     with np.testing.assert_raises(ValueError):
         self.P.reactive_flux(source_states=np.arange(self.P.n_states + 1),
                              target_states=[0])
     with np.testing.assert_raises(ValueError):
         self.P.reactive_flux(source_states=[0],
                              target_states=np.arange(self.P.n_states + 1))
     with np.testing.assert_raises(ValueError):
         reactive_flux(np.array([[.5, .6], [.3, .7]]), [0],
                       [1])  # not a transition matrix
Ejemplo n.º 3
0
def test_with_almost_converged_stat_dist(sparse_mode):
    """ test for https://github.com/markovmodel/msmtools/issues/106 """
    from deeptime.markov.tools.analysis import committor, is_reversible
    from deeptime.markov.tools.flux import flux_matrix, to_netflux
    from deeptime.markov import reactive_flux, ReactiveFlux

    T = np.array([[
        0.2576419223095193, 0.2254214623509954, 0.248270708174756,
        0.2686659071647294
    ],
                  [
                      0.2233847186210225, 0.2130434781715344,
                      0.2793477268264001, 0.284224076381043
                  ],
                  [
                      0.2118717275169231, 0.2405661227681972,
                      0.2943396213976011, 0.2532225283172787
                  ],
                  [
                      0.2328617711043517, 0.2485926610067547,
                      0.2571819311236834, 0.2613636367652102
                  ]])
    if sparse_mode:
        T = csr_matrix(T)
    mu = np.array([
        0.2306979668517676, 0.2328013892993006, 0.2703312416016573,
        0.2661694022472743
    ])
    assert is_reversible(T)
    np.testing.assert_allclose(T.T.dot(mu).T, mu)
    np.testing.assert_equal(T.T.dot(mu).T, T.T.dot(mu))
    A = [0]
    B = [1]

    # forward committor
    qplus = committor(T, A, B, forward=True, mu=mu)
    # backward committor
    if is_reversible(T, mu=mu):
        qminus = 1.0 - qplus
    else:
        qminus = committor(T, A, B, forward=False, mu=mu)

    tpt_obj = reactive_flux(T, A, B)
    tpt_obj.major_flux(1.0)
    # gross flux
    grossflux = flux_matrix(T, mu, qminus, qplus, netflux=False)
    # net flux
    netflux = to_netflux(grossflux)

    F = ReactiveFlux(A,
                     B,
                     netflux,
                     stationary_distribution=mu,
                     qminus=qminus,
                     qplus=qplus,
                     gross_flux=grossflux)
    F.pathways(1.0)
Ejemplo n.º 4
0
    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_95percent = [[0, 1, 4], [0, 2, 4]]
        self.ref_pathfluxes_95percent = np.array(
            [0.00720338983051, 0.00308716707022])
        self.ref_majorflux_95percent = 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.]])

        # Testing:
        self.tpt1 = reactive_flux(self.P, 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.stationary_distribution(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.stationary_distribution(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.]])

        # Testing
        self.tpt2 = reactive_flux(self.P2, self.A2, self.B2)