File size: 4,328 Bytes
edd1589 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | # import json
# from collections import Counter
# import sys
# def tally_original_mpx(jsonl_path):
# nothard=0
# counter = Counter()
# with open(jsonl_path, 'r') as f:
# for line in f:
# item = json.loads(line)
# bucket = item.get("original_dim_bucket")
# counter[bucket] += 1
# if item.get("is_hard") == False:
# nothard += 1
# return counter, nothard
# if __name__ == "__main__":
# if len(sys.argv) != 2:
# print("Usage: python tally_mpx.py <path_to_jsonl>")
# sys.exit(1)
# jsonl_path = sys.argv[1]
# counts, nothard = tally_original_mpx(jsonl_path)
# print(f"Total items with is_hard == False: {nothard}")
# print("Original MPX bucket counts (was_resized == False):")
# for bucket in sorted(counts):
# print(f"{bucket} MPX: {counts[bucket]}")
#!/usr/bin/env python3
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):
# Per-bucket counters
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)
# a) only original (non-resized)
if obj.get("was_resized", True):
continue
overall_total += 1
is_correct = bool(obj.get("pred_result", False))
if is_correct:
overall_correct += 1
# c) bucketed accuracy
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)) |