# Path: pambox/utils.py
# def make_same_length(a, b, extend_first=True):
#     """Make two vectors the same length.
# 
#     Parameters
#     ----------
#     a,b : array_like
#         Arrays to make of the same length.
#     extend_first : bool, optional
#         Zero-pad the first array if it is the shortest if `True`. Otherwise,
#         cut array `b` to the length of `a`. (Default value = True)
# 
#     Returns
#     -------
#     tuple of ndarrays
#         Two arrays with the same length along the last dimension.
# 
#     """
#     a = np.asarray(a)
#     b = np.asarray(b)
#     if a.shape[-1] < b.shape[-1]:
#         if extend_first:
#             c = np.zeros_like(b)
#             c[..., :a.shape[-1]] += a
#             return c, b
#         else:
#             return a, b[..., :a.shape[-1]]
#     else:
#         c = np.zeros_like(a)
#         c[..., :b.shape[-1]] += b
#         return a, c
# 
# def setdbspl(x, lvl, ac=False, offset=0.0):
#     """Sets the level of signal in dB SPL, along its last dimension.
# 
#     Parameters
#     ----------
#     x : array_like
#         Signal.
#     lvl : float
#         Level, in dB SPL, at which to set the signal.
#     ac : bool
#         Calculate the AC RMS power of the signal by default (`ac=True`),
#         e.g. the mean is removed. If  `False`, considers the non-RMS power.
#         (Default value = False)
#     offset : float
#         Level, in dB SPL, corresponding to an RMS of 1. By default, an RMS of
#         1 corresponds to 0 dB SPL, i.e. the default is 0.
# 
#     Returns
#     -------
#     ndarray
#         Signal of the same dimension as the original.
#     """
#     x = np.asarray(x)
#     if np.isinf(lvl) and lvl < 0:
#         return np.zeros_like(x)
# 
#     if x.ndim > 1:
#         rms_value = rms(x, ac)[(slice(None),) + (x.ndim - 1) * (np.newaxis, )]
#     else:
#         rms_value = rms(x, ac)
#     return x / rms_value * 10. ** ((lvl - float(offset)) / 20.)
# 
# def int2srt(x, y, srt_at=50.0):
#     """Finds intersection using linear interpolation.
# 
#     This function finds the x values at which a curve intersects with a
#     constant value.
# 
#     Parameters
#     ----------
#     x : array_like
#         "x" values
#     y : array_like
#         "y" values
#     srt_at : float
#          Value of `y` at which the interception is calculated. (Default value
#          = 50.0)
# 
#     Returns
#     -------
#     float
# 
#     """
#     x = np.asarray(x)
#     y = np.asarray(y)
# 
#     if x.shape != y.shape:
#         raise ValueError('Inputs of different lenghts.')
#     srt_at = np.float(srt_at)
#     idx = np.nonzero(np.diff(y >= srt_at))[0]
#     if not idx.any() and y[0] == srt_at:
#         return x[0]
# 
#     if len(idx) >= 1:
#         idx = idx[0]
#         srt = x[idx] + (srt_at - y[idx]) * (x[idx + 1] - x[idx]) \
#             / (y[idx + 1] - y[idx])
#     else:
#         srt = np.nan
#     return float(srt)


