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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
#!/usr/bin/env python3
"""
Interactive Benchmark Explorer
A comprehensive web application for exploring OpenThoughts benchmark correlations and model performance
"""

import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import pearsonr, spearmanr, kendalltau
from scipy.optimize import minimize
import ast
import io
import base64
from itertools import combinations
import warnings
warnings.filterwarnings('ignore')

# Configure page
st.set_page_config(
    page_title="OpenThoughts Evalchemy Benchmark Explorer",
    page_icon="πŸ“Š",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS for better styling
st.markdown("""
<style>
    .main-header {
        font-size: 2.5rem;
        font-weight: bold;
        color: #1f77b4;
        text-align: center;
        margin-bottom: 2rem;
    }
    .metric-card {
        background-color: #f8f9fa;
        padding: 1rem;
        border-radius: 0.5rem;
        border-left: 4px solid #1f77b4;
        margin: 0.5rem 0;
    }
    .correlation-high { color: #d73027; font-weight: bold; }
    .correlation-medium { color: #fdae61; font-weight: bold; }
    .correlation-low { color: #4575b4; font-weight: bold; }
    .category-math { color: #d73027; font-weight: bold; }
    .category-code { color: #1f78b4; font-weight: bold; }
    .category-science { color: #33a02c; font-weight: bold; }
    .category-general { color: #ff7f00; font-weight: bold; }
</style>
""", unsafe_allow_html=True)

@st.cache_data
def load_comprehensive_data():
    """Load and clean the comprehensive benchmark data."""
    try:
        df = pd.read_csv("comprehensive_benchmark_scores.csv", index_col=0)
        
        # Clean the data - handle list-like values stored as strings
        for col in df.columns:
            def extract_value(x):
                if pd.isna(x):
                    return np.nan
                if isinstance(x, str) and x.startswith('['):
                    try:
                        return ast.literal_eval(x)[0]
                    except:
                        return np.nan
                return x
            
            df[col] = df[col].apply(extract_value)
            df[col] = pd.to_numeric(df[col], errors='coerce')
        
        # Filter to only models that have data for at least a few benchmarks
        min_benchmarks = 3
        df = df.dropna(thresh=min_benchmarks, axis=0)
        
        return df
    except FileNotFoundError:
        st.error("Could not find comprehensive_benchmark_scores.csv. Please ensure the data file exists.")
        return pd.DataFrame()

@st.cache_data
def load_stderr_data():
    """Load and clean standard error data."""
    try:
        stderr_df = pd.read_csv("benchmark_standard_errors.csv", index_col=0)
        
        # Clean the data
        for col in stderr_df.columns:
            def extract_value(x):
                if pd.isna(x):
                    return np.nan
                if isinstance(x, str) and x.startswith('['):
                    try:
                        return ast.literal_eval(x)[0]
                    except:
                        return np.nan
                return x
            
            stderr_df[col] = stderr_df[col].apply(extract_value)
            stderr_df[col] = pd.to_numeric(stderr_df[col], errors='coerce')
        
        return stderr_df
    except FileNotFoundError:
        return None

def clean_benchmark_name(name):
    """Clean benchmark names for consistent display."""
    return (name.replace("LiveCodeBench_accuracy_avg", "LiveCodeBenchv2")
            .replace('_accuracy_avg', '')
            .replace('_accuracy', '')
            .replace('LiveCodeBench', 'LCB')
            .replace('GPQADiamond', 'GPQAD')
            )

def get_focused_benchmark_mapping():
    """Define the target benchmarks and categories."""
    target_benchmarks = {
        # Math benchmarks
        'AIME24': 'AIME24_accuracy_avg',
        'AIME25': 'AIME25_accuracy_avg', 
        'AMC23': 'AMC23_accuracy_avg',
        'MATH500': 'MATH500_accuracy',
        
        # Code benchmarks
        'CodeElo': 'CodeElo_accuracy_avg',
        'CodeForces': 'CodeForces_accuracy_avg',
        'LCBv2': 'LiveCodeBench_accuracy_avg',
        'LCBv5': 'LiveCodeBenchv5_accuracy_avg',
        
        # Science benchmarks
        'GPQADiamond': 'GPQADiamond_accuracy_avg',
        'JEEBench': 'JEEBench_accuracy_avg',
        
        # General benchmarks
        'MMLUPro': 'MMLUPro_accuracy_avg',
        'HLE': 'HLE_accuracy_avg'
    }
    
    benchmark_categories = {
        'Math': ['AIME24', 'AIME25', 'AMC23', 'MATH500'],
        'Code': ['CodeElo', 'CodeForces', 'LCBv2', 'LCBv5'],
        'Science': ['GPQADiamond', 'JEEBench'], 
        'General': ['MMLUPro', 'HLE']
    }
    
    colors = {'Math': '#d73027', 'Code': '#1f78b4', 'Science': '#33a02c', 'General': '#ff7f00'}
    
    # Create reverse mapping
    col_to_category = {}
    for category, bench_list in benchmark_categories.items():
        for bench_name in bench_list:
            actual_name = target_benchmarks.get(bench_name)
            if actual_name:
                col_to_category[actual_name] = category
    
    return target_benchmarks, benchmark_categories, colors, col_to_category

def compute_correlations(df, method='pearson'):
    """Compute correlation matrix with the specified method."""
    if method == 'pearson':
        return df.corr(method='pearson')
    elif method == 'spearman':
        return df.corr(method='spearman')
    elif method == 'kendall':
        return df.corr(method='kendall')

def create_interactive_heatmap(corr_matrix, title="Correlation Heatmap"):
    """Create an interactive correlation heatmap using Plotly."""
    target_benchmarks, benchmark_categories, colors, col_to_category = get_focused_benchmark_mapping()
    
    # Get clean names for display
    clean_names = [clean_benchmark_name(name) for name in corr_matrix.columns]
    
    # Convert to percentages for display
    corr_matrix_pct = (corr_matrix * 100).round(1)
    
    # Create hover text
    hover_text = []
    for i, bench1 in enumerate(corr_matrix.columns):
        hover_row = []
        for j, bench2 in enumerate(corr_matrix.columns):
            if i == j:
                hover_row.append(f"{clean_names[i]}<br>Reliability: 100%")
            else:
                corr_val = corr_matrix_pct.iloc[i, j]
                if pd.isna(corr_val):
                    hover_row.append(f"{clean_names[i]} vs {clean_names[j]}<br>No data")
                else:
                    hover_row.append(f"{clean_names[i]} vs {clean_names[j]}<br>Correlation: {corr_val:.1f}%")
        hover_text.append(hover_row)
    
    # Create the heatmap
    fig = go.Figure(data=go.Heatmap(
        z=corr_matrix.values,
        x=clean_names,
        y=clean_names,
        colorscale='RdBu_r',
        zmid=0,
        text=corr_matrix_pct.values,
        texttemplate="%{text}",
        textfont={"size": 10},
        hoverinfo='text',
        hovertext=hover_text,
        colorbar=dict(title="Correlation", tickformat=".2f")
    ))
    
    # Update layout
    fig.update_layout(
        title=title,
        xaxis_title="",
        yaxis_title="",
        width=800,
        height=800,
        font=dict(size=12)
    )
    
    # Color the axis labels by category
    for i, bench in enumerate(corr_matrix.columns):
        category = col_to_category.get(bench, 'Unknown')
        color = colors.get(category, 'black')
    
    return fig

def create_scatter_plot(df, x_bench, y_bench, stderr_df=None):
    """Create an interactive scatter plot between two benchmarks."""
    if x_bench not in df.columns or y_bench not in df.columns:
        return None
    
    # Get common data
    common_data = df[[x_bench, y_bench]].dropna()
    
    if len(common_data) < 3:
        return None
    
    x_vals = common_data[x_bench]
    y_vals = common_data[y_bench]
    
    # Calculate correlation
    corr, p_val = pearsonr(x_vals, y_vals)
    
    # Create figure
    fig = go.Figure()
    
    # Add scatter points
    fig.add_trace(go.Scatter(
        x=x_vals,
        y=y_vals,
        mode='markers',
        text=common_data.index,
        hovertemplate=(
            "<b>%{text}</b><br>" +
            f"{clean_benchmark_name(x_bench)}: %{{x:.3f}}<br>" +
            f"{clean_benchmark_name(y_bench)}: %{{y:.3f}}<br>" +
            "<extra></extra>"
        ),
        marker=dict(size=8, opacity=0.7, color='steelblue')
    ))
    
    # Add regression line
    z = np.polyfit(x_vals, y_vals, 1)
    p = np.poly1d(z)
    x_line = np.linspace(x_vals.min(), x_vals.max(), 100)
    
    fig.add_trace(go.Scatter(
        x=x_line,
        y=p(x_line),
        mode='lines',
        name=f'r = {corr:.3f}, p = {p_val:.3f}',
        line=dict(color='red', dash='dash')
    ))
    
    # Update layout
    fig.update_layout(
        title=f"{clean_benchmark_name(y_bench)} vs {clean_benchmark_name(x_bench)}",
        xaxis_title=clean_benchmark_name(x_bench),
        yaxis_title=clean_benchmark_name(y_bench),
        showlegend=True,
        width=600,
        height=500
    )
    
    return fig

def filter_target_benchmarks(df):
    """Filter dataframe to only include target benchmarks."""
    target_benchmarks, _, _, _ = get_focused_benchmark_mapping()
    
    available_benchmarks = []
    for display_name, actual_name in target_benchmarks.items():
        if actual_name in df.columns:
            available_benchmarks.append(actual_name)
    
    return df[available_benchmarks].copy()

def main():
    """Main application."""
    
    # Header
    st.markdown('<div class="main-header">πŸ”¬ OpenThoughts Evalchemy Benchmark Explorer</div>', unsafe_allow_html=True)
    st.markdown("**Explore correlations and relationships between OpenThoughts model performance across different benchmarks**")
    
    # Load data
    with st.spinner("Loading benchmark data..."):
        df = load_comprehensive_data()
        stderr_df = load_stderr_data()
    
    if df.empty:
        st.error("No data available. Please check that the data files exist.")
        return
    
    # Filter to target benchmarks
    df_filtered = filter_target_benchmarks(df)
    target_benchmarks, benchmark_categories, colors, col_to_category = get_focused_benchmark_mapping()
    
    # Sidebar
    st.sidebar.header("πŸŽ›οΈ Controls")
    
    # Analysis mode selection
    analysis_mode = st.sidebar.selectbox(
        "Choose Analysis Mode",
        ["πŸ“Š Overview Dashboard", "πŸ”₯ Interactive Heatmap", "πŸ“ˆ Scatter Plot Explorer", 
         "🎯 Model Performance", "πŸ“‹ Statistical Summary", "πŸ”¬ Uncertainty Analysis"]
    )
    
    # Data filtering options
    st.sidebar.subheader("Data Filters")
    
    # Category filter
    selected_categories = st.sidebar.multiselect(
        "Select Benchmark Categories",
        list(benchmark_categories.keys()),
        default=list(benchmark_categories.keys())
    )
    
    # Filter benchmarks based on selected categories
    filtered_benchmarks = []
    for category in selected_categories:
        for bench_name in benchmark_categories[category]:
            actual_name = target_benchmarks.get(bench_name)
            if actual_name in df_filtered.columns:
                filtered_benchmarks.append(actual_name)
    
    if filtered_benchmarks:
        df_display = df_filtered[filtered_benchmarks].copy()
    else:
        df_display = df_filtered.copy()
    
    # Zero filtering
    filter_zeros = st.sidebar.checkbox("Filter out zero/near-zero values", value=False)
    if filter_zeros:
        for col in df_display.columns:
            df_display.loc[(df_display[col] == 0) | (df_display[col] < 0.01), col] = np.nan
    
    # Minimum data points filter
    coverage_counts = [df_display[col].notna().sum() for col in df_display.columns]
    if coverage_counts:
        min_coverage = min(coverage_counts)
        max_coverage = max(coverage_counts)
        default_min = max(10, min_coverage)  # Default to at least 10 or minimum available
        
        min_models = st.sidebar.slider(
            "Minimum models per benchmark", 
            min_value=min_coverage, 
            max_value=max_coverage, 
            value=default_min,
            help=f"Range: {min_coverage} to {max_coverage} models"
        )
    else:
        min_models = 10
    
    # Apply the minimum models filter
    valid_benchmarks = []
    for col in df_display.columns:
        if df_display[col].notna().sum() >= min_models:
            valid_benchmarks.append(col)
    df_display = df_display[valid_benchmarks]
    
    # Main content based on analysis mode
    if analysis_mode == "πŸ“Š Overview Dashboard":
        show_overview_dashboard(df_display, stderr_df)
    
    elif analysis_mode == "πŸ”₯ Interactive Heatmap":
        show_interactive_heatmap(df_display)
    
    elif analysis_mode == "πŸ“ˆ Scatter Plot Explorer":
        show_scatter_explorer(df_display, stderr_df)
    
    elif analysis_mode == "🎯 Model Performance":
        show_model_performance(df_display)
    
    elif analysis_mode == "πŸ“‹ Statistical Summary":
        show_statistical_summary(df_display)
    
    elif analysis_mode == "πŸ”¬ Uncertainty Analysis":
        show_uncertainty_analysis(df_display, stderr_df)

def show_overview_dashboard(df, stderr_df):
    """Show the overview dashboard."""
    st.header("πŸ“Š Overview Dashboard")
    
    # Key metrics
    col1, col2, col3, col4 = st.columns(4)
    
    with col1:
        st.metric("Models", len(df))
    
    with col2:
        st.metric("Benchmarks", len(df.columns))
    
    with col3:
        total_evals = df.notna().sum().sum()
        st.metric("Total Evaluations", f"{total_evals:,}")
    
    with col4:
        avg_coverage = (df.notna().sum() / len(df)).mean() * 100
        st.metric("Avg Coverage", f"{avg_coverage:.1f}%")
    
    # Benchmark coverage chart
    st.subheader("Benchmark Coverage")
    
    coverage_data = []
    target_benchmarks, benchmark_categories, colors, col_to_category = get_focused_benchmark_mapping()
    
    for col in df.columns:
        coverage = df[col].notna().sum()
        category = col_to_category.get(col, 'Unknown')
        clean_name = clean_benchmark_name(col)
        coverage_data.append({
            'Benchmark': clean_name,
            'Coverage': coverage,
            'Percentage': coverage / len(df) * 100,
            'Category': category
        })
    
    coverage_df = pd.DataFrame(coverage_data).sort_values('Coverage', ascending=True)
    
    fig = px.bar(coverage_df, 
                 x='Coverage', 
                 y='Benchmark',
                 color='Category',
                 color_discrete_map=colors,
                 title="Model Coverage by Benchmark",
                 labels={'Coverage': 'Number of Models'},
                 orientation='h')
    
    fig.update_layout(height=400)
    st.plotly_chart(fig, use_container_width=True)
    
    # Quick correlation insights
    st.subheader("Quick Correlation Insights")
    
    corr_matrix = compute_correlations(df, 'pearson')
    
    # Get top correlations
    pairs = []
    for i, bench1 in enumerate(corr_matrix.columns):
        for j, bench2 in enumerate(corr_matrix.columns[i+1:], i+1):
            if not pd.isna(corr_matrix.iloc[i, j]):
                cat1 = col_to_category.get(bench1, 'Unknown')
                cat2 = col_to_category.get(bench2, 'Unknown')
                pairs.append((bench1, bench2, corr_matrix.iloc[i, j], cat1, cat2))
    
    pairs.sort(key=lambda x: abs(x[2]), reverse=True)
    
    col1, col2 = st.columns(2)
    
    with col1:
        st.markdown("**πŸ”₯ Top 5 Highest Correlations**")
        for i, (bench1, bench2, corr, cat1, cat2) in enumerate(pairs[:5]):
            same_cat = "βœ…" if cat1 == cat2 else "πŸ”€"
            st.write(f"{i+1}. {clean_benchmark_name(bench1)} ↔ {clean_benchmark_name(bench2)}")
            st.write(f"   r = {corr:.3f} {same_cat}")
    
    with col2:
        st.markdown("**πŸ“Š Category Analysis**")
        within_cat = [p[2] for p in pairs if p[3] == p[4]]
        across_cat = [p[2] for p in pairs if p[3] != p[4]]
        
        if within_cat:
            st.write(f"Within-category avg: {np.mean(within_cat):.3f}")
        if across_cat:
            st.write(f"Across-category avg: {np.mean(across_cat):.3f}")
        
        st.write(f"Total pairs analyzed: {len(pairs)}")

def show_interactive_heatmap(df):
    """Show the interactive heatmap."""
    st.header("πŸ”₯ Interactive Correlation Heatmap")
    
    # Correlation method selection
    col1, col2 = st.columns([3, 1])
    
    with col2:
        corr_method = st.selectbox(
            "Correlation Method",
            ["pearson", "spearman", "kendall"]
        )
    
    # Compute correlation matrix
    corr_matrix = compute_correlations(df, corr_method)
    
    # Create and display heatmap
    fig = create_interactive_heatmap(corr_matrix, f"{corr_method.capitalize()} Correlation Matrix")
    st.plotly_chart(fig, use_container_width=True)
    
    # Correlation statistics
    st.subheader("Correlation Statistics")
    
    # Get all off-diagonal correlations
    mask = np.triu(np.ones_like(corr_matrix, dtype=bool), k=1)
    corr_values = corr_matrix.where(mask).stack().dropna()
    
    col1, col2, col3, col4 = st.columns(4)
    
    with col1:
        st.metric("Mean Correlation", f"{corr_values.mean():.3f}")
    
    with col2:
        st.metric("Median Correlation", f"{corr_values.median():.3f}")
    
    with col3:
        st.metric("Max Correlation", f"{corr_values.max():.3f}")
    
    with col4:
        st.metric("Min Correlation", f"{corr_values.min():.3f}")
    
    # Distribution of correlations
    st.subheader("Correlation Distribution")
    
    fig = px.histogram(corr_values, 
                       nbins=20,
                       title="Distribution of Pairwise Correlations",
                       labels={'value': 'Correlation Coefficient', 'count': 'Frequency'})
    st.plotly_chart(fig, use_container_width=True)

def show_scatter_explorer(df, stderr_df):
    """Show the scatter plot explorer."""
    st.header("πŸ“ˆ Scatter Plot Explorer")
    
    # Benchmark selection
    col1, col2 = st.columns(2)
    
    with col1:
        x_benchmark = st.selectbox(
            "X-axis Benchmark",
            df.columns,
            format_func=clean_benchmark_name
        )
    
    with col2:
        y_benchmark = st.selectbox(
            "Y-axis Benchmark", 
            df.columns,
            index=1 if len(df.columns) > 1 else 0,
            format_func=clean_benchmark_name
        )
    
    if x_benchmark and y_benchmark and x_benchmark != y_benchmark:
        # Create scatter plot
        fig = create_scatter_plot(df, x_benchmark, y_benchmark, stderr_df)
        
        if fig:
            st.plotly_chart(fig, use_container_width=True)
            
            # Additional statistics
            common_data = df[[x_benchmark, y_benchmark]].dropna()
            
            if len(common_data) >= 3:
                col1, col2, col3 = st.columns(3)
                
                # Correlation metrics
                pearson_r, pearson_p = pearsonr(common_data[x_benchmark], common_data[y_benchmark])
                spearman_r, spearman_p = spearmanr(common_data[x_benchmark], common_data[y_benchmark])
                kendall_r, kendall_p = kendalltau(common_data[x_benchmark], common_data[y_benchmark])
                
                with col1:
                    st.metric("Pearson r", f"{pearson_r:.3f}")
                    st.caption(f"p = {pearson_p:.3f}")
                
                with col2:
                    st.metric("Spearman ρ", f"{spearman_r:.3f}")
                    st.caption(f"p = {spearman_p:.3f}")
                
                with col3:
                    st.metric("Kendall Ο„", f"{kendall_r:.3f}")
                    st.caption(f"p = {kendall_p:.3f}")
                
                # Show data table
                st.subheader("Data Points")
                display_data = common_data.copy()
                display_data.columns = [clean_benchmark_name(col) for col in display_data.columns]
                st.dataframe(display_data, use_container_width=True)
        else:
            st.warning("Insufficient data for the selected benchmark pair.")
    else:
        st.info("Please select two different benchmarks to compare.")

def show_model_performance(df):
    """Show model performance analysis."""
    st.header("🎯 Model Performance Analysis")
    
    # Model search
    search_term = st.text_input("πŸ” Search for models", placeholder="Enter model name or part of name")
    
    if search_term:
        matching_models = df.index[df.index.str.contains(search_term, case=False, na=False)]
        if len(matching_models) > 0:
            df_display = df.loc[matching_models]
        else:
            st.warning(f"No models found matching '{search_term}'")
            df_display = df
    else:
        df_display = df
    
    # Performance ranking
    st.subheader("Model Rankings")
    
    # Calculate average performance (excluding NaN)
    model_avg_scores = df_display.mean(axis=1, skipna=True).sort_values(ascending=False)
    
    # Top performers
    col1, col2 = st.columns(2)
    
    with col1:
        st.markdown("**πŸ† Top 10 Models (by average score)**")
        for i, (model, score) in enumerate(model_avg_scores.head(10).items()):
            st.write(f"{i+1}. {model.split('/')[-1]}: {score:.3f}")
    
    with col2:
        st.markdown("**πŸ“Š Performance Distribution**")
        fig = px.histogram(model_avg_scores, 
                          nbins=20,
                          title="Distribution of Average Model Scores")
        st.plotly_chart(fig, use_container_width=True)
    
    # Model comparison
    st.subheader("Model Comparison")
    
    selected_models = st.multiselect(
        "Select models to compare",
        df_display.index.tolist(),
        default=model_avg_scores.head(3).index.tolist()
    )
    
    if selected_models:
        comparison_data = df_display.loc[selected_models].T
        comparison_data.index = [clean_benchmark_name(idx) for idx in comparison_data.index]
        
        # Radar chart
        if len(selected_models) <= 5:  # Only for manageable number of models
            fig = go.Figure()
            
            for model in selected_models:
                model_data = df_display.loc[model].dropna()
                benchmarks = [clean_benchmark_name(b) for b in model_data.index]
                values = model_data.values.tolist()
                
                # Close the radar chart
                values += values[:1]
                benchmarks += benchmarks[:1]
                
                fig.add_trace(go.Scatterpolar(
                    r=values,
                    theta=benchmarks,
                    fill='toself',
                    name=model.split('/')[-1]
                ))
            
            fig.update_layout(
                polar=dict(
                    radialaxis=dict(
                        visible=True,
                        range=[0, 1]
                    )),
                showlegend=True,
                title="Model Performance Radar Chart"
            )
            
            st.plotly_chart(fig, use_container_width=True)
        
        # Detailed comparison table
        st.subheader("Detailed Comparison")
        st.dataframe(comparison_data, use_container_width=True)

def show_statistical_summary(df):
    """Show statistical summary."""
    st.header("πŸ“‹ Statistical Summary")
    
    # Overall statistics
    st.subheader("Dataset Statistics")
    
    col1, col2 = st.columns(2)
    
    with col1:
        st.markdown("**Data Coverage**")
        total_possible = len(df) * len(df.columns)
        total_actual = df.notna().sum().sum()
        coverage_pct = (total_actual / total_possible) * 100
        
        st.write(f"Total possible evaluations: {total_possible:,}")
        st.write(f"Actual evaluations: {total_actual:,}")
        st.write(f"Overall coverage: {coverage_pct:.1f}%")
    
    with col2:
        st.markdown("**Score Statistics**")
        all_scores = df.values.flatten()
        all_scores = all_scores[~pd.isna(all_scores)]
        
        st.write(f"Mean score: {np.mean(all_scores):.3f}")
        st.write(f"Median score: {np.median(all_scores):.3f}")
        st.write(f"Std deviation: {np.std(all_scores):.3f}")
    
    # Benchmark-wise statistics
    st.subheader("Benchmark Statistics")
    
    benchmark_stats = []
    target_benchmarks, benchmark_categories, colors, col_to_category = get_focused_benchmark_mapping()
    
    for col in df.columns:
        scores = df[col].dropna()
        if len(scores) > 0:
            benchmark_stats.append({
                'Benchmark': clean_benchmark_name(col),
                'Category': col_to_category.get(col, 'Unknown'),
                'Count': len(scores),
                'Mean': scores.mean(),
                'Median': scores.median(),
                'Std': scores.std(),
                'Min': scores.min(),
                'Max': scores.max(),
                'Range': scores.max() - scores.min()
            })
    
    stats_df = pd.DataFrame(benchmark_stats)
    st.dataframe(stats_df, use_container_width=True)
    
    # Correlation summary
    st.subheader("Correlation Analysis Summary")
    
    for method in ['pearson', 'spearman', 'kendall']:
        corr_matrix = compute_correlations(df, method)
        
        # Get all off-diagonal correlations
        mask = np.triu(np.ones_like(corr_matrix, dtype=bool), k=1)
        corr_values = corr_matrix.where(mask).stack().dropna()
        
        st.write(f"**{method.capitalize()} Correlations:**")
        col1, col2, col3, col4 = st.columns(4)
        
        with col1:
            st.metric("Mean", f"{corr_values.mean():.3f}")
        with col2:
            st.metric("Median", f"{corr_values.median():.3f}")
        with col3:
            st.metric("Max", f"{corr_values.max():.3f}")
        with col4:
            st.metric("Min", f"{corr_values.min():.3f}")

def show_uncertainty_analysis(df, stderr_df):
    """Show uncertainty analysis if standard error data is available."""
    st.header("πŸ”¬ Uncertainty Analysis")
    
    if stderr_df is None:
        st.warning("Standard error data not available. This analysis requires benchmark_standard_errors.csv")
        return
    
    st.info("This section analyzes measurement uncertainty and reliability of benchmark evaluations.")
    
    # Match benchmarks with standard errors
    matched_benchmarks = []
    for score_col in df.columns:
        # Try to find matching stderr column
        potential_stderr_cols = [
            f"{score_col}_std_err",
            f"{score_col.replace('_accuracy', '_accuracy_std_err')}",
            f"{score_col.replace('_accuracy_avg', '_accuracy_std_err')}"
        ]
        
        for stderr_col in potential_stderr_cols:
            if stderr_col in stderr_df.columns:
                matched_benchmarks.append((score_col, stderr_col))
                break
    
    if not matched_benchmarks:
        st.warning("No matching standard error data found for the selected benchmarks.")
        return
    
    st.success(f"Found standard error data for {len(matched_benchmarks)} benchmarks.")
    
    # Measurement precision analysis
    st.subheader("Measurement Precision")
    
    precision_data = []
    for score_col, stderr_col in matched_benchmarks:
        scores = df[score_col].dropna()
        stderrs = stderr_df[stderr_col].dropna()
        
        if len(stderrs) > 0:
            mean_stderr = stderrs.mean()
            median_stderr = stderrs.median()
            
            # Signal-to-noise ratio
            if len(scores) > 0:
                signal_std = scores.std()
                snr = signal_std / mean_stderr if mean_stderr > 0 else float('inf')
            else:
                snr = 0
            
            precision_data.append({
                'Benchmark': clean_benchmark_name(score_col),
                'Mean StdErr': mean_stderr,
                'Median StdErr': median_stderr,
                'Signal/Noise': snr,
                'N Models': len(stderrs)
            })
    
    if precision_data:
        precision_df = pd.DataFrame(precision_data)
        st.dataframe(precision_df, use_container_width=True)
        
        # Visualization
        fig = px.scatter(precision_df, 
                        x='Mean StdErr', 
                        y='Signal/Noise',
                        size='N Models',
                        hover_name='Benchmark',
                        title="Measurement Precision: Signal-to-Noise vs Standard Error",
                        labels={'Signal/Noise': 'Signal-to-Noise Ratio'})
        st.plotly_chart(fig, use_container_width=True)
    
    # Uncertainty-aware scatter plot
    st.subheader("Uncertainty-Aware Scatter Plot")
    
    # Let user select benchmarks with stderr data
    available_benchmarks = [score_col for score_col, _ in matched_benchmarks]
    
    col1, col2 = st.columns(2)
    
    with col1:
        x_bench = st.selectbox(
            "X-axis Benchmark (with uncertainty)",
            available_benchmarks,
            format_func=clean_benchmark_name
        )
    
    with col2:
        y_bench = st.selectbox(
            "Y-axis Benchmark (with uncertainty)",
            available_benchmarks,
            index=1 if len(available_benchmarks) > 1 else 0,
            format_func=clean_benchmark_name
        )
    
    if x_bench and y_bench and x_bench != y_bench:
        # Find corresponding stderr columns
        x_stderr_col = None
        y_stderr_col = None
        
        for score_col, stderr_col in matched_benchmarks:
            if score_col == x_bench:
                x_stderr_col = stderr_col
            if score_col == y_bench:
                y_stderr_col = stderr_col
        
        if x_stderr_col and y_stderr_col:
            # Get data
            x_scores = df[x_bench]
            y_scores = df[y_bench]
            x_err = stderr_df[x_stderr_col]
            y_err = stderr_df[y_stderr_col]
            
            # Find common valid data
            valid_mask = ~(x_scores.isna() | y_scores.isna() | x_err.isna() | y_err.isna())
            
            if valid_mask.sum() >= 3:
                x_clean = x_scores[valid_mask]
                y_clean = y_scores[valid_mask]
                x_err_clean = x_err[valid_mask]
                y_err_clean = y_err[valid_mask]
                
                # Create uncertainty scatter plot
                fig = go.Figure()
                
                # Add error bars
                fig.add_trace(go.Scatter(
                    x=x_clean,
                    y=y_clean,
                    error_x=dict(
                        type='data',
                        array=1.96 * x_err_clean,  # 95% CI
                        visible=True
                    ),
                    error_y=dict(
                        type='data',
                        array=1.96 * y_err_clean,  # 95% CI
                        visible=True
                    ),
                    mode='markers',
                    text=x_clean.index,
                    hovertemplate=(
                        "<b>%{text}</b><br>" +
                        f"{clean_benchmark_name(x_bench)}: %{{x:.3f}} Β± %{{error_x:.3f}}<br>" +
                        f"{clean_benchmark_name(y_bench)}: %{{y:.3f}} Β± %{{error_y:.3f}}<br>" +
                        "<extra></extra>"
                    ),
                    marker=dict(size=8, opacity=0.7),
                    name='Models'
                ))
                
                # Add regression line
                corr, p_val = pearsonr(x_clean, y_clean)
                z = np.polyfit(x_clean, y_clean, 1)
                p = np.poly1d(z)
                x_line = np.linspace(x_clean.min(), x_clean.max(), 100)
                
                fig.add_trace(go.Scatter(
                    x=x_line,
                    y=p(x_line),
                    mode='lines',
                    name=f'r = {corr:.3f}, p = {p_val:.3f}',
                    line=dict(color='red', dash='dash')
                ))
                
                fig.update_layout(
                    title=f"Uncertainty-Aware Correlation: {clean_benchmark_name(y_bench)} vs {clean_benchmark_name(x_bench)}",
                    xaxis_title=f"{clean_benchmark_name(x_bench)} (Β±95% CI)",
                    yaxis_title=f"{clean_benchmark_name(y_bench)} (Β±95% CI)",
                    showlegend=True
                )
                
                st.plotly_chart(fig, use_container_width=True)
                
                st.info(f"Showing {len(x_clean)} models with both score and uncertainty data. Error bars represent 95% confidence intervals.")
            else:
                st.warning("Insufficient data with uncertainty estimates for the selected benchmark pair.")

if __name__ == "__main__":
    main()