# plot_radar.py
import pandas as pd
import numpy as np
import plotly.graph_objects as go

# Read CSV
df = pd.read_csv("longbench.csv")

# Clean model names (first column header looks like 'Model' with no space)
# Ensure header name — adapt if needed:
if "Model" not in df.columns:
    # if the file used "Model,Params..." without exact header spacing
    # try the first column name as model
    model_col = df.columns[0]
else:
    model_col = "Model"

# Columns to include in radar (order preserved)
radar_cols = [
#    "Overall (%)", 
 "Overall (%) w/ CoT",
#    "Easy (%)",    
"Easy (%) w/ CoT",
#    "Hard (%)",    
"Hard (%) w/ CoT",
#    "Short (%)",   
"Short (%)w/ CoT",
#    "Medium (%)",  
"Medium (%) w/ CoT",
#    "Long (%)",    
"Long (%) w/ CoT"
]

# Some CSVs may have slight header typos (e.g., "Short (%)w/ CoT" missing space).
# Map existing columns to desired names
col_map = {}
for c in df.columns:
    for target in radar_cols:
        if c.replace(" ", "").lower() == target.replace(" ", "").lower():
            col_map[c] = target
df = df.rename(columns=col_map)

# Convert '-' to NaN and numeric strings to float
for c in radar_cols:
    if c in df.columns:
        df[c] = pd.to_numeric(df[c].replace("-", np.nan), errors="coerce")

# Prepare categories for the radar (Plotly requires the first and last category to be the same to close the polygon)
categories = radar_cols.copy()
# Ensure categories exist in df
categories = [c for c in categories if c in df.columns]
if not categories:
    raise SystemExit("No radar columns found in CSV. Check headers.")

categories_closed = categories + [categories[0]]

# Build traces
fig = go.Figure()
for _, row in df.iterrows():
    name = str(row[model_col])
    values = [row[c] if not pd.isna(row[c]) else None for c in categories]
    # Replace None with 0 or interpolate? Leave None to create gaps. Here leave NaN -> Plotly will close line but may drop None.
    values_closed = values + [values[0]]
    fig.add_trace(go.Scatterpolar(
        r=values_closed,
        theta=categories_closed,
        fill='toself',
        name=name,
        hovertemplate='%{theta}: %{r}<extra>'+name+'</extra>'
    ))

# Layout
fig.update_layout(
    title="LongBench: Model comparison (radar)",
    polar=dict(
        radialaxis=dict(
            visible=True,
            range=[0, 100]
        )
    ),
    legend=dict(orientation="h", yanchor="bottom", y=-0.15, xanchor="center", x=0.5)
)

#fig.show()
fig.write_html("radar.html")
