Files
ML-Porosity/ml_porosity/benchmark/data.py
lamlvh 381b5ffb06 Add P6 benchmark code, locked protocol_bench, T2 study
- ml_porosity/benchmark/ runner, scripts 32-35, scripts/run_bench.sh;
  misspec= option in synth_d2 (default path reproduces P2r bit for bit).
- configs/protocol_bench.{yaml,sha256,lock.json} locked 29/10 14:39 UTC
  (sha256 aa763f0a...), configs/d2_synth_bench.yaml.
- T2 mechanism declaration and results; operations/p6_supervisor.py.
- Docs, report and paper edits of 29/10.

outputs/ stays out of git; the repository folder is synced as a whole to
the new server. Main S1 runs are still training at this commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 01:33:18 +00:00

91 lines
3.6 KiB
Python

"""Inputs of protocol_bench: generator configuration, horizon grid, warp prior, wavelet, and the registration /
training / metric blocks of protocol_p2 (the same objects scripts 11/14/15 build), plus the realization builder with
misspecification presets and the nominal-fingerprint helper of tests/test_d2_misspec.py."""
from __future__ import annotations
import hashlib
import importlib.util
import json
from dataclasses import dataclass
from functools import lru_cache
import numpy as np
from ml_porosity.benchmark.protocol import generator_config, protocol_p2
from ml_porosity.data.synth_d2 import D2Realization, load_horizon_grid, preset_misspec
from ml_porosity.paths import REPO_ROOT
from ml_porosity.registration.warp_prior import WarpHyper
WELL_FIELDS = ("seis_clean", "noise_unit", "phi_t", "z_g", "z_b", "boundaries_t", "eps_a")
OBS_KEYS = ("seis", "phi_log", "ai_log", "tau_k", "t_k", "b", "g", "t_of_tau_true", "y_true")
FP_DRIFT, FP_SNR_DB, FP_SIGMA_A, FP_DENSITY = (0.0, 1.0, 2.0), 10, 7.0, "full"
@lru_cache(maxsize=None)
def load_script(name: str):
"""scripts/<name> as a module (scripts 11's reg_settings / method_specs / evaluate are the P2r semantics)."""
spec = importlib.util.spec_from_file_location("bench_" + name[:2], REPO_ROOT / "scripts" / name)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
@dataclass
class Inputs:
p: dict
cfg: dict
hg: object
prior_hyper: dict
wav: np.ndarray
P2: dict
reg: dict
tcfg: dict
f_c: float
def realization(self, rid: int, preset: str | None) -> D2Realization:
return D2Realization(self.cfg, int(rid), self.hg, WarpHyper(**self.prior_hyper), self.wav,
misspec=preset_misspec(self.cfg, preset))
def regime(self, name: str) -> dict:
return dict(self.cfg["regimes"][name])
@property
def sigma_a(self) -> float:
return float(self.cfg["anchors"]["sigma_a_ms"])
def observe(self, R: D2Realization, c: float, regime: str, N: int) -> tuple[list[dict], list[dict]]:
"""Training observations (first N pool wells, regime anchors) and the 8 test wells (full anchors, same SNR),
exactly as scripts 14/15."""
st = self.regime(regime)
obs = [R.observe(w, c, st["snr_db"], self.sigma_a, st["anchor_density"]) for w in R.pool[:N]]
test = [R.observe(w, c, st["snr_db"], self.sigma_a) for w in R.test]
return obs, test
def load_inputs(p: dict) -> Inputs:
cfg = generator_config(p)
P2 = protocol_p2(p)
hg = load_horizon_grid(cfg["horizons"], REPO_ROOT / cfg["horizon_cache"])
wp = p["inputs"]["warp_prior"]
prior = json.loads((REPO_ROOT / wp["path"]).read_text())["fits"][wp["fit"]]
wav = np.load(REPO_ROOT / p["inputs"]["wavelet"]["path"])[p["inputs"]["wavelet"]["key"]]
run_d2 = load_script("11_run_d2.py")
return Inputs(p, cfg, hg, prior["hyper"], wav, P2, run_d2.reg_settings(P2),
P2["training"]["model"] | P2["training"]["optimizer"], float(P2["metrics"]["bands"]["f_c_hz"]))
def _sha(chunks) -> str:
h = hashlib.sha256()
for a in chunks:
h.update(np.ascontiguousarray(np.asarray(a)).tobytes())
return h.hexdigest()
def fingerprint_realization(R: D2Realization) -> dict:
"""{"wells": {field: sha}, "obs": {key: sha}} as tests/test_d2_misspec.py records them."""
wells = R.test + R.pool[:4]
obs = [R.observe(w, c, FP_SNR_DB, FP_SIGMA_A, FP_DENSITY) for c in FP_DRIFT for w in wells]
return {"wells": {k: _sha(getattr(w, k) for w in R.wells) for k in WELL_FIELDS},
"obs": {k: _sha(o[k] for o in obs) for k in OBS_KEYS}}