In [1]:
%pylab inline
from pyannote.core import notebook
In [2]:
from pyannote.core import Timeline
Timeline
instances are used to describe sets of temporal fragments (e.g. of an audio file).
One can optionally store an identifier of the associated multimedia document using uri
keyword argument.
In [3]:
timeline = Timeline(uri='MyAudioFile')
Temporal fragments can be added to the timeline in any order and may be overlapping.
In [4]:
from pyannote.core import Segment
timeline.add(Segment(6, 8))
timeline.add(Segment(0.5, 3))
timeline.add(Segment(8.5, 10))
timeline.add(Segment(1, 4))
timeline.add(Segment(5, 7))
timeline.add(Segment(7, 8))
They are automatically sorted internally (by start time first).
In [5]:
for segment in timeline:
print segment
In [6]:
print "Timeline contains %d segments" % len(timeline)
print "The second segment of timeline is %s" % (str(timeline[1]))
One can visualize Timeline
instances in IPython Notebook.
In [7]:
timeline
Out[7]:
The extent of a timeline is the segment of minimum duration that contains every segments of the timeline.
In [8]:
timeline.extent()
Out[8]:
The coverage of a timeline is the timeline with the minimum number of segments with exactly the same time span as the original timeline.
In [9]:
timeline.coverage()
Out[9]:
One can also retrieve gaps in a timeline.
In [10]:
timeline.gaps()
Out[10]:
In [11]:
timeline.gaps(focus=Segment(0, 10.5))
Out[11]:
Using the crop method, it is possible to select a subpart of timeline.
In [12]:
selection = Segment(3,7)
selection
Out[12]:
In [13]:
timeline
Out[13]:
In intersection mode, segments are cut in half if needed.
In [14]:
timeline.crop(selection, mode='intersection')
Out[14]:
In strict mode, only segments fully included in selection are kept.
In [15]:
timeline.crop(selection, mode='strict')
Out[15]:
In loose mode, any segment with a non-empty intersection is kept unchanged even though it starts before or end after the selection.
In [16]:
timeline.crop(selection, mode='loose')
Out[16]:
The update (in place) and union (copy) methods can be used to combine two timelines.
In [17]:
notebook.crop = Segment(0, 6)
first_timeline = Timeline([Segment(0, 1), Segment(2, 3), Segment(4, 5)])
first_timeline
Out[17]:
In [18]:
second_timeline = Timeline([Segment(1.5, 4.5)])
second_timeline
Out[18]:
In [19]:
new_timeline = first_timeline.union(second_timeline)
new_timeline
Out[19]:
In [20]:
second_timeline.crop(first_timeline)
Out[20]:
co_iter method allow to iterator over pairs of intersecting segments.
In [21]:
for s_first, s_second in first_timeline.co_iter(second_timeline):
print s_first, s_second
You can always try the following...
Who knows? It might give you the information you are looking for!
In [22]:
help(Timeline)