Exemple #1
0
    def plotFluxPathways(self,
                         statetype='macro',
                         mode='net_flux',
                         fraction=1.0):
        """ Plot flux pathways between source and sink state.

        The flux is in units of transition events per lag time used to construct the Markov model.

        Parameters
        ----------
        statetype : {'macro','coarse','micro'}
            What type of states to plot
        mode : {'net_flux', 'gross_flux'}
            Type of fluxes to plot
        fraction : float
            Fraction of fluxes for which to report pathways. Doesn't change the plot, only the text output.
        """
        # Make mode a radio button with interactive plot
        from pyemma import msm
        from pyemma.plots import plot_flux
        from matplotlib import pylab as plt
        self._intergrityCheck()

        plt.figure()
        if statetype == 'micro':
            tpt = msm.tpt(self.model.msm, [self.sourcemicro], [self.sinkmicro])
            fig, pos = plot_flux(tpt, attribute_to_plot=mode)
        elif statetype == 'macro' or statetype == 'coarse':
            metastable_sets = []
            for i in range(self.model.macronum):
                metastable_sets.append(
                    np.where(self.model.macro_ofmicro == i)[0])
            tpt = msm.tpt(self.model.msm, metastable_sets[self.source],
                          metastable_sets[self.sink])
            #from IPython.core.debugger import Tracer
            #Tracer()()
            newsets, tpt = tpt.coarse_grain(metastable_sets)
            setmap = []
            # getting the mapping of new sets to old sets
            for set1 in newsets:
                for idx2, set2 in enumerate(metastable_sets):
                    if set(set1) == set(set2):
                        setmap.append(idx2)
                        continue
            setmap = np.array(setmap)
            fig, pos = plot_flux(tpt,
                                 attribute_to_plot=mode,
                                 state_labels=setmap)
        fig.show()

        paths, pathfluxes = tpt.pathways(fraction=fraction)
        cumflux = 0
        print("Path flux\t\t%path\t%of total\tpath")
        for i in range(len(paths)):
            cumflux += pathfluxes[i]
            print('{}\t{:3.1f}%\t{:3.1f}%\t\t{}'.format(
                pathfluxes[i], 100.0 * pathfluxes[i] / tpt.total_flux,
                100.0 * cumflux / tpt.total_flux, setmap[paths[i]]))
Exemple #2
0
    def plotFluxPathways(self, statetype='macro', mode='net_flux', fraction=1.0):
        """ Plot flux pathways between source and sink state.

        Parameters
        ----------
        statetype : {'macro','coarse','micro'}
            What type of states to plot
        mode : {'net_flux', 'gross_flux'}
            Type of fluxes to plot
        fraction : float
            Fraction of fluxes for which to report pathways. Doesn't change the plot, only the text output.
        """
        # Make mode a radio button with interactive plot
        from pyemma import msm
        from pyemma.plots import plot_flux
        from matplotlib import pylab as plt
        self._intergrityCheck()

        plt.figure()
        if statetype == 'micro':
            tpt = msm.tpt(self.model.msm, [self.sourcemicro], [self.sinkmicro])
            fig, pos = plot_flux(tpt, attribute_to_plot=mode)
        elif statetype == 'macro' or statetype == 'coarse':
            metastable_sets = []
            for i in range(self.model.macronum):
                metastable_sets.append(np.where(self.model.macro_ofmicro == i)[0])
            tpt = msm.tpt(self.model.msm, metastable_sets[self.source], metastable_sets[self.sink])
            #from IPython.core.debugger import Tracer
            #Tracer()()
            newsets, tpt = tpt.coarse_grain(metastable_sets)
            setmap = []
            # getting the mapping of new sets to old sets
            for set1 in newsets:
                for idx2, set2 in enumerate(metastable_sets):
                    if set(set1) == set(set2):
                        setmap.append(idx2)
                        continue
            setmap = np.array(setmap)
            fig, pos = plot_flux(tpt, attribute_to_plot=mode, state_labels=setmap)
        fig.show()

        paths, pathfluxes = tpt.pathways(fraction=fraction)
        cumflux = 0
        print("Path flux\t\t%path\t%of total\tpath")
        for i in range(len(paths)):
            cumflux += pathfluxes[i]
            print('{}\t{:3.1f}%\t{:3.1f}%\t\t{}'.format(
                    pathfluxes[i], 100.0*pathfluxes[i]/tpt.total_flux,
                    100.0*cumflux/tpt.total_flux, setmap[paths[i]]))
