|
| 1 | +# ID correspondence between views (sub-project G) |
| 2 | + |
| 3 | +**Status:** draft 2026-10-09, awaiting review |
| 4 | +**Part of:** [the 3-D roadmap](2026-10-08-3d-roadmap.md). Builds on [the mode switch](2026-10-09-mode-switch-design.md) (sub-project E). |
| 5 | + |
| 6 | +## Why |
| 7 | + |
| 8 | +A 3-D project has two tracked views of the same fish. Fusion (D) cannot combine them until it |
| 9 | +knows which fish in the top view is which fish in the side view, and nothing in the project says |
| 10 | +which session is the top view and which is the side view, or which top session goes with which |
| 11 | +side session. G records all three: the role of each session, the pairs, and the fish mapping. |
| 12 | + |
| 13 | +## Sub-project order |
| 14 | + |
| 15 | +| | Sub-project | Scope | |
| 16 | +|---|---|---| |
| 17 | +| E | Mode switch | Done | |
| 18 | +| F | Panel split (layout i) | One video, two panels, two logical sessions. Creates the `ViewPair` for the two panels itself | |
| 19 | +| **G** | **ID correspondence** (this spec) | View roles, pairing sessions (by regex or by hand), fish mapping, the Views step | |
| 20 | +| D | Fusion | Reads `manifest.view_pairs` and each `fish_map` | |
| 21 | +| B | 3-D metrics | | |
| 22 | + |
| 23 | +G is built and tested against any two sessions, so it does not wait for F. |
| 24 | + |
| 25 | +## Decisions |
| 26 | + |
| 27 | +- **G owns the pair, not only the mapping.** A fish mapping hangs off a pair of sessions, so the |
| 28 | + pair record and the UI to create it belong here. |
| 29 | +- **Roles are per session.** The user says which sessions are the top view and which are the side |
| 30 | + view. |
| 31 | +- **Pairing by regex.** Two regexes, one for top sessions and one for side sessions, each with a |
| 32 | + `key` group. Sessions with an equal `key` become a pair. The regex runs on the session id. The |
| 33 | + user can always correct a pairing by hand. |
| 34 | +- **"Same IDs" tick per pair.** If ticked, fish with the same label are the same fish. If not, the |
| 35 | + user matches them by hand. |
| 36 | +- **Manual matching is visual.** A table with one dropdown per top-view fish, and two track plots |
| 37 | + with the selected fish highlighted. |
| 38 | +- **Stable identities are required.** A pair with an identity-free session can exist but is flagged |
| 39 | + "cannot match fish: this session has no stable identities". Matching such fish by geometry belongs |
| 40 | + to D, if ever. |
| 41 | +- **`mode.id_map` is removed.** It was reserved, unused, and a single project-wide dictionary cannot |
| 42 | + describe many pairs. Manifests that contain it still load (extra keys are ignored). |
| 43 | +- **The step never blocks Next.** Computing is blocked in 3-D anyway. D will start requiring |
| 44 | + complete pairs. |
| 45 | + |
| 46 | +## Design |
| 47 | + |
| 48 | +1. **Data model** (`track2data/core/models.py`). |
| 49 | + - `SessionRef.view_role: Literal["top", "side"] | None = None`. |
| 50 | + - `ViewPair(top_session_id: str, side_session_id: str, same_ids: bool = False, |
| 51 | + fish_map: dict[str, str] = {})`. `fish_map` maps a top-view label to a side-view label; a fish |
| 52 | + not in it is unmatched. |
| 53 | + - `PairingPatterns(top_regex: str = "", side_regex: str = "")`, stored as |
| 54 | + `ProjectMode.pairing`. |
| 55 | + - `ProjectManifest.view_pairs: list[ViewPair] = []`. |
| 56 | + - Labels are `Session.identities_labels`, falling back to the 0-based index as text (the same |
| 57 | + fallback as `MappingRule.individual_match`). |
| 58 | + - Everything is hashed into `project_hash` through `model_dump`. |
| 59 | +2. **Pure logic** (`track2data/views/pairing.py`, no Qt). |
| 60 | + - `pair_by_regex(sessions, top_regex, side_regex) -> PairingResult` with `pairs`, `unpaired_top`, |
| 61 | + `unpaired_side`, `ambiguous_keys` and `errors` (bad regex, missing `key` group). A key shared by |
| 62 | + more than one session on one side is reported as ambiguous and not paired. |
| 63 | + - `identity_map(top_labels, side_labels) -> tuple[dict[str, str], list[str]]` returns the map for |
| 64 | + the "same IDs" tick and the labels that differ. |
| 65 | + - `validate_fish_map(pair, top_labels, side_labels, identity_free) -> list[str]` reports |
| 66 | + duplicate side targets, unknown labels, a difference in fish counts, and the identity-free |
| 67 | + message above. |
| 68 | +3. **Store** (`ui/store/project_store.py`). `update_view_role(session_id, role)`, |
| 69 | + `update_pairing(patterns)`, `apply_regex_pairing()` (replaces the auto-created pairs, keeps |
| 70 | + hand-made ones), `update_view_pair(pair)`, `remove_view_pair(top_id, side_id)`, and a |
| 71 | + `viewsChanged` signal wired into `_on_manifest_changed`. Removing a session removes its pairs |
| 72 | + and clears nothing else. All of this is for 3-D projects only; the calls are rejected in 2-D. |
| 73 | +4. **The Views step** (`ui/views_screen.py`, 3-D only, between Sessions and Calibration). |
| 74 | + - *Roles and patterns.* A role selector per session and the two regex fields. Each field has an |
| 75 | + ⓘ popover in plain language (what `key` is, three worked examples such as `trial01_top`, and |
| 76 | + a live "this catches N sessions" line), plus a preview that lists unpaired sessions. |
| 77 | + - *Pairs.* One row per pair with the "same IDs" tick, a status (matched, needs matching, |
| 78 | + identity-free) and a manual re-pair option. |
| 79 | + - *Matching.* A table with one row per top-view fish and a dropdown for the side-view fish or |
| 80 | + "no match"; beside it two `TrajectoryView` plots with the selected fish highlighted. |
| 81 | +5. **Navigation.** The sidebar and page lists are static today. Views is a new page, shown only in |
| 82 | + 3-D projects; in 2-D it is hidden and skipped by Next and Back. `screen_flow(mode)` returns |
| 83 | + which variant of the Sessions-to-Calibration route to show. `stage_status` marks Views `empty` |
| 84 | + until every session has a role, `warning` while any pair needs matching or is identity-free, |
| 85 | + and `valid` when every pair has a complete map. It never returns `blocked`. |
| 86 | +6. **Hand-off to D.** `manifest.view_pairs[i].fish_map` is the mapping between the two views. |
| 87 | + F creates the `ViewPair` for the two panels of one video. |
| 88 | + |
| 89 | +## Testing |
| 90 | + |
| 91 | +- Model: defaults, round trip, old manifest without the new fields, old manifest with `id_map`, |
| 92 | + hash changes with pairs. |
| 93 | +- `pair_by_regex`: matching keys, unpaired on each side, ambiguous keys, bad regex, missing `key`. |
| 94 | +- `identity_map` and `validate_fish_map`: equal labels, differing labels, duplicate targets, |
| 95 | + unknown labels, count mismatch, identity-free sessions. |
| 96 | +- Store: role and pair updates, removing a session removes its pairs, rejection in 2-D, |
| 97 | + `viewsChanged`, dirty marking. |
| 98 | +- Views step: popover text, live match count, role selector, "same IDs" tick filling the map, |
| 99 | + manual matching, status; navigation shows the step in 3-D and skips it in 2-D. |
| 100 | +- Docs: CHANGELOG, a new decision row in `docs/dev/DECISIONS.md`, the user guide. |
| 101 | + |
| 102 | +## Not in this cycle |
| 103 | + |
| 104 | +Creating the pair for layout (i) (F), fusion (D), 3-D metrics (B), matching identity-free fish by |
| 105 | +geometry, and an automatic guess of the fish mapping when labels differ. |
0 commit comments