File size: 17,537 Bytes
1dedb52
 
ab82350
127cc6f
27638f8
 
1dedb52
ffca6e7
10d59b8
c297628
127cc6f
ffca6e7
 
10d59b8
c297628
1dedb52
127cc6f
 
 
 
 
 
 
1dedb52
27638f8
 
28bec71
 
 
 
ffca6e7
 
 
 
 
 
 
 
 
1dedb52
28bec71
 
 
1dedb52
28bec71
0e48ab9
28bec71
 
ab82350
28bec71
 
 
 
10d59b8
 
28bec71
 
 
16d8300
28bec71
 
16d8300
28bec71
 
127cc6f
28bec71
 
127cc6f
28bec71
 
127cc6f
 
28bec71
 
ab82350
28bec71
 
 
 
 
ab82350
127cc6f
28bec71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127cc6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1501da9
127cc6f
 
 
 
 
 
 
a86996f
127cc6f
 
 
 
 
1501da9
127cc6f
 
 
 
 
 
 
 
 
 
 
 
 
ac0e33a
127cc6f
 
 
28bec71
 
 
127cc6f
1dedb52
 
 
3a4d296
 
 
 
 
 
 
 
 
ffca6e7
127cc6f
 
 
 
ffca6e7
127cc6f
e3793a3
ffca6e7
 
 
 
 
 
 
 
 
 
127cc6f
 
 
 
 
 
 
 
ffca6e7
127cc6f
 
 
 
 
 
28bec71
 
127cc6f
 
 
 
 
 
 
e6fcd81
 
98371a5
27638f8
3a4d296
d0317c5
27638f8
7d08e8b
1dedb52
 
 
 
 
 
 
 
 
 
 
ffca6e7
e3793a3
1dedb52
127cc6f
 
e3793a3
127cc6f
 
 
 
28bec71
 
 
 
 
 
 
 
 
 
 
 
 
 
127cc6f
28bec71
 
 
 
 
 
 
 
 
 
 
ffca6e7
 
 
e6fcd81
ffca6e7
28bec71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6fcd81
28bec71
 
 
 
ffca6e7
28bec71
 
 
 
 
 
 
 
 
 
 
 
e6fcd81
 
 
 
 
 
 
 
28bec71
27638f8
 
10d59b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e48ab9
 
3a4d296
 
 
 
 
 
0e48ab9
10d59b8
 
 
 
 
 
 
 
49c7961
c297628
 
10d59b8
49c7961
27638f8
127cc6f
 
 
 
8c46e7b
 
 
 
 
127cc6f
 
a86996f
 
 
 
 
 
 
 
 
 
c435fcc
27638f8
 
127cc6f
 
 
 
 
 
 
 
 
 
 
 
 
e6fcd81
127cc6f
 
 
ffca6e7
127cc6f
 
 
e6fcd81
127cc6f
1dedb52
 
 
 
95d1cf3
1dedb52
 
 
27638f8
cb35049
e6fcd81
 
28bec71
 
b7b8a3b
 
 
 
 
c297628
49c7961
 
eae3909
c297628
49c7961
eae3909
b7b8a3b
49c7961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a4d296
49c7961
3a4d296
 
49c7961
 
 
 
 
 
 
127cc6f
 
 
 
c435fcc
27638f8
 
 
10d59b8
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
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
from __future__ import annotations

import zipfile
from dataclasses import dataclass
from pathlib import Path
import gradio as gr
import pandas as pd
import numpy as np
from functools import partial
from copy import deepcopy
import website_texts
import os
import re
from constants import model_type_emoji
from apscheduler.schedulers.background import BackgroundScheduler
from gradio_leaderboard import ColumnFilter, Leaderboard, SelectColumns
from website_texts import (
    ABOUT_TEXT,
    CITATION_BUTTON_LABEL,
    CITATION_BUTTON_TEXT,
    INTRODUCTION_TEXT,
    TITLE,
    VERSION_HISTORY_BUTTON_TEXT,
)


