"""NE1 · 31 · cotejo impl1 frente a impl2 (§7).""" import json, math a = json.load(open('/w/interno/celdas_impl1.json')); b = json.load(open('/w/interno/celdas_impl2.json')) def rel(x, y): if x is None or y is None: return float('inf') nd = lambda v: isinstance(v, float) and (math.isnan(v) or math.isinf(v)) # «no definido» (OR de antes = 0): inf en impl1, nan en impl2 if nd(x) and nd(y): return 0.0 if x == y: return 0.0 return abs(x - y) / max(abs(x), abs(y)) cel = [k for k in a if isinstance(a[k], dict)]; esc = [k for k in a if not isinstance(a[k], dict)] falta = [k for k in a if k not in b]; sobra = [k for k in b if k not in a] ok = 0; malas = [] for k in cel: if k not in b: continue x, y = a[k], b[k] if x['n_v'] == y['n_v'] and x['O'] == y['O'] and rel(x['E'], y['E']) <= 0.005: ok += 1 else: malas.append((k, x, y)) bal = [k for k in esc if k.startswith('bal|')]; bal_ok = sum(1 for k in bal if a[k] == b.get(k)) pts = [k for k in esc if k.startswith('c|') or k.startswith('d|')] pts_mal = [(k, a[k], b.get(k)) for k in pts if not (rel(a[k], b.get(k)) <= 0.005 or abs((a[k] or 0) - (b.get(k) or 0)) <= 0.005)] R = {'celdas_exceso': len(cel), 'identicas': ok, 'pct': round(100 * ok / max(len(cel), 1), 3), 'distintas': malas[:20], 'balance_componentes': len(bal), 'balance_identicos': bal_ok, 'puntuales_ROR_DiD': len(pts), 'puntuales_fuera_tolerancia': pts_mal, 'faltan_en_impl2': falta, 'sobran_en_impl2': sobra[:20], 'max_rel_E': max((rel(a[k]['E'], b[k]['E']) for k in cel if k in b), default=0)} R['pasa'] = R['pct'] >= 99.5 and bal_ok == len(bal) and not pts_mal and not falta json.dump(R, open('/w/salidas/31_cotejo_impl2.json', 'w'), ensure_ascii=False, indent=1, default=str); print(json.dumps(R, ensure_ascii=False, default=str)[:3000])