Skip to content

Commit b1470d2

Browse files
authored
Merge pull request #167 from mbel-tech/fatina-del-codice/sessions-real-span
feat(ui): Sessions screen shows the real span of a session tracked in intervals
2 parents d83acff + ca739d3 commit b1470d2

4 files changed

Lines changed: 113 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
4343

4444
### Changed
4545

46+
- **Sessions screen: a session tracked in separate intervals shows its real length.** The Frames column and the detail panel read "400 of 600" (rows tracked of video frames up to the end of the last interval), and the minutes in the detail panel and the summary line use the real elapsed time rather than the row count, which understated a file with gaps (a file with frames 0-199 and 400-599 showed 13 s instead of 20 s). Sessions without gaps look as before; the reader contract (`video.n_frames` is the row count) is unchanged.
4647
- **Clearer native 3-D wording on the command line.** `track2data validate` and `run` name the project setting (`calibration.water_column`) instead of the Calibration page when IL-15 waits for the water column; the Metrics screen and the run `README.md` keep their text. `track2data list-metrics` ends with a line saying that availability also depends on the project (3-D data, cm scale, pixel frame, number of animals) and pointing to `validate`. `track2data add --yes` prints a `check:` line for every required option the reader proposed rather than you gave (for example `unit=mm` for Anipose). `docs/METRICS_SPEC.md` gains an Availability note on IL-3, IL-9, IL-10, IL-14 and the group metrics for native 3-D projects.
4748

4849
### Fixed

