Calculate bootstrap estimates of connectivity.

    To obtain a bootstrap estimate trials are sampled randomly with replacement
    from the data set.

    .. note:: Parameter `var` will be modified by the function. Treat as
    undefined after the function returns.

    Parameters
    ----------
    measures : str or list of str
        Name(s) of the connectivity measure(s) to calculate. See
        :class:`Connectivity` for supported measures.
    data : array, shape (trials, channels, samples)
        Time series data (multiple trials).
    var : VARBase-like object
        Instance of a VAR model.
    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 bootstrap estimates to take.
    num_samples : int, optional
        Number of samples to take for each bootstrap estimates. Defaults to the
        same number of trials as present in the data.
    n_jobs : int, optional
        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
    -------
    measure : array, shape (`repeats`, n_channels, n_channels, nfft)
        Values of the connectivity measure for each bootstrap estimate. 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).

def bootstrap_connectivity(measures, data, var, nfft=512, repeats=100,
                           num_samples=None, n_jobs=1, verbose=0,
                           random_state=None):
    """Calculate bootstrap estimates of connectivity.

    To obtain a bootstrap estimate trials are sampled randomly with replacement
    from the data set.

    .. note:: Parameter `var` will be modified by the function. Treat as
    undefined after the function returns.

    Parameters
    ----------
    measures : str or list of str
        Name(s) of the connectivity measure(s) to calculate. See
        :class:`Connectivity` for supported measures.
    data : array, shape (trials, channels, samples)
        Time series data (multiple trials).
    var : VARBase-like object
        Instance of a VAR model.
    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 bootstrap estimates to take.
    num_samples : int, optional
        Number of samples to take for each bootstrap estimates. Defaults to the
        same number of trials as present in the data.
    n_jobs : int, optional
        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
    -------
    measure : array, shape (`repeats`, n_channels, n_channels, nfft)
        Values of the connectivity measure for each bootstrap estimate. 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).
    """
    rng = check_random_state(random_state)
    data = atleast_3d(data)
    n, m, t = data.shape

    assert(t > 1)

    if num_samples is None:
        num_samples = t

    mask = lambda r: rng.random_integers(0, data.shape[0]-1, num_samples)

    par, func = parallel_loop(_calc_bootstrap, n_jobs=n_jobs, verbose=verbose)
    output = par(func(data[mask(r), :, :], var, measures, nfft)
                 for r in range(repeats))
    return convert_output_(output, measures)