|
@@ -7,9 +7,7 @@ import logging
|
|
import traceback
|
|
import traceback
|
|
import os
|
|
import os
|
|
from common.database_dml import get_df_list_from_mongo, insert_data_into_mongo
|
|
from common.database_dml import get_df_list_from_mongo, insert_data_into_mongo
|
|
-import plotly.express as px
|
|
|
|
import plotly.graph_objects as go
|
|
import plotly.graph_objects as go
|
|
-import pandas as pd
|
|
|
|
import plotly.io as pio
|
|
import plotly.io as pio
|
|
from bson.decimal128 import Decimal128
|
|
from bson.decimal128 import Decimal128
|
|
import numbers
|
|
import numbers
|
|
@@ -22,15 +20,16 @@ def put_analysis_report_to_html(args, df_predict, df_accuracy):
|
|
label = args['label']
|
|
label = args['label']
|
|
label_pre = args['label_pre']
|
|
label_pre = args['label_pre']
|
|
farmId = args['farmId']
|
|
farmId = args['farmId']
|
|
-
|
|
|
|
- df_predict = df_predict.applymap(lambda x: float(x.to_decimal()) if isinstance(x, Decimal128) else float(x) if isinstance(x,
|
|
|
|
- numbers.Number) else x).sort_values(by=col_time)
|
|
|
|
- df_accuracy = df_accuracy.applymap(lambda x:float(x.to_decimal()) if isinstance(x, Decimal128) else float(x) if isinstance(x,
|
|
|
|
- numbers.Number) else x).sort_values(by=col_time)
|
|
|
|
-
|
|
|
|
- # -------------------- 预测功率与实际功率曲线 --------------------
|
|
|
|
-
|
|
|
|
- # 生成折线图(以 C_GLOBALR 和 NWP预测总辐射 为例)
|
|
|
|
|
|
+ acc_flag = df_accuracy.shape[0]
|
|
|
|
+ df_predict = df_predict.applymap(
|
|
|
|
+ lambda x: float(x.to_decimal()) if isinstance(x, Decimal128) else float(x) if isinstance(x,
|
|
|
|
+ numbers.Number) else x).sort_values(
|
|
|
|
+ by=col_time)
|
|
|
|
+ if acc_flag > 0:
|
|
|
|
+ df_accuracy = df_accuracy.applymap(
|
|
|
|
+ lambda x: float(x.to_decimal()) if isinstance(x, Decimal128) else float(x) if isinstance(x,
|
|
|
|
+ numbers.Number) else x).sort_values(
|
|
|
|
+ by=col_time)
|
|
# 创建一个图表对象
|
|
# 创建一个图表对象
|
|
fig = go.Figure()
|
|
fig = go.Figure()
|
|
|
|
|
|
@@ -42,8 +41,8 @@ def put_analysis_report_to_html(args, df_predict, df_accuracy):
|
|
y=df_predict[label],
|
|
y=df_predict[label],
|
|
mode='lines+markers',
|
|
mode='lines+markers',
|
|
name='实际功率', # 实际功率
|
|
name='实际功率', # 实际功率
|
|
- line=dict(dash='dot', width=2), # 虚线
|
|
|
|
- marker=dict(symbol='cross'),
|
|
|
|
|
|
+ line=dict(width=1), # 虚线
|
|
|
|
+ marker=dict(symbol='circle'),
|
|
))
|
|
))
|
|
# 为每个模型添加预测值和实际功率的曲线
|
|
# 为每个模型添加预测值和实际功率的曲线
|
|
for model in models:
|
|
for model in models:
|
|
@@ -101,40 +100,45 @@ def put_analysis_report_to_html(args, df_predict, df_accuracy):
|
|
power_html = pio.to_html(fig, full_html=False)
|
|
power_html = pio.to_html(fig, full_html=False)
|
|
|
|
|
|
# -------------------- 准确率表展示--------------------
|
|
# -------------------- 准确率表展示--------------------
|
|
- acc_html = df_accuracy.sort_values(by=col_time).to_html(classes='table table-bordered table-striped', index=False)
|
|
|
|
|
|
+ acc_html = ''
|
|
|
|
+ if acc_flag > 0:
|
|
|
|
+ acc_html = df_accuracy.sort_values(by=col_time).to_html(classes='table table-bordered table-striped',
|
|
|
|
+ index=False)
|
|
# -------------------- 准确率汇总展示--------------------
|
|
# -------------------- 准确率汇总展示--------------------
|
|
- # 指定需要转换的列
|
|
|
|
- cols_to_convert = ['MAE', 'accuracy', 'RMSE', 'deviationElectricity', 'deviationAssessment']
|
|
|
|
- for col in cols_to_convert:
|
|
|
|
- if col in df_accuracy.columns:
|
|
|
|
- df_accuracy[col] = df_accuracy[col].apply(
|
|
|
|
- lambda x: float(x.to_decimal()) if isinstance(x, Decimal128) else float(x) if isinstance(x,
|
|
|
|
- numbers.Number) else np.nan)
|
|
|
|
|
|
+ summary_html = ''
|
|
|
|
+ if acc_flag > 0:
|
|
|
|
+ # 指定需要转换的列
|
|
|
|
+ cols_to_convert = ['MAE', 'accuracy', 'RMSE', 'deviationElectricity', 'deviationAssessment']
|
|
|
|
+ for col in cols_to_convert:
|
|
|
|
+ if col in df_accuracy.columns:
|
|
|
|
+ df_accuracy[col] = df_accuracy[col].apply(
|
|
|
|
+ lambda x: float(x.to_decimal()) if isinstance(x, Decimal128) else float(x) if isinstance(x,
|
|
|
|
+ numbers.Number) else np.nan)
|
|
|
|
|
|
- # 确定存在的列
|
|
|
|
- agg_dict = {}
|
|
|
|
- rename_cols = ['model']
|
|
|
|
- if 'MAE' in df_accuracy.columns:
|
|
|
|
- agg_dict['MAE'] = np.nanmean
|
|
|
|
- rename_cols.append('MAE平均值')
|
|
|
|
- if 'accuracy' in df_accuracy.columns:
|
|
|
|
- agg_dict['accuracy'] = np.nanmean
|
|
|
|
- rename_cols.append('准确率平均值')
|
|
|
|
- if 'RMSE' in df_accuracy.columns:
|
|
|
|
- agg_dict['RMSE'] = np.nanmean
|
|
|
|
- rename_cols.append('RMSE平均值')
|
|
|
|
- if 'deviationElectricity' in df_accuracy.columns:
|
|
|
|
- agg_dict['deviationElectricity'] = [np.nanmean, np.nansum]
|
|
|
|
- rename_cols.append('考核电量平均值')
|
|
|
|
- rename_cols.append('考核总电量')
|
|
|
|
- if 'deviationAssessment' in df_accuracy.columns:
|
|
|
|
- agg_dict['deviationAssessment'] = [np.nanmean, np.nansum]
|
|
|
|
- rename_cols.append('考核分数平均值')
|
|
|
|
- rename_cols.append('考核总分数')
|
|
|
|
- # 进行分组聚合,如果有需要聚合的列
|
|
|
|
- summary_df = df_accuracy.groupby('model').agg(agg_dict).reset_index()
|
|
|
|
- summary_df.columns = rename_cols
|
|
|
|
- summary_html = summary_df.to_html(classes='table table-bordered table-striped', index=False)
|
|
|
|
|
|
+ # 确定存在的列
|
|
|
|
+ agg_dict = {}
|
|
|
|
+ rename_cols = ['model']
|
|
|
|
+ if 'MAE' in df_accuracy.columns:
|
|
|
|
+ agg_dict['MAE'] = np.nanmean
|
|
|
|
+ rename_cols.append('MAE平均值')
|
|
|
|
+ if 'accuracy' in df_accuracy.columns:
|
|
|
|
+ agg_dict['accuracy'] = np.nanmean
|
|
|
|
+ rename_cols.append('准确率平均值')
|
|
|
|
+ if 'RMSE' in df_accuracy.columns:
|
|
|
|
+ agg_dict['RMSE'] = np.nanmean
|
|
|
|
+ rename_cols.append('RMSE平均值')
|
|
|
|
+ if 'deviationElectricity' in df_accuracy.columns:
|
|
|
|
+ agg_dict['deviationElectricity'] = [np.nanmean, np.nansum]
|
|
|
|
+ rename_cols.append('考核电量平均值')
|
|
|
|
+ rename_cols.append('考核总电量')
|
|
|
|
+ if 'deviationAssessment' in df_accuracy.columns:
|
|
|
|
+ agg_dict['deviationAssessment'] = [np.nanmean, np.nansum]
|
|
|
|
+ rename_cols.append('考核分数平均值')
|
|
|
|
+ rename_cols.append('考核总分数')
|
|
|
|
+ # 进行分组聚合,如果有需要聚合的列
|
|
|
|
+ summary_df = df_accuracy.groupby('model').agg(agg_dict).reset_index()
|
|
|
|
+ summary_df.columns = rename_cols
|
|
|
|
+ summary_html = summary_df.to_html(classes='table table-bordered table-striped', index=False)
|
|
# -------------------- 生成完整 HTML 页面 --------------------
|
|
# -------------------- 生成完整 HTML 页面 --------------------
|
|
|
|
|
|
html_content = f"""
|
|
html_content = f"""
|
|
@@ -208,7 +212,6 @@ def put_analysis_report_to_html(args, df_predict, df_accuracy):
|
|
<div class="container">
|
|
<div class="container">
|
|
<h1>分析报告</h1>
|
|
<h1>分析报告</h1>
|
|
<!-- Pandas DataFrame 表格 -->
|
|
<!-- Pandas DataFrame 表格 -->
|
|
-
|
|
|
|
<div class="plot-container">
|
|
<div class="plot-container">
|
|
<h2>1. 预测功率与实际功率曲线对比</h2>
|
|
<h2>1. 预测功率与实际功率曲线对比</h2>
|
|
{power_html}
|
|
{power_html}
|
|
@@ -244,8 +247,13 @@ def put_analysis_report_to_html(args, df_predict, df_accuracy):
|
|
f.write(html_content)
|
|
f.write(html_content)
|
|
print("HTML report generated successfully!")
|
|
print("HTML report generated successfully!")
|
|
return path
|
|
return path
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+import lightgbm as lgb
|
|
|
|
+gbm_model = lgb.train(params,
|
|
|
|
+ lgb_train,
|
|
|
|
+ num_boost_round=500,
|
|
|
|
+ eval_metric=custom_loss,
|
|
|
|
+ valid_sets=[lgb_train, lgb_eval],
|
|
|
|
+ )
|
|
@app.route('/analysis_report_small', methods=['POST'])
|
|
@app.route('/analysis_report_small', methods=['POST'])
|
|
def analysis_report():
|
|
def analysis_report():
|
|
start_time = time.time()
|
|
start_time = time.time()
|
|
@@ -259,7 +267,7 @@ def analysis_report():
|
|
logger.info(args)
|
|
logger.info(args)
|
|
# 获取数据
|
|
# 获取数据
|
|
df_predict, df_accuracy = get_df_list_from_mongo(args)[0], get_df_list_from_mongo(args)[1]
|
|
df_predict, df_accuracy = get_df_list_from_mongo(args)[0], get_df_list_from_mongo(args)[1]
|
|
- path = put_analysis_report_to_html(args,df_predict, df_accuracy)
|
|
|
|
|
|
+ path = put_analysis_report_to_html(args, df_predict, df_accuracy)
|
|
success = 1
|
|
success = 1
|
|
except Exception as e:
|
|
except Exception as e:
|
|
my_exception = traceback.format_exc()
|
|
my_exception = traceback.format_exc()
|