Beispiel #1
0
def number_of_lp_lattice():
 
    for D in [2]:   
        myfile = open(str(D)+'number_lp_lattice', 'w')
        myfile.write('N_side' + '\t' + 'N'+ '\t'+ 'lp length' +  '\t'+ 'no. lp'+ '\n')
        start_time = time.time()
        for N_side in range(10,16,1):
            N = N_side**2

    #        model = models.COd(2, N)
            model = models.square_lattice_model(D, N_side)
            DAG = model[0]
            extremes = model[1]
            tr_DAG = tr.trans_red(DAG)
            lp = dl.lpd(tr_DAG, extremes[1], extremes[0])
            length_lp = lp[2]
            
            j=0
            paths_list = list(nx.all_simple_paths(tr_DAG, extremes[1], extremes[0], cutoff=length_lp+1))
            for i in range(len(paths_list)):
                if len(paths_list[i])==length_lp+1:
                    j+=1
    
            myfile.write(str(N_side) + '\t' + str(N) +'\t' + str(length_lp)+ '\t' + str(j) + '\n')
            print 'done', N_side
        elapsed = time.time() - start_time
        
        print 'finished.',D,'Dimension. Time elapsed = ',elapsed
        
    return
def degree_distribution(trans_red = 'False', N=100, m=10, number_of_trials = 1, filename = 'degree_histogram'):
    
    '''
    Outputs node label (=integer time value) and in-degree before and after Transitive reduction.
    
    n, int = final number of nodes, as usual for BA model.
    m, int = number of edges to add with addition of each new node.
    trials, int = number of realisations of model to be averaged over.
    filename, string = txt file name for output
    
    '''
    
    f = open(filename, 'w')
    f.write("<k_in>" + '\t' + "rank <k_in>"+ '\t' + "<k_out>"+ '\t' + "rank <k_out>"+ '\t' + "prob_of_in_degree"+ '\t' + "prob_of_out_degree"+ '\n')
   
    in_freq=[]
    out_freq = []
    for i in range(N):
        in_freq.append(0)
        out_freq.append(0)
        
    for trial in range(number_of_trials):
        
        model = models.barabasi_albert_graph(N, m, None)
        G = model[0]
        
        if trans_red:
            G = tr.trans_red(G)
            
        in_list = degree_histogram(G, 'in')
        out_list = degree_histogram(G, 'out')
        
        for i in range(len(in_list)):
            in_freq[i] += in_list[i] 
        
        for i in range(len(out_list)):
            out_freq[i] += out_list[i]
        
        print "done trial " , trial
    
    prob_of_in_degree = []
    prob_of_out_degree = []
    
    for i in range(len(in_freq)):
        in_freq[i] = float(in_freq[i])/float(number_of_trials) 
        out_freq[i] = float(out_freq[i])/float(number_of_trials)
    
        prob_of_in_degree.append(float(in_freq[i])/float(N))
        prob_of_out_degree.append(float(out_freq[i])/float(N))
        f.write(str(i) + '\t' + str(in_freq[i])+ '\t' + str(i)+ '\t' + str(in_freq[i])+ '\t' + str(prob_of_in_degree[i])+ '\t' + str(prob_of_out_degree[i])+ '\n')
    
    f.close()
    
    return G
Beispiel #3
0
def create_TR_data(DAG, filename):
   
    print 'Making TRed DAG'
    TR = tr.trans_red(DAG)
    print 'TR made'

    # Write TR to data file
    full_filename = './Data/%s.txt' % filename
    TR_out = open(full_filename, 'w')
    for edge in TR.edges():
        TR_out.write(edge[0] + '\t' + edge[1] + '\n')
    return TR    
Beispiel #4
0
def test(N, D):
    
    box = dl.box_generator(N, D, 'minkowski')
    #box = mod2.COd(D,N)
    dag = box[0]
    ext = box[1]
    
    tr_dag = tr.trans_red(dag)
    good_dag = tr_dag
    lp = dl.lpd(tr_dag, ext[1], ext[0])
    length = lp[2]
    print length
    return length
Beispiel #5
0
def lpd_test(N, D):
    box = dl.box_generator(N, D, 'minkowski')
    dag = box[0]
    ext = box[1]
    
    tr_dag = tr.trans_red(dag)
    
    print ext[0]
    print ext[1]
    
    
    lp = dl.lpd(tr_dag, ext[1], ext[0])
    print lp[1]
    return lp
Beispiel #6
0
def number_of_lp_BA(m_range=[1,5,10,50], n_max=50, n_step=5):
 
    for m in m_range:  
        n_range=range(m+1,n_max,n_step) 
