ts.py 3.1 KB

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