瀏覽代碼

awg commit algorithm components

anweiguo 4 月之前
父節點
當前提交
4a89ec1d51

+ 7 - 1
common/database_dml.py

@@ -146,13 +146,19 @@ def insert_h5_model_into_mongo(model,feature_scaler_bytes,target_scaler_bytes ,a
                                 args['mongodb_database'],args['scaler_table'],args['model_table'],args['model_name'])
     client = MongoClient(mongodb_connection)
     db = client[mongodb_database]
+    if scaler_table in db.list_collection_names():
+        db[scaler_table].drop()
+        print(f"Collection '{scaler_table} already exist, deleted successfully!")
     collection = db[scaler_table]  # 集合名称
     # Save the scalers in MongoDB as binary data
     collection.insert_one({
         "feature_scaler": feature_scaler_bytes.read(),
         "target_scaler": target_scaler_bytes.read()
     })
-    print("model inserted successfully!")
+    print("scaler_model inserted successfully!")
+    if model_table in db.list_collection_names():
+        db[model_table].drop()
+        print(f"Collection '{model_table} already exist, deleted successfully!")
     model_table = db[model_table]
     # 创建 BytesIO 缓冲区
     model_buffer = BytesIO()

+ 2 - 0
common/processing_data_common.py

@@ -30,3 +30,5 @@ def missing_features(df, features, col_time, threshold=0.2):
     df = df[~df['day'].isin(days_with_high_missing)]
     print("**********删除后维度", df.shape)
     return df.drop('day',axis=1)
+
+

+ 50 - 9
evaluation_processing/analysis_report.py

@@ -122,25 +122,66 @@ def put_analysis_report_to_html(args, df_clean, df_predict, df_accuracy):
 
     # -------------------- 6.实测气象与预测气象趋势曲线 --------------------
 
-    # 生成折线图(以 C_GLOBALR 和 NWP预测总辐射 为例)
-    y_env = [col_x_env]+ col_x_pre
-    fig_line = px.line(df_clean[(df_clean[col_time]>=df_predict[col_time].min())&(df_clean[col_time]<=df_predict[col_time].max())], x=col_time, y=y_env, markers=True)
-    # 自定义趋势图布局
+    # # 生成折线图(以 C_GLOBALR 和 NWP预测总辐射 为例)实际功率
+    # y_env = [label,col_x_env]+ col_x_pre
+    # fig_line = px.line(df_clean, x=col_time, y=y_env, markers=True)
+    # # fig_line = px.line(df_clean[(df_clean[col_time] >= df_predict[col_time].min()) & (
+    # #             df_clean[col_time] <= df_predict[col_time].max())], x=col_time, y=y_env, markers=True)
+    # # 自定义趋势图布局
+    # fig_line.update_layout(
+    #     template='seaborn',
+    #     # title=dict(text=f"{col_x_env}与{col_x_pre}趋势曲线",
+    #     # x=0.5, font=dict(size=24, color='darkblue')),
+    #     plot_bgcolor='rgba(255, 255, 255, 0.8)',  # 改为白色背景
+    #     xaxis=dict(
+    #         showgrid=True,
+    #         gridcolor='rgba(200, 200, 200, 0.5)',  # 网格线颜色
+    #         rangeslider=dict(visible=True),  # 显示滚动条
+    #         rangeselector=dict(visible=True)  # 显示预设的时间范围选择器
+    #     ),
+    #     yaxis=dict(showgrid=True, gridcolor='rgba(200, 200, 200, 0.5)'),
+    #     legend=dict(x=0.01, y=0.99, bgcolor='rgba(255, 255, 255, 0.7)', bordercolor='black', borderwidth=1)
+    # )
+    #
+    # # 将折线图保存为 HTML 片段
+    # env_pre_html = pio.to_html(fig_line, full_html=False)
+    # 创建折线图(label 单独一个纵轴, [col_x_env] + col_x_pre 一个纵轴)
+    fig_line = px.line(df_clean, x=col_time, y=[label] + [col_x_env] + col_x_pre, markers=True)
+
+    # 修改布局,添加双轴设置
     fig_line.update_layout(
         template='seaborn',
-        # title=dict(text=f"{col_x_env}与{col_x_pre}趋势曲线",
-        # x=0.5, font=dict(size=24, color='darkblue')),
-        plot_bgcolor='rgba(255, 255, 255, 0.8)',  # 改为白色背景
+        plot_bgcolor='rgba(255, 255, 255, 0.8)',  # 设置白色背景
         xaxis=dict(
             showgrid=True,
             gridcolor='rgba(200, 200, 200, 0.5)',  # 网格线颜色
             rangeslider=dict(visible=True),  # 显示滚动条
             rangeselector=dict(visible=True)  # 显示预设的时间范围选择器
         ),
-        yaxis=dict(showgrid=True, gridcolor='rgba(200, 200, 200, 0.5)'),
-        legend=dict(x=0.01, y=0.99, bgcolor='rgba(255, 255, 255, 0.7)', bordercolor='black', borderwidth=1)
+        yaxis=dict(
+            title="实际功率",  # 主纵轴用于 label
+            showgrid=True,
+            gridcolor='rgba(200, 200, 200, 0.5)'
+        ),
+        yaxis2=dict(
+            title="环境数据",  # 第二纵轴用于 [col_x_env] + col_x_pre
+            overlaying='y',  # 与主纵轴叠加
+            side='right',  # 放置在右侧
+            showgrid=False  # 不显示网格线
+        ),
+        legend=dict(
+            x=0.01,
+            y=0.99,
+            bgcolor='rgba(255, 255, 255, 0.7)',
+            bordercolor='black',
+            borderwidth=1
+        )
     )
 
