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