@@ -74,8 +74,6 @@ def _resolve_annotation_items(payload: dict[str, Any]) -> list[dict[str, Any]]:
7474 return []
7575
7676
77-
78-
7977def _resolve_segment_items (payload : dict [str , Any ]) -> list [dict [str , Any ]]:
8078 if payload .get ('schema' , '' ).startswith ('pybehaviorlog-' ):
8179 return [item for item in payload .get ('segments' , []) if isinstance (item , dict )]
@@ -103,13 +101,19 @@ def normalize_session_payload(payload: dict[str, Any]) -> dict[str, Any]:
103101 event_kind = str (item .get ('event_kind' ) or item .get ('type' ) or 'point' ).lower ()
104102 events .append (
105103 {
106- 'time' : _normalize_time (item .get ('time' ) or item .get ('timestamp_seconds' ) or item .get ('start' )),
104+ 'time' : _normalize_time (
105+ item .get ('time' ) or item .get ('timestamp_seconds' ) or item .get ('start' )
106+ ),
107107 'behavior' : str (behavior ),
108108 'event_kind' : event_kind ,
109109 'modifiers' : _string_list (item .get ('modifiers' )),
110110 'subjects' : _string_list (item .get ('subjects' ) or item .get ('subject' )),
111- 'comment' : str (item .get ('comment' ) or item .get ('comment_start' ) or item .get ('image_path' ) or '' ),
112- 'frame_index' : int (item .get ('frame_index' ) or item .get ('frame' ) or 0 ) if str (item .get ('frame_index' ) or item .get ('frame' ) or '' ).strip () else None ,
111+ 'comment' : str (
112+ item .get ('comment' ) or item .get ('comment_start' ) or item .get ('image_path' ) or ''
113+ ),
114+ 'frame_index' : int (item .get ('frame_index' ) or item .get ('frame' ) or 0 )
115+ if str (item .get ('frame_index' ) or item .get ('frame' ) or '' ).strip ()
116+ else None ,
113117 }
114118 )
115119 events .sort (key = lambda item : (item ['time' ], item ['behavior' ], item ['event_kind' ]))
@@ -124,12 +128,14 @@ def normalize_session_payload(payload: dict[str, Any]) -> dict[str, Any]:
124128 annotations .sort (key = lambda item : (item ['time' ], item ['text' ]))
125129 segments = []
126130 for item in _resolve_segment_items (payload ):
127- segments .append ({
128- 'title' : str (item .get ('title' ) or '' ),
129- 'start' : _normalize_time (item .get ('start_seconds' ) or item .get ('start' )),
130- 'end' : _normalize_time (item .get ('end_seconds' ) or item .get ('end' )),
131- 'status' : str (item .get ('status' ) or '' ),
132- })
131+ segments .append (
132+ {
133+ 'title' : str (item .get ('title' ) or '' ),
134+ 'start' : _normalize_time (item .get ('start_seconds' ) or item .get ('start' )),
135+ 'end' : _normalize_time (item .get ('end_seconds' ) or item .get ('end' )),
136+ 'status' : str (item .get ('status' ) or '' ),
137+ }
138+ )
133139 segments .sort (key = lambda item : (item ['start' ], item ['end' ], item ['title' ]))
134140 variables = payload .get ('variables' ) or payload .get ('independent_variables' ) or {}
135141 if not isinstance (variables , dict ):
@@ -139,7 +145,9 @@ def normalize_session_payload(payload: dict[str, Any]) -> dict[str, Any]:
139145 'events' : events ,
140146 'annotations' : annotations ,
141147 'variables' : {str (key ): str (value ) for key , value in sorted (variables .items ())},
142- 'media_paths' : sorted (_string_list (payload .get ('media_paths' ) or payload .get ('image_paths' ))),
148+ 'media_paths' : sorted (
149+ _string_list (payload .get ('media_paths' ) or payload .get ('image_paths' ))
150+ ),
143151 'segments' : segments ,
144152 }
145153
@@ -169,6 +177,7 @@ def compare_session_payloads(expected: dict[str, Any], actual: dict[str, Any]) -
169177
170178def normalize_project_payload (payload : dict [str , Any ]) -> dict [str , Any ]:
171179 """Normalize project-like payloads for BORIS/PyBehaviorLog round-trip comparisons."""
180+
172181 def _item_names (value : Any , * , key : str = 'name' , fallback : str = 'label' ) -> list [str ]:
173182 results = []
174183 if isinstance (value , dict ):
@@ -200,15 +209,21 @@ def _item_names(value: Any, *, key: str = 'name', fallback: str = 'label') -> li
200209 if isinstance (observations , list ):
201210 for observation in observations :
202211 if isinstance (observation , dict ):
203- session_titles .append (str (observation .get ('title' ) or observation .get ('description' ) or '' ))
212+ session_titles .append (
213+ str (observation .get ('title' ) or observation .get ('description' ) or '' )
214+ )
204215 return {
205216 'schema_family' : str (payload .get ('schema' ) or 'unknown' ),
206217 'categories' : _item_names (payload .get ('categories' )),
207218 'behaviors' : _item_names (payload .get ('behaviors' )),
208219 'modifiers' : _item_names (payload .get ('modifiers' )),
209220 'subject_groups' : _item_names (payload .get ('subject_groups' )),
210221 'subjects' : _item_names (payload .get ('subjects' )),
211- 'variables' : _item_names (payload .get ('variables' ) or payload .get ('independent_variables' ), key = 'label' , fallback = 'name' ),
222+ 'variables' : _item_names (
223+ payload .get ('variables' ) or payload .get ('independent_variables' ),
224+ key = 'label' ,
225+ fallback = 'name' ,
226+ ),
212227 'templates' : _item_names (payload .get ('observation_templates' )),
213228 'sessions' : sorted (item for item in session_titles if item ),
214229 }
@@ -219,7 +234,16 @@ def compare_project_payloads(expected: dict[str, Any], actual: dict[str, Any]) -
219234 actual_normalized = normalize_project_payload (actual )
220235 mismatches = [
221236 key
222- for key in ('categories' , 'behaviors' , 'modifiers' , 'subject_groups' , 'subjects' , 'variables' , 'templates' , 'sessions' )
237+ for key in (
238+ 'categories' ,
239+ 'behaviors' ,
240+ 'modifiers' ,
241+ 'subject_groups' ,
242+ 'subjects' ,
243+ 'variables' ,
244+ 'templates' ,
245+ 'sessions' ,
246+ )
223247 if expected_normalized [key ] != actual_normalized [key ]
224248 ]
225249 return {
@@ -230,7 +254,9 @@ def compare_project_payloads(expected: dict[str, Any], actual: dict[str, Any]) -
230254 }
231255
232256
233- def build_roundtrip_report (expected : dict [str , Any ], actual : dict [str , Any ], family : str ) -> dict [str , Any ]:
257+ def build_roundtrip_report (
258+ expected : dict [str , Any ], actual : dict [str , Any ], family : str
259+ ) -> dict [str , Any ]:
234260 """Build a machine-readable round-trip report for CI and fixture certification."""
235261 comparator = compare_project_payloads if family == 'project' else compare_session_payloads
236262 comparison = comparator (expected , actual )
0 commit comments