+    # 更新每个曲线的 y 轴对应性
+    for i, col in enumerate([label] + [col_x_env] + col_x_pre):
+        fig_line.data[i].update(yaxis='y' if col == label else 'y2')
+
     # 将折线图保存为 HTML 片段
     env_pre_html = pio.to_html(fig_line, full_html=False)
 

+ 2 - 6
models_processing/model_predict/model_prediction_lstm.py

@@ -5,6 +5,7 @@ import traceback
 import numpy as np
 from itertools import chain
 from common.database_dml import get_data_from_mongo,insert_data_into_mongo,get_h5_model_from_mongo,get_scaler_model_from_mongo
+from common.processing_data_common import str_to_list
 app = Flask('model_prediction_lstm——service')
 
 
@@ -25,7 +26,7 @@ def create_sequences(data_features,data_target,time_steps):
 def model_prediction(df,args):
     features, time_steps, col_time, model_name,col_reserve =  str_to_list(args['features']), int(args['time_steps']),args['col_time'],args['model_name'],str_to_list(args['col_reserve'])
     feature_scaler,target_scaler = get_scaler_model_from_mongo(args)
-    df = df.fillna(method='ffill').fillna(method='bfill').sort_values(by=col_time)
+    df = df.sort_values(by=col_time).fillna(method='ffill').fillna(method='bfill')
     scaled_features = feature_scaler.transform(df[features])
     X_predict, _ = create_sequences(scaled_features, [], time_steps)
     # 加载模型时传入自定义损失函数
@@ -39,11 +40,6 @@ def model_prediction(df,args):
     features_reserve = col_reserve + ['model', 'predict']
     return result[set(features_reserve)]
 
-def str_to_list(arg):
-    if arg == '':
-        return []
-    else:
-        return arg.split(',')
 
 @app.route('/model_prediction_lstm', methods=['POST'])
 def model_prediction_lstm():

+ 14 - 10
models_processing/model_train/model_training_lstm.py

@@ -15,11 +15,22 @@ from common.database_dml import get_data_from_mongo,insert_h5_model_into_mongo
 from common.processing_data_common import missing_features,str_to_list
 import time
 import random
+import matplotlib.pyplot as plt
 app = Flask('model_training_lightgbm——service')
+
 def rmse(y_true, y_pred):
     return tf.math.sqrt(tf.reduce_mean(tf.square(y_true - y_pred)))
 
-
+def draw_loss(history):
+    #绘制训练集和验证集损失
+    plt.figure(figsize=(20, 8))
+    plt.plot(history.history['loss'], label='Training Loss')
+    plt.plot(history.history['val_loss'], label='Validation Loss')
+    plt.title('Loss Curve')
+    plt.xlabel('Epochs')
+    plt.ylabel('Loss')
+    plt.legend()
+    plt.show()
 # 创建时间序列数据
 def create_sequences(data_features,data_target,time_steps):
     X, y = [], []
@@ -42,8 +53,8 @@ def build_model(data, args):
     if 'is_limit' in data.columns:
         data = data[data['is_limit']==False]
     # 清洗特征平均缺失率大于20%的天
-    df = missing_features(data, features, col_time)
-    train_data = data.fillna(method='ffill').fillna(method='bfill').sort_values(by=col_time)
+    data = missing_features(data, features, col_time)
+    train_data = data.sort_values(by=col_time).fillna(method='ffill').fillna(method='bfill')
     # X_train, X_test, y_train, y_test = process_data(df_clean, params)
     # 创建特征和目标的标准化器
     feature_scaler = MinMaxScaler(feature_range=(0, 1))
@@ -82,13 +93,6 @@ def build_model(data, args):
     return model,feature_scaler_bytes,target_scaler_bytes
 
 
-def str_to_list(arg):
-    if arg == '':
-        return []
-    else:
-        return arg.split(',')
-
-
 @app.route('/model_training_lstm', methods=['POST'])
 def model_training_lstm():
     # 获取程序开始时间