<jupyter_start><jupyter_text>Testing the non-Markovian Path Analysis Package<jupyter_code>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<jupyter_output><empty_output><jupyter_text>2D Toy model<jupyter_code>plot_traj([],[])<jupyter_output><empty_output><jupyter_text>MC simulation<jupyter_code>#Generating MC trajectories
mc_traj1_2d = mc_simulation2D(100000)
mc_traj2_2d = mc_simulation2D(10000)<jupyter_output><empty_output><jupyter_text>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<jupyter_code># Empty ensemble with no trajectories
my_ensemble = Ensemble()<jupyter_output><empty_output><jupyter_text>From a single trajectory:<jupyter_code># from a single trajectory
my_ensemble = Ensemble([mc_traj1_2d],verbose=True)<jupyter_output><empty_output><jupyter_text>From a list of trajectories:<jupyter_code># 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)<jupyter_output><empty_output><jupyter_text>Ensembles are iterable objects<jupyter_code>for traj in my_ensemble:
    print(len(traj))<jupyter_output><empty_output><jupyter_text>Adding trajectories to the EnsembleNew trajectories can be added to the ensemble as long as there is consistency in the number of variables.<jupyter_code>my_ensemble = Ensemble(verbose=True)

my_ensemble.add_trajectory(mc_traj1_2d)

my_ensemble.add_trajectory(mc_traj2_2d)<jupyter_output><empty_output><jupyter_text>"Printing" the ensemble<jupyter_code>print(my_ensemble)<jupyter_output><empty_output><jupyter_text>Defining states and computing MFPTsThe states are considered intervals in the is the class is Ensemble<jupyter_code>stateA = [[0,pi],[0,pi]]
stateB = [[5*pi,6*pi],[5*pi,6*pi]]

my_ensemble.mfpts(stateA, stateB)<jupyter_output><empty_output><jupyter_text>Sum of ensembles (ensemble + ensemble)<jupyter_code>seq1 = mc_simulation2D(20000)
seq2 = mc_simulation2D(20000)

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

ensemble1 = my_e1 + my_e2<jupyter_output><empty_output><jupyter_text>Another simple example<jupyter_code>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])<jupyter_output><empty_output><jupyter_text>Computing the count matrix and transition matrix<jupyter_code>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)
K1 = my_ensemble._mle_transition_matrix(n_states, map_function=rectilinear_mapper(bin_bounds))
print(K1)<jupyter_output><empty_output><jupyter_text>2 - PathEnsemble class  Creating a path ensemble object<jupyter_code>#p_ensemble = PathEnsemble()<jupyter_output><empty_output><jupyter_text>From ensemble<jupyter_code>p_ensemble = PathEnsemble.from_ensemble(my_ensemble, stateA, stateB)

print(p_ensemble)<jupyter_output><empty_output><jupyter_text>MFPTs<jupyter_code>p_ensemble.mfpts(stateA, stateB)<jupyter_output><empty_output><jupyter_text>Count matrix<jupyter_code>print(p_ensemble._count_matrix(n_states, mapping_function2D))
#clusters = p_ensemble.cluster(distance_metric = 'RMSD', n_cluster=10, method = 'K-means')<jupyter_output><empty_output><jupyter_text>3 - DiscreteEnsemble classWe can generate a discrete ensemble from the same mapping function and we should obtain exaclty the same result:<jupyter_code>d_ens = DiscreteEnsemble.from_ensemble(my_ensemble, mapping_function2D)

print(d_ens)<jupyter_output><empty_output><jupyter_text>Count matrix and transition matrix<jupyter_code>C2 = d_ens._count_matrix(n_states)
print(C2)
K2= d_ens._mle_transition_matrix(n_states)
print(K2)<jupyter_output><empty_output><jupyter_text>Defining states and computing MFPTsThe states are now considered sets, defining the states as follow we should obtain the same results<jupyter_code>stateA = [0]
stateB = [N*N-1]

d_ens.mfpts(stateA, stateB)<jupyter_output><empty_output><jupyter_text>Generating a Discrete Ensemble from the transition matrix<jupyter_code>d_ens2 = DiscreteEnsemble.from_transition_matrix(K2, sim_length = 100000)
#d_ens2.mfpts(stateA,stateB)<jupyter_output><empty_output><jupyter_text>4 - DiscretePathEnsemble class  Creating the DPE From Ensemble<jupyter_code>dpathEnsemble = DiscretePathEnsemble.from_ensemble(my_ensemble, stateA, stateB, mapping_function2D)
print(dpathEnsemble)
#MFPT from the transition matrix
dpathEnsemble.nm_mfpt(ini_probs = None, n_states = N*N)<jupyter_output><empty_output><jupyter_text>From the transition matrix<jupyter_code>n_paths = 100

dpathEnsemble2 = DiscretePathEnsemble.from_transition_matrix\
                (K2, stateA = stateA, stateB = stateB, n_paths = n_paths,ini_pops = [1])
    
print(dpathEnsemble2)<jupyter_output><empty_output><jupyter_text>Fundamental sequence<jupyter_code>FSs = dpathEnsemble2.fundamental_sequences(K2)
size = len(FSs)

paths = dpathEnsemble2.trajectories<jupyter_output><empty_output><jupyter_text>Plotting paths A -> B<jupyter_code>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))<jupyter_output><empty_output><jupyter_text>Plotting Fundamental Sequences A -> B<jupyter_code>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))
reduced_FSs, weights = dpathEnsemble2.weighted_fundamental_sequences(K2)
new_size = len(weights)
lw = [weights[i]*100 for i in range(new_size)] 
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) 
### for i,w in enumerate(weights):
#     print(w,' ',reduced_FSs[i])
# 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)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11791383219954649, 'fraction_numerical': 0.021768707482993196, 'mean_word_length': 8.411095305832148, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '26', 'dir': 'jupyter-structured-clean-dedup', 'id': '258263', 'license': 'MIT', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2537, 'n_words': 433, 'path': 'test/.ipynb_checkpoints/nm_path_analysis_testing-checkpoint.ipynb', 'repo_name': 'RobertArbon/NMpathAnalysis', 'size': '6615'}