Add session/per-episode rest-gel reference (robust to all-contact episodes) + validation
Browse files- data/motherboard/reference/2026-05-10/tactile_left.png +3 -0
- data/motherboard/reference/2026-05-10/tactile_right.png +3 -0
- data/motherboard/reference/2026-05-11/tactile_left.png +3 -0
- data/motherboard/reference/2026-05-11/tactile_right.png +3 -0
- data/motherboard/reference/2026-05-19/tactile_left.png +3 -0
- data/motherboard/reference/2026-05-19/tactile_right.png +3 -0
- data/motherboard/reference/drift_check.png +3 -0
- data/motherboard/reference/manifest.json +8 -0
- data/motherboard/reference/session_references_overview.png +3 -0
- data/motherboard/reference/validation.md +74 -0
- toolbox/__init__.py +2 -2
- toolbox/build_session_references.py +70 -0
- toolbox/reference.py +73 -0
data/motherboard/reference/2026-05-10/tactile_left.png
ADDED
|
Git LFS Details
|
data/motherboard/reference/2026-05-10/tactile_right.png
ADDED
|
Git LFS Details
|
data/motherboard/reference/2026-05-11/tactile_left.png
ADDED
|
Git LFS Details
|
data/motherboard/reference/2026-05-11/tactile_right.png
ADDED
|
Git LFS Details
|
data/motherboard/reference/2026-05-19/tactile_left.png
ADDED
|
Git LFS Details
|
data/motherboard/reference/2026-05-19/tactile_right.png
ADDED
|
Git LFS Details
|
data/motherboard/reference/drift_check.png
ADDED
|
Git LFS Details
|
data/motherboard/reference/manifest.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"2026-05-10/tactile_left": "2026-05-10/tactile_left.png",
|
| 3 |
+
"2026-05-10/tactile_right": "2026-05-10/tactile_right.png",
|
| 4 |
+
"2026-05-11/tactile_left": "2026-05-11/tactile_left.png",
|
| 5 |
+
"2026-05-11/tactile_right": "2026-05-11/tactile_right.png",
|
| 6 |
+
"2026-05-19/tactile_left": "2026-05-19/tactile_left.png",
|
| 7 |
+
"2026-05-19/tactile_right": "2026-05-19/tactile_right.png"
|
| 8 |
+
}
|
data/motherboard/reference/session_references_overview.png
ADDED
|
Git LFS Details
|
data/motherboard/reference/validation.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Rest-gel reference — corrected build + validation
|
| 2 |
+
|
| 3 |
+
**Design (corrected after a drift check):** PRIMARY reference = per-episode fuzzy-mode (`fuzzy_mode_background`), drift-free and robust to partial contact. FALLBACK = per-session mode (`session_reference`, `<date>/tactile_*.png`) only for episodes that are ~100% contact (own mode contaminated).
|
| 4 |
+
|
| 5 |
+
A single session reference is NOT a wholesale replacement: intra-day gel drift makes it differ from a given episode by a diffuse ~0.37 area with no object (see `drift_check.png`), so per-episode mode is preferred whenever the episode has clean frames.
|
| 6 |
+
|
| 7 |
+
**Over 64 episode-sides:** need session fallback (no clean frame of own): **1** ['003 left']. Old p01 anchors that were a contact/marginal frame the per-episode MODE fixes: **0** [].
|
| 8 |
+
|
| 9 |
+
| ep | date | side | frames | clean(vs own mode) | clean% | old_p01_area | flag |
|
| 10 |
+
|---|---|---|---|---|---|---|---|
|
| 11 |
+
| 003 | 2026-05-19 | left | 121 | 0 | 0.0 | 0.105 | NEEDS_SESSION_FALLBACK |
|
| 12 |
+
| 017 | 2026-05-11 | left | 14511 | 636 | 4.4 | 0.009 | |
|
| 13 |
+
| 017 | 2026-05-11 | right | 14511 | 2441 | 16.8 | 0.001 | |
|
| 14 |
+
| 001 | 2026-05-19 | left | 740 | 149 | 20.1 | 0.002 | |
|
| 15 |
+
| 000 | 2026-05-19 | left | 1434 | 289 | 20.2 | 0.001 | |
|
| 16 |
+
| 004 | 2026-05-10 | right | 2406 | 591 | 24.6 | 0.0 | |
|
| 17 |
+
| 004 | 2026-05-19 | left | 209 | 52 | 24.9 | 0.005 | |
|
| 18 |
+
| 000 | 2026-05-19 | right | 1434 | 358 | 25.0 | 0.0 | |
|
| 19 |
+
| 006 | 2026-05-10 | right | 3925 | 1133 | 28.9 | 0.0 | |
|
| 20 |
+
| 002 | 2026-05-19 | left | 1710 | 568 | 33.2 | 0.0 | |
|
| 21 |
+
| 002 | 2026-05-19 | right | 1710 | 575 | 33.6 | 0.0 | |
|
| 22 |
+
| 011 | 2026-05-10 | right | 2201 | 743 | 33.8 | 0.0 | |
|
| 23 |
+
| 004 | 2026-05-10 | left | 2406 | 908 | 37.7 | 0.0 | |
|
| 24 |
+
| 010 | 2026-05-10 | left | 1035 | 403 | 38.9 | 0.001 | |
|
| 25 |
+
| 001 | 2026-05-19 | right | 740 | 295 | 39.9 | 0.0 | |
|
| 26 |
+
| 011 | 2026-05-10 | left | 2201 | 915 | 41.6 | 0.001 | |
|
| 27 |
+
| 008 | 2026-05-11 | right | 13705 | 5793 | 42.3 | 0.0 | |
|
| 28 |
+
| 009 | 2026-05-11 | right | 4366 | 1850 | 42.4 | 0.0 | |
|
| 29 |
+
| 004 | 2026-05-19 | right | 209 | 90 | 43.1 | 0.001 | |
|
| 30 |
+
| 014 | 2026-05-11 | right | 2416 | 1044 | 43.2 | 0.0 | |
|
| 31 |
+
| 006 | 2026-05-10 | left | 3925 | 1705 | 43.4 | 0.0 | |
|
| 32 |
+
| 009 | 2026-05-10 | right | 1823 | 807 | 44.3 | 0.0 | |
|
| 33 |
+
| 005 | 2026-05-10 | left | 763 | 342 | 44.8 | 0.001 | |
|
| 34 |
+
| 012 | 2026-05-11 | right | 14828 | 7426 | 50.1 | 0.0 | |
|
| 35 |
+
| 003 | 2026-05-10 | left | 9262 | 4653 | 50.2 | 0.0 | |
|
| 36 |
+
| 000 | 2026-05-10 | left | 6890 | 3534 | 51.3 | 0.0 | |
|
| 37 |
+
| 003 | 2026-05-19 | right | 121 | 65 | 53.7 | 0.0 | |
|
| 38 |
+
| 003 | 2026-05-10 | right | 9262 | 4979 | 53.8 | 0.0 | |
|
| 39 |
+
| 007 | 2026-05-10 | right | 1927 | 1039 | 53.9 | 0.0 | |
|
| 40 |
+
| 005 | 2026-05-11 | left | 14498 | 8123 | 56.0 | 0.001 | |
|
| 41 |
+
| 010 | 2026-05-10 | right | 1035 | 583 | 56.3 | 0.0 | |
|
| 42 |
+
| 013 | 2026-05-11 | right | 12724 | 7176 | 56.4 | 0.0 | |
|
| 43 |
+
| 007 | 2026-05-11 | right | 6908 | 3903 | 56.5 | 0.002 | |
|
| 44 |
+
| 007 | 2026-05-10 | left | 1927 | 1092 | 56.7 | 0.0 | |
|
| 45 |
+
| 016 | 2026-05-11 | left | 11896 | 6756 | 56.8 | 0.0 | |
|
| 46 |
+
| 015 | 2026-05-11 | right | 5959 | 3433 | 57.6 | 0.0 | |
|
| 47 |
+
| 001 | 2026-05-10 | left | 7621 | 4405 | 57.8 | 0.0 | |
|
| 48 |
+
| 008 | 2026-05-10 | right | 1305 | 760 | 58.2 | 0.0 | |
|
| 49 |
+
| 014 | 2026-05-11 | left | 2416 | 1413 | 58.5 | 0.0 | |
|
| 50 |
+
| 008 | 2026-05-10 | left | 1305 | 769 | 58.9 | 0.0 | |
|
| 51 |
+
| 010 | 2026-05-11 | right | 6828 | 4117 | 60.3 | 0.0 | |
|
| 52 |
+
| 012 | 2026-05-11 | left | 14828 | 9042 | 61.0 | 0.001 | |
|
| 53 |
+
| 016 | 2026-05-11 | right | 11896 | 7268 | 61.1 | 0.0 | |
|
| 54 |
+
| 011 | 2026-05-11 | right | 7299 | 4518 | 61.9 | 0.0 | |
|
| 55 |
+
| 009 | 2026-05-11 | left | 4366 | 2718 | 62.3 | 0.001 | |
|
| 56 |
+
| 005 | 2026-05-10 | right | 763 | 477 | 62.5 | 0.0 | |
|
| 57 |
+
| 002 | 2026-05-10 | left | 10475 | 6606 | 63.1 | 0.0 | |
|
| 58 |
+
| 006 | 2026-05-11 | left | 11990 | 7565 | 63.1 | 0.002 | |
|
| 59 |
+
| 009 | 2026-05-10 | left | 1823 | 1154 | 63.3 | 0.001 | |
|
| 60 |
+
| 006 | 2026-05-11 | right | 11990 | 7654 | 63.8 | 0.0 | |
|
| 61 |
+
| 008 | 2026-05-11 | left | 13705 | 8769 | 64.0 | 0.001 | |
|
| 62 |
+
| 015 | 2026-05-11 | left | 5959 | 3881 | 65.1 | 0.0 | |
|
| 63 |
+
| 003 | 2026-05-11 | left | 10032 | 6550 | 65.3 | 0.001 | |
|
| 64 |
+
| 005 | 2026-05-11 | right | 14498 | 9523 | 65.7 | 0.001 | |
|
| 65 |
+
| 010 | 2026-05-11 | left | 6828 | 4491 | 65.8 | 0.0 | |
|
| 66 |
+
| 007 | 2026-05-11 | left | 6908 | 4624 | 66.9 | 0.0 | |
|
| 67 |
+
| 004 | 2026-05-11 | left | 2638 | 1786 | 67.7 | 0.002 | |
|
| 68 |
+
| 000 | 2026-05-10 | right | 6890 | 4762 | 69.1 | 0.0 | |
|
| 69 |
+
| 011 | 2026-05-11 | left | 7299 | 5061 | 69.3 | 0.0 | |
|
| 70 |
+
| 004 | 2026-05-11 | right | 2638 | 1844 | 69.9 | 0.0 | |
|
| 71 |
+
| 002 | 2026-05-10 | right | 10475 | 7455 | 71.2 | 0.0 | |
|
| 72 |
+
| 003 | 2026-05-11 | right | 10032 | 7148 | 71.3 | 0.0 | |
|
| 73 |
+
| 013 | 2026-05-11 | left | 12724 | 9224 | 72.5 | 0.001 | |
|
| 74 |
+
| 001 | 2026-05-10 | right | 7621 | 5799 | 76.1 | 0.0 | |
|
toolbox/__init__.py
CHANGED
|
@@ -9,7 +9,7 @@ React dataset (GelSight Mini, markerless, 640×480). MIT licensed.
|
|
| 9 |
)
|
| 10 |
"""
|
| 11 |
from .io import load_video, load_meta, episode_paths
|
| 12 |
-
from .reference import get_reference, difference, l2_diff
|
| 13 |
from .contact import contact_mask, contact_metrics, contact_centroid, TAU
|
| 14 |
from .actions import next_state_action, delta_pose_action, integrate_delta
|
| 15 |
from .viz import diff_heatmap, contact_overlay, reference_compare, depth_view, height_to_pointcloud
|
|
@@ -17,7 +17,7 @@ from .calibration import load_calibration, project_gel_to_pixel
|
|
| 17 |
|
| 18 |
__all__ = [
|
| 19 |
"load_video", "load_meta", "episode_paths",
|
| 20 |
-
"get_reference", "difference", "l2_diff",
|
| 21 |
"contact_mask", "contact_metrics", "contact_centroid", "TAU",
|
| 22 |
"next_state_action", "delta_pose_action", "integrate_delta",
|
| 23 |
"diff_heatmap", "contact_overlay", "reference_compare", "depth_view", "height_to_pointcloud",
|
|
|
|
| 9 |
)
|
| 10 |
"""
|
| 11 |
from .io import load_video, load_meta, episode_paths
|
| 12 |
+
from .reference import get_reference, difference, l2_diff, fuzzy_mode_background, session_reference
|
| 13 |
from .contact import contact_mask, contact_metrics, contact_centroid, TAU
|
| 14 |
from .actions import next_state_action, delta_pose_action, integrate_delta
|
| 15 |
from .viz import diff_heatmap, contact_overlay, reference_compare, depth_view, height_to_pointcloud
|
|
|
|
| 17 |
|
| 18 |
__all__ = [
|
| 19 |
"load_video", "load_meta", "episode_paths",
|
| 20 |
+
"get_reference", "difference", "l2_diff", "fuzzy_mode_background", "session_reference",
|
| 21 |
"contact_mask", "contact_metrics", "contact_centroid", "TAU",
|
| 22 |
"next_state_action", "delta_pose_action", "integrate_delta",
|
| 23 |
"diff_heatmap", "contact_overlay", "reference_compare", "depth_view", "height_to_pointcloud",
|
toolbox/build_session_references.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Build one rest-gel reference per (date, sensor) for the React dataset.
|
| 2 |
+
|
| 3 |
+
Each episode is a cut from a continuous per-day recording, so all episodes of a
|
| 4 |
+
date share one gel/lighting. This pools every episode's tactile frames for a
|
| 5 |
+
date and takes the per-pixel fuzzy mode (the resting gel) -- robust even when an
|
| 6 |
+
individual episode is in contact its whole length. Writes:
|
| 7 |
+
|
| 8 |
+
data/<task>/reference/<date>/tactile_{left,right}.png
|
| 9 |
+
data/<task>/reference/manifest.json
|
| 10 |
+
|
| 11 |
+
Then use it instead of a per-episode reference:
|
| 12 |
+
|
| 13 |
+
ref = imageio.imread(f"data/{task}/reference/{date}/tactile_{side}.png")
|
| 14 |
+
diff = difference(frame, ref) # toolbox.reference
|
| 15 |
+
|
| 16 |
+
Usage:
|
| 17 |
+
python -m toolbox.build_session_references --root data/motherboard
|
| 18 |
+
"""
|
| 19 |
+
from __future__ import annotations
|
| 20 |
+
import argparse, json
|
| 21 |
+
from collections import defaultdict
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
|
| 24 |
+
import numpy as np
|
| 25 |
+
import imageio.v2 as imageio
|
| 26 |
+
|
| 27 |
+
from .io import load_video, episode_paths
|
| 28 |
+
from .reference import session_reference
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def main():
|
| 32 |
+
ap = argparse.ArgumentParser()
|
| 33 |
+
ap.add_argument("--root", required=True, help="task root, e.g. data/motherboard")
|
| 34 |
+
ap.add_argument("--episodes_jsonl", default=None,
|
| 35 |
+
help="defaults to <root>/episodes.jsonl")
|
| 36 |
+
ap.add_argument("--stride", type=int, default=3, help="frame subsample for speed")
|
| 37 |
+
ap.add_argument("--max_frames", type=int, default=4000)
|
| 38 |
+
args = ap.parse_args()
|
| 39 |
+
root = Path(args.root)
|
| 40 |
+
jl = Path(args.episodes_jsonl or (root / "episodes.jsonl"))
|
| 41 |
+
eps = [json.loads(l) for l in jl.read_text().splitlines() if l.strip()]
|
| 42 |
+
|
| 43 |
+
by_date = defaultdict(list)
|
| 44 |
+
for e in eps:
|
| 45 |
+
by_date[e["date"]].append(e["episode"])
|
| 46 |
+
|
| 47 |
+
manifest = {}
|
| 48 |
+
for date, ep_keys in sorted(by_date.items()):
|
| 49 |
+
for side in ("left", "right"):
|
| 50 |
+
frames = []
|
| 51 |
+
for ek in sorted(ep_keys):
|
| 52 |
+
p = episode_paths(root, ek)["tactile_" + side]
|
| 53 |
+
if not Path(p).is_file():
|
| 54 |
+
continue
|
| 55 |
+
v = load_video(p) # (T,H,W,3)
|
| 56 |
+
frames.append(v[:: args.stride])
|
| 57 |
+
if not frames:
|
| 58 |
+
continue
|
| 59 |
+
ref = session_reference(frames, max_frames=args.max_frames)
|
| 60 |
+
out = root / "reference" / date / f"tactile_{side}.png"
|
| 61 |
+
out.parent.mkdir(parents=True, exist_ok=True)
|
| 62 |
+
imageio.imwrite(out, ref)
|
| 63 |
+
manifest[f"{date}/tactile_{side}"] = str(out.relative_to(root))
|
| 64 |
+
print(f"[ref] {date} {side}: pooled {len(frames)} episodes -> {out}")
|
| 65 |
+
(root / "reference" / "manifest.json").write_text(json.dumps(manifest, indent=2))
|
| 66 |
+
print(f"[done] {len(manifest)} references")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
main()
|
toolbox/reference.py
CHANGED
|
@@ -73,3 +73,76 @@ def l2_diff(frame, reference):
|
|
| 73 |
d[h,w] = ||frame[h,w,:] - ref[h,w,:]||_2.
|
| 74 |
"""
|
| 75 |
return np.sqrt(((frame.astype(np.float32) - reference.astype(np.float32)) ** 2).sum(axis=2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
d[h,w] = ||frame[h,w,:] - ref[h,w,:]||_2.
|
| 74 |
"""
|
| 75 |
return np.sqrt(((frame.astype(np.float32) - reference.astype(np.float32)) ** 2).sum(axis=2))
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# ---------------------------------------------------------------------------
|
| 79 |
+
# Robust rest-gel reference: per-episode fuzzy mode (+ session fallback)
|
| 80 |
+
# ---------------------------------------------------------------------------
|
| 81 |
+
#
|
| 82 |
+
# get_reference(mode="p01") picks the quietest *frame* in an episode. Failure
|
| 83 |
+
# modes (audit over motherboard, 64 episode-sides): it can pick a marginal /
|
| 84 |
+
# contact frame when a clean frame is rare, and if the gel is in contact the
|
| 85 |
+
# episode's ENTIRE length (ep003 left: 121 frames, all contact) there is no
|
| 86 |
+
# clean frame at all -> the "reference" is a contact frame and the difference
|
| 87 |
+
# image inverts.
|
| 88 |
+
#
|
| 89 |
+
# PRIMARY fix -- fuzzy_mode_background(episode_frames): the per-pixel temporal
|
| 90 |
+
# fuzzy MODE is the resting gel even when a pixel is in contact part of the time
|
| 91 |
+
# (robust past the median's 50%-occupancy limit). It is drift-free (this
|
| 92 |
+
# episode's own gel state) and returns a clean reference whenever the gel is at
|
| 93 |
+
# rest for a reasonable fraction of the clip.
|
| 94 |
+
#
|
| 95 |
+
# FALLBACK -- session_reference(all_episodes_of_a_day): for a ~100%-contact clip
|
| 96 |
+
# even its own mode is contaminated. Each React episode is a cut from one
|
| 97 |
+
# continuous per-day recording (episodes.jsonl `date`); pooling the day recovers
|
| 98 |
+
# the rest gel because any pixel is untouched most of the day. Caveat: the gel's
|
| 99 |
+
# fine texture/illumination can DRIFT within a day, so a single session
|
| 100 |
+
# reference differs from a given episode by a diffuse global amount (verified:
|
| 101 |
+
# |episode_clean_frame - session_ref| ~ 7/255 mean, area ~0.37, NO object). Use
|
| 102 |
+
# the session reference ONLY when the episode has no clean frame of its own.
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def fuzzy_mode_background(frames, bin_w: int = 8, max_frames: int = 4000):
|
| 106 |
+
"""Per-pixel temporal fuzzy-mode background (the resting gel).
|
| 107 |
+
|
| 108 |
+
frames: (T, H, W, 3) uint8. Returns (H, W, 3) uint8. The densest value-bin
|
| 109 |
+
(width bin_w ~= +-bin_w/2 tolerance) per pixel-channel, refined by the in-bin
|
| 110 |
+
mean. Robust where a pixel is in contact part of the time (unlike the
|
| 111 |
+
temporal median, which fails past 50% contact occupancy). The mode is
|
| 112 |
+
estimated on up to max_frames evenly-subsampled frames (the rest gel is
|
| 113 |
+
near-static). This is the PREFERRED per-episode reference.
|
| 114 |
+
"""
|
| 115 |
+
import numpy as np
|
| 116 |
+
arr = np.asarray(frames)
|
| 117 |
+
T, H, W, C = arr.shape
|
| 118 |
+
if T > max_frames:
|
| 119 |
+
arr = arr[np.linspace(0, T - 1, max_frames).astype(np.int64)]
|
| 120 |
+
nb = 256 // bin_w
|
| 121 |
+
q = (arr.astype(np.int16) // bin_w).clip(max=nb - 1)
|
| 122 |
+
counts = np.stack([(q == b).sum(0) for b in range(nb)])
|
| 123 |
+
mode_bin = counts.argmax(0)
|
| 124 |
+
in_bin = (q == mode_bin[None]).astype(np.float32)
|
| 125 |
+
ref = (arr.astype(np.float32) * in_bin).sum(0) / np.clip(in_bin.sum(0), 1, None)
|
| 126 |
+
return ref.round().astype(np.uint8)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def session_reference(episode_frames, bin_w: int = 8, max_frames: int = 4000):
|
| 130 |
+
"""FALLBACK rest-gel reference for a whole session (list of episodes' frames).
|
| 131 |
+
|
| 132 |
+
episode_frames: a single (T,H,W,3) array or a list of them (all episodes of
|
| 133 |
+
one date, one sensor). Returns (H, W, 3) uint8. Use only for episodes with no
|
| 134 |
+
clean frame of their own; prefer fuzzy_mode_background otherwise (a session
|
| 135 |
+
reference carries intra-day gel drift).
|
| 136 |
+
"""
|
| 137 |
+
import numpy as np
|
| 138 |
+
if isinstance(episode_frames, (list, tuple)):
|
| 139 |
+
k = max(1, max_frames // max(1, len(episode_frames)))
|
| 140 |
+
pooled = []
|
| 141 |
+
for fr in episode_frames:
|
| 142 |
+
fr = np.asarray(fr)
|
| 143 |
+
idx = np.linspace(0, fr.shape[0] - 1, min(k, fr.shape[0])).astype(np.int64)
|
| 144 |
+
pooled.append(fr[idx])
|
| 145 |
+
frames = np.concatenate(pooled, axis=0)
|
| 146 |
+
else:
|
| 147 |
+
frames = np.asarray(episode_frames)
|
| 148 |
+
return fuzzy_mode_background(frames, bin_w=bin_w, max_frames=max_frames)
|