def calculate_macro_TPT_and_MFPT_basedon_micro_MSM(microTPM, mapping,
                                                   source_state, sink_state,
                                                   lagtime, time_unit):
    P = pyemma_msm.markov_model(microTPM)  #TPM is row-normalized
    A = np.where(mapping == source_state)[0]
    B = np.where(mapping == sink_state)[0]
    print('MFPT from state %d to state %d = %f %s' %
          (source_state, sink_state,
           msmtools.analysis.mfpt(microTPM, A, B) * lagtime, time_unit))
    #get TPT

    tpt = pyemma_msm.tpt(P, A, B)
    (paths, pathfluxes) = tpt.pathways()
    cumflux = 0
    print("summarizing the pathways in macrostate form")
    temp_file = open('tpt_temp.log', 'w')
    for i in range(len(paths)):
        cumflux += pathfluxes[i]
        temp_file.write("%16.9f\t%16.9f\t%16.9f\t%s\n" %
                        (pathfluxes[i], 100.0 * pathfluxes[i] / tpt.total_flux,
                         100.0 * cumflux / tpt.total_flux, mapping[paths[i]]))
    temp_file.close()
    os.system('bash lump.sh')  #may do this in bash
    for line in open('tpt_pathlump.log'):
        print(line.strip())
    print('############################################################')
    print("more details about the paths in the microstate form")
    print("Path flux\t\t%path\t%of total\tpaths")
    for i in range(len(paths)):
        cumflux += pathfluxes[i]
        print(pathfluxes[i], '\t',
              '%3.1f' % (100.0 * pathfluxes[i] / tpt.total_flux), '%\t',
              '%3.1f' % (100.0 * cumflux / tpt.total_flux), '%\t', paths[i])
Exemple #4
0
 def test_flux(self):
     r = tpt(self.msm, self.A, self.B)
     fig, pos = plot_flux(r)
     assert type(fig) is matplotlib.figure.Figure
     # matplotlib.pyplot.show(fig)
     # x values should be close to committor
     np.testing.assert_allclose(pos[:,0], r.committor)
Exemple #5
0
def calculate_flux(mm, topic_labels, A=[8], B=[2, 13]):
    #A=[8],B=[2,13],
    # Calculate the flux between two states camp arrival and camp liquidiation / camp transfer )

    tpt = msm.tpt(mm, A, B)

    nCut = 1
    (bestpaths, bestpathfluxes) = tpt.pathways(fraction=0.7)
    cumflux = 0

    # Print the best path between the two states

    print("Path flux\t\t%path\t%of total\tpath")

    topic_sequences = {}
    for i in range(len(bestpaths)):
        cumflux += bestpathfluxes[i]
        flux = 100.0 * bestpathfluxes[i] / tpt.total_flux
        if flux > 0:
            #print(bestpathfluxes[i],'\t','%3.1f'%(100.0*bestpathfluxes[i]/tpt.total_flux),'%\t','%3.1f'%(100.0*cumflux/tpt.total_flux),'%\t\t',bestpaths[i])

            topic_sequence = []
            for element in bestpaths[i]:
                #print (topic_labels[element])
                topic_sequence.append(topic_labels[element])
            topic_sequence = '-'.join(topic_sequence)
            topic_sequences[
                topic_sequence] = 100.0 * bestpathfluxes[i] / tpt.total_flux

    return topic_sequences
