-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutil.py
108 lines (90 loc) · 3.22 KB
/
util.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
import pandas
from tqdm import tqdm
import numpy as np
import torch
from collections import Counter
import math
def sigmoid(x):
return 1 / (1 + math.exp(-x))
def softmax(x):
x = np.exp(x)
return x / x.sum()
class ATMDataset:
def __init__(self, config, subset):
data = pandas.read_csv(f"data/{subset}_day.csv")
self.subset = subset
self.id = list(data['id'])
self.time = list(data['time'])
self.event = list(data['event'])
self.config = config
self.seq_len = config.seq_len
self.time_seqs, self.event_seqs = self.generate_sequence()
self.statistic()
def generate_sequence(self):
MAX_INTERVAL_VARIANCE = 1
pbar = tqdm(total=len(self.id) - self.seq_len + 1)
time_seqs = []
event_seqs = []
cur_end = self.seq_len - 1
while cur_end < len(self.id):
pbar.update(1)
cur_start = cur_end - self.seq_len + 1
if self.id[cur_start] != self.id[cur_end]:
cur_end += 1
continue
subseq = self.time[cur_start:cur_end + 1]
# if max(subseq) - min(subseq) > MAX_INTERVAL_VARIANCE:
# if self.subset == "train":
# cur_end += 1
# continue
time_seqs.append(self.time[cur_start:cur_end + 1])
event_seqs.append(self.event[cur_start:cur_end + 1])
cur_end += 1
return time_seqs, event_seqs
def __getitem__(self, item):
return self.time_seqs[item], self.event_seqs[item]
def __len__(self):
return len(self.time_seqs)
@staticmethod
def to_features(batch):
times, events = [], []
for time, event in batch:
time = np.array([time[0]] + time)
time = np.diff(time)
times.append(time)
events.append(event)
return torch.FloatTensor(times), torch.LongTensor(events)
def statistic(self):
print("TOTAL SEQs:", len(self.time_seqs))
# for i in range(10):
# print(self.time_seqs[i], "\n", self.event_seqs[i])
intervals = np.diff(np.array(self.time))
for thr in [0.001, 0.01, 0.1, 1, 10, 100]:
print(f"<{thr} = {np.mean(intervals < thr)}")
def importance_weight(self):
count = Counter(self.event)
percentage = [count[k] / len(self.event) for k in sorted(count.keys())]
for i, p in enumerate(percentage):
print(f"event{i} = {p * 100}%")
weight = [len(self.event) / count[k] for k in sorted(count.keys())]
return weight
def abs_error(pred, gold):
return np.mean(np.abs(pred - gold))
def clf_metric(pred, gold, n_class):
gold_count = Counter(gold)
pred_count = Counter(pred)
prec = recall = 0
pcnt = rcnt = 0
for i in range(n_class):
match_count = np.logical_and(pred == gold, pred == i).sum()
if gold_count[i] != 0:
prec += match_count / gold_count[i]
pcnt += 1
if pred_count[i] != 0:
recall += match_count / pred_count[i]
rcnt += 1
prec /= pcnt
recall /= rcnt
print(f"pcnt={pcnt}, rcnt={rcnt}")
f1 = 2 * prec * recall / (prec + recall)
return prec, recall, f1