-
Notifications
You must be signed in to change notification settings - Fork 52
/
tu_utils.py
163 lines (133 loc) · 5.85 KB
/
tu_utils.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
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
160
161
162
163
#
# Copyright (C) 2020 University of Pisa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from collections import defaultdict
import numpy as np
from utils.utils import one_hot
from .graph import Graph
def parse_tu_data(name, raw_dir):
# setup paths
indicator_path = raw_dir / name / f'{name}_graph_indicator.txt'
edges_path = raw_dir / name / f'{name}_A.txt'
graph_labels_path = raw_dir / name / f'{name}_graph_labels.txt'
node_labels_path = raw_dir / name / f'{name}_node_labels.txt'
edge_labels_path = raw_dir / name / f'{name}_edge_labels.txt'
node_attrs_path = raw_dir / name / f'{name}_node_attributes.txt'
edge_attrs_path = raw_dir / name / f'{name}_edge_attributes.txt'
unique_node_labels = set()
unique_edge_labels = set()
indicator, edge_indicator = [-1], [(-1,-1)]
graph_nodes = defaultdict(list)
graph_edges = defaultdict(list)
node_labels = defaultdict(list)
edge_labels = defaultdict(list)
node_attrs = defaultdict(list)
edge_attrs = defaultdict(list)
with open(indicator_path, "r") as f:
for i, line in enumerate(f.readlines(), 1):
line = line.rstrip("\n")
graph_id = int(line)
indicator.append(graph_id)
graph_nodes[graph_id].append(i)
with open(edges_path, "r") as f:
for i, line in enumerate(f.readlines(), 1):
line = line.rstrip("\n")
edge = [int(e) for e in line.split(',')]
edge_indicator.append(edge)
# edge[0] is a node id, and it is used to retrieve
# the corresponding graph id to which it belongs to
# (see README.txt)
graph_id = indicator[edge[0]]
graph_edges[graph_id].append(edge)
if node_labels_path.exists():
with open(node_labels_path, "r") as f:
for i, line in enumerate(f.readlines(), 1):
line = line.rstrip("\n")
node_label = int(line)
unique_node_labels.add(node_label)
graph_id = indicator[i]
node_labels[graph_id].append(node_label)
if edge_labels_path.exists():
with open(edge_labels_path, "r") as f:
for i, line in enumerate(f.readlines(), 1):
line = line.rstrip("\n")
edge_label = int(line)
unique_edge_labels.add(edge_label)
graph_id = indicator[edge_indicator[i][0]]
edge_labels[graph_id].append(edge_label)
if node_attrs_path.exists():
with open(node_attrs_path, "r") as f:
for i, line in enumerate(f.readlines(), 1):
line = line.rstrip("\n")
nums = line.split(",")
node_attr = np.array([float(n) for n in nums])
graph_id = indicator[i]
node_attrs[graph_id].append(node_attr)
if edge_attrs_path.exists():
with open(edge_attrs_path, "r") as f:
for i, line in enumerate(f.readlines(), 1):
line = line.rstrip("\n")
nums = line.split(",")
edge_attr = np.array([float(n) for n in nums])
graph_id = indicator[edge_indicator[i][0]]
edge_attrs[graph_id].append(edge_attr)
# get graph labels
graph_labels = []
with open(graph_labels_path, "r") as f:
for i, line in enumerate(f.readlines(), 1):
line = line.rstrip("\n")
target = int(line)
if target == -1:
graph_labels.append(0)
else:
graph_labels.append(target)
if min(graph_labels) == 1: # Shift by one to the left. Apparently this is necessary for multiclass tasks.
graph_labels = [l - 1 for l in graph_labels]
num_node_labels = max(unique_node_labels) if unique_node_labels != set() else 0
if num_node_labels != 0 and min(unique_node_labels) == 0: # some datasets e.g. PROTEINS have labels with value 0
num_node_labels += 1
num_edge_labels = max(unique_edge_labels) if unique_edge_labels != set() else 0
if num_edge_labels != 0 and min(unique_edge_labels) == 0:
num_edge_labels += 1
return {
"graph_nodes": graph_nodes,
"graph_edges": graph_edges,
"graph_labels": graph_labels,
"node_labels": node_labels,
"node_attrs": node_attrs,
"edge_labels": edge_labels,
"edge_attrs": edge_attrs
}, num_node_labels, num_edge_labels
def create_graph_from_tu_data(graph_data, target, num_node_labels, num_edge_labels):
nodes = graph_data["graph_nodes"]
edges = graph_data["graph_edges"]
G = Graph(target=target)
for i, node in enumerate(nodes):
label, attrs = None, None
if graph_data["node_labels"] != []:
label = one_hot(graph_data["node_labels"][i], num_node_labels)
if graph_data["node_attrs"] != []:
attrs = graph_data["node_attrs"][i]
G.add_node(node, label=label, attrs=attrs)
for i, edge in enumerate(edges):
n1, n2 = edge
label, attrs = None, None
if graph_data["edge_labels"] != []:
label = one_hot(graph_data["edge_labels"][i], num_edge_labels)
if graph_data["edge_attrs"] != []:
attrs = graph_data["edge_attrs"][i]
G.add_edge(n1, n2, label=label, attrs=attrs)
return G