In [132]:
import numpy as np
from numpy.linalg import matrix_rank
from random import randint
import random 
import matplotlib.pyplot as plt
import networkx as nx
import pandas as pd

We want to use the topological framework to see the hourglass effect.


In [127]:
number_of_layers=100
matrix_size=50
#------------------------------
Status=[ 1] #1 is increasing sparsity
prob_init= [0.25]#a vector of probability
Low_Layer= np.array([])
High_Layer= np.array([])
for s in range(len(Status)):
    for ini in range(len(prob_init)):
        init=prob_init[ini]
        print('ppp',prob_init[ini])
        M=np.eye(matrix_size, dtype=int)
        Probability_of_zero=init
        for i in range(number_of_layers):
            #print ('i',i)
            matrix=np.random.choice([0, 1], size=(matrix_size,matrix_size), p=[Probability_of_zero, 1-Probability_of_zero])
            matrix=Have_atleast_one_connection(matrix)
            #print(matrix)
            Non_zero_indexes=np.nonzero(matrix)
            #print(((i*matrix_size)+Non_zero_indexes[0]+1))
            #print((((i+1)*matrix_size)+Non_zero_indexes[1]+1))
            Low_Layer=np.append(Low_Layer,((i*matrix_size)+Non_zero_indexes[0]+1))
            High_Layer=np.append(High_Layer,(((i+1)*matrix_size)+Non_zero_indexes[1]+1))
            prob_prev=Probability_of_zero
            if Status[s]==1:
                Probability_of_zero+=((1-init)/number_of_layers)
            elif Status[s]==2:
                #Probability_of_zero=random.uniform(0,prob_prev)
                Probability_of_zero-=((init-0)/number_of_layers)
        Low_Layer.astype(int)
        High_Layer.astype(int)
        Low_Layer=list(Low_Layer)
        High_Layer=list(High_Layer)


ppp 0.25

In [5]:
def Have_atleast_one_connection(matrix):
    SUM=sum(matrix)
    index=np.where( SUM==0 )
    num = (index[0]).shape[0]
    make_a_connection_index=np.random.randint(matrix.shape[1], size=num)
    matrix[make_a_connection_index,index[0]]=1
    return matrix

In [130]:
#Wrting tsrget nodes and adjacency list in the file.
TargetNodes=((number_of_layers)*matrix_size)+Non_zero_indexes[1]+1
TargetNodes=np.sort(TargetNodes).T
np.savetxt('../PCen_2/target.txt', TargetNodes ,fmt="%d")
#===========================================
dat = np.array([Low_Layer, High_Layer])
dat = dat.T
np.savetxt('../PCen_2/toy.txt', dat, delimiter = ' ',fmt="%d")

In [137]:
data = pd.read_csv('../PCen_2/path_centrality.txt', sep="\t", header=None)
data.columns = ["NodeID", "UNnorPathCen", "NorPathCen"]

In [203]:
data = data.sort_values(['NodeID'], ascending=[1])
plt.figure()
data['NorPathCen'].plot.hist(stacked=True, bins=100)
plt.xlabel('Path Centrality Value')
plt.ylabel('Frequency')
#plt.savefig('Topological View(Path Centrality )/Overall_PathCentrality_Distribution.png')
plt.show()



In [209]:
plt.figure()
data['LayerNUmber']=np.ceil(data['NodeID']/50)
data['LayerNUmber'] = data['LayerNUmber'].astype(int)
Layer_Max_PC_Value=data.groupby(['LayerNUmber'], sort=False)['NorPathCen'].max()
Layer_Max_PC_Value =Layer_Max_PC_Value.to_frame().reset_index()
Layer_Max_PC_Value.columns=['LayerID','MaxPC']
plt.plot(Layer_Max_PC_Value['LayerID'], Layer_Max_PC_Value['MaxPC'], 'ro')
plt.axis([1, number_of_layers, 0, 0.18])
plt.xlabel('Layer_NUmber')
plt.ylabel('Maximum Path Centrality in the Layer')
#plt.savefig('Topological View(Path Centrality )/MaximumPCperLayer.png')
plt.show()



In [210]:
plt.figure()
data['LayerNUmber']=np.ceil(data['NodeID']/50)
data['LayerNUmber'] = data['LayerNUmber'].astype(int)
Layer_Max_PC_Value=data.groupby(['LayerNUmber'], sort=False)['NorPathCen'].min()
Layer_Max_PC_Value =Layer_Max_PC_Value.to_frame().reset_index()
Layer_Max_PC_Value.columns=['LayerID','MaxPC']
plt.plot(Layer_Max_PC_Value['LayerID'], Layer_Max_PC_Value['MaxPC'], 'ro')
plt.axis([1, number_of_layers, 0, 0.020])
plt.xlabel('Layer_NUmber')
plt.ylabel('MInimum Path Centrality in the Layer')
#plt.savefig('Topological View(Path Centrality )/MinimumPCperLayer.png')
plt.show()



In [216]:
data['NorPathCen'].plot.hist(stacked=True, bins=100)
plt.xlabel('Path Centrality Value')
plt.ylabel('Frequency')
#plt.savefig('Topological View(Path Centrality )/Overall_PathCentrality_Distribution.png')
plt.show()



In [250]:
data[data['LayerNUmber']==15]['NorPathCen'].plot.hist(stacked=True, bins=10, alpha=0.5,label="Layer 15")
data[data['LayerNUmber']==50]['NorPathCen'].plot.hist(stacked=True, bins=10, alpha=0.5,label="Layer 50")
data[data['LayerNUmber']==95]['NorPathCen'].plot.hist(stacked=True, bins=20, alpha=0.5 ,label="Layer 95")
plt.xlabel('Path Centrality Value')
plt.ylabel('Frequency')
plt.legend()
#plt.savefig('Topological View(Path Centrality )/DistributionPerLayer.png')
plt.show()



In [238]:




In [ ]: