"""Score prediction-only queues against the existing spread benchmark.""" import csv import json import math import os from pathlib import Path import random import shutil import subprocess import sys import tempfile from urllib.request import urlopen from zipfile import ZipFile from rank_cuts import load_cuts, rank, tie_key here=Path(__file__).resolve().parent pin="e79f5f796fb6237a4ef0ed9c9e43e33a8dbc3e22" url=f"https://huggingface.co/datasets/foliokestrel/folio-spread-bench/resolve/{pin}/" out=Path(os.environ["OUTPUT_DIR"]) with tempfile.TemporaryDirectory() as directory: tmp=Path(directory) with urlopen(url+"comparisons/paired-cuts/paired-cuts.zip",timeout=90) as f,(tmp/"kit.zip").open("wb") as file: shutil.copyfileobj(f,file) with ZipFile(tmp/"kit.zip") as kit: kit.extractall(tmp) sys.path.insert(0,str(tmp)) from score import VIEWS,jsonlines from compare_cuts import errors methods=("line_search","zhnivo") for fold in ("validation","test"): with urlopen(url+f"baselines/{fold}-scores.csv",timeout=60) as f: official={(r["method"],r["group"],r["view"]):int(r["in_band"]) for r in csv.DictReader(f.read().decode().splitlines())} manifest=list(jsonlines(tmp/f"manifests/{fold}.jsonl")) group={r["id"]:r["group"] for r in manifest} paths=[tmp/f"baselines/{m}-{fold}.jsonl" for m in methods] scored=[errors(manifest,list(jsonlines(path))) for path in paths] report=[] for view in VIEWS: first,second=(load_cuts(path,view) for path in paths) ordered=rank(first,second) assert rank(second,first)==ordered shuffled=list(first.items());random.Random(258).shuffle(shuffled) assert rank(dict(shuffled),second)==ordered for archive in ["all",*sorted(set(group.values()))]: queue=[(key,gap) for key,gap in ordered if archive=="all" or group[key]==archive] n=len(queue) for method,cut,labels in zip(methods,(first,second),scored): missed={key for key,_ in queue if labels[key,view]>1e-9} assert n-len(missed)==official[method,archive,view] center=sorted((k for k,_ in queue),key=lambda k:(-max(abs(x-.5) for x in cut[k]),tie_key(k))) entry=dict(archive=archive,view=view,method=method,scans=n,misses=len(missed), identical_endpoints=sum(gap==0 for _,gap in queue), misses_at_identical_endpoints=sum(gap==0 and key in missed for key,gap in queue),budgets=[]) for pct in (10,20,50): count=math.ceil(n*pct/100) entry["budgets"].append(dict(percent=pct,reviewed=count,misses_in_queue=sum(key in missed for key,_ in queue[:count]), center_control=sum(k in missed for k in center[:count]),uniform_expectation=count*len(missed)/n)) report.append(entry) if view=="native": cmd=[sys.executable,here/"rank_cuts.py",*paths,"--limit","17"] observed=list(map(json.loads,subprocess.check_output(cmd,text=True).splitlines())) assert [(r["id"],r["endpoint_difference"]) for r in observed]==ordered[:17] (out/(fold+".json")).write_text(json.dumps(report,indent=2)) (out/"verified.json").write_text(json.dumps(dict(pin=pin,methods=methods,counts_verified=True,ranking_invariant=True)))