# Path: scot/connectivity.py
# def connectivity(measure_names, b, c=None, nfft=512):
#     """Calculate connectivity measures.
# 
#     Parameters
#     ----------
#     measure_names : str or list of str
#         Name(s) of the connectivity measure(s) to calculate. See
#         :class:`Connectivity` for supported measures.
#     b : array, shape (n_channels, n_channels * model_order)
#         VAR model coefficients. See :ref:`var-model-coefficients` for details
#         about the arrangement of coefficients.
#     c : array, shape (n_channels, n_channels), optional
#         Covariance matrix of the driving noise process. Identity matrix is used
#         if set to None (default).
#     nfft : int, optional
#         Number of frequency bins to calculate. Note that these points cover the
#         range between 0 and half the sampling rate.
# 
#     Returns
#     -------
#     result : array, shape (n_channels, n_channels, `nfft`)
#         An array of shape (m, m, nfft) is returned if measures is a string. If
#         measures is a list of strings, a dictionary is returned, where each key
#         is the name of the measure, and the corresponding values are arrays of
#         shape (m, m, nfft).
# 
#     Notes
#     -----
#     When using this function, it is more efficient to get several measures at
#     once than calling the function multiple times.
# 
#     Examples
#     --------
#     >>> c = connectivity(['DTF', 'PDC'], [[0.3, 0.6], [0.0, 0.9]])
#     """
#     con = Connectivity(b, c, nfft)
#     try:
#         return getattr(con, measure_names)()
#     except TypeError:
#         return dict((m, getattr(con, m)()) for m in measure_names)


import numpy as np
from .datatools import randomize_phase, atleast_3d
from .connectivity import connectivity
from .utils import cartesian, check_random_state
from .parallel import parallel_loop
    nfft : int, optional
        Number of frequency bins to calculate. Note that these points cover the
        range between 0 and half the sampling rate.
    repeats : int, optional
        Number of surrogate samples to take.
    n_jobs : int | None, optional
        Number of jobs to run in parallel. If set to None, joblib is not used
        at all. See `joblib.Parallel` for details.
    verbose : int, optional
        Verbosity level passed to joblib.

    Returns
    -------
    result : array, shape (`repeats`, n_channels, n_channels, nfft)
        Values of the connectivity measure for each surrogate. If
        `measure_names` is a list of strings a dictionary is returned, where
        each key is the name of the measure, and the corresponding values are
        arrays of shape (`repeats`, n_channels, n_channels, nfft).

    .. [1] J. Theiler et al. Testing for nonlinearity in time series: the
           method of surrogate data. Physica D, 58: 77-94, 1992.
    """
    par, func = parallel_loop(_calc_surrogate, n_jobs=n_jobs, verbose=verbose)
    output = par(func(randomize_phase(data, random_state=random_state), var,
                      measure_names, nfft) for _ in range(repeats))
    return convert_output_(output, measure_names)


def _calc_surrogate(data, var, measure_names, nfft):
    var.fit(data)

# Path: scot/connectivity.py
# def connectivity(measure_names, b, c=None, nfft=512):
#     """Calculate connectivity measures.
# 
#     Parameters
#     ----------
#     measure_names : str or list of str
#         Name(s) of the connectivity measure(s) to calculate. See
#         :class:`Connectivity` for supported measures.
#     b : array, shape (n_channels, n_channels * model_order)
#         VAR model coefficients. See :ref:`var-model-coefficients` for details
#         about the arrangement of coefficients.
#     c : array, shape (n_channels, n_channels), optional
#         Covariance matrix of the driving noise process. Identity matrix is used
#         if set to None (default).
#     nfft : int, optional
#         Number of frequency bins to calculate. Note that these points cover the
#         range between 0 and half the sampling rate.
# 
#     Returns
#     -------
#     result : array, shape (n_channels, n_channels, `nfft`)
#         An array of shape (m, m, nfft) is returned if measures is a string. If
#         measures is a list of strings, a dictionary is returned, where each key
#         is the name of the measure, and the corresponding values are arrays of
#         shape (m, m, nfft).
# 
#     Notes
#     -----
#     When using this function, it is more efficient to get several measures at
#     once than calling the function multiple times.
# 
#     Examples
#     --------
#     >>> c = connectivity(['DTF', 'PDC'], [[0.3, 0.6], [0.0, 0.9]])
#     """
#     con = Connectivity(b, c, nfft)
#     try:
#         return getattr(con, measure_names)()
#     except TypeError:
#         return dict((m, getattr(con, m)()) for m in measure_names)


# Path: scot/connectivity_statistics.py
import numpy as np
from .datatools import randomize_phase, atleast_3d
from .connectivity import connectivity
from .utils import cartesian, check_random_state
from .parallel import parallel_loop

    nfft : int, optional
        Number of frequency bins to calculate. Note that these points cover the
        range between 0 and half the sampling rate.
    repeats : int, optional
        Number of surrogate samples to take.
    n_jobs : int | None, optional
        Number of jobs to run in parallel. If set to None, joblib is not used
        at all. See `joblib.Parallel` for details.
    verbose : int, optional
        Verbosity level passed to joblib.

    Returns
    -------
    result : array, shape (`repeats`, n_channels, n_channels, nfft)
        Values of the connectivity measure for each surrogate. If
        `measure_names` is a list of strings a dictionary is returned, where
        each key is the name of the measure, and the corresponding values are
        arrays of shape (`repeats`, n_channels, n_channels, nfft).

    .. [1] J. Theiler et al. Testing for nonlinearity in time series: the
           method of surrogate data. Physica D, 58: 77-94, 1992.
    """
    par, func = parallel_loop(_calc_surrogate, n_jobs=n_jobs, verbose=verbose)
    output = par(func(randomize_phase(data, random_state=random_state), var,
                      measure_names, nfft) for _ in range(repeats))
    return convert_output_(output, measure_names)


def _calc_surrogate(data, var, measure_names, nfft):
    var.fit(data)

    return connectivity(measure_names, var.coef, var.rescov, nfft)