@@ -267,40 +267,34 @@ def __call__(self, scores: SlidingWindowFeature) -> Annotation:
267267
268268 num_frames , num_classes = scores .data .shape
269269 frames = scores .sliding_window
270- timestamps = [frames [i ].middle for i in range (num_frames )]
270+ timestamps = np . array ( [frames [i ].middle for i in range (num_frames )])
271271
272272 # annotation meant to store 'active' regions
273273 active = Annotation ()
274274
275275 for k , k_scores in enumerate (scores .data .T ):
276-
277276 label = k if scores .labels is None else scores .labels [k ]
278-
279- # initial state
280- start = timestamps [0 ]
281- is_active = k_scores [0 ] > self .onset
282-
283- for t , y in zip (timestamps [1 :], k_scores [1 :]):
284-
285- # currently active
286- if is_active :
287- # switching from active to inactive
288- if y < self .offset :
289- region = Segment (start - self .pad_onset , t + self .pad_offset )
290- active [region , k ] = label
291- start = t
292- is_active = False
293-
294- # currently inactive
295- else :
296- # switching from inactive to active
297- if y > self .onset :
298- start = t
299- is_active = True
300-
301- # if active at the end, add final region
302- if is_active :
303- region = Segment (start - self .pad_onset , t + self .pad_offset )
277+
278+ # Detect transitions
279+ is_active = k_scores > self .onset
280+ transitions = np .diff (is_active .astype (int ))
281+ starts = np .where (transitions == 1 )[0 ] + 1
282+ ends = np .where (transitions == - 1 )[0 ] + 1
283+
284+ # If the first frame is active, add it as a start
285+ if is_active [0 ]:
286+ starts = np .insert (starts , 0 , 0 )
287+
288+ # If the last frame is active, add it as an end
289+ if is_active [- 1 ]:
290+ ends = np .append (ends , len (is_active ))
291+
292+ # Create segments
293+ for start , end in zip (starts , ends ):
294+ region = Segment (
295+ timestamps [start ] - self .pad_onset ,
296+ timestamps [end ] + self .pad_offset ,
297+ )
304298 active [region , k ] = label
305299
306300 # because of padding, some active regions might be overlapping: merge them.
0 commit comments