| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
|
|
| |
|
|
|
|
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
|
|
| |
| |
| |
|
|
| |
| import os |
| import sys |
| import glob |
| import json |
| from collections import defaultdict |
|
|
| BUCKETS = (1.0, 2.0, 4.0, 6.0, 8.0) |
|
|
| def iter_jsonl_paths(path: str, pattern: str = "*.jsonl"): |
| """Yield JSONL file paths from either a single file or a directory.""" |
| if os.path.isfile(path): |
| yield path |
| return |
| if os.path.isdir(path): |
| files = sorted(glob.glob(os.path.join(path, pattern))) |
| for fp in files: |
| yield fp |
| return |
| raise FileNotFoundError(f"Path not found: {path}") |
|
|
| def tally_accuracy(paths, buckets=BUCKETS): |
| |
| totals = defaultdict(int) |
| corrects = defaultdict(int) |
|
|
| overall_total = 0 |
| overall_correct = 0 |
|
|
| for fp in paths: |
| with open(fp, "r", encoding="utf-8") as f: |
| for line in f: |
| line = line.strip() |
| if not line: |
| continue |
|
|
| obj = json.loads(line) |
|
|
| |
| if obj.get("was_resized", True): |
| continue |
|
|
| overall_total += 1 |
| is_correct = bool(obj.get("pred_result", False)) |
| if is_correct: |
| overall_correct += 1 |
|
|
| |
| b = obj.get("original_mpx_bucket", None) |
| if b is None: |
| continue |
| try: |
| b = float(b) |
| except (TypeError, ValueError): |
| continue |
|
|
| totals[b] += 1 |
| if is_correct: |
| corrects[b] += 1 |
|
|
| overall_acc = (overall_correct / overall_total) if overall_total else 0.0 |
|
|
| table = [] |
| for b in buckets: |
| n = totals.get(float(b), 0) |
| c = corrects.get(float(b), 0) |
| acc = (c / n) if n else 0.0 |
| table.append( |
| { |
| "original_mpx_bucket": b, |
| "n": n, |
| "correct": c, |
| "accuracy": acc, |
| "accuracy_pct": round(acc * 100, 2), |
| } |
| ) |
|
|
| return overall_acc, overall_total, table |
|
|
| def main(argv): |
| if len(argv) < 2: |
| print( |
| "Usage:\n" |
| " tally.py /path/to/file.jsonl\n" |
| " tally.py /path/to/dir/ [--pattern '*.jsonl']\n", |
| file=sys.stderr, |
| ) |
| return 2 |
|
|
| path = argv[1] |
| pattern = "*.jsonl" |
| if "--pattern" in argv: |
| i = argv.index("--pattern") |
| if i + 1 >= len(argv): |
| raise SystemExit("--pattern requires a value (e.g. '*.jsonl')") |
| pattern = argv[i + 1] |
|
|
| paths = list(iter_jsonl_paths(path, pattern=pattern)) |
| if not paths: |
| raise FileNotFoundError(f"No files matched {pattern} in {path}") |
|
|
| overall_acc, n, table = tally_accuracy(paths) |
|
|
| print(f"Overall accuracy (original only): {overall_acc:.6f} ({overall_acc*100:.2f}%)") |
| print(f"Original-only N: {n}\n") |
|
|
| header = f"{'bucket':>8} {'n':>10} {'correct':>10} {'accuracy':>10} {'acc_%':>8}" |
| print(header) |
| print("-" * len(header)) |
| for r in table: |
| print( |
| f"{r['original_mpx_bucket']:>8.1f} " |
| f"{r['n']:>10d} " |
| f"{r['correct']:>10d} " |
| f"{r['accuracy']:>10.6f} " |
| f"{r['accuracy_pct']:>8.2f}" |
| ) |
|
|
| return 0 |
|
|
| if __name__ == "__main__": |
| raise SystemExit(main(sys.argv)) |