-
Notifications
You must be signed in to change notification settings - Fork 48
/
generate_data.py
323 lines (271 loc) · 15.8 KB
/
generate_data.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import os
import numpy as np
import pickle
import argparse
import networkx as nx
import datetime
import random
import multiprocessing as mp
from utils import *
def parse_args():
parser = argparse.ArgumentParser(description='Generate synthetic graph datasets')
parser.add_argument('-D', '--dataset', type=str, default='colors', choices=['colors', 'triangles'])
parser.add_argument('-o', '--out_dir', type=str, default='./data', help='path where to save superpixels')
parser.add_argument('--N_train', type=int, default=500, help='number of training graphs (500 for colors and 30000 for triangles)')
parser.add_argument('--N_val', type=int, default=2500, help='number of graphs in the validation set (2500 for colors and 5000 for triangles)')
parser.add_argument('--N_test', type=int, default=2500, help='number of graphs in each test subset (2500 for colors and 5000 for triangles)')
parser.add_argument('--label_min', type=int, default=0,
help='smallest label value for a graph (i.e. smallest number of green nodes); 1 for triangles')
parser.add_argument('--label_max', type=int, default=10,
help='largest label value for a graph (i.e. largest number of green nodes)')
parser.add_argument('--N_min', type=int, default=4, help='minimum number of nodes')
parser.add_argument('--N_max', type=int, default=200, help='maximum number of nodes (default: 200 for colors and 100 for triangles')
parser.add_argument('--N_max_train', type=int, default=25, help='maximum number of nodes in the training set')
parser.add_argument('--dim', type=int, default=3, help='node feature dimensionality')
parser.add_argument('--green_ch_index', type=int, default=1,
help='index of non-zero value in a one-hot node feature vector, '
'i.e. [0, 1, 0] in case green_channel_index=1 and dim=3')
parser.add_argument('--seed', type=int, default=111, help='seed for shuffling nodes')
parser.add_argument('--threads', type=int, default=0, help='only for triangles')
args = parser.parse_args()
for arg in vars(args):
print(arg, getattr(args, arg))
return args
def check_graph_duplicates(Adj_matrices, node_features=None):
n_graphs = len(Adj_matrices)
print('check for duplicates for %d graphs' % n_graphs)
n_duplicates = 0
for i in range(n_graphs):
if node_features is not None:
assert Adj_matrices[i].shape[0] == node_features[i].shape[0], (
'invalid data', i, Adj_matrices[i].shape[0], node_features[i].shape[0])
for j in range(i + 1, n_graphs):
if Adj_matrices[i].shape[0] == Adj_matrices[j].shape[0]:
if np.allclose(Adj_matrices[i], Adj_matrices[j]): # adjacency matrices are the same
# for Colors graphs are not considered duplicates if they have the same adjacency matrix,
# but different node features
if node_features is None or np.allclose(node_features[i], node_features[j]):
n_duplicates += 1
print('duplicates %d/%d' % (n_duplicates, n_graphs * (n_graphs - 1) / 2))
if n_duplicates > 0:
raise ValueError('%d duplicates found in the dataset' % n_duplicates)
print('no duplicated graphs')
# COLORS
def get_node_features_Colors(N_nodes, N_green, dim, green_ch_index=1, new_colors=False):
node_features = np.zeros((N_nodes, dim))
# Generate indices for non-zero values,
# so that the total number of nodes with features having value 1 in the green_ch_index equals N_green
idx_not_green = rnd.randint(0, dim - 1, size=N_nodes - N_green) # for dim=3 generate values 0,1 for non-green nodes
idx_non_zero = np.concatenate((idx_not_green, np.zeros(N_green, np.int) + dim - 1)) # make green_ch_index=2 temporary
idx_non_zero_cp = idx_non_zero.copy()
idx_non_zero[idx_non_zero_cp == dim - 1] = green_ch_index # set idx_non_zero=1 for green nodes
idx_non_zero[idx_non_zero_cp == green_ch_index] = dim - 1 # set idx_non_zero=2 for those nodes that were green temporary
rnd.shuffle(idx_non_zero) # shuffle nodes
node_features[np.arange(N_nodes), idx_non_zero] = 1
if new_colors:
for ind in np.where(idx_non_zero != green_ch_index)[0]: # for non-green nodes
node_features[ind] = rnd.randint(0, 2, size=dim)
node_features[ind, green_ch_index] = 0 # set value at green_ch_index to 0 to avoid confusion with green nodes
label = np.sum((np.sum(node_features, 1) == node_features[:, green_ch_index]) & (node_features[:, green_ch_index] == 1))
gt_attn = (idx_non_zero == green_ch_index).reshape(-1, 1)
label2 = np.sum(gt_attn)
assert N_green == label == label2, ('invalid node features', N_green, label, label2)
return node_features, idx_non_zero, gt_attn
def generate_graphs_Colors(N_graphs, N_min, N_max, dim, args, rnd, new_colors=False):
Adj_matrices, node_features, GT_attn, graph_labels, N_edges = [], [], [], [], []
n_labels = args.label_max - args.label_min + 1
n_graphs_per_shape = int(np.ceil(N_graphs / (N_max - N_min + 1) / n_labels) * n_labels)
for n_nodes in np.array(range(N_min, N_max + 1)):
c = 0
while True:
labels = np.arange(args.label_min, n_labels)
labels = labels[labels <= n_nodes]
rnd.shuffle(labels)
for lbl in labels:
features, idx_non_zero, gt_attn = get_node_features_Colors(N_nodes=n_nodes,
N_green=lbl,
dim=dim,
green_ch_index=args.green_ch_index,
new_colors=new_colors)
n_edges = int((rnd.rand() + 1) * n_nodes)
A = nx.to_numpy_array(nx.gnm_random_graph(n_nodes, n_edges))
add = True
for k in range(len(Adj_matrices)):
if A.shape[0] == Adj_matrices[k].shape[0] and np.allclose(A, Adj_matrices[k]):
if np.allclose(node_features[k], features):
add = False
break
if add:
Adj_matrices.append(A.astype(np.bool)) # binary adjacency matrix
graph_labels.append(lbl)
node_features.append(features.astype(np.bool)) # binary features
GT_attn.append(gt_attn) # binary GT attention
N_edges.append(n_edges)
c += 1
if c >= n_graphs_per_shape:
break
if c >= n_graphs_per_shape:
break
graph_labels = np.array(graph_labels, np.int32)
N_edges = np.array(N_edges, np.int32)
print(N_graphs, len(graph_labels))
return {'Adj_matrices': Adj_matrices,
'GT_attn': GT_attn, # not normalized to sum=1
'graph_labels': graph_labels,
'node_features': node_features,
'N_edges': N_edges}
# TRIANGLES
def get_gt_atnn_triangles(args):
G, N = args
node_ids = []
if G is not None:
for clq in nx.enumerate_all_cliques(G):
if len(clq) == 3:
node_ids.extend(clq)
node_ids = np.array(node_ids)
gt_attn = np.zeros((N, 1), np.int32)
for i in np.unique(node_ids):
gt_attn[i] = int(np.sum(node_ids == i))
return gt_attn # unnormalized (do not sum to 1, i.e. use int32 for storage efficiency)
def get_graph_triangles(args):
N_nodes, rnd = args
N_edges = int((rnd.rand() + 1) * N_nodes)
G = nx.dense_gnm_random_graph(N_nodes, N_edges, seed=None)
A = nx.to_numpy_array(G)
A_cube = A.dot(A).dot(A)
label = int(np.trace(A_cube) / 6.) # number of triangles
return A.astype(np.bool), label, N_edges, G
def generate_graphs_Triangles(N_graphs, N_min, N_max, args, rnd):
N_nodes = rnd.randint(N_min, N_max + 1, size=int(N_graphs * 10))
print('generating %d graphs with %d-%d nodes' % (N_graphs * 10, N_min, N_max))
if args.threads > 0:
with mp.Pool(processes=args.threads) as pool:
data = pool.map(get_graph_triangles, [(N_nodes[i], rnd) for i in range(len(N_nodes))])
else:
data = [get_graph_triangles((N_nodes[i], rnd)) for i in range(len(N_nodes))]
labels = np.array([data[i][1] for i in range(len(data))], np.int32)
Adj_matrices, node_features, G, graph_labels, N_edges, node_degrees = [], [], [], [], [], []
for lbl in range(args.label_min, args.label_max + 1):
idx = np.where(labels == lbl)[0]
c = 0
for i in idx:
add = True
for k in range(len(Adj_matrices)):
if data[i][0].shape[0] == Adj_matrices[k].shape[0] and labels[i] == graph_labels[k] and np.allclose(data[i][0], Adj_matrices[k]):
add = False
break
if add:
Adj_matrices.append(data[i][0])
graph_labels.append(labels[i])
G.append(data[i][3])
N_edges.append(data[i][2])
node_degrees.append(data[i][0].astype(np.int32).sum(1).max())
c += 1
if c >= int(N_graphs / (args.label_max - args.label_min + 1)):
break
print('label={}, number of graphs={}/{}, total number of generated graphs={}'.format(lbl, c, len(idx), len(Adj_matrices)))
assert c == int(N_graphs / (args.label_max - args.label_min + 1)), (
'invalid data', c, int(N_graphs / (args.label_max - args.label_min + 1)))
print('computing GT attention for %d graphs' % len(Adj_matrices))
if args.threads > 0:
with mp.Pool(processes=args.threads) as pool:
GT_attn = pool.map(get_gt_atnn_triangles, [(G[i], Adj_matrices[i].shape[0]) for i in range(len(Adj_matrices))])
else:
GT_attn = [get_gt_atnn_triangles((G[i], Adj_matrices[i].shape[0])) for i in range(len(Adj_matrices))]
graph_labels = np.array(graph_labels, np.int32)
N_edges = np.array(N_edges, np.int32)
return {'Adj_matrices': Adj_matrices,
'GT_attn': GT_attn, # not normalized to sum=1
'graph_labels': graph_labels,
'N_edges': N_edges,
'Max_degree': np.max(node_degrees)}
if __name__ == '__main__':
dt = datetime.datetime.now()
print('start time:', dt)
args = parse_args()
if not os.path.isdir(args.out_dir):
os.mkdir(args.out_dir)
random.seed(args.seed) # for networkx
np.random.seed(args.seed)
rnd = np.random.RandomState(args.seed)
def print_stats(data, split_name):
print('%s: %d graphs' % (split_name, len(data['graph_labels'])))
for lbl in np.unique(data['graph_labels']):
print('%s: label=%d, %d graphs' % (split_name, lbl, np.sum(data['graph_labels'] == lbl)))
if args.dataset.lower() == 'colors':
# Generate train and test sets
data_test_combined, Adj_matrices, node_features = [], [], []
for N_graphs, N_nodes_min, N_nodes_max, dim, name in zip([args.N_train + args.N_val + args.N_test, args.N_test, args.N_test],
[args.N_min, args.N_max_train + 1, args.N_max_train + 1],
[args.N_max_train, args.N_max, args.N_max],
[args.dim, args.dim, args.dim + 1],
['test orig', 'test large', 'test large-c']):
data = generate_graphs_Colors(N_graphs, N_nodes_min, N_nodes_max, dim, args, rnd, new_colors=dim==args.dim + 1)
if name.find('orig') >= 0:
idx = rnd.permutation(len(data['graph_labels']))
data_train = copy_data(data, idx[:args.N_train])
print_stats(data_train, name.replace('test', 'train'))
node_features += data_train['node_features']
Adj_matrices += data_train['Adj_matrices']
data_val = copy_data(data, idx[args.N_train: args.N_train + args.N_val])
print_stats(data_val, name.replace('test', 'val'))
node_features += data_val['node_features']
Adj_matrices += data_val['Adj_matrices']
data_test = copy_data(data, idx[args.N_train + args.N_val: args.N_train + args.N_val + args.N_test])
else:
data_test = copy_data(data, rnd.permutation(len(data['graph_labels']))[:args.N_test])
Adj_matrices += data_test['Adj_matrices']
node_features += data_test['node_features']
data_test_combined.append(data_test)
print_stats(data_test, name)
# Check for duplicates in the combined train+val+test sets
check_graph_duplicates(Adj_matrices, node_features)
# Saving
with open('%s/random_graphs_colors_dim%d_train.pkl' % (args.out_dir, args.dim), 'wb') as f:
pickle.dump(data_train, f, protocol=2)
with open('%s/random_graphs_colors_dim%d_val.pkl' % (args.out_dir, args.dim), 'wb') as f:
pickle.dump(data_val, f, protocol=2)
with open('%s/random_graphs_colors_dim%d_test.pkl' % (args.out_dir, args.dim), 'wb') as f:
pickle.dump(concat_data(data_test_combined), f, protocol=2)
elif args.dataset.lower() == 'triangles':
data = generate_graphs_Triangles((args.N_train + args.N_val + args.N_test), args.N_min, args.N_max_train, args, rnd)
# Create balanced splits
idx_train, idx_val, idx_test = [], [], []
classes = np.unique(data['graph_labels'])
n_classes = len(classes)
for lbl in classes:
idx = np.where(data['graph_labels'] == lbl)[0]
rnd.shuffle(idx)
n_train = int(args.N_train / n_classes)
n_val = int(args.N_val / n_classes)
n_test = int(args.N_test / n_classes)
idx_train.append(idx[:n_train])
idx_val.append(idx[n_train: n_train + n_val])
idx_test.append(idx[n_train + n_val: n_train + n_val + n_test])
data_train = copy_data(data, np.concatenate(idx_train))
print_stats(data_train, 'train orig')
data_val = copy_data(data, np.concatenate(idx_val))
print_stats(data_val, 'val orig')
data_test = copy_data(data, np.concatenate(idx_test))
print_stats(data_test, 'test orig')
data = generate_graphs_Triangles(args.N_test, args.N_max_train + 1, args.N_max, args, rnd)
data_test_large = copy_data(data, rnd.permutation(len(data['graph_labels']))[:args.N_test])
print_stats(data_test_large, 'test large')
check_graph_duplicates(data_train['Adj_matrices'] + data_val['Adj_matrices'] +
data_test['Adj_matrices'] + data_test_large['Adj_matrices'])
# Saving
# Max degree is max over all graphs in the training and test sets
max_degree = np.max(np.array([d['Max_degree'] for d in (data_train, data_val, data_test, data_test_large)]))
data_train['Max_degree'] = max_degree
with open('%s/random_graphs_triangles_train.pkl' % args.out_dir, 'wb') as f:
pickle.dump(data_train, f, protocol=2)
data_val['Max_degree'] = max_degree
with open('%s/random_graphs_triangles_val.pkl' % args.out_dir, 'wb') as f:
pickle.dump(data_val, f, protocol=2)
data_test = concat_data((data_test, data_test_large))
data_test['Max_degree'] = max_degree
with open('%s/random_graphs_triangles_test.pkl' % args.out_dir, 'wb') as f:
pickle.dump(data_test, f, protocol=2)
else:
raise NotImplementedError('unsupported dataset: ' + args.dataset)
print('done in {}'.format(datetime.datetime.now() - dt))