# Path: jr/stats/base.py
# def fast_mannwhitneyu(Y, X, use_continuity=True, n_jobs=-1):
#     from mne.parallel import parallel_func
#     X = np.array(X)
#     Y = np.array(Y)
#     nx, ny = len(X), len(Y)
#     dims = X.shape
#     X = np.reshape(X, [nx, -1])
#     Y = np.reshape(Y, [ny, -1])
#     parallel, p_time_gen, n_jobs = parallel_func(_loop_mannwhitneyu, n_jobs)
#     n_chunks = np.min([n_jobs, X.shape[1]])
#     chunks = np.array_split(range(X.shape[1]), n_chunks)
#     out = parallel(p_time_gen(X[..., chunk],
#                               Y[..., chunk], use_continuity=use_continuity)
#                    for chunk in chunks)
#     # Unpack estimators into time slices X folds list of lists.
#     U, p_value = map(list, zip(*out))
#     U = np.hstack(U).reshape(dims[1:])
#     p_value = np.hstack(p_value).reshape(dims[1:])
#     AUC = U / (nx * ny)
#     # XXX FIXME this introduces a bug
#     # # correct directionality of U stats imposed by mannwhitneyu
#     # if nx > ny:
#     #     AUC = 1 - AUC
#     return U, p_value, AUC


from nose.tools import assert_true
from numpy.testing import assert_array_equal
from jr.stats import fast_mannwhitneyu
    from nose.tools import assert_true
    from mne.parallel import parallel_func, check_n_jobs
    from jr.stats import repeated_spearman
    from jr.stats import repeated_corr
    from sklearn.preprocessing import LabelBinarizer
    from sklearn.metrics import accuracy_score
import numpy as np
    return y_true, y_pred, shape


def scorer_spearman(y_true, y_pred, n_jobs=1):
    y_true, y_pred, shape = _check_y(y_true, y_pred)
    rho = _parallel_scorer(y_true, y_pred, repeated_spearman, n_jobs)
    if (len(shape) > 1) and (np.sum(shape[1:]) > 1):
        rho = np.reshape(rho, shape[1:])
    else:
        rho = rho[0]
    return rho


def scorer_corr(y_true, y_pred, n_jobs=1):
    y_true, y_pred, shape = _check_y(y_true, y_pred)
    rho = _parallel_scorer(y_true, y_pred, repeated_corr, n_jobs)
    if (len(shape) > 1) and (np.sum(shape[1:]) > 1):
        rho = np.reshape(rho, shape[1:])
    else:
        rho = rho[0]
    return rho


def scorer_auc(y_true, y_pred, n_jobs=1):
    shape = y_pred.shape
    if len(shape) == 1:
        y_pred = y_pred[:, np.newaxis]
    le = LabelBinarizer()
    y_true = le.fit_transform(y_true)
    assert_array_equal(np.unique(y_true), [0, 1])

# Path: jr/stats/base.py
# def fast_mannwhitneyu(Y, X, use_continuity=True, n_jobs=-1):
#     from mne.parallel import parallel_func
#     X = np.array(X)
#     Y = np.array(Y)
#     nx, ny = len(X), len(Y)
#     dims = X.shape
#     X = np.reshape(X, [nx, -1])
#     Y = np.reshape(Y, [ny, -1])
#     parallel, p_time_gen, n_jobs = parallel_func(_loop_mannwhitneyu, n_jobs)
#     n_chunks = np.min([n_jobs, X.shape[1]])
#     chunks = np.array_split(range(X.shape[1]), n_chunks)
#     out = parallel(p_time_gen(X[..., chunk],
#                               Y[..., chunk], use_continuity=use_continuity)
#                    for chunk in chunks)
#     # Unpack estimators into time slices X folds list of lists.
#     U, p_value = map(list, zip(*out))
#     U = np.hstack(U).reshape(dims[1:])
#     p_value = np.hstack(p_value).reshape(dims[1:])
#     AUC = U / (nx * ny)
#     # XXX FIXME this introduces a bug
#     # # correct directionality of U stats imposed by mannwhitneyu
#     # if nx > ny:
#     #     AUC = 1 - AUC
#     return U, p_value, AUC


# Path: jr/gat/scorers.py
from nose.tools import assert_true
from numpy.testing import assert_array_equal
from jr.stats import fast_mannwhitneyu
    from nose.tools import assert_true
    from mne.parallel import parallel_func, check_n_jobs
    from jr.stats import repeated_spearman
    from jr.stats import repeated_corr
    from sklearn.preprocessing import LabelBinarizer
    from sklearn.metrics import accuracy_score
import numpy as np

    return y_true, y_pred, shape


def scorer_spearman(y_true, y_pred, n_jobs=1):
    y_true, y_pred, shape = _check_y(y_true, y_pred)
    rho = _parallel_scorer(y_true, y_pred, repeated_spearman, n_jobs)
    if (len(shape) > 1) and (np.sum(shape[1:]) > 1):
        rho = np.reshape(rho, shape[1:])
    else:
        rho = rho[0]
    return rho


def scorer_corr(y_true, y_pred, n_jobs=1):
    y_true, y_pred, shape = _check_y(y_true, y_pred)
    rho = _parallel_scorer(y_true, y_pred, repeated_corr, n_jobs)
    if (len(shape) > 1) and (np.sum(shape[1:]) > 1):
        rho = np.reshape(rho, shape[1:])
    else:
        rho = rho[0]
    return rho


def scorer_auc(y_true, y_pred, n_jobs=1):
    shape = y_pred.shape
    if len(shape) == 1:
        y_pred = y_pred[:, np.newaxis]
    le = LabelBinarizer()
    y_true = le.fit_transform(y_true)
    assert_array_equal(np.unique(y_true), [0, 1])

    _, _, auc = fast_mannwhitneyu(y_pred[np.where(y_true == 0)[0], ...],