nanori / eval /tests /test_score.py
Plachta's picture
Nanori v1
df7317f
Raw History Blame Contribute Delete
3.63 kB
import random
from conftest import make_item
from nanori_eval import score as SC
from nanori_eval.items import normalise
def _items():
rnd = random.Random(3)
out = {'JA-PLACE': {}, 'JA-DRUG': {}, 'JA-COMPANY': {}}
for i in range(40):
st = ['P-H/F1', 'P-R/F2', 'P-U/F2', 'ADDR/F2'][i % 4]
w = rnd.uniform(0.5, 3.0) if st[:3] in ('P-H', 'P-R') else None
out['JA-PLACE'][f'P{i}'] = normalise(make_item(f'P{i}', 'JA-PLACE', st, weight=w,
flags={'in_pilot': i % 3 == 0}))
for i in range(20):
st = ['D-E/kampo', 'D-N/new_brand_2026'][i % 2]
out['JA-DRUG'][f'D{i}'] = normalise(make_item(f'D{i}', 'JA-DRUG', st))
for i in range(20):
st = ['hard:kanji', 'regular:kana'][i % 2]
out['JA-COMPANY'][f'C{i}'] = normalise(make_item(f'C{i}', 'JA-COMPANY', st))
return out
def _verdicts(items, p, seed):
rnd = random.Random(seed)
return {s: {i: {'R2': ('PASS' if rnd.random() < p else 'FAIL'), 'G': 'PASS'} for i in d} for s, d in items.items()}
def test_score_cells_and_pairs():
items = _items()
V = {'a': _verdicts(items, 0.8, 1), 'b': _verdicts(items, 0.4, 2)}
V['b']['JA-DRUG']['D0']['R2'] = 'UNSCORED'
res = SC.score(items, V, ['a', 'b'], ['R2', 'G'], primary='R2', B=500, split_flags=('in_pilot',))
P = res['subsets']['JA-PLACE']['cells']
assert P['headline_weighted']['n_items'] == 20 and P['headline_weighted']['weighted']
hw = P['headline_weighted']['judges']['R2']['systems']['a']
assert hw['kish_n_eff'] < 20 and 'unweighted_rate' in hw
assert P['all_400_unweighted']['n_items'] == 40
pr = P['headline_weighted']['judges']['R2']['pairs']['a - b']
assert pr['seed_label'].endswith(':JA-PLACE:headline_weighted:R2:a-b')
assert 'ci98_75' in pr and 'p_one_sided_h0_le0' in pr and pr['quoted']
D = res['subsets']['JA-DRUG']['cells']
assert set(D) >= {'D-E', 'D-N'} and 'all' not in D
b = D['D-E']['judges']['R2']['systems']['b']
assert b['unscored'] == 1 and b['unscored_rate'] == 0.1
assert D['D-E']['judges']['R2']['pairs']['a - b']['n_paired'] == 9
assert not D['D-E']['judges']['R2']['pairs']['a - b']['quoted'] # 10 % unscored > 5 %
assert 'splits' in P['headline_weighted']
md = SC.leaderboard_md(res, placeholders={'Ours': 'TBD'}, reference='b')
assert 'TBD' in md and 'a − b' in md
assert 'headline_weighted' in SC.full_md(res)
def test_score_is_deterministic():
items = _items()
V = {'a': _verdicts(items, 0.7, 5), 'b': _verdicts(items, 0.5, 6)}
r1 = SC.score(items, V, ['a', 'b'], ['R2'], B=300)
r2 = SC.score(items, V, ['a', 'b'], ['R2'], B=300)
assert r1 == r2
def test_md_rounds_once_and_labels_placeholder(tmp_path):
items = _items()
V = {'a': _verdicts(items, 0.8, 1), 'b': _verdicts(items, 0.4, 2)}
res = SC.score(items, V, ['a', 'b'], ['R2'], B=200)
p = tmp_path / 'lb.json'
SC.dump(res, str(p))
assert SC.EXACT not in p.read_text() # the JSON output carries no display values
assert SC._pct([94, 107]) == '87.9' # 0.8785 (4 dp) would show 87.8
assert SC._pct([-1, 8], True) == '-12.5' and SC._pct([0, 5], True) == '+0.0' and SC._pct(None) == '–'
md = SC.leaderboard_md(res, placeholders={'ours': 'TBD'}, placeholder_labels={'ours': 'Ours (TBD at release)'})
assert '| Ours (TBD at release) |' in md and '| ours |' not in md
b = res['subsets']['JA-DRUG']['cells']['D-E']['judges']['R2']['systems']['a']
assert f'| {SC._pct([b["pass"], b["n_scored"]])} [' in md