-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeGraphSample.py
More file actions
97 lines (78 loc) · 2.43 KB
/
makeGraphSample.py
File metadata and controls
97 lines (78 loc) · 2.43 KB
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
import pickle
import distinctipy
from igraph import Graph
# set to -1 to do all input data
MAX_NODES = -1
NODE_INTERVAL = 10
with open('final/combinedOUT.pkl', 'rb') as f:
outEdgesDict = pickle.load(f)
with open('final/communitiesSCC.pkl', 'rb') as f:
communities = pickle.load(f)
with open('final/communityColors.pkl', 'rb') as f:
colors = pickle.load(f)
colorsTrans = []
for color in colors:
colorsTrans.append((color[0], color[1], color[2], 0.95))
# get community sizes and determine where to end
commSizes = []
for i in range(len(communities)):
commSizes.append(len(communities[i]))
# TOP 42 COMMUNITIES WILL BE USED
communitiesToUse = 42
# (after 42 it drops from 2416 nodes to 13 nodes)
listNames = []
communityNum = []
nameToCommunity = {}
count = 0
for i in range(communitiesToUse):
for node in communities[i]:
if (count % NODE_INTERVAL == 0):
listNames.append(node)
communityNum.append(i)
nameToCommunity[node] = i
count += 1
if ((MAX_NODES != -1) and (count >= MAX_NODES)):
break
print("Finished node collection.")
setNames = set(listNames)
dictNodeToNum = {}
count = 0
# init unique node # for each node
for i in range(len(listNames)):
if (count % 10000 == 0):
print("Dict translation finished: "+str(count))
dictNodeToNum[listNames[i]] = i
count += 1
print("-----------------------------------")
count = 0
edges = []
edgeColors = []
# init edges
for node in listNames:
if ((MAX_NODES != -1) and count >= MAX_NODES):
break
if (count % 10000 == 0):
print("Nodes finished: "+str(count))
curCom = nameToCommunity[node]
# some nodes don't actually exist in real graph >:(
try:
for dest in outEdgesDict[node]:
if dest in setNames:
edges.append([dictNodeToNum[node], dictNodeToNum[dest]])
if (curCom == nameToCommunity[dest]):
edgeColors.append(colorsTrans[curCom])
else:
edgeColors.append((255, 255, 255, 0.1))
count += 1
except:
pass
print("Finished edge addition with edges: " + str(count))
# g = Graph(n=len(listNames), edges=edges, directed=True)
g = Graph(n=len(listNames), edges=edges)
# init names
g.vs["name"] = listNames
g.vs["community"] = communityNum
g.es["color"] = edgeColors
print("Saving graph...")
g.write_pickle("final/sampleFinishedGRAPH.net")
print("Outputted graph.")