forked from WenweiGu/ISOLATE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
187 lines (162 loc) · 4.95 KB
/
dataloader.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
import logging
import os
import pickle
import numpy as np
from collections import defaultdict
from torch.utils.data import DataLoader, Dataset
data_path_dict = {
"SMD": "./datasets/SMD",
"SMAP": "./datasets/SMAP",
"MSL": "./datasets/MSL",
}
def get_data_dim(dataset):
if "SMAP" in dataset:
return 25
elif "MSL" in dataset:
return 55
elif "SMD" in dataset:
return 38
elif "ASD" in dataset:
return 19
else:
raise ValueError("unknown dataset " + str(dataset))
def load_dataset(
data_root,
entities,
valid_ratio,
dim,
test_label_postfix,
test_postfix,
train_postfix,
nan_value=0,
nrows=None,
train_label_postfix=None
):
"""
use_dim: dimension used in multivariate timeseries
"""
logging.info("Loading data from {}".format(data_root))
data = defaultdict(dict)
total_train_len, total_valid_len, total_test_len = 0, 0, 0
for dataname in entities:
with open(
os.path.join(data_root, "{}_{}".format(dataname, train_postfix)), "rb"
) as f:
# train = pickle.load(f).reshape((-1, dim))[0:nrows, :]
train = pickle.load(f)[0:nrows, :]
if valid_ratio > 0:
split_idx = int(len(train) * valid_ratio)
train, valid = train[:-split_idx], train[-split_idx:]
data[dataname]["valid"] = np.nan_to_num(valid, nan=nan_value)
total_valid_len += len(valid)
data[dataname]["train"] = np.nan_to_num(train, nan=nan_value)
total_train_len += len(train)
with open(
os.path.join(data_root, "{}_{}".format(dataname, test_postfix)), "rb"
) as f:
test = pickle.load(f)[0:nrows, :]
data[dataname]["test"] = np.nan_to_num(test, nan=nan_value)
total_test_len += len(test)
with open(
os.path.join(data_root, "{}_{}".format(dataname, test_label_postfix)), "rb"
) as f:
data[dataname]["test_label"] = pickle.load(f).reshape(-1)[0:nrows]
if train_label_postfix is not None:
with open(
os.path.join(data_root, "{}_{}".format(dataname, train_label_postfix)), "rb"
) as f:
data[dataname]["train_label"] = pickle.load(f).reshape(-1)[0:nrows]
logging.info("Loading {} entities done.".format(len(entities)))
logging.info(
"Train/Valid/Test: {}/{}/{} lines.".format(
total_train_len, total_valid_len, total_test_len
)
)
return data
class sliding_window_dataset(Dataset):
def __init__(self, data, next_steps=0):
self.data = data
self.next_steps = next_steps
def __getitem__(self, index):
if self.next_steps == 0:
x = self.data[index]
return x
else:
x = self.data[index, 0: -self.next_steps]
y = self.data[index, -self.next_steps:]
return x, y
def __len__(self):
return len(self.data)
class sliding_window_positive(Dataset):
def __init__(self, data, label):
self.data = data
self.label = label
def __getitem__(self, index):
x = self.data[index]
y = self.label[index]
return x, y
def __len__(self):
return len(self.data)
def get_dataloaders(
train_data,
test_data,
valid_data=None,
next_steps=0,
batch_size=32,
shuffle=True,
num_workers=1,
):
train_loader = DataLoader(
sliding_window_dataset(train_data, next_steps),
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
)
test_loader = DataLoader(
sliding_window_dataset(test_data, next_steps),
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
)
if valid_data is not None:
valid_loader = DataLoader(
sliding_window_dataset(valid_data, next_steps),
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
)
else:
valid_loader = None
return train_loader, valid_loader, test_loader
def get_positive_dataloaders(
train_data,
train_label,
test_data,
valid_data=None,
next_steps=0,
batch_size=32,
shuffle=True,
num_workers=1,
):
train_loader = DataLoader(
sliding_window_positive(train_data, train_label),
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
)
test_loader = DataLoader(
sliding_window_dataset(test_data, next_steps),
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
)
if valid_data is not None:
valid_loader = DataLoader(
sliding_window_dataset(valid_data, next_steps),
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
)
else:
valid_loader = None
return train_loader, valid_loader, test_loader