-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnwXtext.py
40 lines (32 loc) · 1.37 KB
/
nwXtext.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
import gzip
from numpy import append, average
import networkx as nx
import matplotlib.pyplot as plt
from platform import processor
from mpi4py import MPI
raw = gzip.open('facebook_combined.txt.gz')
#raw = gzip.open('twitter_combined.txt.gz')
#raw = gzip.open('twitter_combined_reduced.txt.gz')
# Read in dataset using NetworkX
G = nx.read_edgelist(raw, create_using=nx.DiGraph(), nodetype=int)
G_und = G.to_undirected()
print(G)
# Calculate closeness centrality using NetworkX for testing
closeness = nx.closeness_centrality(G_und)
#1m 7.8s for facebook_combined.txt.gz
#0.2s for twitter_combined_reduced.txt.gz
# Write betweeness centrality into file output_nwx_twitter_reduced.txt
# with open('output_nwx_twitter_reduced.txt', 'w') as f:
# for key in closeness:
# f.write(str(key) + ' ' + str(closeness[key]) + '\n')
top_five = sorted(closeness.items(), key=lambda x: x[1], reverse=True)[:5]
print("The top 5 nodes are: "+ str(top_five))
print("The average of the closeness centrality values of all nodes is: "+ str(average(list(closeness.values()))))
# Write betweeness centrality into file output_nwx_fb.txt
# with open('output_nwx_fb.txt', 'w') as f:
# for key in closeness:
# f.write(str(key) + ' ' + str(closeness[key]) + '\n')
# Make a histogram for closeness centrality
plt.hist(closeness.values(), bins=100)
plt.title('Closeness Centrality')
plt.show()