ts_fc.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 .base import BasicPredictor
  19. class TSFcPredictor(BasicPredictor):
  20. entities = MODELS
  21. def _build_components(self):
  22. if not self.config.get("info_params", None):
  23. raise Exception("info_params is not found in config file")
  24. self._add_component(ReadTS())
  25. self._add_component(TSCutOff(self.config["size"]))
  26. if self.config.get("scale", None):
  27. scaler_file_path = os.path.join(self.model_dir, "scaler.pkl")
  28. if not os.path.exists(scaler_file_path):
  29. raise Exception(f"Cannot find scaler file: {scaler_file_path}")
  30. self._add_component(
  31. TSNormalize(scaler_file_path, self.config["info_params"])
  32. )
  33. self._add_component(BuildTSDataset(self.config["info_params"]))
  34. if self.config.get("time_feat", None):
  35. self._add_component(
  36. TimeFeature(
  37. self.config["info_params"],
  38. self.config["size"],
  39. self.config["holiday"],
  40. )
  41. )
  42. self._add_component(TStoArray(self.config["input_data"]))
  43. predictor = TSPPPredictor(
  44. model_dir=self.model_dir,
  45. model_prefix=self.MODEL_FILE_PREFIX,
  46. option=self.pp_option,
  47. )
  48. self._add_component(predictor)
  49. self._add_component(ArraytoTS(self.config["info_params"]))
  50. if self.config.get("scale", None):
  51. scaler_file_path = os.path.join(self.model_dir, "scaler.pkl")
  52. if not os.path.exists(scaler_file_path):
  53. raise Exception(f"Cannot find scaler file: {scaler_file_path}")
  54. self._add_component(
  55. TSDeNormalize(scaler_file_path, self.config["info_params"])
  56. )
  57. def _pack_res(self, single):
  58. return TSFcResult({"ts_path": single["ts_path"], "forecast": single["pred"]})