"""Reproduce AMR comparisons from the matching published CSVs.""" from collections import Counter from decimal import Decimal as D from pathlib import Path import csv import json HERE = Path(__file__).resolve().parent DATA = HERE/'data' if (HERE/'data').is_dir() else HERE def rows(name): with (DATA/(name+'.csv')).open(newline='') as file: return list(csv.reader(file))[1:] def pct(part,total): return f'{D(part)*100/D(total):.2f}' routes = rows('byproduct-recovery-routes') stages = Counter(r[2] for r in routes) assert len(routes)==16 and stages=={'Refining':14,'Mining':1,'Smelting':1} for stage,count,share in rows('recovery-route-counts'): assert stages[stage]==int(count) and pct(count,len(routes))==share paired = sorted(k for k,n in Counter(r[1] for r in routes).items() if n>1) assert paired == ['Cobalt','Gallium','Silver'] reliance = {r[0]:r[1] for r in rows('us-import-reliance-2025')} assert reliance=={'Gallium':'100%','Germanium':'>50%','Indium':'100%','Tellurium':'>25%'} print(json.dumps({'route_counts':dict(stages),'refining_share_pct':pct(stages['Refining'],len(routes)),'byproducts_with_multiple_hosts':paired,'usgs_import_reliance':reliance}))