Пример #1
0
    def __init__(self, registration_output_folder, downsampled_image):
        self.registration_output_folder = registration_output_folder

        self.segmentation_directory = (self.registration_output_folder /
                                       "manual_segmentation")

        ensure_directory_exists(self.segmentation_directory)
        self.downsampled_image = self.join_seg_files(downsampled_image)

        self.tmp__inverse_transformed_image = self.join_seg_files(
            "image_standard_space.nii")
        self.tmp__inverse_transform_log_path = self.join_seg_files(
            "inverse_transform_log.txt")
        self.tmp__inverse_transform_error_path = self.join_seg_files(
            "inverse_transform_error.txt")

        self.annotations = self.join_reg_files(reg_paths.ANNOTATIONS)
        self.hemispheres = self.join_reg_files(reg_paths.HEMISPHERES)

        self.regions_directory = self.join_seg_files("regions")
        self.region_summary_csv = self.regions_directory / "summary.csv"

        self.tracks_directory = self.join_seg_files("tracks")
Пример #2
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)
Пример #3
0
def run_all(args, what_to_run, atlas):

    from cellfinder_core.detect import detect
    from cellfinder_core.classify import classify
    from cellfinder_core.tools import prep
    from cellfinder_core.tools.IO import read_with_dask

    from cellfinder.analyse import analyse
    from cellfinder.figures import figures

    from cellfinder.tools.prep import (
        prep_candidate_detection,
        prep_channel_specific_general,
    )

    points = None
    signal_array = None
    args, what_to_run = prep_channel_specific_general(args, what_to_run)

    if what_to_run.detect:
        logging.info("Detecting cell candidates")
        args = prep_candidate_detection(args)
        signal_array = read_with_dask(
            args.signal_planes_paths[args.signal_channel]
        )

        points = detect.main(
            signal_array,
            args.start_plane,
            args.end_plane,
            args.voxel_sizes,
            args.soma_diameter,
            args.max_cluster_size,
            args.ball_xy_size,
            args.ball_z_size,
            args.ball_overlap_fraction,
            args.soma_spread_factor,
            args.n_free_cpus,
            args.log_sigma_size,
            args.n_sds_above_mean_thresh,
        )
        ensure_directory_exists(args.paths.points_directory)

        save_cells(
            points,
            args.paths.detected_points,
            save_csv=args.save_csv,
            artifact_keep=args.artifact_keep,
        )

    else:
        logging.info("Skipping cell detection")
        points = get_cells(args.paths.detected_points)

    if what_to_run.classify:
        model_weights = prep.prep_classification(
            args.trained_model,
            args.model_weights,
            args.install_path,
            args.model,
            args.n_free_cpus,
        )
        if what_to_run.classify:
            if points is None:
                points = get_cells(args.paths.detected_points)
            if signal_array is None:
                signal_array = read_with_dask(
                    args.signal_planes_paths[args.signal_channel]
                )
            logging.info("Running cell classification")
            background_array = read_with_dask(args.background_planes_path[0])

            points = classify.main(
                points,
                signal_array,
                background_array,
                args.n_free_cpus,
                args.voxel_sizes,
                args.network_voxel_sizes,
                args.batch_size,
                args.cube_height,
                args.cube_width,
                args.cube_depth,
                args.trained_model,
                model_weights,
                args.network_depth,
            )
            save_cells(
                points,
                args.paths.classified_points,
                save_csv=args.save_csv,
            )

            what_to_run.cells_exist = cells_exist(args.paths.classified_points)

        else:
            logging.info("No cells were detected, skipping classification.")

    else:
        logging.info("Skipping cell classification")

    what_to_run.update_if_cells_required()

    if what_to_run.analyse or what_to_run.figures:
        downsampled_space = get_downsampled_space(
            atlas, args.brainreg_paths.boundaries_file_path
        )

    if what_to_run.analyse:
        points = get_cells(args.paths.classified_points, cells_only=True)
        if len(points) == 0:
            logging.info("No cells detected, skipping cell position analysis")
        else:
            logging.info("Analysing cell positions")
            analyse.run(args, points, atlas, downsampled_space)
    else:
        logging.info("Skipping cell position analysis")

    if what_to_run.figures:
        points = get_cells(args.paths.detected_points, cells_only=True)
        if len(points) == 0:
            logging.info("No cells detected, skipping")
        else:
            logging.info("Generating figures")
            figures.run(args, atlas, downsampled_space.shape)
    else:
        logging.info("Skipping figure generation")