config.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 TSAnomalyConfig(BaseTSConfig):
  18. """TS Anomaly Detection 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_dataset(self, dataset_dir: str, dataset_type: str = None):
  33. """
  34. update the dataset
  35. Args:
  36. dataset_dir (str): dataset root path
  37. dataset_type (str, optional): type to set for dataset. Default='TSDataset'
  38. """
  39. if dataset_type is None:
  40. dataset_type = "TSADDataset"
  41. dataset_dir = abspath(dataset_dir)
  42. ds_cfg = self._make_custom_dataset_config(dataset_dir)
  43. self.update(ds_cfg)
  44. def update_basic_info(self, info_params: dict):
  45. """
  46. update basic info including time_col, freq, target_cols.
  47. Args:
  48. info_params (dict): update basic info
  49. Raises:
  50. TypeError: if info_params is not dict, raising TypeError
  51. """
  52. if isinstance(info_params, dict):
  53. self.update({"info_params": info_params})
  54. else:
  55. raise TypeError("`info_params` must be a dict.")
  56. def _make_custom_dataset_config(self, dataset_root_path: str):
  57. """construct the dataset config that meets the format requirements
  58. Args:
  59. dataset_root_path (str): the root directory of dataset.
  60. Returns:
  61. dict: the dataset config.
  62. """
  63. ds_cfg = {
  64. "dataset": {
  65. "name": "TSADDataset",
  66. "dataset_root": dataset_root_path,
  67. "train_path": os.path.join(dataset_root_path, "train.csv"),
  68. "val_path": os.path.join(dataset_root_path, "val.csv"),
  69. },
  70. }
  71. return ds_cfg