43 lines
2.9 KiB
Python
43 lines
2.9 KiB
Python
"""Reconcile supplied exemplars without merging statistical series or inventing observations."""
|
|
import argparse
|
|
from decimal import Decimal
|
|
import json
|
|
from pathlib import Path
|
|
from demography_workbook import parse
|
|
|
|
|
|
def reconcile(directory):
|
|
national = parse(directory / 'Dados demograficos.xlsx', 2017)
|
|
comparisons = []
|
|
for province in ('Manica', 'Inhambane'):
|
|
annual = parse(directory / (province + '.xlsx'))
|
|
detail = [r for r in annual['records'] if r['level'] == 'province' and r['year'] == 2017]
|
|
for baseline in national['records']:
|
|
if baseline['province'] != province or baseline['indicator'] != 'population':
|
|
continue
|
|
age = baseline['age_group']
|
|
bands = {'all': ['all'], '0-14': ['0-4', '5-9', '10-14'],
|
|
'15-64': [f'{i}-{i+4}' for i in range(15, 65, 5)], '65+': ['65-69', '70-74', '75-79', '80+']}[age]
|
|
matches = [r for r in detail if r['age_group'] in bands and r['sex'] == baseline['sex'] and r['residence'] == baseline['residence']]
|
|
calculated = sum(Decimal(r['numeric_value']) for r in matches) if len(matches) == len(bands) else None
|
|
difference = calculated - Decimal(baseline['numeric_value']) if calculated is not None else None
|
|
comparisons.append({'province': province, 'year': 2017, 'age_group': age, 'sex': baseline['sex'], 'residence': baseline['residence'],
|
|
'national_value': baseline['original_value'], 'national_reference': baseline['sheet'] + '!' + baseline['cell'],
|
|
'annual_derived_sum': str(calculated), 'annual_references': [r['sheet'] + '!' + r['cell'] for r in matches],
|
|
'difference': str(difference), 'state': 'matching_within_precision' if difference is not None and abs(difference) <= Decimal('0.000001') else 'divergent_or_incomplete'})
|
|
return {'method': 'Independent exact decimal sums; tolerance 0.000001 person for Excel storage residues. Derived sums are reconciliation evidence, not new INE observations.',
|
|
'type_compatibility': 'Annual sheets explicitly say projected; national sheets do not state observed/projected. Matching numbers do not equate statistical versions.',
|
|
'baseline_year': 'Population sheet has no printed year: 2017 explicitly supplied in import metadata; age/vital sheets state 2017.',
|
|
'comparisons': comparisons, 'matching': sum(r['state'] == 'matching_within_precision' for r in comparisons),
|
|
'divergent_or_incomplete': sum(r['state'] != 'matching_within_precision' for r in comparisons)}
|
|
|
|
|
|
if __name__ == '__main__':
|
|
cli = argparse.ArgumentParser()
|
|
cli.add_argument('directory', type=Path)
|
|
cli.add_argument('--output', type=Path, required=True)
|
|
args = cli.parse_args()
|
|
report = reconcile(args.directory)
|
|
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8')
|
|
print(json.dumps({k: v for k, v in report.items() if k != 'comparisons'}, ensure_ascii=False))
|