Exemple #6
0
 def test_state_labels_flux_auto(self):
     """ ensure auto generated labels show up in the plot"""
     A = [0,1]
     B = [2,4]
     flux = tpt(self.msm, A, B)
     fig, pos = plot_flux(flux, state_labels='auto')
     labels_in_fig = np.array([text.get_text() for text in fig.axes[0].texts])
     self.assertEqual((labels_in_fig == "A").sum(), len(A))
     self.assertEqual((labels_in_fig == "B").sum(), len(B))
Exemple #7
0
    def test_state_labels_flux(self):
        """ ensure our labels show up in the plot"""
        flux = tpt(self.msm, [0,1], [2,4])
        labels = ['foo', '0', '1', '2', 'bar']
        fig, pos = plot_flux(flux, state_labels=labels)
        labels_in_fig = np.array([text.get_text() for text in fig.axes[0].texts])

        for l in labels:
            self.assertEqual((labels_in_fig == l).sum(), 1)
Exemple #8
0
    def calc_flux(self):

        def get_map(cg,member):

            sort1 = [sorted(x) for x in cg]
            sort2 = [sorted(x) for x in member]

            mapping = [-1]*len(cg)
            ll = len(cg)
            for ii in range(ll):
                for jj in range(ll):
                    if(len(sort1[ii]) != len(sort2[jj])): continue

                    for el1,el2 in zip(sort1[ii],sort2[jj]):
                        if(el1 != el2):
                            break
                        mapping[ii] = jj
            return mapping


        # find cluster of helix and loop
        cluster_h = self.cluster_labels[self.idx_source]
        cluster_l = self.cluster_labels[self.idx_sink]
        if(cluster_h == cluster_l):
            print "# Fatal error. Source and sink belong to the same cluster (eRMSD= %f)" % (self.dmat[self.idx_source,self.idx_sink])
            sys.exit(1)
            
        A = self.cluster_members[cluster_h]
        B = self.cluster_members[cluster_l]
        
        # get fluxes
        # import pyemma
        import pyemma.msm as msm      

        M = msm.MSM(self.kmat.T,pi=self.weight)
        fluxAB = msm.tpt(M,A,B)
        cg, cgflux =  fluxAB.coarse_grain(self.cluster_members)
        paths, path_fluxes = cgflux.pathways(fraction=0.99)


        # find mapping (pyemma messes up the indeces)
        mapping = get_map(cg,self.cluster_members)

        # write to file
        fh_c = open(self.name + ".pathways.dat",'w')
        fh_c.write("# cluster source " + str(cluster_h) + "\n")
        fh_c.write("# cluster sink  " + str(cluster_l) + "\n")

        stri = "# Flux % - List of clusters \n"
        for i in range(len(paths)):
            stri += "%5.3f - " %  (path_fluxes[i] / np.sum(path_fluxes))
            for el in paths[i]:
                stri += "%3i " % mapping[el]
            stri += "\n"
        fh_c.write(stri)
        fh_c.close()
Exemple #9
0
    def calculate_in_out_rates(self, coarse=False):
        from pyemma import msm
        from IDP_htmd.model_utils import metastable_states

        try:
            self.model.metastable_sets
            lookup = self.model.set_ofmicros
        except:
            print("Recalculating metastable sets")
            metastable_states(self.model)
            lookup = self.model.macro_ofmicro

        if self.statetype == "macro":
            if (self.sink < 0 or self.sink > len(self.model.metastable_sets)
                    or self.source < 0
                    or self.source > len(self.model.metastable_sets)):
                raise Exception("Sink or source out of bounds")
            tpt = msm.tpt(self.model.msm,
                          self.model.metastable_sets[self.source],
                          self.model.metastable_sets[self.sink])
        elif self.statetype == "micro":
            tpt = msm.tpt(self.model.msm, self.source, self.sink)

        if coarse:
            newsets, tpt = tpt.coarse_grain(self.model.metastable_sets)
            paths, pathfluxes = tpt.pathways(fraction=self.fraction)

            # Create a lookup table for new datasets
            if self.statetype == "macro":
                macro2macro = np.zeros(len(self.model.metastable_sets),
                                       dtype=int)
                for idx, micro in enumerate(newsets):
                    macro2macro[idx] = lookup[micro[0]]
            elif self.statetype == "micro":
                macro2macro = np.zeros(len(newsets), dtype=int)
                for idx, micro in enumerate(newsets):
                    macro2macro[idx] = lookup[micro[0]]
        else:
            paths, pathfluxes = tpt.pathways(fraction=self.fraction)
            macro2macro = lookup

        self.tpt = tpt
        return paths, pathfluxes, macro2macro
