new_contents: #!/usr/bin/env python
  # encoding: utf-8
  
  # The MIT License (MIT)
  
  # Copyright (c) 2012-2014 CNRS (Hervé BREDIN - http://herve.niderb.fr)
  
  # Permission is hereby granted, free of charge, to any person obtaining a copy
  # of this software and associated documentation files (the "Software"), to deal
  # in the Software without restriction, including without limitation the rights
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  # copies of the Software, and to permit persons to whom the Software is
  # furnished to do so, subject to the following conditions:
  
  # The above copyright notice and this permission notice shall be included in all
  # copies or substantial portions of the Software.
  
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  # SOFTWARE.
  
  from __future__ import unicode_literals
  
  from base import BaseMetric
  from pyannote.core import Annotation
  
  PTY_NAME = 'segmentation purity'
  CVG_NAME = 'segmentation coverage'
  TOTAL = 'total'
  INTER = 'intersection' 
  
  class SegmentationCoverage(BaseMetric):
      """Segmentation coverage
  
      >>> from pyannote import Timeline, Segment
      >>> from pyannote.metric.segmentation import SegmentationCoverage
      >>> cvg = SegmentationCoverage()
  
      >>> reference = Timeline()
      >>> reference.add(Segment(0, 1))
      >>> reference.add(Segment(1, 2))
      >>> reference.add(Segment(2, 4))
  
      >>> hypothesis = Timeline()
      >>> hypothesis.add(Segment(0, 4))
      >>> cvg(reference, hypothesis)
      1.0
  
      >>> hypothesis = Timeline()
      >>> hypothesis.add(Segment(0, 3))
      >>> hypothesis.add(Segment(3, 4))
      >>> cvg(reference, hypothesis)
      0.75
      """
  
      @classmethod
      def metric_name(cls):
          return CVG_NAME
  
      @classmethod
      def metric_components(cls):
          return [TOTAL, INTER]
  
      def _get_details(self, reference, hypothesis, **kwargs):
  
          if isinstance(reference, Annotation):
              reference = reference.get_timeline()
  
          if isinstance(hypothesis, Annotation):
              hypothesis = hypothesis.get_timeline()
  
          detail = self._init_details()
  
          prev_r = None
          duration = 0.
          intersection = 0.
          for r, h in reference.co_iter(hypothesis):
          
              if r != prev_r:
                  detail[TOTAL] += duration
                  detail[INTER] += intersection
          
                  duration = r.duration
                  intersection = 0.
                  prev_r = r
          
              intersection = max(intersection, (r & h).duration)
  
          detail[TOTAL] += duration
          detail[INTER] += intersection
          
          return detail
  
      def _get_rate(self, detail):
  
          return detail[INTER] / detail[TOTAL]
  
      def _pretty(self, detail):
          string = ""
          string += "  - duration: %.2f seconds\n" % (detail[TOTAL])
          string += "  - correct: %.2f seconds\n" % (detail[INTER])
          string += "  - %s: %.2f %%\n" % (self.name, 100*detail[self.name])
          return string
  
  class SegmentationPurity(SegmentationCoverage):
      """Segmentation purity
  
      >>> from pyannote import Timeline, Segment
      >>> from pyannote.metric.segmentation import SegmentationPurity
      >>> pty = SegmentationPurity()
  
      >>> reference = Timeline()
      >>> reference.add(Segment(0, 1))
      >>> reference.add(Segment(1, 2))
      >>> reference.add(Segment(2, 4))
  
      >>> hypothesis = Timeline()
      >>> hypothesis.add(Segment(0, 1))
      >>> hypothesis.add(Segment(1, 2))
      >>> hypothesis.add(Segment(2, 3))
      >>> hypothesis.add(Segment(3, 4))
      >>> pty(reference, hypothesis)
      1.0
  
      >>> hypothesis = Timeline()
      >>> hypothesis.add(Segment(0, 4))
      >>> pty(reference, hypothesis)
      0.5
  
      """
  
      @classmethod
      def metric_name(cls):
          return PTY_NAME
  
      def _get_details(self, reference, hypothesis, **kwargs):
          return super(SegmentationPurity, self)._get_details(
              hypothesis, reference, **kwargs
          )
  
  
  if __name__ == "__main__":
      import doctest
      doctest.testmod()
  
subject: add segmentation metrics