"""依頼 353 のまとめ: check_353// (simreplay_353.py・features_353.py の出力) を 同じ測り方 (summary_334.agg・agg_feat) で 並べる → check_353/.json・.md. 読むだけ. 表 1: SigLIP の近さ (pool)・明るさの差 (L. シム − 実機)・赤いブロックの重心の差 (中央値. 机側は 100px 以内の中央値も). 机側/手首. 全体と段階ごと 表 2: 色の確かめ (ラベルごとの 実機・シムの L)・手首の影の濃さ・ブロックの面ごとの明るさ・つかんでいる間のすき間 使い方 (conda の lerobot 環境で): python so101/sim/summary_353.py --tags lv0,lv1,lv2,lv3,lv4 --name steps_353 python so101/sim/summary_353.py --tags r342,final --name final_353 """ import argparse import json import sys from pathlib import Path import numpy as np HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) import summary_334 as U # noqa: E402 OUT = HERE.parent / "check_353" C342 = HERE.parent / "check_342" GOAL = {"siglip": 0.88, "desk_px": 5.0, "wrist_px": 15.0, "gap_mm": 1.0} # 済みの条件 (エイイチ 2026-11-28) def load(tag): import pandas as pd if tag == "r342": fr = pd.read_csv(C342 / "frames_342.csv") return fr, pd.read_csv(OUT / tag / "features.csv") if (OUT / tag / "features.csv").exists() else None, None, None d = OUT / tag cat = lambda pat: pd.concat([pd.read_csv(f) for f in sorted(d.glob(pat))], ignore_index=True) if list(d.glob(pat)) else None # noqa: E731 ft = pd.read_csv(d / "features.csv") if (d / "features.csv").exists() else None fr = cat("ep*_frames.csv") if "wrist_sim_block_hidden" not in fr: # simreplay_353 は書かない列 (summary_334.agg が読む) fr["wrist_sim_block_hidden"] = np.nan return fr, ft, cat("ep*_colors.csv"), cat("ep*_faces.csv") def one(fr, ft, cam, stage=None): f = fr if stage is None else fr[fr["stage"] == stage] a = U.agg(f, cam) out = {"frames": a["frames"], "L_diff": a["L_diff_sim_minus_real"], "L_real": a["L_real"], "L_sim": a["L_sim"], "a_diff": a["a_diff"], "b_diff": a["b_diff"], "ssim": a["ssim"], "red_px_median": a.get("red_centroid_dist_px_median"), "red_px_median_within_100": a.get("red_centroid_dist_px_median_within_100"), "red_iou": a.get("red_iou_mean"), "red_area_ratio": a.get("red_area_ratio_sim_over_real_median")} if ft is not None: g = ft[ft["cam"] == cam] if stage is not None: g = g[g["stage"] == stage] out["siglip"] = round(float(g["cos_real_sim_pool"].mean()), 4) if len(g) else None out["siglip_mean_tokens"] = round(float(g["cos_real_sim_mean"].mean()), 4) if len(g) else None return out def gaps(fr): h = fr[fr["held"] == 1] if not len(h): return {} m = np.maximum(h["gap_fixed_mm"].abs(), h["gap_moving_mm"].abs()) return {"held_frames": int(len(h)), "within_1mm_frac": round(float((m <= GOAL["gap_mm"]).mean()), 3), "max_abs_gap_mm_median": round(float(m.median()), 2), "max_abs_gap_mm_p90": round(float(m.quantile(0.9)), 2)} def colors(c): if c is None: return {} out = {} for (cam, lb), g in c.groupby(["cam", "label"]): out[f"{cam}/{lb}"] = {"n_frames": int(len(g)), **{f"{s}_{k}": round(float(g[f"{s}_{k}"].median()), 1) for s in ("real", "sim") for k in ("B", "G", "R", "L", "a", "b")}} return out def faces(x): if x is None: return {} out = {} for k in ("shadow_p10_over_p50", "top_L", "side_L", "side_low_over_high", "ring_a"): for s in ("real", "sim"): col = f"{k}_{s}" if col in x and x[col].notna().any(): out[col] = round(float(x[col].median()), 3) for st in ("ABOVE_BLOCK", "GRASP"): g = x[x["stage"] == st] for s in ("real", "sim"): col = f"shadow_p10_over_p50_{s}" if len(g) and g[col].notna().any(): out[f"{col}_{st}"] = round(float(g[col].median()), 3) if "top_L_real" in x and "side_L_real" in x: g = x.dropna(subset=["top_L_real", "side_L_real"]) out["top_minus_side_real"] = round(float((g["top_L_real"] - g["side_L_real"]).median()), 2) out["top_minus_side_sim"] = round(float((g["top_L_sim"] - g["side_L_sim"]).median()), 2) out["face_frames"] = int(len(g)) return out def fmt(x, n=3): return "-" if x is None or (isinstance(x, float) and np.isnan(x)) else (f"{x:.{n}f}" if isinstance(x, float) else str(x)) def main(): ap = argparse.ArgumentParser() ap.add_argument("--tags", required=True) ap.add_argument("--name", required=True) ap.add_argument("--eps", default="", help="この本だけで数える (例 0,8,18,28,38)") a = ap.parse_args() tags = a.tags.split(",") sel = [int(x) for x in a.eps.split(",")] if a.eps else None res = {} for t in tags: fr, ft, c, x = load(t) if sel is not None: fr = fr[fr["new_episode_index"].isin(sel)] ft = None if ft is None else ft[ft["new_episode_index"].isin(sel)] r = {"episodes": sorted(int(v) for v in fr["new_episode_index"].unique())} for cam in ("desk", "wrist"): r[cam] = {"all": one(fr, ft, cam), "by_stage": {s: one(fr, ft, cam, s) for s in U.STAGE_ORDER if (fr["stage"] == s).any()}} r["gaps"] = gaps(fr) r["colors"] = colors(c) r["faces"] = faces(x) res[t] = r L = [f"# 依頼 353 {a.name}", "", f"本: {res[tags[0]]['episodes']}", "", "## 全体", "", "| | " + " | ".join(tags) + " |", "|---|" + "---|" * len(tags)] rows = [("SigLIP 机側", "desk", "siglip", 3), ("SigLIP 手首", "wrist", "siglip", 3), ("明るさの差 L 机側 (シム−実機)", "desk", "L_diff", 1), ("明るさの差 L 手首", "wrist", "L_diff", 1), ("a* の差 机側", "desk", "a_diff", 1), ("a* の差 手首", "wrist", "a_diff", 1), ("b* の差 机側", "desk", "b_diff", 1), ("b* の差 手首", "wrist", "b_diff", 1), ("赤の重心の差 px 机側 (中央値)", "desk", "red_px_median", 1), ("同 机側 (100px 以内の中央値)", "desk", "red_px_median_within_100", 1), ("赤の重心の差 px 手首 (中央値)", "wrist", "red_px_median", 1), ("赤の IoU 手首", "wrist", "red_iou", 3), ("赤の面積比 手首 (シム/実機)", "wrist", "red_area_ratio", 2), ("SSIM 机側", "desk", "ssim", 3), ("SSIM 手首", "wrist", "ssim", 3)] for nm, cam, k, n in rows: L.append(f"| {nm} | " + " | ".join(fmt(res[t][cam]["all"].get(k), n) for t in tags) + " |") L.append("| つかんでいる間 両方の爪のすき間 1mm 以内の割合 | " + " | ".join(fmt(res[t]["gaps"].get("within_1mm_frac"), 3) for t in tags) + " |") for cam, cj in (("desk", "机側"), ("wrist", "手首")): L += ["", f"## 段階ごと ({cj}): SigLIP / 明るさの差 L / 赤の重心の差 px (中央値)", "", "| 段階 | " + " | ".join(tags) + " |", "|---|" + "---|" * len(tags)] for s in U.STAGE_ORDER: cells = [] for t in tags: b = res[t][cam]["by_stage"].get(s, {}) cells.append(f"{fmt(b.get('siglip'))} / {fmt(b.get('L_diff'), 1)} / {fmt(b.get('red_px_median'), 1)}") L.append(f"| {U.STAGE_JA[s]} | " + " | ".join(cells) + " |") L += ["", "## 色の確かめ (ラベルごとの L の中央値 実機 / シム)", "", "| ラベル | " + " | ".join(tags) + " |", "|---|" + "---|" * len(tags)] keys = sorted({k for t in tags for k in res[t]["colors"]}) for k in keys: L.append(f"| {k} | " + " | ".join( f"{fmt(res[t]['colors'].get(k, {}).get('real_L'), 1)} / {fmt(res[t]['colors'].get(k, {}).get('sim_L'), 1)}" for t in tags) + " |") L += ["", "## 影・ブロックの面 (手首. 実機 / シム)", "", "| | " + " | ".join(tags) + " |", "|---|" + "---|" * len(tags)] for k, nm in (("shadow_p10_over_p50", "机の明るさ 10%÷50% (全段階)"), ("shadow_p10_over_p50_{}_ABOVE_BLOCK", "同 ブロックの上"), ("shadow_p10_over_p50_{}_GRASP", "同 つかむ"), ("top_L", "ブロックの上の面 L"), ("side_L", "ブロックの横の面 L"), ("top_minus_side", "上の面 − 横の面"), ("side_low_over_high", "横の面 下1/3 ÷ 上1/3"), ("ring_a", "まわりの机の a* − 机全体")): def g(t, s): ff = res[t]["faces"] return ff.get(k.format(s)) if "{}" in k else ff.get(f"{k}_{s}") L.append(f"| {nm} | " + " | ".join(f"{fmt(g(t, 'real'), 2)} / {fmt(g(t, 'sim'), 2)}" for t in tags) + " |") (OUT / f"{a.name}.json").write_text(json.dumps({"made_by": "so101/sim/summary_353.py (依頼 353)", "goal": GOAL, "tags": res}, ensure_ascii=False, indent=1)) (OUT / f"{a.name}.md").write_text("\n".join(L) + "\n") print("\n".join(L)) if __name__ == "__main__": main()