File size: 6,483 Bytes
b6fe757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83a372f
 
b6fe757
 
0b2a584
 
b6fe757
 
 
 
 
 
 
 
 
 
 
 
 
2de4806
b6fe757
 
0b2a584
b6fe757
2de4806
b6fe757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83a372f
b6fe757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""One sensor moves, the other stays, they never touch, and depth is visible.

Four properties the first version of the probe set did not have:

  1  THE WHOLE SENSOR FRAME IS DRAWN, not a dot. A dot shows position and
     hides orientation, which makes the six ROTATION probes unreadable — they
     move the marker barely at all. The React previews draw a triad for this
     reason and so does this.

  2  PERSPECTIVE IS REAL. The axis tips are placed in 3D at a fixed
     millimetre length and then projected, so a sensor nearer the camera draws
     a larger triad. Drawing a fixed PIXEL length would assert that two
     sensors at different depths are the same size, which is the one thing a
     projection is supposed to tell you.

  3  ONE SENSOR MOVES PER PROBE, and which one is random. Every probe in the
     first version moved the left sensor, so half the rig was never exercised
     and any left-specific defect would have been invisible.

  4  THEY DO NOT COLLIDE. Each gel carries a 0.12 m-diameter exclusion circle,
     so the two centres must stay at least 0.12 m apart for every step. In the
     real data the centres sit 0.225 m apart at the median and closer than
     0.12 m on 1.67% of frames, so this rejects a little and forbids the
     physically impossible.

    python scripts/test_probe_frame_and_collision.py
"""
from __future__ import annotations

import shutil
import sys
import tempfile
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))

from react_paths import force_meta   # noqa: E402

import numpy as np                                              # noqa: E402

from react_toolbox.staging import staging_dir

RESULTS: list[tuple[bool, str, str]] = []
COLLISION_M = 0.12


def check(ok: bool, name: str, evidence: str) -> None:
    RESULTS.append((bool(ok), name, evidence))


def main() -> int:
    import pyarrow.parquet as pq

    import react_toolbox as T
    from react_toolbox.synth_actions import COLLISION_DIAMETER_M, sample_probe
    from react_toolbox.frames import as_up_axis
    from twm.calib_epoch import calib_dir

    stage = staging_dir()
    shutil.copytree(calib_dir("motherboard"), stage / "calibration")
    cal = as_up_axis(T.load_calibration(stage), "z")  # release poses
    cam = cal["cams"]["middle"]

    check(abs(COLLISION_DIAMETER_M - COLLISION_M) < 1e-9,
          "the collision diameter is 0.12 m",
          f"COLLISION_DIAMETER_M = {COLLISION_DIAMETER_M}")

    # 1 / 2 — the frame projects with real perspective
    if not hasattr(T, "project_gel_frame"):
        check(False, "the whole sensor frame is projected", "no project_gel_frame")
        check(False, "a nearer sensor draws a bigger triad", "not attempted")
    else:
        # DEPTH IS MEASURED, NOT ASSUMED. My first version displaced along
        # world +z and called the result "further from the camera"; world +z
        # is not the camera's view direction here, so the "far" pose came out
        # NEARER and the check failed on its own premise. `project_gel_frame`
        # returns the camera-frame depth, so the two poses are ordered by what
        # it reports rather than by which way I guessed the axis points.
        base = np.array([0.40, 0.02, 0.20, 0.0, 0.0, 0.0, 1.0])
        cands = []
        for k in range(3):
            for sgn in (+1, -1):
                q = base.copy()
                q[k] += sgn * 0.35
                r = T.project_gel_frame(q, cal["gel_left"], cam, axis_len_mm=60.0)
                if r is not None:
                    cands.append((r["depth_mm"], q))
        cands.sort()
        near, far = cands[0][1], cands[-1][1]
        depths = (cands[0][0], cands[-1][0])
        spans = []
        for p in (near, far):
            r = T.project_gel_frame(p, cal["gel_left"], cam, axis_len_mm=60.0)
            if r is None:
                spans.append(None)
                continue
            c, tips = r["centre"], [t for t in r["tips"] if t is not None]
            spans.append(max(float(np.hypot(t[0] - c[0], t[1] - c[1]))
                             for t in tips) if tips else 0.0)
        ok = all(s is not None for s in spans)
        check(ok and len(spans[0:1]) == 1,
              "the whole sensor frame is projected",
              f"centre + {len(tips)} axis tips")
        check(ok and spans[0] > spans[1] * 1.2,
              "a nearer sensor draws a bigger triad",
              f"depth {depths[0]:.0f} mm -> span {spans[0]:.1f} px, "
              f"depth {depths[1]:.0f} mm -> span {spans[1]:.1f} px"
              if ok else "projection failed")

    # 3 / 4 — one sensor moves, chosen at random, and no collision
    P = (str(force_meta("motherboard"))+"/"
         "2026-05-11/episode_003.parquet")
    t = pq.read_table(P, columns=["sensor_left_pose", "sensor_right_pose"]).to_pydict()
    poses = {s: np.asarray([x for x in t[f"sensor_{s}_pose"]], float)
             for s in ("left", "right")}

    moving = set()
    viol = []
    for seed in range(12):
        try:
            r = sample_probe(poses, cal, seed=seed, view="middle")
        except (TypeError, ValueError) as exc:
            check(False, "one sensor moves per probe, chosen at random",
                  f"sample_probe: {type(exc).__name__}: {str(exc)[:70]}")
            check(False, "the two sensors never come within 0.12 m",
                  "not attempted")
            _report()
            return 1
        moving.add(r["moving_side"])
        for p in r["probes"]:
            d = p.get("min_separation_m")
            if d is not None and d < COLLISION_M - 1e-9:
                viol.append(f"{r['moving_side']}/{p['name']}: {d:.3f} m")
    check(moving == {"left", "right"},
          "one sensor moves per probe, chosen at random",
          f"over 12 seeds the moving side was {sorted(moving)}")
    check(not viol, "the two sensors never come within 0.12 m",
          f"{len(viol)} violating probe(s)" + (f"; {viol[:2]}" if viol else ""))

    _report()
    return 1 if sum(not ok for ok, _, _ in RESULTS) else 0


def _report() -> None:
    w = max(len(x) for _, x, _ in RESULTS)
    print()
    for ok, name, ev in RESULTS:
        print(f"  [{'ok' if ok else 'FAIL'}] {name:<{w}}  {ev}")
    print(f"\nprobe frame + collision: {len(RESULTS)} checks, "
          f"{sum(not ok for ok, _, _ in RESULTS)} failing")


if __name__ == "__main__":
    raise SystemExit(main())