File size: 6,506 Bytes
10ad72f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objects as go


def setup_matplotlib():
    """Set up matplotlib configuration."""
    matplotlib.use("Agg")
    plt.close("all")


def get_performance_chart(df, category_name="Overall"):
    plt.close("all")
    score_column = "Category Score"
    df_sorted = df.sort_values(score_column, ascending=True)
    colors = {"Private": "#4F46E5", "Open source": "#16A34A"}

    height = max(8, len(df_sorted) * 0.8)
    fig, ax = plt.subplots(figsize=(16, height))
    plt.rcParams.update({"font.size": 12})

    try:
        bars = ax.barh(
            np.arange(len(df_sorted)),
            df_sorted[score_column],
            height=0.6,
            color=[colors[t] for t in df_sorted["Model Type"]],
        )

        ax.set_title(
            f"Model Performance Comparison - {category_name}",
            pad=20,
            fontsize=20,
            fontweight="bold",
        )
        ax.set_xlabel("Average Score", fontsize=14, labelpad=10)
        ax.set_xlim(0.0, 1.0)

        ax.set_yticks(np.arange(len(df_sorted)))
        ax.set_yticklabels(df_sorted["Model"], fontsize=12)

        plt.subplots_adjust(left=0.35)

        for i, v in enumerate(df_sorted[score_column]):
            ax.text(
                v + 0.01, i, f"{v:.3f}", va="center", fontsize=12, fontweight="bold"
            )

        ax.grid(True, axis="x", linestyle="--", alpha=0.2)
        ax.spines[["top", "right"]].set_visible(False)

        legend_elements = [
            plt.Rectangle((0, 0), 1, 1, facecolor=color, label=label)
            for label, color in colors.items()
        ]
        ax.legend(
            handles=legend_elements,
            title="Model Type",
            loc="lower right",
            fontsize=12,
            title_fontsize=14,
        )

        plt.tight_layout()
        return fig
    finally:
        plt.close(fig)


def create_radar_plot(df, model_names):
    datasets = [col for col in df.columns[7:] if col != "IO Cost"]
    fig = go.Figure()

    colors = ["rgba(99, 102, 241, 0.3)", "rgba(34, 197, 94, 0.3)"]
    line_colors = ["#4F46E5", "#16A34A"]

    for idx, model_name in enumerate(model_names):
        model_data = df[df["Model"] == model_name].iloc[0]
        values = [model_data[m] for m in datasets]
        values.append(values[0])
        datasets_plot = datasets + [datasets[0]]

        fig.add_trace(
            go.Scatterpolar(
                r=values,
                theta=datasets_plot,
                fill="toself",
                fillcolor=colors[idx % len(colors)],
                line=dict(color=line_colors[idx % len(line_colors)], width=2),
                name=model_name,
                text=[f"{val:.3f}" for val in values],
                textposition="middle right",
                mode="lines+markers+text",
            )
        )

    fig.update_layout(
        polar=dict(
            radialaxis=dict(
                visible=True, range=[0, 1], showline=False, tickfont=dict(size=12)
            ),
            angularaxis=dict(
                tickfont=dict(size=13, family="Arial"),
                rotation=90,
                direction="clockwise",
            ),
        ),
        showlegend=True,
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=-0.2,
            xanchor="center",
            x=0.5,
            font=dict(size=14),
        ),
        title=dict(
            text="Model Comparison",
            x=0.5,
            y=0.95,
            font=dict(size=24, family="Arial", color="#1F2937"),
        ),
        paper_bgcolor="white",
        plot_bgcolor="white",
        height=700,
        width=900,
        margin=dict(t=100, b=100, l=80, r=80),
    )

    return fig


def get_performance_cost_chart(df, category_name="Overall"):
    # Create figure and axis with specified style
    fig, ax = plt.subplots(figsize=(12, 8), dpi=300)

    # Configure plot style
    ax.grid(True, linestyle="--", alpha=0.15, which="both")
    ax.set_facecolor("white")
    fig.patch.set_facecolor("white")

    colors = {"Private": "#4F46E5", "Open source": "#16A34A"}
    performance_colors = ["#DCFCE7", "#FEF9C3", "#FEE2E2"]

    score_column = "Category Score"

    # Plot data points
    for _, row in df.iterrows():
        color = colors[row["Model Type"]]
        size = 100 if row[score_column] > 0.85 else 80
        edge_color = "#3730A3" if row["Model Type"] == "Private" else "#166534"

        # Plot scatter points
        ax.scatter(
            row["IO Cost"],
            row[score_column] * 100,
            c=color,
            s=size,
            alpha=0.9,
            edgecolor=edge_color,
            linewidth=1,
            zorder=5,  # Ensure points are above grid
        )

        # Add annotations with model names
        bbox_props = dict(boxstyle="round,pad=0.3", fc="white", ec="none", alpha=0.8)

        ax.annotate(
            f"{row['Model']}\n(${row['IO Cost']:.2f})",
            (row["IO Cost"], row[score_column] * 100),
            xytext=(5, 5),
            textcoords="offset points",
            fontsize=8,
            bbox=bbox_props,
            zorder=6,
        )

    # Configure axes
    ax.set_xscale("log")
    ax.set_xlim(0.08, 40)  # Adjust based on your data range
    ax.set_ylim(60, 95)

    # Customize axis labels
    ax.set_xlabel("I/O Cost per Million Tokens ($)", fontsize=10, labelpad=10)
    ax.set_ylabel("Model Performance Score", fontsize=10, labelpad=10)

    # Add legend
    legend_elements = [
        plt.scatter([], [], c=color, label=label, s=80)
        for label, color in colors.items()
    ]
    ax.legend(
        handles=legend_elements,
        loc="upper right",
        frameon=True,
        facecolor="white",
        edgecolor="none",
        fontsize=9,
    )

    # Set title
    ax.set_title(
        f"AI Language Model Performance vs. Cost - {category_name}", fontsize=12, pad=15
    )

    # Add performance bands
    for y1, y2, color in zip([85, 75, 60], [95, 85, 75], performance_colors):
        ax.axhspan(y1, y2, alpha=0.2, color=color, zorder=1)

    # Customize tick parameters
    ax.tick_params(axis="both", which="major", labelsize=9)
    ax.tick_params(axis="both", which="minor", labelsize=8)

    # Add minor ticks for log scale
    ax.xaxis.set_minor_locator(plt.LogLocator(base=10.0, subs=np.arange(2, 10) * 0.1))

    # Adjust layout
    plt.tight_layout()

    return fig