forked from iguana-parser/iguana
-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph_loader.py
74 lines (58 loc) · 2.11 KB
/
graph_loader.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
import os
import shutil
import argparse
from pathlib import Path
import cfpq_data
import numpy as np
def generate_nodes(path_to_dir, file_name, node_number):
path_to_file = path_to_dir + "/" + file_name + "_all_nodes.csv"
nodes = np.random.permutation(node_number)
with open(path_to_file, 'w') as f:
for node in nodes:
f.write(f"{node}\n")
def graph_to_csv(graph_name, path_to_dir, relationships):
graph_path = cfpq_data.download(graph_name)
if os.path.exists(path_to_dir):
print(f"REMOVE {path_to_dir}")
shutil.rmtree(path_to_dir)
path_to_dir = Path(path_to_dir).resolve()
path_to_dir.mkdir(parents=True, exist_ok=True)
csv_files = dict()
for relationship in relationships + ["other"]:
csv_file = path_to_dir / f"{graph_name}_{relationship}.csv"
csv_files[relationship] = csv_file
with open(csv_file, "w") as f:
f.write("from,to\n")
with open(graph_path, "r") as fin:
for line in fin:
u, v, label = line.strip().split()
if label in relationships:
with open(csv_files[label], "a") as fout:
fout.write(f"{u},{v}\n")
else:
with open(csv_files["other"], "a") as fout:
fout.write(f"{u},{v}\n")
return csv_files
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='command line interface for graph downloader')
parser.add_argument(
'--graph'
, required=True
, type=str
, help='graph name'
)
parser.add_argument(
'--relationships'
, required=True
, type=str
, help='comma separated list of relationships'
)
args = parser.parse_args()
graph_name = args.graph
relationships = args.relationships.split(',')
path = Path(".").parent.resolve()
path_to_graph = cfpq_data.download(graph_name)
graph = cfpq_data.graph_from_csv(path_to_graph)
graph_to_csv(graph_name, f"{path}/" + graph_name, relationships)
generate_nodes(f"{path}/" + graph_name, graph_name, graph.number_of_nodes())