-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
67 lines (55 loc) · 2.17 KB
/
utils.py
File metadata and controls
67 lines (55 loc) · 2.17 KB
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
# -*- coding: utf-8 -*-
import numpy as np
import pickle as pkl
import scipy.sparse as sp
# from scipy.sparse.linalg.eigen.arpack import eigsh
from scipy.sparse.linalg import eigsh
import sys, os
import torch
import re
import string
import torch
import torch.nn.functional as F
from sklearn import preprocessing
from sklearn.metrics import mean_absolute_error
from scipy.signal import find_peaks
# define peak area in ground truth data
def peak_error(y_true_states, y_pred_states, threshold):
# masked some low values (using training mean by states)
y_true_states[y_true_states < threshold] = 0
mask_idx = np.argwhere(y_true_states <= threshold)
for idx in mask_idx:
y_pred_states[idx[0]][idx[1]] = 0
# print(y_pred_states,np.count_nonzero(y_pred_states),np.count_nonzero(y_true_states))
peak_mae_raw = mean_absolute_error(y_true_states, y_pred_states, multioutput='raw_values')
peak_mae = np.mean(peak_mae_raw)
# peak_mae_std = np.std(peak_mae_raw)
return peak_mae
def normalize_adj2(adj):
"""Symmetrically normalize adjacency matrix."""
# print(adj.shape)
# adj += sp.eye(adj.shape[0])
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1))
d_inv_sqrt = np.power(rowsum, -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()
def normalize(mx):
"""Row-normalize sparse matrix (normalize feature)"""
rowsum = np.array(mx.sum(1))
r_inv = np.float_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
def sparse_mx_to_torch_sparse_tensor(sparse_mx):
"""Convert a scipy sparse matrix to a torch sparse tensor."""
sparse_mx = sparse_mx.tocoo().astype(np.float32)
if len(sparse_mx.row) == 0 or len(sparse_mx.col)==0:
print(sparse_mx.row,sparse_mx.col)
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)