-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_dataset.py
292 lines (271 loc) · 10.1 KB
/
custom_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
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
292
import pytorch_lightning as pl
import torch
from torch.utils.data import DataLoader, Dataset
import pandas as pd
class TFTDataset(Dataset):
def __init__(
self,
data,
static_real_cols,
static_cat_cols,
historical_real_cols,
historical_cat_cols,
known_real_cols,
known_cat_cols,
target,
window_size,
group_ids,
time_idx,
mixed_only,
mixed_idx=None,
time_gap=1, # time_gap=1 for 1-step prediction, time_gap=2 for 2-step ahead prediction, etc.
):
super().__init__()
self.static_real_cols = static_real_cols
self.static_cat_cols = static_cat_cols
self.historical_real_cols = historical_real_cols
self.historical_cat_cols = historical_cat_cols
self.known_real_cols = known_real_cols
self.known_cat_cols = known_cat_cols
self.time_gap = time_gap
if self.static_real_cols is not None:
self.static_real_data = torch.tensor(
data[static_real_cols].values, dtype=torch.float
)
if self.static_cat_cols is not None:
self.static_cat_data = torch.tensor(
data[static_cat_cols].values, dtype=torch.long
)
if self.historical_real_cols is not None:
self.historical_real_data = torch.tensor(
data[historical_real_cols].values, dtype=torch.float
)
if self.historical_cat_cols is not None:
self.historical_cat_data = torch.tensor(
data[historical_cat_cols].values, dtype=torch.long
)
if self.known_real_cols is not None:
self.known_real_data = torch.tensor(
data[known_real_cols].values, dtype=torch.float
)
if self.known_cat_cols is not None:
self.known_cat_data = torch.tensor(
data[known_cat_cols].values, dtype=torch.long
)
self.target = torch.tensor(data[target].values, dtype=torch.float)
self.group_ids = torch.tensor(data[group_ids].values, dtype=torch.float)
self.time_idx = torch.tensor(data[time_idx].values, dtype=torch.float)
self.window_size = window_size
self.mixed_only = mixed_only
self.mixed_idx = mixed_idx
self.indices = self.prepare_indices()
def prepare_indices(self):
indices = []
unique_groups = torch.unique(self.group_ids)
for group in unique_groups:
group_idx = torch.where(self.group_ids == group)[0]
# Sort the indices in this group by time_idx
time_idx_group = self.time_idx[group_idx]
sorted_indices = group_idx[torch.argsort(time_idx_group)]
# Create windows from the sorted indices
for i in range(len(sorted_indices) - self.window_size - self.time_gap):
if (self.mixed_only) and (
self.target[
sorted_indices[i + self.window_size + self.time_gap],
self.mixed_idx,
]
.isnan()
.item()
== True
):
continue
else:
indices.append(
sorted_indices[i : i + self.window_size + self.time_gap]
)
return indices
def __len__(self):
return len(self.indices)
def __getitem__(self, index):
sample = {}
idx = self.indices[index][: -self.time_gap]
next_idx = self.indices[index][-1]
# sample["static_real"] = (
# self._get_static_real_sample(idx)
# if self.static_real_cols is not None
# else None
# )
if self.static_real_cols is not None:
sample["static_real"] = self._get_static_real_sample(idx)
if self.static_cat_cols is not None:
sample["static_cat"] = self._get_static_cat_sample(idx)
if self.historical_real_cols is not None:
sample["historical_real"] = self._get_historical_real_sample(idx)
if self.historical_cat_cols is not None:
sample["historical_cat"] = self._get_historical_cat_sample(idx)
if self.known_real_cols is not None:
sample["known_real"] = self._get_known_real_sample(next_idx)
if self.known_cat_cols is not None:
sample["known_cat"] = self._get_known_cat_sample(next_idx)
sample["time_idx"] = idx
sample["group_ids"] = self.group_ids[idx]
target = self.target[next_idx]
assert (
idx[-1] == next_idx - self.time_gap
), "Time gap is not correct! Are you sure the index of your dataset starts from 0 and is continuous?"
return sample, target
def _get_static_real_sample(self, idx):
return {
name: self.static_real_data[idx, i]
for i, name in enumerate(self.static_real_cols)
}
def _get_static_cat_sample(self, idx):
return {
name: self.static_cat_data[idx, i]
for i, name in enumerate(self.static_cat_cols)
}
def _get_historical_real_sample(self, idx):
return {
name: self.historical_real_data[idx, i]
for i, name in enumerate(self.historical_real_cols)
}
def _get_historical_cat_sample(self, idx):
return {
name: self.historical_cat_data[idx, i]
for i, name in enumerate(self.historical_cat_cols)
}
def _get_known_real_sample(self, future_idx):
return {
name: self.known_real_data[future_idx, i]
for i, name in enumerate(self.known_real_cols)
}
def _get_known_cat_sample(self, future_idx):
return {
name: self.known_cat_data[future_idx, i]
for i, name in enumerate(self.known_cat_cols)
}
class TimeSeriesDataLoader(pl.LightningDataModule):
def __init__(
self,
train,
val,
static_real_cols,
static_cat_cols,
historical_real_cols,
historical_cat_cols,
known_real_cols,
known_cat_cols,
target,
window_size,
group_ids,
batch_size,
time_idx,
test=None,
mixed_only=False,
time_gap=1,
mixed_idx=None,
):
super().__init__()
self.train = train
self.val = val
self.test = test
self.static_real_cols = static_real_cols
self.static_cat_cols = static_cat_cols
self.historical_real_cols = historical_real_cols
self.historical_cat_cols = historical_cat_cols
self.known_real_cols = known_real_cols
self.known_cat_cols = known_cat_cols
self.target = target
self.static_cat_mapper = self.get_cat_mappings(self.static_cat_cols)
self.static_cat_sizes = self.get_cat_sizes(self.static_cat_mapper)
self.historical_cat_mapper = self.get_cat_mappings(self.historical_cat_cols)
self.historical_cat_sizes = self.get_cat_sizes(self.historical_cat_mapper)
self.window_size = window_size
self.group_ids = group_ids
self.batch_size = batch_size
self.time_idx = time_idx
self.mixed_only = mixed_only
self.time_gap = time_gap
self.train_dataset = TFTDataset(
self.train,
self.static_real_cols,
self.static_cat_cols,
self.historical_real_cols,
self.historical_cat_cols,
self.known_real_cols,
self.known_cat_cols,
self.target,
self.window_size,
self.group_ids,
self.time_idx,
self.mixed_only,
mixed_idx=None if self.mixed_only is False else mixed_idx,
time_gap=self.time_gap,
)
self.val_dataset = TFTDataset(
self.val,
self.static_real_cols,
self.static_cat_cols,
self.historical_real_cols,
self.historical_cat_cols,
self.known_real_cols,
self.known_cat_cols,
self.target,
self.window_size,
self.group_ids,
self.time_idx,
self.mixed_only,
mixed_idx=None if self.mixed_only is False else mixed_idx,
time_gap=self.time_gap,
)
if self.test is not None:
self.test_dataset = TFTDataset(
self.test,
self.static_real_cols,
self.static_cat_cols,
self.historical_real_cols,
self.historical_cat_cols,
self.known_real_cols,
self.known_cat_cols,
self.target,
self.window_size,
self.group_ids,
self.time_idx,
self.mixed_only,
mixed_idx=None if self.mixed_only is False else mixed_idx,
time_gap=self.time_gap,
)
def train_dataloader(self):
return DataLoader(self.train_dataset, batch_size=self.batch_size, shuffle=True)
def val_dataloader(self):
return DataLoader(
self.val_dataset,
batch_size=self.batch_size * 4,
shuffle=False,
)
def test_dataloader(self):
return DataLoader(
self.test_dataset,
batch_size=self.batch_size * 4,
shuffle=False,
)
def get_cat_mappings(self, cat_cols):
cat_mapper = {}
for col in cat_cols:
self.train[col], cat_mapper[col] = self._map_colname(self.train, col)
self.val[col].replace(
{v: k for k, v in cat_mapper[col].items()}, inplace=True
)
self.test[col].replace(
{v: k for k, v in cat_mapper[col].items()}, inplace=True
)
return cat_mapper
def get_cat_sizes(self, cat_mapper):
cat_sizes = {}
for k in cat_mapper.keys():
cat_sizes[k] = len(cat_mapper[k])
return cat_sizes
def _map_colname(self, data, colname):
labels, unique = pd.factorize(data[colname])
mapper = {k: v for k, v in enumerate(unique)}
return labels, mapper