Ejemplo n.º 1
0
def test_fluxes_2():
    # depends on tpt.committors

    bmsm = BayesianMarkovStateModel(lag_time=1)
    assignments = np.random.randint(3, size=(10, 1000))
    bmsm.fit(assignments)

    # forward committors
    qplus = tpt.committors(0, 2, bmsm)

    ref_fluxes = np.zeros((3, 3))
    ref_net_fluxes = np.zeros((3, 3))
    for el in zip(bmsm.all_populations_, bmsm.all_transmats_):
        pop = el[0]
        tprob = el[1]
        for i in range(3):
            for j in range(3):
                if i != j:
                    # Eq. 2.24 in Metzner et al. Transition Path Theory.
                    # Multiscale Model. Simul. 2009, 7, 1192-1219.
                    ref_fluxes[i, j] += (pop[i] * tprob[i, j] *
                                         (1 - qplus[i]) * qplus[j])

    ref_fluxes /= 100.

    for i in range(3):
        for j in range(3):
            ref_net_fluxes[i, j] = np.max([0, ref_fluxes[i, j] -
                                          ref_fluxes[j, i]])

    fluxes = tpt.fluxes(0, 2, bmsm)
    net_fluxes = tpt.net_fluxes(0, 2, bmsm)

    npt.assert_array_almost_equal(ref_fluxes, fluxes, decimal=2)
    npt.assert_array_almost_equal(ref_net_fluxes, net_fluxes, decimal=2)
Ejemplo n.º 2
0
def test_harder_hubscore():
    # depends on tpt.committors and tpt.conditional_committors

    assignments = np.random.randint(10, size=(10, 1000))
    msm = MarkovStateModel(lag_time=1)
    msm.fit(assignments)

    hub_scores = tpt.hub_scores(msm)

    ref_hub_scores = np.zeros(10)
    for A in range(10):
        for B in range(10):
            committors = tpt.committors(A, B, msm)
            denom = msm.transmat_[A, :].dot(committors)
            for C in range(10):
                if A == B or A == C or B == C:
                    continue
                cond_committors = tpt.conditional_committors(A, B, C, msm)

                temp = 0.0
                for i in range(10):
                    if i in [A, B]:
                        continue
                    temp += cond_committors[i] * msm.transmat_[A, i]
                temp /= denom

                ref_hub_scores[C] += temp

    ref_hub_scores /= (9 * 8)

    npt.assert_array_almost_equal(ref_hub_scores, hub_scores)
Ejemplo n.º 3
0
def test_cond_committors():
    # depends on tpt.committors

    msm = MarkovStateModel(lag_time=1)
    assignments = np.random.randint(4, size=(10, 1000))
    msm.fit(assignments)

    tprob = msm.transmat_

    for_committors = tpt.committors(0, 3, msm)
    cond_committors = tpt.conditional_committors(0, 3, 2, msm)

    # The committor for state one can be decomposed into paths that
    # do and do not visit state 2 along the way. The paths that do not
    # visit state 1 must look like 1, 1, 1, ..., 1, 1, 3. So we can
    # compute them with a similar approximation as the forward committor
    # Since we want the other component of the forward committor, we
    # subtract that probability from the forward committor
    ref = for_committors[1] - np.power(tprob[1, 1],
                                       np.arange(5000)).sum() * tprob[1, 3]
    #print (ref / for_committors[1])
    ref = [0, ref, for_committors[2], 0]

    #print(cond_committors, ref)

    npt.assert_array_almost_equal(ref, cond_committors)
Ejemplo n.º 4
0
def test_fluxes_1():
    # depends on tpt.committors

    msm = MarkovStateModel(lag_time=1)
    assignments = np.random.randint(3, size=(10, 1000))
    msm.fit(assignments)

    tprob = msm.transmat_
    pop = msm.populations_
    # forward committors
    qplus = tpt.committors(0, 2, msm)

    ref_fluxes = np.zeros((3, 3))
    ref_net_fluxes = np.zeros((3, 3))
    for i in range(3):
        for j in range(3):
            if i != j:
                # Eq. 2.24 in Metzner et al. Transition Path Theory.
                # Multiscale Model. Simul. 2009, 7, 1192-1219.
                ref_fluxes[i, j] = (pop[i] * tprob[i, j] *
                                    (1 - qplus[i]) * qplus[j])

    for i in range(3):
        for j in range(3):
            ref_net_fluxes[i, j] = np.max([0, ref_fluxes[i, j] -
                                          ref_fluxes[j, i]])

    fluxes = tpt.fluxes(0, 2, msm)
    net_fluxes = tpt.net_fluxes(0, 2, msm)

    npt.assert_array_almost_equal(ref_fluxes, fluxes)
    npt.assert_array_almost_equal(ref_net_fluxes, net_fluxes)
