SauravMaheshkar
commited on
Commit
•
46a85c5
1
Parent(s):
836b957
feat: add link generation script
Browse files- link_gen.py +108 -0
link_gen.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import dgl
|
2 |
+
from dgl.data import CiteseerGraphDataset
|
3 |
+
import torch
|
4 |
+
import pickle
|
5 |
+
from copy import deepcopy
|
6 |
+
import scipy.sparse as sp
|
7 |
+
import numpy as np
|
8 |
+
import os
|
9 |
+
|
10 |
+
|
11 |
+
def mask_test_edges(adj_orig, val_frac, test_frac):
|
12 |
+
|
13 |
+
# Remove diagonal elements
|
14 |
+
adj = deepcopy(adj_orig)
|
15 |
+
# set diag as all zero
|
16 |
+
adj.setdiag(0)
|
17 |
+
adj.eliminate_zeros()
|
18 |
+
# Check that diag is zero:
|
19 |
+
# assert np.diag(adj.todense()).sum() == 0
|
20 |
+
|
21 |
+
adj_triu = sp.triu(adj, 1)
|
22 |
+
edges = sparse_to_tuple(adj_triu)[0]
|
23 |
+
num_test = int(np.floor(edges.shape[0] * test_frac))
|
24 |
+
num_val = int(np.floor(edges.shape[0] * val_frac))
|
25 |
+
|
26 |
+
all_edge_idx = list(range(edges.shape[0]))
|
27 |
+
np.random.shuffle(all_edge_idx)
|
28 |
+
val_edge_idx = all_edge_idx[:num_val]
|
29 |
+
test_edge_idx = all_edge_idx[num_val : (num_val + num_test)]
|
30 |
+
test_edges = edges[test_edge_idx]
|
31 |
+
val_edges = edges[val_edge_idx]
|
32 |
+
train_edges = edges[all_edge_idx[num_val + num_test :]]
|
33 |
+
|
34 |
+
noedge_mask = np.ones(adj.shape) - adj
|
35 |
+
noedges = np.asarray(sp.triu(noedge_mask, 1).nonzero()).T
|
36 |
+
all_edge_idx = list(range(noedges.shape[0]))
|
37 |
+
np.random.shuffle(all_edge_idx)
|
38 |
+
val_edge_idx = all_edge_idx[:num_val]
|
39 |
+
test_edge_idx = all_edge_idx[num_val : (num_val + num_test)]
|
40 |
+
test_edges_false = noedges[test_edge_idx]
|
41 |
+
val_edges_false = noedges[val_edge_idx]
|
42 |
+
|
43 |
+
data = np.ones(train_edges.shape[0])
|
44 |
+
adj_train = sp.csr_matrix(
|
45 |
+
(data, (train_edges[:, 0], train_edges[:, 1])), shape=adj.shape
|
46 |
+
)
|
47 |
+
adj_train = adj_train + adj_train.T
|
48 |
+
|
49 |
+
train_mask = np.ones(adj_train.shape)
|
50 |
+
for edges_tmp in [val_edges, val_edges_false, test_edges, test_edges_false]:
|
51 |
+
for e in edges_tmp:
|
52 |
+
assert e[0] < e[1]
|
53 |
+
train_mask[edges_tmp.T[0], edges_tmp.T[1]] = 0
|
54 |
+
train_mask[edges_tmp.T[1], edges_tmp.T[0]] = 0
|
55 |
+
|
56 |
+
train_edges = np.asarray(sp.triu(adj_train, 1).nonzero()).T
|
57 |
+
train_edges_false = np.asarray(
|
58 |
+
(sp.triu(train_mask, 1) - sp.triu(adj_train, 1)).nonzero()
|
59 |
+
).T
|
60 |
+
|
61 |
+
# NOTE: all these edge lists only contain single direction of edge!
|
62 |
+
return (
|
63 |
+
train_edges,
|
64 |
+
train_edges_false,
|
65 |
+
val_edges,
|
66 |
+
val_edges_false,
|
67 |
+
test_edges,
|
68 |
+
test_edges_false,
|
69 |
+
)
|
70 |
+
|
71 |
+
|
72 |
+
def sparse_to_tuple(sparse_mx):
|
73 |
+
if not sp.isspmatrix_coo(sparse_mx):
|
74 |
+
sparse_mx = sparse_mx.tocoo()
|
75 |
+
coords = np.vstack((sparse_mx.row, sparse_mx.col)).transpose()
|
76 |
+
values = sparse_mx.data
|
77 |
+
shape = sparse_mx.shape
|
78 |
+
return coords, values, shape
|
79 |
+
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
os.mkdir("links")
|
83 |
+
os.mkdir("pretrain_labels")
|
84 |
+
g = CiteseerGraphDataset()[0]
|
85 |
+
total_pos_edges = torch.randperm(g.num_edges())
|
86 |
+
adj_train = g.adjacency_matrix(scipy_fmt="csr")
|
87 |
+
(
|
88 |
+
train_edges,
|
89 |
+
train_edges_false,
|
90 |
+
val_edges,
|
91 |
+
val_edges_false,
|
92 |
+
test_edges,
|
93 |
+
test_edges_false,
|
94 |
+
) = mask_test_edges(adj_train, 0.1, 0.2)
|
95 |
+
tvt_edges_file = "links/citeseer_tvtEdges.pkl"
|
96 |
+
pickle.dump(
|
97 |
+
(
|
98 |
+
train_edges,
|
99 |
+
train_edges_false,
|
100 |
+
val_edges,
|
101 |
+
val_edges_false,
|
102 |
+
test_edges,
|
103 |
+
test_edges_false,
|
104 |
+
),
|
105 |
+
open(tvt_edges_file, "wb"),
|
106 |
+
)
|
107 |
+
node_assignment = dgl.metis_partition_assignment(g, 10)
|
108 |
+
torch.save(node_assignment, "pretrain_labels/metis_label_citeseer.pt")
|