# ============================================================================= # Author: Simon-Pierre Boucher # Contact: contact@spboucher.ai # ============================================================================= """Compare regenerated result CSVs (_verify/results) against the originals (results/). Numeric columns must match within rtol=1e-9 (and we report whether they are byte-identical); non-numeric columns must match exactly.""" import sys from pathlib import Path import numpy as np import pandas as pd ROOT = Path(__file__).resolve().parents[1] ORIG = ROOT / "results" NEW = ROOT / "_verify" / "results" overall_ok = True rows = [] for new_file in sorted(NEW.glob("*.csv")): name = new_file.name orig_file = ORIG / name if not orig_file.exists(): rows.append((name, "NEW (no original to compare)")) continue byte_identical = new_file.read_bytes() == orig_file.read_bytes() if byte_identical: rows.append((name, "IDENTICAL (byte-for-byte)")) continue a = pd.read_csv(orig_file) b = pd.read_csv(new_file) if a.shape != b.shape: rows.append((name, f"MISMATCH shape {a.shape} vs {b.shape}")) overall_ok = False continue if list(a.columns) != list(b.columns): rows.append((name, f"MISMATCH columns")) overall_ok = False continue bad_cols = [] max_rel = 0.0 for col in a.columns: if pd.api.types.is_numeric_dtype(a[col]) and pd.api.types.is_numeric_dtype(b[col]): av, bv = a[col].values.astype(float), b[col].values.astype(float) both_nan = np.isnan(av) & np.isnan(bv) close = np.isclose(av, bv, rtol=1e-9, atol=1e-15, equal_nan=True) if not (close | both_nan).all(): bad_cols.append(col) with np.errstate(all="ignore"): rel = np.abs(av - bv) / np.maximum(np.abs(av), 1e-300) rel = rel[~(both_nan | np.isnan(rel))] if len(rel): max_rel = max(max_rel, np.nanmax(rel)) else: if not a[col].fillna("§na§").astype(str).equals( b[col].fillna("§na§").astype(str)): bad_cols.append(col) if bad_cols: rows.append((name, f"MISMATCH in columns {bad_cols} (max rel diff {max_rel:.2e})")) overall_ok = False else: rows.append((name, f"EQUAL numerically (max rel diff {max_rel:.2e})")) width = max(len(r[0]) for r in rows) + 2 for name, status in rows: print(f"{name:<{width}} {status}") print("\n" + ("ALL COMPARED FILES MATCH" if overall_ok else "DISCREPANCIES FOUND")) sys.exit(0 if overall_ok else 1)