Download validate.py from faradayfuture/motionSample: direct link, hf CLI and curl.
- Browser
- Download file 4.18 kB
-
https://huggingface.co/datasets/faradayfuture/motionSample/resolve/main/validate.py
- Command line
-
hf download hf://datasets/faradayfuture/motionSample/validate.py
-
curl -L -o validate.py https://huggingface.co/datasets/faradayfuture/motionSample/resolve/main/validate.py
4.18 kB
| """motionSample 的自洽性检查: python3 validate.py | |
| 检查键、形状、四元数模长、pose_aa 能否零误差还原 dof、接触相位, 以及用随文件发布的 | |
| com_pos 拟合腾空相重力。只依赖 numpy。 | |
| """ | |
| import pickle | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| HERE = Path(__file__).resolve().parent | |
| REQUIRED = {"root_trans_offset": 3, "root_rot": 4, "dof": None, "dof_pos": None, | |
| "dof_vel": None, "root_lin_vel": 3, "root_ang_vel": 3, "contacts": 2, | |
| "com_pos": 3} | |
| G = 9.80665 | |
| fail = [] | |
| def chk(name, ok, detail=""): | |
| print((" OK " if ok else " FAIL ") + name + (f" — {detail}" if detail else "")) | |
| if not ok: | |
| fail.append(name) | |
| def air_runs(mask): | |
| m = np.concatenate([[0], mask.astype(int), [0]]) | |
| d = np.diff(m) | |
| return list(zip(np.where(d == 1)[0], np.where(d == -1)[0])) | |
| def main(): | |
| files = sorted(HERE.glob("*.pkl")) | |
| if not files: | |
| print("目录下没有 .pkl") | |
| return 1 | |
| d = {} | |
| for f in files: | |
| one = pickle.load(open(f, "rb")) | |
| print(f"── {f.name} {len(one)} 条: {list(one)}") | |
| for k in one: | |
| chk(f"{f.name}: 文件名与内部键一致", k == f.stem, f"键 {k}") | |
| d.update(one) | |
| for k, m in d.items(): | |
| print(f"── 结构 ── {k}") | |
| T, D = m["dof"].shape | |
| chk(f"{k}: 必需键齐全", not (set(REQUIRED) - set(m)), | |
| str(set(REQUIRED) - set(m)) or f"T={T} D={D}") | |
| bad = [c for c, w in REQUIRED.items() if w and m[c].shape != (T, w)] | |
| chk(f"{k}: 各键形状一致", not bad, ", ".join(bad)) | |
| chk(f"{k}: dof_names / dof_axes 长度 == D", | |
| len(m["dof_names"]) == D and m["dof_axes"].shape == (D, 3)) | |
| chk(f"{k}: quat_order 标注为 xyzw", m.get("quat_order") == "xyzw") | |
| chk(f"{k}: fps 是正整数", isinstance(m["fps"], int) and m["fps"] > 0, str(m["fps"])) | |
| print(f"── 数值 ── {k}") | |
| chk(f"{k}: 无 NaN/Inf", | |
| all(np.isfinite(m[c]).all() for c in REQUIRED if c in m)) | |
| n = np.linalg.norm(m["root_rot"], axis=1) | |
| chk(f"{k}: |root_rot| == 1", np.allclose(n, 1, atol=1e-5), f"{n.min():.6f}~{n.max():.6f}") | |
| chk(f"{k}: dof 与 dof_pos 是同一份", np.array_equal(m["dof"], m["dof_pos"])) | |
| rec = np.einsum("tjc,jc->tj", m["pose_aa"][:, 1:], m["dof_axes"]) | |
| e = np.abs(rec - m["dof"]).max() | |
| chk(f"{k}: pose_aa 沿关节轴投影可还原 dof", e < 1e-5, f"最大误差 {e:.2e} rad") | |
| c, fps = m["contacts"], m["fps"] | |
| air = c.max(1) == 0 | |
| runs = [(a, b) for a, b in air_runs(air) if (b - a) / fps > 0.15] | |
| n2 = (c.sum(1) == 2).mean() * 100 | |
| dur = np.array([(b - a) / fps for a, b in runs]) | |
| print(f" 双支撑 {n2:.1f}% | 腾空 {air.mean()*100:.1f}%" | |
| + (f" | 腾空段 >150ms 共 {len(runs)} 次, 中位 {np.median(dur)*1000:.0f} ms" | |
| if len(runs) else " | 无 >150ms 的腾空段")) | |
| if not runs: | |
| print(" 注: 没有 >150ms 的腾空段, 接触模式偏走路而非跑步(见 README)") | |
| # 腾空相重力: 取每段中间 2/3 拟合抛物线, 避开起落瞬间的接触判定误差 | |
| if runs: | |
| acc = [] | |
| for a, b in runs: | |
| w = (b - a) // 6 | |
| a2, b2 = a + w, b - w | |
| if b2 - a2 < 8: | |
| continue | |
| t = np.arange(b2 - a2) / fps | |
| acc.append(2 * np.polyfit(t, m["com_pos"][a2:b2, 2], 2)[0]) | |
| if acc: | |
| acc = np.array(acc) | |
| r = abs(acc.mean()) / G | |
| print(f" 腾空相重力 {acc.mean():+.3f} ± {acc.std():.3f} m/s²" | |
| f" = {r:.3f} g ({len(acc)} 段)") | |
| if abs(r - 1) > 0.05: | |
| print(f" 注: 偏离 -9.807 达 {abs(r-1)*100:.0f}%," | |
| f" 动力学不自洽(见 README 的 Froude 说明)") | |
| print() | |
| print(f"失败 {len(fail)} 项: " + ", ".join(fail) if fail else "全部通过") | |
| return 1 if fail else 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |