Esempio n. 1
0
def run(
    downsampled_points,
    atlas,
    downsampled_shape,
    registered_atlas_path,
    output_filename,
    smoothing=None,
    mask=True,
):

    points = pd.read_hdf(downsampled_points).values

    bins = get_bins(downsampled_shape, (1, 1, 1))
    heatmap_array, _ = np.histogramdd(points, bins=bins)
    heatmap_array = heatmap_array.astype(np.uint16)

    if smoothing is not None:
        logging.debug("Smoothing heatmap")
        # assume isotropic atlas
        smoothing = int(round(smoothing / atlas.resolution[0]))
        heatmap_array = gaussian(heatmap_array, sigma=smoothing)

    if mask:
        logging.debug("Masking image based on registered atlas")
        atlas = tifffile.imread(registered_atlas_path)
        heatmap_array = mask_image_threshold(heatmap_array, atlas)
        del atlas

    logging.debug("Saving heatmap")
    heatmap_array = scale_and_convert_to_16_bits(heatmap_array)

    logging.debug("Ensuring output directory exists")
    ensure_directory_exists(Path(output_filename).parent)

    logging.debug("Saving heatmap image")
    imio.to_tiff(heatmap_array, output_filename)
Esempio n. 2
0
def run(
    cells_file,
    output_filename,
    target_size,
    raw_image_shape,
    raw_image_bin_sizes,
    transformation_matrix,
    atlas_scale,
    smoothing=10,
    mask=True,
    atlas=None,
    cells_only=True,
    convert_16bit=True,
):
    """

    :param cells_file: Cellfinder output cells file.
    :param output_filename: File to save heatmap into
    :param target_size: Size of the final heatmap
    :param raw_image_shape: Size of the raw data (coordinate space of the
    cells)
    :param raw_image_bin_sizes: List/tuple of the sizes of the bins in the
    raw data space
    :param transformation_matrix: Transformation matrix so that the resulting
    nifti can be processed using other tools.
    :param atlas_scale: Image scaling so that the resulting nifti can be
    processed using other tools.
    :param smoothing: Smoothing kernel size, in the target image space
    :param mask: Whether or not to mask the heatmap based on an atlas file
    :param atlas: Atlas file to mask the heatmap
    :param cells_only: Only use "cells", not artefacts
    :param convert_16bit: Convert final image to 16 bit


    """

    # TODO: compare the smoothing effects of gaussian filtering, and upsampling
    target_size = convert_shape_dict_to_array_shape(target_size, type="fiji")
    raw_image_shape = convert_shape_dict_to_array_shape(raw_image_shape,
                                                        type="fiji")
    cells_array = get_cell_location_array(cells_file, cells_only=cells_only)
    bins = get_bins(raw_image_shape, raw_image_bin_sizes)

    logging.debug("Generating heatmap (3D histogram)")
    heatmap_array, _ = np.histogramdd(cells_array, bins=bins)

    logging.debug("Resizing heatmap to the size of the target image")
    heatmap_array = resize(heatmap_array, target_size, order=0)
    if smoothing is not None:
        logging.debug("Applying Gaussian smoothing with a kernel sigma of: "
                      "{}".format(smoothing))
        heatmap_array = gaussian(heatmap_array, sigma=smoothing)

    if mask:
        logging.debug("Masking image based on registered atlas")
        # copy, otherwise it's modified, which affects later figure generation
        atlas_for_mask = np.copy(atlas)
        heatmap_array = mask_image_threshold(heatmap_array, atlas_for_mask)

    if convert_16bit:
        logging.debug("Converting to 16 bit")
        heatmap_array = scale_and_convert_to_16_bits(heatmap_array)

    logging.debug("Saving heatmap image")
    brainio.to_nii(
        heatmap_array,
        output_filename,
        scale=atlas_scale,
        affine_transform=transformation_matrix,
    )
Esempio n. 3
0
def test_mask_image_threshold():
    result = masking.mask_image_threshold(raw_image, raw_image, threshold=4)
    assert (result == masked_image).all()