-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathtime_series_dataset.py
More file actions
1367 lines (1169 loc) · 64.1 KB
/
Copy pathtime_series_dataset.py
File metadata and controls
1367 lines (1169 loc) · 64.1 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bisect
import copy
import os
import uuid
import warnings
from typing import Any, Dict, List, Optional, Tuple, Union, cast
from gluonts.time_feature import Constant as ConstantTransform
from gluonts.time_feature import TimeFeature, time_features_from_frequency_str
from gluonts.time_feature.lag import get_lags_for_frequency
import numpy as np
import pandas as pd
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
from scipy.sparse import issparse
import torch
from torch.utils.data.dataset import ConcatDataset, Dataset
import torchvision.transforms
from autoPyTorch.constants import (
CLASSIFICATION_OUTPUTS,
MAX_WINDOW_SIZE_BASE,
SEASONALITY_MAP,
STRING_TO_OUTPUT_TYPES,
TASK_TYPES_TO_STRING,
TIMESERIES_FORECASTING
)
from autoPyTorch.data.time_series_forecasting_validator import TimeSeriesForecastingInputValidator
from autoPyTorch.datasets.base_dataset import BaseDataset, type_of_target
from autoPyTorch.datasets.resampling_strategy import (
CrossValFuncs,
CrossValTypes,
DEFAULT_RESAMPLING_PARAMETERS,
HoldOutFuncs,
HoldoutValTypes,
NoResamplingStrategyTypes,
ResamplingStrategies
)
from autoPyTorch.pipeline.components.training.metrics.metrics import compute_mase_coefficient
from autoPyTorch.utils.common import FitRequirement
def extract_feature_index(feature_shapes: Dict[str, int],
feature_names: Tuple[str],
queried_features: Union[Tuple[Union[str, int]], Tuple[()]]) -> Tuple[int]:
"""
extract the index of a set of queried_features from the extracted feature_shapes
Args:
feature_shapes (dict):
feature_shapes recoding the shape of each features
feature_names (List[str]):
names of the features
queried_features (Tuple[str]):
names of the features that we expect their index
Returns:
feature_index (Tuple[int]):
indices of the corresponding features
"""
df_range = pd.DataFrame(feature_shapes, columns=feature_names, index=[0])
df_range_end = df_range.cumsum(axis=1)
df_range = pd.concat([df_range_end - df_range, df_range_end])
value_ranges = df_range[list(queried_features)].T.values
feature_index: List[int] = sum([list(range(*value_r)) for value_r in value_ranges], [])
feature_index.sort()
return tuple(feature_index) # type: ignore[return-value]
def compute_time_features(start_time: pd.DatetimeIndex,
date_period_length: int,
time_feature_length: int,
freq: str,
time_feature_transforms: List[TimeFeature]) -> np.ndarray:
date_info = pd.date_range(start=start_time,
periods=date_period_length,
freq=freq)[-time_feature_length:]
try:
time_features = np.vstack(
[transform(date_info) for transform in time_feature_transforms]
).T
except OutOfBoundsDatetime:
# This is only a temporal solution TODO consider how to solve this!
time_features = np.zeros([time_feature_length, len(time_feature_transforms)])
return time_features
class TimeSeriesSequence(Dataset):
"""
A dataset representing a time series sequence. It returns all the previous observations once it is asked for an item
Args:
X (Optional[np.ndarray]):
past features
Y (np.ndarray):
past targets
start_time (Optional[pd.DatetimeIndex]):
times of the first timestep of the series
freq (str):
frequency that the data is sampled
time_feature_transform (List[TimeFeature]):
available time features applied to the series
X_test (Optional[np.ndarray]):
known future features
Y_test (Optional[np.ndarray]):
future targets
train_transforms (Optional[torchvision.transforms.Compose]):
training transforms, used to transform training features
val_transforms (Optional[torchvision.transforms.Compose]):
validation transforms, used to transform training features
n_prediction_steps (int):
how many steps need to be predicted in advance
known_future_features_index (int):
indices of the known future index
compute_mase_coefficient_value (bool):
if the mase coefficient for this series is pre-computed
time_features (Optional[np.ndarray]):
pre-computed time features
is_test_set (bool):
if this dataset is test sets. Test sequence will simply make X_test and Y_test as future features and
future targets
"""
_is_test_set = False
is_pre_processed = False
def __init__(self,
X: Optional[np.ndarray],
Y: np.ndarray,
start_time: Optional[pd.DatetimeIndex] = None,
freq: str = '1Y',
time_feature_transform: List[TimeFeature] = [ConstantTransform()],
X_test: Optional[np.ndarray] = None,
Y_test: Optional[np.ndarray] = None,
train_transforms: Optional[torchvision.transforms.Compose] = None,
val_transforms: Optional[torchvision.transforms.Compose] = None,
n_prediction_steps: int = 1,
sp: int = 1,
known_future_features_index: Optional[Tuple[int]] = None,
compute_mase_coefficient_value: bool = True,
time_features: Optional[np.ndarray] = None,
is_test_set: bool = False,
) -> None:
self.n_prediction_steps = n_prediction_steps
if X is not None and X.ndim == 1:
X = X[:, np.newaxis]
self.X = X
self.Y = Y
self.observed_target = ~np.isnan(self.Y)
if start_time is None:
start_time = pd.Timestamp('1900-01-01')
self.start_time = start_time
self.X_val = None
self.Y_val = None
if X_test is not None and X_test.ndim == 1:
X_test = X_test[:, np.newaxis]
self.X_test = X_test
self.Y_test = Y_test
self.time_feature_transform = time_feature_transform
self.freq = freq
# We also need to be able to transform the data, be it for pre-processing
# or for augmentation
self.train_transform = train_transforms
self.val_transform = val_transforms
self.sp = sp
if compute_mase_coefficient_value:
if is_test_set:
self.mase_coefficient = compute_mase_coefficient(self.Y, sp=self.sp)
else:
self.mase_coefficient = compute_mase_coefficient(self.Y[:-n_prediction_steps], sp=self.sp)
else:
self.mase_coefficient = np.asarray([1.0])
self.known_future_features_index = known_future_features_index
self.transform_time_features = False
self._cached_time_features: Optional[np.ndarray] = time_features
self.future_observed_target = None
self.is_test_set = is_test_set
@property
def is_test_set(self) -> bool:
return self._is_test_set
@is_test_set.setter
def is_test_set(self, value: bool) -> None:
if value and value != self._is_test_set:
if self.known_future_features_index:
if self.X_test is None:
raise ValueError('When future features are known, X_test '
'for Time Series Sequences must be given!')
if self.Y_test is not None:
self.future_observed_target = ~np.isnan(self.Y_test)
self._is_test_set = value
def __getitem__(self, index: int, train: bool = True) \
-> Tuple[Dict[str, torch.Tensor], Optional[Dict[str, torch.Tensor]]]:
"""
get a subsequent of time series data, unlike vanilla tabular dataset, we obtain all the previous observations
until the given index
Args:
index (int):
what element to yield from the series
train (bool):
Whether a train or test transformation is applied
Returns:
past_information (Dict[str, torch.Tensor]):
a dict contains all the required information required for future forecasting
past_targets (torch.Tensor), past_features(Optional[torch.Tensor]),
future_features(Optional[torch.Tensor]),
mase_coefficient (np.array, cached value to compute MASE scores),
past_observed_targets(torch.BoolTensor), if the past targets are observed.
decoder_lengths(int), length of decoder output
future_information (Optional[Dict[str, torch.Tensor]]):
a dict contains all the future information that are required to predict, including
future_targets: (torch.Tensor) and future_observed_targets (torch.BoolTensor)
"""
if index < 0:
index = self.__len__() + index
if self.X is not None:
past_features = self.X[:index + 1]
if self.known_future_features_index:
if not self.is_test_set:
future_features = \
self.X[index + 1: index + self.n_prediction_steps + 1, self.known_future_features_index]
else:
if index < self.__len__() - 1:
raise ValueError('Test Sequence is only allowed to be accessed with the last index!')
future_features = self.X_test[:, self.known_future_features_index] # type: ignore[index]
else:
future_features = None
else:
past_features = None
future_features = None
if self.train_transform is not None and train and past_features is not None:
past_features = self.train_transform(past_features)
if future_features is not None:
future_features = self.train_transform(future_features)
elif self.val_transform is not None and not train and past_features is not None:
past_features = self.val_transform(past_features)
if future_features is not None:
future_features = self.val_transform(future_features)
if self.transform_time_features:
if self.time_feature_transform:
self.cache_time_features()
if past_features is not None:
past_features = np.hstack(
[past_features, self._cached_time_features[:index + 1]] # type: ignore[index]
)
else:
past_features = self._cached_time_features[:index + 1] # type: ignore[index]
if future_features is not None:
future_features = np.hstack([
future_features,
self._cached_time_features[index + 1:index + self.n_prediction_steps + 1] # type: ignore[index]
])
else:
future_features = self._cached_time_features[index + 1: # type: ignore[index]
index + self.n_prediction_steps + 1]
if future_features is not None and future_features.shape[0] == 0:
future_features = None
# In case of prediction, the targets are not provided
targets = self.Y
if self.is_test_set:
if self.Y_test is not None:
future_targets: Optional[Dict[str, torch.Tensor]] = {
'future_targets': torch.from_numpy(self.Y_test),
'future_observed_targets': torch.from_numpy(self.future_observed_target)
}
else:
future_targets = None
else:
future_targets_np = targets[index + 1: index + self.n_prediction_steps + 1]
future_targets_tt = torch.from_numpy(future_targets_np)
future_targets = {
'future_targets': future_targets_tt,
'future_observed_targets': torch.from_numpy(
self.observed_target[index + 1: index + self.n_prediction_steps + 1]
)
}
if isinstance(past_features, np.ndarray):
past_features = torch.from_numpy(past_features)
if isinstance(future_features, np.ndarray):
future_features = torch.from_numpy(future_features)
past_target = targets[:index + 1]
past_target = torch.from_numpy(past_target)
return {"past_targets": past_target,
"past_features": past_features,
"future_features": future_features,
"mase_coefficient": self.mase_coefficient,
'past_observed_targets': torch.from_numpy(self.observed_target[:index + 1]),
'decoder_lengths': 0 if future_targets is None else future_targets['future_targets'].shape[
0]}, future_targets
def __len__(self) -> int:
return int(self.Y.shape[0]) if self.is_test_set else int(self.Y.shape[0]) - self.n_prediction_steps
def get_target_values(self, index: int) -> np.ndarray:
"""
Get the visible targets in the datasets without generating a tensor. This can be used to create a dummy pipeline
Args:
index (int):
target index
Returns:
y (np.ndarray):
the last visible target value
"""
if index < 0:
index = self.__len__() + index
return self.Y[index]
def cache_time_features(self) -> None:
"""
compute time features if it is not cached. For test sets, we also need to compute the time features for future
"""
if self._cached_time_features is None:
periods = self.Y.shape[0]
if self.is_test_set:
periods += self.n_prediction_steps
self._cached_time_features = compute_time_features(self.start_time, periods,
periods, self.freq, self.time_feature_transform)
else:
if self.is_test_set:
if self._cached_time_features.shape[0] == self.Y.shape[0]:
time_feature_future = compute_time_features(self.start_time,
self.n_prediction_steps + self.Y.shape[0],
self.n_prediction_steps,
self.freq, self.time_feature_transform)
self._cached_time_features = np.concatenate([self._cached_time_features, time_feature_future])
def update_transform(self, transform: Optional[torchvision.transforms.Compose],
train: bool = True,
) -> 'BaseDataset':
"""
During the pipeline execution, the pipeline object might propose transformations
as a product of the current pipeline configuration being tested.
This utility allows to return a self with the updated transformation, so that
a dataloader can yield this dataset with the desired transformations
Args:
transform (torchvision.transforms.Compose):
The transformations proposed by the current pipeline
train (bool):
Whether to update the train or validation transform
Returns:
self: A copy of the update pipeline
"""
if train:
self.train_transform = transform
else:
self.val_transform = transform
return self
def get_val_seq_set(self, index: int) -> "TimeSeriesSequence":
if self.is_test_set:
raise ValueError("get_val_seq_set is not supported for the test sequences!")
if index < 0:
index = self.__len__() + index
if index >= self.__len__() - 1:
# TODO consider X_test?
val_set = copy.deepcopy(self)
if val_set.X is not None:
val_set.X_test = val_set.X[-self.n_prediction_steps:]
val_set.X = val_set.X[:-self.n_prediction_steps]
val_set.Y_test = val_set.Y[-self.n_prediction_steps:]
val_set.Y = val_set.Y[:-self.n_prediction_steps]
val_set.future_observed_target = val_set.observed_target[-self.n_prediction_steps:]
val_set.observed_target = val_set.observed_target[:-self.n_prediction_steps]
val_set.is_test_set = True
return val_set
else:
if self.X is not None:
X = self.X[:index + 1]
else:
X = None
if self.known_future_features_index:
X_test = self.X[index + 1: index + 1 + self.n_prediction_steps] # type: ignore[index]
else:
X_test = None
if self._cached_time_features is None:
cached_time_features = None
else:
cached_time_features = self._cached_time_features[:index + 1 + self.n_prediction_steps]
val_set = TimeSeriesSequence(X=X,
Y=self.Y[:index + 1],
X_test=X_test,
Y_test=self.Y[index + 1: index + 1 + self.n_prediction_steps],
start_time=self.start_time,
freq=self.freq,
time_feature_transform=self.time_feature_transform,
train_transforms=self.train_transform,
val_transforms=self.val_transform,
n_prediction_steps=self.n_prediction_steps,
known_future_features_index=self.known_future_features_index,
sp=self.sp,
compute_mase_coefficient_value=False,
time_features=cached_time_features,
is_test_set=True)
return val_set
def get_test_target(self, test_idx: int) -> np.ndarray:
if self.is_test_set:
raise ValueError("get_test_target is not supported for test sequences!")
if test_idx < 0:
test_idx = self.__len__() + test_idx
Y_future = self.Y[test_idx + 1: test_idx + self.n_prediction_steps + 1]
return Y_future
def update_attribute(self, **kwargs: Any) -> None:
for key, value in kwargs.items():
if not hasattr(self, key):
raise ValueError('Trying to update invalid attribute for TimeSeriesSequence!')
setattr(self, key, value)
class TimeSeriesForecastingDataset(BaseDataset, ConcatDataset):
"""
Dataset class for time series forecasting used in AutoPyTorch. It consists of multiple TimeSeriesSequence.
Train and test tensors are stored as pd.DataFrame whereas their index indicates which series the data belongs to
Args:
X (Optional[Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]]):
time series features. can be None if we work with a uni-variant forecasting task
Y (Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]):
forecasting targets. Must be given
X_test (Optional[Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]]):
known future features. It is a collection of series that has the same amount of data as X. It
is designed to be at the tail of X. If no feature is known in the future, this value can be omitted.
Y_test (Optional[Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]] = None):
future targets. It is a collection of series that has the same data of series as Y. It is designed to be at
the tail of Y after the timestamps that need to be predicted.
start_times (Optional[List[pd.DatetimeIndex]]):
starting time of each series when they are sampled. If it is not given, we simply start with a fixed
timestamp.
series_idx (Optional[Union[List[Union[str, int]], str, int]]):
(only works if X is stored as pd.DataFrame). This value is applied to identify towhich series the data
belongs if the data is presented as a "chunk" dataframe
known_future_features (Optional[Union[Tuple[Union[str, int]], Tuple[()]]]):
future features that are known in advance. For instance, holidays.
time_feature_transform (Optional[List[TimeFeature]]):
A list of time feature transformation methods implemented in gluonts. For more information, please check
gluonts.time_feature
freq (Optional[Union[str, int, List[int]]]):
the frequency that the data is sampled. It needs to keep consistent within one dataset
resampling_strategy (Optional[ResamplingStrategies])
resampling strategy. We designed several special resampling resampling_strategy for forecasting tasks.
Please refer to autoPyTorch.datasets.resampling_strategy
resampling_strategy_args (Optional[Dict[str, Any]]):
arguments passed to resampling_strategy
seed (int):
random seeds
train_transforms (Optional[torchvision.transforms.Compose]):
Transformation applied to training data before it is fed to the dataloader
val_transforms (Optional[torchvision.transforms.Compose]):
Transformation applied to validation data before it is fed to the dataloader
validator (Optional[TimeSeriesForecastingInputValidator]):
Input Validator
lagged_value (Optional[List[int]])
We could consider past targets as additional features for the current timestep. This item indicates the
number of timesteps in advanced that we want to apply the targets as our current features
n_prediction_steps (int):
The number of steps you want to forecast into the future (forecast horizon)
dataset_name (Optional[str]):
dataset name
normalize_y(bool):
if targets are normalized within each series
"""
datasets: List[TimeSeriesSequence]
cumulative_sizes: List[int]
def __init__(self,
X: Optional[Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]],
Y: Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]],
X_test: Optional[Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]] = None,
Y_test: Optional[Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]] = None,
start_times: Optional[List[pd.DatetimeIndex]] = None,
series_idx: Optional[Union[List[Union[str, int]], str, int]] = None,
known_future_features: Optional[Union[Tuple[Union[str, int]], Tuple[()]]] = None,
time_feature_transform: Optional[List[TimeFeature]] = None,
freq: Optional[Union[str, int, List[int]]] = None,
resampling_strategy: Optional[ResamplingStrategies] = HoldoutValTypes.time_series_hold_out_validation,
resampling_strategy_args: Optional[Dict[str, Any]] = None,
seed: Optional[int] = 42,
train_transforms: Optional[torchvision.transforms.Compose] = None,
val_transforms: Optional[torchvision.transforms.Compose] = None,
validator: Optional[TimeSeriesForecastingInputValidator] = None,
lagged_value: Optional[List[int]] = None,
n_prediction_steps: int = 1,
dataset_name: Optional[str] = None,
normalize_y: bool = False,
):
# Preprocess time series data information
assert X is not Y, "Training and Test data needs to belong two different object!!!"
seasonality, freq, freq_value = self.compute_freq_values(freq, n_prediction_steps)
self.seasonality = int(seasonality)
self.freq: str = freq
self.freq_value: Union[float, int] = freq_value
self.n_prediction_steps = n_prediction_steps
if dataset_name is None:
dataset_name = str(uuid.uuid1(clock_seq=os.getpid()))
self.dataset_name = dataset_name
# Data Validation
if validator is None:
validator = TimeSeriesForecastingInputValidator(is_classification=False)
self.validator: TimeSeriesForecastingInputValidator = validator
if not isinstance(validator, TimeSeriesForecastingInputValidator):
raise ValueError(f"This dataset only support TimeSeriesForecastingInputValidator "
f"but receive {type(validator)}")
if not self.validator._is_fitted:
self.validator.fit(X_train=X, y_train=Y, X_test=X_test, y_test=Y_test, series_idx=series_idx,
start_times=start_times)
self.is_uni_variant = self.validator._is_uni_variant
self.numerical_columns = self.validator.feature_validator.numerical_columns
self.categorical_columns = self.validator.feature_validator.categorical_columns
self.num_features: int = self.validator.feature_validator.num_features # type: ignore[assignment]
self.num_targets: int = self.validator.target_validator.out_dimensionality # type: ignore[assignment]
self.categories = self.validator.feature_validator.categories
self.feature_shapes = self.validator.feature_shapes
self.feature_names = tuple(self.validator.feature_names)
assert self.validator.start_times is not None
self.start_times = self.validator.start_times
self.static_features = self.validator.feature_validator.static_features
self._transform_time_features = False
if not time_feature_transform:
time_feature_transform = time_features_from_frequency_str(self.freq)
if not time_feature_transform:
# If time features are empty (as for yearly data), we add a
# constant feature of 0
time_feature_transform = [ConstantTransform()]
self.time_feature_transform = time_feature_transform
self.time_feature_names = tuple([f'time_feature_{t.__class__.__name__}' for t in self.time_feature_transform])
# We also need to be able to transform the data, be it for pre-processing
# or for augmentation
self.train_transform = train_transforms
self.val_transform = val_transforms
# Construct time series sequences
if known_future_features is None:
known_future_features = tuple() # type: ignore[assignment]
known_future_features_index = extract_feature_index(self.feature_shapes,
self.feature_names, # type: ignore[arg-type]
queried_features=known_future_features) # type: ignore
self.known_future_features = tuple(known_future_features) # type: ignore[arg-type]
# initialize datasets
self.sequences_builder_kwargs = {"freq": self.freq,
"time_feature_transform": self.time_feature_transform,
"train_transforms": self.train_transform,
"val_transforms": self.val_transform,
"n_prediction_steps": n_prediction_steps,
"sp": self.seasonality,
"known_future_features_index": known_future_features_index}
self.normalize_y = normalize_y
training_sets = self.transform_data_into_time_series_sequence(X, Y,
start_times=self.start_times,
X_test=X_test,
Y_test=Y_test, )
sequence_datasets, train_tensors, test_tensors, sequence_lengths = training_sets
Y: pd.DataFrame = train_tensors[1] # type: ignore[no-redef]
ConcatDataset.__init__(self, datasets=sequence_datasets)
self.num_sequences = len(Y)
self.sequence_lengths_train: np.ndarray = np.asarray(sequence_lengths) - n_prediction_steps
self.seq_length_min = int(np.min(self.sequence_lengths_train))
self.seq_length_median = int(np.median(self.sequence_lengths_train))
self.seq_length_max = int(np.max(self.sequence_lengths_train))
if int(freq_value) > self.seq_length_median:
self.base_window_size = self.seq_length_median
else:
self.base_window_size = int(freq_value)
self.train_tensors: Tuple[Optional[pd.DataFrame], pd.DataFrame] = train_tensors
self.test_tensors: Optional[Tuple[Optional[pd.DataFrame], pd.DataFrame]] = test_tensors
self.val_tensors = None
self.issparse: bool = issparse(self.train_tensors[0])
self.input_shape: Tuple[int, int] = (self.seq_length_min, self.num_features) # type: ignore[assignment]
# process known future features
if known_future_features is None:
future_feature_shapes: Tuple[int, int] = (self.seq_length_min, 0)
else:
future_feature_shapes = (self.seq_length_min, len(known_future_features))
self.encoder_can_be_auto_regressive = (self.input_shape[-1] == future_feature_shapes[-1])
if len(self.train_tensors) == 2 and self.train_tensors[1] is not None:
self.output_type: str = type_of_target(self.train_tensors[1][0].fillna(method="pad"))
if self.output_type in ["binary", "multiclass"]:
# TODO in the future we also want forecasting classification task, we need to find a way to distinguish
# TODO these tasks with the integral forecasting tasks!
self.output_type = "continuous"
if STRING_TO_OUTPUT_TYPES[self.output_type] in CLASSIFICATION_OUTPUTS:
num_targets: int = len(np.unique(Y))
else:
num_targets = Y.shape[-1] if Y.ndim > 1 else 1 # type: ignore[union-attr]
self.output_shape = [self.n_prediction_steps, num_targets] # type: ignore
else:
raise ValueError('Forecasting dataset must contain target values!')
# TODO: Look for a criteria to define small enough to preprocess
self.is_small_preprocess = True
# dataset split
self.task_type: str = TASK_TYPES_TO_STRING[TIMESERIES_FORECASTING]
self.numerical_features: List[int] = self.numerical_columns
self.categorical_features: List[int] = self.categorical_columns
self.random_state = np.random.RandomState(seed=seed)
resampling_strategy_opt, resampling_strategy_args_opt = self.get_split_strategy(
sequence_lengths=sequence_lengths,
n_prediction_steps=n_prediction_steps,
freq_value=self.freq_value,
resampling_strategy=resampling_strategy, # type: ignore[arg-type]
resampling_strategy_args=resampling_strategy_args
)
self.resampling_strategy = resampling_strategy_opt # type: ignore[assignment]
self.resampling_strategy_args = resampling_strategy_args_opt
if isinstance(self.resampling_strategy, CrossValTypes):
self.cross_validators = CrossValFuncs.get_cross_validators(self.resampling_strategy)
else:
self.cross_validators = CrossValFuncs.get_cross_validators(CrossValTypes.time_series_cross_validation)
if isinstance(self.resampling_strategy, HoldoutValTypes):
self.holdout_validators = HoldOutFuncs.get_holdout_validators(self.resampling_strategy)
else:
self.holdout_validators = HoldOutFuncs.get_holdout_validators(
HoldoutValTypes.time_series_hold_out_validation)
self.splits = self.get_splits_from_resampling_strategy() # type: ignore[assignment]
valid_splits = []
for i, split in enumerate(self.splits):
if len(split[0]) > 0:
valid_splits.append(split)
if len(valid_splits) == 0:
raise ValueError(f'The passed value for {n_prediction_steps} is unsuited for the current dataset, please '
'consider reducing n_prediction_steps')
self.splits = valid_splits
# TODO doing experiments to give the most proper way of defining these two values
if lagged_value is None:
try:
lagged_value = [0] + get_lags_for_frequency(freq)
except Exception:
lagged_value = list(range(8))
self.lagged_value = lagged_value
@staticmethod
def compute_freq_values(freq: Optional[Union[str, int, List[int]]],
n_prediction_steps: int) -> Tuple[Union[int, float], str, Union[int, float]]:
"""
Compute frequency related values
"""
if freq is None:
freq = '1Y'
if isinstance(freq, str):
if freq not in SEASONALITY_MAP:
Warning("The given freq name is not supported by our dataset, we will use the default "
"configuration space on the hyperparameter window_size, if you want to adapt this value"
"you could pass freq with a numerical value")
freq_value = SEASONALITY_MAP.get(freq, 1)
else:
freq_value = freq
freq = '1Y'
seasonality = freq_value
if isinstance(freq_value, list):
min_base_size = min(n_prediction_steps, MAX_WINDOW_SIZE_BASE)
if np.max(freq_value) < min_base_size:
tmp_freq = max(freq_value)
else:
tmp_freq = min([freq_value_item for
freq_value_item in freq_value if freq_value_item >= min_base_size])
freq_value = tmp_freq
if isinstance(seasonality, list):
seasonality = min(seasonality) # Use to calculate MASE
return seasonality, freq, freq_value # type: ignore[return-value]
@staticmethod
def compute_time_features(start_times: List[pd.DatetimeIndex],
seq_lengths: List[int],
freq: Union[str, pd.DateOffset],
time_feature_transform: List[TimeFeature]) -> Dict[pd.DatetimeIndex, np.ndarray]:
"""
compute the max series length for each start_time and compute their corresponding time_features. As lots of
series in a dataset share the same start time, we could only compute the features for longest possible series
and reuse them
"""
series_lengths_max: Dict[pd.DatetimeIndex, int] = {}
for start_t, seq_l in zip(start_times, seq_lengths):
if start_t not in series_lengths_max or seq_l > series_lengths_max[start_t]:
series_lengths_max[start_t] = seq_l
series_time_features = {}
for start_t, max_l in series_lengths_max.items():
series_time_features[start_t] = compute_time_features(start_t, max_l, max_l, freq, time_feature_transform)
return series_time_features
def _get_dataset_indices(self, idx: int, only_dataset_idx: bool = False) -> Union[int, Tuple[int, int]]:
"""get which series the data point belongs to"""
if idx < 0:
if -idx > len(self):
raise ValueError("absolute value of index should not exceed dataset length")
idx = len(self) + idx
dataset_idx = bisect.bisect_right(self.cumulative_sizes, idx)
if only_dataset_idx:
return dataset_idx
if dataset_idx == 0:
sample_idx = idx
else:
sample_idx = idx - self.cumulative_sizes[dataset_idx - 1]
return dataset_idx, sample_idx
def __len__(self) -> int:
return ConcatDataset.__len__(self) # type: ignore[no-any-return]
def __getitem__(self, idx: int, # type: ignore[override]
train: bool = True) -> Tuple[Dict[str, torch.Tensor], Optional[Dict[str, torch.Tensor]]]:
dataset_idx, sample_idx = self._get_dataset_indices(idx) # type: ignore[misc]
return self.datasets[dataset_idx].__getitem__(sample_idx, train)
def get_validation_set(self, idx: int) -> TimeSeriesSequence:
"""generate validation series given the index. It ends at the position of the index"""
dataset_idx, sample_idx = self._get_dataset_indices(idx) # type: ignore[misc]
return self.datasets[dataset_idx].get_val_seq_set(sample_idx)
def get_time_series_seq(self, idx: int) -> TimeSeriesSequence:
"""get the series that the data point belongs to"""
dataset_idx = self._get_dataset_indices(idx, True)
return self.datasets[dataset_idx] # type: ignore[index]
def get_test_target(self, test_indices: np.ndarray) -> np.ndarray:
"""get the target data only. This function simply returns a np.array instead of a dictionary"""
test_indices = np.where(test_indices < 0, test_indices + len(self), test_indices)
y_test = np.ones([len(test_indices), self.n_prediction_steps, self.num_targets])
y_test_argsort = np.argsort(test_indices)
dataset_idx: int = self._get_dataset_indices(test_indices[y_test_argsort[0]], # type: ignore[assignment]
only_dataset_idx=True)
for y_i in y_test_argsort:
test_idx = test_indices[y_i]
while test_idx > self.cumulative_sizes[dataset_idx]:
dataset_idx += 1
if dataset_idx != 0:
test_idx = test_idx - self.cumulative_sizes[dataset_idx - 1]
y_test[y_i] = self.datasets[dataset_idx].get_test_target(test_idx)
return y_test.reshape([-1, self.num_targets])
def transform_data_into_time_series_sequence(self,
X: Optional[Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]],
Y: Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]],
start_times: List[pd.DatetimeIndex],
X_test: Optional[
Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]] = None,
Y_test: Optional[
Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]] = None,
is_test_set: bool = False, ) -> Tuple[
List[TimeSeriesSequence],
Tuple[Optional[pd.DataFrame], pd.DataFrame],
Optional[Tuple[Optional[pd.DataFrame], pd.DataFrame]],
List[int]
]:
"""
Transform the raw data into a list of TimeSeriesSequence that can be processed by AutoPyTorch Time Series
build a series time sequence datasets
Args:
X: Optional[Union[np.ndarray, List[Union[pd.DataFrame, np.ndarray]]]]
features, if is_test_set is True, then its length of
Y: pd.DataFrame (N_all, N_target)
flattened train target array with size N_all (the sum of all the series sequences) and number of targets
start_times: List[pd.DatetimeIndex]
start time of each training series
X_test: Optional[np.ndarray (N_all_test, N_feature)]
flattened test feature array with size N_all_test (the sum of all the series sequences) and N_feature,
number of features
Y_test: np.ndarray (N_all_test, N_target)
flattened test target array with size N_all (the sum of all the series sequences) and number of targets
is_test_set: Optional[List[pd.DatetimeIndex]]
if the generated sequence used for test
Returns:
sequence_datasets : List[TimeSeriesSequence]
a list of datasets
train_tensors: Tuple[Optional[pd.DataFrame], pd.DataFrame]
training tensors
test_tensors: Optional[Tuple[Optional[pd.DataFrame], pd.DataFrame]]
test tensors
"""
dataset_with_future_features = X is not None and len(self.known_future_features) > 0
X, Y, sequence_lengths = self.validator.transform(X, Y)
time_features = self.compute_time_features(start_times,
sequence_lengths,
self.freq,
self.time_feature_transform)
if Y_test is not None or X_test is not None:
X_test, Y_test, _ = self.validator.transform(X_test, Y_test,
validate_for_future_features=dataset_with_future_features)
y_groups: pd.DataFrameGroupBy = Y.groupby(Y.index) # type: ignore[union-attr]
if self.normalize_y:
mean = y_groups.agg("mean")
std = y_groups.agg("std")
std[std == 0] = 1.
std.fillna(1.)
Y = (Y - mean) / std
self.y_mean = mean
self.y_std = std
if Y_test is not None:
Y_test = (Y_test[mean.columns] - mean) / std
sequence_datasets, train_tensors, test_tensors = self.make_sequences_datasets(
X=X, Y=Y,
X_test=X_test, Y_test=Y_test,
start_times=start_times,
time_features=time_features,
is_test_set=is_test_set,
**self.sequences_builder_kwargs)
return sequence_datasets, train_tensors, test_tensors, sequence_lengths
@staticmethod
def make_sequences_datasets(X: Optional[pd.DataFrame],
Y: pd.DataFrame,
start_times: List[pd.DatetimeIndex],
time_features: Optional[Dict[pd.DatetimeIndex, np.ndarray]] = None,
X_test: Optional[pd.DataFrame] = None,
Y_test: Optional[pd.DataFrame] = None,
is_test_set: bool = False,
**sequences_kwargs: Any) -> Tuple[
List[TimeSeriesSequence],
Tuple[Optional[pd.DataFrame], pd.DataFrame],
Optional[Tuple[Optional[pd.DataFrame], pd.DataFrame]]
]:
"""
build a series time sequence datasets
Args:
X: pd.DataFrame (N_all, N_feature)
flattened train feature DataFrame with size N_all (the sum of all the series sequences) and N_feature,
number of features, X's index should contain the information identifying its series number
Y: pd.DataFrame (N_all, N_target)
flattened train target array with size N_all (the sum of all the series sequences) and number of targets
start_times: List[pd.DatetimeIndex]
start time of each training series
time_features: Dict[pd.Timestamp, np.ndarray]:
time features for each possible start training times
X_test: Optional[np.ndarray (N_all_test, N_feature)]
flattened test feature array with size N_all_test (the sum of all the series sequences) and N_feature,
number of features
Y_test: np.ndarray (N_all_test, N_target)
flattened test target array with size N_all (the sum of all the series sequences) and number of targets
is_test_set (bool):
if the generated sequence used for test
sequences_kwargs: Dict
additional arguments for test sets
Returns:
sequence_datasets : List[TimeSeriesSequence]
a list of datasets
train_tensors: Tuple[pd.DataFrame, pd.DataFrame]
training tensors
train_tensors: Optional[Tuple[pd.DataFrame, pd.DataFrame]]
training tensors
"""
sequence_datasets = []
y_group = Y.groupby(Y.index)
if X is not None:
x_group = X.groupby(X.index)
if Y_test is not None:
y_test_group = Y_test.groupby(Y_test.index)
if X_test is not None:
x_test_group = X_test.groupby(X_test.index)
for i_ser, (start_time, y) in enumerate(zip(start_times, y_group)):
ser_id = y[0]
y_ser = y[1].transform(np.array).values
x_ser = x_group.get_group(ser_id).transform(np.array).values if X is not None else None
y_test_ser = y_test_group.get_group(ser_id).transform(np.array).values if Y_test is not None else None
x_test_ser = x_test_group.get_group(ser_id).transform(np.array).values if X_test is not None else None
sequence = TimeSeriesSequence(
X=x_ser,
Y=y_ser,
start_time=start_time,
X_test=x_test_ser,
Y_test=y_test_ser,
time_features=time_features[start_time][:len(y_ser)] if time_features is not None else None,
is_test_set=is_test_set,
**sequences_kwargs)
sequence_datasets.append(sequence)
train_tensors = (X, Y)
# we could guarantee that Y_test has shape [len(seq) * n_prediction_steps, num_targets]
test_tensors = (X_test, Y_test.values) if Y_test is not None else None
return sequence_datasets, train_tensors, test_tensors
def replace_data(self,
X_train: pd.DataFrame,
X_test: Optional[pd.DataFrame],
known_future_features_index: Optional[Tuple[int]] = None) -> 'BaseDataset':
super(TimeSeriesForecastingDataset, self).replace_data(X_train=X_train, X_test=X_test)
if X_train is None:
return self
if X_test is not None:
X_test_group = X_test.groupby(X_test.index)
for seq, x in zip(self.datasets, X_train.groupby(X_train.index)):
ser_id = x[0]
x_ser = x[1].transform(np.array).values
seq.X = x_ser
if X_test is not None:
seq.X_test = X_test_group.get_group(ser_id).transform(np.array).values
seq.known_future_features_index = known_future_features_index
seq.is_pre_processed = True
return self
def update_transform(self, transform: Optional[torchvision.transforms.Compose],
train: bool = True,
) -> 'BaseDataset':
"""
During the pipeline execution, the pipeline object might propose transformations