def summary_run(args):
    # if args.structures_file_path is None:
    #     args.structures_file_path = get_structures_path()
    #     reference_structures_table = pd.read_csv(args.structures_file_path)
    # else:
    #     raise NotImplementedError(
    #         "Only the Allen adult mouse atlas is " "currently supported."
    #     )
    reference_structures_table = pd.read_csv(get_structures_path())

    if not args.regions and not args.regions_list:
        # regions = list(pd.read_csv(args.structures_file_path)["name"])
        regions = list(pd.read_csv(get_structures_path())["name"])

    else:
        regions = []
        if args.regions:
            regions = regions + args.regions
        if args.regions_list:
            regions = regions + list(pd.read_csv(args.regions_list)["name"])

    regions = remove_empty_string(regions)
    regions = unique_elements_lists(regions)
    csvs_folder = args.csv_dir
    destination_folder = os.path.join(args.csv_dir, "summary")

    csvs_names = [f for f in os.listdir(csvs_folder) if f.endswith(".csv")]
    if len(csvs_names) == 0:
        raise FileNotFoundError(
            "No CSV files were found in the directory: "
            "{}. Please check the arguments".format(csvs_folder))

    csvs_paths = [os.path.join(csvs_folder, f) for f in csvs_names]

    if not os.path.exists(destination_folder):
        os.makedirs(destination_folder)

    for csv_file_path in csvs_paths:
        print("CSV file: {}".format(os.path.basename(csv_file_path)))
        summary = make_summary_df_for_brain(
            csv_file_path,
            regions,
            reference_structures_table,
            sum_regions=args.sum_regions,
        )
        filename = os.path.basename(csv_file_path)
        dest_path = os.path.join(destination_folder, filename)

        summary.to_csv(dest_path, index=False)
    print("Done!")
Example #2
0
def analysis_run(args, file_name="summary_cell_counts.csv"):
    args = prep_atlas_conf(args)

    # if args.structures_file_path is None:
    #     args.structures_file_path = get_structures_path()

    atlas = brainio.load_any(args.paths.registered_atlas_path)
    hemisphere = brainio.load_any(args.paths.hemispheres_atlas_path)

    cells = get_cells_data(
        args.paths.classification_out_file, cells_only=args.cells_only,
    )
    max_coords = get_max_coords(cells)  # Useful for debugging dimensions
    # structures_reference_df = load_structures_as_df(args.structures_file_path)
    structures_reference_df = load_structures_as_df(get_structures_path())

    atlas_pixel_sizes = get_atlas_pixel_sizes(args.atlas_config)
    sample_pixel_sizes = args.x_pixel_um, args.y_pixel_um, args.z_pixel_um

    scales = get_scales(
        sample_pixel_sizes, atlas_pixel_sizes, args.scale_cell_coordinates
    )

    structures_with_cells = set()
    for i, cell in enumerate(tqdm(cells)):
        transform_cell_coords(atlas, cell, scales)

        structure_id = get_structure_from_coordinates(
            atlas,
            cell,
            max_coords,
            order=args.coordinates_order,
            structures_reference_df=structures_reference_df,
        )
        if structure_id is not None:
            cell.structure_id = structure_id

            structures_with_cells.add(structure_id)
        else:
            continue

        cell.hemisphere = get_structure_from_coordinates(
            hemisphere, cell, max_coords, order=args.coordinates_order
        )

    sorted_cell_numbers = get_cells_nbs_df(
        cells, structures_reference_df, structures_with_cells
    )

    combined_hemispheres = combine_df_hemispheres(sorted_cell_numbers)
    df = calculate_densities(combined_hemispheres, args.paths.volume_csv_path)
    df = sanitise_df(df)
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)
    output_file = os.path.join(args.output_dir, file_name)
    df.to_csv(output_file, index=False)
