示例#1
0
def Evaluate_Result(homedir='.',
                    tProb_=None,
                    lag_time=1,
                    microstate_mapping_=None,
                    MacroAssignments_=None,
                    name=None):

    MacroMSM = MarkovStateModel(lag_time=lag_time)
    MacroMSM.fit(MacroAssignments_)
    Macro_tProb_ = MacroMSM.tProb_
    Macro_tCount_ = MacroMSM.tCount_
    #plot_matrix(labels=None, tProb_=tProb_, name=name)

    #Calculate metastablilty
    print "Calculating Metastablilty and Modularity..."
    metastability = Macro_tProb_.diagonal().sum()
    metastability /= len(Macro_tProb_)
    print "Metastability:", metastability

    #Calculate modularity
    micro_tProb_ = tProb_
    degree = micro_tProb_.sum(axis=1)  #row sum of tProb_ matrix
    total_degree = degree.sum()

    modularity = 0.0
    len_mapping = len(microstate_mapping_)
    for i in xrange(len_mapping):
        state_i = microstate_mapping_[i]
        for j in xrange(len_mapping):
            state_j = microstate_mapping_[j]
            if state_i == state_j:
                modularity += micro_tProb_[
                    i, j] - degree[i] * degree[j] / total_degree
    modularity /= total_degree
    print "Modularity:", modularity

    Macro_tCountDir = homedir + "/" + name + "Macro_tCounts.mtx"
    Macro_tProbDir = homedir + "/" + name + "Macro_tProb.mtx"
    Metastability_Modularity_Dir = homedir + "/" + name + "Metastability_Modularity.txt"

    np.savetxt(Metastability_Modularity_Dir, [metastability, modularity],
               fmt="%lf")
    scipy.io.mmwrite(Macro_tCountDir,
                     scipy.sparse.csr_matrix(Macro_tCount_),
                     field='integer')
    scipy.io.mmwrite(Macro_tProbDir, scipy.sparse.csr_matrix(Macro_tProb_))
    #Plot tProb matrix
    plot_matrix(tProb_=Macro_tProb_, name=name)
示例#2
0
import os,sys
import numpy as np
import scipy.io
HK_DataMiner_Path = os.path.relpath(os.pardir)
#HK_DataMiner_Path = os.path.abspath("/home/stephen/Dropbox/projects/work-2015.5/HK_DataMiner/")
sys.path.append(HK_DataMiner_Path)
from utils import plot_matrix, plot_block_matrix
from msm import MarkovStateModel
import argparse
cli = argparse.ArgumentParser()
cli.add_argument('-c', '--assignments_dir', type=str)
cli.add_argument('-p', '--tProb', type=str, default=None)
args = cli.parse_args()

assignments_dir = args.assignments_dir
tProb_dir = args.tProb

labels = np.loadtxt(assignments_dir, dtype=np.int32)

if tProb_dir is not None:
    tProb_ = scipy.io.mmread(tProb_dir)
    #plot_matrix(labels=labels, tProb_=tProb_, name='Matrix')
    plot_block_matrix(labels=labels, tProb_=tProb_, name='BlockMatrix')
else:
    MSM = MarkovStateModel(lag_time=1)
    MSM.fit(labels)
    tProb_ = MSM.tProb_
    #plot_matrix(labels=None, tProb_=tProb_, name='Matrix')
    plot_block_matrix(labels=labels, tProb_=tProb_, name='BlockMatrix')