File size: 7,699 Bytes
e37c13d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Add tactile validity flags to parquet files that were built before them.

The already-published release has no ``tactile_*_is_new`` columns, and
rebuilding it from source would mean re-encoding every video. Instead we
recover the flags from data already in the parquet.

Method — a repeated GelSight frame produces a bit-identical contact triple
(intensity, area, mixed), because all three are deterministic reductions of
the same pixels. So a row is a fresh reading exactly when its triple differs
from the previous row's.

This is a *proxy*, not a pixel comparison: two genuinely different frames
would have to agree in all three float32 reductions to be missed, which does
not happen in practice but is not impossible. ``verify_against_video()``
checks the proxy against real decoded frames on a sample.
"""
from __future__ import annotations

from pathlib import Path

import numpy as np
import pyarrow.parquet as pq

from .contact import duplication_stats
from .meta import backfill_is_new

SCALARS = ("intensity", "area", "mixed")


def flags_from_scalars(table, side: str) -> np.ndarray:
    """Recover per-row 'this is a new tactile frame' flags for one side."""
    cols = [np.asarray(table[f"tactile_{side}_{s}"].to_numpy(), np.float64)
            for s in SCALARS if f"tactile_{side}_{s}" in table.column_names]
    if not cols:
        raise KeyError(f"parquet has no tactile_{side}_* contact columns")
    stacked = np.stack(cols, axis=1)
    is_new = np.ones(len(stacked), dtype=bool)
    if len(stacked) > 1:
        is_new[1:] = np.any(stacked[1:] != stacked[:-1], axis=1)
    return is_new


def process_parquet(path: Path, dry_run: bool = False) -> dict:
    """Backfill one parquet in place; returns its duplication stats."""
    table = pq.read_table(str(path))
    left = flags_from_scalars(table, "left")
    right = flags_from_scalars(table, "right")
    if not dry_run:
        pq.write_table(backfill_is_new(table, left, right), str(path))
    return {
        "path": str(path),
        "left": duplication_stats(left),
        "right": duplication_stats(right),
    }


def process_tree(root: Path, dry_run: bool = False) -> list[dict]:
    """Backfill every ``meta/**/episode_*.parquet`` under a task directory."""
    files = sorted(Path(root).rglob("meta/**/episode_*.parquet"))
    if not files:
        files = sorted(Path(root).rglob("episode_*.parquet"))
    return [process_parquet(p, dry_run) for p in files]


def aggregate(reports: list[dict]) -> dict:
    """Dataset-level duplication summary across episodes."""
    total = unique = 0
    worst = 0
    for rep in reports:
        for side in ("left", "right"):
            s = rep[side]
            total += s["n_frames"]
            unique += s["n_unique"]
            worst = max(worst, s["max_repeat_run"])
    ratio = 1.0 - unique / total if total else 0.0
    return {
        "episodes": len(reports),
        "rows": total,
        "unique_tactile": unique,
        "duplicate_ratio": ratio,
        "effective_fps": 30.0 * (1.0 - ratio),
        "max_repeat_run": worst,
    }


def _source_is_new(h5_path: Path, side: str, start: int, count: int) -> np.ndarray:
    """Bit-exact 'this frame differs from the previous one' over source pixels."""
    import h5py
    import hdf5plugin  # noqa: F401

    with h5py.File(str(h5_path), "r") as f:
        block = f[f"gelsight/{side}/frames"][start:start + count]
    truth = np.ones(len(block), bool)
    for i in range(1, len(block)):
        truth[i] = not np.array_equal(block[i], block[i - 1])
    return truth


def verify_against_h5(parquet_path: Path, h5_path: Path, side: str = "left",
                      limit: int = 600, shift: int | None = None,
                      search: range | None = None) -> dict:
    """Ground-truth check of the flags against the source H5 pixels.

    The source frames are the only bit-exact reference — the published MP4s are
    H.264-encoded, so a duplicated frame does not decode back to identical
    pixels (use ``verify_against_video``, which compares with a tolerance).

    A published episode may have had a constant tactile latency shift baked in,
    in which case parquet row ``i`` corresponds to source frame
    ``trim + i + shift``. Pass ``shift``, or leave it None to search for the
    value that lines the two up — that search doubles as an integrity check
    that the intended correction really was applied.
    """
    table = pq.read_table(str(parquet_path))
    proxy = flags_from_scalars(table, side)[:limit]
    trim = int(np.asarray(table["source_h5_frame"].to_numpy())[0])
    n_req = len(proxy)

    candidates = ([shift] if shift is not None
                  else list(search) if search is not None else [0, 15])
    lo, hi = min(candidates), max(candidates)

    # Read the source span once and slide over it — re-reading per candidate
    # shift would multiply the (large) HDF5 traffic by len(candidates).
    start = max(0, trim + lo - 1)                 # one extra for a predecessor
    pad = (trim + lo) - start                     # 1 unless clamped at 0
    span = _source_is_new(h5_path, side, start, (hi - lo) + n_req + pad)

    def slice_truth(sh: int) -> np.ndarray:
        off = pad + (sh - lo)
        return span[off:off + n_req]

    # Row 0 is True by convention on both sides (neither has a predecessor
    # inside its own window), so it carries no evidence — compare from row 1.
    scored = []
    for sh in candidates:
        truth = slice_truth(sh)
        n = min(len(truth), n_req)
        if n < 2:
            continue
        scored.append((int((truth[1:n] != proxy[1:n]).sum()), n, sh))
    if not scored:
        raise ValueError(f"{parquet_path.name}: no overlap with source frames")

    mismatches, n, best = min(scored, key=lambda x: x[0])
    truth = slice_truth(best)
    return {
        "compared": int(n - 1),
        "mismatches": int(mismatches),
        "shift": int(best),
        "shift_detected": shift is None,
        "proxy_unique": int(proxy[1:n].sum()),
        "source_unique": int(truth[1:n].sum()),
    }


def verify_against_video(parquet_path: Path, video_path: Path,
                         limit: int = 300, tol: float = 1.0) -> dict:
    """Check the proxy against decoded video, comparing with a tolerance.

    H.264 is lossy, so a duplicated source frame still decodes to slightly
    different pixels. We therefore call two decoded frames "the same" when
    their mean absolute difference stays below `tol`, and report the observed
    separation so the threshold can be sanity-checked rather than trusted.
    """
    import av

    table = pq.read_table(str(parquet_path))
    side = "left" if "tactile_left" in video_path.name else "right"
    proxy = flags_from_scalars(table, side)[:limit]

    mad, prev = [], None
    with av.open(str(video_path)) as container:
        for i, frame in enumerate(container.decode(video=0)):
            if i >= limit:
                break
            arr = frame.to_ndarray(format="rgb24").astype(np.float32)
            mad.append(0.0 if prev is None
                       else float(np.abs(arr - prev).mean()))
            prev = arr
    mad = np.asarray(mad)
    truth = mad > tol
    truth[0] = True

    n = min(len(truth), len(proxy))
    dup_mad = mad[1:n][~proxy[1:n]]
    new_mad = mad[1:n][proxy[1:n]]
    return {
        "compared": int(n),
        "mismatches": int((truth[:n] != proxy[:n]).sum()),
        "proxy_unique": int(proxy[:n].sum()),
        "video_unique": int(truth[:n].sum()),
        "mad_duplicate_max": float(dup_mad.max()) if len(dup_mad) else 0.0,
        "mad_new_min": float(new_mad.min()) if len(new_mad) else 0.0,
    }