Example #3
0
def generate_region_volume(structure_names,
                           atlas_path,
                           output_path,
                           atlas_config,
                           glass=False):
    structure_csv_file = get_structures_path()
    reference_structures_table = pd.read_csv(structure_csv_file)

    # ensure all names are valid
    for indv_structure_name in structure_names:
        try:
            get_substructures(indv_structure_name, reference_structures_table)
        except IndexError:
            raise ValueError(
                f"Brain region: '{indv_structure_name}' cannot be found "
                f"in file: {structure_csv_file}. Please choose "
                f"another structure.")

    print(f"Loading atlas from: {atlas_path}")
    atlas = brainio.load_nii(atlas_path, as_array=False)
    atlas_scale = atlas.header.get_zooms()
    atlas = atlas.get_data()

    transformation_matrix = brain_tools.get_transformation_matrix(atlas_config)

    if len(structure_names) > 1:
        # Initialise an image to add each subimage to.
        final_image = np.zeros_like(atlas)

    for indv_structure_name in structure_names:
        print(f"Analysing brain region: {indv_structure_name}")
        substructures = get_substructures(indv_structure_name,
                                          reference_structures_table)

        print("This includes structures:")
        indv_substructure_names = substructures["name"].values
        for indv_substructure_name in indv_substructure_names:
            print(indv_substructure_name)

        list_vals = substructures["id"].values

        print("Generating image with specified regions \n")
        sub_image = np.isin(atlas, list_vals)

        if glass:
            print("Generating glass brain")
            sub_image = sk_segmentation.find_boundaries(sub_image)

        # If multiple structures, add them together
        if len(structure_names) > 1:
            final_image = np.logical_or(final_image, sub_image)
        else:
            final_image = sub_image

    print("Converting image to 16 bit")
    final_image = tools.scale_and_convert_to_16_bits(final_image)

    print("Saving image")
    brainio.to_nii(
        final_image,
        output_path,
        scale=atlas_scale,
        affine_transform=transformation_matrix,
    )

    print(f"Saved image at: {output_path}")
Example #4
0
def xml_crop(args, df_query="name"):
    args = prep_atlas_conf(args)

    if args.reference_structures_file_path is None:
        args.reference_structures_file_path = get_structures_path()
    if args.structures_file_path is None:
        args.structures_file_path = get_structures_path()

    reference_struct_df = pd.read_csv(args.reference_structures_file_path)
    curate_struct_df = pd.read_csv(args.structures_file_path)

    curate_struct_df = reference_struct_df[reference_struct_df[df_query].isin(
        curate_struct_df[df_query])]

    curated_ids = list(curate_struct_df["structure_id_path"])

    atlas = brainio.load_any(args.registered_atlas_path)
    hemisphere = brainio.load_any(args.hemispheres_atlas_path)

    structures_reference_df = load_structures_as_df(
        args.reference_structures_file_path)

    atlas_pixel_sizes = cells_regions.get_atlas_pixel_sizes(args.atlas_config)
    sample_pixel_sizes = args.x_pixel_um, args.y_pixel_um, args.z_pixel_um

    scales = cells_regions.get_scales(sample_pixel_sizes, atlas_pixel_sizes)

    destination_folder = os.path.join(args.xml_dir, "xml_crop")
    if not os.path.exists(destination_folder):
        os.makedirs(destination_folder)

    xml_names = [f for f in os.listdir(args.xml_dir) if f.endswith(".xml")]
    xml_paths = [os.path.join(args.xml_dir, f) for f in xml_names]

    for idx, xml_path in enumerate(xml_paths):
        print("Curating file: {}".format(xml_names[idx]))
        cells = cells_regions.get_cells_data(
            xml_path,
            cells_only=args.cells_only,
        )
        max_coords = cells_regions.get_max_coords(cells)

        curated_cells = []
        for i, cell in enumerate(cells):
            cells_regions.transform_cell_coords(atlas, cell, scales)

            structure_id = cells_regions.get_structure_from_coordinates(
                atlas,
                cell,
                max_coords,
                order=args.coordinates_order,
                structures_reference_df=structures_reference_df,
            )
            if structure_id in curated_ids:
                if args.hemisphere_query in [1, 2]:
                    hemisphere = cells_regions.get_structure_from_coordinates(
                        hemisphere,
                        cell,
                        max_coords,
                        order=args.coordinates_order,
                    )
                    if hemisphere is args.hemisphere_query:
                        curated_cells.append(cell)
                else:
                    curated_cells.append(cell)
        cells_to_xml(
            curated_cells,
            os.path.join(destination_folder, xml_names[idx]),
            artifact_keep=True,
        )
    print("Done!")