Skip to content

Commit

Permalink
Merge branch 'pyannote:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
janaab11 authored Sep 25, 2021
2 parents c1208a4 + 30be03c commit d05f9d7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
10 changes: 10 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
Changelog
#########

Version 4.2 (2021-09-24)
~~~~~~~~~~~~~~~~~~~~~~~~

- feat: add Segment.set_precision to automatically round start/end timestamps (@kimdwkimdw)
- feat: add methods {Timeline|Annotation}.{extrude|get_overlap} (@hadware)
- feat: add support for `Segment` in `one_hot_encoding`
- feat: make notebook visualization support an optional installation via the [notebook] extra_requires (@hadware)
- setup: add [doc] and [testing] extra_requires (@hadware)
- fix: several type annotation fixes (@kimdwkimdw, @PaulLerner)

Version 4.1 (2020-05-27)
~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
3 changes: 3 additions & 0 deletions pyannote/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@
from .notebook import notebook
except ImportError as e:
pass

Segment.set_precision()

4 changes: 2 additions & 2 deletions pyannote/core/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

import numpy as np

from pyannote.core.utils.types import CropMode
from pyannote.core.utils.types import Alignment
from .segment import Segment
from .segment import SlidingWindow
from .timeline import Timeline
Expand Down Expand Up @@ -134,7 +134,7 @@ def iterfeatures(
def crop(
self,
focus: Union[Segment, Timeline],
mode: CropMode = "loose",
mode: Alignment = "loose",
fixed: Optional[float] = None,
return_data: bool = True,
) -> Union[np.ndarray, "SlidingWindowFeature"]:
Expand Down
50 changes: 47 additions & 3 deletions pyannote/core/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# The MIT License (MIT)

# Copyright (c) 2014-2019 CNRS
# Copyright (c) 2014-2021 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 @@ -63,6 +63,20 @@
In [11]: segment.overlaps(3) # does segment overlap time t=3?
Use `Segment.set_precision(ndigits)` to automatically round start and end timestamps to `ndigits` precision after the decimal point.
To ensure consistency between `Segment` instances, it is recommended to call this method only once, right after importing `pyannote.core.Segment`.
.. code-block:: ipython
In [12]: Segment(1/1000, 330/1000) == Segment(1/1000, 90/1000+240/1000)
Out[12]: False
In [13]: Segment.set_precision(ndigits=4)
In [14]: Segment(1/1000, 330/1000) == Segment(1/1000, 90/1000+240/1000)
Out[14]: True
See :class:`pyannote.core.Segment` for the complete reference.
"""

Expand All @@ -74,8 +88,6 @@
import numpy as np
from dataclasses import dataclass

# 1 μs (one microsecond)
SEGMENT_PRECISION = 1e-6

# setting 'frozen' to True makes it hashable and immutable
@dataclass(frozen=True, order=True)
Expand Down Expand Up @@ -115,6 +127,32 @@ class Segment:
start: float = 0.0
end: float = 0.0

@staticmethod
def set_precision(ndigits: Optional[int] = None):
"""Automatically round start and end timestamps to `ndigits` precision after the decimal point
To ensure consistency between `Segment` instances, it is recommended to call this method only
once, right after importing `pyannote.core.Segment`.
Usage
-----
>>> from pyannote.core import Segment
>>> Segment.set_precision(2)
>>> Segment(1/3, 2/3)
<Segment(0.33, 0.67)>
"""
global AUTO_ROUND_TIME
global SEGMENT_PRECISION

if ndigits is None:
# backward compatibility
AUTO_ROUND_TIME = False
# 1 μs (one microsecond)
SEGMENT_PRECISION = 1e-6
else:
AUTO_ROUND_TIME = True
SEGMENT_PRECISION = 10 ** (-ndigits)

def __bool__(self):
"""Emptiness
Expand All @@ -130,6 +168,12 @@ def __bool__(self):
"""
return bool((self.end - self.start) > SEGMENT_PRECISION)

def __post_init__(self):
"""Round start and end up to SEGMENT_PRECISION precision (when required)"""
if AUTO_ROUND_TIME:
object.__setattr__(self, 'start', int(self.start / SEGMENT_PRECISION + 0.5) * SEGMENT_PRECISION)
object.__setattr__(self, 'end', int(self.end / SEGMENT_PRECISION + 0.5) * SEGMENT_PRECISION)

@property
def duration(self) -> float:
"""Segment duration (read-only)"""
Expand Down
11 changes: 8 additions & 3 deletions pyannote/core/utils/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

def one_hot_encoding(
annotation: Annotation,
support: Timeline,
support: Union[Segment, Timeline],
window: Union[SlidingWindow, SlidingWindowFeature],
labels: Optional[List[Text]] = None,
mode: Text = "center",
Expand All @@ -51,7 +51,7 @@ def one_hot_encoding(
Parameters
----------
annotation : `pyannote.core.Annotation`
support : `pyannote.core.Timeline`
support : `pyannote.core.Timeline` or `pyannote.core.Segment`
window : `SlidingWindow`
Use this `window`.
labels : list, optional
Expand Down Expand Up @@ -79,7 +79,12 @@ def one_hot_encoding(
)
raise TypeError(msg)

extent = support.extent()
if isinstance(support, Segment):
extent = support
support = Timeline([support])
else:
extent = support.extent()

window = SlidingWindow(
start=extent.start, step=window.step, duration=window.duration
)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_segment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pyannote.core import Segment


def test_creation():

s = 1.
Expand Down Expand Up @@ -43,3 +44,9 @@ def test_other_operation():

other_segment = Segment(14, 15)
assert segment ^ other_segment == Segment(9, 14)


def test_segment_precision_mode():
assert not Segment(90/1000, 90/1000+240/1000) == Segment(90/1000, 330/1000)
Segment.set_precision(4)
assert Segment(90/1000, 90/1000+240/1000) == Segment(90/1000, 330/1000)

0 comments on commit d05f9d7

Please sign in to comment.