Exemple #10
0
    def fluxPathways(self):
        import pyemma.plots as mplt
        import pyemma.msm as msm
        import matplotlib.pylab as plt
        self._intergrityCheck()

        plt.ion()
        tpt = msm.tpt(self.model.coarsemsm, [self.source], [self.sink])
        mplt.plot_flux(tpt[1], show_committor=False)
        plt.show()
def evaluate_dominant_paths(TPM, lagtime, source_state, sink_state, time_unit):
    M = pyemma_msm.markov_model(TPM)
    tpt = pyemma_msm.tpt(M, [source_state], [sink_state])
    (paths,pathfluxes) = tpt.pathways()
    cumflux = 0
    print("Dominant pathways from state %d to state %d:"%(source_state, sink_state))
    print("path\t percentage")
    for i in range(len(paths)):
        print(paths[i], '\t','%3.1f'%(100.0*pathfluxes[i]/tpt.total_flux))
    print('MFPT from state %d to state %d  = %f %s'% (source_state, sink_state, M.mfpt(source_state, sink_state)*lagtime, time_unit))
def calculate_mean_passage_time_between_states(mm,topic_labels):
    #Create a matrix that will hold the data

    passage_times = np.zeros(shape=(len(topic_labels),len(topic_labels)))
    df_passage_times = pd.DataFrame(passage_times)  
    for f,row in enumerate(topic_labels):

        for l,column in enumerate(topic_labels):
            try:
                df_passage_times.iloc()[f][l]=msm.tpt(mm,[f],[l]).mfpt
            except:
                df_passage_times.iloc()[f][l]=0
    column_names = {v: k for v, k in enumerate(topic_labels)}
    df_passage_times = df_passage_times.rename(columns=column_names,index=column_names)
    return df_passage_times
Exemple #13
0
def calculate_mean_passage_time_between_states(mm, topic_labels):
    #Create a matrix that will hold the data
    #topic_labels = {i:topic_labels[j] for i, j in enumerate(mm.active_set)}
    passage_times = np.zeros(shape=(len(topic_labels), len(topic_labels)))
    df_passage_times = pd.DataFrame(passage_times)
    for f, row in enumerate(topic_labels):

        for l, column in enumerate(topic_labels):
            try:
                #TODO: why no active set?
                #df_passage_times.iloc()[f][l]=msm.tpt(mm,[mm._full2active[f]],[mm._full2active[l]]).mfpt
                df_passage_times.iloc()[f][l] = msm.tpt(mm, [f], [l]).mfpt
            except:
                df_passage_times.iloc()[f][l] = 0
    column_names = {v: k for v, k in enumerate(topic_labels)}
    df_passage_times = df_passage_times.rename(columns=column_names,
                                               index=column_names)
    return df_passage_times
