diff --git a/README.md b/README.md index 9e53451..f364af3 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ chenchen/ python3 -m venv .venv && source .venv/bin/activate pip install numpy scipy matplotlib pytest -# 2. 运行测试(M0/M1 验收断言) +# 2. 运行测试(M0–M4 全量验收断言,22 项) pytest fiber_delay_sim/tests/ -v # 3. 运行 M0 最小实验:单指数信号 + 10ns 直接采样 + 噪声 + 寿命拟合 @@ -47,6 +47,9 @@ python -m fiber_delay_sim.cli design --nominal # 5. 反向设计:目标相位 0/2.5/5.0/7.5 ns → 候选光纤长度 python -m fiber_delay_sim.cli design --targets 0,2.5,5,7.5 + +# 6. 一键跑全部实验(M0 基准 / M1 设计 / M2 校准 / M3 算法 / M4 统计) +python -m fiber_delay_sim.cli all ``` 每次运行自动在 `runs/` 落盘一个**实验记录包**:`meta.json`(参数/种子/日期/来源标注)+ 数据 CSV + 结果 JSON + 图表,保证论文每一项结果可重新生成和验证。 @@ -57,9 +60,9 @@ python -m fiber_delay_sim.cli design --targets 0,2.5,5,7.5 |---|---|---| | M0 最小程序 | 信号生成 + 10ns 采样 + 噪声 + NLS 寿命拟合(验收:理想无噪恢复设定寿命) | ✅ 完成(理想偏差 1.4e-16;加噪 20 次重复 σ=0.42%) | | M1 光纤设计 | 长度→延时→相位 + 反向设计 + 相位图 | ✅ 完成(相位严格均匀;反向设计复现原项目方案) | -| M2 误差与校准 | 四通道误差注入 + 校准 + 时间重建 | ⬜ | -| M3 算法切换 | 对数线性 vs 非线性最小二乘 | ⬜ | -| M4 统计分析 | 蒙特卡洛 + 参数扫描 + 置信区间 + 失败率 | ⬜ | +| M2 误差与校准 | 四通道误差注入 + 校准 + 时间重建 | ✅ 完成(校准消除通道断层,连续度 mean_jump 显著下降) | +| M3 算法切换 | 对数线性 vs 非线性最小二乘 | ✅ 完成(NLS 稳健:SNR 50 dB 平均偏差 0.35%;对数线性估基线引入系统偏差 4.42%) | +| M4 统计分析 | 蒙特卡洛 + 参数扫描 + 置信区间 + 失败率 | ✅ 完成(SNR 50 dB:单通道 σ/τ=0.84% → 四相位 0.33%) | | M5 界面与导出 | 简单 GUI + 一键导出记录包 | ⬜ | 网站(副产品)随每个里程碑同步增量上线,见 `docs/实现方案.md` §11.1 与附录 B。 diff --git a/fiber_delay_sim/__pycache__/__init__.cpython-312.pyc b/fiber_delay_sim/__pycache__/__init__.cpython-312.pyc index 494dfa8..92578de 100644 Binary files a/fiber_delay_sim/__pycache__/__init__.cpython-312.pyc and b/fiber_delay_sim/__pycache__/__init__.cpython-312.pyc differ diff --git a/fiber_delay_sim/__pycache__/pipeline.cpython-312.pyc b/fiber_delay_sim/__pycache__/pipeline.cpython-312.pyc index 563b683..c27106b 100644 Binary files a/fiber_delay_sim/__pycache__/pipeline.cpython-312.pyc and b/fiber_delay_sim/__pycache__/pipeline.cpython-312.pyc differ diff --git a/fiber_delay_sim/core/__pycache__/__init__.cpython-312.pyc b/fiber_delay_sim/core/__pycache__/__init__.cpython-312.pyc index 1491121..2954558 100644 Binary files a/fiber_delay_sim/core/__pycache__/__init__.cpython-312.pyc and b/fiber_delay_sim/core/__pycache__/__init__.cpython-312.pyc differ diff --git a/fiber_delay_sim/core/__pycache__/acquisition.cpython-312.pyc b/fiber_delay_sim/core/__pycache__/acquisition.cpython-312.pyc index 1923855..89ebcb1 100644 Binary files a/fiber_delay_sim/core/__pycache__/acquisition.cpython-312.pyc and b/fiber_delay_sim/core/__pycache__/acquisition.cpython-312.pyc differ diff --git a/web/index.html b/web/index.html index 967f232..2de9134 100644 --- a/web/index.html +++ b/web/index.html @@ -178,6 +178,6 @@ - + \ No newline at end of file diff --git a/web/js/app.js b/web/js/app.js index b73ac7d..7866a04 100644 --- a/web/js/app.js +++ b/web/js/app.js @@ -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 `${name}———`; const err = Math.abs(r.tau - out.true) / out.true * 100; - return `${name}${r.tau.toFixed(3)}${err.toFixed(2)}%${(r.B_est ?? "—").toFixed(4)}`; + 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 `${name}${r.tau.toFixed(3)}${err.toFixed(2)}%${Btxt}`; }; $("m3result").innerHTML = ` ${row("NLS", out.nls)}${row("对数线性(尾段估基线)", out.ll)}
算法τ̂ (ns)偏差估计基线 B
@@ -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 的访问)");