#!/usr/bin/env python
# coding=utf-8
"""
V2 Step3: 问题视频 vs 正常视频对比
找出导致排序失准的真正原因
"""
import pandas as pd
import numpy as np
from pathlib import Path
import glob

output_dir = Path(__file__).parent / "output"

# 读取数据
data_dir = output_dir / "v2_step2_统一口径"
all_files = glob.glob(str(data_dir / "*.csv"))
df = pd.concat([pd.read_csv(f) for f in all_files], ignore_index=True)

print("=" * 70)
print("V2 Step3: 问题视频 vs 正常视频对比分析")
print("=" * 70)

# 1. 定义问题视频：预估排名好但真实排名差
df['pred_rank'] = df.groupby(df.index // (len(df)//7) if len(df) > 7 else 0)['pred_score'].rank(ascending=False)
df['real_rank'] = df.groupby(df.index // (len(df)//7) if len(df) > 7 else 0)['real_score'].rank(ascending=False)

# 简化：用整体排名
df['pred_rank_all'] = df['pred_score'].rank(ascending=False)
df['real_rank_all'] = df['real_score'].rank(ascending=False)
df['rank_diff'] = df['real_rank_all'] - df['pred_rank_all']

# 问题视频：预估Top 25%，但真实排名 Bottom 50%
top_25_pct = df['pred_rank_all'].quantile(0.25)
bottom_50_pct = df['real_rank_all'].quantile(0.50)

df['is_problem'] = (df['pred_rank_all'] <= top_25_pct) & (df['real_rank_all'] > bottom_50_pct)

print(f"\n问题定义: 预估排名 Top 25% 但真实排名 Bottom 50%")
print(f"问题视频数: {df['is_problem'].sum()} / {len(df)} ({df['is_problem'].mean()*100:.1f}%)")

# 2. 对比分析
print("\n【对比分析】")
problem = df[df['is_problem']]
normal = df[~df['is_problem']]

comparison = pd.DataFrame({
    '问题视频': [
        len(problem),
        problem['pred_ros'].mean(),
        problem['real_ros'].mean(),
        problem['ros_bias_pct'].mean(),
        problem['pred_str'].mean(),
        problem['real_str'].mean(),
        problem['str_bias_pct'].mean(),
    ],
    '正常视频': [
        len(normal),
        normal['pred_ros'].mean(),
        normal['real_ros'].mean(),
        normal['ros_bias_pct'].mean(),
        normal['pred_str'].mean(),
        normal['real_str'].mean(),
        normal['str_bias_pct'].mean(),
    ]
}, index=['样本数', '预估ROS', '真实ROS', 'ROS偏差%', '预估STR', '真实STR', 'STR偏差%'])

print(comparison.round(4).to_string())

# 3. 关键对比：问题视频的预估vs真实
print("\n【关键发现】")
if len(problem) > 0:
    print(f"\n问题视频特征:")
    print(f"  预估ROS: {problem['pred_ros'].mean():.2f}")
    print(f"  真实ROS: {problem['real_ros'].mean():.2f}")
    print(f"  ROS偏差: {problem['ros_bias_pct'].mean():+.1f}%")
    print(f"  预估STR: {problem['pred_str'].mean():.6f}")
    print(f"  真实STR: {problem['real_str'].mean():.6f}")
    print(f"  STR偏差: {problem['str_bias_pct'].mean():+.1f}%")

print(f"\n正常视频特征:")
print(f"  预估ROS: {normal['pred_ros'].mean():.2f}")
print(f"  真实ROS: {normal['real_ros'].mean():.2f}")
print(f"  ROS偏差: {normal['ros_bias_pct'].mean():+.1f}%")

# 4. 分析偏差来源
print("\n【偏差来源分析】")
if len(problem) > 0:
    ros_diff = problem['ros_bias_pct'].mean() - normal['ros_bias_pct'].mean()
    str_diff = problem['str_bias_pct'].mean() - normal['str_bias_pct'].mean()

    print(f"问题视频 vs 正常视频:")
    print(f"  ROS偏差差异: {ros_diff:+.1f}%")
    print(f"  STR偏差差异: {str_diff:+.1f}%")

    if abs(ros_diff) > abs(str_diff):
        print(f"\n主要原因: ROS预估问题（差异更大）")
    else:
        print(f"\n主要原因: STR预估问题（差异更大）")

# 5. 真实ROS分布对比
print("\n【真实ROS分布对比】")
if len(problem) > 0:
    print(f"问题视频真实ROS分布:")
    print(f"  < 2: {(problem['real_ros'] < 2).sum()} ({(problem['real_ros'] < 2).mean()*100:.1f}%)")
    print(f"  2-4: {((problem['real_ros'] >= 2) & (problem['real_ros'] < 4)).sum()}")
    print(f"  > 4: {(problem['real_ros'] >= 4).sum()}")

print(f"\n正常视频真实ROS分布:")
print(f"  < 2: {(normal['real_ros'] < 2).sum()} ({(normal['real_ros'] < 2).mean()*100:.1f}%)")
print(f"  2-4: {((normal['real_ros'] >= 2) & (normal['real_ros'] < 4)).sum()}")
print(f"  > 4: {(normal['real_ros'] >= 4).sum()}")

# 6. 结论
print("\n" + "=" * 70)
print("【V2 Step3 结论】")
print("=" * 70)

if len(problem) > 0:
    main_cause = "ROS" if abs(ros_diff) > abs(str_diff) else "STR"
    print(f"""
核心发现:
  1. 问题视频占比: {df['is_problem'].mean()*100:.1f}%
  2. 主要原因: {main_cause} 预估不准
  3. 问题视频特征:
     - {'ROS预估偏高' if problem['ros_bias_pct'].mean() > 0 else 'ROS预估偏低'}: {problem['ros_bias_pct'].mean():+.1f}%
     - {'STR预估偏高' if problem['str_bias_pct'].mean() > 0 else 'STR预估偏低'}: {problem['str_bias_pct'].mean():+.1f}%
  4. 真实ROS < 2 的比例:
     - 问题视频: {(problem['real_ros'] < 2).mean()*100:.1f}%
     - 正常视频: {(normal['real_ros'] < 2).mean()*100:.1f}%
""")
else:
    print("问题视频数量为0，可能定义阈值需要调整")

# 保存
df.to_csv(output_dir / "v2_step3_对比分析.csv", index=False)
