Example #1
0
    def setUp(self):
        self.k = 4

        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.bdc = birth_death_chain(q, p)

        self.mu = self.bdc.stationary_distribution
        self.T = self.bdc.transition_matrix_sparse
        """Test matrix-vector product against spectral decomposition"""
        R, D, L = rdl_decomposition(self.T, k=self.k)
        self.L = L
        self.R = R
        self.ts = timescales(self.T, k=self.k)
        self.times = np.array([1, 5, 10, 20, 100])

        ev = np.diagonal(D)
        self.ev_t = ev[np.newaxis, :]**self.times[:, np.newaxis]
        """Observable"""
        obs1 = np.zeros(10)
        obs1[0] = 1
        obs1[1] = 1
        self.obs = obs1
        """Initial distribution"""
        w0 = np.zeros(10)
        w0[0:4] = 0.25
        self.p0 = w0
Example #2
0
 def test_4_2(self):
     t4 = timescales(self.P4)[1]
     lags = [int(t4)]
     its = msm.timescales_msm([self.dtraj4_2], lags=lags, n_jobs=1)
     est = its.timescales[0]
     np.testing.assert_array_less(est, t4 + 20.0)
     np.testing.assert_array_less(t4 - 20.0, est)
Example #3
0
    def setUp(self):
        self.k = 4

        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.bdc = birth_death_chain(q, p)

        self.mu = self.bdc.stationary_distribution
        self.T = self.bdc.transition_matrix_sparse
        R, D, L = rdl_decomposition(self.T, k=self.k)
        self.L = L
        self.R = R
        self.ts = timescales(self.T, k=self.k)
        self.times = np.array([1, 5, 10, 20, 100])

        ev = np.diagonal(D)
        self.ev_t = ev[np.newaxis, :]**self.times[:, np.newaxis]

        obs1 = np.zeros(10)
        obs1[0] = 1
        obs1[1] = 1
        obs2 = np.zeros(10)
        obs2[8] = 1
        obs2[9] = 1

        self.obs1 = obs1
        self.obs2 = obs2
        self.one_vec = np.ones(10)
Example #4
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)
Example #5
0
 def test_2_parallel(self):
     t2 = timescales(self.P2)[1]
     lags = [1, 2, 3, 4, 5]
     its = timescales_msm([self.dtraj2], lags=lags, n_jobs=1)
     est = its.timescales[0]
     np.testing.assert_array_less(est, t2 + 2.0)
     np.testing.assert_array_less(t2 - 2.0, est)
Example #6
0
 def test_timescales(self):
     P = self.bdc.transition_matrix
     ev = eigvals(P)
     """Sort with decreasing magnitude"""
     ev = ev[np.argsort(np.abs(ev))[::-1]]
     ts = -1.0 / np.log(np.abs(ev))
     """k=None"""
     tsn = timescales(P)
     assert_allclose(ts[1:], tsn[1:])
     """k is not None"""
     tsn = timescales(P, k=self.k)
     assert_allclose(ts[1:self.k], tsn[1:])
     """tau=7"""
     """k=None"""
     tsn = timescales(P, tau=7)
     assert_allclose(7 * ts[1:], tsn[1:])
     """k is not None"""
     tsn = timescales(P, k=self.k, tau=7)
     assert_allclose(7 * ts[1:self.k], tsn[1:])
def test_timescales_inf():
    """Multiple eigenvalues of magnitude one, eigenvalues with non-zero imaginary part"""
    W = np.array([[0, 1], [1, 0]])

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('ignore')
        warnings.simplefilter('always', category=SpectralWarning)
        tsn = timescales(W)
        assert_allclose(tsn, np.array([np.inf, np.inf]))
        assert issubclass(w[-1].category, SpectralWarning)
