-
Notifications
You must be signed in to change notification settings - Fork 58
/
dataset.py
70 lines (56 loc) · 1.95 KB
/
dataset.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
import torch
import torch.utils.data
import os
import logging
import numpy as np
import pandas as pd
class MTSFDataset(torch.utils.data.Dataset):
"""Multi-variate Time-Series Dataset for *.txt file
Returns:
[sample, label]
"""
def __init__(self,
window,
horizon,
data_name='wecar',
set_type='train', # 'train'/'validation'/'test'
data_dir='./data'):
assert type(set_type) == type('str')
self.window = window
self.horizon = horizon
self.data_dir = data_dir
self.set_type = set_type
file_path = os.path.join(data_dir, data_name, '{}_{}.txt'.format(data_name, set_type))
rawdata = np.loadtxt(open(file_path), delimiter=',')
self.len, self.var_num = rawdata.shape
self.sample_num = max(self.len - self.window - self.horizon + 1, 0)
self.samples, self.labels = self.__getsamples(rawdata)
def __getsamples(self, data):
X = torch.zeros((self.sample_num, self.window, self.var_num))
Y = torch.zeros((self.sample_num, 1, self.var_num))
for i in range(self.sample_num):
start = i
end = i + self.window
X[i, :, :] = torch.from_numpy(data[start:end, :])
Y[i, :, :] = torch.from_numpy(data[end+self.horizon-1, :])
return (X, Y)
def __len__(self):
return self.sample_num
def __getitem__(self, idx):
sample = [self.samples[idx, :, :], self.labels[idx, :, :]]
return sample
# test
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
logging.debug('Test: data from .txt file')
dataset = MTSFDataset(
window=16,
horizon=3,
data_name='wecar',
set_type='train',
data_dir='./data'
)
i = 0
sample = dataset[i]
logging.debug('Sample #{}, label: {}'.format(i, sample[1]))
logging.debug('data: {}'.format(sample[0]))