#        n_range=range(101,201,10)
        myfile = open(str(m)+'C_for_BA', 'w')
        myfile.write('n' + '\t' + 'lp_av'+'\t' + 'lp_err'+ '\t' + 'no._av' +'\t' +  'no._err'+ '\n')
        start_time = time.time()
        trials = 1000
        for n in n_range:
            lp_length_list = []
            j_list = []
            for _ in range(trials):
        #        model = models.COd(2, N)
                model = models.barabasi_albert_graph(n, m)
                DAG = model[0]
                extremes = model[1]
                tr_DAG = tr.trans_red(DAG)
                lp = dl.lpd(tr_DAG, extremes[1], extremes[0])
                length_lp = lp[2]
                lp_length_list.append(length_lp)
                
                j=0
                paths_list = list(nx.all_simple_paths(tr_DAG, extremes[1], extremes[0], cutoff=length_lp+1))
                for i in range(len(paths_list)):
                    if len(paths_list[i])==length_lp+1:
                        j+=1
                        
                j_list.append(j)
            
            print j_list
            
            lp_stats = stats.list_stats(lp_length_list)
            lp_av = lp_stats[3]
            lp_err = lp_stats[1]
        
            j_stats = stats.list_stats(j_list)
            j_av = j_stats[3]
            j_err = j_stats[1]
            print j_av
            print j_err
        
            myfile.write(str(n) + '\t' + str(lp_av)+'\t' + str(lp_err)+ '\t' + str(j_av) +'\t' +  str(j_err)+ '\n')
            print 'done', n
        elapsed = time.time() - start_time
        
        print 'finished.',m,'m. Time elapsed = ',elapsed
        
    return
Beispiel #7
0
def tr_test(N):
    box = dl.box_generator(N, 3)
    dag = box[0]
    ext = box[1]

    '''for node in dag.nodes():
        print '####################'
        print node
        print dag.successors(node)
        print '####################'''
    start_time = time.clock()
    tr_dag = tr.trans_red(dag)
    tr_time = time.clock() - start_time
    #print tr_dag.successors(ext[1])
    print 'N = %s and time for tr was %s' % (N, tr_time)
def test(N, D):
    
    box = dl.box_generator(N, D)
    dag = box[0]
    ext = box[1]
    tr_dag = tr.trans_red(dag)
    good_dag = tr_dag
    lp = dl.lpd(tr_dag, ext[1], ext[0])
    path = lp[1]
    tr_dag = good_dag
    dim = mps.mpsd(tr_dag, path)
    mm_dim = mmd.MM_dimension(dag)
    print dim
    print mm_dim
    return [dim[0], mm_dim]
Beispiel #9
0
def number_of_lp():
 
    for D in [2,3,4]:   
        myfile = open(str(D)+'how many longest paths?', 'w')
        myfile.write('N' + '\t' + 'L_scale=N^1/D'+ '\t'+ 'average lp length'+ '\t'+'std err'+  '\t'+ 'average no. lp'+ '\t'+'std err' +'\n')
        start_time = time.time()
        trials = 100
        for N in range(2,40,1):
            lp_length_list = []
            j_list = []
            for _ in range(trials):
                model = models.COd(2, N)
#                model = models.barabasi_albert_graph(n, m, seed)
                DAG = model[0]
                extremes = model[1]
                tr_DAG = tr.trans_red(DAG)
                lp = dl.lpd(tr_DAG, extremes[1], extremes[0])
                length_lp = lp[2]
                lp_length_list.append(length_lp)
                
                j=0
                paths_list = list(nx.all_simple_paths(tr_DAG, extremes[1], extremes[0], cutoff=length_lp+1))
                for i in range(len(paths_list)):
                    if len(paths_list[i])==length_lp+1:
                        j+=1
                        
                j_list.append(j)
            
            lp_stats = stats.list_stats(lp_length_list)
            lp_av = lp_stats[3]
            lp_err = lp_stats[1]
            
            j_stats = stats.list_stats(j_list)
            j_av = j_stats[3]
            j_err = j_stats[1]
            
            l_scale = (N**(1.0/float(D)))
        
            myfile.write(str(N) + '\t' + str(l_scale) +'\t' + str(lp_av)+'\t' + str(lp_err)+ '\t' + str(j_av) +'\t' +  str(j_err)+ '\n')
            print 'done', N
        elapsed = time.time() - start_time
        
        print 'finished.',D,'Dimension. Time elapsed = ',elapsed
        
    return
