# Path: jr/utils.py
# def tile_memory_free(y, shape):
#     """
#     XXX Will be deprecated
#     Tile vector along multiple dimension without allocating new memory.
# 
#     Parameters
#     ----------
#      y : np.array, shape (n,)
#         data
#     shape : np.array, shape (m),
#     Returns
#     -------
#     Y : np.array, shape (n, *shape)
#     """
#     import warnings
#     warnings.warn('Will be deprecated. Use np.newaxis instead')
#     for dim in range(len(shape)):
#         y = y[..., np.newaxis]
#     return y
# 
# def pairwise(X, y, func, n_jobs=-1):
#     """Applies pairwise operations on two matrices using multicore:
#     function(X[:, jj, kk, ...], y[:, jj, kk, ...])
# 
#     Parameters
#     ----------
#         X : np.ndarray, shape(n, ...)
#         y : np.array, shape(n, ...) | shape(n,)
#             If shape == X.shape:
#                 parallel(X[:, chunk], y[:, chunk ] for chunk in n_chunks)
#             If shape == X.shape[0]:
#                 parallel(X[:, chunk], y for chunk in n_chunks)
#         func : function
#         n_jobs : int, optional
#             Number of parallel cpu.
#     Returns
#     -------
#         out : np.array, shape(func(X, y))
#     """
#     import numpy as np
#     from mne.parallel import parallel_func
#     dims = X.shape
#     if y.shape[0] != dims[0]:
#         raise ValueError('X and y must have identical shapes')
# 
#     X.resize([dims[0], np.prod(dims[1:])])
#     if y.ndim > 1:
#         Y = np.reshape(y, [dims[0], np.prod(dims[1:])])
# 
#     parallel, pfunc, n_jobs = parallel_func(func, n_jobs)
# 
#     n_cols = X.shape[1]
#     n_chunks = min(n_cols, n_jobs)
#     chunks = np.array_split(range(n_cols), n_chunks)
#     if y.ndim == 1:
#         out = parallel(pfunc(X[:, chunk], y) for chunk in chunks)
#     else:
#         out = parallel(pfunc(X[:, chunk], Y[:, chunk]) for chunk in chunks)
# 
#     # size back in case higher dependencies
#     X.resize(dims)
# 
#     # unpack
#     if isinstance(out[0], tuple):
#         return [np.reshape(out_, dims[1:]) for out_ in zip(*out)]
#     else:
#         return np.reshape(np.hstack(out), dims[1:])


import numpy as np
from ..utils import tile_memory_free, pairwise
    from nose.tools import assert_equal
    from nose.tools import assert_equal, assert_raises


def test_tile_memory_free():
    y = np.arange(100)
    Y = tile_memory_free(y, 33)
    assert_equal(y.shape[0], Y.shape[0])
    np.testing.assert_array_equal(y, Y[:, 0], Y[:, -1])


def _dummy_pairwise_function_1(x, y):
    return x[0, :]


def _dummy_pairwise_function_2(x, y):
    return x[0, :], 0. * x[0, :]


def test_pairwise():
    n_obs = 20
    n_dims1 = 5
    n_dims2 = 10
    y = np.linspace(0, 1, n_obs)
    X = np.zeros((n_obs, n_dims1, n_dims2))
    for dim1 in range(n_dims1):
        for dim2 in range(n_dims2):
            X[:, dim1, dim2] = dim1 + 10*dim2

    # test size

# Path: jr/utils.py
# def tile_memory_free(y, shape):
#     """
#     XXX Will be deprecated
#     Tile vector along multiple dimension without allocating new memory.
# 
#     Parameters
#     ----------
#      y : np.array, shape (n,)
#         data
#     shape : np.array, shape (m),
#     Returns
#     -------
#     Y : np.array, shape (n, *shape)
#     """
#     import warnings
#     warnings.warn('Will be deprecated. Use np.newaxis instead')
#     for dim in range(len(shape)):
#         y = y[..., np.newaxis]
#     return y
# 
# def pairwise(X, y, func, n_jobs=-1):
#     """Applies pairwise operations on two matrices using multicore:
#     function(X[:, jj, kk, ...], y[:, jj, kk, ...])
# 
#     Parameters
#     ----------
#         X : np.ndarray, shape(n, ...)
#         y : np.array, shape(n, ...) | shape(n,)
#             If shape == X.shape:
#                 parallel(X[:, chunk], y[:, chunk ] for chunk in n_chunks)
#             If shape == X.shape[0]:
#                 parallel(X[:, chunk], y for chunk in n_chunks)
#         func : function
#         n_jobs : int, optional
#             Number of parallel cpu.
#     Returns
#     -------
#         out : np.array, shape(func(X, y))
#     """
#     import numpy as np
#     from mne.parallel import parallel_func
#     dims = X.shape
#     if y.shape[0] != dims[0]:
#         raise ValueError('X and y must have identical shapes')
# 
#     X.resize([dims[0], np.prod(dims[1:])])
#     if y.ndim > 1:
#         Y = np.reshape(y, [dims[0], np.prod(dims[1:])])
# 
#     parallel, pfunc, n_jobs = parallel_func(func, n_jobs)
# 
#     n_cols = X.shape[1]
#     n_chunks = min(n_cols, n_jobs)
#     chunks = np.array_split(range(n_cols), n_chunks)
#     if y.ndim == 1:
#         out = parallel(pfunc(X[:, chunk], y) for chunk in chunks)
#     else:
#         out = parallel(pfunc(X[:, chunk], Y[:, chunk]) for chunk in chunks)
# 
#     # size back in case higher dependencies
#     X.resize(dims)
# 
#     # unpack
#     if isinstance(out[0], tuple):
#         return [np.reshape(out_, dims[1:]) for out_ in zip(*out)]
#     else:
#         return np.reshape(np.hstack(out), dims[1:])


# Path: jr/tests/test_utils.py
import numpy as np
from ..utils import tile_memory_free, pairwise
    from nose.tools import assert_equal
    from nose.tools import assert_equal, assert_raises



def test_tile_memory_free():
    y = np.arange(100)
    Y = tile_memory_free(y, 33)
    assert_equal(y.shape[0], Y.shape[0])
    np.testing.assert_array_equal(y, Y[:, 0], Y[:, -1])


def _dummy_pairwise_function_1(x, y):
    return x[0, :]


def _dummy_pairwise_function_2(x, y):
    return x[0, :], 0. * x[0, :]


def test_pairwise():
    n_obs = 20
    n_dims1 = 5
    n_dims2 = 10
    y = np.linspace(0, 1, n_obs)
    X = np.zeros((n_obs, n_dims1, n_dims2))
    for dim1 in range(n_dims1):
        for dim2 in range(n_dims2):
            X[:, dim1, dim2] = dim1 + 10*dim2

    # test size

    score = pairwise(X, y, _dummy_pairwise_function_1, n_jobs=2)