Skip to content

Commit f6d4f4e

Browse files
authored
Merge pull request #136 from mbel-tech/fatina-del-codice/beautiful-hypatia-wlxfol
docs(3d): design spec for ID correspondence between views (sub-project G)
2 parents 604c604 + 551f6de commit f6d4f4e

24 files changed

Lines changed: 1790 additions & 39 deletions

‎app/main_window.py‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from app.state import ProjectStore
4747
from app.theme import RESOURCES, theme
4848
from track2data import __version__
49+
from track2data.core.models import ProjectMode
4950
from ui.calibration_screen import CalibrationScreen
5051
from ui.export_screen import ExportScreen
5152
from ui.import_screen import ImportScreen
@@ -57,6 +58,8 @@
5758

5859
# Placeholder screen imports — all 10 wizard pages.
5960
from ui.project_screen import ProjectScreen
61+
from ui.store.screen_flow import next_page, prev_page
62+
from ui.views_screen import ViewsScreen
6063
from ui.widgets.weak_slot import weak_slot
6164
from ui.zones_screen import ZonesScreen
6265

@@ -150,6 +153,7 @@ def __init__(self, parent: QWidget | None = None) -> None:
150153
ProcessingScreen(self._store), # 7 — stage 5 (last)
151154
PreviewScreen(self._store), # 8 — stage 6 (first)
152155
ExportScreen(self._store), # 9 — stage 6 (last)
156+
ViewsScreen(self._store), # 10 — stage 1, 3-D only (routed after Sessions)
153157
]
154158
# Direct reference (not a self._stack index lookup) so _action_run()
155159
# has exactly one call path into the real run, shared with this
@@ -493,7 +497,6 @@ def _go_to_page(self, page_index: int) -> None:
493497
self._flush_current_page()
494498
self._stack.setCurrentIndex(page_index)
495499
self._sidebar.sync_to_page(page_index)
496-
self._back_action.setEnabled(page_index > 0)
497500
self._update_next_action()
498501

499502
def _on_project_opened(self) -> None:
@@ -543,28 +546,30 @@ def _update_next_action(self) -> None:
543546
from ui.store.stage_status import next_blocker
544547

545548
page = self._stack.currentIndex()
546-
last = self._stack.count() - 1
547549
statuses = getattr(self, "_page_statuses", None)
548550
reason = next_blocker(statuses, page) if statuses else None
549551
if page == 0:
550552
reason = self._project_screen.pending_mode_message() or reason
551-
self._next_action.setEnabled(page < last and reason is None)
553+
self._next_action.setEnabled(next_page(self._mode(), page) is not None and reason is None)
552554
self._next_action.setToolTip(reason or "Go to the next step")
553555
self._refresh_footer_labels(page)
554556

555557
def _refresh_footer_labels(self, page: int) -> None:
556558
from app.navigation import PAGE_TO_STAGE, STAGES
557559

558-
if page > 0:
559-
self._btn_back.setText(f"← {STAGES[PAGE_TO_STAGE[page - 1]][0]}".replace("&", "&&"))
560+
mode = self._mode()
561+
prev_p, next_p = prev_page(mode, page), next_page(mode, page)
562+
self._back_action.setEnabled(prev_p is not None)
563+
if prev_p is not None:
564+
self._btn_back.setText(f"← {STAGES[PAGE_TO_STAGE[prev_p]][0]}".replace("&", "&&"))
560565
else:
561566
self._btn_back.setText("← Back")
562567
if page == 8:
563568
text, role = "Export dataset →", "accent"
564-
elif page >= self._stack.count() - 1:
569+
elif next_p is None:
565570
text, role = "Done", "primary"
566571
else:
567-
text = f"Next: {STAGES[PAGE_TO_STAGE[page + 1]][0]} →".replace("&", "&&")
572+
text = f"Next: {STAGES[PAGE_TO_STAGE[next_p]][0]} →".replace("&", "&&")
568573
role = "primary"
569574
self._btn_next.setText(text)
570575
if self._btn_next.property("role") != role:
@@ -573,7 +578,7 @@ def _refresh_footer_labels(self, page: int) -> None:
573578
self._btn_next.style().polish(self._btn_next)
574579
ran = self._store.run_results is not None
575580
self._btn_run.setText("⟶ Re-run pipeline" if ran else "⟶ Run pipeline")
576-
self._btn_run.setVisible(page < 8)
581+
self._btn_run.setVisible(page not in (8, 9))
577582

578583
def _flush_current_page(self) -> None:
579584
"""Commit the outgoing screen's debounced edits before leaving it."""
@@ -585,11 +590,20 @@ def _flush_current_page(self) -> None:
585590
if callable(flush):
586591
flush()
587592

593+
def _mode(self) -> ProjectMode:
594+
"""The open project's mode; 2-D while no project is open."""
595+
manifest = self._store.manifest
596+
return manifest.mode if manifest is not None else ProjectMode()
597+
588598
def _go_back(self) -> None:
589-
self._go_to_page(self._stack.currentIndex() - 1)
599+
target = prev_page(self._mode(), self._stack.currentIndex())
600+
if target is not None:
601+
self._go_to_page(target)
590602

591603
def _go_next(self) -> None:
592-
self._go_to_page(self._stack.currentIndex() + 1)
604+
target = next_page(self._mode(), self._stack.currentIndex())
605+
if target is not None:
606+
self._go_to_page(target)
593607

594608
# ── actions ─────────────────────────────────────────────────────────────
595609

‎app/navigation.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@
2828
("Preview & Export", 8),
2929
]
3030

31-
# Maps each page index → its parent stage index (9 stages, 10 pages).
32-
PAGE_TO_STAGE: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 8]
31+
# The 3-D-only Views page: appended after the original 10, so their indices
32+
# are unchanged. It is routed between Sessions and Calibration (screen_flow).
33+
VIEWS_PAGE = 10
34+
35+
# Maps each page index → its parent stage index (9 stages, 11 pages; Views
36+
# sits under Sessions).
37+
PAGE_TO_STAGE: list[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 1]
3338

3439
STATUS_ROLE = Qt.ItemDataRole.UserRole
3540
SUMMARY_ROLE = Qt.ItemDataRole.UserRole + 1

‎docs/3d-movement/2026-10-08-3d-roadmap.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ build cycle.
1111
|---|---|---|
1212
| **C** | **Side-view depth**: a camera-view setting and a continuous vertical-position metric from ordinary 2-D side recordings | nothing unmerged, no 3-D data |
1313
| **E** | **2D/3D project mode**: `ProjectManifest.mode`, Project-screen choice, lock, 3-D compute blocked. Done; see [the spec](2026-10-09-mode-switch-design.md) | nothing |
14+
| G | **ID correspondence**: view roles, pairing top and side sessions (regex or by hand), fish mapping between views, a Views step. In design; see [the spec](2026-10-09-id-correspondence-design.md) | E |
1415
| A | Read 3-D tracker files (Anipose pose-3d CSV, DLC-3D). Metrics run on a chosen plane and z is stored (D-031 as written) | `feat/native-units` merged |
1516
| B | Metrics that use z: 3-D speed and path length, vertical position, 3-D neighbour distance. **Amends D-031** | A for data, C for the vertical-axis setting |
1617
| D | Fuse top and side recordings into one 3-D session | B's data model, frame sync, identity matching across views |
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)