"""Python standard library. Run beside genre-prevalences.csv; writes profile-distances.csv.""" from pathlib import Path from decimal import Decimal, ROUND_FLOOR, ROUND_CEILING import csv,json P=Path(__file__).resolve().parent with (P/'genre-prevalences.csv').open(encoding='utf-8',newline='') as f: rows=list(csv.DictReader(f)) groups={} for row in rows:groups.setdefault((row['territory'],row['territory_type']),[]).append(row) out=[] for (name,kind),rr in groups.items(): assert len(rr)==27 and len({r['genre_id'] for r in rr})==27 ok=all(r['difference_pp']!='' for r in rr) d=sum(abs(Decimal(r['percent'])-Decimal(r['national_percent'])) for r in rr)/27 if ok else None if ok: assert all(Decimal(r['difference_pp'])==Decimal(r['percent'])-Decimal(r['national_percent']) for r in rr) low=max(Decimal(0),d-Decimal('.1'));high=d+Decimal('.1') if kind=='national':low=high=d out.append(dict(territory=name,territory_type=kind,categories=27,mean_absolute_deviation_pp=f'{d:.6f}' if ok else '',rounding_only_lower_pp=str(low.quantize(Decimal('.000001'),rounding=ROUND_FLOOR)) if ok else '',rounding_only_upper_pp=str(high.quantize(Decimal('.000001'),rounding=ROUND_CEILING)) if ok else '',status='complete' if ok else 'withheld_missing')) with (P/'profile-distances.csv').open('w',encoding='utf-8',newline='') as f: w=csv.DictWriter(f,fieldnames=out[0].keys(),lineterminator='\n');w.writeheader();w.writerows(out) assert len([r for r in out if r['territory_type']=='region'])==20 print(json.dumps(out,indent=2,ensure_ascii=False))