Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make Annotation.__init__ faster #88

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions pyannote/core/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# The MIT License (MIT)

# Copyright (c) 2014-2021 CNRS
# Copyright (c) 2014- CNRS

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -125,7 +125,6 @@
)

import numpy as np
from sortedcontainers import SortedDict

from . import (
PYANNOTE_SEGMENT,
Expand Down Expand Up @@ -175,10 +174,10 @@ def __init__(self, uri: Optional[str] = None, modality: Optional[str] = None):
self._uri: Optional[str] = uri
self.modality: Optional[str] = modality

# sorted dictionary
# dictionary
# keys: annotated segments
# values: {track: label} dictionary
self._tracks: Dict[Segment, Dict[TrackName, Label]] = SortedDict()
self._tracks: Dict[Segment, Dict[TrackName, Label]] = dict()

# dictionary
# key: label
Expand Down Expand Up @@ -257,7 +256,7 @@ def itersegments(self):
--------
:class:`pyannote.core.Segment` describes how segments are sorted.
"""
return iter(self._tracks)
return iter(self.get_timeline())

def itertracks(
self, yield_label: bool = False
Expand All @@ -281,7 +280,8 @@ def itertracks(
... # do something with the track and its label
"""

for segment, tracks in self._tracks.items():
for segment in iter(self.get_timeline()):
tracks = self._tracks[segment]
for track, lbl in sorted(
tracks.items(), key=lambda tl: (str(tl[0]), str(tl[1]))
):
Expand Down Expand Up @@ -504,7 +504,7 @@ def crop(self, support: Support, mode: CropMode = "intersection") -> "Annotation
_tracks[segment] = tracks
_labels.update(tracks.values())

cropped._tracks = SortedDict(_tracks)
cropped._tracks = dict(_tracks)

cropped._labelNeedsUpdate = {label: True for label in _labels}
cropped._labels = {label: None for label in _labels}
Expand All @@ -530,7 +530,7 @@ def crop(self, support: Support, mode: CropMode = "intersection") -> "Annotation
_tracks[segment] = tracks
_labels.update(tracks.values())

cropped._tracks = SortedDict(_tracks)
cropped._tracks = dict(_tracks)

cropped._labelNeedsUpdate = {label: True for label in _labels}
cropped._labels = {label: None for label in _labels}
Expand Down Expand Up @@ -711,7 +711,7 @@ def copy(self) -> "Annotation":
_labels.update(value.values())
_tracks.append((key, dict(value)))

copied._tracks = SortedDict(_tracks)
copied._tracks = dict(_tracks)

copied._labels = {label: None for label in _labels}
copied._labelNeedsUpdate = {label: True for label in _labels}
Expand Down Expand Up @@ -986,7 +986,7 @@ def subset(self, labels: Iterable[Label], invert: bool = False) -> "Annotation":
_tracks[segment] = sub_tracks
_labels.update(sub_tracks.values())

sub._tracks = SortedDict(_tracks)
sub._tracks = dict(_tracks)

sub._labelNeedsUpdate = {label: True for label in _labels}
sub._labels = {label: None for label in _labels}
Expand Down Expand Up @@ -1526,7 +1526,7 @@ def from_records(
for segment, track, label in records:
tracks[segment][track] = label
labels.add(label)
annotation._tracks = SortedDict(tracks)
annotation._tracks = dict(tracks)
annotation._labels = {label: None for label in labels}
annotation._labelNeedsUpdate = {label: True for label in annotation._labels}
annotation._timeline = None
Expand Down