‎tests/test_ui/test_import_screen.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,3 +899,80 @@ def test_a_refused_session_is_explained_in_a_warning(qtbot, tmp_path: Path, monk
899899
monkeypatch.setattr(QMessageBox, "warning", lambda *a, **k: shown.append(a))
900900
store.sessionRefused.emit("n1", "[NATIVE_3D_MIXED] no")
901901
assert shown and shown[0][1] == "Session not added" and "NATIVE_3D_MIXED" in shown[0][2]
902+
903+
904+
# ── the real span of a session tracked in separate intervals ────────────────
905+
906+
907+
def _detail_values(screen) -> dict[str, str]:
908+
grid = screen._detail_grid
909+
out = {}
910+
for r in range(grid.rowCount()):
911+
k, v = grid.itemAtPosition(r, 0), grid.itemAtPosition(r, 1)
912+
if k and v:
913+
out[k.widget().text()] = v.widget().text()
914+
return out
915+
916+
917+
def _table_frames(screen, row: int = 0) -> str:
918+
from ui.import_screen import _COL_FRAMES
919+
920+
return screen._table.item(row, _COL_FRAMES).text()
921+
922+
923+
def test_a_gappy_session_shows_its_real_span_beside_the_tracked_rows(qtbot, tmp_path: Path) -> None:
924+
from ui.import_screen import ImportScreen
925+
926+
store = _make_store(tmp_path)
927+
_add_ref(store, "s1", tmp_path)
928+
# 400 tracked rows in two intervals of a 600-frame recording at 30 fps (20 s)
929+
_cache_facts(store, "s1", n_frames=400, span_frames=600, fps=30.0)
930+
screen = ImportScreen(store)
931+
qtbot.addWidget(screen)
932+
screen._table.selectRow(0)
933+
934+
assert _table_frames(screen) == "400 of 600"
935+
frames = _detail_values(screen)["Frames"]
936+
assert "400" in frames and "600" in frames and "0 min" in frames
937+
assert "20 s" in screen._facts_label.text() or "0 min" in screen._facts_label.text()
938+
939+
940+
def test_a_session_without_gaps_shows_the_row_count_as_before(qtbot, tmp_path: Path) -> None:
941+
from ui.import_screen import ImportScreen
942+
943+
store = _make_store(tmp_path)
944+
_add_ref(store, "s1", tmp_path)
945+
_cache_facts(store, "s1", n_frames=1000)
946+
screen = ImportScreen(store)
947+
qtbot.addWidget(screen)
948+
screen._table.selectRow(0)
949+
950+
assert _table_frames(screen) == "1000"
951+
assert " of " not in _detail_values(screen)["Frames"]
952+
953+
954+
def test_session_facts_span_comes_from_the_last_tracking_interval() -> None:
955+
import numpy as np
956+
957+
from track2data.core.models import Session, VideoInfo
958+
from ui.store.session_facts import SessionFacts
959+
960+
def sess(intervals):
961+
return Session(
962+
velocity_threshold_px_frame=20.0,
963+
session_id="s",
964+
folder=Path("/tmp/s"),
965+
reader="test",
966+
video=VideoInfo(fps=30.0, n_frames=20, width_px=10, height_px=10),
967+
n_animals=1,
968+
trajectory_variant="with_gaps",
969+
has_stable_identities=True,
970+
raw_xy=np.zeros((20, 1, 2)),
971+
tracking_intervals=intervals,
972+
)
973+
974+
gappy = SessionFacts.from_session(sess([(0, 10), (500, 510)]))
975+
assert gappy.n_frames == 20 and gappy.span_frames == 510
976+
assert SessionFacts.from_session(sess(None)).span_frames is None
977+
# one interval from frame 0 covers exactly its rows: no gap to report
978+
assert SessionFacts.from_session(sess([(0, 20)])).span_frames is None

‎ui/import_screen.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,15 @@ def _update_detail(self) -> None:
331331
video_text, _tip = self._video_cell(ref, facts)
332332
rows = [("Reader", self._reader_label(ref.reader) if ref.reader else _PLACEHOLDER)]
333333
if facts is not None:
334-
minutes = facts.n_frames / facts.fps / 60 if facts.fps else 0
334+
minutes = facts.video_frames / facts.fps / 60 if facts.fps else 0
335335
rows += [
336336
("Frame rate", f"{facts.fps:g} fps"),
337-
("Frames", f"{facts.n_frames:,} ({minutes:.0f} min)".replace(",", chr(0x202F))),
337+
(
338+
"Frames",
339+
f"{facts.frames_text} ({minutes:.0f} min)".replace(",", chr(0x202F))
340+
if facts.span_frames is not None
341+
else f"{facts.n_frames:,} ({minutes:.0f} min)".replace(",", chr(0x202F)),
342+
),
338343
("Animals", str(facts.n_animals)),
339344
]
340345
rows.append(("Video", video_text))
@@ -381,7 +386,7 @@ def _update_chips(self, sessions) -> None:
381386
if facts:
382387
fps = sorted({f"{f.fps:g}" for f in facts})
383388
animals = sorted({f.n_animals for f in facts})
384-
minutes = sum(f.n_frames / f.fps for f in facts if f.fps) / 60
389+
minutes = sum(f.video_frames / f.fps for f in facts if f.fps) / 60
385390
self._facts_label.setText(
386391
f"{'/'.join(fps)} fps · {'/'.join(map(str, animals))} animals · {minutes:.0f} min"
387392
)
@@ -670,7 +675,7 @@ def _populate_table(self) -> None:
670675
values = [
671676
saved_reader or facts.reader,
672677
str(facts.fps),
673-
str(facts.n_frames),
678+
facts.frames_text,
674679
str(facts.n_animals),
675680
"Stable" if facts.has_stable_identities else "Unstable",
676681
]

‎ui/store/session_facts.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ def _tracked_coverage(raw_xy) -> float | None:
4343
return None
4444

4545

46+
def _span_frames(session: Session) -> int | None:
47+
"""End of the last tracking interval when it lies beyond the rows read, else None."""
48+
intervals = session.tracking_intervals
49+
if not intervals:
50+
return None
51+
end = int(max(e for _s, e in intervals))
52+
return end if end > int(session.raw_xy.shape[0]) else None
53+
54+
4655
@dataclass(frozen=True)
4756
class SessionFacts:
4857
"""Read-only facts about one session, probed off its trajectory files.
@@ -97,6 +106,22 @@ class SessionFacts:
97106
calibration_ratios: tuple[float, ...] = ()
98107
# False for a native 3-D session: no image frame, width_px/height_px are 0 and mean nothing.
99108
has_pixel_frame: bool = True
109+
# Video frames from 0 to the end of the last tracking interval, when the session was tracked
110+
# in separate intervals and that exceeds the rows read (n_frames); None when the rows cover
111+
# the recording. Real elapsed time is span_frames / fps, not n_frames / fps.
112+
span_frames: int | None = None
113+
114+
@property
115+
def video_frames(self) -> int:
116+
"""Frames of real elapsed time: the span when the session has gaps, else the row count."""
117+
return self.span_frames if self.span_frames is not None else self.n_frames
118+
119+
@property
120+
def frames_text(self) -> str:
121+
"""The row count, as "tracked of span" for a session with gaps."""
122+
if self.span_frames is None:
123+
return str(self.n_frames)
124+
return f"{self.n_frames} of {self.span_frames}"
100125

101126
@property
102127
def pixel_size_known(self) -> bool:
@@ -138,4 +163,5 @@ def from_session(cls, session: Session) -> SessionFacts:
138163
tracked_coverage=_tracked_coverage(session.raw_xy),
139164
calibration_ratios=tuple(calibration_ratios(session.length_calibrations)),
140165
has_pixel_frame=session.has_pixel_frame,
166+
span_frames=_span_frames(session),
141167
)

0 commit comments

Comments
 (0)