def permuteToBlocks(arr, blockshape):
    """Permute an array so that it consists of linearized blocks.

    Example: A two-dimensional array of the form

        0  1  2  3
        4  5  6  7
        8  9  10 11
        12 13 14 15

    would be turned into an array like this with (2, 2) blocks:

        0 1 4 5 2 3 6 7 8 9 12 13 10 11 14 15
    """
    if len(blockshape) < 2:
        raise ValueError("Need more than one dimension.")
    elif len(blockshape) == 2:
        blockheight, blockwidth = blockshape
        return permuteToBlocks2d(arr, blockheight, blockwidth)
    elif len(blockshape) == 3:
        blockdepth, blockheight, blockwidth = blockshape
        return permuteToBlocks3d(arr, blockdepth, blockheight, blockwidth)
    else:
        raise NotImplementedError("Only for dimensions 2 and 3.")
dataset/ETHPy150Open pybrain/pybrain/pybrain/utilities.py/permuteToBlocks/original