update at 2026-10-12 14:26:51

This commit is contained in:
douboer
2026-10-12 14:26:51 +08:00
parent ae1c774606
commit 7a225ff2b1
51 changed files with 13426 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
"""导出层:实验记录包落盘(本地专用,Pyodide 不加载本模块)。"""
from . import record
__all__ = ["record"]

View File

@@ -0,0 +1,98 @@
"""实验记录包落盘(本地层;浏览器端不使用)。
记录包结构(docs/实现方案.md §10):
runs/<时间戳>-<实验名>-seed<种子>/
├── meta.json 运行时间、git hash、算法版本、全部参数、种子、数据来源标注
├── data/ 采样点 CSV
├── results.json 拟合与统计数值
├── figures/ SVG + PNG 图表
└── README.md 本次运行回答什么问题
"""
import csv
import datetime
import json
import os
import subprocess
from .. import ALGO_VERSION
def git_hash():
try:
h = subprocess.run(["git", "rev-parse", "--short", "HEAD"],
capture_output=True, text=True, timeout=5)
if h.returncode == 0 and h.stdout.strip():
return h.stdout.strip()
except Exception:
pass
return "unversioned"
def save_run(runs_dir, name, records, figures=None, notes="", seed=None, extra_meta=None):
"""落盘一个记录包。
records: dict 或 list[dict],每个含 meta/data 字段(pipeline 输出);
figures: list[(filename, matplotlib.figure)]。
返回记录包目录。
"""
ts = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
if seed is not None:
run_dir = os.path.join(runs_dir, f"{ts}-{name}-seed{seed}")
else:
run_dir = os.path.join(runs_dir, f"{ts}-{name}")
os.makedirs(os.path.join(run_dir, "data"), exist_ok=True)
os.makedirs(os.path.join(run_dir, "figures"), exist_ok=True)
if isinstance(records, dict):
records = [records]
meta = {
"created": datetime.datetime.now().isoformat(timespec="seconds"),
"git_hash": git_hash(),
"algo_version": ALGO_VERSION,
"data_origin": "software-simulated",
"experiment": name,
"notes": notes,
}
if seed is not None:
meta["seed"] = int(seed)
if extra_meta:
meta.update(extra_meta)
results = []
for i, rec in enumerate(records):
m = rec.get("meta", {})
meta.setdefault("params", m.get("params"))
r = {k: v for k, v in rec.items()
if k in ("fit", "tau_hat", "tau_true", "rel_error", "n_samples",
"sigma", "mean", "std", "rel_std", "bias", "tau_hats",
"uniformity", "sorted_phases_ns", "channels", "construction",
"target_phases_ns", "is_uniform_grid", "recommended")}
results.append(r)
# 采样点 CSV
if "t_samples" in rec:
path = os.path.join(run_dir, "data", f"samples_{i:02d}.csv")
with open(path, "w", newline="") as f:
w = csv.writer(f)
w.writerow(["t_ns", "y_clean", "y_measured"])
for t, yc, ym in zip(rec["t_samples"], rec["y_clean"], rec["y_samples"]):
w.writerow([f"{t:.6f}", f"{yc:.9f}", f"{ym:.9f}"])
with open(os.path.join(run_dir, "meta.json"), "w") as f:
json.dump(meta, f, ensure_ascii=False, indent=2)
with open(os.path.join(run_dir, "results.json"), "w") as f:
json.dump(results, f, ensure_ascii=False, indent=2, default=float)
for fname, fig in (figures or []):
fig.savefig(os.path.join(run_dir, "figures", f"{fname}.svg"))
fig.savefig(os.path.join(run_dir, "figures", f"{fname}.png"), dpi=150)
with open(os.path.join(run_dir, "README.md"), "w") as f:
f.write(f"# 记录包:{name}\n\n{notes}\n\n"
f"- 运行时间:{meta['created']}\n"
f"- git:{meta['git_hash']},算法版本:{ALGO_VERSION}\n"
f"- 数据来源:**软件仿真(software-simulated)**,非装置实测\n"
f"- 复现:`pytest fiber_delay_sim/tests/` 或重跑对应 experiments 脚本\n")
return run_dir