Example #8
0
 def test_timescales(self):
     P_dense = self.bdc.transition_matrix
     P = self.bdc.transition_matrix_sparse
     ev = eigvals(P_dense)
     """Sort with decreasing magnitude"""
     ev = ev[np.argsort(np.abs(ev))[::-1]]
     ts = -1.0 / np.log(np.abs(ev))
     """k=None"""
     with self.assertRaises(ValueError):
         tsn = timescales(P)
     """k is not None"""
     tsn = timescales(P, k=self.k)
     assert_allclose(ts[1:self.k], tsn[1:])
     """k is not None, ncv is not None"""
     tsn = timescales(P, k=self.k, ncv=self.ncv)
     assert_allclose(ts[1:self.k], tsn[1:])
     """tau=7"""
     """k is not None"""
     tsn = timescales(P, k=self.k, tau=7)
     assert_allclose(7 * ts[1:self.k], tsn[1:])
Example #9
0
    def test_timescales_2(self):
        """Eigenvalues with non-zero imaginary part"""
        ts = np.array([np.inf, 0.971044, 0.971044])

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('ignore')
            warnings.simplefilter('always',
                                  category=ImaginaryEigenValueWarning)
            tsn = timescales(0.5 * self.T + 0.5 * self.P)
            assert_allclose(tsn, ts)
            assert issubclass(w[-1].category, ImaginaryEigenValueWarning)
Example #10
0
    def test_timescales_rev(self):
        P_dense = self.bdc.transition_matrix
        P = self.bdc.transition_matrix
        mu = self.bdc.stationary_distribution
        ev = eigvals(P_dense)
        """Sort with decreasing magnitude"""
        ev = ev[np.argsort(np.abs(ev))[::-1]]
        ts = -1.0 / np.log(np.abs(ev))

        tsn = timescales(P, reversible=True)
        assert_allclose(ts[1:], tsn[1:])
        """k is not None"""
        tsn = timescales(P, k=self.k, reversible=True)
        assert_allclose(ts[1:self.k], tsn[1:])
        """k is not None, mu is not None"""
        tsn = timescales(P, k=self.k, reversible=True, mu=mu)
        assert_allclose(ts[1:self.k], tsn[1:])
        """tau=7"""
        """k is not None"""
        tsn = timescales(P, k=self.k, tau=7, reversible=True)
        assert_allclose(7 * ts[1:self.k], tsn[1:])
Example #11
0
def test_timescales(scenario, ncv_values, tau, statdist, reversible):
    k, bdc = scenario
    P = bdc.transition_matrix
    mu = bdc.stationary_distribution
    ev = eigvals(P)
    """Sort with decreasing magnitude"""
    ev = ev[np.argsort(np.abs(ev))[::-1]]
    ts = -1.0 / np.log(np.abs(ev))

    with assert_raises(ValueError) if bdc.sparse and k is None else nullcontext():
        tsn = timescales(P, k=k, tau=tau, mu=mu if statdist else None, reversible=reversible)
        assert_allclose(tau * ts[1:k], tsn[1:k])
Example #12
0
def test_timescales_inf2():
    """Eigenvalues with non-zero imaginary part"""
    ts = np.array([np.inf, 0.971044, 0.971044])

    T = np.array([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 0.0]])
    P = np.array([[0.9, 0.1, 0.0], [0.5, 0.0, 0.5], [0.0, 0.1, 0.9]])

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('ignore')
        warnings.simplefilter('always', category=ImaginaryEigenValueWarning)
        tsn = timescales(0.5 * T + 0.5 * P)
        assert_allclose(tsn, ts)
        assert issubclass(w[-1].category, ImaginaryEigenValueWarning)
