Study files

study/compare_grades.py

2.1 KB · View on GitHub →

"""Compare two graders' tables. Usage: python3 -I compare_grades.py <grades_dir>
Reads <pair>-A.md / <pair>-B.md, prints agreement stats and writes disagreements.md."""
import re, sys, pathlib, collections
d = pathlib.Path(sys.argv[1])
def load(p):
    rows = {}
    for l in p.read_text().splitlines():
        c = [x.strip() for x in l.strip().strip('|').split('|')]
        if len(c) >= 8 and c[0] not in ('Runtime',) and not set(c[0]) <= set('-: '):
            rows[(c[0], c[1])] = dict(D=c[2], B=c[3], c=c[4], cfg=c[5], refs=c[6], why=c[7])
    return rows
stats = collections.Counter(); dis = []; ordinal = 0
for a in sorted(d.glob('*-A.md')):
    b = a.with_name(a.name.replace('-A.md', '-B.md'))
    A, B = load(a), load(b)
    keys = sorted(set(A) | set(B))
    for k in keys:
        if k not in A or k not in B: stats['missing'] += 1; dis.append((k, A.get(k), B.get(k), 'missing')); continue
        x, y = A[k], B[k]
        for m in ('D', 'B'):
            if x[m].isdigit() and y[m].isdigit():
                stats[f'{m}_n'] += 1; diff = abs(int(x[m]) - int(y[m]))
                stats[f'{m}_exact'] += diff == 0; stats[f'{m}_within1'] += diff <= 1
            else:
                stats[f'{m}_cat_n'] += 1; stats[f'{m}_cat_exact'] += x[m] == y[m]
        if x['D'] != y['D'] or x['B'] != y['B']: dis.append((k, x, y, 'level'))
for m in ('D', 'B'):
    n = stats[f'{m}_n']
    if n: print(f"{m}: n={n} exact={stats[f'{m}_exact']/n:.0%} within1={stats[f'{m}_within1']/n:.0%}")
    if stats[f'{m}_cat_n']: print(f"{m} categorical/n-a: n={stats[f'{m}_cat_n']} exact={stats[f'{m}_cat_exact']}")
print("missing rows", stats['missing'], "disagreements", len(dis))
with open(d / 'disagreements.md', 'w') as f:
    f.write('| Runtime | Column | A D/B c | B D/B c | A rationale | B rationale |\n|---|---|---|---|---|---|\n')
    for k, x, y, why in dis:
        fx = f"{x['D']}/{x['B']} {x['c']} {x['refs']}" if x else '—'; fy = f"{y['D']}/{y['B']} {y['c']} {y['refs']}" if y else '—'
        f.write(f"| {k[0]} | {k[1]} | {fx} | {fy} | {x['why'] if x else ''} | {y['why'] if y else ''} |\n")