Esempio n. 1
0
    def testInputArrays(self):
        """ this is not supported, has to be list of ndarrays """
        dtrajs = np.array([[0, 1, 2, 0, 0, 1, 2, 1, 0],
                           [0, 1, 2, 0, 0, 1., 2, 1, 1]])

        with self.assertRaises(ValueError):
            count_matrix(dtrajs, 1)
Esempio n. 2
0
    def count(count_mode: str,
              dtrajs: List[np.ndarray],
              lagtime: int,
              sparse: bool = False):
        r""" Computes a count matrix based on a counting mode, some discrete trajectories, a lagtime, and
        whether to use sparse matrices.

        Parameters
        ----------
        count_mode : str
            The counting mode to use. One of "sample", "sliding", "sliding-effective", and "effective".
            See :meth:`__init__` for a more detailed description.
        dtrajs : array_like or list of array_like
            Discrete trajectories, i.e., a list of arrays which contain non-negative integer values. A single ndarray
            can also be passed, which is then treated as if it was a list with that one ndarray in it.
        lagtime : int
            Distance between two frames in the discretized trajectories under which their potential change of state
            is considered a transition.
        sparse : bool, default=False
            Whether to use sparse matrices or dense matrices. Sparse matrices can make sense when dealing with a lot of
            states.

        Returns
        -------
        count_matrix : (N, N) ndarray or sparse array
            The computed count matrix. Can be ndarray or sparse depending on whether sparse was set to true or false.
            N is the number of encountered states, i.e., :code:`np.max(dtrajs)+1`.

        Example
        -------
        >>> dtrajs = [np.array([0,0,1,1]), np.array([0,0,1])]
        >>> count_matrix = TransitionCountEstimator.count(
        ...     count_mode="sliding", dtrajs=dtrajs, lagtime=1, sparse=False
        ... )
        >>> np.testing.assert_equal(count_matrix, np.array([[2, 2], [0, 1]]))
        """
        if count_mode == 'sliding' or count_mode == 'sliding-effective':
            count_matrix = msmest.count_matrix(dtrajs,
                                               lagtime,
                                               sliding=True,
                                               sparse_return=sparse)
            if count_mode == 'sliding-effective':
                count_matrix /= lagtime
        elif count_mode == 'sample':
            count_matrix = msmest.count_matrix(dtrajs,
                                               lagtime,
                                               sliding=False,
                                               sparse_return=sparse)
        elif count_mode == 'effective':
            count_matrix = msmest.effective_count_matrix(dtrajs, lagtime)
            if not sparse and issparse(count_matrix):
                count_matrix = count_matrix.toarray()
        else:
            raise ValueError('Count mode {} is unknown.'.format(count_mode))
        return count_matrix
Esempio n. 3
0
 def test_blocksplit_dtrajs_sliding(self):
     dtrajs = [
         np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
         np.array([0, 1, 9, 10])
     ]
     for lag in range(1, 10):
         dtrajs_new = blocksplit_trajs(dtrajs, blocksize=lag, sliding=True)
         C1 = count_matrix(dtrajs, lag, sliding=True, nstates=11).toarray()
         C2 = count_matrix(dtrajs_new, lag, sliding=True,
                           nstates=11).toarray()
         assert np.all(C1 == C2)
 def test_singletraj(self):
     # lag 1
     C = count_matrix(self.dtraj_long, 1)
     Ceff = effective_count_matrix(self.dtraj_long, 1)
     assert np.array_equal(Ceff.shape, C.shape)
     assert np.array_equal(C.nonzero(), Ceff.nonzero())
     assert np.all(Ceff.toarray() <= C.toarray())
     # lag 100
     C = count_matrix(self.dtraj_long, 100)
     Ceff = effective_count_matrix(self.dtraj_long, 100)
     assert np.array_equal(Ceff.shape, C.shape)
     assert np.array_equal(C.nonzero(), Ceff.nonzero())
     assert np.all(Ceff.toarray() <= C.toarray())
 def test_multitraj(self):
     dtrajs = [[1, 0, 1, 0, 1, 1, 0, 0, 0, 1], [2], [0, 1, 0, 1]]
     # lag 1
     C = count_matrix(dtrajs, 1)
     Ceff = effective_count_matrix(dtrajs, 1)
     assert np.array_equal(Ceff.shape, C.shape)
     assert np.array_equal(C.nonzero(), Ceff.nonzero())
     assert np.all(Ceff.toarray() <= C.toarray())
     # lag 2
     C = count_matrix(dtrajs, 2)
     Ceff = effective_count_matrix(dtrajs, 2)
     assert np.array_equal(Ceff.shape, C.shape)
     assert np.array_equal(C.nonzero(), Ceff.nonzero())
     assert np.all(Ceff.toarray() <= C.toarray())
