"""Check every count file in data/measured/county-counts/ against the totals printed on the County sheet. For each .csv with a matching .totals.csv: - each row: L + T + R + peds == app_total - each approach: column sums == printed grand totals - each hour: sum of app_totals == printed intersection total; and the grand intersection total Any mismatch = a transcription error to fix before the data is used. Exit code 1 if anything fails. """ import csv, pathlib, sys, collections DIR = pathlib.Path(__file__).resolve().parents[1]/"data/measured/county-counts" def rows(p): return [r for r in csv.DictReader(l for l in open(p) if not l.startswith("#"))] def main(): bad = 0 for f in sorted(DIR.glob("*.csv")): if f.name.endswith(".totals.csv"): continue tf = f.with_name(f.stem+".totals.csv") R = rows(f); errs = [] for r in R: s = sum(int(r[k]) for k in ("L", "T", "R", "peds")) if s != int(r["app_total"]): errs.append(f"row {r['hour']} {r['approach']}: L+T+R+peds={s} != {r['app_total']}") if tf.exists(): T = rows(tf); col = collections.defaultdict(lambda: collections.Counter()); hr = collections.Counter() for r in R: for k in ("L", "T", "R", "peds", "app_total"): col[r["approach"]][k] += int(r[k]) hr[r["hour"]] += int(r["app_total"]) for t in T: if t["kind"] == "grand": for k in ("L", "T", "R", "peds", "app_total"): if col[t["key"]][k] != int(t[k]): errs.append(f"grand {t['key']} {k}: sum {col[t['key']][k]} != printed {t[k]}") elif t["kind"] == "int": got = sum(hr.values()) if t["key"] == "grand" else hr[t["key"]] if got != int(t["app_total"]): errs.append(f"intersection total {t['key']}: sum {got} != printed {t['app_total']}") status = "OK" if not errs else f"{len(errs)} ERRORS" print(f"{f.name}: {len(R)} rows, {status}") for e in errs: print(" ", e) bad += len(errs) sys.exit(1 if bad else 0) if __name__ == "__main__": main()