Testing the non-Markovian Path Analysis Package


In [1]:
import sys
sys.path.append("../")
sys.path.append("../nmpath/")
from test.tools_for_notebook import *
%matplotlib inline
from nmpath.auxfunctions import *
from nmpath.mfpt import *
from nmpath.mappers import rectilinear_mapper
#from nmpath.mappers import voronoi_mapper


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-02ef15679571> in <module>()
      2 sys.path.append("../")
      3 sys.path.append("../nmpath/")
----> 4 from test.tools_for_notebook import *
      5 get_ipython().magic('matplotlib inline')
      6 from nmpath.auxfunctions import *

/home/esuarez/Dropbox/workspace/NMpathAnalysis/test/tools_for_notebook.py in <module>()
      8 from nmpath.interval import Interval
      9 from nmpath.auxfunctions import *
---> 10 from nmpath.ensembles import *
     11 
     12 # global variable: number of partitions per dimension in the 2D toy model

/home/esuarez/Dropbox/workspace/NMpathAnalysis/nmpath/ensembles.py in <module>()
      8 from nmpath.interval import Interval
      9 from nmpath.auxfunctions import get_shape, weighted_choice
---> 10 from nmpath.auxfunctions import reverse_sort_lists, directional_mfpt
     11 
     12 

ImportError: cannot import name 'directional_mfpt'

2D Toy model


In [ ]:
plot_traj([],[])

MC simulation


In [ ]:
#Generating MC trajectories
mc_traj1_2d = mc_simulation2D(100000)
mc_traj2_2d = mc_simulation2D(10000)

1 - Ensemble class (analysis of continuos trajectories)

Stores an esemble (list) of trajectories (np.arrays). The ensemble could have any number of trajectories including no trajectories at all.

Creating an Ensemble


In [ ]:
# Empty ensemble with no trajectories
my_ensemble = Ensemble()

From a single trajectory:


In [ ]:
# from a single trajectory
my_ensemble = Ensemble([mc_traj1_2d],verbose=True)

From a list of trajectories:


In [ ]:
# We have to set list_of_trajs = True

my_list_of_trajs = [mc_traj1_2d, mc_traj2_2d]

my_ensemble = Ensemble(my_list_of_trajs, verbose=True)

Ensembles are iterable objects


In [ ]:
for traj in my_ensemble:
    print(len(traj))

Adding trajectories to the Ensemble

New trajectories can be added to the ensemble as long as there is consistency in the number of variables.


In [ ]:
my_ensemble = Ensemble(verbose=True)

my_ensemble.add_trajectory(mc_traj1_2d)

my_ensemble.add_trajectory(mc_traj2_2d)

"Printing" the ensemble


In [ ]:
print(my_ensemble)

Defining states and computing MFPTs

The states are considered intervals in the is the class is Ensemble


In [ ]:
stateA = [[0,pi],[0,pi]]
stateB = [[5*pi,6*pi],[5*pi,6*pi]]

my_ensemble.mfpts(stateA, stateB)

Sum of ensembles (ensemble + ensemble)


In [ ]:
seq1 = mc_simulation2D(20000)
seq2 = mc_simulation2D(20000)

my_e1 = Ensemble([seq1])
my_e2 = Ensemble([seq2])

ensemble1 = my_e1 + my_e2

Another simple example


In [ ]:
e1 = Ensemble([[1.,2.,3.,4.]],verbose=True)
e2 = Ensemble([[2,3,4,5]])
e3 = Ensemble([[2,1,1,4]])

my_ensembles = [e1, e2, e3]

ensemble_tot = Ensemble([])

for ens in my_ensembles:
    ensemble_tot += ens

#ensemble_tot.mfpts([1,1],[4,4])

Computing the count matrix and transition matrix


In [ ]:
n_states = N**2
bin_bounds = [[i*pi for i in range(7)],[i*pi for i in range(7)]]
C1 = my_ensemble._count_matrix(n_states, map_function=rectilinear_mapper(bin_bounds))
print(C1)