Exemple #14
0
    def test_time_units(self):
        dtraj = np.random.randint(0, 4, 1000)
        tau = 12
        dt = 0.456
        msmobj = estimate_markov_model(dtraj, lag=tau, dt_traj='%f ns' % dt)

        # check MFPT consistency
        mfpt_ref = msmobj.mfpt([0], [1])
        tptobj = tpt(msmobj, [0], [1])
        assert_allclose(tptobj.mfpt, mfpt_ref)
        assert_allclose(
            msmana.mfpt(msmobj.P, [1], [0], tau=tau) * dt, mfpt_ref)
        assert_allclose(
            np.dot(msmobj.stationary_distribution, tptobj.backward_committor) /
            tptobj.total_flux, mfpt_ref)

        # check flux consistency
        total_flux_ref = tptobj.total_flux
        A = tptobj.A
        B = tptobj.B
        I = tptobj.I
        assert_allclose(
            tptobj.gross_flux[A, :][:, B].sum() +
            tptobj.gross_flux[A, :][:, I].sum(), total_flux_ref)
        assert_allclose(
            tptobj.net_flux[A, :][:, B].sum() +
            tptobj.net_flux[A, :][:, I].sum(), total_flux_ref)
        assert_allclose(
            tptobj.flux[A, :][:, B].sum() + tptobj.flux[A, :][:, I].sum(),
            total_flux_ref)
        mf = tptobj.major_flux(1.0)
        assert_allclose(mf[A, :][:, B].sum() + mf[A, :][:, I].sum(),
                        total_flux_ref)

        # check that the coarse-grained version is consistent too
        _, tptobj2 = tptobj.coarse_grain([A, I, B])
        assert_allclose(tptobj2.total_flux, total_flux_ref)
        assert_allclose(tptobj2.mfpt, mfpt_ref)
Exemple #15
0
def createTPT(MSM_object, A, B):
    """ Calculate the reactive flux between sets A and B.
    Return a ReactiveFlux object"""
    return msm.tpt(MSM_object, A, B)
Exemple #16
0
    def plotFluxPathways(
        self, statetype="macro", mode="net_flux", fraction=1.0, plot=True, save=None
    ):
        """Plot flux pathways between source and sink state.

        The flux is in units of transition events per lag time used to construct the Markov model.

        Parameters
        ----------
        statetype : {'macro','coarse','micro'}
            What type of states to plot
        mode : {'net_flux', 'gross_flux'}
            Type of fluxes to plot
        fraction : float
            Fraction of fluxes for which to report pathways. Doesn't change the plot, only the text output.
        plot : bool
            If set it False the plot will not show up in a figure
        save : str
            If a path is passed to save, the plot will be saved to the specified file
        """
        # Make mode a radio button with interactive plot
        from pyemma import msm
        from pyemma.plots import plot_flux
        from matplotlib import pylab as plt

        self._intergrityCheck()

        plt.figure()
        if statetype == "micro":
            tpt = msm.tpt(self.model.msm, [self.sourcemicro], [self.sinkmicro])
            fig, pos = plot_flux(tpt, attribute_to_plot=mode)
        elif statetype == "macro" or statetype == "coarse":
            metastable_sets = []
            for i in range(self.model.macronum):
                metastable_sets.append(np.where(self.model.macro_ofmicro == i)[0])
            tpt = msm.tpt(
                self.model.msm, metastable_sets[self.source], metastable_sets[self.sink]
            )
            newsets, tpt = tpt.coarse_grain(metastable_sets)
            setmap = []
            # getting the mapping of new sets to old sets
            for set1 in newsets:
                for idx2, set2 in enumerate(metastable_sets):
                    if set(set1) == set(set2):
                        setmap.append(idx2)
                        continue
            setmap = np.array(setmap)
            fig, pos = plot_flux(tpt, attribute_to_plot=mode, state_labels=setmap)

        if save is not None:
            fig.savefig(save, dpi=300, bbox_inches="tight", pad_inches=0.2)
        if plot:
            fig.show()

        paths, pathfluxes = tpt.pathways(fraction=fraction)
        cumflux = 0
        print("Path flux\t\t%path\t%of total\tpath")
        for i in range(len(paths)):
            cumflux += pathfluxes[i]
            print(
                "{}\t{:3.1f}%\t{:3.1f}%\t\t{}".format(
                    pathfluxes[i],
                    100.0 * pathfluxes[i] / tpt.total_flux,
                    100.0 * cumflux / tpt.total_flux,
                    setmap[paths[i]],
                )
            )
