forked from VA602AA-master/VASTKnowledgeGraphVisualization
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_datasets.py
More file actions
159 lines (138 loc) · 4.85 KB
/
Copy pathcreate_datasets.py
File metadata and controls
159 lines (138 loc) · 4.85 KB
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
import argparse
import csv
import json
import re
import shutil
import sys
from pathlib import Path
import networkx as nx
from download_github_zip import process_url
def create_influence_graph(destination_dir):
data_dir = Path('data')
try:
process_url(
'https://github.com/vast-challenge/2025-data/blob/main/MC1_release.zip',
destination_dir=data_dir,
)
except Exception as e: # noqa: BLE001
print('Error:', e, file=sys.stderr)
return 1
data_path = data_dir / 'MC1_release/MC1_graph.json'
try:
with open(data_path) as file:
data = json.load(file)
except OSError as e:
print('Error', e, file=sys.stderr)
return 1
graph = nx.node_link_graph(data, edges='links')
nodes = filter(
lambda node: node[1].get('Node Type') in ('Song', 'Album'),
graph.nodes(data=True),
)
infl_graph = graph.subgraph([node for node, _ in nodes]).copy()
for node in infl_graph.nodes:
infl_graph.nodes[node]['Node Type'] = infl_graph.nodes[node].get('genre', 'Unknown')
output_path = destination_dir / 'genre_influence.json'
try:
with open( output_path, 'w') as file:
json.dump(nx.node_link_data(infl_graph), file)
except OSError as e:
print('Error', e, file=sys.stderr)
return 1
print(f'Created {output_path}:', graph)
return 0
def download_asoiaf_data(data_dir):
names = ['all-nodes'] + [f'book{i}-edges' for i in range(1, 6)]
urls = [
f'https://github.com/mathbeveridge/asoiaf/blob/master/data/asoiaf-{name}.csv'
for name in names
]
try:
data_dir.mkdir(parents=True, exist_ok=False)
except Exception as e: # noqa: BLE001
print(f'Error {e}. Using existing files.', file=sys.stderr)
else:
try:
for url in urls:
process_url(url, destination_dir=data_dir)
except Exception as e: # noqa: BLE001
print('Error', e, file=sys.stderr)
return 1
return 0
def create_asoiaf_graph(destination_dir):
data_dir = Path('data')
asoiaf_dir = data_dir / 'asoiaf'
if download_asoiaf_data(asoiaf_dir):
return 1
graph = nx.MultiGraph()
# Add nodes
node_file = next(iter(asoiaf_dir.glob('*nodes*')))
nodes = []
families_re = re.compile('(Targaryen|Greyjoy|Snow|Baratheon|Lannister|Stark|Frey|Tyrell|Martell|Arryn|Mormont|Bolton|Tully)')
books = {'1': 'A Game of Thrones', '2': 'A Clash of Kings', '3': 'A Storm of Swords', '4':'A Feast of Crows', '5': 'A Dance with Dragons'}
try:
with open(node_file) as file:
reader = csv.DictReader(file)
for row in reader:
nodes.append((row['Id'],
{
'name': row['Label'],
'Node Type': families_re.search(row['Label']).group()
if families_re.search(row['Label']) else 'Other',
}))
except Exception as e: # noqa: BLE001
print('Error', e, file=sys.stderr)
return 1
graph.add_nodes_from(nodes)
# Add edges
edge_files = asoiaf_dir.glob('*edges*')
try:
for path in edge_files:
with open(path) as file:
reader = csv.DictReader(file)
graph.add_edges_from([
(row['Source'], row['Target'], {
'Edge Type': books.get(row['book'], 'Book Unknown'),
})
for row in reader
])
except Exception as e: # noqa: BLE001
print('Error', e, file=sys.stderr)
return 1
output_path = destination_dir / 'asoiaf_interaction.json'
try:
with open(output_path, 'w') as file:
json.dump(nx.node_link_data(graph, edges='links'), file)
except Exception as e: # noqa: BLE001
print('Error', e, file=sys.stderr)
return 1
print(f'Created {output_path}', graph)
return 0
def cleanup():
data_dir = Path('data')
dirs = ['MC1_release', 'asoiaf']
try:
for d in dirs:
shutil.rmtree(data_dir / d)
except Exception as e: # noqa: BLE001
print('Error', e, file=sys.stderr)
def parse_args():
parser = argparse.ArgumentParser(
description=("Create two Knowledge Graphs for testing the application."),
)
parser.add_argument(
"--output-dir",
default=Path(__file__).resolve().parent,
type=Path,
help='Directory where knowledge graph files are saved (default ./data)'
)
return parser.parse_args()
def main() -> int:
args = parse_args()
destination_dir = args.output_dir.resolve()
destination_dir.mkdir(parents=True, exist_ok=True)
create_influence_graph(destination_dir)
create_asoiaf_graph(destination_dir)
cleanup()
if __name__ == '__main__':
raise SystemExit(main())