-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
150 lines (132 loc) · 5.15 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
import os
import math
import sys
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as Func
from torch.nn import init
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
import torch.optim as optim
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from numpy import linalg as LA
import networkx as nx
from tqdm import tqdm
import time
from pandas import read_csv
from sklearn.preprocessing import MinMaxScaler
def asim(p1,p2,attributes):
distance = 0
for i in range(attributes):
distance = distance + (p1[i]-p2[i])**2
NORM = math.sqrt(distance)
if NORM ==0:
return 0
return 1/(NORM)
def anorm(p1,p2,attributes):
distance = 0
for i in range(attributes):
distance = distance + (p1[i]-p2[i])**2
NORM = math.sqrt(distance)
return NORM
def aexp(p1,p2,attributes):
distance = 0
for i in range(attributes):
distance = distance + (p1[i]-p2[i])**2
NORM = math.exp(-math.sqrt(distance))
return NORM
def seq_to_graph(seq_,norm_lap_matr = True):
seq_len = seq_.shape[0] # time_sequence(T=1,2,4,8...)
category = seq_.shape[1] # A/N=5
attributes = seq_.shape[2] #D=3
V = np.zeros((seq_len,category,attributes))
A = np.zeros((seq_len,category,category))
A2 = np.zeros((seq_len,category,category)) #kernel
for s in range(seq_len):
V[s] = seq_[s]
A[s] = [[0,0.127,0.61,0.209,0.054],
[0.092,0,0.533,0.243,0.131],
[0.265,0.32,0,0.174,0.241],
[0.19,0.305,0.365,0,0.14],
[0.057,0.192,0.587,0.163,0]]
for h in range(category):
A2[s,h,h] = 1
for k in range(h+1,category):
l2_norm = aexp(seq_[s][h],seq_[s][k],attributes)
A2[s,h,k] = l2_norm
A2[s,k,h] = l2_norm
if norm_lap_matr:
G = nx.from_numpy_matrix(A[s,:,:])
A[s,:,:] = nx.normalized_laplacian_matrix(G).toarray()
G = nx.from_numpy_matrix(A2[s,:,:])
A2[s,:,:] = nx.normalized_laplacian_matrix(G).toarray()
return torch.from_numpy(V).type(torch.float),\
torch.from_numpy(A).type(torch.float),\
torch.from_numpy(A2).type(torch.float)
def dataLoading(data_dir, groundTruth_col):
dataset = read_csv(data_dir, usecols=range(0,groundTruth_col), engine='python').values.astype('float32')
groundTruth = read_csv(data_dir, usecols=[groundTruth_col], engine='python').values.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
return dataset, groundTruth
class ElectricalDataset(Dataset):
def __init__(
self, data_dir, obs_len=8, pred_len=1, groundTruth_col=15, real_time = True, norm_lap_matr = True):
super(ElectricalDataset, self).__init__()
self.data_dir = data_dir
self.obs_len = obs_len
self.pred_len = pred_len
self.groundTruth_col = groundTruth_col
if real_time: #real-time prediction
self.seq_len = self.obs_len + self.pred_len - 1
else: #anticipation
self.seq_len = self.obs_len + self.pred_len
self.norm_lap_matr = norm_lap_matr
num_in_seq = []
seq_list = []
groundTruth_list = []
dataset, groundTruth = dataLoading(data_dir, groundTruth_col)
num_sequences = len(dataset) - self.seq_len + 1
for idx in range(num_sequences):
curr_seq_data = np.concatenate(dataset[idx:idx + self.seq_len], axis=0)
curr_seq_data = np.reshape(curr_seq_data, (self.seq_len, 5, 3))
num_in_seq.append(self.seq_len)
seq_list.append(curr_seq_data)
groundTruth_list.append(groundTruth[idx+self.seq_len-1])
self.num_seq = len(seq_list)
seq_list = np.concatenate(seq_list, axis=0)
groundTruth_list = np.concatenate(groundTruth_list, axis=0)
# Convert numpy -> Torch Tensor
self.seq_list = torch.from_numpy(seq_list).type(torch.float)
self.groundTruth_list = torch.from_numpy(groundTruth_list).type(torch.float)
cum_start_idx = [0] + np.cumsum(num_in_seq).tolist()
self.seq_start_end = [
(start, end)
for start, end in zip(cum_start_idx, cum_start_idx[1:])
]
#Convert to Graphs
self.v_obs = []
self.A_obs = []
self.A2_obs = []
print("Processing Data .....")
pbar = tqdm(total=len(self.seq_start_end))
for ss in range(len(self.seq_start_end)):
pbar.update(1)
start, end = self.seq_start_end[ss]
v_,a_,a2_ = seq_to_graph(self.seq_list[start:end,:],self.norm_lap_matr)
self.v_obs.append(v_.clone())
self.A_obs.append(a_.clone())
self.A2_obs.append(a2_.clone())
pbar.close()
print("Data Processed .....")
def __len__(self):
return self.num_seq
def __getitem__(self, index):
out = [
self.v_obs[index], self.A_obs[index], self.A2_obs[index],
self.groundTruth_list[index]
]
return out