Ejemplo n.º 5
0
def test_cond_committors():
    # depends on tpt.committors
    
    msm = MarkovStateModel(lag_time=1)
    assignments = np.random.randint(4, size=(10, 1000))
    msm.fit(assignments)

    tprob = msm.transmat_

    for_committors = tpt.committors(0, 3, msm)
    cond_committors = tpt.conditional_committors(0, 3, 2, msm)

    # The committor for state one can be decomposed into paths that
    # do and do not visit state 2 along the way. The paths that do not
    # visit state 1 must look like 1, 1, 1, ..., 1, 1, 3. So we can
    # compute them with a similar approximation as the forward committor
    # Since we want the other component of the forward committor, we
    # subtract that probability from the forward committor
    ref = for_committors[1] - np.power(tprob[1, 1], np.arange(5000)).sum() * tprob[1, 3]
    #print (ref / for_committors[1])
    ref = [0, ref, for_committors[2], 0]

    #print(cond_committors, ref)

    npt.assert_array_almost_equal(ref, cond_committors)
Ejemplo n.º 6
0
def test_harder_hubscore():
    # depends on tpt.committors and tpt.conditional_committors

    assignments = np.random.randint(10, size=(10, 1000))
    msm = MarkovStateModel(lag_time=1)
    msm.fit(assignments)

    hub_scores = tpt.hub_scores(msm)

    ref_hub_scores = np.zeros(10)
    for A in xrange(10):
        for B in xrange(10):
            committors = tpt.committors(A, B, msm)
            denom = msm.transmat_[A, :].dot(committors)  #+ msm.transmat_[A, B]
            for C in xrange(10):
                if A == B or A == C or B == C:
                    continue
                cond_committors = tpt.conditional_committors(A, B, C, msm)

                temp = 0.0
                for i in xrange(10):
                    if i in [A, B]:
                        continue
                    temp += cond_committors[i] * msm.transmat_[A, i]
                temp /= denom

                ref_hub_scores[C] += temp

    ref_hub_scores /= (9 * 8)

    #print(ref_hub_scores, hub_scores)

    npt.assert_array_almost_equal(ref_hub_scores, hub_scores)
Ejemplo n.º 7
0
def test_fluxes():
    # depends on tpt.committors

    msm = MarkovStateModel(lag_time=1)
    assignments = np.random.randint(3, size=(10, 1000))
    msm.fit(assignments)

    tprob = msm.transmat_
    pop = msm.populations_
    # forward committors
    qplus = tpt.committors(0, 2, msm)

    ref_fluxes = np.zeros((3, 3))
    ref_net_fluxes = np.zeros((3, 3))
    for i in xrange(3):
        for j in xrange(3):
            if i != j:
                # Eq. 2.24 in Metzner et al. Transition Path Theory.
                # Multiscale Model. Simul. 2009, 7, 1192-1219.
                ref_fluxes[i, j] = (pop[i] * tprob[i, j] * (1 - qplus[i]) *
                                    qplus[j])

    for i in xrange(3):
        for j in xrange(3):
            ref_net_fluxes[i, j] = np.max(
                [0, ref_fluxes[i, j] - ref_fluxes[j, i]])

    fluxes = tpt.fluxes(0, 2, msm)
    net_fluxes = tpt.net_fluxes(0, 2, msm)

    # print(fluxes)
    # print(ref_fluxes)

    npt.assert_array_almost_equal(ref_fluxes, fluxes)
    npt.assert_array_almost_equal(ref_net_fluxes, net_fluxes)
Ejemplo n.º 8
0
def test_committors_2():
    bmsm = BayesianMarkovStateModel(lag_time=1)
    assignments = np.random.randint(3, size=(10, 1000))
    bmsm.fit(assignments)

    committors = tpt.committors([0], [2], bmsm)

    ref = 0
    for tprob in bmsm.all_transmats_:
        ref += np.power(tprob[1, 1], np.arange(1000)).sum() * tprob[1, 2]
    ref = np.array([0, ref / 100., 1])

    npt.assert_array_almost_equal(ref, committors, decimal=2)
Ejemplo n.º 9
0
def test_cond_committors_2():
    # depends on tpt.committors

    bmsm = BayesianMarkovStateModel(lag_time=1)
    assignments = np.random.randint(4, size=(10, 1000))
    bmsm.fit(assignments)

    for_committors = tpt.committors(0, 3, bmsm)
    cond_committors = tpt.conditional_committors(0, 3, 2, bmsm)

    ref = 0
    for tprob in bmsm.all_transmats_:
        ref += (for_committors[1] -
                np.power(tprob[1, 1], np.arange(5000)).sum() *
                tprob[1, 3])
    ref = [0, ref / 100., for_committors[2], 0]

    npt.assert_array_almost_equal(ref, cond_committors, decimal=2)
