"""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}' records = rows('top-three-shares') for row in records: assert int(row[5]) == int(row[4])-int(row[3]) means = {} for year,status,number,up,down,gap in rows('equal-weight-summary'): subset = [row for row in records if row[1]==year] assert len(subset)==int(number) extract = sum(D(r[3]) for r in subset)/len(subset) refining = sum(D(r[4]) for r in subset)/len(subset) assert [f'{a:.2f}' for a in (extract,refining,refining-extract)] == [up,down,gap] means[year] = [up,down,gap] current = {r[0]:int(r[5]) for r in records if r[1]=='2025'} assert len(current)==6 and all(n>0 for n in current.values()) assert max(current,key=current.get)=='Lithium' and current['Lithium']==20 assert means['2025']==['76.67','86.17','9.50'] print(json.dumps({'mean_stage_shares_and_spread': means, '2025_stage_gaps':current}))