cdq_coe_gen.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # @FileName :cdq_coe_gen.py
  4. # @Time :2025/3/28 16:20
  5. # @Author :David
  6. # @Company: shenyang JY
  7. import os, requests, json, time, traceback
  8. import pandas as pd
  9. import numpy as np
  10. from common.database_dml_koi import get_data_from_mongo
  11. from flask import Flask, request, g
  12. from datetime import datetime
  13. from common.logs import Log
  14. logger = Log('post-processing').logger
  15. current_path = os.path.dirname(__file__)
  16. API_URL = "http://ds2:18080/accuracyAndBiasByJSON"
  17. app = Flask('cdq_coe_gen——service')
  18. @app.before_request
  19. def update_config():
  20. # ------------ 整理参数,整合请求参数 ------------
  21. g.coe = {'T'+str(x):{} for x in range(1, 17)}
  22. def iterate_coe(pre_data, point, col_power, col_pre, coe):
  23. """
  24. 更新16个点系数
  25. """
  26. T = 'T' + str(point + 1)
  27. best_acc, best_score1, best_coe_m, best_coe_n = 0, 0, 0, 0
  28. best_score, best_acc1, best_score_m, best_score_n = 999, 0, 999, 0
  29. req_his_fix = prepare_request_body(pre_data, col_power, 'his_fix')
  30. req_dq = prepare_request_body(pre_data, col_power, col_pre)
  31. his_fix_acc, his_fix_score = calculate_acc(API_URL, req_his_fix)
  32. dq_acc, dq_score = calculate_acc(API_URL, req_dq)
  33. for i in range(5, 210):
  34. for j in range(5, 210):
  35. pre_data["new"] = round(i / 170 * pre_data[col_pre] + j / 170 * pre_data['his_fix'], 3)
  36. req_new = prepare_request_body(pre_data, col_power, 'new')
  37. acc, acc_score = calculate_acc(API_URL, req_new)
  38. if acc > best_acc:
  39. best_acc = acc
  40. best_score1 = acc_score
  41. best_coe_m = i / 170
  42. best_coe_n = j / 170
  43. if acc_score < best_score:
  44. best_score = acc_score
  45. best_acc1 = acc
  46. best_score_m = i / 170
  47. best_score_n = j / 170
  48. pre_data["coe-acc"] = round(best_coe_m * pre_data[col_pre] + best_coe_n * pre_data['his_fix'], 3)
  49. pre_data["coe-ass"] = round(best_score_m * pre_data[col_pre] + best_score_n * pre_data['his_fix'], 3)
  50. logger.info("1.过去{} - {}的短期的准确率:{:.4f},自动确认系数后,{} 超短期的准确率:{:.4f},历史功率:{:.4f}".format(pre_data['C_TIME'][0], pre_data['C_TIME'].iloc[-1], dq_acc, T, best_acc, his_fix_acc))
  51. logger.info("2.过去{} - {}的短期的考核分:{:.4f},自动确认系数后,{} 超短期的考核分:{:.4f},历史功率:{:.4f}".format(pre_data['C_TIME'][0], pre_data['C_TIME'].iloc[-1], dq_score, T, best_score1, his_fix_score))
  52. logger.info("3.过去{} - {}的短期的准确率:{:.4f},自动确认系数后,{} 超短期的准确率:{:.4f},历史功率:{:.4f}".format(pre_data['C_TIME'][0], pre_data['C_TIME'].iloc[-1], dq_acc, T, best_acc1, his_fix_acc))
  53. logger.info("4.过去{} - {}的短期的考核分:{:.4f},自动确认系数后,{} 超短期的考核分:{:.4f},历史功率:{:.4f}".format(pre_data['C_TIME'][0], pre_data['C_TIME'].iloc[-1], dq_score, T, best_score, his_fix_score))
  54. coe[T]['score_m'] = round(best_score_m, 3)
  55. coe[T]['score_n'] = round(best_score_n, 3)
  56. coe[T]['acc_m'] = round(best_coe_m, 3)
  57. coe[T]['acc_n'] = round(best_coe_n, 3)
  58. logger.info("系数轮询后,最终调整的系数为:{}".format(coe))
  59. def iterate_his_coe(pre_data, point, col_power, col_pre, coe):
  60. """
  61. 更新临近时长Δ
  62. """
  63. T = 'T' + str(point + 1)
  64. best_acc, best_hour = 0, 1
  65. for hour in np.arange(0.25, 4.25, 0.25):
  66. his_window = hour // 0.25
  67. pre_data = history_error(pre_data, col_power, col_pre, his_window)
  68. req_his_fix = prepare_request_body(pre_data, col_power, 'his_fix')
  69. his_fix_acc, his_fix_score = calculate_acc(API_URL, req_his_fix)
  70. if his_fix_acc > best_acc:
  71. best_acc = his_fix_acc
  72. best_hour = float(round(hour, 2))
  73. coe[T]['hour'] = best_hour
  74. logger.info(f"{T} 点的最优临近时长:{best_hour}")
  75. return pre_data
  76. def prepare_request_body(df, col_power, col_pre):
  77. """
  78. 准备请求体,动态保留MongoDB中的所有字段
  79. """
  80. data = df.copy()
  81. # 转换时间格式为字符串
  82. if 'dateTime' in data.columns and isinstance(data['dateTime'].iloc[0], datetime):
  83. data['dateTime'] = data['dateTime'].dt.strftime('%Y-%m-%d %H:%M:%S')
  84. data['model'] = col_pre
  85. # 排除不需要的字段(如果有)
  86. exclude_fields = ['_id'] # 通常排除MongoDB的默认_id字段
  87. # 获取所有字段名(排除不需要的字段)
  88. available_fields = [col for col in data.columns if col not in exclude_fields]
  89. # 转换为记录列表(保留所有字段)
  90. data = data[available_fields].to_dict('records')
  91. # 构造请求体(固定部分+动态数据部分)
  92. request_body = {
  93. "stationCode": "J00600",
  94. "realPowerColumn": col_power,
  95. "ablePowerColumn": col_power,
  96. "predictPowerColumn": col_pre,
  97. "inStalledCapacityName": 153,
  98. "computTypeEnum": "E2",
  99. "computMeasEnum": "E2",
  100. "openCapacityName": 153,
  101. "onGridEnergy": 0,
  102. "price": 0,
  103. "fault": -99,
  104. "colTime": "dateTime", #时间列名(可选,要与上面'dateTime一致')
  105. # "computPowersEnum": "E4" # 计算功率类型(可选)
  106. "data": data # MongoDB数据
  107. }
  108. return request_body
  109. def calculate_acc(api_url, request_body):
  110. """
  111. 调用API接口
  112. """
  113. headers = {
  114. 'Content-Type': 'application/json',
  115. 'Accept': 'application/json'
  116. }
  117. try:
  118. response = requests.post(
  119. api_url,
  120. data=json.dumps(request_body, ensure_ascii=False),
  121. headers=headers
  122. )
  123. result = response.json()
  124. if response.status_code == 200:
  125. acc = np.average([res['accuracy'] for res in result])
  126. # ass = np.average([res['accuracyAssessment'] for res in result])
  127. print("111111111")
  128. return acc, 0
  129. else:
  130. logger.info(f"失败:{result['status']},{result['error']}")
  131. print(f"失败:{result['status']},{result['error']}")
  132. print("22222222")
  133. except requests.exceptions.RequestException as e:
  134. print(f"API调用失败: {e}")
  135. print("333333333")
  136. return None
  137. def history_error(data, col_power, col_pre, his_window):
  138. data['error'] = data[col_power] - data[col_pre]
  139. data['error'] = data['error'].round(2)
  140. data.reset_index(drop=True, inplace=True)
  141. # 用前面5个点的平均error,和象心力相加
  142. numbers = len(data) - his_window
  143. datas = [data.iloc[x: x+his_window, :].reset_index(drop=True) for x in range(0, numbers)]
  144. data_error = [np.mean(d.iloc[0:his_window, -1]) for d in datas]
  145. pad_data_error = np.pad(data_error, (his_window, 0), mode='constant', constant_values=0)
  146. data['his_fix'] = data[col_pre] + pad_data_error
  147. data = data.iloc[5:, :].reset_index(drop=True)
  148. data.loc[data[col_pre] <= 0, ['his_fix']] = 0
  149. data['dateTime'] = pd.to_datetime(data['dateTime'])
  150. data = data.loc[:, ['dateTime', col_power, col_pre, 'his_fix']]
  151. # data.to_csv('J01080原始数据.csv', index=False)
  152. return data
  153. @app.route('/cdq_coe_gen', methods=['POST'])
  154. def get_station_cdq_coe():
  155. # 获取程序开始时间
  156. start_time = time.time()
  157. result = {}
  158. success = 0
  159. args = {}
  160. coe = g.coe
  161. try:
  162. args = request.values.to_dict()
  163. logger.info(args)
  164. data = get_data_from_mongo(args).sort_values(by=args['col_time'], ascending=True)
  165. for point in range(0, 16, 1):
  166. pre_data = iterate_his_coe(data, point, args['col_power'], args['col_pre'], coe)
  167. iterate_coe(pre_data, point, args['col_power'], args['col_pre'], coe)
  168. success = 1
  169. except Exception as e:
  170. my_exception = traceback.format_exc()
  171. my_exception.replace("\n", "\t")
  172. result['msg'] = my_exception
  173. logger.info("调系数出错:{}".format(my_exception))
  174. end_time = time.time()
  175. result['success'] = success
  176. result['args'] = args
  177. result['coe'] = coe
  178. result['start_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time))
  179. result['end_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))
  180. return result
  181. if __name__ == "__main__":
  182. # args = {
  183. # 'mongodb_database': 'ldw_ftp',
  184. # 'mongodb_read_table': 'j00600',
  185. # # 'timeBegin': '2025-01-01 00:00:00',
  186. # # 'timeEnd': '2025-01-03 23:45:00'
  187. # }
  188. # data = get_data_from_mongo(args).sort_values(by='dateTime', ascending=True)
  189. # pre_data = history_error(data, col_power='realPower', col_pre='dq')
  190. # for point in range(0, 16, 1):
  191. # iterate_coe(pre_data, point, 'realPower', 'dq')
  192. # run_code = 0
  193. print("Program starts execution!")
  194. from waitress import serve
  195. serve(app, host="0.0.0.0", port=10123,
  196. threads=8, # 指定线程数(默认4,根据硬件调整)
  197. channel_timeout=600 # 连接超时时间(秒)
  198. )
  199. print("server start!")