"""V-INT · cotejo impl. 1 frente a impl. 2 (DEFINICIONES_PREVIAS §7): puntuaciones fila a fila y métricas por par.""" import csv, sys C = '/vint' def f(x): return None if x in ('', None) else float(x) i1 = {(r['etiqueta'], r['nif'], int(r['ejercicio']), r['ind']): r for r in csv.DictReader(open(f'{C}/interno/i1_scores.csv'))} i2 = {(r['etiqueta'], r['nif'], int(r['anio']), r['ind']): r for r in csv.DictReader(open(f'{C}/interno/i2_scores.csv'))} print('# V-INT · cotejo de las dos implementaciones\n') print(f'Filas entidad-año × indicador: impl. 1 {len(i1)}, impl. 2 {len(i2)}; comunes {len(set(i1) & set(i2))}; ' f'solo 1: {len(set(i1) - set(i2))}; solo 2: {len(set(i2) - set(i1))}\n') for k in sorted(set(i1) ^ set(i2))[:15]: print(' distinta presencia:', k, 'en 1' if k in i1 else 'en 2') dif = {'y': 0, 'n_el': 0, 'v': 0, 's1': 0, 's2': 0, 'tamano': 0} ej = {} for k in set(i1) & set(i2): a, b = i1[k], i2[k] for c1, c2, nm, tol in (('y', 'y', 'y', 0), ('n_el', 'n_el', 'n_el', 0), ('v_dir', 'v', 'v', 1e-8), ('s1_dir', 's1', 's1', 1e-6), ('s2_dir', 's2', 's2', 1e-6), ('tamano', 'tamano', 'tamano', 1e-9)): x, y = f(a[c1]), f(b[c2]) if (x is None) != (y is None) or (x is not None and abs(x - y) > tol): dif[nm] += 1 ej.setdefault(nm, (k, x, y)) print('Diferencias por columna (tolerancia: v 1e-8, S1/S2 1e-6):', dif) for nm, e in ej.items(): print(' ejemplo', nm, e) m1 = {(r['etiqueta'], r['ind']): r for r in csv.DictReader(open(f'{C}/salidas/03_metricas_i1.csv'))} m2 = {(r['etiqueta'], r['ind']): r for r in csv.DictReader(open(f'{C}/salidas/04_metricas_i2.csv'))} print('\n| Etiqueta | Ind. | n (1 / 2) | AUC 1 | AUC 2 | |Δ AUC| | IC 1 | IC 2 | OR 1 | OR 2 | estrat. 1 / 2 | Veredicto 1 | Veredicto 2 | ¿igual? |') print('|---|---|---|---|---|---|---|---|---|---|---|---|---|---|') malos = 0 for k in m1: a, b = m1[k], m2.get(k) if b is None: if k[1] != 'SERIES': print(f'| {k[0]} | {k[1]} | falta en impl. 2 |'); malos += 1 continue n1, n2 = f"{a['n_pos']}/{a['n_neg']}", f"{b['n_pos']}/{b['n_neg']}" A1, A2 = f(a['auc']), f(b['auc']) dA = abs(A1 - A2) if A1 is not None and A2 is not None else None ic_ok = all(abs((f(a[c]) or 0) - (f(b[c]) or 0)) <= 0.03 for c in ('ic_inf', 'ic_sup')) o1, o2 = f(a['or']), f(b['or']) or_ok = (o1 is None and o2 is None) or (o1 is not None and o2 is not None and abs(o1 - o2) / max(abs(o1), 1e-12) < 1e-3) v1 = a['veredicto'].replace(' (pendiente de e)', '') igual = n1 == n2 and (dA is None or dA < 1e-9) and ic_ok and or_ok and v1 == b['veredicto'] malos += not igual print(f"| {k[0]} | {k[1]} | {n1} / {n2} | {A1} | {A2} | {dA} | {a['ic_inf'][:5]}-{a['ic_sup'][:5]} | {b['ic_inf'][:5]}-{b['ic_sup'][:5]} | " f"{a['or'][:8]} | {b['or'][:8]} | {a['auc_estrat'][:5]} / {b['auc_estrat'][:5]} | {v1} | {b['veredicto']} | {'sí' if igual else 'NO'} |") print(f'\nPares que no cumplen la aceptación: {malos}')