predict.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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='str')=='除湿机模型'
  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. room_model_name = 'TinR'
  66. point_A = config_reader.get_equp_point(each_eaup_name,equp_class=['A'])
  67. point = config_reader.get_equp_point(each_eaup_name,equp_class=['C'])
  68. if each_equp_type in ['DHU_A','DHU_B']:
  69. TinR_name = ['wheel_1_TinR','wheel_2_TinR']
  70. point['wheel_1_TinR'] = point_A['wheel_1_TinR']
  71. point['wheel_2_TinR'] = point_A['wheel_2_TinR']
  72. elif each_equp_type in ['SDHU_A','SDHU_B']:
  73. TinR_name = ['wheel_1_TinR']
  74. point['wheel_1_TinR'] = point_A['wheel_1_TinR']
  75. else:
  76. raise NotImplementedError
  77. equp_data = (
  78. DataLoader(
  79. path = f'{config_reader_path}/data/room_predict/data_cur/',
  80. start_time = NOW - timedelta(minutes=60),
  81. end_time = NOW,
  82. print_process = False
  83. )
  84. .download_equp_data(
  85. equp_name = each_eaup_name,
  86. point = point,
  87. url = data_URL,
  88. clean_cache = True
  89. )
  90. .get_equp_data(
  91. equp_name = each_eaup_name,
  92. )
  93. )
  94. equp_data_sm = equp_data.rolling(10,min_periods=0).mean()
  95. wheel_TinR = equp_data_sm.loc[:,TinR_name].mean(axis=1).values
  96. room_SP_point = config_reader.get_equp_point(equp_name = each_eaup_name,equp_class = ['C','D'])
  97. for each_room_num in range(1,config_reader.get_equp_info(each_eaup_name,'房间数量','int') + 1):
  98. try:
  99. Droom_cur = equp_data.loc[:,f'room_{each_room_num}_Dpv'].values[-1]
  100. if use_DHU_model:
  101. room_model = RoomDewPredictor.load(f'{config_reader_path}/model/{each_eaup_name}_room_{each_room_num}_Dpv.pkl')
  102. Droom_pred = room_model.predict_Droom(Dout=Dout_pred,Droom_cur=Droom_cur)
  103. else:
  104. room_model = RoomDewPredictor.load(f'{config_reader_path}/model/{each_eaup_name}_room_{each_room_num}_Dpv_{room_model_name}.pkl')
  105. Droom_pred = room_model.predict_Droom(Dout=wheel_TinR,Droom_cur=Droom_cur,sm_frac=0.5)
  106. pred_lag = room_model.model_info['model_Droom']['lag']
  107. print(f'{each_eaup_name}房间{each_room_num}在{NOW}的预测值')
  108. print(f'完整预测时序:{Droom_pred}')
  109. index = pd.Index(
  110. pd.date_range(
  111. start = NOW + timedelta(minutes=1),
  112. end = NOW + timedelta(minutes=1)+timedelta(minutes=int(pred_lag)),
  113. freq = '1min'
  114. )
  115. )
  116. # 完整时序预测
  117. write_ts_predict(
  118. point_id = f"{each_eaup_name}_{room_SP_point[f'room_{each_room_num}_Dpv']}",
  119. value = Droom_pred,
  120. ts = index
  121. )
  122. if config_reader.get_equp_info(each_eaup_name,'房间露点预测用当前值',info_type='bool') or len(Droom_pred)==0:
  123. print(f'{each_eaup_name}房间{each_room_num}仅输出当前露点')
  124. Droom_pred_last = Droom_cur
  125. pred_lag = 1
  126. else:
  127. Droom_pred_last = Droom_pred[-1]
  128. print(f'{pred_lag}分钟后的预测值为{Droom_pred_last}')
  129. # 最远预测点
  130. write_lag_predict(
  131. point_id = f"{each_eaup_name}_{room_SP_point[f'room_{each_room_num}_Dpd']}",
  132. value = Droom_pred_last
  133. )
  134. # 最远预测步长
  135. write_lag_predict(
  136. point_id = f"{each_eaup_name}_{room_SP_point[f'room_{each_room_num}_Dpdlag']}",
  137. value = pred_lag
  138. )
  139. except Exception as e:
  140. print(e)
  141. continue
  142. def load_data(
  143. cur_time,
  144. each_eaup_name,
  145. config_reader,
  146. config_reader_path,
  147. data_URL,
  148. ):
  149. data = (
  150. DataLoader(
  151. path = f'{config_reader_path}/data/room_predict/data_cur/',
  152. start_time = cur_time - timedelta(minutes=120),
  153. end_time = cur_time,
  154. print_process = False
  155. )
  156. .download_equp_data(
  157. equp_name = each_eaup_name,
  158. point = config_reader.get_equp_point(each_eaup_name,equp_class=['A','C']),
  159. url = data_URL,
  160. clean_cache = True
  161. )
  162. .get_equp_data(
  163. equp_name = each_eaup_name,
  164. )
  165. )
  166. summary_dataframe(data,f'{each_eaup_name}除湿机数据')
  167. return data
  168. def write_ts_predict(point_id,value,ts):
  169. ts_and_value = []
  170. for ts_i,value_i in zip(ts,value):
  171. each_ts_and_value = []
  172. each_ts_and_value.append(ts_i.to_pydatetime().timestamp())
  173. each_ts_and_value.append(value_i)
  174. ts_and_value.append(each_ts_and_value)
  175. try:
  176. from workflowlib import requests
  177. data = {'point_id': point_id, "json_data": ts_and_value}
  178. res = requests.post(
  179. 'http://m2-backend-svc:8000/api/ai/predict_data/add',
  180. json = data
  181. )
  182. print(res.json())
  183. # if res['errcode'] != 0:
  184. # print(res.json())
  185. # raise
  186. except Exception as e:
  187. print(f'{point_id}写入预测时序失败,{e}')
  188. def write_lag_predict(point_id,value):
  189. try:
  190. from workflowlib import requests
  191. res = requests.post(
  192. 'http://basedataportal-svc:8080/value/set_value',
  193. json={'point_id': point_id, "value": float(value)}
  194. )
  195. print(res.json())
  196. print(f'写入实时点位{point_id}={value}')
  197. except Exception as e:
  198. print(f'{point_id}写入预测值失败,{e}')