"""Nested blind folds for P4 (plan §5.2). Outer fold: test well u, training wells T_out = blind_training_wells(u, scheme) (`lowo`: every other well; `slot`: every well outside the slot group of u). Inner folds: leave one well of T_out out, under both schemes — under `slot` an inner validation well may share a slot with inner training wells, so inner validation can be optimistic there (recorded deviation in protocol_volve). Every model sees seismic only through blind keys: the input of a non-training well u under key K is placed in time by the TD model of the wells K, with T_in ⊆ K ⊆ W∖{u} (never u's own picks or DT). Validation of v in T_out uses K = T_in; the outer test of u uses K = T_out. Training wells use their calibrated reference path. """ from __future__ import annotations import itertools from ml_porosity.data.volve_wells import blind_key, blind_training_wells def outer_folds(wells: list[str], scheme: str, p4: dict) -> list[tuple[str, tuple[str, ...]]]: """[(test well u, T_out)] in `wells` order; T_out sorted.""" out = [] for u in wells: train = tuple(sorted(blind_training_wells(u, scheme, p4))) if u in train or not train: raise RuntimeError(f"invalid outer fold for {u}: {train}") out.append((u, train)) return out def inner_folds(outer_train: tuple[str, ...]) -> list[tuple[str, tuple[str, ...]]]: """[(validation well v, T_in = T_out ∖ {v})], leave-one-well-out within the outer training set.""" train = tuple(sorted(outer_train)) if len(train) < 2: raise ValueError("inner LOWO needs at least two outer training wells") return [(v, tuple(w for w in train if w != v)) for v in train] def nested_folds(wells: list[str], schemes: list[str], p4: dict) -> list[dict]: """Every (scheme, test well, validation well, T_out, T_in), with the blind key of each role.""" rows = [] for scheme in schemes: for u, t_out in outer_folds(wells, scheme, p4): for v, t_in in inner_folds(t_out): if u in t_in or v in t_in: raise RuntimeError("held-out well entered an inner training set") rows.append(dict(scheme=scheme, test=u, validation=v, outer_train=t_out, inner_train=t_in, validation_key=blind_key(t_in), test_key=blind_key(t_out))) return rows def training_sets(wells: list[str], schemes: list[str], p4: dict) -> list[tuple[str, ...]]: """Distinct inner training sets over all schemes (one trajectory per set, method, seed and σ_a arm).""" return sorted({f["inner_train"] for f in nested_folds(wells, schemes, p4)}, key=lambda s: (-len(s), s)) def prediction_targets(wells: list[str], train: tuple[str, ...]) -> list[tuple[str, str]]: """(well, key) inputs predicted by a model trained on `train`: training wells on their reference path (key 'reference'), every other well u on every blind key K with train ⊆ K ⊆ W∖{u}. Covers the validation (K = T_in) and outer-test (K = T_out ⊋ T_in) inputs of every fold that trains on `train`.""" train = tuple(sorted(train)) out = [(w, "reference") for w in train] for u in wells: if u in train: continue rest = [w for w in wells if w != u and w not in train] for k in range(len(rest) + 1): for extra in itertools.combinations(rest, k): out.append((u, blind_key(train + extra))) return out def target_name(well: str, key: str) -> str: return f"{well}|{key}"