%pylab inline
from pyannote.core import notebook
Populating the interactive namespace from numpy and matplotlib
pyannote.core.segment.Segment
)¶from pyannote.core import Segment
Segment
instances are used to describe temporal fragments (e.g. of an audio file).
# start time in seconds
s = 1.
# end time in seconds
e = 9.
segment = Segment(start=s, end=e)
segment
Segment
instances are nothing more than 2-tuples augmented with several useful methods and properties.
start, end = segment
print('from {} to {}'.format(start, end))
from 1.0 to 9.0
print('Segment %s ends at %g seconds.' % (segment, segment.end))
print('Its duration is %g seconds.' % (segment.duration))
print('Its middle stands as %g seconds.' % (segment.middle))
Segment [ 00:00:01.000 --> 00:00:09.000] ends at 9 seconds. Its duration is 8 seconds. Its middle stands as 5 seconds.
other_segment = Segment(4, 13)
if segment.intersects(other_segment):
print('Segment %s intersects other segment %s.' % (segment, other_segment))
intersection = segment & other_segment
print('Their intersection is %s.' % (str(intersection)))
Segment [ 00:00:01.000 --> 00:00:09.000] intersects other segment [ 00:00:04.000 --> 00:00:13.000]. Their intersection is [ 00:00:04.000 --> 00:00:09.000].
notebook.crop = Segment(0, 15)
segment
other_segment
segment & other_segment
other_segment = Segment(13, 20)
if not (segment & other_segment):
print('Those two segments do not intersect.')
Those two segments do not intersect.
notebook.crop = Segment(0, 30)
segment
other_segment
other_segment = Segment(5, 6)
if other_segment in segment:
print('%s in included in %s' % (other_segment, segment))
[ 00:00:05.000 --> 00:00:06.000] in included in [ 00:00:01.000 --> 00:00:09.000]
t = 23.
if not segment.overlaps(t):
print('%s does not contain time %f' % (segment, t))
[ 00:00:01.000 --> 00:00:09.000] does not contain time 23.000000
other_segment = Segment(10, 30)
print('Union of %s and %s is %s' % (segment, other_segment, segment | other_segment))
Union of [ 00:00:01.000 --> 00:00:09.000] and [ 00:00:10.000 --> 00:00:30.000] is [ 00:00:01.000 --> 00:00:30.000]
other_segment = Segment(14, 15)
print('Gap between %s and %s is %s' % (segment, other_segment, segment ^ other_segment))
Gap between [ 00:00:01.000 --> 00:00:09.000] and [ 00:00:14.000 --> 00:00:15.000] is [ 00:00:09.000 --> 00:00:14.000]
pyannote.core.segment.SlidingWindow
)¶from pyannote.core import SlidingWindow, Timeline
notebook.crop = Segment(0, 10)
window = SlidingWindow(start=0.0, step=1.1, duration=2., end=10.)
Timeline(window)
Given an interval, .crop
can be used to return every position of the sliding window within this interval.
interval = Segment(3., 7.5)
interval
strict
mode only returns fully contained positions.
indices = window.crop(interval, mode='strict')
Timeline(window[i] for i in indices)
loose
mode returns any intersecting position.
indices = window.crop(interval, mode='loose')
Timeline(window[i] for i in indices)
center
mode centers first and last position on interval boundaries.
indices = window.crop(interval, mode='center')
Timeline(window[i] for i in indices)
You can always try the following...
Who knows? It might give you the information you are looking for!
help(Segment)
Help on class Segment in module pyannote.core.segment: class Segment(Segment) | Temporal interval defined by its `start` and `end` times. | | Multiple segment operators are available -- including intersection (&), | inclusion (in), emptiness test, start/end time shifting (+, -, >>, <<). | They are illustrated in **Examples** section. | | Comparison of two segments is also available (==, !=, <, <=, >, >=). | Two segments are equal iff they have identical start and end times. | Segment S is smaller than segment T iff S.start < T.start or if they have | the same start time and S.end < T.start. | | Parameters | ---------- | start, end : float | `start` and `end` times, in seconds. | | Returns | ------- | segment : Segment | New segment with `start` and `end` times. | | Examples | -------- | Create a new temporal interval between 00:13.000 and 00:37.000. | | >>> segment = Segment(start=13., end=37) | >>> print segment | [13.000 --> 37.000] | | Inclusion, intersection, union & gap | | >>> s1 = Segment(1, 2) | >>> s2 = Segment(0, 3) | >>> if s1 in s2: | ... print "Segment %s is included in segment %s." % (s1, s2) | Segment [1.000 --> 2.000] is included in segment [0.000 --> 3.000]. | >>> s3 = Segment(2, 5) | >>> print s1 & s3 | ∅ | >>> print s2 & s3 | [2.000 --> 3.000] | >>> print s2 | s3 | [0.000 --> 5.000] | >>> print s1 ^ Segment(5, 7) | [2.000 --> 5.000] | | Test whether segment is empty or not. | | >>> if not Segment(10, 10): | ... print "Segment is empty." | Segment is empty. | | Comparison | | >>> s1 = Segment(1, 3) | >>> s2 = Segment(1, 3) | >>> s3 = Segment(2, 6) | >>> s4 = Segment(1, 2) | >>> for s in sorted([s1, s2, s3, s4]): | ... print s | [1.000 --> 2.000] | [1.000 --> 3.000] | [1.000 --> 3.000] | [2.000 --> 6.000] | | Method resolution order: | Segment | Segment | __builtin__.tuple | __builtin__.object | | Methods defined here: | | __and__(self, other) | Use the expression 'segment & other' | | Returns | ------- | segment : Segment | Intersection of the two segments | | __bool__(self) | Use the expression 'if segment' | | Returns | ------- | valid : bool | False is segment is empty, True otherwise. | | __contains__(self, other) | Use the expression 'other in segment' | | Returns | ------- | contains : bool | True if other segment is fully included, False otherwise | | __iter__(self) | Makes sure tuple(segment) is a tuple of float | | __nonzero__(self) | | __or__(self, other) | Use the expression 'segment | other' | | Returns | ------- | segment : Segment | Shortest segment that contains both segments | | __repr__(self) | | __str__(self) | Use the expression str(segment) | | __xor__(self, other) | Use the expression 'segment ^ other' | | Returns | ------- | segment : Segment | Gap between the two segments | | _get_duration(self) | | _get_middle(self) | | _pretty(self, seconds) | | _repr_png_(self) | | copy(self) | Duplicate segment. | | for_json(self) | | intersects(self, other) | Check whether two segments intersect each other | | Parameters | ---------- | other : Segment | Other segment | | Returns | ------- | intersects : bool | True if segments intersect, False otherwise | | overlaps(self, t) | | pretty(self) | Human-readable representation of segments | | ---------------------------------------------------------------------- | Class methods defined here: | | from_json(cls, data) from __builtin__.type | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(cls, start=0.0, end=0.0) | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | duration | | middle | | ---------------------------------------------------------------------- | Methods inherited from Segment: | | __getnewargs__(self) | Return self as a plain tuple. Used by copy and pickle. | | __getstate__(self) | Exclude the OrderedDict from pickling | | _asdict(self) | Return a new OrderedDict which maps field names to their values | | _replace(_self, **kwds) | Return a new Segment object replacing specified fields with new values | | ---------------------------------------------------------------------- | Class methods inherited from Segment: | | _make(cls, iterable, new=<built-in method __new__ of type object>, len=<built-in function len>) from __builtin__.type | Make a new Segment object from a sequence or iterable | | ---------------------------------------------------------------------- | Data descriptors inherited from Segment: | | end | Alias for field number 1 | | start | Alias for field number 0 | | ---------------------------------------------------------------------- | Data and other attributes inherited from Segment: | | _fields = ('start', 'end') | | ---------------------------------------------------------------------- | Methods inherited from __builtin__.tuple: | | __add__(...) | x.__add__(y) <==> x+y | | __eq__(...) | x.__eq__(y) <==> x==y | | __ge__(...) | x.__ge__(y) <==> x>=y | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] | | Use of negative indices is not supported. | | __gt__(...) | x.__gt__(y) <==> x>y | | __hash__(...) | x.__hash__() <==> hash(x) | | __le__(...) | x.__le__(y) <==> x<=y | | __len__(...) | x.__len__() <==> len(x) | | __lt__(...) | x.__lt__(y) <==> x<y | | __mul__(...) | x.__mul__(n) <==> x*n | | __ne__(...) | x.__ne__(y) <==> x!=y | | __rmul__(...) | x.__rmul__(n) <==> n*x | | count(...) | T.count(value) -> integer -- return number of occurrences of value | | index(...) | T.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present.