Esempio n. 6
0
    def setUp(self):
        """Store state of the rng"""
        self.state = np.random.mtrand.get_state()
        """Reseed the rng to enforce 'deterministic' behavior"""
        np.random.mtrand.seed(42)
        """Meta-stable birth-death chain"""
        b = 2
        q = np.zeros(7)
        p = np.zeros(7)
        q[1:] = 0.5
        p[0:-1] = 0.5
        q[2] = 1.0 - 10**(-b)
        q[4] = 10**(-b)
        p[2] = 10**(-b)
        p[4] = 1.0 - 10**(-b)

        bdc = BirthDeathChain(q, p)
        self.dtraj = bdc.msm.simulate(10000, start=0)
        self.tau = 1
        """Estimate MSM"""
        self.C_MSM = count_matrix(self.dtraj, self.tau, sliding=True)
        self.lcc_MSM = largest_connected_set(self.C_MSM)
        self.Ccc_MSM = largest_connected_submatrix(self.C_MSM,
                                                   lcc=self.lcc_MSM)
        self.mle_rev_max_err = 1E-8
        self.P_MSM = transition_matrix(self.Ccc_MSM,
                                       reversible=True,
                                       maxerr=self.mle_rev_max_err)
        self.mu_MSM = stationary_distribution(self.P_MSM)
        self.k = 3
        self.ts = timescales(self.P_MSM, k=self.k, tau=self.tau)
Esempio n. 7
0
def test_simulate_recover_transition_matrix(msm):
    # test if transition matrix can be reconstructed
    N = 5000
    trajs = msm.simulate(N, seed=42)
    # trajs = msmgen.generate_traj(self.P, N, random_state=self.random_state)
    C = count_matrix(trajs, 1, sparse_return=False)
    T = transition_matrix(C)
    np.testing.assert_allclose(T, msm.transition_matrix, atol=.01)
    def test_multitraj_njobs(self):
        dtrajs = [[1, 0, 1, 0, 1, 1, 0, 0, 0, 1], [2], [0, 1, 0, 1]]
        # lag 1
        C = count_matrix(dtrajs, 1)
        Ceff = effective_count_matrix(dtrajs, 1, n_jobs=1)
        assert np.array_equal(Ceff.shape, C.shape)
        assert np.array_equal(C.nonzero(), Ceff.nonzero())
        assert np.all(Ceff.toarray() <= C.toarray())

        Ceff2 = effective_count_matrix(dtrajs, 1, n_jobs=2)
        np.testing.assert_equal(Ceff2.toarray(), Ceff.toarray())
        np.testing.assert_allclose(Ceff2.toarray(), Ceff.toarray())
        assert np.array_equal(Ceff2.shape, C.shape)
        assert np.array_equal(C.nonzero(), Ceff2.nonzero())
        assert np.all(Ceff2.toarray() <= C.toarray())

        # lag 2
        C = count_matrix(dtrajs, 2)
        Ceff2 = effective_count_matrix(dtrajs, 2)
        assert np.array_equal(Ceff2.shape, C.shape)
        assert np.array_equal(C.nonzero(), Ceff2.nonzero())
        assert np.all(Ceff2.toarray() <= C.toarray())
Esempio n. 9
0
def test_simulate(msm):
    N = 1000
    traj = msm.simulate(n_steps=N, start=0, seed=42)

    # test shapes and sizes
    assert traj.size == N
    assert traj.min() >= 0
    assert traj.max() <= 1

    # test statistics of transition matrix
    C = count_matrix(traj, 1)
    Pest = transition_matrix(C)
    assert np.max(np.abs(Pest - msm.transition_matrix)) < 0.025
