Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
237 lines (194 loc) · 8.42 KB
/
Copy pathcli.py
File metadata and controls
237 lines (194 loc) · 8.42 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""SeqForge 命令行入口。
子命令
------
``demo`` 跑完整基准并落盘 ``benchmark.json``(默认)
``list`` 列出全部方法 / 数据集 / 能力
``selftest`` 只跑确定性不变量(CI 快速门禁,不跑随机基准)
``version`` 打印版本
Windows 注意:所有打印显式 UTF-8,避免 ✅/⚠️ 触发 UnicodeEncodeError
(踩坑库 §D:非 GBK 输出流会崩)。
"""
from __future__ import annotations
import argparse
import contextlib
import json
import sys
import time
from pathlib import Path
def _force_utf8_stdout() -> None:
"""把 stdout/stderr 切到 UTF-8,规避 Windows 控制台的 GBK 限制。
两个坑:
1. 必须用 ``getattr(..., None)`` 而非 ``hasattr``:pytest 的 capsys
替换对象在部分版本下**有** ``reconfigure`` 但调用会抛异常,
直接 try/except 更稳。
2. ``reconfigure`` 只在 TextIOWrapper 上存在;若 stdout 已被
重定向成二进制流则直接跳过。
"""
for stream in (sys.stdout, sys.stderr):
reconfigure = getattr(stream, "reconfigure", None)
if reconfigure is None:
continue
# pytest 捕获流 / 已关闭流:忽略,不影响主流程
with contextlib.suppress(ValueError, OSError, AttributeError):
reconfigure(encoding="utf-8", errors="replace")
_force_utf8_stdout()
sys.path.insert(0, str(Path(__file__).resolve().parent))
from core.config import SeqForgeConfig, load_config # noqa: E402
from data.dgp import DEFAULT_SPECS # noqa: E402
from eval.metrics import format_table # noqa: E402
from filters import backends # noqa: E402
from pipeline.runner import SeqForgePipeline # noqa: E402
def _print_banner(title: str) -> None:
print(f"\n{'=' * 72}\n {title}\n{'=' * 72}")
def cmd_demo(args: argparse.Namespace) -> int:
"""跑基准并落盘。"""
cfg = load_config(
n_steps=args.steps,
n_particles=args.particles,
output_dir=args.out,
)
seeds = tuple(int(s) for s in args.seeds.split(","))
datasets = args.datasets.split(",") if args.datasets else None
_print_banner("SeqForge ·序贯贝叶斯推断与状态空间估计")
print(f"配置: steps={cfg.n_steps} particles={cfg.n_particles} seeds={seeds}")
print(f"数据集: {datasets or list(DEFAULT_SPECS)}")
print("\n后端探测:")
for name, probe in backends.BACKEND_PROBE.items():
ok, info = probe()
mark = "OK " if ok else "SKIP"
print(f" [{mark}] {name:<14} {info}")
t0 = time.perf_counter()
report = SeqForgePipeline(cfg).run(datasets=datasets, seeds=seeds)
elapsed = time.perf_counter() - t0
_print_banner(f"基准结果({elapsed:.1f}s)")
rows = report["results"]
for m in rows:
if m["skipped"]:
print(f" {m['method']:<10} skipped {m['skip_reason']}")
class _R:
def __init__(self, d):
self.__dict__.update(d)
self.per_dataset = d["per_dataset"]
print()
print(format_table([_R(m) for m in rows if not m["skipped"]]))
truth = report.get("kf_reference") or {}
if truth:
_print_banner("线性高斯域:KF 精确 log-lik(粒子法偏差的唯一金标准)")
for seed, val in truth.items():
print(f" seed {seed:>4}:KF log-lik = {val:.6f}")
print("\n 粒子法偏差(负值 = bootstrap 的已知向下有偏):")
for m in rows:
if m["family"] != "particle" or m["skipped"]:
continue
pd_ = m["per_dataset"].get("linear_gaussian", {})
if "log_lik_mean" in pd_:
deltas = [pd_["log_lik_mean"] - truth[s] for s in truth if s in pd_]
if deltas:
print(f" {m['method']:<8} bias = {sum(deltas) / len(deltas):+8.4f}")
cases = report.get("failure_cases") or []
if cases:
_print_banner("失败案例(全部从实测结果派生)")
for i, c in enumerate(cases, 1):
print(f" {i}. [{c['kind']}] {c['symptom']}")
print(f" 根因: {c['root_cause']}")
print(f" 证据: {c['evidence']}")
out_dir = Path(cfg.output_dir)
out_dir.mkdir(parents=True, exist_ok=True)
path = out_dir / "benchmark.json"
path.write_text(json.dumps(report, indent=2, ensure_ascii=False), encoding="utf-8")
print(f"\n报告已写入: {path}")
return 0
def cmd_list(args: argparse.Namespace) -> int:
"""列出方法与数据集。"""
_print_banner("数据集")
for name, spec in DEFAULT_SPECS.items():
print(f" {name:<18} {type(spec).__name__:<20} {spec}")
_print_banner("后端能力")
for name, probe in backends.BACKEND_PROBE.items():
ok, info = probe()
print(f" {name:<14} {'可用' if ok else '不可用'} {info}")
_print_banner("方法矩阵(按数据集)")
from pipeline.runner import build_methods
for ds in DEFAULT_SPECS:
cfg = SeqForgeConfig()
mdl = None
try:
mdl = __import__("data.dgp", fromlist=["build_model"]).build_model(
ds, DEFAULT_SPECS[ds], 7
)
methods = build_methods(mdl, ds, cfg)
names = ", ".join(sorted(methods))
except Exception as exc:
names = f"<构造失败: {exc}>"
print(f" {ds:<18} {names}")
return 0
def cmd_selftest(args: argparse.Namespace) -> int:
"""只跑不变量(CI 快速门禁)。
踩坑记录:`subprocess.run(text=True)` 不指定 ``encoding`` 时会用
**locale默认编码**解码子进程输出。Windows CI runner 的 locale 是 cp1252,
而 pytest 输出含中文 docstring 名与 ✅ 符号 ⇒抛
``UnicodeDecodeError``,表现为「本地全绿、CI 莫名红」。
必须显式 ``encoding="utf-8"`` + ``errors="replace"``。
"""
import subprocess
root = Path(__file__).resolve().parent
r = subprocess.run(
[
sys.executable,
"-m",
"pytest",
str(root / "tests" / "test_invariants.py"),
"-q",
"-W",
"ignore::UserWarning",
"-p",
"no:cacheprovider",
],
cwd=str(root),
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
check=False,
)
sys.stdout.write(r.stdout[-3000:])
return r.returncode
def cmd_version(args: argparse.Namespace) -> int:
"""打印版本。"""
import core
print(f"SeqForge {core.__version__} by {core.__author__} ({core.__license__})")
return 0
def build_parser() -> argparse.ArgumentParser:
"""构造 argparse 解析器。"""
p = argparse.ArgumentParser(
prog="seqforge",
description="SeqForge · 序贯贝叶斯推断与状态空间估计",
)
p.add_argument("--version", action="store_true", help="打印版本并退出")
sub = p.add_subparsers(dest="cmd")
d = sub.add_parser("demo", help="跑完整基准并落盘 benchmark.json")
d.add_argument("--steps", type=int, default=120, help="时间步长(默认 120)")
d.add_argument("--particles", type=int, default=1000, help="粒子数(默认 1000)")
d.add_argument("--seeds", type=str, default="7,101,202", help="逗号分隔的 seeds")
d.add_argument("--datasets", type=str, default="", help="逗号分隔的数据集名(默认全部)")
d.add_argument("--out", type=str, default="artifacts", help="输出目录")
d.set_defaults(func=cmd_demo)
lst = sub.add_parser("list", help="列出方法/数据集/后端能力")
lst.set_defaults(func=cmd_list)
st = sub.add_parser("selftest", help="只跑确定性不变量(CI 门禁)")
st.set_defaults(func=cmd_selftest)
v = sub.add_parser("version", help="打印版本")
v.set_defaults(func=cmd_version)
return p
def main(argv: list[str] | None = None) -> int:
"""CLI 主入口。"""
parser = build_parser()
args = parser.parse_args(argv)
if getattr(args, "version", False) and not getattr(args, "cmd", None):
return cmd_version(args)
if not getattr(args, "cmd", None):
parser.print_help()
return 0
return int(args.func(args))
if __name__ == "__main__":
raise SystemExit(main())