utils.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.ArraytoTS(self.inner_cfg["info_params"])
  66. tfs.append(tf)
  67. if self.inner_cfg.get("scale", False):
  68. scaler_file_path = os.path.join(self.model_dir, "scaler.pkl")
  69. if not os.path.exists(scaler_file_path):
  70. raise FileNotFoundError(
  71. f"Cannot find scaler file: {scaler_file_path}"
  72. )
  73. tf = ts_common.TSDeNormalize(
  74. scaler_file_path, self.inner_cfg["info_params"]
  75. )
  76. tfs.append(tf)
  77. else:
  78. raise ValueError("info_params is not found in config file")
  79. return tfs