Esempio n. 10
0
    def setUpClass(cls):
        n_states = 50
        traj_length = 10000

        dtraj = np.zeros(traj_length, dtype=int)
        dtraj[::2] = np.random.randint(1,
                                       n_states,
                                       size=(traj_length - 1) // 2 + 1)

        c = count_matrix(dtraj, lag=1)
        while not is_connected(c, directed=True):
            dtraj = np.zeros(traj_length, dtype=int)
            dtraj[::2] = np.random.randint(1,
                                           n_states,
                                           size=(traj_length - 1) // 2 + 1)
            c = count_matrix(dtraj, lag=1)

        #state_counts = np.bincount(dtraj)[:,np.newaxis]
        ttraj = np.zeros(traj_length, dtype=int)
        btraj = np.zeros((traj_length, 1))
        cls.tram_trajs = ([ttraj], [dtraj], [btraj])

        cls.T_ref = transition_matrix(c, reversible=True).toarray()
Esempio n. 11
0
def test_birth_death_chain(fixed_seed, sparse):
    """Meta-stable birth-death chain"""
    b = 2
    q = np.zeros(7)
    p = np.zeros(7)
    q[1:] = 0.5
    p[0:-1] = 0.5
    q[2] = 1.0 - 10**(-b)
    q[4] = 10**(-b)
    p[2] = 10**(-b)
    p[4] = 1.0 - 10**(-b)

    bdc = deeptime.data.birth_death_chain(q, p)
    dtraj = bdc.msm.simulate(10000, start=0)
    tau = 1

    reference_count_matrix = msmest.count_matrix(dtraj, tau, sliding=True)
    reference_largest_connected_component = msmest.largest_connected_set(
        reference_count_matrix)
    reference_lcs = msmest.largest_connected_submatrix(
        reference_count_matrix, lcc=reference_largest_connected_component)
    reference_msm = msmest.transition_matrix(reference_lcs,
                                             reversible=True,
                                             maxerr=1e-8)
    reference_statdist = msmana.stationary_distribution(reference_msm)
    k = 3
    reference_timescales = msmana.timescales(reference_msm, k=k, tau=tau)

    msm = estimate_markov_model(dtraj, tau, sparse=sparse)
    assert_equal(tau, msm.count_model.lagtime)
    assert_array_equal(reference_largest_connected_component,
                       msm.count_model.connected_sets()[0])
    assert_(scipy.sparse.issparse(msm.count_model.count_matrix) == sparse)
    assert_(scipy.sparse.issparse(msm.transition_matrix) == sparse)
    if sparse:
        count_matrix = msm.count_model.count_matrix.toarray()
        transition_matrix = msm.transition_matrix.toarray()
    else:
        count_matrix = msm.count_model.count_matrix
        transition_matrix = msm.transition_matrix
    assert_array_almost_equal(reference_lcs.toarray(), count_matrix)
    assert_array_almost_equal(reference_count_matrix.toarray(), count_matrix)
    assert_array_almost_equal(reference_msm.toarray(), transition_matrix)
    assert_array_almost_equal(reference_statdist, msm.stationary_distribution)
    assert_array_almost_equal(reference_timescales[1:], msm.timescales(k - 1))
Esempio n. 12
0
    def count_lagged(self,
                     lag,
                     count_mode='sliding',
                     mincount_connectivity='1/n',
                     show_progress=True,
                     n_jobs=None,
                     name='',
                     core_set=None,
                     milestoning_method='last_core'):
        r""" Counts transitions at given lag time

        Parameters
        ----------
        lag : int
            lagtime in trajectory steps

        count_mode : str, optional, default='sliding'
            mode to obtain count matrices from discrete trajectories. Should be one of:

            * 'sliding' : A trajectory of length T will have :math:`T-\tau` counts
              at time indexes
              .. math:: (0 \rightarray \tau), (1 \rightarray \tau+1), ..., (T-\tau-1 \rightarray T-1)

            * 'effective' : Uses an estimate of the transition counts that are
              statistically uncorrelated. Recommended when used with a
              Bayesian MSM.

            * 'sample' : A trajectory of length T will have :math:`T / \tau` counts
              at time indexes
              .. math:: (0 \rightarray \tau), (\tau \rightarray 2 \tau), ..., (((T/tau)-1) \tau \rightarray T)

        show_progress: bool, default=True
            show the progress for the expensive effective count mode computation.

        n_jobs: int or None

        """
        # store lag time
        self._lag = lag

        # Compute count matrix
        count_mode = count_mode.lower()
        if core_set is not None and count_mode in ('sliding', 'sample'):
            if milestoning_method == 'last_core':

                # assign -1 frames to last visited core
                for d in self._dtrajs:
                    assert d[0] != -1
                    while -1 in d:
                        mask = (d == -1)
                        d[mask] = d[np.roll(mask, -1)]
                self._C = count_matrix(self._dtrajs,
                                       lag,
                                       sliding=count_mode == 'sliding')

            else:
                raise NotImplementedError(
                    'Milestoning method {} not implemented.'.format(
                        milestoning_method))
        else:
            cm = TransitionCountEstimator(lag,
                                          count_mode=count_mode,
                                          sparse=True).fit(
                                              self._dtrajs).fetch_model()
            self._C = cm.count_matrix

        # store mincount_connectivity
        if mincount_connectivity == '1/n':
            mincount_connectivity = 1.0 / np.shape(self._C)[0]
        self._mincount_connectivity = mincount_connectivity

        self._count_model_full = TransitionCountModel(self._C)
        self._connected_sets = self._count_model_full.connected_sets(
            connectivity_threshold=self._mincount_connectivity)
        self._count_model = self._count_model_full.submodel_largest(
            connectivity_threshold=self._mincount_connectivity)

        # set sizes and count matrices on reversibly connected sets
        self._connected_set_sizes = np.array(
            (len(cs) for cs in self._connected_sets))
        # largest connected set
        self._lcs = self._connected_sets[0]

        # if lcs has no counts, make lcs empty
        if submatrix(self._C, self._lcs).sum() == 0:
            self._lcs = np.array([], dtype=int)

        # mapping from full to lcs
        self._full2lcs = -1 * np.ones((self._nstates), dtype=int)
        self._full2lcs[self._lcs] = np.arange(len(self._lcs))

        # remember that this function was called
        self._counted_at_lag = True
Esempio n. 13
0
    def setUpClass(cls, init_trammbar):
        # (1-D FEL) TRAM unit test
        # (1) define mu(k,x) on a fine grid, k=0 is defined as unbiased
        # (2) define coarse grid (of Markov states) on x
        # (3) -> compute pi from the grid definition and mu. Compute the conditional mu_i^k
        # (4) -> from pi, generate transtion matrix
        # (5) -> run two-level stochastic process to generate the bias trajectories
        # (6) optional if init_trammbar=True, add a global equilibrium trajectory for TRAMMBAR

        cls.test_trammbar = init_trammbar

        # (1)
        if init_trammbar:
            therm_states_local = [0, 1, 3]  # 2 has only equilibrium data
            therm_states_global = [1, 2]  # 1 has both kinds of data
        else:
            therm_states_local = [0, 1, 2]
            therm_states_global = []
        therm_states = list(set(therm_states_local).union(therm_states_global))
        n_therm_states_local = len(therm_states_local)
        n_therm_states_global = len(therm_states_global)
        n_therm_states_total = max(therm_states) + 1
        n_conf_states = 3
        n_micro_states = 50
        traj_length = 30000

        mu = np.zeros((n_therm_states_total, n_micro_states))
        for k in therm_states:
            mu[k, :] = np.random.rand(n_micro_states) * 0.8 + 0.2
            if k > 0:
                mu[k, :] *= (np.random.rand() * 0.8 + 0.2)
        energy = -np.log(mu)
        # (2)
        chi = np.zeros((n_micro_states, n_conf_states))  # (crisp)
        for i in range(n_conf_states):
            chi[n_micro_states * i // n_conf_states:n_micro_states * (i + 1) //
                n_conf_states, i] = 1
        assert np.allclose(chi.sum(axis=1), np.ones(n_micro_states))
        # (3)
        #             k  x  i                 k           x  i
        mu_joint = mu[:, :, np.newaxis] * chi[np.newaxis, :, :]
        assert np.allclose(mu_joint.sum(axis=2), mu)
        z = mu_joint.sum(axis=1)
        pi = z / z.sum(axis=1)[:, np.newaxis]
        assert np.allclose(pi.sum(axis=1), np.ones(n_therm_states_total))
        mu_conditional = mu_joint / z[:, np.newaxis, :]
        assert np.allclose(mu_conditional.sum(axis=1),
                           np.ones((n_therm_states_total, n_conf_states)))
        # (4)
        T = np.zeros((n_therm_states_total, n_conf_states, n_conf_states))
        for k in therm_states:
            T[k, :, :] = T_matrix(-np.log(pi[k, :]))
            assert np.allclose(T[k, :, :].sum(axis=1), np.ones(n_conf_states))
        # (5)
        ttrajs = []
        dtrajs = []
        btrajs = []
        xes = np.zeros(n_therm_states_local * traj_length, dtype=int)
        C = np.zeros(
            (max(therm_states_local) + 1, n_conf_states, n_conf_states),
            dtype=int)
        h = 0
        for k in therm_states_local:
            ttrajs.append(k * np.ones(traj_length, dtype=int))
            dtrajs.append(
                generate_simple_trajectory(T[k, :, :], traj_length, 0))
            C[k, :, :] = count_matrix(dtrajs[-1], lag=1).toarray()
            btraj = np.zeros((traj_length, n_therm_states_total))
            btrajs.append(btraj)
            for t, i in enumerate(dtrajs[-1]):
                x = tower_sample(mu_conditional[k, :, i])
                assert mu_conditional[k, x, i] > 0
                xes[h * traj_length + t] = x
                btraj[t, :] = energy[:,
                                     x] - energy[0,
                                                 x]  # define k=0 as "unbiased"
            h += 1

        # (6)
        if init_trammbar:
            eq_ttrajs = []
            eq_dtrajs = []
            eq_btrajs = []
            eq_xes = np.zeros(n_therm_states_global * traj_length, dtype=int)
            h = 0
            for k in therm_states_global:
                eq_ttrajs.append(k * np.ones(traj_length, dtype=int))
                eq_dtrajs.append(
                    global_equilibrium_samples(pi[k, :], traj_length))
                eq_btraj = np.zeros((traj_length, n_therm_states_total))
                eq_btrajs.append(eq_btraj)
                for t, i in enumerate(eq_dtrajs[-1]):
                    x = tower_sample(mu_conditional[k, :, i])
                    assert mu_conditional[k, x, i] > 0
                    eq_xes[h * traj_length + t] = x
                    eq_btraj[t, :] = energy[:, x] - energy[0, x]
                h += 1

        cls.n_conf_states = n_conf_states
        cls.therm_states = therm_states
        cls.therm_states_global = therm_states_global
        cls.therm_states_local = therm_states_local
        cls.n_therm_states_local = n_therm_states_local
        cls.n_therm_states_global = n_therm_states_global
        cls.n_micro_states = n_micro_states
        cls.ttrajs = ttrajs
        cls.dtrajs = dtrajs
        cls.btrajs = btrajs
        if not init_trammbar:
            cls.eq = None
        else:
            cls.eq = [False
                      ] * n_therm_states_local + [True] * n_therm_states_global
            cls.eq_ttrajs = eq_ttrajs
            cls.eq_dtrajs = eq_dtrajs
            cls.eq_btrajs = eq_btrajs
            cls.eq_xes = eq_xes
        cls.z = z
        cls.T = T
        cls.C = C
        cls.energy = energy
        cls.mu = mu
        cls.xes = xes
Esempio n. 14
0
    def score(self, dtrajs, score_method=None, score_k=None):
        """ Scores the MSM using the dtrajs using the variational approach for Markov processes [1]_ [2]_

        Currently only implemented using dense matrices - will be slow for large state spaces.

        Parameters
        ----------
        dtrajs : list of arrays
            test data (discrete trajectories).
        score_method : str
            Overwrite scoring method if desired. If `None`, the estimators scoring
            method will be used. See __init__ for documentation.
        score_k : int or None
            Overwrite scoring rank if desired. If `None`, the estimators scoring
            rank will be used. See __init__ for documentation.
        score_method : str, optional, default='VAMP2'
            Overwrite scoring method to be used if desired. If `None`, the estimators scoring
            method will be used.
            Available scores are based on the variational approach for Markov processes [1]_ [2]_ :

            *  'VAMP1'  Sum of singular values of the symmetrized transition matrix [2]_ .
                        If the MSM is reversible, this is equal to the sum of transition
                        matrix eigenvalues, also called Rayleigh quotient [1]_ [3]_ .
            *  'VAMP2'  Sum of squared singular values of the symmetrized transition matrix [2]_ .
                        If the MSM is reversible, this is equal to the kinetic variance [4]_ .

        score_k : int or None
            The maximum number of eigenvalues or singular values used in the
            score. If set to None, all available eigenvalues will be used.

        References
        ----------
        .. [1] Noe, F. and F. Nueske: A variational approach to modeling slow processes
            in stochastic dynamical systems. SIAM Multiscale Model. Simul. 11, 635-655 (2013).
        .. [2] Wu, H and F. Noe: Variational approach for learning Markov processes
            from time series data (in preparation)
        .. [3] McGibbon, R and V. S. Pande: Variational cross-validation of slow
            dynamical modes in molecular kinetics, J. Chem. Phys. 142, 124105 (2015)
        .. [4] Noe, F. and C. Clementi: Kinetic distance and kinetic maps from molecular
            dynamics simulation. J. Chem. Theory Comput. 11, 5002-5011 (2015)

        """
        dtrajs = ensure_dtraj_list(dtrajs)  # ensure format

        # reset estimator data if needed
        if score_method is not None:
            self.score_method = score_method
        if score_k is not None:
            self.score_k = score_k

        # determine actual scoring rank
        if self.score_k is None:
            self.score_k = self.nstates
        if self.score_k > self.nstates:
            self.logger.warning('Requested scoring rank {rank} exceeds number of MSM states. '
                                'Reduced to score_k = {nstates}'.format(rank=self.score_k, nstates=self.nstates))
            self.score_k = self.nstates  # limit to nstates

        # training data
        K = self.transition_matrix  # model
        C0t_train = self.count_matrix_active
        from scipy.sparse import issparse
        if issparse(K):  # can't deal with sparse right now.
            K = K.toarray()
        if issparse(C0t_train):  # can't deal with sparse right now.
            C0t_train = C0t_train.toarray()
        C00_train = _np.diag(C0t_train.sum(axis=1))  # empirical cov
        Ctt_train = _np.diag(C0t_train.sum(axis=0))  # empirical cov

        # test data
        C0t_test_raw = count_matrix(dtrajs, self.lag, sparse_return=False)
        # map to present active set
        map_from = self.active_set[_np.where(self.active_set < C0t_test_raw.shape[0])[0]]
        map_to = _np.arange(len(map_from))
        C0t_test = _np.zeros((self.nstates, self.nstates))
        C0t_test[_np.ix_(map_to, map_to)] = C0t_test_raw[_np.ix_(map_from, map_from)]
        C00_test = _np.diag(C0t_test.sum(axis=1))
        Ctt_test = _np.diag(C0t_test.sum(axis=0))

        # score
        from pyemma.util.metrics import vamp_score
        return vamp_score(K, C00_train, C0t_train, Ctt_train, C00_test, C0t_test, Ctt_test,
                          k=self.score_k, score=self.score_method)
Esempio n. 15
0
 def testInputList(self):
     dtrajs = [0, 1, 2, 0, 0, 1, 2, 1, 0]
     count_matrix(dtrajs, 1)
Esempio n. 16
0
 def testInputNestedListsDiffSize(self):
     dtrajs = [[0, 1, 2, 0, 0, 1, 2, 1, 0],
               [0, 1, 0, 1, 1, 1, 1, 0, 2, 1, 2, 1]]
     count_matrix(dtrajs, 1)
Esempio n. 17
0
 def testInputArray(self):
     dtrajs = np.array([0, 1, 2, 0, 0, 1, 2, 1, 0])
     count_matrix(dtrajs, 1)
Esempio n. 18
0
def cktest_resource():
    """Reseed the rng to enforce 'deterministic' behavior"""
    rnd_state = np.random.mtrand.get_state()
    np.random.mtrand.seed(42)
    """Meta-stable birth-death chain"""
    b = 2
    q = np.zeros(7)
    p = np.zeros(7)
    q[1:] = 0.5
    p[0:-1] = 0.5
    q[2] = 1.0 - 10**(-b)
    q[4] = 10**(-b)
    p[2] = 10**(-b)
    p[4] = 1.0 - 10**(-b)

    bdc = BirthDeathChain(q, p)
    dtraj = bdc.msm.simulate(10000, start=0)
    tau = 1
    """Estimate MSM"""
    MSM = estimate_markov_model(dtraj, tau)
    P_MSM = MSM.transition_matrix
    mu_MSM = MSM.stationary_distribution
    """Meta-stable sets"""
    A = [0, 1, 2]
    B = [4, 5, 6]

    w_MSM = np.zeros((2, mu_MSM.shape[0]))
    w_MSM[0, A] = mu_MSM[A] / mu_MSM[A].sum()
    w_MSM[1, B] = mu_MSM[B] / mu_MSM[B].sum()

    K = 10
    P_MSM_dense = P_MSM

    p_MSM = np.zeros((K, 2))
    w_MSM_k = 1.0 * w_MSM
    for k in range(1, K):
        w_MSM_k = np.dot(w_MSM_k, P_MSM_dense)
        p_MSM[k, 0] = w_MSM_k[0, A].sum()
        p_MSM[k, 1] = w_MSM_k[1, B].sum()
    """Assume that sets are equal, A(\tau)=A(k \tau) for all k"""
    w_MD = 1.0 * w_MSM
    p_MD = np.zeros((K, 2))
    eps_MD = np.zeros((K, 2))
    p_MSM[0, :] = 1.0
    p_MD[0, :] = 1.0
    eps_MD[0, :] = 0.0
    for k in range(1, K):
        """Build MSM at lagtime k*tau"""
        C_MD = count_matrix(dtraj, k * tau, sliding=True) / (k * tau)
        lcc_MD = largest_connected_set(C_MD)
        Ccc_MD = largest_connected_submatrix(C_MD, lcc=lcc_MD)
        c_MD = Ccc_MD.sum(axis=1)
        P_MD = transition_matrix(Ccc_MD).toarray()
        w_MD_k = np.dot(w_MD, P_MD)
        """Set A"""
        prob_MD = w_MD_k[0, A].sum()
        c = c_MD[A].sum()
        p_MD[k, 0] = prob_MD
        eps_MD[k, 0] = np.sqrt(k * (prob_MD - prob_MD**2) / c)
        """Set B"""
        prob_MD = w_MD_k[1, B].sum()
        c = c_MD[B].sum()
        p_MD[k, 1] = prob_MD
        eps_MD[k, 1] = np.sqrt(k * (prob_MD - prob_MD**2) / c)
    """Input"""
    yield MSM, p_MSM, p_MD
    np.random.mtrand.set_state(rnd_state)
Esempio n. 19
0
    def test_nstates_keyword(self):
        C = count_matrix(self.dtrajs_short, 1, sliding=False, nstates=10)
        self.assertTrue(C.shape == (10, 10))

        with self.assertRaises(ValueError):
            C = count_matrix(self.dtrajs_short, 1, sliding=False, nstates=1)
Esempio n. 20
0
    def test_count_matrix_mult(self):
        """Small test cases"""
        C = count_matrix(self.dtrajs_short, 1, sliding=False).toarray()
        assert_allclose(C, self.B1_lag)

        C = count_matrix(self.dtrajs_short, 2, sliding=False).toarray()
        assert_allclose(C, self.B2_lag)

        C = count_matrix(self.dtrajs_short, 3, sliding=False).toarray()
        assert_allclose(C, self.B3_lag)

        C = count_matrix(self.dtrajs_short, 1).toarray()
        assert_allclose(C, self.B1_sliding)

        C = count_matrix(self.dtrajs_short, 2).toarray()
        assert_allclose(C, self.B2_sliding)

        C = count_matrix(self.dtrajs_short, 3).toarray()
        assert_allclose(C, self.B3_sliding)
        """Larger test cases"""
        C = count_matrix(self.dtrajs_long, 1, sliding=False).toarray()
        assert_allclose(C, self.C1_lag)

        C = count_matrix(self.dtrajs_long, 7, sliding=False).toarray()
        assert_allclose(C, self.C7_lag)

        C = count_matrix(self.dtrajs_long, 13, sliding=False).toarray()
        assert_allclose(C, self.C13_lag)

        C = count_matrix(self.dtrajs_long, 1).toarray()
        assert_allclose(C, self.C1_sliding)

        C = count_matrix(self.dtrajs_long, 7).toarray()
        assert_allclose(C, self.C7_sliding)

        C = count_matrix(self.dtrajs_long, 13).toarray()
        assert_allclose(C, self.C13_sliding)
        """Test raising of value error if lag greater than trajectory length"""
        with self.assertRaises(ValueError):
            C = count_matrix(self.dtrajs_short, 10)
Esempio n. 21
0
 def testInputFloat(self):
     dtraj_with_floats = [0.0, 1, 0, 2, 3, 1, 0.1]
     # dtraj_int = [0, 1, 0, 2, 3, 1, 0]
     with self.assertRaises(ValueError):
         count_matrix(dtraj_with_floats, 1)