File size: 2,446 Bytes
ed881c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Leaderboard visualization functions."""

import pandas as pd
import plotly.express as px
from typing import Dict, List, Tuple
from plotly.graph_objects import Figure

from app.data import TASK_DATA

def create_leaderboard_table(data: Dict[str, List], selected_models: List[str] = None) -> pd.DataFrame:
    """Create a formatted DataFrame for the leaderboard."""
    df = pd.DataFrame(data)
    
    if selected_models:
        df = df[df['Model'].isin(selected_models)]
    
    # Format numeric columns to 1 decimal place
    numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
    df[numeric_cols] = df[numeric_cols].apply(lambda x: x.apply(lambda y: f"{y:.1f}"))
    
    return df

def create_performance_plot(data: Dict[str, List], task: str, metric: str, selected_models: List[str] = None) -> Figure:
    """Create a bar plot showing model performance."""
    df = pd.DataFrame(data)
    
    if selected_models:
        df = df[df['Model'].isin(selected_models)]
    
    fig = px.bar(
        df,
        x="Model",
        y=metric,
        color="Dataset",
        title=f"{task} - {metric}",
        barmode="group",
    )
    fig.update_layout(
        xaxis_title="Model",
        yaxis_title=metric,
        showlegend=True,
    )
    return fig

def get_best_models(data: Dict[str, List], metrics: List[str]) -> pd.DataFrame:
    """Get the best performing models for each metric across datasets."""
    df = pd.DataFrame(data)
    best_models = []
    
    for metric in metrics:
        top_models = df.groupby('Model')[metric].mean().sort_values(ascending=False).head(3)
        best_models.extend([
            {
                'Metric': metric,
                'Rank': rank,
                'Model': model,
                'Average Score': f"{score:.1f}"
            }
            for rank, (model, score) in enumerate(top_models.items(), 1)
        ])
    
    return pd.DataFrame(best_models)

def update_leaderboard(task: str, selected_models: List[str] = None) -> Tuple[pd.DataFrame, Figure, Figure, pd.DataFrame]:
    """Update the leaderboard based on selected task and models."""
    data, metrics = TASK_DATA[task]
    return (
        create_leaderboard_table(data, selected_models),
        *[create_performance_plot(data, task, metric, selected_models) for metric in metrics],
        get_best_models(data, metrics)
    )