utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 codecs
  15. import yaml
  16. import os
  17. from ....utils import logging
  18. from ...base.predictor.transforms import ts_common
  19. class InnerConfig(object):
  20. """Inner Config"""
  21. def __init__(self, config_path, model_dir=None):
  22. self.inner_cfg = self.load(config_path)
  23. self.model_dir = model_dir
  24. def load(self, config_path):
  25. """load config"""
  26. with codecs.open(config_path, "r", "utf-8") as file:
  27. dic = yaml.load(file, Loader=yaml.FullLoader)
  28. return dic
  29. @property
  30. def pre_transforms(self):
  31. """read preprocess transforms from config file"""
  32. tfs = []
  33. if self.inner_cfg.get("info_params", False):
  34. tf = ts_common.TSCutOff(self.inner_cfg["size"])
  35. tfs.append(tf)
  36. if self.inner_cfg.get("scale", False):
  37. scaler_file_path = os.path.join(self.model_dir, "scaler.pkl")
  38. if not os.path.exists(scaler_file_path):
  39. raise FileNotFoundError(
  40. f"Cannot find scaler file: {scaler_file_path}"
  41. )
  42. tf = ts_common.TSNormalize(
  43. scaler_file_path, self.inner_cfg["info_params"]
  44. )
  45. tfs.append(tf)
  46. tf = ts_common.BuildTSDataset(self.inner_cfg["info_params"])
  47. tfs.append(tf)
  48. if self.inner_cfg.get("time_feat", False):
  49. tf = ts_common.TimeFeature(
  50. self.inner_cfg["info_params"],
  51. self.inner_cfg["size"],
  52. self.inner_cfg["holiday"],
  53. )
  54. tfs.append(tf)
  55. tf = ts_common.TStoArray(self.inner_cfg["input_data"])
  56. tfs.append(tf)
  57. else:
  58. raise ValueError("info_params is not found in config file")
  59. return tfs
  60. @property
  61. def post_transforms(self):
  62. """read preprocess transforms from config file"""
  63. tfs = []
  64. if self.inner_cfg.get("info_params", False):
  65. tf = ts_common.GetAnomaly(
  66. self.inner_cfg["model_threshold"], self.inner_cfg["info_params"]
  67. )
  68. tfs.append(tf)
  69. else:
  70. raise ValueError("info_params is not found in config file")
  71. return tfs