def BA_node_degree_pre_post_TR(n=100, m=10, trials = 1000, filename = 'degree_before_after'):
    
    '''
    Outputs node label (=integer time value) and in-degree before and after Transitive reduction.
    
    n, int = final number of nodes, as usual for BA model.
    m, int = number of edges to add with addition of each new node.
    trials, int = number of realisations of model to be averaged over.
    filename, string = txt file name for output
    
    '''

    in_list = []
    in_tr_list = []
    node_list = []
    for i in range(n):
    
        in_list.append(0)
        in_tr_list.append(0)
    
    for trial in range(trials):
        model = models.barabasi_albert_graph(n, m=10)
        G = model[0]
        H= copy.deepcopy(G)
        G_tr = tr.trans_red(H)
        
        f = open(filename, 'w')
        f.write('node' + '\t' + 'degree'+ '\t'+'node_tr'+ '\t' + 'degree_tr'+'\n')
        j=0
        for node, node_tr in zip(G.nodes(), G_tr.nodes()):
            j+=1
            node_list.append(node)
            in_degree = G.in_degree(node)
            in_list[node] += in_degree
            in_degree_tr = G_tr.in_degree(node_tr)
            in_tr_list[node] += in_degree_tr
      
        print "done trial no. ", trial
    
    for i in range(i):
        in_list[i] = in_list[i] / float(trials)
        in_tr_list[i] = in_tr_list[i] / float(trials)
        f.write(str(node_list[i]) + '\t' + str(in_list[i])+ '\t'+str(node_list[i])+ '\t' + str(in_tr_list[i])+'\n')
        
    return
Beispiel #11
0
def lp_with_removal(number_of_trials, number_of_probabilities_considered, N, D, filename):

    """
    arguments: number_of_trials for each probability, output data is averaged over this.
    number_of_probabilities_considered = integer: number of data points
    N = integer: number of nodes in network
    D = integer: dimension of network
    filename = string: output file path e.g. './testing.txt'
    """
    start_time = time.time() #start clock
    myfile = open(filename, 'w')

    p_step = 10.0/number_of_probabilities_considered #define size of step in probability (prob = prob that edge is removed given it's causally allowed)
    myfile.write(str(D)+"D box w/ random removal prob p, and longest path l. Number trials averaged over=" + str(number_of_trials) + " N="+ str(N) + '\n')
    myfile.write('p' + '\t' + 'l(p)' +'\t' + 'std error'  +  '\n')  #send column headings to output file
    #initialize lists for each output variable
    p_list = []
    lp_average_list = []
    lp_std_error_list = []
    
    x_range = np.arange(1.0,10.0+p_step,p_step)  #defines list of x values to loop over   

    for x in x_range: #Loop over x, where x=10^p, so we get more points around prob's closer to 1 and can see phase transition-y behavior more clearly.
        #initialize lists for each quantity for which we require an individual value to be stored after every trial (e.g. for calculations stats)
        lp_list = []
        p = math.log10(x)

        for _ in range(int(number_of_trials)): 
            model = models.box_model(D, N,1-p)  #create model with prob of no edge given edge is causally allowed = p
            DAG_with_removal =  model[0]    #networkx DAG object from above model
            tr_DAG_with_removal = tr.trans_red(DAG_with_removal)    #transitively reduce DAG - doesn't effect lp or MM dimension, DOES MIDPOINT SCALING
            extremes = model[1] #returns list of extremes from model

            if nx.has_path(DAG_with_removal, extremes[1], extremes[0]):  
                longest_path_between_extremes = dl.lpd(tr_DAG_with_removal, extremes[1],extremes[0]) #calculate longest path between extremes
                length_of_longest_path = longest_path_between_extremes[2]   #save length of longest path between extremes
      
            else: 
                length_of_longest_path=0   #if no path between extremes, longest path = 0 is physical.
        
            lp_list.append(length_of_longest_path)
        
        statistics = stats(lp_list)
        lp_average = statistics[3] #calculates average lp over all trials for the current probability
        
        if p == 0.0:
            lp_0 = lp_average  #calculate average longest path for p==0 and use as normalization constant

        lp_average = lp_average / lp_0  #normalize average longest paths so can compare  results of different dimensions
       
        for i in range(len(lp_list)):
            lp_list[i] /= lp_0  #normalize longest paths (need to do this for std error calculation)
        
        statistics = stats(lp_list)
        lp_std_error = statistics[0]
        
        p_list.append(p)
        lp_average_list.append(lp_average)
        lp_std_error_list.append(lp_std_error)

        myfile.write(str(p)+ '\t' + str(lp_average) + '\t' + str(lp_std_error) + '\n')
        
        print "finished ", p , "probability"
        
    elapsed = (time.time() - start_time)   #calculate time for method completion
    print "finished. Time elapsed = " , elapsed

    return [p_list, lp_average_list, lp_std_error_list] 
