update at 2026-10-12 16:40:38

This commit is contained in:
douboer
2026-10-12 16:40:38 +08:00
parent a484c7914f
commit 4a734e6299
7 changed files with 30 additions and 12 deletions

View File

@@ -96,6 +96,7 @@ def m4_web(repeats, snr, seed):
let pyodide = null;
let seed = 42;
let pyodideReady = null; // 初始化完成的 Promise(供 callPy 统一等待)
function setLoading(text) { $("loadingText").textContent = text; }
@@ -137,13 +138,25 @@ async function initPyodide() {
$("loading").classList.add("hidden");
}
/* 等待 Pyodide 完全就绪(含 numpy/scipy 与 wrapper 注册);重复调用共享同一个 Promise */
function waitPyodide() {
if (!pyodideReady) {
pyodideReady = initPyodide().catch((e) => {
pyodideReady = null; // 失败后允许下次重试
throw e;
});
}
return pyodideReady;
}
async function callPy(name, args) {
await waitPyodide();
let fn = pyodide.globals.get(name);
if (fn === undefined) {
// 防御:首次注册可能受 Pyodide 文件导入时序影响,重新执行一次 wrapper
console.warn("注册函数缺失,重新执行 wrapper:", name);
pyodide.runPython("import importlib; importlib.invalidate_caches()");
pyodide.runPython(eval("PY_WRAPPER"));
pyodide.runPython(PY_WRAPPER);
fn = pyodide.globals.get(name);
if (fn === undefined) throw new Error(`Python 函数 ${name} 注册失败`);
}
@@ -223,9 +236,10 @@ async function m1Forward() {
const out = await callPy("m1_forward_web", [lens, ng]);
const ch = out.channels;
getChart("chartM1a").setOption({
grid: { left: 46, right: 96, top: 40, bottom: 42 },
grid: { left: 46, right: 96, top: 40, bottom: 54 },
tooltip: {},
xAxis: { type: "value", name: "延时 (ns)", min: 0, max: 165, interval: 20,
xAxis: { type: "value", name: "延时 (ns)", nameLocation: "middle", nameGap: 34,
min: 0, max: 165, interval: 20,
axisLabel: { fontSize: 11 } },
yAxis: { type: "category", data: ch.map((c) => `CH${c.index + 1}`),
splitLine: { show: false } },
@@ -307,7 +321,7 @@ async function runM3() {
const out = await callPy("m3_web", [snr, seed]);
const t = out.t;
const yNls = t.map((x) => out.nls.A * Math.exp(-x / out.nls.tau) + out.nls.B);
const yLl = (out.ll.tau != null) ? t.map((x) => out.ll.A * Math.exp(-x / out.ll.tau)) : [];
const yLl = (out.ll.tau != null) ? t.map((x) => out.ll.A * Math.exp(-x / out.ll.tau) + (out.ll.B_est || 0)) : [];
const series = [
{ name: "真值", type: "line", showSymbol: false, data: t.map((x, i) => [x, out.y[i] * 0 + Math.exp(-x / out.true)]),
lineStyle: { color: "#111", width: 1.2 }, silent: true },
@@ -330,7 +344,9 @@ async function runM3() {
const row = (name, r) => {
if (r.tau == null) return `<tr><td>${name}</td><td>—</td><td>—</td><td>—</td></tr>`;
const err = Math.abs(r.tau - out.true) / out.true * 100;
return `<tr><td>${name}</td><td>${r.tau.toFixed(3)}</td><td>${err.toFixed(2)}%</td><td>${(r.B_est ?? "—").toFixed(4)}</td></tr>`;
const Bv = typeof r.B_est === "number" ? r.B_est : (typeof r.B === "number" ? r.B : null);
const Btxt = Bv == null ? "—" : Bv.toFixed(4);
return `<tr><td>${name}</td><td>${r.tau.toFixed(3)}</td><td>${err.toFixed(2)}%</td><td>${Btxt}</td></tr>`;
};
$("m3result").innerHTML = `<table class="tbl"><tr><th>算法</th><th>τ̂ (ns)</th><th>偏差</th><th>估计基线 B</th></tr>
${row("NLS", out.nls)}${row("对数线性(尾段估基线)", out.ll)}</table>
@@ -437,11 +453,10 @@ function bindControls() {
bindControls();
m1Inputs();
try {
await initPyodide();
await waitPyodide();
// 仅渲染当前可见的 M0 tab;其余 tab 首次切换时惰性渲染(避免隐藏容器尺寸为 0)
tabRan["tab-m0"] = true;
await runM0();
await runM4();
} catch (e) {
console.error(e);
setLoading("加载失败:" + e.message + "(请刷新重试,或检查网络对 cdn.jsdelivr.net 的访问)");