-
Notifications
You must be signed in to change notification settings - Fork 0
/
points1_8.py
69 lines (49 loc) · 1.9 KB
/
points1_8.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
import json
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
with open('./data/network.json') as f:
edges_list = json.load(f)
print(f"Loading network with {len(edges_list)} timesteps")
# Build aggregated network
flat_list = [item for sublist in edges_list for item in sublist]
tmp = list(map(list, zip(*flat_list)))
unique_nodes = set(tmp[0] + tmp[1])
G = nx.Graph()
G.add_nodes_from(list(unique_nodes))
G.add_edges_from([tuple(item) for sublist in edges_list for item in sublist])
# Point 1)
print(f"Number of nodes: {G.number_of_nodes()}")
print(f"Number of edges: {G.number_of_edges()}")
print(f"Density: {nx.density(G)}")
degrees = sorted([d for n, d in G.degree], reverse=True) # degree sequence
print(f"Average Degree: { np.average(degrees)}")
print(f"Degree variance: {np.var(degrees)}")
# Point 2)
plt.hist(degrees, bins=20)
plt.xlabel("Degree (k)")
plt.ylabel("# of nodes with degree = k")
plt.show()
# Point 3)
print(f"Assortativity: {nx.degree_assortativity_coefficient(G)}")
# Point 4)
print(f"Average clustering coefficient: {nx.average_clustering(G)}")
# Point 5)
shortest_paths = list(nx.shortest_path_length(G))
unique_shortest_paths = []
for key, value in shortest_paths:
value = dict(value)
value.pop(key)
for k, v in value.items():
comb = tuple(sorted([key, k]) + [v])
unique_shortest_paths.append(comb)
unique_shortest_paths = list(set(unique_shortest_paths))
avg_hopcount_s_paths = np.average([l[2] for l in unique_shortest_paths])
print(f"Average hopcount shortest paths: {avg_hopcount_s_paths}")
# print(f"Average hopcount shortest paths NETWORKX: {nx.average_shortest_path_length(G)}")
print(f"Diameter: {sorted(unique_shortest_paths, key=lambda x: x[2], reverse=True)[0]}")
# Point 7
A_sp = nx.adjacency_spectrum(G)
print(f"Spectral radius: {float(np.max(A_sp))}")
# Point 8
print(f"Algebraic connectivity: {nx.algebraic_connectivity(G)}")