diff --git a/pkg/dash/func/info_test.py b/pkg/dash/func/info_test.py index cde2dc8..2009a0a 100644 --- a/pkg/dash/func/info_test.py +++ b/pkg/dash/func/info_test.py @@ -2,6 +2,8 @@ from pkg.dash.func.info_func import * from pkg.dash.app_init import app from dash.dependencies import Input, Output from dash import html +import os +import csv import pandas as pd import re from datetime import timedelta @@ -120,4 +122,86 @@ def update_test_info(n_clicks, test_date, test_time): html.Td("Model prediction does not match actual outcome.", colSpan=6, style={'color': 'red'})])) test_table = html.Table(test_table_rows, style={'width': '100%', 'textAlign': 'left', 'borderCollapse': 'collapse'}) - return [test_table] \ No newline at end of file + return [test_table] + + +def run_loop_test(start_date="2024-10-01", end_date="2025-03-12", interval_hours=1, output_file="test_results.csv"): + est = pytz.timezone('US/Eastern') + + # 生成测试时间序列 + start_dt = pd.to_datetime(start_date).tz_localize(est) + end_dt = pd.to_datetime(end_date).tz_localize(est) + time_points = [] + current_dt = start_dt + while current_dt <= end_dt: + time_points.append(current_dt) + current_dt += timedelta(hours=interval_hours) + + # 准备 CSV 文件 + headers = [ + "Test Date", "Test Time", "Cycle Start", "Cycle End", "Tweet Count at Test Time", + "Actual Final Tweet Count", "Predicted Range Start", "Predicted Range End", + "Probability Min (%)", "Probability Max (%)", "Actual in Range", "Expected Probability (%)", "Note" + ] + + # 如果文件不存在,写入表头 + if not os.path.exists(output_file): + with open(output_file, 'w', newline='') as f: + writer = csv.writer(f) + writer.writerow(headers) + + # 循环测试 + for test_datetime in time_points: + test_date = test_datetime.date().strftime('%Y-%m-%d') + test_time = test_datetime.time().strftime('%H:%M:%S') + n_clicks = 1 # 假设已点击 + + # 调用原始函数 + result = update_test_info(n_clicks, test_date, test_time) + + # 解析结果 + if isinstance(result[0], html.Table): + table = result[0] + rows = table.children + + # 提取数据 + cycle_start = str(rows[0].children[1].children) + test_dt = str(rows[1].children[1].children) + cycle_end = str(rows[2].children[1].children) + tweet_count = int(rows[3].children[1].children) + actual_end_count = int(rows[4].children[1].children) + prob_range = rows[5].children[1].children # 例如 "2.74% - 3.25%" + # 移除 % 符号并转换为浮点数 + prob_min, prob_max = [float(x.strip('%')) for x in prob_range.split(" - ")] + # 从表头提取预测范围 + prob_start, prob_end = map(int, rows[5].children[0].children.split("(")[1].split(")")[0].split("-")) + in_range = rows[6].children[1].children == "Yes" + # 检查是否有 Expected Probability 或 Note + expected_prob = None + note = "" + if len(rows) > 7: + if "Expected" in rows[7].children[0].children: + expected_prob = float(rows[7].children[1].children.split()[0][1:-1]) # 移除 "~" 和 "%" + elif "Note" in rows[7].children[0].children: + note = rows[7].children[1].children + + # 写入 CSV + with open(output_file, 'a', newline='') as f: + writer = csv.writer(f) + writer.writerow([ + test_date, test_time, cycle_start, cycle_end, tweet_count, + actual_end_count, prob_start, prob_end, prob_min, prob_max, + "Yes" if in_range else "No", expected_prob if expected_prob is not None else "", note + ]) + else: + # 如果返回错误信息,也记录 + with open(output_file, 'a', newline='') as f: + writer = csv.writer(f) + writer.writerow([test_date, test_time, "", "", "", "", "", "", "", "", "", "", result[0].children]) + + print(f"Processed: {test_date} {test_time}") + + +# 运行测试 +if __name__ == "__main__": + run_loop_test(start_date="2024-10-01", end_date="2025-03-12", interval_hours=1, output_file="test_results.csv") \ No newline at end of file