config.py 2.7 KB

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