"""P4 Volve post hoc description (not part of protocol_volve; computed after G-D, for scale only). Reads the registered combined-ensemble predictions (outputs/p4/summary/predictions.npz) and the calibrated reference, and writes outputs/p4/posthoc/: wells.csv per scheme and outer well: label statistics, a constant prediction at the pooled label mean of the outer training wells (no seismic), and, per method (σ_a = 7 ms), RMSE split into mean bias and the SD of the error. macro.csv the same, unweighted mean over wells per scheme. Nothing here chooses a method, budget or setting, and no G-D number is changed. """ from __future__ import annotations import json import numpy as np import pandas as pd from ml_porosity.paths import outputs_dir from ml_porosity.volve.data import VolveData, load_arrays from ml_porosity.volve.folds import outer_folds from ml_porosity.volve.protocol import load_protocol, source_config def main() -> None: p = load_protocol() ref, sa = p["data"]["primary_reference"], p["registration"]["sigma_a_ms"] full = VolveData(p, arrays=load_arrays()) summary = outputs_dir() / "p4" / "summary" index = json.loads((summary / "prediction_index.json").read_text(encoding="utf-8")) with np.load(summary / "predictions.npz") as z: mu = {i: 100 * z[f"{i}/mu"] for i, e in enumerate(index) if str(e["seed"]) == "ensemble" and e["sigma_a_ms"] == sa} rows = [] for scheme in p["split"]["schemes"]: for test, train in outer_folds(p["data"]["wells"], scheme, source_config(p)["p4"]): y = 100 * full.y(test, ref) ok = np.isfinite(y) pooled = np.concatenate([100 * full.y(w, ref) for w in train]) const = float(np.nanmean(pooled)) row = dict(scheme=scheme, well=test, n=int(ok.sum()), label_mean=float(y[ok].mean()), label_sd=float(y[ok].std()), train_mean=const, const_rmse=float(np.sqrt(np.mean((const - y[ok]) ** 2))), const_bias=const - float(y[ok].mean())) for i, e in enumerate(index): if i in mu and e["scheme"] == scheme and e["test_well"] == test: err = mu[i][ok] - y[ok] m = e["method"] row |= {f"{m}/rmse": float(np.sqrt(np.mean(err ** 2))), f"{m}/bias": float(err.mean()), f"{m}/err_sd": float(err.std()), f"{m}/pred_sd": float(mu[i][ok].std())} rows.append(row) wells = pd.DataFrame(rows) out = outputs_dir() / "p4" / "posthoc" out.mkdir(parents=True, exist_ok=True) wells.to_csv(out / "wells.csv", index=False, float_format="%.4f") numeric = wells.drop(columns=["well"]) macro = numeric.groupby("scheme").mean() abs_bias = wells.filter(like="bias").abs().groupby(wells.scheme).mean().add_prefix("abs_") macro.join(abs_bias).to_csv(out / "macro.csv", float_format="%.4f") print(macro.join(abs_bias).T.to_string(float_format="%.3f")) if __name__ == "__main__": main()