predict.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import os
  2. from datetime import datetime,timedelta
  3. from pathlib import Path
  4. from pprint import pprint
  5. import numpy as np
  6. import pandas as pd
  7. from ..DHU.config_reader import ConfigReader
  8. from ..._data.main import get_data
  9. from ..DHU.optimize import load_model
  10. from ...tools.data_loader import DataLoader
  11. from ..._utils.data_summary import print_dataframe,summary_dataframe
  12. from ...model.Room.room import RoomDewPredictor
  13. from ..DHU.optimize import load_dhu_State
  14. PATH = os.path.dirname(os.path.realpath(__file__)).replace('\\','/')
  15. MODEL_FUNC_PATH = f'{PATH}/model_func.py'
  16. MODEL_FILE_PATH = f'./model.pkl'
  17. def predict(*inputs,config=None):
  18. if '__LOCAL' in config.keys():
  19. config_reader_path = config['__LOCAL']
  20. data_URL = config['__URL']
  21. else:
  22. config_reader_path = '/mnt/workflow_data'
  23. data_URL = 'http://basedataportal-svc:8080/data/getpointsdata'
  24. config_reader = ConfigReader(path=f'{config_reader_path}/DHU配置.xlsx')
  25. for each_eaup_name in config_reader.all_equp_names:
  26. each_equp_type = config_reader.get_equp_info(each_eaup_name,key='设备类型',info_type='str')
  27. NOW = config_reader.get_app_info(each_eaup_name,'露点预测',key='开始时间',info_type='datetime')
  28. dhu_State = load_dhu_State(
  29. each_eaup_name = each_eaup_name,
  30. config_reader = config_reader,
  31. config_reader_path = config_reader_path,
  32. data_URL = data_URL,
  33. NOW = NOW
  34. )
  35. print(f'{each_eaup_name}设备状态为{dhu_State}')
  36. if dhu_State < 1:
  37. print('设备处于非运行状态,跳过')
  38. continue
  39. equp_data = load_data(
  40. cur_time = NOW,
  41. each_eaup_name = each_eaup_name,
  42. config_reader = config_reader,
  43. config_reader_path = config_reader_path,
  44. data_URL = data_URL,
  45. )
  46. equp_data_sm = equp_data.rolling(10,min_periods=0).mean()
  47. equp_model = load_model(
  48. each_eaup_name = each_eaup_name,
  49. each_equp_type = each_equp_type,
  50. config_reader = config_reader,
  51. config_reader_path = config_reader_path,
  52. use_adj_name = False
  53. )
  54. room_SP_point = config_reader.get_equp_point(
  55. equp_name = each_eaup_name,
  56. equp_class = ['C','D']
  57. )
  58. if each_equp_type in ['DHU_A','DHU_B']:
  59. Dout_pred = equp_model.predict(equp_data_sm)['coil_3']['DoutA']
  60. elif each_equp_type in ['SDHU_A','SDHU_B']:
  61. Dout_pred = equp_model.predict(equp_data_sm)['wheel_1']['DoutP']
  62. else:
  63. raise NotImplementedError
  64. for each_room_num in range(
  65. 1,config_reader.get_equp_info(each_eaup_name,'房间数量','int') + 1
  66. ):
  67. room_model = RoomDewPredictor.load(
  68. f'{config_reader_path}/model/{each_eaup_name}_room_{each_room_num}_Dpv.pkl'
  69. )
  70. Droom_cur = equp_data.loc[:,f'room_{each_room_num}_Dpv'].values[-1]
  71. Droom_pred = room_model.predict_Droom(Dout=Dout_pred,Droom_cur=Droom_cur)
  72. pred_lag = room_model.model_info['model_Droom']['lag']
  73. print(f'{each_eaup_name}房间{each_room_num}在{NOW}的预测值')
  74. print(f'完整预测时序:{Droom_pred}')
  75. print(f'{pred_lag}分钟后的预测值为{Droom_pred[-1]}')
  76. index = pd.Index(
  77. pd.date_range(
  78. start = NOW+timedelta(minutes=1),
  79. end = NOW+timedelta(minutes=1)+timedelta(minutes=int(pred_lag)),
  80. freq = '1min'
  81. )
  82. )
  83. # 完整时序预测
  84. write_ts_predict(
  85. point_id = f"{each_eaup_name}_{room_SP_point[f'room_{each_room_num}_Dpv']}",
  86. value = Droom_pred,
  87. ts = index
  88. )
  89. # 最远预测点
  90. write_lag_predict(
  91. point_id = f"{each_eaup_name}_{room_SP_point[f'room_{each_room_num}_Dpd']}",
  92. value = Droom_pred[-1]
  93. )
  94. # 最远预测步长
  95. write_lag_predict(
  96. point_id = f"{each_eaup_name}_{room_SP_point[f'room_{each_room_num}_Dpdlag']}",
  97. value = pred_lag
  98. )
  99. def load_data(
  100. cur_time,
  101. each_eaup_name,
  102. config_reader,
  103. config_reader_path,
  104. data_URL,
  105. ):
  106. data = (
  107. DataLoader(
  108. path = f'{config_reader_path}/data/room_predict/data_cur/',
  109. start_time = cur_time - timedelta(minutes=120),
  110. end_time = cur_time,
  111. print_process = False
  112. )
  113. .download_equp_data(
  114. equp_name = each_eaup_name,
  115. point = config_reader.get_equp_point(each_eaup_name,equp_class=['A','C']),
  116. url = data_URL,
  117. clean_cache = True
  118. )
  119. .get_equp_data(
  120. equp_name = each_eaup_name,
  121. )
  122. )
  123. summary_dataframe(data,f'{each_eaup_name}除湿机数据')
  124. return data
  125. def write_ts_predict(point_id,value,ts):
  126. ts_and_value = []
  127. for ts_i,value_i in zip(ts,value):
  128. each_ts_and_value = []
  129. each_ts_and_value.append(ts_i.to_pydatetime().timestamp())
  130. each_ts_and_value.append(value_i)
  131. ts_and_value.append(each_ts_and_value)
  132. try:
  133. from workflowlib import requests
  134. data = {'point_id': point_id, "json_data": ts_and_value}
  135. res = requests.post(
  136. 'http://m2-backend-svc:8000/api/ai/predict_data/add',
  137. json = data
  138. )
  139. print(res.json())
  140. # if res['errcode'] != 0:
  141. # print(res.json())
  142. # raise
  143. except Exception as e:
  144. print(f'{point_id}写入预测时序失败,{e}')
  145. def write_lag_predict(point_id,value):
  146. try:
  147. from workflowlib import requests
  148. res = requests.post(
  149. 'http://basedataportal-svc:8080/value/set_value',
  150. json={'point_id': point_id, "value": float(value)}
  151. )
  152. print(res.json())
  153. print(f'写入实时点位{point_id}={value}')
  154. except Exception as e:
  155. print(f'{point_id}写入预测值失败,{e}')