cdq_coe_gen.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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
  8. import pandas as pd
  9. import numpy as np
  10. from common.database_dml_koi import get_data_from_mongo
  11. from pymongo import MongoClient
  12. from flask import Flask,request,jsonify
  13. from datetime import datetime
  14. from common.logs import Log
  15. logger = Log('post-processing').logger
  16. current_path = os.path.dirname(__file__)
  17. API_URL = "http://ds2:18080/accuracyAndBiasByJSON"
  18. def iterate_coe(pre_data, point, col_power, col_pre):
  19. """
  20. 更新16个点系数
  21. """
  22. coe = {}
  23. T = 'T' + str(point + 1)
  24. best_acc, best_score1, best_coe_m, best_coe_n = 0, 0, 0, 0
  25. best_score, best_acc1, best_score_m, best_score_n = 999, 0, 0, 0
  26. req_his_fix = prepare_request_body(pre_data, col_power, 'his_fix')
  27. req_dq = prepare_request_body(pre_data, col_power, col_pre)
  28. his_fix_acc, his_fix_score = calculate_acc(API_URL, req_his_fix)
  29. dq_acc, dq_score = calculate_acc(API_URL, req_dq)
  30. for i in range(5, 210):
  31. for j in range(5, 210):
  32. pre_data["new"] = round(i / 170 * pre_data[col_pre] + j / 170 * pre_data['his_fix'], 3)
  33. req_new = prepare_request_body(pre_data, col_power, 'new')
  34. acc, acc_score = calculate_acc(API_URL, req_new)
  35. if acc > best_acc:
  36. best_acc = acc
  37. best_score1 = acc_score
  38. best_coe_m = i / 170
  39. best_coe_n = j / 170
  40. if acc_score < best_score:
  41. best_score = acc_score
  42. best_acc1 = acc
  43. best_score_m = i / 170
  44. best_score_n = j / 170
  45. pre_data["coe-acc"] = round(best_coe_m * pre_data[col_pre] + best_coe_n * pre_data['his_fix'], 3)
  46. pre_data["coe-ass"] = round(best_score_m * pre_data[col_pre] + best_score_n * pre_data['his_fix'], 3)
  47. logger.info(
  48. "1.过去{} - {}的短期的准确率:{:.4f},自动确认系数后,{} 超短期的准确率:{:.4f},历史功率:{:.4f}".format(
  49. pre_data['C_TIME'][0], pre_data['C_TIME'].iloc[-1], dq_acc, T, best_acc, his_fix_acc))
  50. logger.info(
  51. "2.过去{} - {}的短期的考核分:{:.4f},自动确认系数后,{} 超短期的考核分:{:.4f},历史功率:{:.4f}".format(
  52. pre_data['C_TIME'][0], pre_data['C_TIME'].iloc[-1], dq_score, T, best_score1, his_fix_score))
  53. logger.info(
  54. "3.过去{} - {}的短期的准确率:{:.4f},自动确认系数后,{} 超短期的准确率:{:.4f},历史功率:{:.4f}".format(
  55. pre_data['C_TIME'][0], pre_data['C_TIME'].iloc[-1], dq_acc, T, best_acc1, his_fix_acc))
  56. logger.info(
  57. "4.过去{} - {}的短期的考核分:{:.4f},自动确认系数后,{} 超短期的考核分:{:.4f},历史功率:{:.4f}".format(
  58. pre_data['C_TIME'][0], pre_data['C_TIME'].iloc[-1], dq_score, T, best_score, his_fix_score))
  59. coe[T]['score_m'] = round(best_score_m, 3)
  60. coe[T]['score_n'] = round(best_score_n, 3)
  61. coe[T]['acc_m'] = round(best_coe_m, 3)
  62. coe[T]['acc_n'] = round(best_coe_n, 3)
  63. logger.info("系数轮询后,最终调整的系数为:{}".format(coe))
  64. def prepare_request_body(df, col_power, col_pre):
  65. """
  66. 准备请求体,动态保留MongoDB中的所有字段
  67. """
  68. # 转换时间格式为字符串
  69. if 'dateTime' in df.columns and isinstance(df['dateTime'].iloc[0], datetime):
  70. df['dateTime'] = df['dateTime'].dt.strftime('%Y-%m-%d %H:%M:%S')
  71. # 排除不需要的字段(如果有)
  72. exclude_fields = ['_id'] # 通常排除MongoDB的默认_id字段
  73. # 获取所有字段名(排除不需要的字段)
  74. available_fields = [col for col in df.columns if col not in exclude_fields]
  75. # 转换为记录列表(保留所有字段)
  76. data = df[available_fields].to_dict('records')
  77. # 构造请求体(固定部分+动态数据部分)
  78. request_body = {
  79. "stationCode": "J00600",
  80. "realPowerColumn": col_power,
  81. "ablePowerColumn": col_power,
  82. "predictPowerColumn": col_pre,
  83. "inStalledCapacityName": 153,
  84. "computTypeEnum": "E2",
  85. "computMeasEnum": "E2",
  86. "openCapacityName": 153,
  87. "onGridEnergy": 0,
  88. "price": 0,
  89. "fault": -99,
  90. "colTime": "dateTime", #时间列名(可选,要与上面'dateTime一致')
  91. # "computPowersEnum": "E4" # 计算功率类型(可选)
  92. "data": data # MongoDB数据
  93. }
  94. return request_body
  95. def calculate_acc(api_url, request_body):
  96. """
  97. 调用API接口
  98. """
  99. headers = {
  100. 'Content-Type': 'application/json',
  101. 'Accept': 'application/json'
  102. }
  103. try:
  104. response = requests.post(
  105. api_url,
  106. data=json.dumps(request_body, ensure_ascii=False),
  107. headers=headers
  108. )
  109. result = response.json()
  110. if result['status'] == 200:
  111. acc = np.average([res['accuracy'] for res in result])
  112. ass = np.average([res['accuracyAssessment'] for res in result])
  113. return acc, ass
  114. else:
  115. logger.info(f"失败:{result['status']},{result['error']}")
  116. except requests.exceptions.RequestException as e:
  117. print(f"API调用失败: {e}")
  118. return None
  119. def history_error(data, col_power, col_pre):
  120. data['error'] = data[col_power] - data[col_pre]
  121. data['error'] = data['error'].round(2)
  122. data.reset_index(drop=True, inplace=True)
  123. # 用前面5个点的平均error,和象心力相加
  124. numbers = len(data) - 5
  125. datas = [data.iloc[x: x+5, :].reset_index(drop=True) for x in range(0, numbers)]
  126. data_error = [np.mean(d.iloc[0:5, -1]) for d in datas]
  127. pad_data_error = np.pad(data_error, (5, 0), mode='constant', constant_values=0)
  128. data['his_fix'] = data[col_pre] + pad_data_error
  129. data = data.iloc[5:, :].reset_index(drop=True)
  130. data.loc[data[col_pre] <= 0, ['his_fix']] = 0
  131. data['dateTime'] = pd.to_datetime(data['dateTime'])
  132. data = data.loc[:, ['dateTime', col_power, col_pre, 'his_fix']]
  133. # data.to_csv('J01080原始数据.csv', index=False)
  134. return data
  135. if __name__ == "__main__":
  136. args = {
  137. 'mongodb_database': 'ldw_ftp',
  138. 'mongodb_read_table': 'j00600',
  139. # 'timeBegin': '2025-01-01 00:00:00',
  140. # 'timeEnd': '2025-01-03 23:45:00'
  141. }
  142. data = get_data_from_mongo(args).sort_values(by='dateTime', ascending=True)
  143. pre_data = history_error(data, col_power='realPower', col_pre='dq')
  144. for point in range(0, 16, 1):
  145. iterate_coe(pre_data, point, 'realPower', 'dq')
  146. run_code = 0