#!/usr/bin/env python
# encoding: utf-8

# The MIT License (MIT)

# Copyright (c) 2016 CNRS

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

# AUTHORS
# Hervé BREDIN - http://herve.niderb.fr


from .base import YaafeMixin
from pyannote.core import SlidingWindowFeature
from pyannote.generators.fragment import SlidingLabeledSegments
from pyannote.generators.batch import FileBasedBatchGenerator
from scipy.stats import zscore


class SpeakerEmbeddingBatchGenerator(YaafeMixin, FileBasedBatchGenerator):

    def __init__(self, feature_extractor, duration=3.2, normalize=False,
                 step=0.8, batch_size=32):

        segment_generator = SlidingLabeledSegments(duration=duration, step=step)
        super(SpeakerEmbeddingBatchGenerator, self).__init__(
            segment_generator, batch_size=batch_size)

        self.feature_extractor = feature_extractor
        self.duration = duration
        self.step = step
        self.normalize = normalize

    def get_shape(self):
        n_samples = self.feature_extractor.sliding_window().samples(self.duration, mode='center')
        dimension = self.feature_extractor.dimension()
        return (n_samples, dimension)

    def signature(self):
        return [
            {'type': 'sequence', 'shape': self.get_shape()},
            {'type': 'label'}
        ]

    def preprocess(self, current_file, identifier=None):
        """Pre-compute file-wise X"""

        return self.yaafe_preprocess(
            current_file, identifier=identifier)

    # defaults to extracting frames centered on segment
    def process_segment(self, segment, signature=None, identifier=None):
        """Extract X subsequence"""

        duration = signature.get('duration', None)

        X = self.preprocessed_['X'][identifier].crop(
            segment, mode='center', fixed=duration)
        if self.normalize:
            X = zscore(X, axis=0)

        return X

@@ -1225,55 +1225,8 @@
 xin%0A
-from pyannote.core import SlidingWindowFeature%0A
 from
@@ -1285,16 +1285,76 @@
 egments%0A
+from pyannote.generators.fragment import RandomSegmentPairs%0A
 from pya
@@ -1411,39 +1411,8 @@
 tor%0A
-from scipy.stats import zscore%0A
 %0A%0Acl
@@ -1671,16 +1671,67 @@
 uration,
+%0A                                                  
  step=st
@@ -2004,24 +2004,24 @@
     def 
-get_shap
+signatur
 e(self):
@@ -2033,123 +2033,215 @@
     
-n_samples = self.feature_extractor.sliding_window().samples(self.duration, mode='center')%0A        dimension = 
+return %5B%0A            %7B'type': 'sequence', 'shape': self.get_shape()%7D,%0A            %7B'type': 'label'%7D%0A        %5D%0A%0A%0Aclass SpeakerPairsBatchGenerator(YaafeMixin, FileBasedBatchGenerator):%0A%0A    def __init__(
 self
-.
+, 
 feat
@@ -2257,201 +2257,238 @@
 ctor
-.dimension()%0A        return (n_samples, dimension)%0A%0A    def signature(self):%0A        return %5B%0A            %7B'type': 'sequence', 'shape': self.get_shape()%7D,%0A            %7B'type': 'label'%7D%0A
+, duration=3.2, normalize=False,%0A                 per_label=40, batch_size=32):%0A%0A        pair_generator = RandomSegmentPairs(duration=duration,%0A                                            per_label=per_label,%0A             
         
 %5D%0A%0A 
@@ -2487,580 +2487,559 @@
     
-%5D%0A%0A
     
-def preprocess(self, current_file, identifier=None):%0A        %22%22%22Pre-compute file-wise X%22%22%22%0A%0A        return self.yaafe_preprocess(%0A            current_file, identifier=identifier)%0A%0A    # defaults to extracting frames centered on segment%0A    def process_segment(self, segment, signature=None, identifier=None):%0A        %22%22%22Extract X subsequence%22%22%22%0A%0A        duration = signature.get('duration', None)%0A%0A        X = self.preprocessed_%5B'X'%5D%5Bidentifier%5D.crop(%0A            segment, mode='center', fixed=duration)%0A        if self.normalize:%0A            X = zscore(X, axis=0)%0A
+                   yield_label=False)%0A        super(SpeakerPairsBatchGenerator, self).__init__(%0A            pair_generator, batch_size=batch_size)%0A%0A        self.feature_extractor = feature_extractor%0A        self.duration = duration%0A        self.normalize = normalize%0A        self.per_label = per_label%0A%0A    def signature(self):%0A        shape = self.get_shape()%0A        return %5B%0A            (%0A                %7B'type': 'sequence', 'shape': shape%7D,%0A                %7B'type': 'sequence', 'shape': shape%7D,%0A            ),%0A            %7B'type': 'boolean'%7D,
 %0A   
@@ -3047,13 +3047,6 @@
     
-return X
+%5D
 %0A

add SpeakerParisBatchGenerator