#!/usr/bin/env python3 """Paired comparison of two CV runs (range_predictions.csv from compare_ranges.py --save-predictions). Scores both runs on the pairs they share (optionally minus --exclude-views and --exclude-datasets): Spearman per AQ and mean, range coverage / width / Winkler (80%), and a paired bootstrap over place groups for the difference B - A in mean Spearman and Winkler. """ from __future__ import annotations import sys as _sys from pathlib import Path as _Path _SCRIPTS = next(_p for _p in _Path(__file__).resolve().parents if _p.name == "scripts") _sys.path[:0] = [str(_SCRIPTS.parent), str(_SCRIPTS.parent / "models")] + [str(_d) for _d in sorted(_SCRIPTS.rglob("*")) if _d.is_dir() and _d.name != "__pycache__"] import argparse from pathlib import Path import numpy as np import pandas as pd from scipy.stats import spearmanr AQ = ["pleasant", "eventful", "chaotic", "vibrant", "uneventful", "calm", "annoying", "monotonous"] NEW = ["sonicurban", "open_sipset_v2", "eth_binaural_zurich", "taiwan_linebot"] def metrics(d, idx): P = d[[f"pred_{q}" for q in AQ]].to_numpy()[idx] T = d[[f"target_{q}" for q in AQ]].to_numpy()[idx] lo = d[[f"lo_{q}" for q in AQ]].to_numpy()[idx] hi = d[[f"hi_{q}" for q in AQ]].to_numpy()[idx] rho = np.array([spearmanr(P[:, k], T[:, k])[0] for k in range(8)]) w = hi - lo wink = w + (2 / 0.2) * np.clip(lo - T, 0, None) + (2 / 0.2) * np.clip(T - hi, 0, None) return rho, float(((T >= lo) & (T <= hi)).mean()), float(w.mean()), float(wink.mean()) def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument("a", type=Path) p.add_argument("b", type=Path) p.add_argument("--exclude-views", type=Path, nargs="*", default=[]) p.add_argument("--exclude-datasets", nargs="*", default=NEW) p.add_argument("--boot", type=int, default=500) a = p.parse_args() A, B = pd.read_csv(a.a), pd.read_csv(a.b) keep = set(A.view_id) & set(B.view_id) for x in a.exclude_views: keep -= set(pd.read_csv(x).view_id) A = A[A.view_id.isin(keep) & ~A.training_dataset.isin(a.exclude_datasets)].set_index("view_id").sort_index() B = B.set_index("view_id").loc[A.index] assert np.allclose(A[[f"target_{q}" for q in AQ]], B[[f"target_{q}" for q in AQ]]), "targets differ" n = len(A); allidx = np.arange(n) print(f"{n} shared pairs, {A.group.nunique()} place groups") print(f"{'run':4s} " + " ".join(f"{q[:6]:>7s}" for q in AQ) + " mean cover width Winkler") for name, d in (("A", A), ("B", B)): r, c, w, k = metrics(d, allidx) print(f"{name:4s} " + " ".join(f"{v:7.3f}" for v in r) + f" {r.mean():.3f} {c:.3f} {w:.3f} {k:.3f}") g = A.group.astype(str).to_numpy(); ug = np.unique(g); idx = {u: np.flatnonzero(g == u) for u in ug} rng = np.random.default_rng(0); dr, dk = [], [] for _ in range(a.boot): s = np.concatenate([idx[u] for u in rng.choice(ug, len(ug))]) ra, _, _, ka = metrics(A, s); rb, _, _, kb = metrics(B, s) dr.append(rb.mean() - ra.mean()); dk.append(kb - ka) ra, _, _, ka = metrics(A, allidx); rb, _, _, kb = metrics(B, allidx) print(f"B - A mean Spearman {rb.mean() - ra.mean():+.4f} [{np.percentile(dr, 2.5):+.4f}, {np.percentile(dr, 97.5):+.4f}]") print(f"B - A Winkler {kb - ka:+.4f} [{np.percentile(dk, 2.5):+.4f}, {np.percentile(dk, 97.5):+.4f}] (lower is better)") if __name__ == "__main__": main()