@dataclass
class LBContainer:
    name: str
    base_path_to_results: str
    blurb: str | None = None
    n_datasets: int | None = None

    def __post_init__(self):
        for fname in os.listdir(self._base_path):
            match = re.match(r"n_datasets_(.+)", fname)
            if match:
                self.n_datasets = match.group(1)
                break

    @property
    def _base_path(self):
        return Path(__file__).parent / "data" / self.base_path_to_results

    def load_df_leaderboard(self) -> pd.DataFrame:
        df = pd.read_csv(self._base_path.resolve() / "website_leaderboard.csv")
        df = df.rename(columns={"1#": "#"})
        return df

    def _handle_img_zip(self, img_name: str) -> str:
        _base_path = self._base_path / img_name
        zip_path = _base_path.with_suffix(".png.zip")
        img_path = _base_path.with_suffix(".png")
        if img_path.exists():
            return str(img_path)
        with zipfile.ZipFile(zip_path, "r") as zipf:
            zipf.extractall(img_path.parent)
        return str(img_path)

    def get_path_to_tuning_impact_elo(self) -> str:
        return self._handle_img_zip("tuning-impact-elo")

    def get_path_to_pareto_front_improvability_vs_time_infer(self) -> str:
        return self._handle_img_zip("pareto_front_improvability_vs_time_infer")

    def get_path_to_pareto_n_configs_imp(self) -> str:
        return self._handle_img_zip("pareto_n_configs_imp")

    def get_path_to_winrate_matrix(self) -> str:
        return self._handle_img_zip("winrate_matrix")


def make_overview_images(lb: LBContainer, subset_name):
    # Main Figure
    gr.Image(
        lb.get_path_to_tuning_impact_elo(),
        label=f"Leaderboard Overview [{subset_name}]",
        show_label=True,
        height=500,
        show_share_button=True,
    )

    with gr.Row():
        with gr.Column(scale=1):
            gr.Image(
                value=lb.get_path_to_pareto_front_improvability_vs_time_infer(),
                label=f"Inference Time Pareto Front [{subset_name}]",
                height=400,
                show_label=True,
                show_share_button=True,
            )
        with gr.Column(scale=1):
            gr.Image(
                value=lb.get_path_to_pareto_n_configs_imp(),
                label=f"Tuning Trajectories [{subset_name}]",
                height=400,
                show_label=True,
                show_share_button=True,
            )


def make_overview_leaderboard(lbs: [LBContainer]):
    # Create column per LB
    all_models = {
        m.split("[")[0].strip()
        for lb in lbs
        for m in lb.df_leaderboard[
            ~lb.df_leaderboard["TypeName"].isin(["Reference Pipeline"])
        ]["Model"]
        .unique()
        .tolist()
    }

    full_df = None
    for lb in lbs:
        df = lb.df_leaderboard.copy()
        df = df[~df["TypeName"].isin(["Reference Pipeline"])]
        df[lb.name] = df["Elo [⬆️]"].rank(ascending=False, method="first").astype(int)
        df = df.sort_values(by=lb.name, ascending=True)

        df = df[["Type", "Model", lb.name]]
        # Remove imputed message.
        df["Model"] = (
            df["Model"].apply(lambda x: x.split("[")[0].strip()).astype("string")
        )

        if full_df is None:
            # TODO: add support in case a model did not run on the full LB.
            assert all_models.difference(set(df["Model"].unique())) == set()
            full_df = df
        else:
            df = df[["Model", lb.name]]
            df_models = set(df["Model"].unique())
            missing_models = all_models.difference(df_models)
            if missing_models:
                missing_models_df = pd.DataFrame(
                    [[mm, "--"] for mm in missing_models],
                    columns=["Model", lb.name],
                )
                df = pd.concat([df, missing_models_df], ignore_index=True)
            df["Model"] = df["Model"].astype("string")
            # Merge
            full_df = full_df.merge(df, how="left", on="Model", validate="1:1")

    medal_colors = ["#998A00", "#808080", "#8C5520"]

    # Highlight function
    def highlight_top3(col):
        styles = [""] * len(col)
        for index_i in range(len(col)):
            if (not isinstance(col.iloc[index_i], str)) and col.iloc[index_i] <= 3:
                styles[index_i] = (
                    f"background-color: {medal_colors[col.iloc[index_i] - 1]};"
                )

        return styles

    styler = full_df.style.apply(highlight_top3, axis=0, subset=[lb.name for lb in lbs])

    return gr.DataFrame(
        styler,
        pinned_columns=2,
        interactive=False,
        show_search="search",
        label="The ranking of all models (with imputation) across various leaderboards.",
    )