Example #13
0
def test_fingerprint_correlation(fingerprints_data, observables):
    obs1, obs2 = observables
    T = fingerprints_data.transition_matrix
    ts = timescales(T, k=4 if fingerprints_data.sparse else None)
    R, D, L = rdl_decomposition(T, k=4 if fingerprints_data.sparse else None)
    mu = fingerprints_data.stationary_distribution
    tau = 7.5
    if not fingerprints_data.sparse:
        """k=None, tau=1"""
        acorr_amp = np.dot(mu * obs1, R) * np.dot(L, obs1)
        tsn, acorr_ampn = fingerprint_correlation(T, obs1)
        assert_allclose(tsn, ts)
        assert_allclose(acorr_ampn, acorr_amp)
        """k=None, tau=7.5"""
        tau = tau
        tsn, acorr_ampn = fingerprint_correlation(T, obs1, tau=tau)
        assert_allclose(tsn, tau * ts)
        assert_allclose(acorr_ampn, acorr_amp)
    """k=4, tau=1"""
    k = 4
    acorr_amp = np.dot(mu * obs1, R[:, 0:k]) * np.dot(L[0:k, :], obs1)
    tsn, acorr_ampn = fingerprint_correlation(T, obs1, k=k)
    assert_allclose(tsn, ts[0:k])
    assert_allclose(acorr_ampn, acorr_amp)
    """k=4, tau=7.5"""
    tau = tau
    tsn, acorr_ampn = fingerprint_correlation(T, obs1, k=k, tau=tau)
    assert_allclose(tsn, tau * ts[0:k])
    assert_allclose(acorr_ampn, acorr_amp)
    """Cross-correlation"""

    if not fingerprints_data.sparse:
        """k=None, tau=1"""
        corr_amp = np.dot(mu * obs1, R) * np.dot(L, obs2)
        tsn, corr_ampn = fingerprint_correlation(T, obs1, obs2=obs2)
        assert_allclose(tsn, ts)
        assert_allclose(corr_ampn, corr_amp)
        """k=None, tau=7.5"""
        tau = tau
        tsn, corr_ampn = fingerprint_correlation(T, obs1, obs2=obs2, tau=tau)
        assert_allclose(tsn, tau * ts)
        assert_allclose(corr_ampn, corr_amp)
    """k=4, tau=1"""
    corr_amp = np.dot(mu * obs1, R[:, 0:k]) * np.dot(L[0:k, :], obs2)
    tsn, corr_ampn = fingerprint_correlation(T, obs1, obs2=obs2, k=k)
    assert_allclose(tsn, ts[0:k])
    assert_allclose(corr_ampn, corr_amp)
    """k=4, tau=7.5"""
    tsn, corr_ampn = fingerprint_correlation(T, obs1, obs2=obs2, k=k, tau=tau)
    assert_allclose(tsn, tau * ts[0:k])
    assert_allclose(corr_ampn, corr_amp)
Example #14
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))
Example #15
0
def test_fingerprint_relaxation(fingerprints_data, observables):
    obs1, obs2 = observables
    T = fingerprints_data.transition_matrix
    ts = timescales(T, k=4 if fingerprints_data.sparse else None)
    R, D, L = rdl_decomposition(T, k=4 if fingerprints_data.sparse else None)
    """Initial vector for relaxation"""
    p0 = np.zeros(10)
    p0[0:4] = 0.25
    if not fingerprints_data.sparse:
        """k=None"""
        relax_amp = np.dot(p0, R) * np.dot(L, obs1)
        tsn, relax_ampn = fingerprint_relaxation(T, p0, obs1)
        assert_allclose(tsn, ts)
        assert_allclose(relax_ampn, relax_amp)
    """k=4"""
    k = 4
    relax_amp = np.dot(p0, R[:, 0:k]) * np.dot(L[0:k, :], obs1)
    tsn, relax_ampn = fingerprint_relaxation(T, p0, obs1, k=k)
    assert_allclose(tsn, ts[0:k])
    assert_allclose(relax_ampn, relax_amp)
Example #16
0
    def setUp(self):
        self.k = 4

        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.bdc = birth_death_chain(q, p)

        self.mu = self.bdc.stationary_distribution
        self.T = self.bdc.transition_matrix_sparse
        R, D, L = rdl_decomposition(self.T, k=self.k)
        self.L = L
        self.R = R
        self.ts = timescales(self.T, k=self.k)
        self.times = np.array([1, 5, 10, 20])

        ev = np.diagonal(D)
        self.ev_t = ev[np.newaxis, :]**self.times[:, np.newaxis]

        self.tau = 7.5
        """Observables"""
        obs1 = np.zeros(10)
        obs1[0] = 1
        obs1[1] = 1
        obs2 = np.zeros(10)
        obs2[8] = 1
        obs2[9] = 1

        self.obs1 = obs1
        self.obs2 = obs2
        """Initial vector for relaxation"""
        w0 = np.zeros(10)
        w0[0:4] = 0.25
        self.p0 = w0