File size: 2,338 Bytes
5f0f535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Avg WER chart above the ASR board (leaderboard_view.render_wer_chart)."""

from __future__ import annotations

import re

import pandas as pd
import pytest

import leaderboard_view as lv

AVG = lv.AutoEvalColumn.avg_wer_core.name


def _frame(rows):
    return pd.DataFrame(rows, columns=[lv.RANK_COL, lv.MODEL_COL, AVG])


def test_columns_follow_rank_with_avg_wer_heights():
    df = _frame([[2, "org/b", 6.0], [1, "org/a", 5.0], [3, "org/c", 7.0]])
    extras = {"org/a": {"ffas": 80.0}, "org/b": {"ffas": 91.5, "rtf": 120.0, "params": 0.6}}
    core = {"org/b": (4.0, 6.0, 9.0, 60.0)}
    html = lv.render_wer_chart(df, core, extras)

    assert re.findall(r"data-model='([^']+)'", html) == ["org/a", "org/b", "org/c"]
    assert re.findall(r"data-rank='(\d+)'", html) == ["1", "2", "3"]
    # Axis tops out at the next round number above the tallest bar (7 -> 8).
    assert "data-scale='8'" in html and "--v:62.5%" in html and "--v:87.5%" in html
    assert re.findall(r"data-t='\d+'>([\d.]+)<", html) == ["0", "2", "4", "6", "8"]
    # Avg WER on the caps; FFAS and the rest for the hover card.
    assert "<span class='ff-colval'>5.00</span>" in html
    assert "data-ffas='91.5'" in html and "data-low='60.00'" in html and "data-rtf='120.0'" in html
    assert "data-ffas=''" in html  # org/c has no FFAS, still drawn
    assert html.count("tabindex='0'") == 1
    assert "Avg WER by model" in html and "FFAS, 0–100" not in html


@pytest.mark.parametrize(
    "top, scale", [(3.9, 4), (4.0, 4), (4.1, 8), (7.0, 8), (11.4, 12), (19.5, 20), (33.0, 40), (97.0, 100), (130.0, 160), (900.0, 900)]
)
def test_chart_scale(top, scale):
    assert lv.chart_scale(top) == scale


def test_models_without_avg_wer_are_counted_not_drawn():
    df = _frame([[1, "org/a", 5.0], [2, "org/b", None]])
    html = lv.render_wer_chart(df, {}, {})
    assert re.findall(r"data-model='([^']+)'", html) == ["org/a"]
    assert "1 model has no Avg WER yet" in html


def test_empty_frames_render_nothing():
    assert lv.render_wer_chart(_frame([]), {}, {}) == ""
    assert lv.render_wer_chart(_frame([[1, "org/a", None]]), {}, {}) == ""


def test_model_names_are_escaped():
    cell = "org/<img src=x onerror=alert(1)>"
    html = lv.render_wer_chart(_frame([[1, cell, 5.0]]), {}, {})
    assert "<img" not in html
    assert "&lt;img" in html