# 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 nandigitize(x, bins, right=None):
#     x = np.array(x)
#     dims = x.shape
#     x = np.reshape(x, [1, -1])
#     sel = ~ np.isnan(x)
#     x_ = x[sel]
#     x[sel] = np.digitize(x_, bins, right=right)
#     x[~sel] = np.nan
#     return x.reshape(dims)


import numpy as np
from ..utils import tile_memory_free, nandigitize
from nose.tools import assert_almost_equal
    bins = np.array(bins)
    dims = radius.shape
    if dims[0] != 2:
        radius = np.reshape(radius, [len(radius), -1])
    if wrapped:
        bins = np.hstack((bins[:-1] / 2, np.pi + bins[:-1] / 2, bins[0]))
        radius = np.vstack((radius[:-1, :], radius))
    else:
        bins = np.hstack((bins / 2, np.pi + bins / 2, bins[0]))
        radius = np.vstack((radius, radius, radius[0, :]))
    shape = np.hstack((dims[0] * 2 - 1, dims[1:]))
    radius = np.reshape(radius, shape.astype(int))
    return radius, bins


def circ_diff(A, B):
    """WIP explodes memory"""
    raise NotImplementedError
    A = np.array(A) % (2 * np.pi)
    B = np.array(B) % (2 * np.pi)
    dims = A.shape
    if (B.ndim == 1) and (len(B) == 1):
        A = np.reshape([-1, 1])
        Bx = np.cos(B)
        By = np.sin(B)
        out = [np.angle(np.cos(a) - Bx + 1.j * (np.sin(a) - By)) for a in A]
    else:
        if len(A) != len(B):
            raise ValueError('A and B must share first axis')
        if (B.ndim == 1) and (A.ndim > 1):

# 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 nandigitize(x, bins, right=None):
#     x = np.array(x)
#     dims = x.shape
#     x = np.reshape(x, [1, -1])
#     sel = ~ np.isnan(x)
#     x_ = x[sel]
#     x[sel] = np.digitize(x_, bins, right=right)
#     x[~sel] = np.nan
#     return x.reshape(dims)


# Path: jr/stats/circ.py
import numpy as np
from ..utils import tile_memory_free, nandigitize
from nose.tools import assert_almost_equal

    bins = np.array(bins)
    dims = radius.shape
    if dims[0] != 2:
        radius = np.reshape(radius, [len(radius), -1])
    if wrapped:
        bins = np.hstack((bins[:-1] / 2, np.pi + bins[:-1] / 2, bins[0]))
        radius = np.vstack((radius[:-1, :], radius))
    else:
        bins = np.hstack((bins / 2, np.pi + bins / 2, bins[0]))
        radius = np.vstack((radius, radius, radius[0, :]))
    shape = np.hstack((dims[0] * 2 - 1, dims[1:]))
    radius = np.reshape(radius, shape.astype(int))
    return radius, bins


def circ_diff(A, B):
    """WIP explodes memory"""
    raise NotImplementedError
    A = np.array(A) % (2 * np.pi)
    B = np.array(B) % (2 * np.pi)
    dims = A.shape
    if (B.ndim == 1) and (len(B) == 1):
        A = np.reshape([-1, 1])
        Bx = np.cos(B)
        By = np.sin(B)
        out = [np.angle(np.cos(a) - Bx + 1.j * (np.sin(a) - By)) for a in A]
    else:
        if len(A) != len(B):
            raise ValueError('A and B must share first axis')
        if (B.ndim == 1) and (A.ndim > 1):

            B = tile_memory_free(B, A.shape[0])