config.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. import yaml
  16. from ..ts_base.config import BaseTSConfig
  17. from ....utils.misc import abspath
  18. class LongForecastConfig(BaseTSConfig):
  19. """Long Forecast Config"""
  20. def update_input_len(self, seq_len: int):
  21. """
  22. upadte the input sequence length
  23. Args:
  24. seq_len (int): input length
  25. Raises:
  26. TypeError: if seq_len is not dict, raising TypeError
  27. """
  28. if "seq_len" not in self:
  29. raise RuntimeError(
  30. "Not able to update seq_len, because no seq_len config was found."
  31. )
  32. self.set_val("seq_len", seq_len)
  33. def update_predict_len(self, predict_len: int):
  34. """
  35. updaet the predict sequence length
  36. Args:
  37. predict_len (int): predict length
  38. Raises:
  39. RuntimeError: if predict_len is not set, raising RuntimeError
  40. """
  41. if "predict_len" not in self:
  42. raise RuntimeError(
  43. "Not able to update predict_len, because no predict_len config was found."
  44. )
  45. self.set_val("predict_len", predict_len)
  46. def update_sampling_stride(self, sampling_stride: int):
  47. """
  48. updaet the sampling stride of sequence to reduce the training time
  49. Args:
  50. sampling_stride (int): sampling rate
  51. Raises:
  52. RuntimeError: if sampling stride is not set, raising RuntimeError
  53. """
  54. if "sampling_stride" not in self:
  55. raise RuntimeError(
  56. "Not able to update sampling_stride, because no sampling_stride config was found."
  57. )
  58. self.set_val("sampling_stride", sampling_stride)
  59. def update_dataset(self, dataset_dir: str, dataset_type: str = None):
  60. """
  61. upadte the dataset
  62. Args:
  63. dataset_dir (str): dataset root path
  64. dataset_type (str, optional): type to set for dataset. Default='TSDataset'
  65. """
  66. if dataset_type is None:
  67. dataset_type = "TSDataset"
  68. dataset_dir = abspath(dataset_dir)
  69. ds_cfg = self._make_custom_dataset_config(dataset_dir)
  70. self.update(ds_cfg)
  71. def update_basic_info(self, info_params: dict):
  72. """
  73. update basic info including time_col, freq, target_cols.
  74. Args:
  75. info_params (dict): upadte basic info
  76. Raises:
  77. TypeError: if info_params is not dict, raising TypeError
  78. """
  79. if isinstance(info_params, dict):
  80. self.update({"info_params": info_params})
  81. else:
  82. raise TypeError("`info_params` must be dict.")
  83. def update_patience(self, patience: int):
  84. """
  85. update patience.
  86. Args:
  87. patience (int): upadte patience
  88. Raises:
  89. RuntimeError: if patience is not found, raising RuntimeError
  90. """
  91. if "patience" not in self.model["model_cfg"]:
  92. raise RuntimeError(
  93. "Not able to update patience, because no patience config was found."
  94. )
  95. self.model["model_cfg"]["patience"] = patience
  96. def _make_custom_dataset_config(self, dataset_root_path: str):
  97. """construct the dataset config that meets the format requirements
  98. Args:
  99. dataset_root_path (str): the root directory of dataset.
  100. Returns:
  101. dict: the dataset config.
  102. """
  103. ds_cfg = {
  104. "dataset": {
  105. "name": "TSDataset",
  106. "dataset_root": dataset_root_path,
  107. "train_path": os.path.join(dataset_root_path, "train.csv"),
  108. "val_path": os.path.join(dataset_root_path, "val.csv"),
  109. },
  110. }
  111. return ds_cfg