-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_utils.py
441 lines (348 loc) · 15.1 KB
/
data_utils.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import os
import random
import torch
import torch.nn.functional as F
import numpy as np
from collections import defaultdict
from scipy import sparse as sp
from sklearn.metrics import roc_auc_score, f1_score, average_precision_score
from torch_sparse import SparseTensor
from sklearn import metrics
from baselines import Mahalanobis
from torch_geometric.utils import to_dense_adj, dense_to_sparse
import gdown
def set_random_seed(seed: int) -> None:
"""set seeds for controlled randomness"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
def rand_splits(node_idx, train_prop=.5, valid_prop=.25):
""" randomly splits label into train/valid/test splits """
splits = {}
n = node_idx.size(0)
train_num = int(n * train_prop)
valid_num = int(n * valid_prop)
perm = torch.as_tensor(np.random.permutation(n))
train_indices = perm[:train_num]
val_indices = perm[train_num:train_num + valid_num]
test_indices = perm[train_num + valid_num:]
splits['train'] = node_idx[train_indices]
splits['valid'] = node_idx[val_indices]
splits['test'] = node_idx[test_indices]
return splits
def rand_train_test_idx(label, train_prop=.5, valid_prop=.25, ignore_negative=True):
""" randomly splits label into train/valid/test splits """
if ignore_negative:
labeled_nodes = torch.where(label != -1)[0]
else:
labeled_nodes = label
n = labeled_nodes.shape[0]
train_num = int(n * train_prop)
valid_num = int(n * valid_prop)
perm = torch.as_tensor(np.random.permutation(n))
train_indices = perm[:train_num]
val_indices = perm[train_num:train_num + valid_num]
test_indices = perm[train_num + valid_num:]
if not ignore_negative:
return train_indices, val_indices, test_indices
train_idx = labeled_nodes[train_indices]
valid_idx = labeled_nodes[val_indices]
test_idx = labeled_nodes[test_indices]
return train_idx, valid_idx, test_idx
def even_quantile_labels(vals, nclasses, verbose=True):
""" partitions vals into nclasses by a quantile based split,
where the first class is less than the 1/nclasses quantile,
second class is less than the 2/nclasses quantile, and so on
vals is np array
returns an np array of int class labels
"""
label = -1 * np.ones(vals.shape[0], dtype=int)
interval_lst = []
lower = -np.inf
for k in range(nclasses - 1):
upper = np.nanquantile(vals, (k + 1) / nclasses)
interval_lst.append((lower, upper))
inds = (vals >= lower) * (vals < upper)
label[inds] = k
lower = upper
label[vals >= lower] = nclasses - 1
interval_lst.append((lower, np.inf))
if verbose:
print('Class Label Intervals:')
for class_idx, interval in enumerate(interval_lst):
print(f'Class {class_idx}: [{interval[0]}, {interval[1]})]')
return label
def to_planetoid(dataset):
"""
Takes in a NCDataset and returns the dataset in H2GCN Planetoid form, as follows:
x => the feature vectors of the training instances as scipy.sparse.csr.csr_matrix object;
tx => the feature vectors of the test instances as scipy.sparse.csr.csr_matrix object;
allx => the feature vectors of both labeled and unlabeled training instances
(a superset of ind.dataset_str.x) as scipy.sparse.csr.csr_matrix object;
y => the one-hot labels of the labeled training instances as numpy.ndarray object;
ty => the one-hot labels of the test instances as numpy.ndarray object;
ally => the labels for instances in ind.dataset_str.allx as numpy.ndarray object;
graph => a dict in the format {index: [index_of_neighbor_nodes]} as collections.defaultdict
object;
split_idx => The ogb dictionary that contains the train, valid, test splits
"""
split_idx = dataset.get_idx_split('random', 0.25)
train_idx, valid_idx, test_idx = split_idx["train"], split_idx["valid"], split_idx["test"]
graph, label = dataset[0]
label = torch.squeeze(label)
print("generate x")
x = graph['node_feat'][train_idx].numpy()
x = sp.csr_matrix(x)
tx = graph['node_feat'][test_idx].numpy()
tx = sp.csr_matrix(tx)
allx = graph['node_feat'].numpy()
allx = sp.csr_matrix(allx)
y = F.one_hot(label[train_idx]).numpy()
ty = F.one_hot(label[test_idx]).numpy()
ally = F.one_hot(label).numpy()
edge_index = graph['edge_index'].T
graph = defaultdict(list)
for i in range(0, label.shape[0]):
graph[i].append(i)
for start_edge, end_edge in edge_index:
graph[start_edge.item()].append(end_edge.item())
return x, tx, allx, y, ty, ally, graph, split_idx
def to_sparse_tensor(edge_index, edge_feat, num_nodes):
""" converts the edge_index into SparseTensor
"""
num_edges = edge_index.size(1)
(row, col), N, E = edge_index, num_nodes, num_edges
perm = (col * N + row).argsort()
row, col = row[perm], col[perm]
value = edge_feat[perm]
adj_t = SparseTensor(row=col, col=row, value=value,
sparse_sizes=(N, N), is_sorted=True)
# Pre-process some important attributes.
adj_t.storage.rowptr()
adj_t.storage.csr2csc()
return adj_t
def normalize(edge_index):
""" normalizes the edge_index
"""
adj_t = edge_index.set_diag()
deg = adj_t.sum(dim=1).to(torch.float)
deg_inv_sqrt = deg.pow(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
adj_t = deg_inv_sqrt.view(-1, 1) * adj_t * deg_inv_sqrt.view(1, -1)
return adj_t
def edge_index_to_adjacency_matrix(edge_index, N):
A = to_dense_adj(edge_index, max_num_nodes=N).squeeze()
return A
def adjecency_matrix_to_edge_index(A):
edge_index, value = dense_to_sparse(A)
return edge_index, value
def gen_normalized_adjs(dataset):
""" returns the normalized adjacency matrix
"""
row, col = dataset.edge_index
N = dataset.num_nodes
adj = SparseTensor(row=row, col=col, sparse_sizes=(N, N))
deg = adj.sum(dim=1).to(torch.float)
D_isqrt = deg.pow(-0.5)
D_isqrt[D_isqrt == float('inf')] = 0
DAD = D_isqrt.view(-1,1) * adj * D_isqrt.view(1,-1)
DA = D_isqrt.view(-1,1) * D_isqrt.view(-1,1) * adj
AD = adj * D_isqrt.view(1,-1) * D_isqrt.view(1,-1)
return DAD, DA, AD
def eval_acc(y_true, y_pred):
acc_list = []
y_true = y_true.detach().cpu().numpy()
y_pred = y_pred.argmax(dim=-1, keepdim=True).detach().cpu().numpy()
for i in range(y_true.shape[1]):
is_labeled = y_true[:, i] == y_true[:, i]
correct = y_true[is_labeled, i] == y_pred[is_labeled, i]
acc_list.append(float(np.sum(correct))/len(correct))
return sum(acc_list)/len(acc_list)
def eval_rocauc(y_true, y_pred):
""" adapted from ogb
https://github.com/snap-stanford/ogb/blob/master/ogb/nodeproppred/evaluate.py"""
rocauc_list = []
y_true = y_true.detach().cpu().numpy()
if y_true.shape[1] == 1:
# use the predicted class for single-class classification
y_pred = F.softmax(y_pred, dim=-1)[:,1].unsqueeze(1).cpu().numpy()
else:
y_pred = y_pred.detach().cpu().numpy()
for i in range(y_true.shape[1]):
# AUC is only defined when there is at least one positive data.
if np.sum(y_true[:, i] == 1) > 0 and np.sum(y_true[:, i] == 0) > 0:
is_labeled = y_true[:, i] == y_true[:, i]
score = roc_auc_score(y_true[is_labeled, i], y_pred[is_labeled, i])
rocauc_list.append(score)
if len(rocauc_list) == 0:
raise RuntimeError(
'No positively labeled data available. Cannot compute ROC-AUC.')
return sum(rocauc_list)/len(rocauc_list)
@torch.no_grad()
def evaluate(model, dataset, split_idx, eval_func, result=None, sampling=False, subgraph_loader=None):
if result is not None:
out = result
else:
model.eval()
if not sampling:
out = model(dataset)
else:
out = model.inference(dataset, subgraph_loader)
train_acc = eval_func(
dataset.y[split_idx['train']], out[split_idx['train']])
valid_acc = eval_func(
dataset.y[split_idx['valid']], out[split_idx['valid']])
test_acc = eval_func(
dataset.y[split_idx['test']], out[split_idx['test']])
return train_acc, valid_acc, test_acc, out
@torch.no_grad()
def evaluate_covariate(model, dataset, dataset2, split_idx, eval_func, result=None, sampling=False, subgraph_loader=None):
if result is not None:
out = result
else:
model.eval()
if not sampling:
out = model(dataset)
out2 = model(dataset2)
else:
out = model.inference(dataset, subgraph_loader)
train_acc = eval_func(
dataset.y[split_idx['train']], out[split_idx['train']])
valid_acc = eval_func(
dataset.y[split_idx['valid']], out[split_idx['valid']])
test_acc = eval_func(dataset2.y, out2)
return train_acc, valid_acc, test_acc, out
@torch.no_grad()
def evaluate_detect(model, dataset_ind, dataset_ood, criterion, eval_func, args=None, device=None, return_score=False):
model.eval()
score = model.detect(dataset_ind, device)
test_ind_score = score[dataset_ind.splits['test']]
test_ood_score = score[dataset_ood.node_idx]
auroc, aupr, fpr, _ = evaluate_ood(test_ind_score, test_ood_score)
result = [auroc, aupr, fpr]
out = model(dataset_ind).cpu()
test_idx = dataset_ind.splits['test']
test_score = eval_func(dataset_ind.y[test_idx], out[test_idx])
split_idx = dataset_ind.splits
valid_idx = split_idx['valid']
if args.ood == 'OODGAT':
valid_metric = eval_func(dataset_ind.y[split_idx['valid']], out[split_idx['valid']])
elif args.ood in ["GPN", 'SGCN']:
valid_metric = model.valid_loss(dataset_ind, device)
else:
if args.dataset in ('proteins', 'ppi'):
valid_metric = criterion(out[valid_idx], dataset_ind.y[valid_idx].to(torch.float))
else:
valid_out = F.log_softmax(out[valid_idx], dim=1)
valid_metric = criterion(valid_out, dataset_ind.y[valid_idx].squeeze(1))
result += [test_score] + [valid_metric]
if return_score:
return result, score
else:
return result
def evaluate_ood(iid_score, ood_score, tpr=0.95):
iid_score, ood_score = iid_score.cpu(), ood_score.cpu()
scores = torch.cat([iid_score, ood_score]).numpy()
y_true = np.zeros(len(scores))
y_true[:len(iid_score)] = 1
fpr_list, tpr_list, threshold_list = metrics.roc_curve(y_true, scores)
fpr = fpr_list[np.argmax(tpr_list >= tpr)]
thresh = threshold_list[np.argmax(tpr_list >= tpr)]
auroc = metrics.auc(fpr_list, tpr_list)
precision_in, recall_in, thresholds_in \
= metrics.precision_recall_curve(y_true, scores)
aupr_in = metrics.auc(recall_in, precision_in)
return auroc, aupr_in, fpr, thresh
def stable_cumsum(arr, rtol=1e-05, atol=1e-08):
"""Use high precision for cumsum and check that final value matches sum
Parameters
----------
arr : array-like
To be cumulatively summed as flat
rtol : float
Relative tolerance, see ``np.allclose``
atol : float
Absolute tolerance, see ``np.allclose``
"""
out = np.cumsum(arr, dtype=np.float64)
expected = np.sum(arr, dtype=np.float64)
if not np.allclose(out[-1], expected, rtol=rtol, atol=atol):
raise RuntimeError('cumsum was found to be unstable: '
'its last element does not correspond to sum')
return out
def fpr_and_fdr_at_recall(y_true, y_score, recall_level= 0.95, pos_label=None):
classes = np.unique(y_true)
if (pos_label is None and
not (np.array_equal(classes, [0, 1]) or
np.array_equal(classes, [-1, 1]) or
np.array_equal(classes, [0]) or
np.array_equal(classes, [-1]) or
np.array_equal(classes, [1]))):
raise ValueError("Data is not binary and pos_label is not specified")
elif pos_label is None:
pos_label = 1.
# make y_true a boolean vector
y_true = (y_true == pos_label)
# sort scores and corresponding truth values
desc_score_indices = np.argsort(y_score, kind="mergesort")[::-1]
y_score = y_score[desc_score_indices]
y_true = y_true[desc_score_indices]
# y_score typically has many tied values. Here we extract
# the indices associated with the distinct values. We also
# concatenate a value for the end of the curve.
distinct_value_indices = np.where(np.diff(y_score))[0]
threshold_idxs = np.r_[distinct_value_indices, y_true.size - 1]
# accumulate the true positives with decreasing threshold
tps = stable_cumsum(y_true)[threshold_idxs]
fps = 1 + threshold_idxs - tps # add one because of zero-based indexing
thresholds = y_score[threshold_idxs]
recall = tps / tps[-1]
last_ind = tps.searchsorted(tps[-1])
sl = slice(last_ind, None, -1) # [last_ind::-1]
recall, fps, tps, thresholds = np.r_[recall[sl], 1], np.r_[fps[sl], 0], np.r_[tps[sl], 0], thresholds[sl]
cutoff = np.argmin(np.abs(recall - recall_level))
if np.array_equal(classes, [1]):
return thresholds[cutoff] # return threshold
return fps[cutoff] / (np.sum(np.logical_not(y_true))), thresholds[cutoff]
def get_measures(_pos, _neg, recall_level=0.95):
pos = np.array(_pos[:]).reshape((-1, 1))
neg = np.array(_neg[:]).reshape((-1, 1))
examples = np.squeeze(np.vstack((pos, neg)))
labels = np.zeros(len(examples), dtype=np.int32)
labels[:len(pos)] += 1
auroc = roc_auc_score(labels, examples)
aupr = average_precision_score(labels, examples)
fpr, threshould = fpr_and_fdr_at_recall(labels, examples, recall_level)
return auroc, aupr, fpr, threshould
def load_fixed_splits(dataset, sub_dataset):
""" loads saved fixed splits for dataset
"""
name = dataset
if sub_dataset and sub_dataset != 'None':
name += f'-{sub_dataset}'
if not os.path.exists(f'./data/splits/{name}-splits.npy'):
assert dataset in splits_drive_url.keys()
gdown.download(
id=splits_drive_url[dataset], \
output=f'./data/splits/{name}-splits.npy', quiet=False)
splits_lst = np.load(f'./data/splits/{name}-splits.npy', allow_pickle=True)
for i in range(len(splits_lst)):
for key in splits_lst[i]:
if not torch.is_tensor(splits_lst[i][key]):
splits_lst[i][key] = torch.as_tensor(splits_lst[i][key])
return splits_lst
dataset_drive_url = {
'twitch-gamer_feat' : '1fA9VIIEI8N0L27MSQfcBzJgRQLvSbrvR',
'twitch-gamer_edges' : '1XLETC6dG3lVl7kDmytEJ52hvDMVdxnZ0',
'snap-patents' : '1ldh23TSY1PwXia6dU0MYcpyEgX-w3Hia',
'pokec' : '1dNs5E7BrWJbgcHeQ_zuy5Ozp2tRCWG0y',
'yelp-chi': '1fAXtTVQS4CfEk4asqrFw9EPmlUPGbGtJ',
'wiki_views': '1p5DlVHrnFgYm3VsNIzahSsvCD424AyvP', # Wiki 1.9M
'wiki_edges': '14X7FlkjrlUgmnsYtPwdh-gGuFla4yb5u', # Wiki 1.9M
'wiki_features': '1ySNspxbK-snNoAZM7oxiWGvOnTRdSyEK' # Wiki 1.9M
}
splits_drive_url = {
'snap-patents' : '12xbBRqd8mtG_XkNLH8dRRNZJvVM4Pw-N',
'pokec' : '1ZhpAiyTNc0cE_hhgyiqxnkKREHK7MK-_',
}