def make_leaderboard(lb: LBContainer) -> Leaderboard:
    df_leaderboard = lb.load_df_leaderboard()

    # -- Add filters
    df_leaderboard["TypeFiler"] = df_leaderboard["TypeName"].apply(
        lambda m: f"{m} {model_type_emoji[m]}"
    )
    df_leaderboard["Only Default"] = df_leaderboard["Model"].str.contains(
        "(default)", regex=False
    )
    df_leaderboard["Only Tuned"] = df_leaderboard["Model"].str.contains(
        "(tuned)", regex=False
    )
    df_leaderboard["Only Tuned + Ensembled"] = df_leaderboard["Model"].str.contains(
        r"(tuned + ensembled)", regex=False
    ) | df_leaderboard["Model"].str.contains(r"AutoGluon", regex=False)

    filter_columns = [
        ColumnFilter("TypeFiler", type="checkboxgroup", label="πŸ€– Model Types"),
        ColumnFilter("Only Default", type="boolean", default=False),
        ColumnFilter("Only Tuned", type="boolean", default=False),
        ColumnFilter("Only Tuned + Ensembled", type="boolean", default=False),
    ]

    datatypes = []
    for s in df_leaderboard.T.values:
        dt = s.dtype
        if dt == bool:
            datatypes.append("bool")
        elif np.issubdtype(dt, np.number):
            datatypes.append("number")
        else:
            datatypes.append("markdown")

    # Add Imputed count postfix
    if any(df_leaderboard["Imputed"]):
        df_leaderboard["Imputed"] = df_leaderboard["Imputed"].replace(
            {
                True: "Imputed",
                False: "Not Imputed",
            }
        )
        datatypes.append("bool")
        filter_columns.append(
            ColumnFilter(
                "Imputed",
                type="checkboxgroup",
                label="(Not) Imputed Models",
                info="We impute the performance for models that cannot run on all"
                " datasets due to task or dataset size constraints. We impute with"
                " the performance of a default RandomForest."
                " We add a postfix [X% IMPUTED] to the model if any results were"
                " imputed. The X% shows the percentage of"
                " datasets that were imputed. In general, imputation negatively"
                " represents the model performance, punishing the model for not"
                " being able to run on all datasets.",
            )
        )
    else:
        df_leaderboard = df_leaderboard.drop(columns=["Imputed (%) [⬇️]"])

    return Leaderboard(
        # label=f"Full Leaderboard [{lb.name}]",
        elem_id=f"lb_for_{lb.name}",
        value=df_leaderboard,
        datatype=datatypes,
        select_columns=SelectColumns(
            default_selection=list(df_leaderboard.columns),
            cant_deselect=["Type", "Model"],
            label="Select Columns to Display:",
        ),
        hide_columns=[
            "TypeName",
            "TypeFiler",
            "RefModel",
            "Only Default",
            "Only Tuned",
            "Only Tuned + Ensembled",
            "Imputed",
        ],
        search_columns=["Model", "TypeName"],
        filter_columns=filter_columns,
        bool_checkboxgroup_label="Custom Views (exclusive, only toggle one at a time):",
        height=800,
    )


@dataclass
class LBMatrixElement:
    imputation: str
    splits: str
    tasks: str
    datasets: str

    def get_path_to_results(self) -> str:
        return (
            f"imputation_{self.imputation}/"
            f"splits_{self.splits}/"
            f"tasks_{self.tasks}/"
            f"datasets_{self.datasets}/"
        )


