neurosynth/base/imageutils.py
def create_grid(image, scale=4, apply_mask=True, save_file=None):
    """ Creates an image containing labeled cells in a 3D grid.
    Args:
        image: String or nibabel image. The image used to define the grid
            dimensions. Also used to define the mask to apply to the grid.
            Only voxels with non-zero values in the mask will be retained; all
            other voxels will be zeroed out in the returned image.
        scale: The scaling factor which controls the grid size. Value reflects
            diameter of cube in voxels.
        apply_mask: Boolean indicating whether or not to zero out voxels not in
            image.
        save_file: Optional string giving the path to save image to. Image
            written out is a standard Nifti image. If save_file is None, no
            image is written.
    Returns:
        A nibabel image with the same dimensions as the input image. All voxels
        in each cell in the 3D grid are assigned the same non-zero label.
    """
    if isinstance(image, string_types):
        image = nb.load(image)

    # create a list of cluster centers
    centers = []
    x_length, y_length, z_length = image.shape
    for x in range(0, x_length, scale):
        for y in range(0, y_length, scale):
            for z in range(0, z_length, scale):
                centers.append((x, y, z))

    # create a box around each center with the diameter equal to the scaling
    # factor
    grid = np.zeros(image.shape)
    for (i, (x, y, z)) in enumerate(centers):
        for mov_x in range((-scale + 1) // 2, (scale + 1) // 2):
            for mov_y in range((-scale + 1) // 2, (scale + 1) // 2):
                for mov_z in range((-scale + 1) // 2, (scale + 1) // 2):
                    try:  # Ignore voxels outside bounds of image
                        grid[x + mov_x, y + mov_y, z + mov_z] = i + 1
                    except:
                        pass

    if apply_mask:
        mask = image
        if isinstance(mask, string_types):
            mask = nb.load(mask)
        if type(mask).__module__ != np.__name__:
            mask = mask.get_data()
        grid[~mask.astype(bool)] = 0.0

    grid = nb.Nifti1Image(grid, image.get_affine(), image.get_header())

    if save_file is not None:
        nb.save(grid, save_file)

    return grid
def create_grid(image, scale=4, apply_mask=True, save_file=None):
    """ Creates an image containing labeled cells in a 3D grid.
    Args:
        image: String or nibabel image. The image used to define the grid
            dimensions. Also used to define the mask to apply to the grid.
            Only voxels with non-zero values in the mask will be retained; all
            other voxels will be zeroed out in the returned image.
        scale: The scaling factor which controls the grid size. Value reflects
            diameter of cube in voxels.
        apply_mask: Boolean indicating whether or not to zero out voxels not in
            image.
        save_file: Optional string giving the path to save image to. Image
            written out is a standard Nifti image. If save_file is None, no
            image is written.
    Returns:
        A nibabel image with the same dimensions as the input image. All voxels
        in each cell in the 3D grid are assigned the same non-zero label.
    """
    if isinstance(image, string_types):
        image = nb.load(image)

    # create a list of cluster centers
    centers = []
    x_length, y_length, z_length = image.shape
    for x in range(0, x_length, scale):
        for y in range(0, y_length, scale):
            for z in range(0, z_length, scale):
                centers.append((x, y, z))

    # create a box around each center with the diameter equal to the scaling
    # factor
    grid = np.zeros(image.shape)
    for (i, (x, y, z)) in enumerate(centers):
        for mov_x in range((-scale + 1) // 2, (scale + 1) // 2):
            for mov_y in range((-scale + 1) // 2, (scale + 1) // 2):
                for mov_z in range((-scale + 1) // 2, (scale + 1) // 2):
                    try:  # Ignore voxels outside bounds of image
                        grid[x + mov_x, y + mov_y, z + mov_z] = i + 1
                    except:
                        pass

    if apply_mask:
        mask = image
        if isinstance(mask, string_types):
            mask = nb.load(mask)
        if type(mask).__module__ != np.__name__:
            mask = mask.get_data()
        grid[~mask.astype(bool)] = 0.0

    grid = nb.Nifti1Image(grid, image.get_affine(), image.get_header())

    if save_file is not None:
        nb.save(grid, save_file)

    return grid
Creates an image containing labeled cells in a 3D grid.
    Args:
        image: String or nibabel image. The image used to define the grid
            dimensions. Also used to define the mask to apply to the grid.
            Only voxels with non-zero values in the mask will be retained; all
            other voxels will be zeroed out in the returned image.
        scale: The scaling factor which controls the grid size. Value reflects
            diameter of cube in voxels.
        apply_mask: Boolean indicating whether or not to zero out voxels not in
            image.
        save_file: Optional string giving the path to save image to. Image
            written out is a standard Nifti image. If save_file is None, no
            image is written.
    Returns:
        A nibabel image with the same dimensions as the input image. All voxels
        in each cell in the 3D grid are assigned the same non-zero label.