In [ ]:
K1 = my_ensemble._mle_transition_matrix(n_states, map_function=rectilinear_mapper(bin_bounds))
print(K1)

2 - PathEnsemble class

Creating a path ensemble object


In [ ]:
#p_ensemble = PathEnsemble()

From ensemble


In [ ]:
p_ensemble = PathEnsemble.from_ensemble(my_ensemble, stateA, stateB)

print(p_ensemble)

MFPTs


In [ ]:
p_ensemble.mfpts(stateA, stateB)

Count matrix


In [ ]:
print(p_ensemble._count_matrix(n_states, mapping_function2D))

In [ ]:
#clusters = p_ensemble.cluster(distance_metric = 'RMSD', n_cluster=10, method = 'K-means')

3 - DiscreteEnsemble class

We can generate a discrete ensemble from the same mapping function and we should obtain exaclty the same result:


In [ ]:
d_ens = DiscreteEnsemble.from_ensemble(my_ensemble, mapping_function2D)

print(d_ens)

Count matrix and transition matrix


In [ ]:
C2 = d_ens._count_matrix(n_states)
print(C2)

In [ ]:
K2= d_ens._mle_transition_matrix(n_states)
print(K2)

Defining states and computing MFPTs

The states are now considered sets, defining the states as follow we should obtain the same results


In [ ]:
stateA = [0]
stateB = [N*N-1]

d_ens.mfpts(stateA, stateB)

Generating a Discrete Ensemble from the transition matrix


In [ ]:
d_ens2 = DiscreteEnsemble.from_transition_matrix(K2, sim_length = 100000)

In [ ]:
#d_ens2.mfpts(stateA,stateB)

4 - DiscretePathEnsemble class

Creating the DPE

From Ensemble


In [ ]:
dpathEnsemble = DiscretePathEnsemble.from_ensemble(my_ensemble, stateA, stateB, mapping_function2D)
print(dpathEnsemble)

In [ ]:
#MFPT from the transition matrix
dpathEnsemble.nm_mfpt(ini_probs = None, n_states = N*N)

From the transition matrix


In [ ]:
n_paths = 100

dpathEnsemble2 = DiscretePathEnsemble.from_transition_matrix\
                (K2, stateA = stateA, stateB = stateB, n_paths = n_paths,ini_pops = [1])
    
print(dpathEnsemble2)

Fundamental sequence


In [ ]:
FSs = dpathEnsemble2.fundamental_sequences(K2)
size = len(FSs)

paths = dpathEnsemble2.trajectories

Plotting paths A -> B


In [ ]:
discrete = [True for i in range(size)]

plot_traj([[paths[i],[]] for i in range(size)] , discrete, \
          line_width=0.1, std=0.5, color='k', title = '{} paths A->B'.format(n_paths))

Plotting Fundamental Sequences A -> B


In [ ]:
plot_traj([[FSs[i],[]] for i in range(size)] ,discrete, \
          line_width=0.5, std=0.4, color='k', title = '{} FSs A->B'.format(n_paths))

In [ ]:
reduced_FSs, weights = dpathEnsemble2.weighted_fundamental_sequences(K2)
new_size = len(weights)
lw = [weights[i]*100 for i in range(new_size)]

In [ ]:
np.random.seed(3)
plot_traj([[reduced_FSs[i],[]] for i in range(new_size)] ,discrete=[True for i in range(new_size)],\
          line_width = lw,std = 0.02, alpha=0.05)

In [ ]:
### for i,w in enumerate(weights):
#     print(w,' ',reduced_FSs[i])

In [ ]:
# G = nx.DiGraph()
# G.add_nodes_from(list(range(N*N)))
# max_path = 2
# count = 0
# for path in reduced_FSs:
#     for i in range(len(path)-1):
#         G.add_edge(path[i],path[i+1], weight = 0)
#     count+=1
#     if count >= max_path: break
# nx.draw_random(G,with_labels=True)