@dataclass
class LBMatrix:
    imputation = ["no", "yes"]
    splits = ["all", "lite"]
    tasks = ["all", "classification", "regression"]
    datasets = ["all", "small", "medium", "tabpfn"]

    @staticmethod
    def get_name_for_lb(lb_key, lb_value):
        if lb_key == "imputation":
            return (
                "Models (w/o imputation)"
                if lb_value == "no"
                else "πŸ”Ή Models (with imputation)"
            )
        if lb_key == "splits":
            return "All Repeats" if lb_value == "all" else "Lite"
        if lb_key == "tasks":
            match lb_value:
                case "all":
                    return "All Tasks"
                case "classification":
                    return "Classification"
                case "regression":
                    return "Regression"
                case _:
                    raise ValueError()
        if lb_key == "datasets":
            match lb_value:
                case "all":
                    return "All Datasets"
                case "small":
                    return "Small"
                case "medium":
                    return "Medium"
                case "tabpfn":
                    return "πŸ”Έ TabPFNv2-data"
                case _:
                    raise ValueError()
        raise ValueError()

    def element_to_blurb(self, element: LBMatrixElement, n_datasets: int) -> str:
        datasets_name = (
            element.datasets if element.datasets != "tabpfn" else "TabPFNv2-compatible"
        )
        blurb = f"Leaderboard for {n_datasets} datasets ({datasets_name} datasets, {element.tasks} tasks) "

        if element.splits == "lite":
            blurb += "for one split (1st fold, 1st repeat) "

        blurb += "including all "
        if element.imputation == "yes":
            blurb += "(imputed) "
        blurb += f"models."

        if datasets_name == "small":
            blurb += "<br>Small datasets contain between 500 and 10,000 samples."
        elif datasets_name == "medium":
            blurb += "<br>Medium datasets contain between 10,000 and 250,000 samples."
        elif datasets_name == "TabPFNv2-compatible":
            blurb += "<br>TabPFNv2-compatible datasets contain at most 10,000 samples, 500 features, and 10 classes."

        return blurb


def render_details(imputation, splits, tasks, datasets, lb_matrix):
    """
    Renders the heavy content (images, dataframes).
    """
    impute_t_name = lb_matrix.get_name_for_lb("imputation", imputation)
    splits_t = lb_matrix.get_name_for_lb("splits", splits)
    tasks_t_name = lb_matrix.get_name_for_lb("tasks", tasks)
    datasets_t_name = lb_matrix.get_name_for_lb("datasets", datasets)

    lb_element = LBMatrixElement(
        imputation=imputation,
        splits=splits,
        tasks=tasks,
        datasets=datasets,
    )
    lb = LBContainer(
        name=f"{impute_t_name} | {splits_t} | {tasks_t_name} | {datasets_t_name}",
        base_path_to_results=lb_element.get_path_to_results(),
    )

    lb.blurb = lb_matrix.element_to_blurb(
        lb_element,
        n_datasets=lb.n_datasets,
    )

    gr.Markdown(
        lb.blurb,
        elem_classes="markdown-text",
    )
    make_overview_images(lb, subset_name=lb.name)

    # Render Leaderboard safely
    with gr.Group():
        gr.Markdown(
            "## ⭐ Full Leaderboard Table",
            elem_classes="markdown-text",
        )
        make_leaderboard(lb)

    gr.Image(
        lb.get_path_to_winrate_matrix(),
        label=f"Winmatrix Overview [{lb.name}]",
        show_label=True,
        height=800,
        show_share_button=True,
    )


def render_func(evt: gr.SelectData):
    print(f"Tab Selected: {evt.value} (Type: {evt.index})")


