/
igraph-cite.py
69 lines (57 loc) · 1.87 KB
/
igraph-cite.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 csv
from igraph import *
# Create empty graph
igraph_cite = Graph()
print("Importing citation network graph ...")
# Set of vertex names
vertices = { }
# Open file to citation network
f = open('Week1/cit-HepPh.txt', 'rt')
# Skip comments
f.readline()
f.readline()
f.readline()
f.readline()
# Loop thru file of edges
counter = 0
edges = []
try:
for line in f.readlines():
row = [int(x) for x in line.strip().split('\t')]
print "bp %d" % counter
# Keep running track of all nodes and add one by one
if row[0] not in vertices:
vertices[row[0]] = 1
igraph_cite.add_vertices(row[0])
if row[1] not in vertices:
vertices[row[1]] = 1
igraph_cite.add_vertices(row[1])
# Attempt to add edge to graph
edges.append((row[0], row[1]))
counter = counter + 1
if counter % 100 == 0:
print "Adding another 100 edges (%d total)" % (counter * 100)
try:
igraph_cite.add_edges( edges )
edges = []
except Exception as e:
print e
finally:
f.close()
print("Graph imported!")
print()
print("Edge Betweenness Centrality:")
ebs = g.edge_betweenness()
max_eb = max(ebs)
central_edges = [g.es[idx].tuple for idx, eb in enumerate(ebs) if eb == max_eb] # [(7, 8)]
print("The edge between %s and %s is the most central edge" %
(igraph_cite.vs[central_edges[0][0]]["name"],
igraph_cite.vs[central_edges[0][1]]["name"]))
print("Print out the PageRanks for vertices")
igraph_cite.pagerank()
layout = igraph_cite.layout("kamada_kawai")
#plot(igraph_cite, layout = layout)
igraph_cite.vs["label"] = igraph_cite.vs["name"]
#color_dict = {"m": "blue", "f": "pink"}
#igraph_cite.vs["color"] = [color_dict[gender] for gender in igraph_cite.vs["gender"]]
plot(igraph_cite, layout = layout, bbox = (300, 300), margin = 20)