Skip to content

Commit

Permalink
Remove FrameGroup._frame_idx_registry class attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
roomrys committed Apr 18, 2024
1 parent ca6319a commit 176efb2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
14 changes: 2 additions & 12 deletions sleap/io/cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,10 +1317,6 @@ class FrameGroup:
) # Akin to `LabeledFrame.instances`
_instance_group_name_registry: Set[str] = field(factory=set)

# TODO(LM): Should we move this to an instance attribute of `RecordingSession`?
# Class attribute to keep track of frame indices across all `RecordingSession`s
_frame_idx_registry: Dict[RecordingSession, Set[int]] = {}

# "Hidden" class attribute
_cams_to_include: Optional[List[Camcorder]] = None
_excluded_views: Optional[Tuple[str]] = ()
Expand Down Expand Up @@ -1354,12 +1350,6 @@ def __attrs_post_init__(self):
if self._cams_to_include is not None:
self.cams_to_include = self._cams_to_include

# Add frame index to registry
if self.session not in self._frame_idx_registry:
self._frame_idx_registry[self.session] = set()

self._frame_idx_registry[self.session].add(self.frame_idx)

# Add `FrameGroup` to `RecordingSession`
self.session._frame_group_by_frame_idx[self.frame_idx] = self

Expand Down Expand Up @@ -2016,11 +2006,11 @@ def enforce_frame_idx_unique(
frame_idx: Frame index.
"""

if frame_idx in self._frame_idx_registry.get(session, set()):
if session.frame_groups.get(frame_idx, None) is not None:
# Remove existing `FrameGroup` object from the
# `RecordingSession._frame_group_by_frame_idx`
logger.warning(
f"Frame index {frame_idx} for FrameGroup already exists in this "
"RecordingSession. Overwriting."
)
session._frame_group_by_frame_idx.pop(frame_idx)
session.frame_groups.pop(frame_idx)
28 changes: 12 additions & 16 deletions tests/io/test_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,34 +440,30 @@ def test_frame_group(multiview_min_session_labels: Labels):
session=session, instance_groups=instance_groups
)
assert isinstance(frame_group_1, FrameGroup)
assert session in frame_group_1._frame_idx_registry
assert len(frame_group_1._frame_idx_registry) == 1
assert frame_group_1._frame_idx_registry[session] == {frame_idx_1}
assert frame_idx_1 in session.frame_groups
assert len(session.frame_groups) == 1
assert frame_group_1 == session.frame_groups[frame_idx_1]
assert len(frame_group_1.instance_groups) == 1

# Test `_frame_idx_registry` property
# Test `RecordingSession.frame_groups` property
frame_idx_2 = 1
instance_group = create_instance_group(labels=labels, frame_idx=frame_idx_2)
instance_groups: List[InstanceGroup] = [instance_group]
frame_group_2 = FrameGroup.from_instance_groups(
session=session, instance_groups=instance_groups
)
assert isinstance(frame_group_2, FrameGroup)
assert session in frame_group_2._frame_idx_registry
assert len(frame_group_2._frame_idx_registry) == 1
assert frame_group_2._frame_idx_registry[session] == {frame_idx_1, frame_idx_2}
assert frame_group_1._frame_idx_registry == frame_group_2._frame_idx_registry
assert frame_idx_2 in session.frame_groups
assert len(session.frame_groups) == 2
assert frame_group_2 == session.frame_groups[frame_idx_2]
assert len(frame_group_2.instance_groups) == 1

frame_idx_3 = 2
frame_group_3 = FrameGroup(frame_idx=frame_idx_3, session=session)
assert isinstance(frame_group_3, FrameGroup)
assert session in frame_group_3._frame_idx_registry
assert len(frame_group_3._frame_idx_registry) == 1
assert frame_group_3._frame_idx_registry[session] == {
frame_idx_1,
frame_idx_2,
frame_idx_3,
}
assert frame_group_1._frame_idx_registry == frame_group_3._frame_idx_registry
assert frame_idx_3 in session.frame_groups
assert len(session.frame_groups) == 3
assert frame_group_3 == session.frame_groups[frame_idx_3]
assert len(frame_group_3.instance_groups) == 0

# TODO(LM): Test underlying dictionaries more thoroughly

0 comments on commit 176efb2

Please sign in to comment.