-
Notifications
You must be signed in to change notification settings - Fork 0
/
aug.py
424 lines (322 loc) · 15.1 KB
/
aug.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import os
import os.path as osp
import shutil
import torch
from torch_geometric.data import InMemoryDataset, download_url, extract_zip
from torch_geometric.io import read_tu_data
from itertools import repeat, product
import numpy as np
from copy import deepcopy
import pdb
class TUDataset_aug(InMemoryDataset):
r"""A variety of graph kernel benchmark datasets, *.e.g.* "IMDB-BINARY",
"REDDIT-BINARY" or "PROTEINS", collected from the `TU Dortmund University
<https://chrsmrrs.github.io/datasets>`_.
In addition, this dataset wrapper provides `cleaned dataset versions
<https://github.com/nd7141/graph_datasets>`_ as motivated by the
`"Understanding Isomorphism Bias in Graph Data Sets"
<https://arxiv.org/abs/1910.12091>`_ paper, containing only non-isomorphic
graphs.
.. note::
Some datasets may not come with any node labels.
You can then either make use of the argument :obj:`use_node_attr`
to load additional continuous node attributes (if present) or provide
synthetic node features using transforms such as
like :class:`torch_geometric.transforms.Constant` or
:class:`torch_geometric.transforms.OneHotDegree`.
Args:
root (string): Root directory where the dataset should be saved.
name (string): The `name
<https://chrsmrrs.github.io/datasets/docs/datasets/>`_ of the
dataset.
transform (callable, optional): A function/transform that takes in an
:obj:`torch_geometric.data.Data` object and returns a transformed
version. The data object will be transformed before every access.
(default: :obj:`None`)
pre_transform (callable, optional): A function/transform that takes in
an :obj:`torch_geometric.data.Data` object and returns a
transformed version. The data object will be transformed before
being saved to disk. (default: :obj:`None`)
pre_filter (callable, optional): A function that takes in an
:obj:`torch_geometric.data.Data` object and returns a boolean
value, indicating whether the data object should be included in the
final dataset. (default: :obj:`None`)
use_node_attr (bool, optional): If :obj:`True`, the dataset will
contain additional continuous node attributes (if present).
(default: :obj:`False`)
use_edge_attr (bool, optional): If :obj:`True`, the dataset will
contain additional continuous edge attributes (if present).
(default: :obj:`False`)
cleaned: (bool, optional): If :obj:`True`, the dataset will
contain only non-isomorphic graphs. (default: :obj:`False`)
"""
url = ('http://ls11-www.cs.tu-dortmund.de/people/morris/'
'graphkerneldatasets')
cleaned_url = ('https://raw.githubusercontent.com/nd7141/'
'graph_datasets/master/datasets')
def __init__(self, root, name, transform=None, pre_transform=None,
pre_filter=None, use_node_attr=False, use_edge_attr=False,
cleaned=False, aug=None):
self.name = name
self.cleaned = cleaned
super(TUDataset_aug, self).__init__(root, transform, pre_transform,
pre_filter)
self.data, self.slices = torch.load(self.processed_paths[0])
if self.data.x is not None and not use_node_attr:
num_node_attributes = self.num_node_attributes
self.data.x = self.data.x[:, num_node_attributes:]
if self.data.edge_attr is not None and not use_edge_attr:
num_edge_attributes = self.num_edge_attributes
self.data.edge_attr = self.data.edge_attr[:, num_edge_attributes:]
if not (self.name == 'MUTAG' or self.name == 'PTC_MR' or self.name == 'DD' or self.name == 'PROTEINS' or self.name == 'NCI1' or self.name == 'NCI109'):
# if not (self.name == 'MUTAG' or self.name == 'DD' or self.name == 'PROTEINS' or self.name == 'NCI1' or self.name == 'NCI109'):
edge_index = self.data.edge_index[0, :].numpy()
_, num_edge = self.data.edge_index.size()
nlist = [edge_index[n] + 1 for n in range(num_edge - 1) if edge_index[n] > edge_index[n + 1]]
nlist.append(edge_index[-1] + 1)
num_node = np.array(nlist).sum()
self.data.x = torch.ones((num_node, 1))
edge_slice = [0]
k = 0
for n in nlist:
k = k + n
edge_slice.append(k)
self.slices['x'] = torch.tensor(edge_slice)
'''
print(self.data.x.size())
print(self.slices['x'])
print(self.slices['x'].size())
assert False
'''
self.aug = aug
@property
def raw_dir(self):
name = 'raw{}'.format('_cleaned' if self.cleaned else '')
return osp.join(self.root, self.name, name)
@property
def processed_dir(self):
name = 'processed{}'.format('_cleaned' if self.cleaned else '')
return osp.join(self.root, self.name, name)
@property
def num_node_labels(self):
if self.data.x is None:
return 0
for i in range(self.data.x.size(1)):
x = self.data.x[:, i:]
if ((x == 0) | (x == 1)).all() and (x.sum(dim=1) == 1).all():
return self.data.x.size(1) - i
return 0
@property
def num_node_attributes(self):
if self.data.x is None:
return 0
return self.data.x.size(1) - self.num_node_labels
@property
def num_edge_labels(self):
if self.data.edge_attr is None:
return 0
for i in range(self.data.edge_attr.size(1)):
if self.data.edge_attr[:, i:].sum() == self.data.edge_attr.size(0):
return self.data.edge_attr.size(1) - i
return 0
@property
def num_edge_attributes(self):
if self.data.edge_attr is None:
return 0
return self.data.edge_attr.size(1) - self.num_edge_labels
@property
def raw_file_names(self):
names = ['A', 'graph_indicator']
return ['{}_{}.txt'.format(self.name, name) for name in names]
@property
def processed_file_names(self):
return 'data.pt'
def download(self):
url = self.cleaned_url if self.cleaned else self.url
folder = osp.join(self.root, self.name)
path = download_url('{}/{}.zip'.format(url, self.name), folder)
extract_zip(path, folder)
os.unlink(path)
shutil.rmtree(self.raw_dir)
os.rename(osp.join(folder, self.name), self.raw_dir)
def process(self):
self.data, self.slices = read_tu_data(self.raw_dir, self.name)
if self.pre_filter is not None:
data_list = [self.get(idx) for idx in range(len(self))]
data_list = [data for data in data_list if self.pre_filter(data)]
self.data, self.slices = self.collate(data_list)
if self.pre_transform is not None:
data_list = [self.get(idx) for idx in range(len(self))]
data_list = [self.pre_transform(data) for data in data_list]
self.data, self.slices = self.collate(data_list)
torch.save((self.data, self.slices), self.processed_paths[0])
def __repr__(self):
return '{}({})'.format(self.name, len(self))
def get_num_feature(self):
data = self.data.__class__()
if hasattr(self.data, '__num_nodes__'):
data.num_nodes = self.data.__num_nodes__[0]
# print(data.x.size())
for key in self.slices.keys():
item, slices = self.data[key], self.slices[key]
if torch.is_tensor(item):
s = list(repeat(slice(None), item.dim()))
s[self.data.__cat_dim__(key,
item)] = slice(slices[0],
slices[0 + 1])
else:
s = slice(slices[idx], slices[idx + 1])
data[key] = item[s]
_, num_feature = data.x.size()
return num_feature
def get(self, idx):
data = self.data.__class__()
if hasattr(self.data, '__num_nodes__'):
data.num_nodes = self.data.__num_nodes__[idx]
for key in self.slices.keys():
item, slices = self.data[key], self.slices[key]
if torch.is_tensor(item):
s = list(repeat(slice(None), item.dim()))
s[self.data.__cat_dim__(key,
item)] = slice(slices[idx],
slices[idx + 1])
else:
s = slice(slices[idx], slices[idx + 1])
data[key] = item[s]
"""
edge_index = data.edge_index
node_num = data.x.size()[0]
edge_num = data.edge_index.size()[1]
data.edge_index = torch.tensor([[edge_index[0, n], edge_index[1, n]] for n in range(edge_num) if edge_index[0, n] < node_num and edge_index[1, n] < node_num] + [[n, n] for n in range(node_num)], dtype=torch.int64).t()
"""
node_num = data.edge_index.max()
sl = torch.tensor([[n,n] for n in range(node_num)]).t()
data.edge_index = torch.cat((data.edge_index, sl), dim=1)
if self.aug == 'dnodes':
data_aug = drop_nodes(deepcopy(data))
elif self.aug == 'pedges':
data_aug = permute_edges(deepcopy(data))
elif self.aug == 'subgraph':
data_aug = subgraph(deepcopy(data))
elif self.aug == 'mask_nodes':
data_aug = mask_nodes(deepcopy(data))
elif self.aug == 'none':
"""
if data.edge_index.max() > data.x.size()[0]:
print(data.edge_index)
print(data.x.size())
assert False
"""
data_aug = deepcopy(data)
data_aug.x = torch.ones((data.edge_index.max()+1, 1))
elif self.aug == 'random2':
n = np.random.randint(2)
if n == 0:
data_aug = drop_nodes(deepcopy(data))
elif n == 1:
data_aug = subgraph(deepcopy(data))
else:
print('sample error')
assert False
elif self.aug == 'random3':
n = np.random.randint(3)
if n == 0:
data_aug = drop_nodes(deepcopy(data))
elif n == 1:
data_aug = permute_edges(deepcopy(data))
elif n == 2:
data_aug = subgraph(deepcopy(data))
else:
print('sample error')
assert False
elif self.aug == 'random4':
n = np.random.randint(4)
if n == 0:
data_aug = drop_nodes(deepcopy(data))
elif n == 1:
data_aug = permute_edges(deepcopy(data))
elif n == 2:
data_aug = subgraph(deepcopy(data))
elif n == 3:
data_aug = mask_nodes(deepcopy(data))
else:
print('sample error')
assert False
else:
print('augmentation error')
assert False
# print(data, data_aug)
# assert False
return data, data_aug
def drop_nodes(data):
node_num, _ = data.x.size()
_, edge_num = data.edge_index.size()
drop_num = int(node_num / 10)
idx_drop = np.random.choice(node_num, drop_num, replace=False)
idx_nondrop = [n for n in range(node_num) if not n in idx_drop]
idx_dict = {idx_nondrop[n]:n for n in list(range(node_num - drop_num))}
# data.x = data.x[idx_nondrop]
edge_index = data.edge_index.numpy()
adj = torch.zeros((node_num, node_num))
adj[edge_index[0], edge_index[1]] = 1
adj[idx_drop, :] = 0
adj[:, idx_drop] = 0
edge_index = adj.nonzero().t()
data.edge_index = edge_index
# edge_index = [[idx_dict[edge_index[0, n]], idx_dict[edge_index[1, n]]] for n in range(edge_num) if (not edge_index[0, n] in idx_drop) and (not edge_index[1, n] in idx_drop)]
# edge_index = [[edge_index[0, n], edge_index[1, n]] for n in range(edge_num) if (not edge_index[0, n] in idx_drop) and (not edge_index[1, n] in idx_drop)] + [[n, n] for n in idx_nondrop]
# data.edge_index = torch.tensor(edge_index).transpose_(0, 1)
return data
def permute_edges(data):
node_num, _ = data.x.size()
_, edge_num = data.edge_index.size()
permute_num = int(edge_num / 10)
edge_index = data.edge_index.transpose(0, 1).numpy()
idx_add = np.random.choice(node_num, (permute_num, 2))
# idx_add = [[idx_add[0, n], idx_add[1, n]] for n in range(permute_num) if not (idx_add[0, n], idx_add[1, n]) in edge_index]
# edge_index = np.concatenate((np.array([edge_index[n] for n in range(edge_num) if not n in np.random.choice(edge_num, permute_num, replace=False)]), idx_add), axis=0)
# edge_index = np.concatenate((edge_index[np.random.choice(edge_num, edge_num-permute_num, replace=False)], idx_add), axis=0)
edge_index = edge_index[np.random.choice(edge_num, edge_num-permute_num, replace=False)]
# edge_index = [edge_index[n] for n in range(edge_num) if not n in np.random.choice(edge_num, permute_num, replace=False)] + idx_add
data.edge_index = torch.tensor(edge_index).transpose_(0, 1)
return data
def subgraph(data):
node_num, _ = data.x.size()
_, edge_num = data.edge_index.size()
sub_num = int(node_num * 0.2)
edge_index = data.edge_index.numpy()
idx_sub = [np.random.randint(node_num, size=1)[0]]
idx_neigh = set([n for n in edge_index[1][edge_index[0]==idx_sub[0]]])
count = 0
while len(idx_sub) <= sub_num:
count = count + 1
if count > node_num:
break
if len(idx_neigh) == 0:
break
sample_node = np.random.choice(list(idx_neigh))
if sample_node in idx_sub:
continue
idx_sub.append(sample_node)
idx_neigh.union(set([n for n in edge_index[1][edge_index[0]==idx_sub[-1]]]))
idx_drop = [n for n in range(node_num) if not n in idx_sub]
idx_nondrop = idx_sub
idx_dict = {idx_nondrop[n]:n for n in list(range(len(idx_nondrop)))}
# data.x = data.x[idx_nondrop]
edge_index = data.edge_index.numpy()
adj = torch.zeros((node_num, node_num))
adj[edge_index[0], edge_index[1]] = 1
adj[idx_drop, :] = 0
adj[:, idx_drop] = 0
edge_index = adj.nonzero().t()
data.edge_index = edge_index
# edge_index = [[idx_dict[edge_index[0, n]], idx_dict[edge_index[1, n]]] for n in range(edge_num) if (not edge_index[0, n] in idx_drop) and (not edge_index[1, n] in idx_drop)]
# edge_index = [[edge_index[0, n], edge_index[1, n]] for n in range(edge_num) if (not edge_index[0, n] in idx_drop) and (not edge_index[1, n] in idx_drop)] + [[n, n] for n in idx_nondrop]
# data.edge_index = torch.tensor(edge_index).transpose_(0, 1)
return data
def mask_nodes(data):
node_num, feat_dim = data.x.size()
mask_num = int(node_num / 10)
idx_mask = np.random.choice(node_num, mask_num, replace=False)
data.x[idx_mask] = torch.tensor(np.random.normal(loc=0.5, scale=0.5, size=(mask_num, feat_dim)), dtype=torch.float32)
return data