#!/usr/bin/env python
# coding=utf-8
"""
Step3e: COPC 校准分析
看预估 ROS 在不同分段下的真实 ROS（校准曲线）
"""
import pandas as pd
from pathlib import Path

# 读取数据
data_file = Path(__file__).parent / "output" / "step3b_整体偏差_合并.csv"
df = pd.read_csv(data_file)

print("=" * 70)
print("Step3e: COPC 校准分析")
print("=" * 70)

# 1. 按预估 ROS 分桶，看真实 ROS
print("\n【1. ROS 校准曲线（按预估值分桶）】")
df['pred_ros_bucket'] = pd.cut(df['pred_ros'], bins=[0, 2, 3, 4, 5, 6, 100],
                               labels=['0-2', '2-3', '3-4', '4-5', '5-6', '>6'])
copc = df.groupby('pred_ros_bucket', observed=True).agg({
    'pred_ros': 'mean',
    'real_ros': 'mean',
    'vid': 'count'
}).round(4)
copc.columns = ['预估ROS均值', '真实ROS均值', '样本数']
copc['COPC'] = (copc['真实ROS均值'] / copc['预估ROS均值']).round(4)
print(copc.to_string())

# 2. 计算整体 COPC
overall_copc = df['real_ros'].mean() / df['pred_ros'].mean()
print(f"\n整体 COPC = {overall_copc:.4f}")
print(f"（理想值为 1.0，<1 表示预估偏高，>1 表示预估偏低）")

# 3. 按预估 STR 分桶
print("\n【2. STR 校准曲线（按预估值分桶）】")
df['pred_str_bucket'] = pd.cut(df['pred_str'], bins=[0, 0.005, 0.008, 0.01, 0.015, 1],
                               labels=['0-0.5%', '0.5-0.8%', '0.8-1%', '1-1.5%', '>1.5%'])
copc_str = df.groupby('pred_str_bucket', observed=True).agg({
    'pred_str': 'mean',
    'real_str': 'mean',
    'vid': 'count'
}).round(6)
copc_str.columns = ['预估STR均值', '真实STR均值', '样本数']
copc_str['COPC'] = (copc_str['真实STR均值'] / copc_str['预估STR均值']).round(4)
print(copc_str.to_string())

overall_copc_str = df['real_str'].mean() / df['pred_str'].mean()
print(f"\nSTR 整体 COPC = {overall_copc_str:.4f}")

# 4. 结论
print("\n" + "=" * 70)
print("【Step3e 结论】")
print("=" * 70)
print(f"ROS COPC = {overall_copc:.4f}（预估偏高 {(1-overall_copc)*100:.1f}%）")
print(f"STR COPC = {overall_copc_str:.4f}（预估偏低 {(overall_copc_str-1)*100:.1f}%）")

print("\nROS 校准问题:")
for idx, row in copc.iterrows():
    if row['COPC'] < 0.8:
        print(f"  - 预估 {idx}: COPC={row['COPC']:.2f}，严重偏高")
    elif row['COPC'] > 1.2:
        print(f"  - 预估 {idx}: COPC={row['COPC']:.2f}，严重偏低")

# 保存结果
output_dir = Path(__file__).parent / "output"
copc.to_csv(output_dir / "step3e_ros_copc.csv")
copc_str.to_csv(output_dir / "step3e_str_copc.csv")

with open(output_dir / "step3e_结论.txt", 'w', encoding='utf-8') as f:
    f.write("=" * 70 + "\n")
    f.write("Step3e: COPC 校准分析\n")
    f.write("=" * 70 + "\n\n")
    f.write(f"ROS 整体 COPC = {overall_copc:.4f}\n")
    f.write(f"STR 整体 COPC = {overall_copc_str:.4f}\n\n")
    f.write("ROS 分桶 COPC:\n")
    f.write(copc.to_string())

print(f"\n结果已保存到 output/ 目录")
