Download tlsq_infer/stack_error.py from shanto268/PlanarCapacitanceModel: direct link, hf CLI and curl.
- Browser
- Download file 5.61 kB
-
https://huggingface.co/spaces/shanto268/PlanarCapacitanceModel/resolve/main/tlsq_infer/stack_error.py
- Command line
-
hf download hf://spaces/shanto268/PlanarCapacitanceModel/tlsq_infer/stack_error.py
-
curl -L -o stack_error.py https://huggingface.co/spaces/shanto268/PlanarCapacitanceModel/resolve/main/tlsq_infer/stack_error.py
5.61 kB
| """Expected extra error when the layer stack differs from the training stack (Si, eps 11.45, 350 µm), evaluated from | |
| the parameters fitted by deploy/stack_error.py on the 15-design stack DoE (deploy/artifacts/stack_error.json). | |
| Study tool only since 2026-11-03: no serving path rescales or calls it (tlsq_infer.substrate: prior solved at the stack). | |
| Two independent residuals, combined in quadrature with the model's own validation error: | |
| material after the eps correction (1 + eps_sub) / (1 + 11.45): r_mat = a_mat * |ln((1 + eps) / (1 + 11.45))| + floor | |
| thickness finite substrate seen by the fringe field: r_t = A * (t / L)^(-p) for t < 350 µm (L = lateral extent of the | |
| conductors), measured relative to the 350 µm reference; thicker substrates: the measured small shift. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| EPS_TRAIN = 11.45 | |
| T_TRAIN = 350.0 | |
| def eps_factor(eps_sub: float) -> float: | |
| """Quasi-static half-space correction of every capacitance for another substrate permittivity.""" | |
| return (1.0 + float(eps_sub)) / (1.0 + EPS_TRAIN) | |
| def load_params(path=None) -> dict | None: | |
| for p in [path, Path(__file__).resolve().parent / "stack_error.json"] + [q / "artifacts" / "stack_error.json" for q in Path(__file__).resolve().parents]: | |
| if p and Path(p).exists(): | |
| return json.loads(Path(p).read_text()) | |
| return None | |
| def lateral_extent(conductors: list, P: dict | None) -> float: | |
| """L of the thickness fit (definition stored in the parameters; default sqrt of the total conductor area).""" | |
| import shapely | |
| S = shapely.union_all(conductors) | |
| how = (P or {}).get("thickness", {}).get("L_definition", "sqrt_area") | |
| x0, y0, x1, y1 = S.bounds | |
| return float({"sqrt_area": np.sqrt(S.area), "bbox_diag": np.hypot(x1 - x0, y1 - y0), | |
| "bbox_max": max(x1 - x0, y1 - y0)}.get(how, np.sqrt(S.area))) | |
| def material_residual_pct(P: dict, eps_sub: float) -> float: | |
| x = abs(np.log(eps_factor(eps_sub))) | |
| m = P["material"] | |
| return 0.0 if x < 1e-9 else float(m["slope_pct_per_log"] * x + m["floor_pct"]) | |
| def thickness_residual_pct(P: dict, t_um: float, L_um: float) -> float: | |
| th = P["thickness"] | |
| if t_um >= T_TRAIN: | |
| return float(th["thick_side_pct"]) if t_um > T_TRAIN * 1.01 else 0.0 | |
| x = t_um / max(L_um, 1e-9) | |
| x_ref = T_TRAIN / max(L_um, 1e-9) | |
| r = th["A_pct"] * x ** (-th["p"]) - th["A_pct"] * x_ref ** (-th["p"]) | |
| return float(max(r, 0.0)) | |
| def breakdown_thickness_um(P: dict, L_um: float, base_pct: float) -> float: | |
| """Thinnest substrate at which the thickness residual stays below sqrt(3) x base error (total error <= 2x base).""" | |
| th = P["thickness"] | |
| target = np.sqrt(3.0) * base_pct | |
| xr = (T_TRAIN / L_um) ** (-th["p"]) | |
| # A x^-p - A xr = target -> x = ((target / A) + xr)^(-1/p) | |
| x = ((target / th["A_pct"]) + xr) ** (-1.0 / th["p"]) | |
| return float(x * L_um) | |
| def expected_error(P: dict | None, eps_sub: float, t_um: float, L_um: float, base: dict, mean_abs_ff: float, rms_ff: float) -> dict: | |
| """base = the model's validation metrics {median_pct, mae_ff, rmse_ff} (never-seen set); mean_abs_ff / rms_ff = the | |
| mean and rms of |mutual C| of THIS prediction (to scale the paper MAE/RMSE to this layout).""" | |
| out = {"validation": base, "assumptions": []} | |
| if P is None: | |
| out["warning"] = "stack-error parameters not available" | |
| return out | |
| rm = material_residual_pct(P, eps_sub) | |
| rt = thickness_residual_pct(P, t_um, L_um) | |
| b = float(base["median_pct"]) | |
| tot = float(np.sqrt(b**2 + rm**2 + rt**2)) | |
| sc = P.get("base_scale", {}) | |
| k_mae = mean_abs_ff / sc["never_seen_mean_abs_ff"] if sc.get("never_seen_mean_abs_ff") else np.nan | |
| k_rms = rms_ff / sc["never_seen_rms_ff"] if sc.get("never_seen_rms_ff") else np.nan | |
| # stack residuals as relative errors on this layout's entries: MAE ~ r * mean|C|, RMSE ~ r * rms|C| | |
| rs = np.sqrt(rm**2 + rt**2) / 100 | |
| mae = float(np.sqrt((base["mae_ff"] * k_mae) ** 2 + (rs * mean_abs_ff) ** 2)) | |
| rmse = float(np.sqrt((base["rmse_ff"] * k_rms) ** 2 + (rs * rms_ff) ** 2)) | |
| t_break = breakdown_thickness_um(P, L_um, b) | |
| out.update({"material_residual_pct": rm, "thickness_residual_pct": rt, "expected_median_pct": tot, | |
| "expected_mae_ff": mae, "expected_rmse_ff": rmse, "breakdown_thickness_um": t_break, | |
| "breakdown": bool(t_um < t_break), "lateral_extent_um": float(L_um), "t_over_L": float(t_um / L_um), | |
| "doe_designs": P.get("n_designs"), | |
| "assumptions": ["errors add in quadrature (model, material, thickness)", | |
| "material residual fitted on 11 substrates x 15 designs after the eps correction", | |
| "thickness residual fitted vs t/L on 8 Si thicknesses x 15 designs (3 design groups)", | |
| "MAE/RMSE scaled from the never-seen validation set by this layout's mean / rms |C|"]}) | |
| if out["breakdown"]: | |
| out["warning"] = (f"substrate {t_um:g} µm is below the breakdown thickness {t_break:.0f} µm for a layout of lateral " | |
| f"extent {L_um:.0f} µm: the zero-shot prediction (training stack 350 µm) is not reliable here") | |
| elif rt > b: | |
| out["warning"] = f"finite-substrate error ({rt:.1f} %) exceeds the model error ({b:.1f} %)" | |
| if not (2.0 <= eps_sub <= 30.0): | |
| out.setdefault("warning", "") | |
| out["warning"] += f" eps={eps_sub:g} is outside the validated range 3.8-24." | |
| return out | |