Exemple #17
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_99percent = [[0, 1, 4], [0, 2, 4]]
        self.ref_pathfluxes_99percent = np.array(
            [0.00720338983051, 0.00308716707022])
        self.ref_majorflux_99percent = 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.]])

        msmobj = markov_model(self.P)
        msmobj.mu = msmana.statdist(self.P)
        msmobj.estimated = True
        msmobj1 = msmobj

        # Testing:
        # self.tpt1 = tpt(self.P, self.A, self.B)
        self.tpt1 = tpt(msmobj1, 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.statdist(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.statdist(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.]])
        """Dummy dtraj to trick trick constructor of MSM"""
        dtraj = [0, 0]
        tau = 1

        msmobj = markov_model(self.P2)
        msmobj.mu = msmana.statdist(self.P2)
        msmobj.estimated = True
        msmobj2 = msmobj

        # Testing
        self.tpt2 = tpt(msmobj2, self.A2, self.B2)
Exemple #18
0
import pdb
import matplotlib.pyplot as plt

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]])

M = msm.markov_model(P)
pos = np.array([[2.0, -1.5], [1, 0], [2.0, 1.5], [0.0, -1.5], [0.0, 1.5]])
pl = mplt.plot_markov_model(M, pos=pos)

#pl[0].show()

A = [0]
B = [4]
tpt = msm.tpt(M, A, B)

# get tpt gross flux
F = tpt.gross_flux
print('**Flux matrix**:')
print(F)
print('**forward committor**:')
print(tpt.committor)
print('**backward committor**:')
print(tpt.backward_committor)
# we position states along the y-axis according to the commitor
tptpos = np.array([tpt.committor, [0, 0, 0.5, -0.5, 0]]).transpose()
print('\n**Gross flux illustration**: ')
pl = mplt.plot_flux(tpt,
                    pos=tptpos,
                    arrow_label_format="%10.4f",
# Get the model
# ------

kin_zip = np.load('../kin_zip.npz')
TNres_CG = kin_zip['T_zip']

# Let's compute the conditional path entropy for each path and then perform a weighted sum according to the fractional flux
from copy import deepcopy
s = 0
d = 31
# we are going to need the probability that a traj passes from i to j through d
Alpha_CG = np.zeros(shape=(TNres_CG.shape[0],TNres_CG.shape[1]))
for row in range(TNres_CG.shape[0]):
    for col in np.delete(np.arange(TNres_CG.shape[0]),[row]):
        mle_Nres_CG = pyemma.msm.markov_model(TNres_CG)
        tpt_tmp = msm.tpt(mle_Nres_CG,[row],[col])
        try: # some of the node pairs have too low connections for this analysis??
            paths_tmp = np.array(tpt_tmp.pathways())
            # grab only paths that pass through d
            subpaths = np.where( np.array([d in path for path in paths_tmp[0]]) == True )
            Alpha_CG[row,col] = np.sum(paths_tmp[1][subpaths]) / np.sum(paths_tmp[1])
        except:
            Alpha_CG[row,col] = 0.

np.save('Alpha_unf',Alpha_CG)

s = 31
d = 0
# we are going to need the probability that a traj passes from i to j through d
Alpha_CG = np.zeros(shape=(TNres_CG.shape[0],TNres_CG.shape[1]))
for row in range(TNres_CG.shape[0]):
                  1.94053514e-02, 0.00000000e+00
              ],
              [
                  9.75896673e-03, 0.00000000e+00, -3.56068076e-02,
                  1.53539843e-02, 1.04938566e-02
              ],
              [
                  4.98873182e-05, 6.06203157e-03, 3.28301032e-03,
                  -9.39492921e-03, 0.00000000e+00
              ],
              [
                  1.35496766e-02, 0.00000000e+00, 1.17575223e-02,
                  0.00000000e+00, -2.53071990e-02
              ]])

tau = 1.E-2
T = expm(K * tau)  # NB TPT results should be invariant with tau

my_msm = msm.markov_model(T)
A = [0]
B = [4]
my_msm_tpt = msm.tpt(my_msm, A, B)

print("Stationary distribution:")
print(my_msm_tpt.mu)

