forked from FourquetDavid/boguna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology_map.py
98 lines (82 loc) · 3.1 KB
/
topology_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import networkx as nx
import exponent
import equations
import numpy as np
import generate_networks
import math
import loglikelihood
import warnings
from matplotlib import pyplot as plt
warnings.filterwarnings("ignore")
def main():
np.random.seed(50)
name = "drug_net.txt"
#name = "Internet_hyperbolic_map/AS_ARK200906_topology.txt"
r1,t1 = study(name)
r2,t2 = study(name,theta_init = t1)
print t1[:5]
print t2[:5]
graphe = nx.read_weighted_edgelist("drug_net.txt")
graphe = nx.convert_node_labels_to_integers(graphe)
degree = graphe.degree()
tdiff = math.pi - np.abs(math.pi - np.abs(t1 - t2+np.mean(t2-t1)))
from numpy import cosh,sinh,cos,arccosh
distances = arccosh(cosh(r1)*cosh(r2)-sinh(r1)*sinh(r2)*cos(tdiff))
print distances
degres = [1,2,3,4,5,6]
diffavg = [np.average([tdiff[node] for node in degree if degree[node]==deg]) for deg in degres]
#plt.plot(range(len(tdiff)),tdiff,degree.values(),'o')
plt.plot(np.arange(len(graphe)),distances,'o')
plt.show()
#Etude 2 :
def study(name,theta_init = None) :
# graphe = nx.read_edgelist("Internet_hyperbolic_map/AS_ARK200906_topology.txt")
graphe = nx.read_weighted_edgelist(name)
gamma = exponent.get_exponent(graphe)
# gamma = 2.1
# Verify gamma :
# exponent.plot_gamma(graphe)
Nobs = graphe.number_of_nodes()
k_bar_obs = np.mean(graphe.degree().values())
k_max_obs = max(graphe.degree().values())
C_obs = nx.average_clustering(graphe, count_zeros=False)
print Nobs, k_bar_obs, k_max_obs, C_obs
alpha, kappa0, kappaC, P0, k_bar = equations.solve(gamma, k_bar_obs, k_max_obs)
# Verify equations
#equations.verify(alpha, kappa0, kappaC, P0, k_bar, gamma, k_bar_obs, k_max_obs)
#N = Nobs / (1 - P0)
#print gamma, alpha, kappa0, kappaC, P0, k_bar, N
"""
alpha = 0.58
kappa0 = 0.9
kappaC = 4790
P0 = 0.33
N = 35685
k_bar = 9.86
"""
# Estimating beta
'''
beta,path_followed = generate_networks.approximated_beta(N,k_bar,kappa0,gamma,C_obs,
start=1.01, step = 0.01,nb_networks=100)
print beta
'''
# Verify beta:
# print path_followed
beta = 1.02
#beta =1.45
# Creating map:
graphe = nx.convert_node_labels_to_integers(graphe)
constants = kappa0,gamma,beta,Nobs,k_bar
r_opt = loglikelihood.rayon(graphe,constants)
if theta_init is None : theta_init = 2*math.pi*np.random.randint(0,Nobs,Nobs)/Nobs
theta_opt = loglikelihood.optimisation_likelihood(graphe,constants,theta_init=theta_init.copy())
#theta_opt = loglikelihood.optimisation_complete_par_etapes_likelihood(graphe,[5,4,3,2,0],theta_init.copy(),constants)
#theta_opt = loglikelihood.optimisation_par_etapes_likelihood(graphe,"",14,theta_init.copy(),constants,method="core_number")
with open('test50.txt', 'w') as f:
f.write(str(theta_opt))
print "saved"
return r_opt,theta_opt
#Verify loglikelihood
#loglikelihood.verifyloglikelihood(theta_init,graphe,constants)
#loglikelihood.verifyloglikelihood(theta_opt,graphe,constants)
main()