-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphml_1.py
67 lines (58 loc) · 2.19 KB
/
graphml_1.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
import urllib.request
import io
import zipfile
import numpy as np
import networkx as nx
import matplotlib as mpl
import matplotlib.pyplot as plt
def GetGraphMetrics(graph):
graph_degree = dict(graph.degree)
print("Graph Summary:")
print(f"Number of nodes : {len(graph.nodes)}")
print(f"Number of edges : {len(graph.edges)}")
print(f"Maximum degree : {np.max(list(graph_degree.values()))}")
print(f"Minimum degree : {np.min(list(graph_degree.values()))}")
print(f"Average degree : {np.mean(list(graph_degree.values()))}")
print(f"Median degree : {np.median(list(graph_degree.values()))}")
print("")
print("Graph Connectivity")
try:
print(f"Connected Components : {nx.number_connected_components(graph)}")
except:
print(f"Strongly Connected Components : {nx.number_strongly_connected_components(graph)}")
print(f"Weakly Connected Components : {nx.number_weakly_connected_components(graph)}")
print("")
print("Graph Distance")
print(f"Average Distance : {nx.average_shortest_path_length(graph)}")
print(f"Diameter : {nx.algorithms.distance_measures.diameter(graph)}")
print("")
print("Graph Clustering")
print(f"Transitivity : {nx.transitivity(graph)}")
print(f"Average Clustering Coefficient : {nx.average_clustering(graph)}")
return None
url = "http://www-personal.umich.edu/~mejn/netdata/football.zip"
sock = urllib.request.urlopen(url) # open URL
s = io.BytesIO(sock.read()) # read into BytesIO "file"
sock.close()
zf = zipfile.ZipFile(s) # zipfile object
txt = zf.read("football.txt").decode() # read info file
gml = zf.read("football.gml").decode() # read gml data
# throw away bogus first line with # from mejn files
gml = gml.split("\n")[1:]
G = nx.parse_gml(gml) # parse gml data
d = dict(G.degree)
low, *_, high = sorted(d.values())
norm = mpl.colors.Normalize(vmin=low, vmax=high, clip=True)
mapper = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.twilight_shifted)
options = {
"node_size": 50,
"linewidths": 0,
"width": 0.1,
}
plt.figure(figsize =(30, 18))
nx.draw(G,
nodelist=d,
node_color=[mapper.to_rgba(i)
for i in d.values()])
plt.show()
GetGraphMetrics(G)