-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilitiesNetwork.py
206 lines (181 loc) · 7.54 KB
/
UtilitiesNetwork.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 3 00:51:30 2017
@author: anilosmantur
Visiualization functions
"""
import networkx as nx
import matplotlib.pyplot as plt
def drawCommunityGraph(G, pos, part, fgSize, nodeSize=35):
size = float(len(set(part.values())))
print ('Found community count: ', size)
count = 0.
plt.figure(figsize=(fgSize, fgSize))
plt.axis('off')
plt.margins(tight=True)
for com in set(part.values()) :
count = count + 1.
list_nodes = [nodes for nodes in part.keys() if part[nodes] == com]
values = [ (count / size) for nodes in list_nodes]
nodes = nx.draw_networkx_nodes(G,
pos,
list_nodes,
cmap=plt.get_cmap('jet'),
with_labels=False,
node_size = nodeSize,
node_color = values,
vmin=0.0, vmax=1.0 ) # color map magma
nodes.set_edgecolor('black')
nodes.set_linewidth(1.0)
edges = nx.draw_networkx_edges(G, pos, alpha=0.5)
edges.set_linewidth(0.5)
plt.show()
print('Printing community layout finished')
def drawCommunityGraphSave(G, pos, part, fgSize, name):
size = float(len(set(part.values())))
print ('Found community count: ', size)
count = 0.
plt.figure(figsize=(fgSize, fgSize))
plt.axis('off')
plt.margins(tight=True)
for com in set(part.values()) :
count = count + 1.
list_nodes = [nodes for nodes in part.keys() if part[nodes] == com]
values = [ (count / size) for nodes in list_nodes]
nodes = nx.draw_networkx_nodes(G,
pos,
list_nodes,
cmap=plt.get_cmap('jet'),
with_labels=False,
node_size = 35,
node_color = values,
vmin=0.0, vmax=1.0 ) # color map magma
nodes.set_edgecolor('black')
nodes.set_linewidth(1.0)
edges = nx.draw_networkx_edges(G, pos, alpha=0.5)
edges.set_linewidth(0.5)
plt.savefig('graphs/' + name +'_net_communities.png')
print('Printing community layout finished')
def drawCentralityGraph(G, pos, cent, fgSize, nodeSize=35):
count = 0.
plt.figure(figsize=(fgSize, fgSize))
plt.axis('off')
plt.margins(tight=True)
for com in set(cent.values()) :
count = count + 1.
list_nodes = [nodes for nodes in cent.keys() if cent[nodes] == com]
values = [ (400 * com) for nodes in list_nodes]
nodes = nx.draw_networkx_nodes(G,
pos,
list_nodes,
cmap=plt.get_cmap('jet'),
with_labels=False,
node_size = values,
node_color = values,
vmin=0.0, vmax=1.0 ) # color map magma
nodes.set_edgecolor('black')
nodes.set_linewidth(1.0)
edges = nx.draw_networkx_edges(G, pos, alpha=0.5)
edges.set_linewidth(0.5)
plt.show()
print('Printing centrality layout finished')
def drawCentralityGraphSave(G, pos, cent, fgSize, name='', c_type=''):
count = 0.
plt.figure(figsize=(fgSize, fgSize))
plt.axis('off')
plt.margins(tight=True)
for com in set(cent.values()) :
count = count + 1.
list_nodes = [nodes for nodes in cent.keys() if cent[nodes] == com]
values = [ (400 * com) for nodes in list_nodes]
nodes = nx.draw_networkx_nodes(G,
pos,
list_nodes,
cmap=plt.get_cmap('jet'),
with_labels=False,
node_size = values,
node_color = values,
vmin=0.0, vmax=1.0 ) # color map magma
nodes.set_edgecolor('black')
nodes.set_linewidth(1.0)
edges = nx.draw_networkx_edges(G, pos, alpha=0.5)
edges.set_linewidth(0.5)
plt.savefig('graphs/' + name +'_net_'+ c_type + '.png')
print('Printing centrality layout finished')
def nxDrawCommunityGraph(G, pos, coms, fgSize, nodeSize=35):
size = len(coms)
print ('community count: ', size)
count = 0.
plt.figure(figsize=(fgSize, fgSize))
plt.axis('off')
plt.margins(tight=True)
for com in coms :
count = count + 1.
list_nodes = [nodes for nodes in com]
values = [ (count / size) for nodes in list_nodes]
nodes = nx.draw_networkx_nodes(G,
pos,
list_nodes,
cmap=plt.get_cmap('jet'),
with_labels=False,
node_size = nodeSize,
node_color = values,
vmin=0.0, vmax=1.0 ) # color map magma
nodes.set_edgecolor('black')
nodes.set_linewidth(1.0)
edges = nx.draw_networkx_edges(G, pos, alpha=0.5)
edges.set_linewidth(0.5)
plt.show()
print('Printing community layout finished')
def drawGraph(G, pos, fgSize, nodeSize=35):
plt.figure(figsize=(fgSize, fgSize))
plt.axis('off')
plt.margins(tight=True)
nodes = nx.draw_networkx_nodes(G, pos, node_size=nodeSize, node_color='red')
nodes.set_edgecolor('black')
nodes.set_linewidth(1.0)
edges = nx.draw_networkx_edges(G, pos, edge_color='blue')
edges.set_linewidth(0.5)
plt.show()
print('Printing layout finished')
def drawGraphSave(G, pos, fgSize, name=''):
plt.figure(figsize=(fgSize, fgSize))
plt.axis('off')
plt.margins(tight=True)
nodes = nx.draw_networkx_nodes(G, pos, node_size=35, node_color='red')
nodes.set_edgecolor('black')
nodes.set_linewidth(1.0)
edges = nx.draw_networkx_edges(G, pos, edge_color='blue')
edges.set_linewidth(0.5)
plt.savefig('graphs/' + name +'_net.png')
print('Printing layout finished')
def drawEgoGraph(G, pos, egoNode, fgSize):
plt.figure(figsize=(fgSize, fgSize))
plt.axis('off')
plt.margins(tight=True)
nodes = nx.draw_networkx_nodes(G, pos, node_size=35, node_color='green')
nodes.set_edgecolor('black')
nodes.set_linewidth(1.0)
edges = nx.draw_networkx_edges(G, pos, edge_color='blue')
edges.set_linewidth(0.5)
plt.show()
print('Printing Ego layout finished')
def centralityPlot(cent, fgSize):
plt.figure(figsize=(fgSize, fgSize))
plt.margins(tight=True)
cent = sorted(cent.items())
values = [ c for (node, c) in cent]
nodes = [ node for (node, c) in cent]
plt.plot(nodes, values)
plt.show()
print('Printing Centrality Plot Finished')
def centralityPlotSave(cent, fgSize, name='', c_type=''):
plt.figure(figsize=(fgSize, fgSize))
plt.margins(tight=True)
cent = sorted(cent.items())
values = [ c for (node, c) in cent]
nodes = [ node for (node, c) in cent]
plt.plot(nodes, values)
plt.savefig('graphs/' + name +'_net_plot_'+ c_type + '.png')
print('Printing Centrality Plot Finished')