Calculate jackknife estimates of connectivity.

    For each jackknife estimate a block of trials is left out. This is repeated
    until each trial was left out exactly once. The number of estimates depends
    on the number of trials and the value of `leaveout`. It is calculated by
    repeats = `n_trials` // `leaveout`.

    .. 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.
    leaveout : int, optional
        Number of trials to leave out in each estimate.
    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).

def jackknife_connectivity(measures, data, var, nfft=512, leaveout=1, n_jobs=1,
                           verbose=0):
    """Calculate jackknife estimates of connectivity.

    For each jackknife estimate a block of trials is left out. This is repeated
    until each trial was left out exactly once. The number of estimates depends
    on the number of trials and the value of `leaveout`. It is calculated by
    repeats = `n_trials` // `leaveout`.

    .. 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.
    leaveout : int, optional
        Number of trials to leave out in each estimate.
    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).
    """
    data = atleast_3d(data)
    t, m, n = data.shape

    assert(t > 1)

    if leaveout < 1:
        leaveout = int(leaveout * t)

    num_blocks = t // leaveout

    mask = lambda block: [i for i in range(t) if i < block*leaveout or
                                                 i >= (block + 1) * leaveout]

    par, func = parallel_loop(_calc_jackknife, n_jobs=n_jobs, verbose=verbose)
    output = par(func(data[mask(b), :, :], var, measures, nfft)
                 for b in range(num_blocks))
    return convert_output_(output, measures)