print("Forward committor:")
print(my_msm_tpt.committor)
print("Backward committor:")
print(my_msm_tpt.backward_committor)
        print(topic_list_with_labels[element])
        print(topic_list[element])
        print('\n')
        if i == 10:
            break

    # Print the eigenvalues of states
    '''for element in mm.eigenvalues().argsort()[::-1]:
        print (topic_list[element])
    '''

    # Calculate the flux between two states (topic_2, selection and topic_8_14 camp liquidiation / camp transfer )

    A = [topic_list.index('topic_2')]
    B = [topic_list.index('topic_8_14')]
    tpt = msm.tpt(mm, A, B)

    nCut = 1
    (bestpaths, bestpathfluxes) = tpt.pathways(fraction=0.95)
    cumflux = 0

    # Print the best path between the two states

    print("Path flux\t\t%path\t%of total\tpath")
    for i in range(len(bestpaths)):
        cumflux += bestpathfluxes[i]

        print(bestpathfluxes[i], '\t',
              '%3.1f' % (100.0 * bestpathfluxes[i] / tpt.total_flux), '%\t',
              '%3.1f' % (100.0 * cumflux / tpt.total_flux), '%\t\t',
              bestpaths[i])
            'topic_name': topic_labels[element],
            'stationary_prob': mm.pi[element]
        })
        if i == 12:
            break

    # Print the eigenvalues of states
    '''for element in mm.eigenvalues().argsort()[::-1]:
        print (topic_list[element])
    '''

    # Calculate the flux between two states (topic_2, selection and topic_8_14 camp liquidiation / camp transfer )

    A = [8]
    B = [2, 13]
    tpt = msm.tpt(mm, A, B)

    nCut = 1
    (bestpaths, bestpathfluxes) = tpt.pathways(fraction=0.5)
    cumflux = 0

    # Print the best path between the two states

    print("Path flux\t\t%path\t%of total\tpath")

    for i in range(len(bestpaths)):
        cumflux += bestpathfluxes[i]

        print(bestpathfluxes[i], '\t',
              '%3.1f' % (100.0 * bestpathfluxes[i] / tpt.total_flux), '%\t',
              '%3.1f' % (100.0 * cumflux / tpt.total_flux), '%\t\t',
from msmtools.analysis.dense.decomposition import eigenvectors, eigenvalues
import operator
from copy import deepcopy

# Get the model
TNres_CG = np.load('../MLE/T_mle.npy')
# Get the probability that a traj passes from i to j through d
Alpha_CG = np.load('Alpha_fol.npy')

# Let's compute the conditional path entropy for each of the dominant reaction pathways
s = 31
d = 0

# get the reaction pathways from s to d
mle_Nres_CG = pyemma.msm.markov_model(TNres_CG)
tpt_mle_CG = msm.tpt(mle_Nres_CG, [s], [d])
paths_CG_fol = np.array(tpt_mle_CG.pathways())

# compute the conditional path entropy according to Kafsi et al. IEEE Inf Theory (2013)
H_path_cond_CG = []
for path in paths_CG_fol[0]:
    tmp = 1.
    H_tmp = 0.
    for node in range(len(path) - 2):
        # Get Tp
        Tp = deepcopy(TNres_CG)
        for row in range(TNres_CG.shape[0]):
            if ((row == path[node + 1]) or (row == d)):
                Tp[row] *= 0.
                Tp[row, row] = 1.
                continue
                             reversible_type='mle',
                             sliding_window=1,
                             ergodic_cutoff='on',
                             prior_counts=0,
                             verbose=True)
msm_model.fit(assignment_array)

print msm_model.transmat_
TPM = msm.markov_model(msm_model.transmat_)

########################using pyemma to get transition paths
set1 = numpy.where(mapping == source)[0]
set2 = numpy.where(mapping == sink)[0]
#print set1
#print set2
tpt = msm.tpt(TPM, set1, set2)

## get tpt gross flux
#print "grpss flux matrix"
#print tpt.gross_flux
#print "forward committor matrix"
#print tpt.committor
#print "backward committor matrix"
#print tpt.backward_committor_matrix
#
## get tpt net flux
#Fp = tpt.net_flux
#print "net flux matrix"
#print Fp
#
#print "###########################################################"