From f97e19d8ad48a4901fcd6f613f88c9d1996d0daa Mon Sep 17 00:00:00 2001 From: roomrys <38435167+roomrys@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:40:54 -0700 Subject: [PATCH] Ensure cameras follow same order when (de)serializing --- sleap/io/cameras.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sleap/io/cameras.py b/sleap/io/cameras.py index 18b530cd1..28b7a9afc 100644 --- a/sleap/io/cameras.py +++ b/sleap/io/cameras.py @@ -1581,10 +1581,13 @@ def to_session_dict( # Unstructure `CameraCluster` and `metadata` calibration_dict = self.camera_cluster.to_calibration_dict() + # Sort `Camcorder`s following same schema as when deserializing + self.camera_cluster.cameras.sort(key=lambda x: x.name) + # Store camcorder-to-video indices map where key is camcorder index # and value is video index from `Labels.videos` camcorder_to_video_idx_map = {} - for cam_idx, camcorder in enumerate(self.camera_cluster): + for cam_idx, camcorder in enumerate(self.camera_cluster.cameras): # Skip if Camcorder is not linked to any Video if camcorder not in self._video_by_camcorder: continue @@ -1644,6 +1647,9 @@ def from_session_dict( calibration_dict ) + # Ensure `Camcorder`s are in the same order as they were serialized + session.camera_cluster.cameras.sort(key=lambda x: x.name) + # Retrieve all `Camcorder` and `Video` objects, then add to `RecordingSession` camcorder_to_video_idx_map = session_dict["camcorder_to_video_idx_map"] for cam_idx, video_idx in camcorder_to_video_idx_map.items():