In [1]:
%pylab inline
from pyannote.core import notebook
In [2]:
from pyannote.core import Segment
Segment instances are used to describe temporal fragments (e.g. of an audio file).
In [3]:
# start time in seconds
s = 1.
# end time in seconds
e = 9.
segment = Segment(start=s, end=e)
segment
Out[3]:
Segment instances are nothing more than 2-tuples augmented with several useful methods and properties.
In [4]:
start, end = segment
print 'from %f to %f' % (start, end)
In [5]:
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)
In [6]:
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))
In [7]:
notebook.crop = Segment(0, 15)
segment
Out[7]:
In [8]:
other_segment
Out[8]:
In [9]:
segment & other_segment
Out[9]:
In [10]:
other_segment = Segment(13, 20)
if not (segment & other_segment):
print 'Those two segments do not intersect.'
In [11]:
notebook.crop = Segment(0, 30)
segment
Out[11]:
In [12]:
other_segment
Out[12]:
In [13]:
other_segment = Segment(5, 6)
if other_segment in segment:
print '%s in included in %s' % (other_segment, segment)
In [14]:
t = 23.
if not segment.overlaps(t):
print '%s does not contain time %f' % (segment, t)
In [15]:
other_segment = Segment(10, 30)
print 'Union of %s and %s is %s' % (segment, other_segment, segment | other_segment)
In [16]:
other_segment = Segment(14, 15)
print 'Gap between %s and %s is %s' % (segment, other_segment, segment ^ other_segment)
In [17]:
from pyannote.core import SlidingWindow, Timeline
In [18]:
notebook.crop = Segment(0, 10)
window = SlidingWindow(start=0.0, step=1.1, duration=2., end=10.)
Timeline(window)
Out[18]:
Given an interval, .crop can be used to return every position of the sliding window within this interval.
In [19]:
interval = Segment(3., 7.5)
interval
Out[19]:
strict mode only returns fully contained positions.
In [20]:
indices = window.crop(interval, mode='strict')
Timeline(window[i] for i in indices)
Out[20]:
loose mode returns any intersecting position.
In [21]:
indices = window.crop(interval, mode='loose')
Timeline(window[i] for i in indices)
Out[21]:
center mode centers first and last position on interval boundaries.
In [22]:
indices = window.crop(interval, mode='center')
Timeline(window[i] for i in indices)
Out[22]:
You can always try the following...
Who knows? It might give you the information you are looking for!
In [23]:
help(Segment)