Beispiel #12
0
def ms_dim_with_removal(number_of_trials, number_of_probabilities_considered, N, D, filename):

    """
    arguments: number_of_trials for each probability, output data is averaged over this.
    number_of_probabilities_considered = integer: number of data points
    N = integer: number of nodes in network
    D = integer: dimension of network
    filename = string: output file path e.g. './testing.txt'
    
    """

    start_time = time.time() #start clock
    myfile = open(filename, 'w')

    p_step = 1.0/number_of_probabilities_considered #define size of step in probability (prob = prob that edge is removed given it's causally allowed)
    myfile.write(str(D)+"D box w/ random removal prob p, and longest path l. Number trials averaged over=" + str(number_of_trials) + " N="+ str(N) + '\n')
    myfile.write('p' + '\t' + 'ms dimension (p)' +'\t' + 'std error' +  '\n')  #send column headings to output file
    #initialize lists for each output variable
    p_list_for_ms = []
    ms_average_list = []
    ms_std_error_list = []
    
    p_range = np.arange(0.0,1.0+p_step,p_step)   

    for p in p_range: #Loop over x, where x=10^p, so we get more points around prob's closer to 1 and can see phase transition-y behavior more clearly.
        #initialize lists for each quantity for which we require an individual value to be stored after every trial (e.g. for calculations stats)
       
        ms_list = []
        ms_diff_sq_list = []
        ms_failed_trials = 0
        
        ms_sum = ms_average = 0.0

        for _ in range(int(number_of_trials)): 
            model = models.box_model(D, N,1-p)  #create model with prob of no edge given edge is causally allowed = p
            DAG_with_removal =  model[0]    #networkx DAG object from above model
            tr_DAG_with_removal = tr.trans_red(DAG_with_removal)    #transitively reduce DAG - doesn't effect lp or MM dimension, DOES MIDPOINT SCALING
            extremes = model[1] #returns list of extremes from model

            if nx.has_path(DAG_with_removal, extremes[1], extremes[0]):  
                longest_path_between_extremes = dl.lpd(tr_DAG_with_removal, extremes[1],extremes[0]) #calculate longest path between extremes
                longest_path = longest_path_between_extremes[1]
                length_of_longest_path = longest_path_between_extremes[2]   #save length of longest path between extremes
      
            else: 
                length_of_longest_path=0   #if no path between extremes, longest path = 0 is physical.
        

            if length_of_longest_path > 2:
                ms_dimension = ms.mpsd(DAG_with_removal, longest_path)[0]  #need to trans red DAG first as otherwise lp method does not return the correct longest path. James said he'd sorted this?
            else: 
                ms_dimension = 0
                ms_failed_trials += 1

            ms_list.append(ms_dimension)
        
        if len(ms_list)<number_of_trials:
            ms_list.append(None)
        
        ms_sum = sum(ms_list)
        if (number_of_trials - ms_failed_trials - 1)>0:
            ms_average = ms_sum/float(number_of_trials-ms_failed_trials)
        else: ms_average=None
        
        ms_diff_sq_sum = 0.0
       
        if (number_of_trials - ms_failed_trials - 1)>0:
            for i in range(number_of_trials - ms_failed_trials):
                ms_diff_sq_list.append((ms_list[i] - ms_average)**2)
                ms_diff_sq_sum += ms_diff_sq_list[i]
            ms_std_dev = math.sqrt(ms_diff_sq_sum/float(number_of_trials - ms_failed_trials -1))
            ms_std_error = ms_std_dev/math.sqrt(float(number_of_trials - ms_failed_trials))
        else:
            ms_std_error = 0
            
        p_list_for_ms.append(p)
        ms_average_list.append(ms_average)
        ms_std_error_list.append(ms_std_error)

        myfile.write(str(p)+ '\t' + str(ms_average) + '\t' + str(ms_std_error) + '\n')
        
        clean = clean_for_plotting(ms_average_list,p_list_for_ms, ms_std_error_list)
        ms_average_list = clean[0]
        p_list_for_ms = clean[1]
        ms_std_error_list = clean[2]
        
        print "finished ", p , "probability"
        
    elapsed = (time.time() - start_time)   #calculate time for method completion
    print "finished. Time elapsed = " , elapsed

    return [p_list_for_ms, ms_average_list, ms_std_error_list]
Beispiel #13
0
import models
import dag_lib as dl
import trans_red as tr
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import time

start_time = time.time()

model = models.COd(2, 200)

DAG = model[0]
extremes = model[1]
print 'made model'
tr_DAG = tr.trans_red(DAG)
print 'done transitive reduction'
lp = dl.lpd(tr_DAG, extremes[1], extremes[0])
length_lp = lp[2]
path = lp[1]

print 'calculated lp'
t_list = []
x_list = []
path_t_list = []
path_x_list = []
#
for node in range(len(DAG.nodes())):
    t_list.append(DAG.nodes()[node][0])
    x_list.append(DAG.nodes()[node][1])
#