Download stats/report.mjs from PulseOS/budgetbench: direct link, hf CLI and curl.
- Browser
- Download file 8.93 kB
-
https://huggingface.co/datasets/PulseOS/budgetbench/resolve/main/stats/report.mjs
- Command line
-
hf download hf://datasets/PulseOS/budgetbench/stats/report.mjs
-
curl -L -o report.mjs https://huggingface.co/datasets/PulseOS/budgetbench/resolve/main/stats/report.mjs
8.93 kB
| // BudgetBench REPORT: the six labels of one run (never merged), with 95% intervals from a household-clustered | |
| // bootstrap, per category and per source; optional regrade from the stored transcripts; optional paired comparison | |
| // of two runs on the same cases (McNemar on each label, paired bootstrap of the difference). | |
| // node budgetbench/stats/report.mjs <run dir> [--regrade] [--json out.json] [--md out.md] | |
| // node budgetbench/stats/report.mjs <run A> --compare <run B> [--regrade] | |
| import fs from "node:fs"; | |
| import path from "node:path"; | |
| import {fileURLToPath, pathToFileURL} from "node:url"; | |
| const HERE = path.dirname(fileURLToPath(import.meta.url)); | |
| const BB = path.resolve(HERE, ".."); | |
| export const LABELS = ["wrong_shown", "wrong_offered", "right_no_tap", "right_tap", "not_sure", "declined", "review"]; | |
| export const TITLES = {wrong_shown: "Wrong shown", wrong_offered: "Wrong offered", right_no_tap: "Right, no tap", right_tap: "Right after one tap", not_sure: "Not sure", declined: "Declined", review: "Needs review"}; | |
| function rng(seed) { let a = seed >>> 0; return () => { a = (a + 0x6D2B79F5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } | |
| export async function loadRun(dir, {regrade = false} = {}) { | |
| const rows = fs.readFileSync(path.join(dir, "results.jsonl"), "utf8").split("\n").filter(Boolean).map((l) => JSON.parse(l)); | |
| const inputs = JSON.parse(fs.readFileSync(path.join(dir, "inputs.json"), "utf8")); | |
| const errors = fs.existsSync(path.join(dir, "errors.jsonl")) ? fs.readFileSync(path.join(dir, "errors.jsonl"), "utf8").split("\n").filter(Boolean).map((l) => JSON.parse(l)) : []; | |
| if (regrade) { | |
| const {loadHousehold} = await import(pathToFileURL(path.join(BB, "oracle/oracle.mjs")).href); | |
| const {scoreCase} = await import(pathToFileURL(path.join(BB, "grader/score.mjs")).href); | |
| const split = inputs.split; | |
| const [casesRel, hhRel] = {public: ["cases/public", "households/public"], sealed: ["sealed/cases", "sealed/households"], "sealed-v2": ["sealed-v2/cases", "sealed-v2/households"], "sealed-v3": ["sealed-v3/cases", "sealed-v3/households"]}[split]; | |
| const casesDir = path.join(BB, casesRel), hhDir = path.join(BB, hhRel); | |
| const cases = new Map(); | |
| for (const f of fs.readdirSync(casesDir).filter((x) => x.endsWith(".jsonl"))) for (const l of fs.readFileSync(path.join(casesDir, f), "utf8").split("\n").filter(Boolean)) { const c = JSON.parse(l); cases.set(c.id, c); } | |
| const hhs = new Map(); | |
| for (const r of rows) { | |
| const c = cases.get(r.id); | |
| if (!c) throw new Error(`case ${r.id} is not in the frozen split`); | |
| if (!hhs.has(c.household)) hhs.set(c.household, loadHousehold(path.join(hhDir, c.household))); | |
| const s = scoreCase(hhs.get(c.household), c, r.turns); | |
| r.label = s.label; r.taps = s.taps; r.outcomes = s.turns.map((t) => ({outcome: t.outcome, class: t.class ?? null, reason: t.reason})); | |
| r.source = c.source ?? r.source; | |
| } | |
| } | |
| // the last row per case wins (a resumed run never writes one twice, but be safe) | |
| const by = new Map(rows.map((r) => [r.id, r])); | |
| return {rows: [...by.values()], inputs, errors, dir}; | |
| } | |
| /** counts and household-clustered bootstrap intervals for each label */ | |
| export function summarize(rows, {reps = 2000, seed = 20261019} = {}) { | |
| const n = rows.length; | |
| const counts = Object.fromEntries(LABELS.map((l) => [l, rows.filter((r) => r.label === l).length])); | |
| const byHh = new Map(); | |
| for (const r of rows) { if (!byHh.has(r.household)) byHh.set(r.household, []); byHh.get(r.household).push(r); } | |
| const groups = [...byHh.values()]; | |
| const R = rng(seed); | |
| const draws = Object.fromEntries(LABELS.map((l) => [l, []])); | |
| for (let b = 0; b < reps && n > 0; b++) { | |
| const tally = Object.fromEntries(LABELS.map((l) => [l, 0])); | |
| let m = 0; | |
| for (let g = 0; g < groups.length; g++) { | |
| const grp = groups[Math.floor(R() * groups.length)]; | |
| for (let i = 0; i < grp.length; i++) { const r = grp[Math.floor(R() * grp.length)]; tally[r.label]++; m++; } | |
| } | |
| for (const l of LABELS) draws[l].push(m ? tally[l] / m : 0); | |
| } | |
| const ci = (xs) => { const s = [...xs].sort((a, b) => a - b); return [s[Math.floor(0.025 * (s.length - 1))] ?? 0, s[Math.ceil(0.975 * (s.length - 1))] ?? 0]; }; | |
| return {n, counts, rates: Object.fromEntries(LABELS.map((l) => [l, n ? counts[l] / n : 0])), ci95: Object.fromEntries(LABELS.map((l) => [l, ci(draws[l])])), households: groups.length}; | |
| } | |
| const pct = (x) => `${(100 * x).toFixed(1)}%`; | |
| export function row(s, l) { return `${s.counts[l]} (${pct(s.rates[l])}, ${pct(s.ci95[l][0])} to ${pct(s.ci95[l][1])})`; } | |
| function table(title, groups) { | |
| const lines = [`### ${title}`, "", `| ${["slice", "n", ...LABELS.map((l) => TITLES[l])].join(" | ")} |`, `|${Array(LABELS.length + 2).fill("---").join("|")}|`]; | |
| for (const [k, rs] of groups) { const s = summarize(rs, {reps: 1000}); lines.push(`| ${k} | ${s.n} | ${LABELS.map((l) => `${s.counts[l]} (${pct(s.rates[l])})`).join(" | ")} |`); } | |
| return lines.join("\n"); | |
| } | |
| const groupBy = (rows, f) => { const m = new Map(); for (const r of rows) { const k = f(r); if (!m.has(k)) m.set(k, []); m.get(k).push(r); } return [...m.entries()].sort((a, b) => a[0].localeCompare(b[0])); }; | |
| /** McNemar exact (two-sided) on discordant pairs */ | |
| function mcnemar(b, c) { | |
| const n = b + c; if (n === 0) return 1; | |
| const k = Math.min(b, c); | |
| let p = 0; for (let i = 0; i <= k; i++) p += binom(n, i) * 0.5 ** n; | |
| return Math.min(1, 2 * p); | |
| } | |
| function binom(n, k) { let r = 1; for (let i = 1; i <= k; i++) r = r * (n - k + i) / i; return r; } | |
| async function main() { | |
| const argv = process.argv.slice(2); | |
| const dir = argv[0]; | |
| if (!dir) { console.error("usage: report.mjs <run dir> [--regrade] [--compare <run dir>] [--json f] [--md f]"); process.exit(2); } | |
| const regrade = argv.includes("--regrade"); | |
| const flag = (n) => { const i = argv.indexOf(n); return i >= 0 ? argv[i + 1] : null; }; | |
| const run = await loadRun(dir, {regrade}); | |
| const s = summarize(run.rows); | |
| const sys = run.inputs.system; | |
| const out = [`## ${sys.system}${sys.variant ? ` (${sys.variant})` : ""}${sys.version ? ` ${sys.version}` : ""}${sys.model ? `, ${sys.model}` : ""}`, "", | |
| `Split ${run.inputs.split}, run ${run.inputs.run_id}, ${s.n} cases graded, ${run.errors.length} harness errors, ${s.households} households. Commit ${run.inputs.git_head?.slice(0, 9) ?? "?"}${run.inputs.app_source_sha256 ? `, app source ${run.inputs.app_source_sha256.slice(0, 12)}` : ""}.`, "", | |
| "| label | cases (rate, 95% interval) |", "|---|---|", ...LABELS.map((l) => `| ${TITLES[l]} | ${row(s, l)} |`), "", | |
| table("By category", groupBy(run.rows, (r) => r.category)), "", table("By source", groupBy(run.rows, (r) => r.source ?? "?")), ""]; | |
| if (flag("--compare")) { | |
| const other = await loadRun(flag("--compare"), {regrade}); | |
| const b = new Map(other.rows.map((r) => [r.id, r])); | |
| const paired = run.rows.filter((r) => b.has(r.id)); | |
| out.push(`### Paired with ${other.inputs.system.system} (${other.inputs.system.variant ?? ""}), ${paired.length} common cases`, "", "| label | A | B | A only | B only | McNemar p |", "|---|---|---|---|---|---|"); | |
| for (const l of LABELS) { | |
| const a = paired.filter((r) => r.label === l).length, bb = paired.filter((r) => b.get(r.id).label === l).length; | |
| const aOnly = paired.filter((r) => r.label === l && b.get(r.id).label !== l).length, bOnly = paired.filter((r) => r.label !== l && b.get(r.id).label === l).length; | |
| out.push(`| ${TITLES[l]} | ${a} | ${bb} | ${aOnly} | ${bOnly} | ${mcnemar(aOnly, bOnly).toFixed(4)} |`); | |
| } | |
| out.push(""); | |
| } | |
| const md = out.join("\n"); | |
| console.log(md); | |
| if (flag("--md")) fs.writeFileSync(flag("--md"), md + "\n"); | |
| const summary = {system: sys, split: run.inputs.split, run_id: run.inputs.run_id, git_head: run.inputs.git_head, app_source_sha256: run.inputs.app_source_sha256 ?? null, | |
| cases_sha256: run.inputs.cases_sha256, n: s.n, errors: run.errors.length, counts: s.counts, rates: s.rates, ci95: s.ci95, | |
| by_category: Object.fromEntries(groupBy(run.rows, (r) => r.category).map(([k, rs]) => [k, summarize(rs, {reps: 1000}).counts])), | |
| by_source: Object.fromEntries(groupBy(run.rows, (r) => r.source ?? "?").map(([k, rs]) => [k, summarize(rs, {reps: 1000}).counts])), regraded: regrade}; | |
| fs.writeFileSync(flag("--json") ?? path.join(dir, "summary.json"), JSON.stringify(summary, null, 1) + "\n"); | |
| } | |
| if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) await main(); | |