-
Notifications
You must be signed in to change notification settings - Fork 136
/
util.py
173 lines (154 loc) · 6.18 KB
/
util.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
import community
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
# ---- NetworkX compatibility
def node_iter(G):
if float(nx.__version__)<2.0:
return G.nodes()
else:
return G.nodes
def node_dict(G):
if float(nx.__version__)>2.1:
node_dict = G.nodes
else:
node_dict = G.node
return node_dict
# ---------------------------
def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, origin=None):
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False)
canvas = FigureCanvas(fig)
fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
fig.savefig(fname, dpi=1, format=format)
def plot_graph(plt, G):
plt.title('num of nodes: '+str(G.number_of_nodes()), fontsize = 4)
parts = community.best_partition(G)
values = [parts.get(node) for node in G.nodes()]
colors = []
for i in range(len(values)):
if values[i] == 0:
colors.append('red')
if values[i] == 1:
colors.append('green')
if values[i] == 2:
colors.append('blue')
if values[i] == 3:
colors.append('yellow')
if values[i] == 4:
colors.append('orange')
if values[i] == 5:
colors.append('pink')
if values[i] == 6:
colors.append('black')
plt.axis("off")
pos = nx.spring_layout(G)
# pos = nx.spectral_layout(G)
nx.draw_networkx(G, with_labels=True, node_size=4, width=0.3, font_size = 3, node_color=colors,pos=pos)
def draw_graph_list(G_list, row, col, fname = 'figs/test'):
# draw graph view
plt.switch_backend('agg')
for i, G in enumerate(G_list):
plt.subplot(row,col,i+1)
plot_graph(plt, G)
plt.tight_layout()
plt.savefig(fname+'_view.png', dpi=600)
plt.close()
# draw degree distribution
plt.switch_backend('agg')
for i, G in enumerate(G_list):
plt.subplot(row, col, i + 1)
G_deg = np.array(list(G.degree(G.nodes()).values()))
bins = np.arange(20)
plt.hist(np.array(G_deg), bins=bins, align='left')
plt.xlabel('degree', fontsize = 3)
plt.ylabel('count', fontsize = 3)
G_deg_mean = 2*G.number_of_edges()/float(G.number_of_nodes())
plt.title('average degree: {:.2f}'.format(G_deg_mean), fontsize=4)
plt.tick_params(axis='both', which='major', labelsize=3)
plt.tick_params(axis='both', which='minor', labelsize=3)
plt.tight_layout()
plt.savefig(fname+'_degree.png', dpi=600)
plt.close()
# degree_sequence = sorted(nx.degree(G).values(), reverse=True) # degree sequence
# plt.loglog(degree_sequence, 'b-', marker='o')
# plt.title("Degree rank plot")
# plt.ylabel("degree")
# plt.xlabel("rank")
# plt.savefig('figures/degree_view_' + prefix + '.png', dpi=200)
# plt.close()
# draw clustering distribution
#plt.switch_backend('agg')
#for i, G in enumerate(G_list):
# plt.subplot(row, col, i + 1)
# G_cluster = list(nx.clustering(G).values())
# bins = np.linspace(0,1,20)
# plt.hist(np.array(G_cluster), bins=bins, align='left')
# plt.xlabel('clustering coefficient', fontsize=3)
# plt.ylabel('count', fontsize=3)
# G_cluster_mean = sum(G_cluster) / len(G_cluster)
# # if i % 2 == 0:
# # plt.title('real average clustering: {:.4f}'.format(G_cluster_mean), fontsize=4)
# # else:
# # plt.title('pred average clustering: {:.4f}'.format(G_cluster_mean), fontsize=4)
# plt.title('average clustering: {:.4f}'.format(G_cluster_mean), fontsize=4)
# plt.tick_params(axis='both', which='major', labelsize=3)
# plt.tick_params(axis='both', which='minor', labelsize=3)
#plt.tight_layout()
#plt.savefig(fname+'_clustering.png', dpi=600)
#plt.close()
## draw circle distribution
#plt.switch_backend('agg')
#for i, G in enumerate(G_list):
# plt.subplot(row, col, i + 1)
# cycle_len = []
# cycle_all = nx.cycle_basis(G)
# for item in cycle_all:
# cycle_len.append(len(item))
# bins = np.arange(20)
# plt.hist(np.array(cycle_len), bins=bins, align='left')
# plt.xlabel('cycle length', fontsize=3)
# plt.ylabel('count', fontsize=3)
# G_cycle_mean = 0
# if len(cycle_len)>0:
# G_cycle_mean = sum(cycle_len) / len(cycle_len)
# # if i % 2 == 0:
# # plt.title('real average cycle: {:.4f}'.format(G_cycle_mean), fontsize=4)
# # else:
# # plt.title('pred average cycle: {:.4f}'.format(G_cycle_mean), fontsize=4)
# plt.title('average cycle: {:.4f}'.format(G_cycle_mean), fontsize=4)
# plt.tick_params(axis='both', which='major', labelsize=3)
# plt.tick_params(axis='both', which='minor', labelsize=3)
#plt.tight_layout()
#plt.savefig(fname+'_cycle.png', dpi=600)
#plt.close()
## draw community distribution
#plt.switch_backend('agg')
#for i, G in enumerate(G_list):
# plt.subplot(row, col, i + 1)
# parts = community.best_partition(G)
# values = np.array([parts.get(node) for node in G.nodes()])
# counts = np.sort(np.bincount(values)[::-1])
# pos = np.arange(len(counts))
# plt.bar(pos,counts,align = 'edge')
# plt.xlabel('community ID', fontsize=3)
# plt.ylabel('count', fontsize=3)
# G_community_count = len(counts)
# # if i % 2 == 0:
# # plt.title('real average clustering: {}'.format(G_community_count), fontsize=4)
# # else:
# # plt.title('pred average clustering: {}'.format(G_community_count), fontsize=4)
# plt.title('average clustering: {}'.format(G_community_count), fontsize=4)
# plt.tick_params(axis='both', which='major', labelsize=3)
# plt.tick_params(axis='both', which='minor', labelsize=3)
#plt.tight_layout()
#plt.savefig(fname+'_community.png', dpi=600)
#plt.close()
def exp_moving_avg(x, decay=0.9):
shadow = x[0]
a = [shadow]
for v in x[1:]:
shadow -= (1-decay) * (shadow-v)
a.append(shadow)
return a