"""AMR downloadable numerical validation. Run with Python 3 in the extracted bundle.""" from pathlib import Path from decimal import Decimal as D import csv,json R=Path(__file__).resolve().parent def read(key): with (R/(key+'.csv')).open(newline='') as f:return list(csv.reader(f))[1:] def near(x,y,tol=D('.011')): assert abs(D(x)-D(y))<=tol,(x,y) def pct(x,y):return D(x)*100/D(y) world=read('world-mine-production') a,b=map(D,next(r for r in world if r[1]=='World total')[3:5]) for r in world: assert D(r[5])==D(r[4])-D(r[3]);near(r[6],pct(r[4],b)) names=[r for r in world if r[1]=='Named country'] ranking=sorted(names,key=lambda r:D(r[4]),reverse=True) for r in read('named-producer-concentration'): k=int(r[0]);assert r[1]==', '.join(x[0] for x in ranking[:k]) assert D(r[2])==sum(D(x[4]) for x in ranking[:k]);near(r[4],pct(r[2],r[3])) for r in read('country-output-change-contributions'): assert D(r[3])==D(r[2])-D(r[1]) and D(r[4])==b-a;near(r[5],pct(r[3],r[4])) for r in read('same-edition-concentration-change'): near(r[4],pct(r[2],r[3]));near(r[8],pct(r[6],r[7]));near(r[9],pct(r[6],r[7])-pct(r[2],r[3])) for r in read('same-year-2024-estimate-revisions'): assert D(r[5])==D(r[4])-D(r[3]);near(r[6],pct(r[5],r[3])) for r in read('reserve-output-conditional-ratios'):near(r[4],D(r[2])/D(r[3])) pr=read('price-benchmark-index') for row in pr: start=next(x for x in pr if x[0]==row[0] and x[2]=='2021');near(row[4],pct(row[3],start[3])) origins=read('us-import-origin-shares') for form,name,value in read('largest-named-import-source'): amount=max(D(x[2]) for x in origins if x[0]==form and x[1]!='Other' and not x[2].startswith('<')) assert amount==D(value) and [form,name,value] in origins for filename in R.glob('*.csv'): key=filename.stem if key=='source-cell-ledger':continue for row in read(key): if key=='antimony-apparent-consumption-bridge':assert sum(D(x) for x in row[1:5])==D(row[5]) and D(row[6])-D(row[5])==D(row[7]) elif key=='tungsten-price-content-conversion':near(row[3],D(row[1])/D(row[2])) elif key=='tin-recycling-and-refined-balance': assert D(row[3])==D(row[1])+D(row[2]);near(row[4],pct(row[1],row[3]));assert D(row[6])==D(row[1])+D(row[5]) and D(row[8])==D(row[7])-D(row[6]) elif key=='chromium-recycled-and-net-import-balance': assert D(row[4])==D(row[2])-D(row[3]) and D(row[5])==D(row[1])+D(row[4]) and D(row[7])==D(row[6])-D(row[5]);near(row[8],pct(row[1],row[6])) elif key=='molybdenum-mine-trade-balance': assert D(row[4])==D(row[2])-D(row[3]) and D(row[7])==D(row[5])-D(row[6]);assert D(row[8])==D(row[1])+D(row[4])+D(row[7]) and D(row[10])==D(row[9])-D(row[8]) elif key=='battery-grade-manganese-stage-benchmarks':assert read(key)[2][3]=='45' and D(read(key)[1][3])+D(read(key)[2][3])==100 elif key=='us-tin-end-uses':assert sum(D(x[1]) for x in read(key))==100 # Every cell must have a matching source-ledger record. This catches transcription or stale file errors. ledger={} with (R/'source-cell-ledger.csv').open(newline='') as f: for row in csv.reader(f): if row[0]=='Dataset':continue assert row[4].startswith('https://') ledger.setdefault((row[0],int(row[1]),row[2]),set()).add(row[3]) count=0;rows=0 for path in sorted(R.glob('*.csv')): if path.name=='source-cell-ledger.csv':continue with path.open(newline='') as f: for i,row in enumerate(csv.DictReader(f),2): assert None not in row for field,value in row.items():assert value in ledger[(path.name,i,field)],(path.name,field,value) rows+=1 count+=1 print(json.dumps({'status':'PASS','datasets':count,'rows':rows,'arithmetic_and_ledger':'verified'}))