|
@@ -17,42 +17,41 @@ import numbers
|
|
|
|
|
|
app = Flask('analysis_report——service')
|
|
|
|
|
|
-def create_fig(df_predict, col_time, label, label_pre):
|
|
|
+def create_fig(df_predict, col_time, label, label_pre, point):
|
|
|
# 创建一个图表对象
|
|
|
- fig = make_subplots(rows=1, cols=18, subplot_titles=['超短期-第{}点'.format(p) for p in range(1, 17)]+['超短期-平均值'])
|
|
|
- for point in range(1, 18):
|
|
|
- point_data = df_predict[df_predict['howLongAgo']==point]
|
|
|
- # 获取所有的模型
|
|
|
- models = df_predict['model'].unique()
|
|
|
- # 添加实际功率曲线
|
|
|
+ fig = go.Figure()
|
|
|
+ point_data = df_predict[df_predict['howLongAgo']==point]
|
|
|
+ # 获取所有的模型
|
|
|
+ models = df_predict['model'].unique()
|
|
|
+ # 添加实际功率曲线
|
|
|
+ fig.add_trace(go.Scatter(
|
|
|
+ x=df_predict[col_time],
|
|
|
+ y=df_predict[label],
|
|
|
+ mode='lines+markers',
|
|
|
+ name='实际功率', # 实际功率
|
|
|
+ line=dict(width=1), # 虚线
|
|
|
+ marker=dict(symbol='circle'),
|
|
|
+ ))
|
|
|
+ # 为每个模型添加预测值和实际功率的曲线
|
|
|
+ for model in models:
|
|
|
+ # 筛选该模型的数据
|
|
|
+ model_data = point_data[point_data['model'] == model]
|
|
|
+
|
|
|
+ # 添加预测值曲线
|
|
|
fig.add_trace(go.Scatter(
|
|
|
- x=df_predict[col_time],
|
|
|
- y=df_predict[label],
|
|
|
+ x=model_data[col_time],
|
|
|
+ y=model_data[label_pre],
|
|
|
mode='lines+markers',
|
|
|
- name='实际功率', # 实际功率
|
|
|
- line=dict(width=1), # 虚线
|
|
|
+ name=f'{model} 预测值', # 预测值
|
|
|
marker=dict(symbol='circle'),
|
|
|
- ), row=1, col=point)
|
|
|
- # 为每个模型添加预测值和实际功率的曲线
|
|
|
- for model in models:
|
|
|
- # 筛选该模型的数据
|
|
|
- model_data = point_data[point_data['model'] == model]
|
|
|
-
|
|
|
- # 添加预测值曲线
|
|
|
- fig.add_trace(go.Scatter(
|
|
|
- x=model_data[col_time],
|
|
|
- y=model_data[label_pre],
|
|
|
- mode='lines+markers',
|
|
|
- name=f'{model} 预测值', # 预测值
|
|
|
- marker=dict(symbol='circle'),
|
|
|
- line=dict(width=2)
|
|
|
- ), row=1, col=point)
|
|
|
-
|
|
|
+ line=dict(width=2)
|
|
|
+ ))
|
|
|
+ fig_name = '超短期-第{}点'.format(point) if point < 17 else '超短期-平均值'
|
|
|
# 设置图表的标题和标签
|
|
|
fig.update_layout(
|
|
|
template='seaborn', # 使用 seaborn 模板
|
|
|
title=dict(
|
|
|
- # text=f"{label_pre} 与 {label} 对比", # 标题
|
|
|
+ text=fig_name, # 标题
|
|
|
x=0.5, font=dict(size=20, color='darkblue') # 标题居中并设置字体大小和颜色
|
|
|
),
|
|
|
plot_bgcolor='rgba(255, 255, 255, 0.8)', # 背景色
|
|
@@ -94,9 +93,6 @@ def put_analysis_report_to_html(args, df_predict, df_accuracy):
|
|
|
label_pre = args['label_pre']
|
|
|
farmId = args['farmId']
|
|
|
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)
|
|
|
# 获取所有的模型
|
|
|
models = df_predict['model'].unique()
|
|
|
aves = []
|
|
@@ -114,11 +110,15 @@ def put_analysis_report_to_html(args, df_predict, df_accuracy):
|
|
|
ave['howLongAgo'] = 17
|
|
|
ave = ave.reindex(columns=df_predict.columns.tolist())
|
|
|
aves.append(ave)
|
|
|
-
|
|
|
df_predict = pd.concat([df_predict]+aves)
|
|
|
- fig = create_fig(df_predict, col_time, label, label_pre)
|
|
|
+ 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)
|
|
|
+ figs = [create_fig(df_predict, col_time, label, label_pre, p) for p in [1, 8, 16, 17]]
|
|
|
# 将折线图保存为 HTML 片段
|
|
|
- power_htmls = pio.to_html(fig, full_html=False)
|
|
|
+ power_htmls = [pio.to_html(f, full_html=False) for f in figs]
|
|
|
+ power_htmls = ["<div class='plot-container'>{}</div>".format(html) for html in power_htmls]
|
|
|
+
|
|
|
# -------------------- 准确率表展示--------------------
|
|
|
acc_html = ''
|
|
|
if acc_flag > 0:
|
|
@@ -232,10 +232,10 @@ def put_analysis_report_to_html(args, df_predict, df_accuracy):
|
|
|
<div class="container">
|
|
|
<h1>分析报告</h1>
|
|
|
<!-- 曲线对比 -->
|
|
|
- <div class="plot-container">
|
|
|
- <h2>1. 预测功率与实际功率曲线对比</h2>
|
|
|
- {power_htmls}
|
|
|
- </div>
|
|
|
+ <h2>1. 预测功率与实际功率曲线对比</h2>
|
|
|
+
|
|
|
+ {''.join(power_htmls)}
|
|
|
+
|
|
|
<!-- Pandas DataFrame 表格 -->
|
|
|
<div style="display:flex; justify-content: space-between;">
|
|
|
<h2>2. 准确率对比</h2>
|
|
@@ -257,7 +257,7 @@ def put_analysis_report_to_html(args, df_predict, df_accuracy):
|
|
|
"""
|
|
|
filename = f"{farmId}_{int(time.time() * 1000)}_{random.randint(1000, 9999)}.html"
|
|
|
# 保存为 HTML
|
|
|
- directory = '/usr/share/nginx/html'
|
|
|
+ directory = './test'
|
|
|
if not os.path.exists(directory):
|
|
|
os.makedirs(directory)
|
|
|
file_path = os.path.join(directory, filename)
|
|
@@ -303,5 +303,9 @@ if __name__ == "__main__":
|
|
|
logger = logging.getLogger("analysis_report log")
|
|
|
from waitress import serve
|
|
|
|
|
|
- serve(app, host="0.0.0.0", port=10108)
|
|
|
- print("server start!")
|
|
|
+ # serve(app, host="0.0.0.0", port=10108)
|
|
|
+ # print("server start!")
|
|
|
+ args_dict = {"mongodb_database": 'db_cdq', 'mongodb_read_table': 'j00234_neu_overwrite,j00234_neu_res', 'col_time': 'dateTime',
|
|
|
+ 'label': 'C_REAL_VALUE', 'label_pre': 'power_forecast', 'farmId': 'j00234'}
|
|
|
+ df_predict, df_accuracy = get_df_list_from_mongo(args_dict)[0], get_df_list_from_mongo(args_dict)[1]
|
|
|
+ path = put_analysis_report_to_html(args_dict, df_predict, df_accuracy)
|