File size: 5,828 Bytes
fe976b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""Per-entry error model (ERROR_MODEL.md): physics features of every mutual entry -> predicted median |log error|,
P(|err| > 5 %) and split-conformal 80 % / 95 % intervals.

The same `features` function builds the training table (HPC, fold networks; scripts/error_model.py) and the inference
features (here), from one "ingredients" record per design:

  names          ["G", ...] (index 0 = ground)
  logC_seeds     (S, k, k) per-seed network output (diag log C_ii, off-diag log |C_ij|), training stack
  prior_C        (k, k) Maxwell matrix of the MoM prior the network saw
  prior_coarse_C (k, k) gap/3 solve when the prior is v3-rich (else None)
  edges          {(i, j): (log_dmin, log_cps, share_mean)} from the scene graph
  h_min          smallest MoM cell (µm)
"""

from __future__ import annotations

import json
from pathlib import Path

import numpy as np

FEATURES = ["is_cg", "log_c", "log_rel_max", "log_rel_min", "d_prior", "prior_conv", "cells_per_gap", "n_cond",
            "log_min_gap", "log_dmin", "log_cps", "share_mean", "seed_sd"]
MONOTONE = {"log_c": -1, "log_rel_max": -1, "prior_conv": 1, "seed_sd": 1, "cells_per_gap": -1}
THRESHOLD = 0.05  # |C_pred / C_true - 1| > 5 % = "large error"


def ingredients_from_graph(names, logC_seeds, prior_C, graph, h_min, prior_coarse_C=None) -> dict:
    edges = {tuple(e): (float(f["log_dmin"]), float(f["log_cps"]), float(f["share_mean"])) for e, f in graph["edges"].items()}
    return {"names": list(names), "logC_seeds": np.asarray(logC_seeds, np.float64), "prior_C": np.asarray(prior_C, np.float64),
            "prior_coarse_C": None if prior_coarse_C is None else np.asarray(prior_coarse_C, np.float64), "edges": edges,
            "h_min": float(h_min)}


def _neglog(C, i, j):
    return np.log(max(-C[i, j], 1e-9))


def features(ing: dict, use_prior_conv: bool) -> tuple[list[tuple[int, int]], np.ndarray]:
    """(pairs, X) with X[:, FEATURES]: one row per mutual entry i < j (prior_conv NaN when unused)."""
    S = ing["logC_seeds"]
    k = len(ing["names"])
    L = S.mean(0)
    sd = S.std(0, ddof=1) if S.shape[0] > 1 else np.zeros_like(L)
    P, Pc = ing["prior_C"], ing.get("prior_coarse_C")
    gaps = [v[0] for v in ing["edges"].values()]
    log_min_gap = float(min(gaps)) if gaps else np.nan
    cpg = float(np.exp(log_min_gap) / max(ing["h_min"], 1e-9)) if gaps else np.nan
    pairs, rows = [], []
    for i in range(k):
        for j in range(i + 1, k):
            lc = L[i, j]
            ends = [x for x in (i, j) if x != 0]
            rel = [lc - L[x, x] for x in ends]
            conv = abs(_neglog(P, i, j) - _neglog(Pc, i, j)) if (use_prior_conv and Pc is not None) else np.nan
            ld, lcps, sh = ing["edges"].get((i, j), ing["edges"].get((j, i), (np.nan, np.nan, np.nan)))
            rows.append([float(i == 0), lc, max(rel), min(rel), abs(lc - _neglog(P, i, j)), conv, cpg, float(k - 1),
                         log_min_gap, ld, lcps, sh, sd[i, j]])
            pairs.append((i, j))
    return pairs, np.asarray(rows, np.float64)


def targets(ing: dict, Y: np.ndarray, pairs) -> tuple[np.ndarray, np.ndarray]:
    """a = |log(C_pred / C_true)| and rel = |C_pred / C_true - 1| per entry (Y: Maxwell labels, fF)."""
    L = ing["logC_seeds"].mean(0)
    d = np.array([L[i, j] - np.log(-Y[i, j]) for i, j in pairs])
    return np.abs(d), np.abs(np.expm1(d))


# ---------------------------------------------------------------- inference

_CACHE: dict = {}


def find_artifact(model: str, reg=None) -> Path | None:
    """deploy/artifacts/error_model_<tag>.joblib for the registry model (registry artifacts.error_model maps names)."""
    tag = None
    if reg is not None:
        tag = (reg["artifacts"].get("error_model") or {}).get(model)
    if not tag:
        return None
    here = Path(__file__).resolve()
    for p in [here.parent / tag] + [q / "artifacts" / tag for q in here.parents] + [q / "deploy" / "artifacts" / tag for q in here.parents]:
        if p.exists():
            return p
    try:
        from .registry import _hub_download

        return Path(_hub_download(reg.model(model)["repo_id"], tag))
    except Exception:  # noqa: BLE001
        return None


def load(path) -> dict:
    path = str(path)
    if path not in _CACHE:
        import joblib

        _CACHE[path] = joblib.load(path)
    return _CACHE[path]


def predict_entries(art: dict, ing: dict, labels: list[str], scale: float = 1.0, stack_rel: float = 0.0) -> list[dict]:
    """Per-entry estimates for the result dict. scale: the eps correction factor applied to the matrix; stack_rel: median
    relative stack residual (material + thickness, deploy/stack_error.py), added in quadrature (Gaussian assumption)."""
    pairs, X = features(ing, art["use_prior_conv"])
    Xu = X[:, [FEATURES.index(f) for f in art["features"]]]
    sig = np.maximum(art["regressor"].predict(Xu), 1e-4)
    p = art["classifier"].predict_proba(Xu)[:, 1]
    L = ing["logC_seeds"].mean(0)
    s_stack = stack_rel / 0.674  # Gaussian sigma of the stack residual in log space (small-error approximation)
    out = []
    for (i, j), x, s, pl in zip(pairs, X, sig, p):
        kind = "conductor-ground" if x[0] > 0.5 else "conductor-conductor"
        q = art["conformal"][kind]
        c = float(np.exp(L[i, j]) * scale)
        rec = {"i": labels[i], "j": labels[j], "kind": kind, "C_fF": c,
               "predicted_median_err_pct": float(100 * np.sqrt(np.expm1(s) ** 2 + (0.674 * s_stack) ** 2)),
               "p_large_error": float(pl)}
        for lev, z in (("80", 1.2816), ("95", 1.96)):
            h = float(np.hypot(q[lev] * s, z * s_stack))
            rec[f"interval_{lev}_fF"] = [c * float(np.exp(-h)), c * float(np.exp(h))]
            rec[f"halfwidth_{lev}_pct"] = float(100 * np.expm1(h))
        out.append(rec)
    return out