-
Notifications
You must be signed in to change notification settings - Fork 176
/
gengraph.py
295 lines (228 loc) · 10.3 KB
/
gengraph.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""gengraph.py
Generating and manipulaton the synthetic graphs needed for the paper's experiments.
"""
import os
from matplotlib import pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.colors as colors
# Set matplotlib backend to file writing
plt.switch_backend("agg")
import networkx as nx
import numpy as np
from tensorboardX import SummaryWriter
from utils import synthetic_structsim
from utils import featgen
import utils.io_utils as io_utils
####################################
#
# Experiment utilities
#
####################################
def perturb(graph_list, p):
""" Perturb the list of (sparse) graphs by adding/removing edges.
Args:
p: proportion of added edges based on current number of edges.
Returns:
A list of graphs that are perturbed from the original graphs.
"""
perturbed_graph_list = []
for G_original in graph_list:
G = G_original.copy()
edge_count = int(G.number_of_edges() * p)
# randomly add the edges between a pair of nodes without an edge.
for _ in range(edge_count):
while True:
u = np.random.randint(0, G.number_of_nodes())
v = np.random.randint(0, G.number_of_nodes())
if (not G.has_edge(u, v)) and (u != v):
break
G.add_edge(u, v)
perturbed_graph_list.append(G)
return perturbed_graph_list
def join_graph(G1, G2, n_pert_edges):
""" Join two graphs along matching nodes, then perturb the resulting graph.
Args:
G1, G2: Networkx graphs to be joined.
n_pert_edges: number of perturbed edges.
Returns:
A new graph, result of merging and perturbing G1 and G2.
"""
assert n_pert_edges > 0
F = nx.compose(G1, G2)
edge_cnt = 0
while edge_cnt < n_pert_edges:
node_1 = np.random.choice(G1.nodes())
node_2 = np.random.choice(G2.nodes())
F.add_edge(node_1, node_2)
edge_cnt += 1
return F
def preprocess_input_graph(G, labels, normalize_adj=False):
""" Load an existing graph to be converted for the experiments.
Args:
G: Networkx graph to be loaded.
labels: Associated node labels.
normalize_adj: Should the method return a normalized adjacency matrix.
Returns:
A dictionary containing adjacency, node features and labels
"""
adj = np.array(nx.to_numpy_matrix(G))
if normalize_adj:
sqrt_deg = np.diag(1.0 / np.sqrt(np.sum(adj, axis=0, dtype=float).squeeze()))
adj = np.matmul(np.matmul(sqrt_deg, adj), sqrt_deg)
existing_node = list(G.nodes)[-1]
feat_dim = G.nodes[existing_node]["feat"].shape[0]
f = np.zeros((G.number_of_nodes(), feat_dim), dtype=float)
for i, u in enumerate(G.nodes()):
f[i, :] = G.nodes[u]["feat"]
# add batch dim
adj = np.expand_dims(adj, axis=0)
f = np.expand_dims(f, axis=0)
labels = np.expand_dims(labels, axis=0)
return {"adj": adj, "feat": f, "labels": labels}
####################################
#
# Generating synthetic graphs
#
###################################
def gen_syn1(nb_shapes=80, width_basis=300, feature_generator=None, m=5):
""" Synthetic Graph #1:
Start with Barabasi-Albert graph and attach house-shaped subgraphs.
Args:
nb_shapes : The number of shapes (here 'houses') that should be added to the base graph.
width_basis : The width of the basis graph (here 'Barabasi-Albert' random graph).
feature_generator : A `FeatureGenerator` for node features. If `None`, add constant features to nodes.
m : number of edges to attach to existing node (for BA graph)
Returns:
G : A networkx graph
role_id : A list with length equal to number of nodes in the entire graph (basis
: + shapes). role_id[i] is the ID of the role of node i. It is the label.
name : A graph identifier
"""
basis_type = "ba"
list_shapes = [["house"]] * nb_shapes
plt.figure(figsize=(8, 6), dpi=300)
G, role_id, _ = synthetic_structsim.build_graph(
width_basis, basis_type, list_shapes, start=0, m=5
)
G = perturb([G], 0.01)[0]
if feature_generator is None:
feature_generator = featgen.ConstFeatureGen(1)
feature_generator.gen_node_features(G)
name = basis_type + "_" + str(width_basis) + "_" + str(nb_shapes)
return G, role_id, name
def gen_syn2(nb_shapes=100, width_basis=350):
""" Synthetic Graph #2:
Start with Barabasi-Albert graph and add node features indicative of a community label.
Args:
nb_shapes : The number of shapes (here 'houses') that should be added to the base graph.
width_basis : The width of the basis graph (here 'Barabasi-Albert' random graph).
Returns:
G : A networkx graph
label : Label of the nodes (determined by role_id and community)
name : A graph identifier
"""
basis_type = "ba"
random_mu = [0.0] * 8
random_sigma = [1.0] * 8
# Create two grids
mu_1, sigma_1 = np.array([-1.0] * 2 + random_mu), np.array([0.5] * 2 + random_sigma)
mu_2, sigma_2 = np.array([1.0] * 2 + random_mu), np.array([0.5] * 2 + random_sigma)
feat_gen_G1 = featgen.GaussianFeatureGen(mu=mu_1, sigma=sigma_1)
feat_gen_G2 = featgen.GaussianFeatureGen(mu=mu_2, sigma=sigma_2)
G1, role_id1, name = gen_syn1(feature_generator=feat_gen_G1, m=4)
G2, role_id2, name = gen_syn1(feature_generator=feat_gen_G2, m=4)
G1_size = G1.number_of_nodes()
num_roles = max(role_id1) + 1
role_id2 = [r + num_roles for r in role_id2]
label = role_id1 + role_id2
# Edit node ids to avoid collisions on join
g1_map = {n: i for i, n in enumerate(G1.nodes())}
G1 = nx.relabel_nodes(G1, g1_map)
g2_map = {n: i + G1_size for i, n in enumerate(G2.nodes())}
G2 = nx.relabel_nodes(G2, g2_map)
# Join
n_pert_edges = width_basis
G = join_graph(G1, G2, n_pert_edges)
name = basis_type + "_" + str(width_basis) + "_" + str(nb_shapes) + "_2comm"
return G, label, name
def gen_syn3(nb_shapes=80, width_basis=300, feature_generator=None, m=5):
""" Synthetic Graph #3:
Start with Barabasi-Albert graph and attach grid-shaped subgraphs.
Args:
nb_shapes : The number of shapes (here 'grid') that should be added to the base graph.
width_basis : The width of the basis graph (here 'Barabasi-Albert' random graph).
feature_generator : A `FeatureGenerator` for node features. If `None`, add constant features to nodes.
m : number of edges to attach to existing node (for BA graph)
Returns:
G : A networkx graph
role_id : Role ID for each node in synthetic graph.
name : A graph identifier
"""
basis_type = "ba"
list_shapes = [["grid", 3]] * nb_shapes
plt.figure(figsize=(8, 6), dpi=300)
G, role_id, _ = synthetic_structsim.build_graph(
width_basis, basis_type, list_shapes, start=0, m=5
)
G = perturb([G], 0.01)[0]
if feature_generator is None:
feature_generator = featgen.ConstFeatureGen(1)
feature_generator.gen_node_features(G)
name = basis_type + "_" + str(width_basis) + "_" + str(nb_shapes)
return G, role_id, name
def gen_syn4(nb_shapes=60, width_basis=8, feature_generator=None, m=4):
""" Synthetic Graph #4:
Start with a tree and attach cycle-shaped subgraphs.
Args:
nb_shapes : The number of shapes (here 'houses') that should be added to the base graph.
width_basis : The width of the basis graph (here a random 'Tree').
feature_generator : A `FeatureGenerator` for node features. If `None`, add constant features to nodes.
m : The tree depth.
Returns:
G : A networkx graph
role_id : Role ID for each node in synthetic graph
name : A graph identifier
"""
basis_type = "tree"
list_shapes = [["cycle", 6]] * nb_shapes
fig = plt.figure(figsize=(8, 6), dpi=300)
G, role_id, plugins = synthetic_structsim.build_graph(
width_basis, basis_type, list_shapes, start=0
)
G = perturb([G], 0.01)[0]
if feature_generator is None:
feature_generator = featgen.ConstFeatureGen(1)
feature_generator.gen_node_features(G)
name = basis_type + "_" + str(width_basis) + "_" + str(nb_shapes)
path = os.path.join("log/syn4_base_h20_o20")
writer = SummaryWriter(path)
io_utils.log_graph(writer, G, "graph/full")
return G, role_id, name
def gen_syn5(nb_shapes=80, width_basis=8, feature_generator=None, m=3):
""" Synthetic Graph #5:
Start with a tree and attach grid-shaped subgraphs.
Args:
nb_shapes : The number of shapes (here 'houses') that should be added to the base graph.
width_basis : The width of the basis graph (here a random 'grid').
feature_generator : A `FeatureGenerator` for node features. If `None`, add constant features to nodes.
m : The tree depth.
Returns:
G : A networkx graph
role_id : Role ID for each node in synthetic graph
name : A graph identifier
"""
basis_type = "tree"
list_shapes = [["grid", m]] * nb_shapes
plt.figure(figsize=(8, 6), dpi=300)
G, role_id, _ = synthetic_structsim.build_graph(
width_basis, basis_type, list_shapes, start=0
)
G = perturb([G], 0.1)[0]
if feature_generator is None:
feature_generator = featgen.ConstFeatureGen(1)
feature_generator.gen_node_features(G)
name = basis_type + "_" + str(width_basis) + "_" + str(nb_shapes)
path = os.path.join("log/syn5_base_h20_o20")
writer = SummaryWriter(path)
return G, role_id, name