config.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. from ....utils.misc import abspath
  16. from ..ts_base.config import BaseTSConfig
  17. class LongForecastConfig(BaseTSConfig):
  18. """Long Forecast Config"""
  19. def update_input_len(self, seq_len: int):
  20. """
  21. update the input sequence length
  22. Args:
  23. seq_len (int): input length
  24. Raises:
  25. TypeError: if seq_len is not dict, raising TypeError
  26. """
  27. if "seq_len" not in self:
  28. raise RuntimeError(
  29. "Not able to update seq_len, because no seq_len config was found."
  30. )
  31. self.set_val("seq_len", seq_len)
  32. def update_predict_len(self, predict_len: int):
  33. """
  34. updaet the predict sequence length
  35. Args:
  36. predict_len (int): predict length
  37. Raises:
  38. RuntimeError: if predict_len is not set, raising RuntimeError
  39. """
  40. if "predict_len" not in self:
  41. raise RuntimeError(
  42. "Not able to update predict_len, because no predict_len config was found."
  43. )
  44. self.set_val("predict_len", predict_len)
  45. def update_sampling_stride(self, sampling_stride: int):
  46. """
  47. updaet the sampling stride of sequence to reduce the training time
  48. Args:
  49. sampling_stride (int): sampling rate
  50. Raises:
  51. RuntimeError: if sampling stride is not set, raising RuntimeError
  52. """
  53. if "sampling_stride" not in self:
  54. raise RuntimeError(
  55. "Not able to update sampling_stride, because no sampling_stride config was found."
  56. )
  57. self.set_val("sampling_stride", sampling_stride)
  58. def update_dataset(self, dataset_dir: str, dataset_type: str = None):
  59. """
  60. update the dataset
  61. Args:
  62. dataset_dir (str): dataset root path
  63. dataset_type (str, optional): type to set for dataset. Default='TSDataset'
  64. """
  65. if dataset_type is None:
  66. dataset_type = "TSDataset"
  67. dataset_dir = abspath(dataset_dir)
  68. ds_cfg = self._make_custom_dataset_config(dataset_dir)
  69. self.update(ds_cfg)
  70. def update_basic_info(self, info_params: dict):
  71. """
  72. update basic info including time_col, freq, target_cols.
  73. Args:
  74. info_params (dict): update basic info
  75. Raises:
  76. TypeError: if info_params is not dict, raising TypeError
  77. """
  78. if isinstance(info_params, dict):
  79. self.update({"info_params": info_params})
  80. else:
  81. raise TypeError("`info_params` must be dict.")
  82. def update_patience(self, patience: int):
  83. """
  84. update patience.
  85. Args:
  86. patience (int): update patience
  87. Raises:
  88. RuntimeError: if patience is not found, raising RuntimeError
  89. """
  90. if "patience" not in self.model["model_cfg"]:
  91. raise RuntimeError(
  92. "Not able to update patience, because no patience config was found."
  93. )
  94. self.model["model_cfg"]["patience"] = patience
  95. def _make_custom_dataset_config(self, dataset_root_path: str):
  96. """construct the dataset config that meets the format requirements
  97. Args:
  98. dataset_root_path (str): the root directory of dataset.
  99. Returns:
  100. dict: the dataset config.
  101. """
  102. ds_cfg = {
  103. "dataset": {
  104. "name": "TSDataset",
  105. "dataset_root": dataset_root_path,
  106. "train_path": os.path.join(dataset_root_path, "train.csv"),
  107. "val_path": os.path.join(dataset_root_path, "val.csv"),
  108. },
  109. }
  110. return ds_cfg