#!/usr/bin/env python3 """Reproduce AMR's calculations from the accompanying CSVs, using only Python's standard library. Run from any directory: python3 /path/to/reproduce.py Optional --self-test proves arithmetic gates reject changed input cells in memory. """ from pathlib import Path import csv,json,sys,copy,math R=Path(__file__).resolve().parent D=R/'data' if (R/'data').is_dir() else R def read(name): with (D/name).open(newline='') as f:return list(csv.DictReader(f)) def close(actual,expected,tol=0.000051): if not math.isfinite(float(actual)) or abs(float(actual)-float(expected))>tol:raise AssertionError((actual,expected)) def check(d): out={} m=d['mass-composition.csv'];assert sum(float(r['Contained mass in 2022 (billion kg)']) for r in m)==62 for r in m:close(r['AMR share of 62 billion kg (%)'],float(r['Contained mass in 2022 (billion kg)'])/62*100) out['metal_share_percent']=float(m[0]['Contained mass in 2022 (billion kg)'])/62*100 for r in d['selected-metal-mass.csv']:close(r['AMR share of 31 billion kg metal total (%)'],float(r['Contained mass in 2022 (billion kg)'])/31*100) v=d['selected-metal-values.csv'];subtotal=sum(float(r['Contained metal value in 2022 (USD billion)']) for r in v) for r in v:close(r['AMR share of reported USD 91 billion total (%)'],float(r['Contained metal value in 2022 (USD billion)'])/91*100) out['selected_contained_value_USD_billion']=subtotal;out['selected_share_percent']=subtotal/91*100 nums=[(13.8,62),(19,31),(28,91)] for r,(n,den) in zip(d['recovery-denominators.csv'],nums):close(r['AMR calculation from rounded inputs (%)'],n/den*100) out['formal_collection_rounded_input_percent']=13.8/62*100;out['recovered_mass_percent']=19/31*100;out['recovered_value_percent']=28/91*100 route=d['management-routes.csv'];out['rounded_route_input_sum_billion_kg']=sum(float(r['2022 e-waste input (billion kg)']) for r in route) out['rounded_route_recovered_metal_sum_billion_kg']=sum(float(r['2022 viable recovered metal (billion kg)']) for r in route) value=d['value-recovery-routes.csv'];out['rounded_route_value_sum_USD_billion']=sum(float(r['Recovered metal value in 2022 (USD billion)']) for r in value[:-1]);out['published_recovered_value_USD_billion']=float(value[-1]['Recovered metal value in 2022 (USD billion)']) running=0 for r in d['economic-bridge.csv']: running+=float(r['2022 signed value (USD billion)']);close(r['AMR running balance (USD billion)'],running) out['societal_net_USD_billion']=running;assert running==-37 h=d['history-endpoints.csv'];assert [int(x['Year']) for x in h]==[2010,2022] cols=['E-waste generated (billion kg per year)','Documented formal collection and recycling (billion kg per year)'];deltas=[] for c,g in zip(cols,d['growth-calculations.csv']): a,b=float(h[0][c]),float(h[1][c]);delta=b-a;deltas.append(delta) close(g['2010 input'],a);close(g['2022 input'],b);close(g['AMR absolute increase (billion kg per year)'],delta) close(g['Elapsed years'],2022-2010);close(g['AMR annual average increase (billion kg per year added each year)'],delta/12) out['absolute_growth_increment_ratio']=deltas[0]/deltas[1] return out names=['mass-composition.csv','selected-metal-mass.csv','selected-metal-values.csv','recovery-denominators.csv','management-routes.csv','value-recovery-routes.csv','economic-bridge.csv','history-endpoints.csv','growth-calculations.csv'] d={n:read(n) for n in names};out=check(d) if '--self-test' in sys.argv: mutations=[('mass-composition.csv',0,'AMR share of 62 billion kg (%)','51'),('selected-metal-values.csv',0,'AMR share of reported USD 91 billion total (%)','25'),('recovery-denominators.csv',1,'AMR calculation from rounded inputs (%)','22.3'),('economic-bridge.csv',3,'AMR running balance (USD billion)','37'),('growth-calculations.csv',0,'AMR absolute increase (billion kg per year)','29')] for file,row,col,val in mutations: bad=copy.deepcopy(d);bad[file][row][col]=val try:check(bad) except AssertionError:continue raise AssertionError('Gate did not reject changed cell: '+file+' '+col) out['negative_arithmetic_tests_passed']=len(mutations) print(json.dumps(out,indent=2))