"""Score one A/B prediction per recording against the published Balanced-80 test split.""" import argparse import json from collections import defaultdict from pathlib import Path import pyarrow.parquet as pq def score(data, predictions): rows = pq.read_table(data, columns=['id', 'pair_id', 'category', 'answer']).to_pylist() expected = {r['id'] for r in rows} answers = {} for line in predictions.read_text().splitlines(): if not line.strip(): continue prediction = json.loads(line) key, choice = prediction['id'], prediction['prediction'] if key not in expected or key in answers or choice not in ('A', 'B'): raise ValueError(f'Unknown or duplicate ID, or invalid A/B choice: {key!r}') answers[key] = choice if answers.keys() != expected: raise ValueError(f'Missing {len(expected - answers.keys())} predictions') pairs, categories = defaultdict(list), defaultdict(list) for r in rows: correct = answers[r['id']] == r['answer'] pairs[r['pair_id']].append(correct) categories[r['category']].append(correct) return { 'n_recordings': len(rows), 'n_pairs': len(pairs), 'recording_accuracy': sum(sum(c) for c in categories.values()) / len(rows), 'both_recordings_correct': sum(all(p) for p in pairs.values()) / len(pairs), 'accuracy_by_category': {k: sum(v) / len(v) for k, v in sorted(categories.items())}, } if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('predictions', type=Path, help='JSONL with id and prediction fields') parser.add_argument('--data', type=Path, default=Path(__file__).parent / 'data/test.parquet') args = parser.parse_args() print(json.dumps(score(args.data, args.predictions), indent=2))