def main():
    css = """
    .markdown-text-box {
        padding: 4px;
        border-radius: 2px;
    }    
    
    .tab-buttons {
    margin-top: -14px !important; 
    margin-bottom: -14px !important;
    }
    """
    js_func = """
    function refresh() {
        const url = new URL(window.location);

        if (url.searchParams.get('__theme') !== 'dark') {
            url.searchParams.set('__theme', 'dark');
            window.location.href = url.href;
        }
    }
    """
    with gr.Blocks(css=css, js=js_func, title="TabArena") as website:
        gr.HTML(TITLE)

        # -- Introduction
        gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
        with gr.Row():
            with gr.Column(), gr.Accordion("πŸ“Š Datasets", open=False):
                gr.Markdown(
                    website_texts.OVERVIEW_DATASETS, elem_classes="markdown-text-box"
                )

            with gr.Column(), gr.Accordion("πŸ€– Models", open=False):
                gr.Markdown(
                    website_texts.OVERVIEW_MODELS, elem_classes="markdown-text-box"
                )
        with gr.Row():
            with gr.Column(), gr.Accordion("πŸ“ˆ Metrics, Imputation, Repeats", open=False):
                gr.Markdown(
                    website_texts.OVERVIEW_METRICS, elem_classes="markdown-text-box"
                )
            with gr.Column(), gr.Accordion("πŸ“Š Reference Pipelines", open=False):
                gr.Markdown(
                    website_texts.OVERVIEW_REF_PIPE, elem_classes="markdown-text-box"
                )
        with gr.Row(), gr.Accordion("πŸ“ About", open=False):
            gr.Markdown(ABOUT_TEXT, elem_classes="markdown-text-box")
        with gr.Row(), gr.Accordion("πŸ“™ Citation", open=False):
            gr.Textbox(
                value=CITATION_BUTTON_TEXT,
                label=CITATION_BUTTON_LABEL,
                lines=7,
                elem_id="citation-button",
                show_copy_button=True,
            )

        gr.Markdown("## πŸ† TabArena Leaderboards")
        gr.Markdown("Change the filters below to compare models with our without imputation across repeats, tasks, and dataset subsets.")
        gr.Markdown("")
        lb_matrix = LBMatrix()

        impute_state = gr.State(lb_matrix.imputation[0])
        splits_state = gr.State(lb_matrix.splits[0])
        tasks_state = gr.State(lb_matrix.tasks[0])
        datasets_state = gr.State(lb_matrix.datasets[0])

        # Impute
        with gr.Tabs(elem_classes="tab-buttons") as impute_tabs:
            for impute_t in lb_matrix.imputation:
                with gr.TabItem(
                    lb_matrix.get_name_for_lb("imputation", impute_t),
                    id=impute_t,
                ) as t_impute:
                    t_impute.select(lambda x=impute_t: x, outputs=impute_state)
        # Splits
        with gr.Tabs(elem_classes="tab-buttons") as split_tabs:
            for splits_t in lb_matrix.splits:
                with gr.TabItem(
                    lb_matrix.get_name_for_lb("splits", splits_t),
                    id=f"{impute_t}_{splits_t}",
                ) as t_splits:
                    t_splits.select(lambda x=splits_t: x, outputs=splits_state)
        # Tasks
        with gr.Tabs(elem_classes="tab-buttons") as task_tabs:
            for tasks_t in lb_matrix.tasks:
                with gr.TabItem(
                    lb_matrix.get_name_for_lb("tasks", tasks_t),
                    id=f"{impute_t}_{splits_t}_{tasks_t}",
                ) as t_tasks:
                    t_tasks.select(lambda x=tasks_t: x, outputs=tasks_state)
        # Datasets
        with gr.Tabs(elem_classes="tab-buttons") as dataset_tabs:
            for datasets_t in lb_matrix.datasets:
                with gr.TabItem(
                    lb_matrix.get_name_for_lb("datasets", datasets_t),
                    id=f"{impute_t}_{splits_t}_{tasks_t}_{datasets_t}",
                ) as t_dataset:
                    t_dataset.select(
                        lambda x=datasets_t: x,
                        outputs=datasets_state,
                    )

        with gr.Column():

            @gr.render(inputs=[impute_state, splits_state, tasks_state, datasets_state])
            def reactive_render(sel_i, sel_s, sel_t, sel_d):
                render_details(
                    imputation=sel_i,
                    splits=sel_s,
                    tasks=sel_t,
                    datasets=sel_d,
                    lb_matrix=lb_matrix,
                )

        with gr.Row(), gr.Accordion("πŸ“‚ Version History", open=False):
            gr.Markdown(VERSION_HISTORY_BUTTON_TEXT, elem_classes="markdown-text")

    website.launch(show_error=True, ssr_mode=False, debug=True)


if __name__ == "__main__":
    main()