Source code for openddi.utils.utils

from __future__ import division
from __future__ import print_function

import os
import random
import numpy as np
import scipy.sparse as sp
import torch
from sklearn.metrics import (
    accuracy_score,
    precision_recall_fscore_support
)
import matplotlib.pyplot as plt

os.environ["CUDA_VISIBLE_DEVICES"] = "1"

[docs] def set_random_seed(seed, deterministic=False): torch.manual_seed(seed) torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) np.random.seed(seed) random.seed(seed) if deterministic: torch.backends.cudnn.benchmark = False torch.backends.cudnn.deterministic = True
set_random_seed(1, deterministic=True)
[docs] def normalize(mx): rowsum = np.array(mx.sum(1)) r_inv = np.power(rowsum, -1).flatten() r_inv[np.isinf(r_inv)] = 0. r_mat_inv = sp.diags(r_inv) mx = r_mat_inv.dot(mx) return mx
[docs] def normalize_adj(adj): row_sum = np.array(adj.sum(1)) d_inv_sqrt = np.power(row_sum, -0.5).flatten() d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0. d_mat_inv_sqrt = sp.diags(d_inv_sqrt) return adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()
[docs] def skip_adj(adj): adj2 = adj.dot(adj) adj2 = adj2.sign() return adj2
[docs] def sparse_mx_to_torch_sparse_tensor(sparse_mx): sparse_mx = sparse_mx.tocoo().astype(np.float32) indices = torch.from_numpy( np.vstack((sparse_mx.row, sparse_mx.col)).astype(np.int64)) values = torch.from_numpy(sparse_mx.data) shape = torch.Size(sparse_mx.shape) return torch.sparse.FloatTensor(indices, values, shape)
[docs] def compute_multiclass_metrics(y_true, y_pred): y_true = np.asarray(y_true) y_pred = np.asarray(y_pred) acc = accuracy_score(y_true, y_pred) prec, rec, f1, _ = precision_recall_fscore_support( y_true, y_pred, average='macro', zero_division=0 ) return { 'accuracy': acc, 'precision_macro': prec, 'recall_macro': rec, 'f1_macro': f1 }
[docs] def plot_metric(history, metric_name, save_path): plt.figure() plt.plot(history.get('train', []), label='train') plt.plot(history.get('val', []), label='val') plt.xlabel('epoch') plt.ylabel(metric_name) plt.legend() os.makedirs(os.path.dirname(save_path), exist_ok=True) plt.savefig(save_path, bbox_inches='tight') plt.close()
[docs] def ensure_dir(path): os.makedirs(path, exist_ok=True) return path