-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
291 lines (223 loc) · 11.1 KB
/
datasets.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import numpy as np
import torch
from dataclasses import dataclass
from typing import Optional, Tuple, Union
import numpy.typing
NDArray = numpy.typing.NDArray[np.floating]
def train_test_split(time_series: NDArray,
train_test_ratio: float = 0.7,
shift: Union[int, float] = 0) -> Tuple[NDArray, NDArray]:
assert time_series.ndim == 2, "Time series expected, each datapoint is a 1D array"
assert 0 < train_test_ratio < 1, "train_test_ratio should be within (0, 1) interval"
if isinstance(shift, float):
assert .0 <= shift < 1., "if 'shift' argument is a float, it should be from [0,1)"
shift = int(len(time_series) * shift)
split_index = int(len(time_series) * train_test_ratio)
time_series = np.roll(time_series, shift=shift)
train, test = time_series[:split_index], time_series[split_index:]
assert len(train) > 0, "Bad train-test split"
assert len(test) > 0, "Bad train-test split"
return train, test
def chop_time_series_into_chunks(time_series: NDArray,
chunk_len: int,
take_each_nth_chunk: int,
reverse: bool = False) -> NDArray:
assert time_series.ndim == 2, "Time series expected, each datapoint is a 1D array"
assert len(time_series) >= chunk_len, f"chunk_len={chunk_len} is too large"
if reverse:
time_series = np.flip(time_series, axis=0)
chunks = np.array([time_series[i:i+chunk_len]
for i in range(0, len(time_series) - chunk_len + 1, take_each_nth_chunk)])
chunks = chunks.copy() # Being extra cautious: prevent edit-by-reference error.
return chunks
def split_chunks_into_windows_and_targets(chunks: NDArray,
target_len: int = 1) -> Tuple[NDArray, NDArray]:
assert chunks.ndim == 3, "Shape should be (n_chunks, chunk_len, datapoint_dim)"
chunk_len: int = chunks.shape[1]
assert 0 < target_len < chunk_len, f"target_len={target_len} is too large or non-positive"
window_len: int = chunk_len - target_len
windows: NDArray = chunks[:, :window_len, :]
targets: NDArray = chunks[:, window_len:, :]
# Being extra cautious: prevent edit-by-reference error.
windows = windows.copy()
targets = targets.copy()
return windows, targets
def reverse_windows_targets(windows: NDArray, targets: NDArray) -> Tuple[NDArray, NDArray]:
err_str = f"incompatible shapes: {windows.shape}, {targets.shape}"
assert windows.ndim == targets.ndim == 3, err_str
assert windows.shape[0] == targets.shape[0], err_str
assert windows.shape[-1] == targets.shape[-1], err_str
target_len = targets.shape[-2]
chunks = np.hstack((windows, targets))
chunks = np.flip(chunks, axis=1)
return split_chunks_into_windows_and_targets(chunks, target_len=target_len)
class TimeSeriesDataset(torch.utils.data.Dataset):
def __init__(self, windows: NDArray, targets: NDArray) -> None:
assert len(windows) == len(targets) != 0
assert windows.ndim == 3
assert targets.ndim == 3
assert windows.shape[2] == targets.shape[2]
super().__init__()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.windows = torch.from_numpy(windows).to(device=device, dtype=torch.float32)
self.targets = torch.from_numpy(targets).to(device=device, dtype=torch.float32)
self.n_points: int = len(windows)
def __getitem__(self, index: int) -> Tuple:
return (self.windows[index], self.targets[index])
def __len__(self) -> int:
return self.n_points
def time_series_to_dataset(ts: NDArray,
window_len: int,
take_each_nth_chunk: int,
target_len: int = 1,
reverse: bool = False) -> TimeSeriesDataset:
chunks = chop_time_series_into_chunks(time_series=ts,
chunk_len=window_len + target_len,
take_each_nth_chunk=take_each_nth_chunk,
reverse=reverse)
windows, targets = split_chunks_into_windows_and_targets(chunks, target_len=target_len)
return TimeSeriesDataset(windows, targets)
@dataclass
class DataHolderOneDirection:
train_dataset: TimeSeriesDataset
test_dataset: TimeSeriesDataset
train_loader: torch.utils.data.DataLoader
@dataclass
class AllDataHolder:
forward: DataHolderOneDirection
backward: DataHolderOneDirection
test_ts: Optional[NDArray] = None
train_ts: Optional[NDArray] = None
def prepare_time_series_for_learning(train_ts: NDArray,
test_ts: NDArray,
window_len: int = 40,
target_len: int = 1,
loader_batch_size: int = 20,
take_each_nth_chunk: Optional[int] = None) -> AllDataHolder:
assert window_len >= 2
assert target_len >= 1
assert loader_batch_size >= 1
if take_each_nth_chunk is None:
take_each_nth_chunk = int((window_len + target_len) * 0.2)
# Forward
train_dataset = time_series_to_dataset(train_ts, window_len=window_len, target_len=target_len,
take_each_nth_chunk=take_each_nth_chunk)
test_dataset = time_series_to_dataset(test_ts, window_len=window_len, target_len=target_len,
take_each_nth_chunk=take_each_nth_chunk)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=loader_batch_size)
forward = DataHolderOneDirection(train_dataset, test_dataset, train_loader)
# Backward
train_dataset = time_series_to_dataset(train_ts, window_len=window_len, target_len=target_len,
reverse=True, take_each_nth_chunk=take_each_nth_chunk)
test_dataset = time_series_to_dataset(test_ts, window_len=window_len, target_len=target_len,
reverse=True, take_each_nth_chunk=take_each_nth_chunk)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=loader_batch_size)
backward = DataHolderOneDirection(train_dataset, test_dataset, train_loader)
return AllDataHolder(forward, backward, test_ts=test_ts, train_ts=train_ts)
def test_train_test_split():
def compare(actual: Tuple[NDArray, NDArray],
expected: Tuple[NDArray, NDArray]) -> None:
assert len(actual) == len(expected) == 2
assert np.array_equal(actual[0], expected[0]), f"{actual} \n\t!=\n{expected}"
assert np.array_equal(actual[1], expected[1]), f"{actual} \n\t!=\n{expected}"
simple_data = np.array([[1], [2], [3], [4]])
compare(
train_test_split(simple_data, train_test_ratio=0.5),
(np.array([[1], [2]]), np.array([[3], [4]]))
)
compare(
train_test_split(simple_data, train_test_ratio=0.5, shift=1),
(np.array([[4], [1]]), np.array([[2], [3]]))
)
print("Tests for train_test_split passed successfully")
def test_chop_time_series_into_chunks() -> None:
def compare(actual: NDArray, expected: NDArray) -> None:
assert np.array_equal(actual, expected), f"{actual} \n\t!=\n{expected}"
simple_data = np.array([[1], [2], [3], [4]])
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]])
compare(
chop_time_series_into_chunks(simple_data, chunk_len=1, take_each_nth_chunk=1),
np.array([[[1]], [[2]], [[3]], [[4]]])
)
compare(
chop_time_series_into_chunks(simple_data, chunk_len=1, take_each_nth_chunk=3),
np.array([[[1]], [[4]]])
)
compare(
chop_time_series_into_chunks(simple_data, chunk_len=2,
take_each_nth_chunk=1, reverse=True),
np.array([[[4], [3]], [[3], [2]], [[2], [1]]])
)
compare(
chop_time_series_into_chunks(simple_data, chunk_len=2, take_each_nth_chunk=1),
np.array([[[1], [2]], [[2], [3]], [[3], [4]]])
)
compare(
chop_time_series_into_chunks(simple_data, chunk_len=3, take_each_nth_chunk=1),
np.array([[[1], [2], [3]], [[2], [3], [4]]])
)
compare(
chop_time_series_into_chunks(data, chunk_len=1, take_each_nth_chunk=1),
np.array([[[1, 2, 3]], [[4, 5, 6]], [[7, 8, 9]], [[10, 11, 12]], [[13, 14, 15]]])
)
compare(
chop_time_series_into_chunks(data, chunk_len=2, take_each_nth_chunk=2),
np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
)
compare(
chop_time_series_into_chunks(data, chunk_len=2, take_each_nth_chunk=3),
np.array([[[1, 2, 3], [4, 5, 6]], [[10, 11, 12], [13, 14, 15]]])
)
print("Tests for chop_time_series_into_chunks passed successfully")
def test_split_chunks_into_windows_and_targets() -> None:
def compare(actual: Tuple[NDArray, NDArray],
expected: Tuple[NDArray, NDArray]) -> None:
assert len(actual) == len(expected) == 2
assert np.array_equal(actual[0], expected[0]), f"{actual} \n\t!=\n{expected}"
assert np.array_equal(actual[1], expected[1]), f"{actual} \n\t!=\n{expected}"
simple_data = np.array([[[1], [2], [3], [4]],
[[5], [6], [7], [8]],
[[9], [10], [11], [12]]])
compare(
split_chunks_into_windows_and_targets(simple_data, target_len=1),
(
np.array([[[1], [2], [3]],
[[5], [6], [7]],
[[9], [10], [11]]]),
np.array([[[4]],
[[8]],
[[12]]])
)
)
compare(
split_chunks_into_windows_and_targets(simple_data, target_len=2),
(
np.array([[[1], [2]],
[[5], [6]],
[[9], [10]]]),
np.array([[[3], [4]],
[[7], [8]],
[[11], [12]]])
)
)
print("Tests for split_chunks_into_windows_and_targets passed successfully")
def test_reverse_windows_targets() -> None:
def compare(actual: Tuple[NDArray, NDArray],
expected: Tuple[NDArray, NDArray]) -> None:
assert len(actual) == len(expected) == 2
assert np.array_equal(actual[0], expected[0]), f"{actual} \n\t!=\n{expected}"
assert np.array_equal(actual[1], expected[1]), f"{actual} \n\t!=\n{expected}"
windows = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
targets = np.array([[[13, 14]], [[15, 16]], [[17, 18]]])
windows_expected = np.array([[[13, 14], [3, 4]], [[15, 16], [7, 8]], [[17, 18], [11, 12]]])
targets_expected = np.array([[[1, 2]], [[5, 6]], [[9, 10]]])
compare(
reverse_windows_targets(windows, targets),
(windows_expected, targets_expected)
)
print("Tests for reverse_windows_targets passed successfully")
if __name__ == "__main__":
test_train_test_split()
test_chop_time_series_into_chunks()
test_split_chunks_into_windows_and_targets()
test_reverse_windows_targets()