predict.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. use_DHU_model = config_reader.get_app_info(each_eaup_name,'露点预测',key='基于除湿机模型',info_type='bool')
  29. dhu_State = load_dhu_State(
  30. each_eaup_name = each_eaup_name,
  31. config_reader = config_reader,
  32. config_reader_path = config_reader_path,
  33. data_URL = data_URL,
  34. NOW = NOW
  35. )
  36. print(f'{each_eaup_name}设备状态为{dhu_State}')
  37. if dhu_State < 1:
  38. print('设备处于非运行状态,跳过')
  39. continue
  40. if use_DHU_model:
  41. print('基于DHU模型进行预测')
  42. equp_data = load_data(
  43. cur_time = NOW,
  44. each_eaup_name = each_eaup_name,
  45. config_reader = config_reader,
  46. config_reader_path = config_reader_path,
  47. data_URL = data_URL,
  48. )
  49. equp_data_sm = equp_data.rolling(10,min_periods=0).mean()
  50. equp_model = load_model(
  51. each_eaup_name = each_eaup_name,
  52. each_equp_type = each_equp_type,
  53. config_reader = config_reader,
  54. config_reader_path = config_reader_path,
  55. use_adj_name = False
  56. )
  57. if each_equp_type in ['DHU_A','DHU_B']:
  58. Dout_pred = equp_model.predict(equp_data_sm)['coil_3']['DoutA']
  59. elif each_equp_type in ['SDHU_A','SDHU_B']:
  60. Dout_pred = equp_model.predict(equp_data_sm)['wheel_1']['DoutP']
  61. else:
  62. raise NotImplementedError
  63. else:
  64. print('基于实测送风露点进行预测')
  65. point_B = config_reader.get_equp_point(each_eaup_name,equp_class=['B'])
  66. point = config_reader.get_equp_point(each_eaup_name,equp_class=['C'])
  67. if each_equp_type in ['DHU_A','DHU_B']:
  68. Dout_name = 'wheel_2_DoutP'
  69. point[Dout_name] = point_B[Dout_name]
  70. elif each_equp_type in ['SDHU_A','SDHU_B']:
  71. Dout_name = 'coil_2_DoutA'
  72. point[Dout_name] = point_B[Dout_name]
  73. else:
  74. raise NotImplementedError
  75. equp_data = (
  76. DataLoader(
  77. path = f'{config_reader_path}/data/room_predict/data_cur/',
  78. start_time = NOW - timedelta(minutes=120),
  79. end_time = NOW,
  80. print_process = False
  81. )
  82. .download_equp_data(
  83. equp_name = each_eaup_name,
  84. point = config_reader.get_equp_point(each_eaup_name,equp_class=['B','C']),
  85. url = data_URL,
  86. clean_cache = True
  87. )
  88. .get_equp_data(
  89. equp_name = each_eaup_name,
  90. )
  91. )
  92. Dout = equp_data.loc[:,Dout_name].values
  93. room_SP_point = config_reader.get_equp_point(equp_name = each_eaup_name,equp_class = ['C','D'])
  94. for each_room_num in range(1,config_reader.get_equp_info(each_eaup_name,'房间数量','int') + 1):
  95. Droom_cur = equp_data.loc[:,f'room_{each_room_num}_Dpv'].values[-1]
  96. if use_DHU_model:
  97. room_model = RoomDewPredictor.load(f'{config_reader_path}/model/{each_eaup_name}_room_{each_room_num}_Dpv.pkl')
  98. Droom_pred = room_model.predict_Droom(Dout=Dout_pred,Droom_cur=Droom_cur)
  99. else:
  100. room_model = RoomDewPredictor.load(f'{config_reader_path}/model/{each_eaup_name}_room_{each_room_num}_Dpv_bk.pkl')
  101. Droom_pred = room_model.predict_Droom(Dout=Dout,Droom_cur=Droom_cur)
  102. pred_lag = room_model.model_info['model_Droom']['lag']
  103. print(f'{each_eaup_name}房间{each_room_num}在{NOW}的预测值')
  104. print(f'完整预测时序:{Droom_pred}')
  105. index = pd.Index(
  106. pd.date_range(
  107. start = NOW+timedelta(minutes=1),
  108. end = NOW+timedelta(minutes=1)+timedelta(minutes=int(pred_lag)),
  109. freq = '1min'
  110. )
  111. )
  112. # 完整时序预测
  113. write_ts_predict(
  114. point_id = f"{each_eaup_name}_{room_SP_point[f'room_{each_room_num}_Dpv']}",
  115. value = Droom_pred,
  116. ts = index
  117. )
  118. if config_reader.get_equp_info(each_eaup_name,'房间露点预测用当前值',info_type='bool'):
  119. print(f'{each_eaup_name}房间{each_room_num}仅输出当前露点')
  120. Droom_pred_last = Droom_cur
  121. pred_lag = 1
  122. else:
  123. Droom_pred_last = Droom_pred[-1]
  124. print(f'{pred_lag}分钟后的预测值为{Droom_pred_last}')
  125. # 最远预测点
  126. write_lag_predict(
  127. point_id = f"{each_eaup_name}_{room_SP_point[f'room_{each_room_num}_Dpd']}",
  128. value = Droom_pred_last
  129. )
  130. # 最远预测步长
  131. write_lag_predict(
  132. point_id = f"{each_eaup_name}_{room_SP_point[f'room_{each_room_num}_Dpdlag']}",
  133. value = pred_lag
  134. )
  135. def load_data(
  136. cur_time,
  137. each_eaup_name,
  138. config_reader,
  139. config_reader_path,
  140. data_URL,
  141. ):
  142. data = (
  143. DataLoader(
  144. path = f'{config_reader_path}/data/room_predict/data_cur/',
  145. start_time = cur_time - timedelta(minutes=120),
  146. end_time = cur_time,
  147. print_process = False
  148. )
  149. .download_equp_data(
  150. equp_name = each_eaup_name,
  151. point = config_reader.get_equp_point(each_eaup_name,equp_class=['A','C']),
  152. url = data_URL,
  153. clean_cache = True
  154. )
  155. .get_equp_data(
  156. equp_name = each_eaup_name,
  157. )
  158. )
  159. summary_dataframe(data,f'{each_eaup_name}除湿机数据')
  160. return data
  161. def write_ts_predict(point_id,value,ts):
  162. ts_and_value = []
  163. for ts_i,value_i in zip(ts,value):
  164. each_ts_and_value = []
  165. each_ts_and_value.append(ts_i.to_pydatetime().timestamp())
  166. each_ts_and_value.append(value_i)
  167. ts_and_value.append(each_ts_and_value)
  168. try:
  169. from workflowlib import requests
  170. data = {'point_id': point_id, "json_data": ts_and_value}
  171. res = requests.post(
  172. 'http://m2-backend-svc:8000/api/ai/predict_data/add',
  173. json = data
  174. )
  175. print(res.json())
  176. # if res['errcode'] != 0:
  177. # print(res.json())
  178. # raise
  179. except Exception as e:
  180. print(f'{point_id}写入预测时序失败,{e}')
  181. def write_lag_predict(point_id,value):
  182. try:
  183. from workflowlib import requests
  184. res = requests.post(
  185. 'http://basedataportal-svc:8080/value/set_value',
  186. json={'point_id': point_id, "value": float(value)}
  187. )
  188. print(res.json())
  189. print(f'写入实时点位{point_id}={value}')
  190. except Exception as e:
  191. print(f'{point_id}写入预测值失败,{e}')