"""FTS-REPARTO (web) · el antes y el después de fondos_ue_empresa.parquet: el publicado (main ae92e9ba, md5 72c6bf9c…) frente al rehecho con las dos correcciones. Celda = NIF × fuente × año. Cuenta qué cambia, en cuántas celdas y sociedades, y comprueba que ninguna cifra de lo contratado sube (ni por celda ni en el total de la sociedad, que es la cifra de arriba del bloque). Salida: /w/salidas/diferencia_ue.json (solo recuentos) y /w/priv/celdas_cambian.json (las celdas, con su NIF: sociedades, nunca personas). python3 diferencia_ue.py """ import collections import json import sys import duckdb A, D = sys.argv[1:3] c = duckdb.connect() c.execute("SET memory_limit='1500MB'; SET threads=2") COLS = "nif, f, anio, n, coord, importe::DOUBLE importe, sin_reparto, importe_sin_reparto::DOUBLE importe_sin_reparto, programas" a = {(r[0], int(r[1]), int(r[2])): r[3:] for r in c.execute(f"SELECT {COLS} FROM read_parquet('{A}')").fetchall()} d = {(r[0], int(r[1]), int(r[2])): r[3:] for r in c.execute(f"SELECT {COLS} FROM read_parquet('{D}')").fetchall()} CAMPOS = ("n", "coord", "importe", "sin_reparto", "importe_sin_reparto", "programas") def igual(x, y): if isinstance(x, float) or isinstance(y, float): return (x is None) == (y is None) and (x is None or abs(x - y) <= 0.005) return x == y cambia = {} por_campo = collections.Counter() clases = collections.Counter() sube = [] for k in sorted(set(a) | set(d)): x, y = a.get(k), d.get(k) if x is None or y is None: cambia[k] = {"que": "solo_antes" if y is None else "solo_despues"} clases["celda_que_entra_o_sale"] += 1 continue dif = [CAMPOS[i] for i in range(6) if not igual(x[i], y[i])] if not dif: continue for f in dif: por_campo[f] += 1 ia, id_ = x[2], y[2] if ia is not None and id_ is not None and id_ > ia + 0.005: sube.append(k) if ia is not None and id_ is None: clases["contratado: de una cifra a «sin importe»" + (" (era 0 €)" if abs(ia) < 0.005 else " (NO era 0 €)")] += 1 elif ia is not None and id_ is not None and id_ < ia - 0.005: clases["contratado: baja"] += 1 elif "importe" not in dif: clases["contratado: igual (cambian las líneas de cada lado o la suma sin reparto)"] += 1 if y[3] is not None and x[3] is not None and y[3] > x[3]: clases["líneas sin reparto: suben"] += 1 if y[3] is not None and x[3] is not None and y[3] < x[3]: clases["líneas sin reparto: BAJAN"] += 1 cambia[k] = {"antes": dict(zip(CAMPOS, x)), "despues": dict(zip(CAMPOS, y)), "campos": dif} def tot(t, nif): xs = [v[2] for k, v in t.items() if k[0] == nif and k[1] == 0 and v[2] is not None] return sum(xs) if xs else None soc = sorted({k[0] for k in cambia}) sube_soc, total = [], collections.Counter() for nif in soc: ta, td = tot(a, nif), tot(d, nif) if ta is not None and td is not None and td > ta + 0.005: sube_soc.append(nif) if ta is not None and td is None: total["total contratado de la sociedad: de una cifra a «sin importe»" + (" (era 0 €)" if abs(ta) < 0.005 else " (NO era 0 €)")] += 1 elif ta is not None and td is not None and td < ta - 0.005: total["total contratado de la sociedad: baja"] += 1 elif ta == td or (ta is not None and td is not None and abs(ta - td) <= 0.005): total["total contratado de la sociedad: igual"] += 1 r = {"antes": A.rsplit("/", 1)[-1], "despues": D.rsplit("/", 1)[-1], "celdas_antes": len(a), "celdas_despues": len(d), "celdas_que_cambian": len(cambia), "sociedades_que_cambian": len(soc), "por_campo": dict(por_campo), "clases": dict(clases), "totales_de_sociedad": dict(total), "celdas_en_que_sube_lo_contratado": len(sube), "sociedades_en_que_sube_lo_contratado": len(sube_soc), "fuentes": dict(collections.Counter("FTS" if k[1] == 0 else "CORDIS" for k in cambia)), "suma_contratado_antes": round(sum(v[2] for k, v in a.items() if k[1] == 0 and v[2] is not None), 2), "suma_contratado_despues": round(sum(v[2] for k, v in d.items() if k[1] == 0 and v[2] is not None), 2), "suma_sin_reparto_antes": round(sum(v[4] for k, v in a.items() if k[1] == 0 and v[4] is not None), 2), "suma_sin_reparto_despues": round(sum(v[4] for k, v in d.items() if k[1] == 0 and v[4] is not None), 2)} json.dump(r, open("/w/salidas/diferencia_ue.json", "w"), ensure_ascii=False, indent=1) json.dump([{"nif": k[0], "f": k[1], "anio": k[2], **v} for k, v in cambia.items()], open("/w/priv/celdas_cambian.json", "w"), ensure_ascii=False, indent=1, default=str) print(json.dumps(r, ensure_ascii=False))