In [1]:
%pylab inline
from pyannote.core import notebook


Populating the interactive namespace from numpy and matplotlib

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


/Users/bredin/Python/py27-pyannote-core.dev/lib/python2.7/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == str('face'):
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)


from 1.000000 to 9.000000

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)


Segment [1.000 --> 9.000] ends at 9 seconds.
Its duration is 8 seconds.
Its middle stands as 5 seconds.

Intersection


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))


Segment [1.000 --> 9.000] intersects other segment [4.000 --> 13.000].
Their intersection is [4.000 --> 9.000].

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.'


Those two segments do not intersect.

In [11]:
notebook.crop = Segment(0, 30)
segment


Out[11]:

In [12]:
other_segment


Out[12]:

Inclusion


In [13]:
other_segment = Segment(5, 6)
if other_segment in segment:
    print '%s in included in %s' % (other_segment, segment)


[5.000 --> 6.000] in included in [1.000 --> 9.000]

In [14]:
t = 23.
if not segment.overlaps(t):
    print '%s does not contain time %f' % (segment, t)


[1.000 --> 9.000] does not contain time 23.000000

Other operations


In [15]:
other_segment = Segment(10, 30)
print 'Union of %s and %s is %s' % (segment, other_segment, segment | other_segment)


Union of [1.000 --> 9.000] and [10.000 --> 30.000] is [1.000 --> 30.000]

In [16]:
other_segment = Segment(14, 15)
print 'Gap between %s and %s is %s' % (segment, other_segment, segment ^ other_segment)


Gap between [1.000 --> 9.000] and [14.000 --> 15.000] is [9.000 --> 14.000]

SlidingWindow (pyannote.core.segment.SlidingWindow)


In [17]:
from pyannote.core import SlidingWindow, Timeline

Iteration


In [18]:
notebook.crop = Segment(0, 10)
window = SlidingWindow(start=0.0, step=1.1, duration=2., end=10.)
Timeline(window)


Out[18]:

Cropping

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]:

Need help?

You can always try the following...
Who knows? It might give you the information you are looking for!


In [23]:
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.