ts_fc.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. from ...modules.ts_forecast.model_list import MODELS
  16. from ..components import *
  17. from ..results import TSFcResult
  18. from ..utils.process_hook import batchable_method
  19. from .base import BasicPredictor
  20. class TSFcPredictor(BasicPredictor):
  21. entities = MODELS
  22. def _check_args(self, kwargs):
  23. pass
  24. def _build_components(self):
  25. preprocess = self._build_preprocess()
  26. predictor = TSPPPredictor(
  27. model_dir=self.model_dir,
  28. model_prefix=self.MODEL_FILE_PREFIX,
  29. option=self.pp_option,
  30. )
  31. postprocess = self._build_postprocess()
  32. return {**preprocess, "predictor": predictor, **postprocess}
  33. def _build_preprocess(self):
  34. if not self.config.get("info_params", None):
  35. raise Exception("info_params is not found in config file")
  36. ops = {}
  37. ops["ReadTS"] = ReadTS()
  38. ops["TSCutOff"] = TSCutOff(self.config["size"])
  39. if self.config.get("scale", None):
  40. scaler_file_path = os.path.join(self.model_dir, "scaler.pkl")
  41. if not os.path.exists(scaler_file_path):
  42. raise Exception(f"Cannot find scaler file: {scaler_file_path}")
  43. ops["TSNormalize"] = TSNormalize(
  44. scaler_file_path, self.config["info_params"]
  45. )
  46. ops["BuildTSDataset"] = BuildTSDataset(self.config["info_params"])
  47. if self.config.get("time_feat", None):
  48. ops["TimeFeature"] = TimeFeature(
  49. self.config["info_params"],
  50. self.config["size"],
  51. self.config["holiday"],
  52. )
  53. ops["TStoArray"] = TStoArray(self.config["input_data"])
  54. return ops
  55. def _build_postprocess(self):
  56. if not self.config.get("info_params", None):
  57. raise Exception("info_params is not found in config file")
  58. ops = {}
  59. ops["ArraytoTS"] = ArraytoTS(self.config["info_params"])
  60. if self.config.get("scale", None):
  61. scaler_file_path = os.path.join(self.model_dir, "scaler.pkl")
  62. if not os.path.exists(scaler_file_path):
  63. raise Exception(f"Cannot find scaler file: {scaler_file_path}")
  64. ops["TSDeNormalize"] = TSDeNormalize(
  65. scaler_file_path, self.config["info_params"]
  66. )
  67. return ops
  68. def _pack_res(self, single):
  69. return TSFcResult({"ts_path": single["ts_path"], "forecast": single["pred"]})