forked from OliiverHu/MaskGraphene
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
327 lines (253 loc) · 10.8 KB
/
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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import argparse
import random
import psutil
import yaml
import logging
from functools import partial
from tensorboardX import SummaryWriter
import wandb
import numpy as np
import torch
import torch.nn as nn
from torch import optim as optim
import dgl
import dgl.function as fn
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
def accuracy(y_pred, y_true):
y_true = y_true.squeeze().long()
preds = y_pred.max(1)[1].type_as(y_true)
correct = preds.eq(y_true).double()
correct = correct.sum().item()
return correct / len(y_true)
def set_random_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.determinstic = True
def get_current_lr(optimizer):
return optimizer.state_dict()["param_groups"][0]["lr"]
def build_args_ST():
parser = argparse.ArgumentParser(description="GAT")
parser.add_argument("--seeds", type=int, nargs="+", default=[0])
parser.add_argument("--dataset", type=str, default="DLPFC")
parser.add_argument("--exp_fig_dir", type=str, default="./")
parser.add_argument("--h5ad_save_dir", type=str, default="./")
parser.add_argument("--st_data_dir", type=str, default="./")
parser.add_argument("--pi_dir", type=str, default="./")
parser.add_argument("--consecutive_prior", type=int, default=0)
parser.add_argument("--section_ids", type=str, help="a list of slice name strings sep by comma, with no spacing")
parser.add_argument("--num_class", type=int, default=7)
parser.add_argument("--hvgs", type=int, default=7000)
parser.add_argument("--device", type=int, default=3)
parser.add_argument("--max_epoch", type=int, default=500,
help="number of training epochs")
parser.add_argument("--max_epoch_triplet", type=int, default=500,
help="number of training epochs for triplet loss")
parser.add_argument("--warmup_steps", type=int, default=-1)
parser.add_argument("--num_heads", type=int, default=1,
help="number of hidden attention heads")
parser.add_argument("--num_out_heads", type=int, default=1,
help="number of output attention heads")
parser.add_argument("--num_layers", type=int, default=2,
help="number of hidden layers")
parser.add_argument("--num_dec_layers", type=int, default=2)
parser.add_argument("--num_remasking", type=int, default=3) # K views as in paper
parser.add_argument("--num_hidden", type=str, default="1024,64",
help="number of hidden units")
parser.add_argument("--residual", action="store_true", default=False,
help="use residual connection")
parser.add_argument("--in_drop", type=float, default=.1,
help="input feature dropout")
parser.add_argument("--attn_drop", type=float, default=.05,
help="attention dropout")
parser.add_argument("--norm", type=str, default=None)
parser.add_argument("--lr", type=float, default=0.001,
help="learning rate")
parser.add_argument("--weight_decay", type=float, default=0,
help="weight decay")
parser.add_argument("--negative_slope", type=float, default=0.2,
help="the negative slope of leaky relu")
parser.add_argument("--activation", type=str, default="elu")
parser.add_argument("--mask_rate", type=float, default=0.5) # mask rate for input node features
parser.add_argument("--remask_rate", type=float, default=0.5) # mask rate for node enc features
parser.add_argument("--remask_method", type=str, default="random")
parser.add_argument("--mask_type", type=str, default="mask",
help="`mask` or `drop`")
parser.add_argument("--mask_method", type=str, default="random")
parser.add_argument("--drop_edge_rate", type=float, default=0.0)
parser.add_argument("--encoder", type=str, default="gat")
parser.add_argument("--decoder", type=str, default="gat")
parser.add_argument("--loss_fn", type=str, default="sce") # sce or mse
parser.add_argument("--alpha_l", type=float, default=2) # gamma in recon loss, gamma in latent loss is set to be 1
parser.add_argument("--optimizer", type=str, default="adam")
parser.add_argument("--linear_prob", action="store_true", default=False)
parser.add_argument("--no_pretrain", action="store_true")
# parser.add_argument("--load_model", action="store_true")
parser.add_argument("--checkpoint_path", type=str, default=None)
parser.add_argument("--use_cfg", action="store_true")
parser.add_argument("--logging", default=False)
parser.add_argument("--scheduler", action="store_true", default=False)
parser.add_argument("--batch_size", type=int, default=512) # not used in our setting with full batch training
parser.add_argument("--sampling_method", type=str, default="saint", help="sampling method, `lc` or `saint`")
parser.add_argument("--label_rate", type=float, default=1.0)
parser.add_argument("--lam", type=float, default=1.0) # mixing coeff in latent with recon balancing
# parser.add_argument("--full_graph_forward", action="store_true", default=False)
parser.add_argument("--delayed_ema_epoch", type=int, default=0)
parser.add_argument("--replace_rate", type=float, default=0.0)
parser.add_argument("--momentum", type=float, default=0.996)
parser.add_argument("--load_model", default=False)
# args = parser.parse_args()
args, unknown = parser.parse_known_args()
return args
def create_activation(name):
if name == "relu":
return nn.ReLU()
elif name == "gelu":
return nn.GELU()
elif name == "prelu":
return nn.PReLU()
elif name == "selu":
return nn.SELU()
elif name == "elu":
return nn.ELU()
elif name == "silu":
return nn.SiLU()
elif name is None:
return nn.Identity()
else:
raise NotImplementedError(f"{name} is not implemented.")
def identity_norm(x):
def func(x):
return x
return func
def create_norm(name):
if name == "layernorm":
return nn.LayerNorm
elif name == "batchnorm":
return nn.BatchNorm1d
elif name == "identity":
return identity_norm
else:
# print("Identity norm")
return None
def create_optimizer(opt, model, lr, weight_decay, get_num_layer=None, get_layer_scale=None):
opt_lower = opt.lower()
parameters = model.parameters()
opt_args = dict(lr=lr, weight_decay=weight_decay)
opt_split = opt_lower.split("_")
opt_lower = opt_split[-1]
if opt_lower == "adam":
optimizer = optim.Adam(parameters, **opt_args)
elif opt_lower == "adamw":
optimizer = optim.AdamW(parameters, **opt_args)
elif opt_lower == "adadelta":
optimizer = optim.Adadelta(parameters, **opt_args)
elif opt_lower == "sgd":
opt_args["momentum"] = 0.9
return optim.SGD(parameters, **opt_args)
else:
raise NotImplementedError("Invalid optimizer")
return optimizer
def show_occupied_memory():
process = psutil.Process(os.getpid())
return process.memory_info().rss / 1024**2
# -------------------
def mask_edge(graph, mask_prob):
E = graph.num_edges()
mask_rates = torch.ones(E) * mask_prob
masks = torch.bernoulli(1 - mask_rates)
mask_idx = masks.nonzero().squeeze(1)
return mask_idx
def drop_edge(graph, drop_rate, return_edges=False):
if drop_rate <= 0:
return graph
graph = graph.remove_self_loop()
n_node = graph.num_nodes()
edge_mask = mask_edge(graph, drop_rate)
src, dst = graph.edges()
nsrc = src[edge_mask]
ndst = dst[edge_mask]
ng = dgl.graph((nsrc, ndst), num_nodes=n_node)
ng = ng.add_self_loop()
return ng
def visualize(x, y, method="tsne"):
if torch.is_tensor(x):
x = x.cpu().numpy()
if torch.is_tensor(y):
y = y.cpu().numpy()
if method == "tsne":
func = TSNE(n_components=2)
else:
func = PCA(n_components=2)
out = func.fit_transform(x)
plt.scatter(out[:, 0], out[:, 1], c=y)
plt.savefig("vis.png")
def load_best_configs(args):
dataset_name = args.dataset
config_path = os.path.join("configs", f"{dataset_name}.yaml")
with open(config_path, "r") as f:
configs = yaml.load(f, yaml.FullLoader)
for k, v in configs.items():
if "lr" in k or "weight_decay" in k:
v = float(v)
setattr(args, k, v)
logging.info(f"----- Using best configs from {config_path} -----")
return args
def cosine_scheduler(base_value, final_value, epochs, niter_per_ep, warmup_epochs=0, start_warmup_value=0):
warmup_schedule = np.array([])
warmup_iters = warmup_epochs * niter_per_ep
if warmup_epochs > 0:
warmup_schedule = np.linspace(start_warmup_value, base_value, warmup_iters)
iters = np.arange(epochs * niter_per_ep - warmup_iters)
schedule = final_value + 0.5 * (base_value - final_value) * (1 + np.cos(np.pi * iters / len(iters)))
scheduler = np.concatenate((warmup_schedule, schedule))
assert len(scheduler) == epochs * niter_per_ep
return scheduler
# ------ logging ------
class TBLogger(object):
def __init__(self, log_path="./logging_data", name="run"):
super(TBLogger, self).__init__()
if not os.path.exists(log_path):
os.makedirs(log_path, exist_ok=True)
self.last_step = 0
self.log_path = log_path
raw_name = os.path.join(log_path, name)
name = raw_name
for i in range(1000):
name = raw_name + str(f"_{i}")
if not os.path.exists(name):
break
self.writer = SummaryWriter(logdir=name)
def note(self, metrics, step=None):
if step is None:
step = self.last_step
for key, value in metrics.items():
self.writer.add_scalar(key, value, step)
self.last_step = step
def finish(self):
self.writer.close()
class WandbLogger(object):
def __init__(self, log_path, project, args):
self.log_path = log_path
self.project = project
self.args = args
self.last_step = 0
self.project = project
self.start()
def start(self):
self.run = wandb.init(config=self.args, project=self.project)
def log(self, metrics, step=None):
if not hasattr(self, "run"):
self.start()
if step is None:
step = self.last_step
self.run.log(metrics)
self.last_step = step
def finish(self):
self.run.finish()