-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_test_links_split.py
50 lines (35 loc) · 1.56 KB
/
train_test_links_split.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
import argparse
import numpy as np
import networkx as nx
import os, gzip
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from stellargraph.data import EdgeSplitter
#########################################################
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--input', default='data/cora.edgelist.gz',
help='Input graph edgelist file')
parser.add_argument('--graph-output', default='predict/cora.edgelist.gz',
help='Output graph edgelist file')
parser.add_argument('--test-output', default='predict/cora.test.npz',
help='Test edges output file')
parser.add_argument('--test-frac', default=0.1, type=float,
help='Fraction of test edges')
parser.add_argument('--seed', default=42, type=int,
help='Seed for edge sampling')
#########################################################
def main():
params = vars(parser.parse_args())
graph = nx.read_weighted_edgelist(params['input'])
graph.remove_edges_from(nx.selfloop_edges(graph))
node_name = np.array([str(n) for n in graph.nodes()])
print('Sampling edges')
edge_splitter_test = EdgeSplitter(graph)
graph_train, edges_test, labels_test = edge_splitter_test.train_test_split(
p=params['test_frac'], method="global", keep_connected=True, seed=params['seed'])
print('Writing files')
nx.set_edge_attributes(graph_train, 1., 'weight')
nx.write_weighted_edgelist(graph_train, params['graph_output'])
np.savez(params['test_output'], np.concatenate((edges_test, labels_test[:, np.newaxis]), axis=1))
if __name__ == '__main__':
main()