"""BU2 · 31 · cotejo principal (20) frente a segunda (30), §7.""" import json, numpy as np, pandas as pd A = json.load(open('/w/salidas/20_analisis.json')); B = json.load(open('/w/salidas/30_impl2.json')) R = {} ea = {f"{e['anio']}-{e['tipo']}": [e['franja_exp'], e['franja_no'], e['control_exp'], e['control_no']] for e in A['estratos']} R['celdas_2x2_iguales'] = sum(ea[k] == B['estratos'].get(k) for k in ea); R['celdas_2x2_total'] = len(ea) def rel(a, b): return abs(a - b) / abs(b) if b else abs(a - b) R['or_rel_max'] = {} for k in ('principal_mh', 'dentro_organo_mh', 'sin_pivote_mh'): R['or_rel_max'][k] = max(rel(A[k][i], B[k][i]) for i in range(3)) for y in A['loo']: R['or_rel_max'][f'loo_{y}'] = max(rel(A['loo'][y][i], B['loo'][y][i]) for i in range(3)) d = pd.read_csv('/w/salidas/densidad_hueco.csv') bins_ok = 0; bins_tot = 0; ee = [] for h in A['hueco']: k = f"{h['tipo']}-{h['anio']}"; nb = d[(d.tipo == h['tipo']) & (d.anio == h['anio'])].sort_values('b').n.tolist(); nb2 = B['hueco'][k]['bins'] bins_ok += sum(int(a == b) for a, b in zip(nb, nb2)); bins_tot += 60 ee += [rel(h['E_f'], B['hueco'][k]['E_f']), rel(h['E_e'], B['hueco'][k]['E_e']), rel(h['H'], B['hueco'][k]['E_e'] - B['hueco'][k]['O_e'])] R['bins_iguales'] = f'{bins_ok}/{bins_tot}'; R['hueco_E_H_rel_max'] = max(ee) R['pasa'] = (R['celdas_2x2_iguales'] / R['celdas_2x2_total'] >= 0.995 and bins_ok / bins_tot >= 0.995 and max(R['or_rel_max'].values()) <= 0.005 and R['hueco_E_H_rel_max'] <= 0.005) print(json.dumps(R, indent=1)); json.dump(R, open('/w/salidas/31_cotejo_impl2.json', 'w'), indent=1)