from datetime import datetime
from functools import partial
from itertools import product
from IPython import parallel as ipyparallel
from six.moves import zip
from pambox.utils import make_same_length, setdbspl, int2srt
import logging
import os
import os.path
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import six
            import os
            Name of the column to use for the SRT calculation. The default
            value is the 'Intelligibility' column.
        srt_at : float, tuple (optional)
            Value corresponding to the SRT. The default is 50 (%).
        model_srts : dict
            Overrides default ``srt_at`` for particular models. The dictionary
            must be a tuple of the model name and model output: ('Model',
            'Output')
        Returns
        -------
        out : Data frame
            Data frame, with an SRT column.
        """
        snrs = df['SNR'].unique()
        averaging_groups = cls._get_groups(df)
        # Average across sentence
        mean_df = df.groupby(averaging_groups).mean().reset_index()

        # Join both columns as tuple
        mean_df['model_output_pair'] = list(zip(mean_df['Model'],
                                                mean_df['Output']))
        model_output_pairs = mean_df.model_output_pair.unique()

        # Set default criterion for all models
        transformations = {pair: partial(int2srt, snrs, srt_at=srt_at)
                           for pair in model_output_pairs}
        # Override for specified models
        if model_srts is not None:
            for (model, output), criterion in model_srts.iteritems():
                if (model, output) in model_output_pairs:

# Path: pambox/utils.py
# def make_same_length(a, b, extend_first=True):
#     """Make two vectors the same length.
# 
#     Parameters
#     ----------
#     a,b : array_like
#         Arrays to make of the same length.
#     extend_first : bool, optional
#         Zero-pad the first array if it is the shortest if `True`. Otherwise,
#         cut array `b` to the length of `a`. (Default value = True)
# 
#     Returns
#     -------
#     tuple of ndarrays
#         Two arrays with the same length along the last dimension.
# 
#     """
#     a = np.asarray(a)
#     b = np.asarray(b)
#     if a.shape[-1] < b.shape[-1]:
#         if extend_first:
#             c = np.zeros_like(b)
#             c[..., :a.shape[-1]] += a
#             return c, b
#         else:
#             return a, b[..., :a.shape[-1]]
#     else:
#         c = np.zeros_like(a)
#         c[..., :b.shape[-1]] += b
#         return a, c
# 
# def setdbspl(x, lvl, ac=False, offset=0.0):
#     """Sets the level of signal in dB SPL, along its last dimension.
# 
#     Parameters
#     ----------
#     x : array_like
#         Signal.
#     lvl : float
#         Level, in dB SPL, at which to set the signal.
#     ac : bool
#         Calculate the AC RMS power of the signal by default (`ac=True`),
#         e.g. the mean is removed. If  `False`, considers the non-RMS power.
#         (Default value = False)
#     offset : float
#         Level, in dB SPL, corresponding to an RMS of 1. By default, an RMS of
#         1 corresponds to 0 dB SPL, i.e. the default is 0.
# 
#     Returns
#     -------
#     ndarray
#         Signal of the same dimension as the original.
#     """
#     x = np.asarray(x)
#     if np.isinf(lvl) and lvl < 0:
#         return np.zeros_like(x)
# 
#     if x.ndim > 1:
#         rms_value = rms(x, ac)[(slice(None),) + (x.ndim - 1) * (np.newaxis, )]
#     else:
#         rms_value = rms(x, ac)
#     return x / rms_value * 10. ** ((lvl - float(offset)) / 20.)
# 
# def int2srt(x, y, srt_at=50.0):
#     """Finds intersection using linear interpolation.
# 
#     This function finds the x values at which a curve intersects with a
#     constant value.
# 
#     Parameters
#     ----------
#     x : array_like
#         "x" values
#     y : array_like
#         "y" values
#     srt_at : float
#          Value of `y` at which the interception is calculated. (Default value
#          = 50.0)
# 
#     Returns
#     -------
#     float
# 
#     """
#     x = np.asarray(x)
#     y = np.asarray(y)
# 
#     if x.shape != y.shape:
#         raise ValueError('Inputs of different lenghts.')
#     srt_at = np.float(srt_at)
#     idx = np.nonzero(np.diff(y >= srt_at))[0]
#     if not idx.any() and y[0] == srt_at:
#         return x[0]
# 
#     if len(idx) >= 1:
#         idx = idx[0]
#         srt = x[idx] + (srt_at - y[idx]) * (x[idx + 1] - x[idx]) \
#             / (y[idx + 1] - y[idx])
#     else:
#         srt = np.nan
#     return float(srt)


# Path: pambox/speech/experiment.py
from datetime import datetime
from functools import partial
from itertools import product
from IPython import parallel as ipyparallel
from six.moves import zip
from pambox.utils import make_same_length, setdbspl, int2srt
import logging
import os
import os.path
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import six
            import os

            Name of the column to use for the SRT calculation. The default
            value is the 'Intelligibility' column.
        srt_at : float, tuple (optional)
            Value corresponding to the SRT. The default is 50 (%).
        model_srts : dict
            Overrides default ``srt_at`` for particular models. The dictionary
            must be a tuple of the model name and model output: ('Model',
            'Output')
        Returns
        -------
        out : Data frame
            Data frame, with an SRT column.
        """
        snrs = df['SNR'].unique()
        averaging_groups = cls._get_groups(df)
        # Average across sentence
        mean_df = df.groupby(averaging_groups).mean().reset_index()

        # Join both columns as tuple
        mean_df['model_output_pair'] = list(zip(mean_df['Model'],
                                                mean_df['Output']))
        model_output_pairs = mean_df.model_output_pair.unique()

        # Set default criterion for all models
        transformations = {pair: partial(int2srt, snrs, srt_at=srt_at)
                           for pair in model_output_pairs}
        # Override for specified models
        if model_srts is not None:
            for (model, output), criterion in model_srts.iteritems():
                if (model, output) in model_output_pairs:

                    transformations[(model, output)] = partial(int2srt, snrs,