-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
432 lines (379 loc) · 15.1 KB
/
main.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import sys
from typing import Tuple, Optional, Union
import numpy as np
import pandas as pd
from darts import TimeSeries
from darts.models import TFTModel
from darts.utils.data import MixedCovariatesSequentialDataset, MixedCovariatesInferenceDataset
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import EarlyStopping
from sklearn.metrics import mean_squared_error as mse
class CustomDataset:
"""
A custom dataset class that contains common attributes and methods for training and inference datasets.
"""
def __init__(self):
# Initialize all attributes to None
self.labels_timestamps = None
self.labels = None
self.timestamps = None
self.data = None
self.features = None
self.target = None
self.data_windows = None
def check_unique_timestamps(self):
"""
Check the uniqueness of timestamps in the data windows.
If timestamps are not unique, print the count of each timestamp and raise a ValueError.
"""
timestamps_unique = np.unique(self.timestamps, axis=0)
if timestamps_unique.shape != self.timestamps.shape:
for timestamp, count in zip(
*np.unique(self.timestamps, return_counts=True, axis=0)
):
if count > 1:
print(f"{int(timestamp)}: {count}.")
raise ValueError("Timestamps in the data windows are not unique.")
def init_common_attributes(self, **kwargs):
"""
Initialize common attributes for the dataset.
"""
self.data_windows = kwargs["data_windows"]
self.target = kwargs["target_feature"]
self.features = len(self.data_windows["data"][0][0])
self.data = self.data_windows["data"]
self.timestamps = self.data_windows["timestamps"]
self.labels = self.data_windows["labels"]
self.labels_timestamps = self.data_windows["labels_timestamps"]
def common_init(self):
"""
This method should be implemented in subclasses.
"""
raise NotImplementedError
class CustomTrainingDataset(CustomDataset):
def __init__(self):
super().__init__()
self.output_chunk_length = None
self.input_chunk_length = None
def common_init(self, **kwargs):
"""
Initialize common attributes for the training dataset.
"""
self.input_chunk_length = kwargs["input_chunk_length"]
self.output_chunk_length = kwargs["output_chunk_length"]
self.init_common_attributes(**kwargs)
self.check_unique_timestamps()
print(f"Initialized dataset with {np.unique(self.timestamps).shape[0]} timestamps.", flush=True)
class CustomInferenceDataset(CustomDataset):
def __init__(self):
super().__init__()
def _common_init(self, **kwargs):
"""
Initialize common attributes for the inference dataset.
"""
self.input_time_series = kwargs["target_series"]
target_series = kwargs["target_series"]
target_feature = kwargs["target_feature"]
if isinstance(target_feature, str):
print(
"Target feature passed as string. Getting index of the feature from the TimeSeries object.",
file=sys.stderr,
)
target = target_series.features.index(target_feature)
else:
target = target_feature
kwargs["target_feature"] = target
self.init_common_attributes(
**kwargs
)
self.check_unique_timestamps()
print(f"Initialized dataset with {np.unique(self.timestamps).shape[0]} timestamps.", flush=True)
class CustomMixedCovariatesSequentialDataset(CustomTrainingDataset, MixedCovariatesSequentialDataset):
def __init__(
self,
target_series: TimeSeries,
covariates: TimeSeries,
target_feature: int,
input_chunk_length: int,
output_chunk_length: int,
data_windows: dict,
):
CustomTrainingDataset.__init__(self)
MixedCovariatesSequentialDataset.__init__(
self,
target_series=target_series,
past_covariates=covariates,
input_chunk_length=input_chunk_length,
output_chunk_length=output_chunk_length,
)
self.common_init(
target_series=target_series,
target_feature=target_feature,
data_windows=data_windows,
input_chunk_length=input_chunk_length,
output_chunk_length=output_chunk_length
)
def __len__(self):
return len(self.data_windows["data"])
def __getitem__(
self, item
) -> Tuple[
np.ndarray,
Optional[np.ndarray],
Optional[np.ndarray],
Optional[np.ndarray],
Optional[np.ndarray],
np.ndarray,
]:
past_target = self.data[item][:, [self.target]]
past_covariate = self.data[item][
:, [i for i in range(self.features) if i != self.target]
]
historic_future_covariate = None
future_covariate = None
static_covariate = None
future_target = self.labels[item].reshape(1, 1)
# copied these names from MixedCovariatesSequentialDataset as a reference
return (
past_target,
past_covariate,
historic_future_covariate,
future_covariate,
static_covariate,
future_target,
)
class CustomMixedCovariatesInferenceDataset(CustomInferenceDataset, MixedCovariatesInferenceDataset):
def __init__(
self,
target_series: TimeSeries,
covariates: TimeSeries,
target_feature: int,
input_chunk_length: int,
output_chunk_length: int,
data_windows: dict,
):
CustomInferenceDataset.__init__(self)
MixedCovariatesInferenceDataset.__init__(
self,
target_series=target_series,
past_covariates=covariates,
input_chunk_length=input_chunk_length,
output_chunk_length=output_chunk_length,
)
self._common_init(
target_series=target_series,
target_feature=target_feature,
data_windows=data_windows,
input_chunk_length=input_chunk_length,
output_chunk_length=output_chunk_length
)
def __len__(self):
return len(self.data_windows["data"])
def __getitem__(
self, item
) -> Tuple[
np.ndarray,
Optional[np.ndarray],
Optional[np.ndarray],
Optional[np.ndarray],
Optional[np.ndarray],
Optional[np.ndarray],
TimeSeries,
Union[pd.Timestamp, int],
]:
# extract only target feature
# darts sanity checking wants the predict sample to be the same shape as the train sample
past_target = self.data[item][:, [self.target]]
# extract all features except target
past_covariate = self.data[item][
:, [i for i in range(self.features) if i != self.target]
]
historic_future_covariate = None
future_covariate = None
future_past_covariate = None
static_covariate = None
ts_target = self.input_time_series
if isinstance(ts_target.pd_dataframe().index[0], pd.Timestamp):
pred_point = pd.to_datetime(self.labels_timestamps[item][0])
else:
pred_point = self.labels_timestamps[item].astype(int)[0]
return (
past_target,
past_covariate,
historic_future_covariate,
future_covariate,
future_past_covariate,
static_covariate,
ts_target,
pred_point,
)
def generate_dataset(data: pd.DataFrame) -> dict:
"""
This function generates a dataset for training, validation and testing.
The dataset is a dictionary containing data, timestamps, labels and labels_timestamps.
Args:
data (pd.DataFrame): The input data as a pandas DataFrame.
Returns:
dict: The generated dataset.
"""
... # data is used to generate the sliding window datasets
return {
"data": np.array(
[
[[1, 6, 11], [2, 7, 12], [3, 8, 13], [4, 9, 14], [5, 10, 15]],
[[2, 7, 12], [3, 8, 13], [4, 9, 14], [5, 10, 15], [6, 11, 16]],
[[3, 8, 13], [4, 9, 14], [5, 10, 15], [6, 11, 16], [7, 12, 17]],
[[4, 9, 14], [5, 10, 15], [6, 11, 16], [7, 12, 17], [8, 13, 18]],
[[5, 10, 15], [6, 11, 16], [7, 12, 17], [8, 13, 18], [9, 14, 19]],
[[6, 11, 16], [7, 12, 17], [8, 13, 18], [9, 14, 19], [10, 15, 20]],
[[7, 12, 17], [8, 13, 18], [9, 14, 19], [10, 15, 20], [11, 16, 21]],
[[8, 13, 18], [9, 14, 19], [10, 15, 20], [11, 16, 21], [12, 17, 22]],
[[9, 14, 19], [10, 15, 20], [11, 16, 21], [12, 17, 22], [13, 18, 23]],
[[10, 15, 20], [11, 16, 21], [12, 17, 22], [13, 18, 23], [14, 19, 24]],
[[11, 16, 21], [12, 17, 22], [13, 18, 23], [14, 19, 24], [15, 20, 25]],
[[12, 17, 22], [13, 18, 23], [14, 19, 24], [15, 20, 25], [16, 21, 26]],
[[13, 18, 23], [14, 19, 24], [15, 20, 25], [16, 21, 26], [17, 22, 27]],
[[14, 19, 24], [15, 20, 25], [16, 21, 26], [17, 22, 27], [18, 23, 28]],
[[15, 20, 25], [16, 21, 26], [17, 22, 27], [18, 23, 28], [19, 24, 29]],
],
dtype=np.float32,
),
"timestamps": np.array(
[
[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8],
[5, 6, 7, 8, 9],
[6, 7, 8, 9, 10],
[7, 8, 9, 10, 11],
[8, 9, 10, 11, 12],
[9, 10, 11, 12, 13],
[10, 11, 12, 13, 14],
[11, 12, 13, 14, 15],
[12, 13, 14, 15, 16],
[13, 14, 15, 16, 17],
[14, 15, 16, 17, 18],
],
dtype=np.float32,
),
"labels": np.array(
[
[6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20]
], dtype=np.float32
),
"labels_timestamps": np.array(
[
[5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19]
], dtype=np.float32
),
}
def main():
"""
The main function of the script. It prepares the data, trains the model, and makes predictions.
"""
input_chunk_length = 5
output_chunk_length = 1
model = TFTModel(input_chunk_length=input_chunk_length, output_chunk_length=output_chunk_length)
target_feature = 0
target_feature_name = 'feature1'
data = pd.DataFrame({
"feature1": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
"feature2": [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
"feature3": [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
}, index=range(20))
# split train, val and test data. Not necessary for this example as we are using a fixed dataset
# train_data, val_test_data = target_series.split_after(0.6) # up to [6, 11, 16], 6 elements
# val_data, test_data = val_test_data.split_after(0.5) # up to [8, 13, 18] and [10, 15, 20], 2 elements each
data_windows = generate_dataset(data)
train_data_windows = {
"data": data_windows["data"][:6], "timestamps": data_windows["timestamps"][:6],
"labels": data_windows["labels"][:6], "labels_timestamps": data_windows["labels_timestamps"][:6]
}
val_data_windows = {
"data": data_windows["data"][6:8], "timestamps": data_windows["timestamps"][6:8],
"labels": data_windows["labels"][6:8], "labels_timestamps": data_windows["labels_timestamps"][6:8]
}
test_data_windows = {
"data": data_windows["data"][8:], "timestamps": data_windows["timestamps"][8:],
"labels": data_windows["labels"][8:], "labels_timestamps": data_windows["labels_timestamps"][8:]
}
train_target_series = TimeSeries.from_dataframe(
data.iloc[:6][[target_feature_name]]
)
val_target_series = TimeSeries.from_dataframe(
data.iloc[6:8][[target_feature_name]]
)
test_target_series = TimeSeries.from_dataframe(
data.iloc[8:][[target_feature_name]]
)
train_covariates = TimeSeries.from_dataframe(
data.iloc[:6][['feature2', 'feature3']]
)
val_covariates = TimeSeries.from_dataframe(
data.iloc[6:8][['feature2', 'feature3']]
)
test_covariates = TimeSeries.from_dataframe(
data.iloc[8:][['feature2', 'feature3']]
)
train_dataset = CustomMixedCovariatesSequentialDataset(
target_series=train_target_series,
covariates=train_covariates,
target_feature=target_feature,
input_chunk_length=input_chunk_length,
output_chunk_length=output_chunk_length,
data_windows=train_data_windows,
)
val_dataset = CustomMixedCovariatesSequentialDataset(
target_series=val_target_series,
covariates=val_covariates,
target_feature=target_feature,
input_chunk_length=input_chunk_length,
output_chunk_length=output_chunk_length,
data_windows=val_data_windows,
)
early_stopping = EarlyStopping(
monitor="val_loss",
patience=10,
min_delta=0.05,
verbose=True,
mode="min",
)
# Can't use EarlyStopping with TFTModel as it raises the following exception:
# RuntimeError: Early stopping conditioned on metric `val_loss` which is not available.
# Pass in or modify your `EarlyStopping` callback to use any of the following: ``
trainer = Trainer(
accelerator='auto',
devices=1,
# callbacks=[early_stopping],
max_epochs=100,
log_every_n_steps=1 # used to print the loss at each step, but it is not printed as the epochs are not run.
)
model.fit_from_dataset(
train_dataset=train_dataset,
val_dataset=val_dataset,
trainer=trainer,
verbose=True,
)
inference_dataset = CustomMixedCovariatesInferenceDataset(
target_series=test_target_series,
covariates=test_covariates,
target_feature=target_feature,
input_chunk_length=input_chunk_length,
output_chunk_length=output_chunk_length,
data_windows=test_data_windows,
)
tester = Trainer(accelerator='auto', devices=1)
forecast_horizon = 1
y_pred: list[TimeSeries] = model.predict_from_dataset(
n=forecast_horizon,
input_series_dataset=inference_dataset,
trainer=tester,
verbose=True,
)
y_pred = np.concatenate([[ts.values()[0]] for ts in y_pred])
y_true = test_data_windows["labels"].flatten()
print("MSE:", mse(y_true, y_pred))
if __name__ == '__main__':
main()