-
Notifications
You must be signed in to change notification settings - Fork 10
/
prepare_dataset.py
407 lines (311 loc) · 16.3 KB
/
prepare_dataset.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 math
import pickle
import torch
import pandas as pd
import networkx as nx
from tqdm import tqdm
from torch_geometric.seed import seed_everything
import torch_geometric.transforms as T
from torch_geometric.data import Data
from torch_geometric.datasets import CitationFull, Coauthor, Flickr, RelLinkPredDataset, WordNet18, WordNet18RR
from torch_geometric.utils import train_test_split_edges, k_hop_subgraph, negative_sampling, to_undirected, is_undirected, to_networkx
from ogb.linkproppred import PygLinkPropPredDataset
from framework.utils import *
data_dir = './data'
df_size = [i / 100 for i in range(10)] + [i / 10 for i in range(10)] + [i for i in range(10)] # Df_size in percentage
seeds = [42, 21, 13, 87, 100]
graph_datasets = ['Cora', 'PubMed', 'DBLP', 'CS', 'ogbl-citation2', 'ogbl-collab'][4:]
kg_datasets = ['FB15k-237', 'WordNet18', 'WordNet18RR', 'ogbl-biokg'][-1:]
os.makedirs(data_dir, exist_ok=True)
num_edge_type_mapping = {
'FB15k-237': 237,
'WordNet18': 18,
'WordNet18RR': 11
}
def train_test_split_edges_no_neg_adj_mask(data, val_ratio: float = 0.05, test_ratio: float = 0.1, two_hop_degree=None, kg=False):
'''Avoid adding neg_adj_mask'''
num_nodes = data.num_nodes
row, col = data.edge_index
edge_attr = data.edge_attr
if kg:
edge_type = data.edge_type
data.edge_index = data.edge_attr = data.edge_weight = data.edge_year = data.edge_type = None
if not kg:
# Return upper triangular portion.
mask = row < col
row, col = row[mask], col[mask]
if edge_attr is not None:
edge_attr = edge_attr[mask]
n_v = int(math.floor(val_ratio * row.size(0)))
n_t = int(math.floor(test_ratio * row.size(0)))
if two_hop_degree is not None: # Use low degree edges for test sets
low_degree_mask = two_hop_degree < 50
low = low_degree_mask.nonzero().squeeze()
high = (~low_degree_mask).nonzero().squeeze()
low = low[torch.randperm(low.size(0))]
high = high[torch.randperm(high.size(0))]
perm = torch.cat([low, high])
else:
perm = torch.randperm(row.size(0))
row = row[perm]
col = col[perm]
# Train
r, c = row[n_v + n_t:], col[n_v + n_t:]
if kg:
# data.edge_index and data.edge_type has reverse edges and edge types for message passing
pos_edge_index = torch.stack([r, c], dim=0)
# rev_pos_edge_index = torch.stack([r, c], dim=0)
train_edge_type = edge_type[n_v + n_t:]
# train_rev_edge_type = edge_type[n_v + n_t:] + edge_type.unique().shape[0]
# data.edge_index = torch.cat((torch.stack([r, c], dim=0), torch.stack([r, c], dim=0)), dim=1)
# data.edge_type = torch.cat([train_edge_type, train_rev_edge_type], dim=0)
data.edge_index = pos_edge_index
data.edge_type = train_edge_type
# data.train_pos_edge_index and data.train_edge_type only has one direction edges and edge types for decoding
data.train_pos_edge_index = torch.stack([r, c], dim=0)
data.train_edge_type = train_edge_type
else:
data.train_pos_edge_index = torch.stack([r, c], dim=0)
if edge_attr is not None:
# out = to_undirected(data.train_pos_edge_index, edge_attr[n_v + n_t:])
data.train_pos_edge_index, data.train_pos_edge_attr = out
else:
data.train_pos_edge_index = data.train_pos_edge_index
# data.train_pos_edge_index = to_undirected(data.train_pos_edge_index)
assert not is_undirected(data.train_pos_edge_index)
# Test
r, c = row[:n_t], col[:n_t]
data.test_pos_edge_index = torch.stack([r, c], dim=0)
if kg:
data.test_edge_type = edge_type[:n_t]
neg_edge_index = negative_sampling_kg(
edge_index=data.test_pos_edge_index,
edge_type=data.test_edge_type)
else:
neg_edge_index = negative_sampling(
edge_index=data.test_pos_edge_index,
num_nodes=data.num_nodes,
num_neg_samples=data.test_pos_edge_index.shape[1])
data.test_neg_edge_index = neg_edge_index
# Valid
r, c = row[n_t:n_t+n_v], col[n_t:n_t+n_v]
data.val_pos_edge_index = torch.stack([r, c], dim=0)
if kg:
data.val_edge_type = edge_type[n_t:n_t+n_v]
neg_edge_index = negative_sampling_kg(
edge_index=data.val_pos_edge_index,
edge_type=data.val_edge_type)
else:
neg_edge_index = negative_sampling(
edge_index=data.val_pos_edge_index,
num_nodes=data.num_nodes,
num_neg_samples=data.val_pos_edge_index.shape[1])
data.val_neg_edge_index = neg_edge_index
return data
def process_graph():
for d in graph_datasets:
if d in ['Cora', 'PubMed', 'DBLP']:
dataset = CitationFull(os.path.join(data_dir, d), d, transform=T.NormalizeFeatures())
elif d in ['CS', 'Physics']:
dataset = Coauthor(os.path.join(data_dir, d), d, transform=T.NormalizeFeatures())
elif d in ['Flickr']:
dataset = Flickr(os.path.join(data_dir, d), transform=T.NormalizeFeatures())
elif 'ogbl' in d:
dataset = PygLinkPropPredDataset(root=os.path.join(data_dir, d), name=d)
else:
raise NotImplementedError
print('Processing:', d)
print(dataset)
data = dataset[0]
data.train_mask = data.val_mask = data.test_mask = None
graph = to_networkx(data)
# Get two hop degree for all nodes
node_to_neighbors = {}
for n in tqdm(graph.nodes(), desc='Two hop neighbors'):
neighbor_1 = set(graph.neighbors(n))
neighbor_2 = sum([list(graph.neighbors(i)) for i in neighbor_1], [])
neighbor_2 = set(neighbor_2)
neighbor = neighbor_1 | neighbor_2
node_to_neighbors[n] = neighbor
two_hop_degree = []
row, col = data.edge_index
mask = row < col
row, col = row[mask], col[mask]
for r, c in tqdm(zip(row, col), total=len(row)):
neighbor_row = node_to_neighbors[r.item()]
neighbor_col = node_to_neighbors[c.item()]
neighbor = neighbor_row | neighbor_col
num = len(neighbor)
two_hop_degree.append(num)
two_hop_degree = torch.tensor(two_hop_degree)
for s in seeds:
seed_everything(s)
# D
data = dataset[0]
if 'ogbl' in d:
data = train_test_split_edges_no_neg_adj_mask(data, test_ratio=0.05, two_hop_degree=two_hop_degree)
else:
data = train_test_split_edges_no_neg_adj_mask(data, test_ratio=0.05)
print(s, data)
with open(os.path.join(data_dir, d, f'd_{s}.pkl'), 'wb') as f:
pickle.dump((dataset, data), f)
# Two ways to sample Df from the training set
## 1. Df is within 2 hop local enclosing subgraph of Dtest
## 2. Df is outside of 2 hop local enclosing subgraph of Dtest
# All the candidate edges (train edges)
# graph = to_networkx(Data(edge_index=data.train_pos_edge_index, x=data.x))
# Get the 2 hop local enclosing subgraph for all test edges
_, local_edges, _, mask = k_hop_subgraph(
data.test_pos_edge_index.flatten().unique(),
2,
data.train_pos_edge_index,
num_nodes=dataset[0].num_nodes)
distant_edges = data.train_pos_edge_index[:, ~mask]
print('Number of edges. Local: ', local_edges.shape[1], 'Distant:', distant_edges.shape[1])
in_mask = mask
out_mask = ~mask
# df_in_mask = torch.zeros_like(mask)
# df_out_mask = torch.zeros_like(mask)
# df_in_all_idx = in_mask.nonzero().squeeze()
# df_out_all_idx = out_mask.nonzero().squeeze()
# df_in_selected_idx = df_in_all_idx[torch.randperm(df_in_all_idx.shape[0])[:df_size]]
# df_out_selected_idx = df_out_all_idx[torch.randperm(df_out_all_idx.shape[0])[:df_size]]
# df_in_mask[df_in_selected_idx] = True
# df_out_mask[df_out_selected_idx] = True
# assert (in_mask & out_mask).sum() == 0
# assert (df_in_mask & df_out_mask).sum() == 0
# local_edges = set()
# for i in range(data.test_pos_edge_index.shape[1]):
# edge = data.test_pos_edge_index[:, i].tolist()
# subgraph = get_enclosing_subgraph(graph, edge)
# local_edges = local_edges | set(subgraph[2])
# distant_edges = graph.edges() - local_edges
# print('aaaaaaa', len(local_edges), len(distant_edges))
# local_edges = torch.tensor(sorted(list([i for i in local_edges if i[0] < i[1]])))
# distant_edges = torch.tensor(sorted(list([i for i in distant_edges if i[0] < i[1]])))
# df_in = torch.randperm(local_edges.shape[1])[:df_size]
# df_out = torch.randperm(distant_edges.shape[1])[:df_size]
# df_in = local_edges[:, df_in]
# df_out = distant_edges[:, df_out]
# df_in_mask = torch.zeros(data.train_pos_edge_index.shape[1], dtype=torch.bool)
# df_out_mask = torch.zeros(data.train_pos_edge_index.shape[1], dtype=torch.bool)
# for row in df_in:
# i = (data.train_pos_edge_index.T == row).all(axis=1).nonzero()
# df_in_mask[i] = True
# for row in df_out:
# i = (data.train_pos_edge_index.T == row).all(axis=1).nonzero()
# df_out_mask[i] = True
torch.save(
{'out': out_mask, 'in': in_mask},
os.path.join(data_dir, d, f'df_{s}.pt')
)
def process_kg():
for d in kg_datasets:
# Create the dataset to calculate node degrees
if d in ['FB15k-237']:
dataset = RelLinkPredDataset(os.path.join(data_dir, d), d, transform=T.NormalizeFeatures())
data = dataset[0]
data.x = torch.arange(data.num_nodes)
edge_index = torch.cat([data.train_edge_index, data.valid_edge_index, data.test_edge_index], dim=1)
edge_type = torch.cat([data.train_edge_type, data.valid_edge_type, data.test_edge_type])
data = Data(edge_index=edge_index, edge_type=edge_type)
elif d in ['WordNet18RR']:
dataset = WordNet18RR(os.path.join(data_dir, d), transform=T.NormalizeFeatures())
data = dataset[0]
data.x = torch.arange(data.num_nodes)
data.train_mask = data.val_mask = data.test_mask = None
elif d in ['WordNet18']:
dataset = WordNet18(os.path.join(data_dir, d), transform=T.NormalizeFeatures())
data = dataset[0]
data.x = torch.arange(data.num_nodes)
# Use original split
data.train_pos_edge_index = data.edge_index[:, data.train_mask]
data.train_edge_type = data.edge_type[data.train_mask]
data.val_pos_edge_index = data.edge_index[:, data.val_mask]
data.val_edge_type = data.edge_type[data.val_mask]
data.val_neg_edge_index = negative_sampling_kg(data.val_pos_edge_index, data.val_edge_type)
data.test_pos_edge_index = data.edge_index[:, data.test_mask]
data.test_edge_type = data.edge_type[data.test_mask]
data.test_neg_edge_index = negative_sampling_kg(data.test_pos_edge_index, data.test_edge_type)
elif 'ogbl' in d:
dataset = PygLinkPropPredDataset(root=os.path.join(data_dir, d), name=d)
split_edge = dataset.get_edge_split()
train_edge, valid_edge, test_edge = split_edge["train"], split_edge["valid"], split_edge["test"]
entity_dict = dict()
cur_idx = 0
for key in dataset[0]['num_nodes_dict']:
entity_dict[key] = (cur_idx, cur_idx + dataset[0]['num_nodes_dict'][key])
cur_idx += dataset[0]['num_nodes_dict'][key]
nentity = sum(dataset[0]['num_nodes_dict'].values())
valid_head_neg = valid_edge.pop('head_neg')
valid_tail_neg = valid_edge.pop('tail_neg')
test_head_neg = test_edge.pop('head_neg')
test_tail_neg = test_edge.pop('tail_neg')
train = pd.DataFrame(train_edge)
valid = pd.DataFrame(valid_edge)
test = pd.DataFrame(test_edge)
# Convert to global index
train['head'] = [idx + entity_dict[tp][0] for idx, tp in zip(train['head'], train['head_type'])]
train['tail'] = [idx + entity_dict[tp][0] for idx, tp in zip(train['tail'], train['tail_type'])]
valid['head'] = [idx + entity_dict[tp][0] for idx, tp in zip(valid['head'], valid['head_type'])]
valid['tail'] = [idx + entity_dict[tp][0] for idx, tp in zip(valid['tail'], valid['tail_type'])]
test['head'] = [idx + entity_dict[tp][0] for idx, tp in zip(test['head'], test['head_type'])]
test['tail'] = [idx + entity_dict[tp][0] for idx, tp in zip(test['tail'], test['tail_type'])]
valid_pos_edge_index = torch.tensor([valid['head'], valid['tail']])
valid_edge_type = torch.tensor(valid.relation)
valid_neg_edge_index = torch.stack([valid_pos_edge_index[0], valid_tail_neg[:, 0]])
test_pos_edge_index = torch.tensor([test['head'], test['tail']])
test_edge_type = torch.tensor(test.relation)
test_neg_edge_index = torch.stack([test_pos_edge_index[0], test_tail_neg[:, 0]])
train_directed = train[train.head_type != train.tail_type]
train_undirected = train[train.head_type == train.tail_type]
train_undirected_uni = train_undirected[train_undirected['head'] < train_undirected['tail']]
train_uni = pd.concat([train_directed, train_undirected_uni], ignore_index=True)
train_pos_edge_index = torch.tensor([train_uni['head'], train_uni['tail']])
train_edge_type = torch.tensor(train_uni.relation)
r, c = train_pos_edge_index
rev_edge_index = torch.stack([c, r])
rev_edge_type = train_edge_type + 51
edge_index = torch.cat([train_pos_edge_index, rev_edge_index], dim=1)
edge_type = torch.cat([train_edge_type, rev_edge_type], dim=0)
data = Data(
x=torch.arange(nentity), edge_index=edge_index, edge_type=edge_type,
train_pos_edge_index=train_pos_edge_index, train_edge_type=train_edge_type,
val_pos_edge_index=valid_pos_edge_index, val_edge_type=valid_edge_type, val_neg_edge_index=valid_neg_edge_index,
test_pos_edge_index=test_pos_edge_index, test_edge_type=test_edge_type, test_neg_edge_index=test_neg_edge_index)
else:
raise NotImplementedError
print('Processing:', d)
print(dataset)
for s in seeds:
seed_everything(s)
# D
# data = train_test_split_edges_no_neg_adj_mask(data, test_ratio=0.05, two_hop_degree=two_hop_degree, kg=True)
print(s, data)
with open(os.path.join(data_dir, d, f'd_{s}.pkl'), 'wb') as f:
pickle.dump((dataset, data), f)
# Two ways to sample Df from the training set
## 1. Df is within 2 hop local enclosing subgraph of Dtest
## 2. Df is outside of 2 hop local enclosing subgraph of Dtest
# All the candidate edges (train edges)
# graph = to_networkx(Data(edge_index=data.train_pos_edge_index, x=data.x))
# Get the 2 hop local enclosing subgraph for all test edges
_, local_edges, _, mask = k_hop_subgraph(
data.test_pos_edge_index.flatten().unique(),
2,
data.train_pos_edge_index,
num_nodes=dataset[0].num_nodes)
distant_edges = data.train_pos_edge_index[:, ~mask]
print('Number of edges. Local: ', local_edges.shape[1], 'Distant:', distant_edges.shape[1])
in_mask = mask
out_mask = ~mask
torch.save(
{'out': out_mask, 'in': in_mask},
os.path.join(data_dir, d, f'df_{s}.pt')
)
def main():
process_graph()
# process_kg()
if __name__ == "__main__":
main()