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