def distance_func(func, coords, shape, renorm=True, mp=True):
    """Generates masks for seperable distance functions"""
    nZ, nY, nX = shape
    dx = coords[:,0] - np.atleast_2d(np.arange(nX)).T
    dy = coords[:,1] - np.atleast_2d(np.arange(nY)).T
    dz = coords[:,2] - np.atleast_2d(np.arange(nZ)).T

    Lx, Ly, Lz = func(dx), func(dy), func(dz)
    ix, jx = np.nonzero(Lx)
    iy, jy = np.nonzero(Ly)
    iz, jz = np.nonzero(Lz)
    ba = np.broadcast_arrays
    def func(v):
        mx, my, mz = ix[jx == v], iy[jy == v], iz[jz == v]
        idx, idy, idz = [i.ravel() for i in ba(*np.ix_(mx, my, mz))]
        vx, vy, vz = [i.ravel() for i in ba(*np.ix_(Lx[mx, v], Ly[my, v], Lz[mz, v]))]

        i = v * np.ones((len(idx,)))
        j = np.ravel_multi_index((idz, idy, idx), shape, mode='clip')
        data = vx*vy*vz
        if renorm:
            data /= data.sum()
        return i, j, data

    if mp:
        from .. import mp
        ijdata = mp.map(func, range(len(coords)))
    else:
        ijdata = map(func, range(len(coords)))

    return np.hstack(ijdata)
dataset/ETHPy150Open gallantlab/pycortex/cortex/mapper/samplers.py/distance_func/original