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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
import argparse
import csv
import os
import random
import sys
from pathlib import Path
import numpy as np
import pandas as pd
import torch
try:
import wandb
except Exception as e:
pass
def update_average(prev_avg, prev_counts, curr_avg, curr_counts):
denom = prev_counts + curr_counts
if isinstance(curr_counts, torch.Tensor):
denom += (denom == 0).float()
elif isinstance(curr_counts, int) or isinstance(curr_counts, float):
if denom == 0:
return 0.
else:
raise ValueError('Type of curr_counts not recognized')
prev_weight = prev_counts / denom
curr_weight = curr_counts / denom
return prev_weight * prev_avg + curr_weight * curr_avg
# Taken from https://sumit-ghosh.com/articles/parsing-dictionary-key-value-pairs-kwargs-argparse-python/
class ParseKwargs(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, dict())
for value in values:
key, value_str = value.split('=')
if value_str.replace('-', '').isnumeric():
processed_val = int(value_str)
elif value_str.replace('-', '').replace('.', '').isnumeric():
processed_val = float(value_str)
elif value_str in ['True', 'true']:
processed_val = True
elif value_str in ['False', 'false']:
processed_val = False
else:
processed_val = value_str
getattr(namespace, self.dest)[key] = processed_val
def parse_bool(v):
if v.lower() == 'true':
return True
elif v.lower() == 'false':
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def save_model(algorithm, epoch, best_val_metric, prefix, suffix):
path = prefix.parent / (prefix.name + suffix)
state = {}
state['algorithm'] = algorithm.state_dict()
state['epoch'] = epoch
state['best_val_metric'] = best_val_metric
torch.save(state, path)
def load(algorithm, path):
path = Path(path)
state = torch.load(path)
algorithm.load_state_dict(state['algorithm'])
return state['epoch'], state['best_val_metric']
def log_group_data(datasets, grouper, logger):
for k, dataset in datasets.items():
name = dataset['name']
dataset = dataset['dataset']
logger.write(f'{name} data...\n')
if grouper is None:
logger.write(f' n = {len(dataset)}\n')
else:
_, group_counts = grouper.metadata_to_group(
dataset.metadata_array,
return_counts=True)
group_counts = group_counts.tolist()
for group_idx in range(grouper.n_groups):
logger.write(f' {grouper.group_str(group_idx)}: n = {group_counts[group_idx]:.0f}\n')
logger.flush()
class Logger(object):
def __init__(self, fpath=None, mode='w'):
self.console = sys.stdout
self.file = None
if fpath is not None:
self.file = open(Path(fpath), mode)
def __del__(self):
self.close()
def __enter__(self):
pass
def __exit__(self, *args):
self.close()
def write(self, msg):
self.console.write(msg)
if self.file is not None:
self.file.write(msg)
def flush(self):
self.console.flush()
if self.file is not None:
self.file.flush()
os.fsync(self.file.fileno())
def close(self):
self.console.close()
if self.file is not None:
self.file.close()
class BatchLogger:
def __init__(self, csv_path, mode='w', use_wandb=False):
self.path = Path(csv_path)
self.mode = mode
self.file = open(self.path, mode)
self.is_initialized = False
# Use Weights and Biases for logging
self.use_wandb = use_wandb
if use_wandb:
self.split = self.path.stem
def setup(self, log_dict):
columns = log_dict.keys()
# Move epoch and batch to the front if in the log_dict
for key in ['batch', 'epoch']:
if key in columns:
columns = [key] + [k for k in columns if k != key]
self.writer = csv.DictWriter(self.file, fieldnames=columns)
if self.mode == 'w' or (not os.path.exists(self.path)) or os.path.getsize(self.path) == 0:
self.writer.writeheader()
self.is_initialized = True
def log(self, log_dict):
if self.is_initialized is False:
self.setup(log_dict)
self.writer.writerow(log_dict)
self.flush()
if self.use_wandb:
results = {}
for key in log_dict:
new_key = f'{self.split}/{key}'
results[new_key] = log_dict[key]
wandb.log(results)
def flush(self):
self.file.flush()
def close(self):
self.file.close()
def set_seed(seed):
"""Sets seed"""
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def log_config(config, logger):
for name, val in vars(config).items():
logger.write(f'{name.replace("_", " ").capitalize()}: {val}\n')
logger.write('\n')
def initialize_wandb(config):
name = config.dataset + '_' + config.algorithm + '_' + f"seed-{config.seed}"
wandb_runner = wandb.init(name=name, project=f"graphdg", entity='graphnet', config=config, reinit=True)
return wandb_runner
def close_wandb(wandb_runner):
wandb_runner.finish()
def save_pred(y_pred, prefix, suffix):
csv_path = prefix.parent / (prefix.name + suffix + '.csv')
pth_path = prefix.parent / (prefix.name + suffix + '.pth')
# Single tensor
if torch.is_tensor(y_pred):
df = pd.DataFrame(y_pred.numpy())
df.to_csv(csv_path, index=False, header=False)
# Dictionary
elif isinstance(y_pred, dict) or isinstance(y_pred, list):
torch.save(y_pred, pth_path)
else:
raise TypeError("Invalid type for save_pred")
def get_replicate_str(dataset, config):
if dataset['dataset'].dataset_name == 'poverty':
replicate_str = f"fold-{config.dataset_kwargs['fold']}"
else:
replicate_str = f"seed-{config.seed}"
return replicate_str
def get_pred_prefix(dataset, config):
dataset_name = dataset['dataset'].dataset_name
split = dataset['split']
replicate_str = get_replicate_str(dataset, config)
prefix = Path(config.log_dir) / f"{dataset_name}_{config.algorithm}_{config.parameter}_{config.model}_{replicate_str}_split-{split}_"
return prefix
def get_model_prefix(dataset, config):
dataset_name = dataset['dataset'].dataset_name
replicate_str = get_replicate_str(dataset, config)
prefix = Path(config.log_dir) / f"{dataset_name}_{config.algorithm}_{config.parameter}_{config.model}_{replicate_str}_"
return prefix
def move_to(obj, device):
if isinstance(obj, dict):
return {k: move_to(v, device) for k, v in obj.items()}
elif isinstance(obj, list):
return [move_to(v, device) for v in obj]
elif isinstance(obj, float) or isinstance(obj, int):
return obj
else:
# Assume obj is a Tensor or other type
# (like Batch, for MolPCBA) that supports .to(device)
return obj.to(device)
def detach_and_clone(obj):
if torch.is_tensor(obj):
return obj.detach().clone()
elif isinstance(obj, dict):
return {k: detach_and_clone(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [detach_and_clone(v) for v in obj]
elif isinstance(obj, float) or isinstance(obj, int):
return obj
else:
raise TypeError("Invalid type for detach_and_clone")
def collate_list(vec):
"""
If vec is a list of Tensors, it concatenates them all along the first dimension.
If vec is a list of lists, it joins these lists together, but does not attempt to
recursively collate. This allows each element of the list to be, e.g., its own dict.
If vec is a list of dicts (with the same keys in each dict), it returns a single dict
with the same keys. For each key, it recursively collates all entries in the list.
"""
if not isinstance(vec, list):
raise TypeError("collate_list must take in a list")
elem = vec[0]
if torch.is_tensor(elem):
return torch.cat(vec)
elif isinstance(elem, list):
return [obj for sublist in vec for obj in sublist]
elif isinstance(elem, dict):
return {k: collate_list([d[k] for d in vec]) for k in elem}
else:
raise TypeError("Elements of the list to collate must be tensors or dicts.")
def remove_key(key):
"""
Returns a function that strips out a key from a dict.
"""
def remove(d):
if not isinstance(d, dict):
raise TypeError("remove_key must take in a dict")
return {k: v for (k, v) in d.items() if k != key}
return remove
|