Spaces:
Sleeping
Sleeping
File size: 42,485 Bytes
bacf16b |
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 |
import sys
import os
import yaml
import random
from typing import Any, BinaryIO, Callable, Dict, List, Optional, Sequence, Tuple, Union
import numpy as np
from scipy import stats
import pandas as pd
import darts
from darts import models
from darts import metrics
from darts import TimeSeries
from darts.dataprocessing.transformers import Scaler
from pytorch_lightning.callbacks import Callback
# for darts dataset
from darts.logging import get_logger, raise_if_not
from darts.utils.data.training_dataset import PastCovariatesTrainingDataset, \
DualCovariatesTrainingDataset, \
MixedCovariatesTrainingDataset
from darts.utils.data.inference_dataset import PastCovariatesInferenceDataset, \
DualCovariatesInferenceDataset, \
MixedCovariatesInferenceDataset
from darts.utils.data.utils import CovariateType
# import data formatter
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from data_formatter.base import *
def get_valid_sampling_locations(target_series: Union[TimeSeries, Sequence[TimeSeries]],
output_chunk_length: int = 12,
input_chunk_length: int = 12,
random_state: Optional[int] = 0,
max_samples_per_ts: Optional[int] = None):
"""
Get valid sampling indices data for the model.
Parameters
----------
target_series
The target time series.
output_chunk_length
The length of the output chunk.
input_chunk_length
The length of the input chunk.
use_static_covariates
Whether to use static covariates.
max_samples_per_ts
The maximum number of samples per time series.
"""
random.seed(random_state)
valid_sampling_locations = {}
total_length = input_chunk_length + output_chunk_length
for id, series in enumerate(target_series):
num_entries = len(series)
if num_entries >= total_length:
valid_sampling_locations[id] = [i for i in range(num_entries - total_length + 1)]
if max_samples_per_ts is not None:
updated_sampling_locations = {}
for id, locations in valid_sampling_locations.items():
if len(locations) > max_samples_per_ts:
updated_sampling_locations[id] = random.sample(locations, max_samples_per_ts)
else:
updated_sampling_locations[id] = locations
valid_sampling_locations = updated_sampling_locations
return valid_sampling_locations
class SamplingDatasetPast(PastCovariatesTrainingDataset):
def __init__(
self,
target_series: Union[TimeSeries, Sequence[TimeSeries]],
covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None,
output_chunk_length: int = 12,
input_chunk_length: int = 12,
use_static_covariates: bool = True,
random_state: Optional[int] = 0,
max_samples_per_ts: Optional[int] = None,
remove_nan: bool = False,
) -> None:
"""
Parameters
----------
target_series
One or a sequence of target `TimeSeries`.
covariates:
Optionally, one or a sequence of `TimeSeries` containing past-observed covariates. If this parameter is set,
the provided sequence must have the same length as that of `target_series`. Moreover, all
covariates in the sequence must have a time span large enough to contain all the required slices.
The joint slicing of the target and covariates is relying on the time axes of both series.
output_chunk_length
The length of the "output" series emitted by the model
input_chunk_length
The length of the "input" series fed to the model
use_static_covariates
Whether to use/include static covariate data from input series.
random_state
The random state to use for sampling.
max_samples_per_ts
The maximum number of samples to be drawn from each time series. If None, all samples will be drawn.
remove_nan
Whether to remove None from the output. E.g. if no covariates are provided, the covariates output will be None
or (optionally) removed from the __getitem__ output.
"""
super().__init__()
self.remove_nan = remove_nan
self.target_series = (
[target_series] if isinstance(target_series, TimeSeries) else target_series
)
self.covariates = (
[covariates] if isinstance(covariates, TimeSeries) else covariates
)
# checks
raise_if_not(
covariates is None or len(self.target_series) == len(self.covariates),
"The provided sequence of target series must have the same length as "
"the provided sequence of covariate series.",
)
# get valid sampling locations
self.valid_sampling_locations = get_valid_sampling_locations(target_series,
output_chunk_length,
input_chunk_length,
random_state,
max_samples_per_ts)
# set parameters
self.output_chunk_length = output_chunk_length
self.input_chunk_length = input_chunk_length
self.total_length = input_chunk_length + output_chunk_length
self.total_number_samples = sum([len(v) for v in self.valid_sampling_locations.values()])
self.use_static_covariates = use_static_covariates
def __len__(self):
"""
Returns the total number of possible (input, target) splits.
"""
return self.total_number_samples
def __getitem__(self, idx: int):
# get idx of target series
target_idx = 0
while idx >= len(self.valid_sampling_locations[target_idx]):
idx -= len(self.valid_sampling_locations[target_idx])
target_idx += 1
# get sampling location within the target series
sampling_location = self.valid_sampling_locations[target_idx][idx]
# get target series
target_series = self.target_series[target_idx].values()
past_target_series = target_series[sampling_location : sampling_location + self.input_chunk_length]
future_target_series = target_series[sampling_location + self.input_chunk_length : sampling_location + self.total_length]
# get covariates
if self.covariates is not None:
covariates = self.covariates[target_idx].values()
covariates = covariates[sampling_location : sampling_location + self.input_chunk_length]
else:
covariates = None
# get static covariates
if self.use_static_covariates:
static_covariates = self.target_series[target_idx].static_covariates_values(copy=True)
else:
static_covariates = None
# return elements that are not None
if self.remove_nan:
out = []
out += [past_target_series] if past_target_series is not None else []
out += [covariates] if covariates is not None else []
out += [static_covariates] if static_covariates is not None else []
out += [future_target_series] if future_target_series is not None else []
return tuple(out)
else:
return tuple([past_target_series,
covariates,
static_covariates,
future_target_series])
class SamplingDatasetDual(DualCovariatesTrainingDataset):
def __init__(
self,
target_series: Union[TimeSeries, Sequence[TimeSeries]],
covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None,
output_chunk_length: int = 12,
input_chunk_length: int = 12,
use_static_covariates: bool = True,
random_state: Optional[int] = 0,
max_samples_per_ts: Optional[int] = None,
remove_nan: bool = False,
) -> None:
"""
Parameters
----------
target_series
One or a sequence of target `TimeSeries`.
covariates:
Optionally, one or a sequence of `TimeSeries` containing future-known covariates. If this parameter is set,
the provided sequence must have the same length as that of `target_series`. Moreover, all
covariates in the sequence must have a time span large enough to contain all the required slices.
The joint slicing of the target and covariates is relying on the time axes of both series.
output_chunk_length
The length of the "output" series emitted by the model
input_chunk_length
The length of the "input" series fed to the model
use_static_covariates
Whether to use/include static covariate data from input series.
random_state
The random state to use for sampling.
max_samples_per_ts
The maximum number of samples to be drawn from each time series. If None, all samples will be drawn.
remove_nan
Whether to remove None from the output. E.g. if no covariates are provided, the covariates output will be None
or (optionally) removed from the __getitem__ output.
"""
super().__init__()
self.remove_nan = remove_nan
self.target_series = (
[target_series] if isinstance(target_series, TimeSeries) else target_series
)
self.covariates = (
[covariates] if isinstance(covariates, TimeSeries) else covariates
)
# checks
raise_if_not(
covariates is None or len(self.target_series) == len(self.covariates),
"The provided sequence of target series must have the same length as "
"the provided sequence of covariate series.",
)
# get valid sampling locations
self.valid_sampling_locations = get_valid_sampling_locations(target_series,
output_chunk_length,
input_chunk_length,
random_state,
max_samples_per_ts,)
# set parameters
self.output_chunk_length = output_chunk_length
self.input_chunk_length = input_chunk_length
self.total_length = input_chunk_length + output_chunk_length
self.total_number_samples = sum([len(v) for v in self.valid_sampling_locations.values()])
self.use_static_covariates = use_static_covariates
def __len__(self):
"""
Returns the total number of possible (input, target) splits.
"""
return self.total_number_samples
def __getitem__(self, idx: int):
# get idx of target series
target_idx = 0
while idx >= len(self.valid_sampling_locations[target_idx]):
idx -= len(self.valid_sampling_locations[target_idx])
target_idx += 1
# get sampling location within the target series
sampling_location = self.valid_sampling_locations[target_idx][idx]
# get target series
target_series = self.target_series[target_idx].values()
past_target_series = target_series[sampling_location : sampling_location + self.input_chunk_length]
future_target_series = target_series[sampling_location + self.input_chunk_length : sampling_location + self.total_length]
# get covariates
if self.covariates is not None:
covariates = self.covariates[target_idx].values()
past_covariates = covariates[sampling_location : sampling_location + self.input_chunk_length]
future_covariates = covariates[sampling_location + self.input_chunk_length : sampling_location + self.total_length]
else:
past_covariates = None
future_covariates = None
# get static covariates
if self.use_static_covariates:
static_covariates = self.target_series[target_idx].static_covariates_values(copy=True)
else:
static_covariates = None
# return elements that are not None
if self.remove_nan:
out = []
out += [past_target_series] if past_target_series is not None else []
out += [past_covariates] if past_covariates is not None else []
out += [future_covariates] if future_covariates is not None else []
out += [static_covariates] if static_covariates is not None else []
out += [future_target_series] if future_target_series is not None else []
return tuple(out)
else:
return tuple([past_target_series,
past_covariates,
future_covariates,
static_covariates,
future_target_series])
class SamplingDatasetMixed(MixedCovariatesTrainingDataset):
def __init__(
self,
target_series: Union[TimeSeries, Sequence[TimeSeries]],
past_covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None,
future_covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None,
output_chunk_length: int = 12,
input_chunk_length: int = 12,
use_static_covariates: bool = True,
random_state: Optional[int] = 0,
max_samples_per_ts: Optional[int] = None,
remove_nan: bool = False,
) -> None:
"""
Parameters
----------
target_series
One or a sequence of target `TimeSeries`.
past_covariates
Optionally, one or a sequence of `TimeSeries` containing past-observed covariates. If this parameter is set,
the provided sequence must have the same length as that of `target_series`. Moreover, all
covariates in the sequence must have a time span large enough to contain all the required slices.
The joint slicing of the target and covariates is relying on the time axes of both series.
future_covariates
Optionally, one or a sequence of `TimeSeries` containing future-known covariates. This has to follow
the same constraints as `past_covariates`.
output_chunk_length
The length of the "output" series emitted by the model
input_chunk_length
The length of the "input" series fed to the model
use_static_covariates
Whether to use/include static covariate data from input series.
random_state
The random state to use for sampling.
max_samples_per_ts
The maximum number of samples to be drawn from each time series. If None, all samples will be drawn.
remove_nan
Whether to remove None from the output. E.g. if no covariates are provided, the covariates output will be None
or (optionally) removed from the __getitem__ output.
"""
super().__init__()
self.remove_nan = remove_nan
self.target_series = (
[target_series] if isinstance(target_series, TimeSeries) else target_series
)
self.past_covariates = (
[past_covariates] if isinstance(past_covariates, TimeSeries) else past_covariates
)
self.future_covariates = (
[future_covariates] if isinstance(future_covariates, TimeSeries) else future_covariates
)
# checks
raise_if_not(
future_covariates is None or len(self.target_series) == len(self.future_covariates),
"The provided sequence of target series must have the same length as "
"the provided sequence of covariate series.",
)
raise_if_not(
past_covariates is None or len(self.target_series) == len(self.past_covariates),
"The provided sequence of target series must have the same length as "
"the provided sequence of covariate series.",
)
# get valid sampling locations
self.valid_sampling_locations = get_valid_sampling_locations(target_series,
output_chunk_length,
input_chunk_length,
random_state,
max_samples_per_ts,)
# set parameters
self.output_chunk_length = output_chunk_length
self.input_chunk_length = input_chunk_length
self.total_length = input_chunk_length + output_chunk_length
self.total_number_samples = sum([len(v) for v in self.valid_sampling_locations.values()])
self.use_static_covariates = use_static_covariates
def __len__(self):
"""
Returns the total number of possible (input, target) splits.
"""
return self.total_number_samples
def __getitem__(self, idx: int):
# get idx of target series
target_idx = 0
while idx >= len(self.valid_sampling_locations[target_idx]):
idx -= len(self.valid_sampling_locations[target_idx])
target_idx += 1
# get sampling location within the target series
sampling_location = self.valid_sampling_locations[target_idx][idx]
# get target series
target_series = self.target_series[target_idx].values()
past_target_series = target_series[sampling_location : sampling_location + self.input_chunk_length]
future_target_series = target_series[sampling_location + self.input_chunk_length : sampling_location + self.total_length]
# get past covariates
if self.past_covariates is not None:
past_covariates = self.past_covariates[target_idx].values()
past_covariates = past_covariates[sampling_location : sampling_location + self.input_chunk_length]
else:
past_covariates = None
# get future covariates
if self.future_covariates is not None:
future_covariates = self.future_covariates[target_idx].values()
historic_future_covariates = future_covariates[sampling_location : sampling_location + self.input_chunk_length]
future_covariates = future_covariates[sampling_location + self.input_chunk_length : sampling_location + self.total_length]
else:
future_covariates = None
historic_future_covariates = None
# get static covariates
if self.use_static_covariates:
static_covariates = self.target_series[target_idx].static_covariates_values(copy=True)
else:
static_covariates = None
# return elements that are not None
if self.remove_nan:
out = []
out += [past_target_series] if past_target_series is not None else []
out += [past_covariates] if past_covariates is not None else []
out += [historic_future_covariates] if historic_future_covariates is not None else []
out += [future_covariates] if future_covariates is not None else []
out += [static_covariates] if static_covariates is not None else []
out += [future_target_series] if future_target_series is not None else []
return tuple(out)
else:
return tuple([past_target_series,
past_covariates,
historic_future_covariates,
future_covariates,
static_covariates,
future_target_series])
class SamplingDatasetInferenceMixed(MixedCovariatesInferenceDataset):
def __init__(
self,
target_series: Union[TimeSeries, Sequence[TimeSeries]],
past_covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None,
future_covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None,
n: int = 1,
input_chunk_length: int = 12,
output_chunk_length: int = 1,
use_static_covariates: bool = True,
random_state: Optional[int] = 0,
max_samples_per_ts: Optional[int] = None,
array_output_only: bool = False,
):
"""
Parameters
----------
target_series
One or a sequence of target `TimeSeries`.
past_covariates
Optionally, one or a sequence of `TimeSeries` containing past-observed covariates. If this parameter is set,
the provided sequence must have the same length as that of `target_series`. Moreover, all
covariates in the sequence must have a time span large enough to contain all the required slices.
The joint slicing of the target and covariates is relying on the time axes of both series.
future_covariates
Optionally, one or a sequence of `TimeSeries` containing future-known covariates. This has to follow
the same constraints as `past_covariates`.
n
Number of predictions into the future, could be greater than the output chunk length, in which case, the model
will be called autorregressively.
output_chunk_length
The length of the "output" series emitted by the model
input_chunk_length
The length of the "input" series fed to the model
use_static_covariates
Whether to use/include static covariate data from input series.
random_state
The random state to use for sampling.
max_samples_per_ts
The maximum number of samples to be drawn from each time series. If None, all samples will be drawn.
array_output_only
Whether __getitem__ returns only the arrays or adds the full `TimeSeries` object to the output tuple
This may cause problems with the torch collate and loader functions but works for Darts.
"""
super().__init__(target_series = target_series,
past_covariates = past_covariates,
future_covariates = future_covariates,
n = n,
input_chunk_length = input_chunk_length,
output_chunk_length = output_chunk_length,)
self.target_series = (
[target_series] if isinstance(target_series, TimeSeries) else target_series
)
self.past_covariates = (
[past_covariates] if isinstance(past_covariates, TimeSeries) else past_covariates
)
self.future_covariates = (
[future_covariates] if isinstance(future_covariates, TimeSeries) else future_covariates
)
# checks
raise_if_not(
future_covariates is None or len(self.target_series) == len(self.future_covariates),
"The provided sequence of target series must have the same length as "
"the provided sequence of covariate series.",
)
raise_if_not(
past_covariates is None or len(self.target_series) == len(self.past_covariates),
"The provided sequence of target series must have the same length as "
"the provided sequence of covariate series.",
)
# get valid sampling locations
self.valid_sampling_locations = get_valid_sampling_locations(target_series,
output_chunk_length,
input_chunk_length,
random_state,
max_samples_per_ts,)
# set parameters
self.output_chunk_length = output_chunk_length
self.input_chunk_length = input_chunk_length
self.total_length = input_chunk_length + output_chunk_length
self.total_number_samples = sum([len(v) for v in self.valid_sampling_locations.values()])
self.use_static_covariates = use_static_covariates
self.array_output_only = array_output_only
def __len__(self):
"""
Returns the total number of possible (input, target) splits.
"""
return self.total_number_samples
def __getitem__(self, idx: int):
# get idx of target series
target_idx = 0
while idx >= len(self.valid_sampling_locations[target_idx]):
idx -= len(self.valid_sampling_locations[target_idx])
target_idx += 1
# get sampling location within the target series
sampling_location = self.valid_sampling_locations[target_idx][idx]
# get target series
target_series = self.target_series[target_idx]
past_target_series_with_time = target_series[sampling_location : sampling_location + self.input_chunk_length]
past_end = past_target_series_with_time.time_index[-1]
target_series = self.target_series[target_idx].values()
past_target_series = target_series[sampling_location : sampling_location + self.input_chunk_length]
# get past covariates
if self.past_covariates is not None:
past_covariates = self.past_covariates[target_idx].values()
past_covariates = past_covariates[sampling_location : sampling_location + self.input_chunk_length]
future_past_covariates = past_covariates[sampling_location + self.input_chunk_length : sampling_location + self.total_length]
else:
past_covariates = None
future_past_covariates = None
# get future covariates
if self.future_covariates is not None:
future_covariates = self.future_covariates[target_idx].values()
historic_future_covariates = future_covariates[sampling_location : sampling_location + self.input_chunk_length]
future_covariates = future_covariates[sampling_location + self.input_chunk_length : sampling_location + self.total_length]
else:
future_covariates = None
historic_future_covariates = None
# get static covariates
if self.use_static_covariates:
static_covariates = self.target_series[target_idx].static_covariates_values(copy=True)
else:
static_covariates = None
# whether to remove Timeseries and None and return only arrays
if self.array_output_only:
out = []
out += [past_target_series] if past_target_series is not None else []
out += [past_covariates] if past_covariates is not None else []
out += [historic_future_covariates] if historic_future_covariates is not None else []
out += [future_covariates] if future_covariates is not None else []
out += [future_past_covariates] if future_past_covariates is not None else []
out += [static_covariates] if static_covariates is not None else []
return tuple(out)
else:
return tuple([past_target_series,
past_covariates,
historic_future_covariates,
future_covariates,
future_past_covariates,
static_covariates,
past_target_series_with_time,
past_end + past_target_series_with_time.freq
])
def evalsample(
self, idx: int
) -> TimeSeries:
"""
Returns the future target series at the given index.
"""
# get idx of target series
target_idx = 0
while idx >= len(self.valid_sampling_locations[target_idx]):
idx -= len(self.valid_sampling_locations[target_idx])
target_idx += 1
# get sampling location within the target series
sampling_location = self.valid_sampling_locations[target_idx][idx]
# get target series
target_series = self.target_series[target_idx][sampling_location + self.input_chunk_length : sampling_location + self.total_length]
return target_series
class SamplingDatasetInferencePast(PastCovariatesInferenceDataset):
def __init__(
self,
target_series: Union[TimeSeries, Sequence[TimeSeries]],
covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None,
n: int = 1,
input_chunk_length: int = 12,
output_chunk_length: int = 1,
use_static_covariates: bool = True,
random_state: Optional[int] = 0,
max_samples_per_ts: Optional[int] = None,
array_output_only: bool = False,
):
"""
Parameters
----------
target_series
One or a sequence of target `TimeSeries`.
past_covariates
Optionally, one or a sequence of `TimeSeries` containing past-observed covariates. If this parameter is set,
the provided sequence must have the same length as that of `target_series`. Moreover, all
covariates in the sequence must have a time span large enough to contain all the required slices.
The joint slicing of the target and covariates is relying on the time axes of both series.
n
Number of predictions into the future, could be greater than the output chunk length, in which case, the model
will be called autorregressively.
output_chunk_length
The length of the "output" series emitted by the model
input_chunk_length
The length of the "input" series fed to the model
use_static_covariates
Whether to use/include static covariate data from input series.
random_state
The random state to use for sampling.
max_samples_per_ts
The maximum number of samples to be drawn from each time series. If None, all samples will be drawn.
array_output_only
Whether __getitem__ returns only the arrays or adds the full `TimeSeries` object to the output tuple
This may cause problems with the torch collate and loader functions but works for Darts.
"""
super().__init__(target_series = target_series,
covariates = covariates,
n = n,
input_chunk_length = input_chunk_length,
output_chunk_length = output_chunk_length,)
self.target_series = (
[target_series] if isinstance(target_series, TimeSeries) else target_series
)
self.covariates = (
[covariates] if isinstance(covariates, TimeSeries) else covariates
)
raise_if_not(
covariates is None or len(self.target_series) == len(self.covariates),
"The provided sequence of target series must have the same length as "
"the provided sequence of covariate series.",
)
# get valid sampling locations
self.valid_sampling_locations = get_valid_sampling_locations(target_series,
output_chunk_length,
input_chunk_length,
random_state,
max_samples_per_ts,)
# set parameters
self.output_chunk_length = output_chunk_length
self.input_chunk_length = input_chunk_length
self.total_length = input_chunk_length + output_chunk_length
self.total_number_samples = sum([len(v) for v in self.valid_sampling_locations.values()])
self.use_static_covariates = use_static_covariates
self.array_output_only = array_output_only
def __len__(self):
"""
Returns the total number of possible (input, target) splits.
"""
return self.total_number_samples
def __getitem__(self, idx: int):
# get idx of target series
target_idx = 0
while idx >= len(self.valid_sampling_locations[target_idx]):
idx -= len(self.valid_sampling_locations[target_idx])
target_idx += 1
# get sampling location within the target series
sampling_location = self.valid_sampling_locations[target_idx][idx]
# get target series
target_series = self.target_series[target_idx]
past_target_series_with_time = target_series[sampling_location : sampling_location + self.input_chunk_length]
past_end = past_target_series_with_time.time_index[-1]
target_series = self.target_series[target_idx].values()
past_target_series = target_series[sampling_location : sampling_location + self.input_chunk_length]
# get past covariates
if self.covariates is not None:
past_covariates = self.covariates[target_idx].values()
past_covariates = past_covariates[sampling_location : sampling_location + self.input_chunk_length]
future_past_covariates = past_covariates[sampling_location + self.input_chunk_length : sampling_location + self.total_length]
else:
past_covariates = None
future_past_covariates = None
# get static covariates
if self.use_static_covariates:
static_covariates = self.target_series[target_idx].static_covariates_values(copy=True)
else:
static_covariates = None
# return arrays or arrays with TimeSeries
if self.array_output_only:
out = []
out += [past_target_series] if past_target_series is not None else []
out += [past_covariates] if past_covariates is not None else []
out += [future_past_covariates] if future_past_covariates is not None else []
out += [static_covariates] if static_covariates is not None else []
return tuple(out)
else:
return tuple([past_target_series,
past_covariates,
future_past_covariates,
static_covariates,
past_target_series_with_time,
past_end + past_target_series_with_time.freq])
def evalsample(
self, idx: int
) -> TimeSeries:
"""
Returns the future target series at the given index.
"""
# get idx of target series
target_idx = 0
while idx >= len(self.valid_sampling_locations[target_idx]):
idx -= len(self.valid_sampling_locations[target_idx])
target_idx += 1
# get sampling location within the target series
sampling_location = self.valid_sampling_locations[target_idx][idx]
# get target series
target_series = self.target_series[target_idx][sampling_location + self.input_chunk_length : sampling_location + self.total_length]
return target_series
class SamplingDatasetInferenceDual(DualCovariatesInferenceDataset):
def __init__(
self,
target_series: Union[TimeSeries, Sequence[TimeSeries]],
covariates: Optional[Union[TimeSeries, Sequence[TimeSeries]]] = None,
n: int = 12,
input_chunk_length: int = 12,
output_chunk_length: int = 1,
use_static_covariates: bool = True,
random_state: Optional[int] = 0,
max_samples_per_ts: Optional[int] = None,
array_output_only: bool = False,
):
"""
Parameters
----------
target_series
One or a sequence of target `TimeSeries`.
covariates
Optionally, some future-known covariates that are used for predictions. This argument is required
if the model was trained with future-known covariates.
n
Number of predictions into the future, could be greater than the output chunk length, in which case, the model
will be called autorregressively.
output_chunk_length
The length of the "output" series emitted by the model
input_chunk_length
The length of the "input" series fed to the model
use_static_covariates
Whether to use/include static covariate data from input series.
random_state
The random state to use for sampling.
max_samples_per_ts
The maximum number of samples to be drawn from each time series. If None, all samples will be drawn.
array_output_only
Whether __getitem__ returns only the arrays or adds the full `TimeSeries` object to the output tuple
This may cause problems with the torch collate and loader functions but works for Darts.
"""
super().__init__(target_series = target_series,
covariates = covariates,
n = n,
input_chunk_length = input_chunk_length,
output_chunk_length = output_chunk_length,)
self.target_series = (
[target_series] if isinstance(target_series, TimeSeries) else target_series
)
self.covariates = (
[covariates] if isinstance(covariates, TimeSeries) else covariates
)
raise_if_not(
covariates is None or len(self.target_series) == len(self.covariates),
"The provided sequence of target series must have the same length as "
"the provided sequence of covariate series.",
)
# get valid sampling locations
self.valid_sampling_locations = get_valid_sampling_locations(target_series,
output_chunk_length,
input_chunk_length,
random_state,
max_samples_per_ts,)
# set parameters
self.output_chunk_length = output_chunk_length
self.input_chunk_length = input_chunk_length
self.total_length = input_chunk_length + output_chunk_length
self.total_number_samples = sum([len(v) for v in self.valid_sampling_locations.values()])
self.use_static_covariates = use_static_covariates
self.array_output_only = array_output_only
def __len__(self):
"""
Returns the total number of possible (input, target) splits.
"""
return self.total_number_samples
def __getitem__(self, idx: int):
# get idx of target series
target_idx = 0
while idx >= len(self.valid_sampling_locations[target_idx]):
idx -= len(self.valid_sampling_locations[target_idx])
target_idx += 1
# get sampling location within the target series
sampling_location = self.valid_sampling_locations[target_idx][idx]
# get target series
target_series = self.target_series[target_idx]
past_target_series_with_time = target_series[sampling_location : sampling_location + self.input_chunk_length]
past_end = past_target_series_with_time.time_index[-1]
target_series = self.target_series[target_idx].values()
past_target_series = target_series[sampling_location : sampling_location + self.input_chunk_length]
# get past covariates
if self.covariates is not None:
future_covariates = self.covariates[target_idx].values()
historic_future_covariates = future_covariates[sampling_location : sampling_location + self.input_chunk_length]
future_covariates = future_covariates[sampling_location + self.input_chunk_length : sampling_location + self.total_length]
else:
historic_future_covariates = None
future_covariates = None
# get static covariates
if self.use_static_covariates:
static_covariates = self.target_series[target_idx].static_covariates_values(copy=True)
else:
static_covariates = None
# return arrays or arrays with TimeSeries
if self.array_output_only:
out = []
out += [past_target_series] if past_target_series is not None else []
out += [historic_future_covariates] if historic_future_covariates is not None else []
out += [future_covariates] if future_covariates is not None else []
out += [static_covariates] if static_covariates is not None else []
return tuple(out)
else:
return tuple([past_target_series,
historic_future_covariates,
future_covariates,
static_covariates,
past_target_series_with_time,
past_end + past_target_series_with_time.freq,])
def evalsample(
self, idx: int
) -> TimeSeries:
"""
Returns the future target series at the given index.
"""
# get idx of target series
target_idx = 0
while idx >= len(self.valid_sampling_locations[target_idx]):
idx -= len(self.valid_sampling_locations[target_idx])
target_idx += 1
# get sampling location within the target series
sampling_location = self.valid_sampling_locations[target_idx][idx]
# get target series
target_series = self.target_series[target_idx][sampling_location + self.input_chunk_length : sampling_location + self.total_length]
return target_series
|