10
10
import warnings
11
11
from collections import defaultdict
12
12
from datetime import datetime
13
- from typing import List , Union , Optional , Dict , cast , Iterator
13
+ from typing import List , Union , Optional , Dict , cast , Iterator , Tuple
14
14
15
15
import jsonschema .validators
16
16
@@ -516,6 +516,13 @@ def _is_in_time_between(self, start: Union[int, float], end: Union[int, float],
516
516
s , e = self .get_start (annotation ), self .get_end (annotation )
517
517
return (s < start < e ) or (s > start and e < end ) or (s < end < e )
518
518
519
+ def _handle_time_unit (self , input_unit : str , ann_unit : str ,
520
+ start : int , end : int ) -> Tuple [Union [int , float , str ], Union [int , float , str ]]:
521
+ from mmif .utils .timeunit_helper import convert
522
+ start = convert (start , input_unit , ann_unit , 1 )
523
+ end = convert (end , input_unit , ann_unit , 1 )
524
+ return start , end
525
+
519
526
def get_annotations_between_time (self , start : int , end : int , time_unit : str = "milliseconds" ) -> Iterator [Annotation ]:
520
527
"""
521
528
Version: 1.0
@@ -540,31 +547,26 @@ def get_annotations_between_time(self, start: int, end: int, time_unit: str = "m
540
547
# 2. For each view, extract annotations that satisfy conditions that are TF/TP and fall into time interval
541
548
for view in views :
542
549
# Make sure time unit stay at the same level
543
- unit_of_time = view .metadata .contains .get (AnnotationTypes .TimeFrame )["timeUnit" ]
544
- if time_unit != unit_of_time :
545
- if time_unit == "seconds" :
546
- start *= 1000
547
- end *= 1000
548
- else :
549
- start /= 1000
550
- end /= 1000
550
+ start_time , end_time = self ._handle_time_unit (time_unit , view .metadata .contains .get (AnnotationTypes .TimeFrame )["timeUnit" ],
551
+ start , end )
551
552
tf_anns = view .get_annotations (at_type = AnnotationTypes .TimeFrame )
552
553
al_anns = view .get_annotations (at_type = AnnotationTypes .Alignment )
553
554
554
555
# Select 'TimeFrame' annotations within given time interval
555
556
for tf in tf_anns :
556
- if self ._is_in_time_between (start , end , tf ):
557
+ if self ._is_in_time_between (start_time , end_time , tf ):
557
558
valid_tf_anns .append (tf )
558
559
559
560
# Map 'TimeFrame' annotation to its aligned annotation
560
561
for align in al_anns :
561
562
source_id , target_id = align .get_property ('source' ), align .get_property ('target' )
563
+ to_long_id = lambda x : x if self .id_delimiter in x else f'{ view .id } { self .id_delimiter } { x } '
562
564
try :
563
565
source , target = view .get_annotation_by_id (source_id ), view .get_annotation_by_id (target_id )
564
566
if source in valid_tf_anns :
565
- tf_to_anns [source_id ].append (target )
567
+ tf_to_anns [to_long_id ( source_id ) ].append (target )
566
568
elif target in valid_tf_anns :
567
- tf_to_anns [target_id ].append (source )
569
+ tf_to_anns [to_long_id ( target_id ) ].append (source )
568
570
except KeyError :
569
571
pass
570
572
@@ -573,7 +575,7 @@ def get_annotations_between_time(self, start: int, end: int, time_unit: str = "m
573
575
574
576
# 4. Yield all annotations aligned with sorted 'TimeFrame' annotations
575
577
for tf_ann in sort_tf_anns :
576
- anns = tf_to_anns [tf_ann .get_property ( "id" ) ]
578
+ anns = tf_to_anns [tf_ann .long_id ]
577
579
for ann in anns :
578
580
yield ann
579
581
0 commit comments