David hai 1 mes
pai
achega
90f8d893f4
Modificáronse 1 ficheiros con 43 adicións e 36 borrados
  1. 43 36
      post_processing/cdq_coe_gen.py

+ 43 - 36
post_processing/cdq_coe_gen.py

@@ -8,13 +8,11 @@ import os, requests, json, time, traceback
 import pandas as pd
 import numpy as np
 from common.database_dml_koi import get_data_from_mongo
-from pymongo import MongoClient
-from flask import Flask,request,jsonify, g
+from flask import Flask, request, g
 from datetime import datetime
-# from common.logs import Log
-# logger = Log('post-processing').logger
-from logging import getLogger
-logger = getLogger('xx')
+from common.logs import Log
+
+logger = Log('post-processing').logger
 current_path = os.path.dirname(__file__)
 API_URL = "http://ds2:18080/accuracyAndBiasByJSON"
 app = Flask('cdq_coe_gen——service')
@@ -23,7 +21,7 @@ app = Flask('cdq_coe_gen——service')
 @app.before_request
 def update_config():
     # ------------ 整理参数,整合请求参数 ------------
-    g.coe = {}
+    g.coe = {'T'+str(x):{} for x in range(1, 17)}
 
 
 def iterate_coe(pre_data, point, col_power, col_pre, coe):
@@ -32,7 +30,8 @@ def iterate_coe(pre_data, point, col_power, col_pre, coe):
     """
     T = 'T' + str(point + 1)
     best_acc, best_score1, best_coe_m, best_coe_n = 0, 0, 0, 0
-    best_score, best_acc1, best_score_m, best_score_n = 999, 0, 0, 0
+    best_score, best_acc1, best_score_m, best_score_n = 999, 0, 999, 0
+
     req_his_fix = prepare_request_body(pre_data, col_power, 'his_fix')
     req_dq = prepare_request_body(pre_data, col_power, col_pre)
 
@@ -57,18 +56,10 @@ def iterate_coe(pre_data, point, col_power, col_pre, coe):
 
     pre_data["coe-acc"] = round(best_coe_m * pre_data[col_pre] + best_coe_n * pre_data['his_fix'], 3)
     pre_data["coe-ass"] = round(best_score_m * pre_data[col_pre] + best_score_n * pre_data['his_fix'], 3)
-    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))
-    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))
-    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))
-    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))
+    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))
+    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))
+    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))
+    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))
 
     coe[T]['score_m'] = round(best_score_m, 3)
     coe[T]['score_n'] = round(best_score_n, 3)
@@ -76,6 +67,25 @@ def iterate_coe(pre_data, point, col_power, col_pre, coe):
     coe[T]['acc_n'] = round(best_coe_n, 3)
     logger.info("系数轮询后,最终调整的系数为:{}".format(coe))
 
+def iterate_his_coe(pre_data, point, col_power, col_pre, coe):
+    """
+    更新临近时长Δ
+    """
+    T = 'T' + str(point + 1)
+    best_acc, best_hour = 0, 1
+    for hour in np.arange(0.25, 4.25, 0.25):
+        his_window = hour // 0.25
+        pre_data = history_error(pre_data, col_power, col_pre, his_window)
+        req_his_fix = prepare_request_body(pre_data, col_power, 'his_fix')
+        his_fix_acc, his_fix_score = calculate_acc(API_URL, req_his_fix)
+
+        if his_fix_acc > best_acc:
+            best_acc = his_fix_acc
+            best_hour = float(round(hour, 2))
+    coe[T]['hour'] = best_hour
+    logger.info(f"{T} 点的最优临近时长:{best_hour}")
+    return pre_data
+
 def prepare_request_body(df, col_power, col_pre):
     """
     准备请求体,动态保留MongoDB中的所有字段
@@ -143,15 +153,15 @@ def calculate_acc(api_url, request_body):
         print("333333333")
         return None
 
-def history_error(data, col_power, col_pre):
+def history_error(data, col_power, col_pre, his_window):
     data['error'] =  data[col_power] - data[col_pre]
     data['error'] = data['error'].round(2)
     data.reset_index(drop=True, inplace=True)
     # 用前面5个点的平均error,和象心力相加
-    numbers = len(data) - 5
-    datas = [data.iloc[x: x+5, :].reset_index(drop=True) for x in range(0, numbers)]
-    data_error = [np.mean(d.iloc[0:5, -1]) for d in datas]
-    pad_data_error = np.pad(data_error, (5, 0), mode='constant', constant_values=0)
+    numbers = len(data) - his_window
+    datas = [data.iloc[x: x+his_window, :].reset_index(drop=True) for x in range(0, numbers)]
+    data_error = [np.mean(d.iloc[0:his_window, -1]) for d in datas]
+    pad_data_error = np.pad(data_error, (his_window, 0), mode='constant', constant_values=0)
     data['his_fix'] = data[col_pre] + pad_data_error
     data = data.iloc[5:, :].reset_index(drop=True)
     data.loc[data[col_pre] <= 0, ['his_fix']] = 0
@@ -171,10 +181,10 @@ def get_station_cdq_coe():
     try:
         args = request.values.to_dict()
         logger.info(args)
-        data = get_data_from_mongo(args).sort_values(by='dateTime', ascending=True)
-        pre_data = history_error(data, col_power='realPower', col_pre='dq')
+        data = get_data_from_mongo(args).sort_values(by=args['col_time'], ascending=True)
         for point in range(0, 16, 1):
-            iterate_coe(pre_data, point, 'realPower', 'dq', coe)
+            pre_data = iterate_his_coe(data, point, args['col_power'], args['col_pre'], coe)
+            iterate_coe(pre_data, point, args['col_power'], args['col_pre'], coe)
         success = 1
     except Exception as e:
         my_exception = traceback.format_exc()
@@ -203,11 +213,8 @@ if __name__ == "__main__":
     # run_code = 0
     print("Program starts execution!")
     from waitress import serve
-
-    serve(
-        app,
-        host="0.0.0.0",
-        port=10123,
-        threads=8,  # 指定线程数(默认4,根据硬件调整)
-        channel_timeout=600  # 连接超时时间(秒)
-    )
+    serve(app, host="0.0.0.0", port=10123,
+          threads=8,  # 指定线程数(默认4,根据硬件调整)
+          channel_timeout=600  # 连接超时时间(秒)
+          )
+    print("server start!")