-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_graph_kernels.py
268 lines (222 loc) · 7.19 KB
/
main_graph_kernels.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import argparse
import grakel
import networkx as nx
import numpy as np
from grakel import graph_from_networkx
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import ParameterGrid
from sklearn.multioutput import MultiOutputClassifier
from sklearn.svm import SVC
from torch_geometric.data import Dataset
from tqdm import tqdm
from data_loading import (
load_dataset,
DATASET_TASK_TYPES,
load_dataset_splits,
)
from models import evaluate_model
from utils import ensure_bool, multioutput_auroc_score
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset_name",
choices=[
"all",
"ogbg-molbace",
"ogbg-molbbbp",
"ogbg-molclintox",
"ogbg-molsider",
"ogbg-moltox21",
],
default="all",
help=(
"Dataset name. You can either provide dataset name from "
"MoleculeNet (via OGB), or 'all' to run on MoleculeNet "
"apart from HIV and MUV"
),
)
parser.add_argument(
"--kernel_type",
type=str,
default="WL",
choices=[
"EH",
"graphlet",
"propagation",
"SP",
"RW",
"VH",
"WL",
"WL-OA",
],
help="Kernel type to use",
)
parser.add_argument(
"--verbose",
type=ensure_bool,
default=True,
help="Should print out verbose output?",
)
return parser.parse_args()
def create_grakel_graphs(
dataset: Dataset, kernel_type: str, verbose: bool
) -> np.ndarray:
graphs = []
dataset = tqdm(dataset) if verbose else dataset
for data in dataset:
graph = nx.Graph()
for idx, atom_features in enumerate(data.x):
atom_type = atom_features[0].item()
graph.add_node(idx, atom_type=atom_type)
for bond_idx, (atom_idx_1, atom_idx_2) in enumerate(
zip(data.edge_index[0].tolist(), data.edge_index[1].tolist())
):
bond_features = data.edge_attr[bond_idx]
bond_type = bond_features[0].item() if bond_features.shape[0] > 0 else 0
graph.add_edge(atom_idx_1, atom_idx_2, bond_type=bond_type)
graphs.append(graph)
graphs = list(
graph_from_networkx(
graphs, node_labels_tag="atom_type", edge_labels_tag="bond_type"
)
)
return np.array(graphs)
def precomputed_kernel_svm_tuning(
X_kernel_train: np.ndarray,
X_kernel_valid: np.ndarray,
y_train: np.ndarray,
y_valid: np.ndarray,
task_type: str,
verbose: bool,
):
best_model = None
best_score = 0
Cs = [1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3]
Cs = tqdm(Cs) if verbose else Cs
# make sure we have no NaN values
X_kernel_train[~np.isfinite(X_kernel_train)] = 0
X_kernel_valid[~np.isfinite(X_kernel_valid)] = 0
for C in Cs:
svm = SVC(
C=C,
kernel="precomputed",
probability=True,
cache_size=1024,
class_weight="balanced",
random_state=0,
)
if task_type == "multioutput_classification":
svm = MultiOutputClassifier(svm, n_jobs=-1)
svm.fit(X_kernel_train, y_train)
y_pred = svm.predict_proba(X_kernel_valid)
if task_type == "multioutput_classification":
# extract positive class probability for each task
y_pred = [y_pred_i[:, 1] for y_pred_i in y_pred]
y_pred = np.column_stack(y_pred)
score = multioutput_auroc_score(y_valid, y_pred)
else:
score = roc_auc_score(y_valid, y_pred[:, 1])
if score > best_score:
best_model = svm
best_score = score
return best_model, best_score
def train_kernel_and_svm(
X_train: list[grakel.Graph],
X_valid: list[grakel.Graph],
y_train: np.ndarray,
y_valid: np.ndarray,
task_type: str,
kernel_type: str,
verbose: bool,
):
common_kernel_args = dict(n_jobs=-1, normalize=True, verbose=verbose)
kernels_map = {
"EH": (grakel.EdgeHistogram, {}),
"graphlet": (grakel.GraphletSampling, {"k": list(range(3, 6))}),
"propagation": (grakel.Propagation, {"t_max": list(range(1, 6))}),
"SP": (grakel.ShortestPath, {}),
"VH": (grakel.VertexHistogram, {}),
"WL": (grakel.WeisfeilerLehman, {"n_iter": list(range(1, 6))}),
"WL-OA": (
grakel.WeisfeilerLehmanOptimalAssignment,
{"n_iter": list(range(1, 6))},
),
}
try:
kernel_cls, kernel_param_grid = kernels_map[kernel_type]
except KeyError:
raise ValueError(f"Kernel type {kernel_type} not recognized")
best_kernel = None
best_model = None
best_score = 0
kernel_param_grid = ParameterGrid(kernel_param_grid)
kernel_param_grid = tqdm(kernel_param_grid) if verbose else kernel_param_grid
for kernel_params in kernel_param_grid:
kernel = kernel_cls(**kernel_params, **common_kernel_args)
X_kernel_train = kernel.fit_transform(X_train)
X_kernel_valid = kernel.transform(X_valid)
model, score = precomputed_kernel_svm_tuning(
X_kernel_train, X_kernel_valid, y_train, y_valid, task_type, verbose
)
if score > best_score:
best_kernel = kernel
best_model = model
best_score = score
return best_kernel, best_model
def perform_experiment(
dataset_name: str,
kernel_type: str,
verbose: bool,
) -> float:
dataset = load_dataset(dataset_name)
task_type = DATASET_TASK_TYPES[dataset_name]
train_idxs, valid_idxs, test_idxs = load_dataset_splits(
dataset_name, train_valid_test_idxs=True
)
X = create_grakel_graphs(dataset, kernel_type, verbose)
X_train = X[train_idxs]
X_valid = X[valid_idxs]
X_test = X[test_idxs]
y = np.array(dataset.y)
if task_type == "classification":
y = y.ravel()
y_train = y[train_idxs]
y_valid = y[valid_idxs]
y_test = y[test_idxs]
# fill NaN values with zeros for multioutput classification
y_train[np.isnan(y_train)] = 0
y_valid[np.isnan(y_valid)] = 0
kernel, model = train_kernel_and_svm(
X_train, X_valid, y_train, y_valid, task_type, kernel_type, verbose
)
X_test = kernel.transform(X_test)
# make sure we have no NaN values
X_test[~np.isfinite(X_test)] = 0
test_metric = evaluate_model(
dataset_name=dataset_name,
task_type=task_type,
model=model,
X_test=X_test,
y_test=y_test,
)
return test_metric
if __name__ == "__main__":
args = parse_args()
if args.dataset_name == "all":
datasets = [
"ogbg-molbace",
"ogbg-molbbbp",
"ogbg-molclintox",
"ogbg-molsider",
"ogbg-moltox21",
]
else:
datasets = [args.dataset_name]
for dataset_name in datasets:
print(dataset_name)
test_mean = perform_experiment(
dataset_name=dataset_name,
kernel_type=args.kernel_type,
verbose=args.verbose,
)
print(f"AUROC: {100 * test_mean:.1f}")