"""Alisson Bento: reproduce aggregate contrasts; optional official-source verification. See rights.md for NIC.br source-data attribution and CC BY 4.0 terms. """ import argparse,csv,hashlib,io,json,pathlib,zipfile P=pathlib.Path(__file__).resolve().parent ap=argparse.ArgumentParser();ap.add_argument('--archive');args=ap.parse_args() rows=list(csv.DictReader((P/'estimates.csv').open(encoding='utf-8'))) assert len(rows)==34 idx={(r['indicator'],r['group'],r['category']):r for r in rows} if args.archive: import openpyxl raw=pathlib.Path(args.archive).read_bytes() expected=json.loads((P/'sources.json').read_text())['official_archive']['sha256'] assert hashlib.sha256(raw).hexdigest()==expected,'Source archive hash differs' with zipfile.ZipFile(io.BytesIO(raw)) as z: for kind,field in [('proporcao','percent'),('margem_de_erro','margin95_pp')]: name=next(n for n in z.namelist() if 'tabela_'+kind+'_v' in n) w=openpyxl.load_workbook(io.BytesIO(z.read(name)),read_only=True,data_only=True) for r in rows: s=w[r['indicator']];s.reset_dimensions() assert abs(float(s[r['source_excel_column']+r['source_excel_row']].value)-float(r[field]))<1e-9 w.close() out=[] for group,a,b in [('ÁREA','Urbana','Rural'),('RENDA FAMILIAR','Mais de 5 SM até 10 SM','Até 1 SM'),('RENDA FAMILIAR','Mais de 10 SM','Até 1 SM')]: for ind in ['H2','H3']: x,y=idx[ind,group,a],idx[ind,group,b] out.append(dict(indicator=ind,group=group,category_a=a,category_b=b,percent_a=x['percent'],margin95_a_pp=x['margin95_pp'],percent_b=y['percent'],margin95_b_pp=y['margin95_pp'],descriptive_difference_pp=round(float(x['percent'])-float(y['percent']),4),difference_inference='Not computed; joint covariance unavailable')) with (P/'descriptive-contrasts.csv').open('w',encoding='utf-8',newline='') as f: w=csv.DictWriter(f,fieldnames=out[0],lineterminator='\n');w.writeheader();w.writerows(out) print('Verified 34 aggregate rows and reproduced six descriptive contrasts. No significance tests.')