From fc103f6a53f4d0651fa39e6b999bddebbf93ee79 Mon Sep 17 00:00:00 2001 From: FrancescoBonzi Date: Fri, 11 Mar 2022 11:50:33 +0100 Subject: [PATCH 1/3] write annotation to lab file supported --- pyannote/core/annotation.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/pyannote/core/annotation.py b/pyannote/core/annotation.py index b342144..f3b865c 100755 --- a/pyannote/core/annotation.py +++ b/pyannote/core/annotation.py @@ -187,7 +187,7 @@ def __init__(self, uri: Optional[str] = None, modality: Optional[str] = None): # key: label # value: timeline self._labels: Dict[Label, Timeline] = {} - self._labelNeedsUpdate: [Label, bool] = {} + self._labelNeedsUpdate: Dict[Label, bool] = {} # timeline meant to store all annotated segments self._timeline: Timeline = None @@ -397,6 +397,31 @@ def write_rttm(self, file: TextIO): ) file.write(line) + def write_lab(self, file: TextIO): + """Dump annotation to file using LAB format + + Parameters + ---------- + file : file object + + Usage + ----- + >>> with open('file.lab', 'w') as file: + ... annotation.write_lab(file) + """ + + for segment, _, label in self.itertracks(yield_label=True): + if isinstance(label, Text) and " " in label: + msg = ( + f"Space-separated LAB file format does not allow labels " + f'containing spaces (got: "{label}").' + ) + raise ValueError(msg) + line = ( + f"{segment.start:.3f} {segment.start + segment.duration:.3f} {label}\n" + ) + file.write(line) + def crop(self, support: Support, mode: CropMode = "intersection") -> "Annotation": """Crop annotation to new support From 30eec2292be1aa799dfd9a2accadf0a54ee239e7 Mon Sep 17 00:00:00 2001 From: FrancescoBonzi Date: Mon, 6 Feb 2023 12:58:37 +0100 Subject: [PATCH 2/3] fill_gaps added --- pyannote/core/annotation.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pyannote/core/annotation.py b/pyannote/core/annotation.py index f3b865c..47f9ecc 100755 --- a/pyannote/core/annotation.py +++ b/pyannote/core/annotation.py @@ -364,6 +364,36 @@ def __contains__(self, included: Union[Segment, Timeline]): """ return included in self.get_timeline(copy=False) + def fill_gaps(self) -> "Annotation": + """Fill the gaps of the annotation assigning the closest label + + A simple illustration: + + annotation + |------| |------| |------| + | spk0 | | spk1 | | spk0 | + + filled `Annotation` + |----------------------------------------------------| + | spk0 | spk0 | spk1 | spk1 | spk1 | spk0 | spk0 | + + Returns + ------- + filled : Annotation + Filled annotation + + """ + mapping = dict() + filled = self.__class__(uri=self.uri, modality=self.modality) + for segment, _, label in self.itertracks(yield_label=True): + mapping[segment.start] = label + mapping[segment.end] = label + gaps = [[Segment(segment.start, segment.middle), Segment(segment.middle, segment.end)] for segment in self.get_timeline().gaps()] + for first_half, second_half in gaps: + filled[first_half] = mapping[first_half.start] + filled[second_half] = mapping[second_half.end] + return filled.support() + def write_rttm(self, file: TextIO): """Dump annotation to file using RTTM format From 0c29e03131e8f3f19ec8b42e46e788a016ce609a Mon Sep 17 00:00:00 2001 From: FrancescoBonzi Date: Mon, 6 Feb 2023 13:02:50 +0100 Subject: [PATCH 3/3] remove write_lab because it already existed --- pyannote/core/annotation.py | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/pyannote/core/annotation.py b/pyannote/core/annotation.py index 47f9ecc..bf87c3c 100755 --- a/pyannote/core/annotation.py +++ b/pyannote/core/annotation.py @@ -427,31 +427,6 @@ def write_rttm(self, file: TextIO): ) file.write(line) - def write_lab(self, file: TextIO): - """Dump annotation to file using LAB format - - Parameters - ---------- - file : file object - - Usage - ----- - >>> with open('file.lab', 'w') as file: - ... annotation.write_lab(file) - """ - - for segment, _, label in self.itertracks(yield_label=True): - if isinstance(label, Text) and " " in label: - msg = ( - f"Space-separated LAB file format does not allow labels " - f'containing spaces (got: "{label}").' - ) - raise ValueError(msg) - line = ( - f"{segment.start:.3f} {segment.start + segment.duration:.3f} {label}\n" - ) - file.write(line) - def crop(self, support: Support, mode: CropMode = "intersection") -> "Annotation": """Crop annotation to new support