Ejemplo n.º 10
0
def do_tpt(ev_id):
    plt.figure(figsize=(15, 10))
    # TPT "FROM":
    sources = [7]
    # TPT "To":
    sinks = [10]
    net_flux = tpt.net_fluxes(sources, sinks, msm, for_committors=None)
    np.savetxt('net_flux.txt', net_flux)
    pfold = tpt.committors(sources, sinks, msm)
    np.savetxt('pfold.txt', pfold)
    paths = tpt.paths(sources,
                      sinks,
                      net_flux,
                      remove_path='subtract',
                      flux_cutoff=0.9999999999)
    mfpts = tpt.mfpts(
        msm, sinks=None, lag_time=1.0
    )  #     Default is (1) which is in units of the lag time of the MSM.
    print "mfpts:", mfpts
    np.savetxt('mfpts_from_i_to_j.txt', np.array(mfpts))
    total_flux = np.sum(paths[1])
    print "total_flux:", total_flux
    sort = np.argsort(pfold)
    total_line_width = np.sum(paths[1][0:5])
    # top 5 paths, to get all paths set this number higher
    for j in range(5):
        print "path:", paths[0][j]
        print "flux:", paths[1][j], paths[1][j] / float(np.sum(paths[1]))
        x = []
        for k in range(len(paths[0][j])):
            x.extend(np.where(np.arange(100)[sort] == paths[0][j][k])[0])
        plt.plot(x,
                 pfold[paths[0][j]],
                 linewidth=np.log(paths[1][j] / float(total_line_width)))
    plt.legend(['%1.8f' % i for i in paths[1][0:5]],
               fontsize=18,
               loc='upper left')
    for i in range(len(pfold)):
        plt.plot(i, pfold[sort[i]], 'o')
        plt.text(i, pfold[sort[i]], sort[i])
    plt.savefig('pfold_ev%d_0.01.png' % ev_id)
Ejemplo n.º 11
0
def test_committors_1():
    msm = MarkovStateModel(lag_time=1)
    assignments = np.random.randint(3, size=(10, 1000))
    msm.fit(assignments)

    tprob = msm.transmat_

    committors = tpt.committors([0], [2], msm)

    # The probability of hitting state 2 before going back to state 1
    # is a sum over possible paths that don't go back to state 0.
    # Since there are only three states the paths are all something
    # of the form 1, 1, 1, 1, ..., 1, 1, 2
    # Theoretically we need infinitely many 1->1 transitions, but
    # that approaches zero, so the approximation below is probably
    # just fine.
    ref = np.power(tprob[1, 1], np.arange(1000)).sum() * tprob[1, 2]
    ref = np.array([0, ref, 1])

    # print(committors, ref)

    npt.assert_array_almost_equal(ref, committors)
Ejemplo n.º 12
0
def test_committors():
    msm = MarkovStateModel(lag_time=1)
    assignments = np.random.randint(3, size=(10, 1000))
    msm.fit(assignments)

    tprob = msm.transmat_

    committors = tpt.committors([0], [2], msm)

    # The probability of hitting state 2 before going back to state 1
    # is a sum over possible paths that don't go back to state 0.
    # Since there are only three states the paths are all something
    # of the form 1, 1, 1, 1, ..., 1, 1, 2
    # Theoretically we need infinitely many 1->1 transitions, but
    # that approaches zero, so the approximation below is probably
    # just fine.
    ref = np.power(tprob[1, 1], np.arange(1000)).sum() * tprob[1, 2]
    ref = np.array([0, ref, 1])

    # print(committors, ref)

    npt.assert_array_almost_equal(ref, committors)
Ejemplo n.º 13
0
def rate_estimation_tpt(source_states,sink_states,msm):
    tau = 5
    #timescales_macro = 3788.1 #1st eigenmode
    #timescales_micro = 12095.2 #1st eigenmode, may not be correct!

    c_p53 = 7.1*10**-3 #Mol

    committors = tpt.committors(sources=source_states,sinks=sink_states,msm=msm)
    back_com = 1-committors

    #print committors
    F = 0
    for i in source_states:
        for j in range(msm.n_states_):
            if j not in source_states:
                F += msm.populations_[i]*msm.transmat_[i,j]*committors[j]

    kab = F/((tau*1e-9)*np.dot(msm.populations_,back_com))
    #print kab*(timescales_macro/timescales_micro)
    #print kab*(1/c_p53)

    #print "kAB:",kab*(1/c_p53)*(timescales_macro/timescales_micro)
    return kab
Ejemplo n.º 14
0
import os, sys
import numpy as np
from msmbuilder import tpt
from scipy.sparse import coo_matrix
from scipy.io import mmwrite
from sklearn.externals import joblib

ids = range(6383, 6391)

for p_id in ids:

    msm = joblib.load(
        'MSMs-{pid}-macro40/MSMs-{pid}-macro40.pkl'.format(pid=p_id))
    committors = tpt.committors(sources=[4], sinks=[13], msm=msm)
    net_fluxes = tpt.net_fluxes(sources=[4],
                                sinks=[13],
                                msm=msm,
                                for_committors=committors)
    top_10_paths_sub, top_10_fluxes_sub = tpt.paths(sources=[4],
                                                    sinks=[13],
                                                    net_flux=net_fluxes,
                                                    num_paths=10,
                                                    remove_path='subtract')
    top_10_paths_bot, top_10_fluxes_bot = tpt.paths(sources=[4],
                                                    sinks=[13],
                                                    net_flux=net_fluxes,
                                                    num_paths=10,
                                                    remove_path='bottleneck')
    output_dir = "Data-{pid}-macro40".format(pid=p_id)
    print top_10_paths_sub, top_10_fluxes_sub
    print top_10_paths_bot, top_10_fluxes_bot