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!")
def save_analyse_regions(viewer): ensure_directory_exists(paths.regions_directory) delete_directory_contents(str(paths.regions_directory)) if volumes: annotations = load_any(paths.annotations) hemispheres = load_any(paths.hemispheres) structures_reference_df = load_structures_as_df( get_structures_path()) print( f"\nSaving summary volumes to: {paths.regions_directory}") for label_layer in label_layers: analyse_region_brain_areas( label_layer, paths.regions_directory, annotations, hemispheres, structures_reference_df, ) print(f"\nSaving regions to: {paths.regions_directory}") for label_layer in label_layers: save_regions_to_file( label_layer, paths.regions_directory, paths.downsampled_image, ) close_viewer(viewer)
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)
def main(): print("Starting amap viewer") args = parser().parse_args() structures_df = load_structures_as_df(get_structures_path()) if not args.memory: print( "By default amap_vis does not load data into memory. " "To speed up visualisation, use the '-m' flag. Be aware " "this will make the viewer slower to open initially." ) paths = Paths(args.amap_directory) with napari.gui_qt(): v = napari.Viewer(title="amap viewer") if ( Path(paths.registered_atlas_path).exists() and Path(paths.boundaries_file_path).exists() ): if args.raw: image_scales = display_raw(v, args) else: if Path(paths.downsampled_brain_path).exists(): image_scales = display_downsampled(v, args, paths) else: raise FileNotFoundError( f"The downsampled image: " f"{paths.downsampled_brain_path} could not be found. " f"Please ensure this is the correct " f"directory and that amap has completed. " ) region_labels = display_registration( v, paths.registered_atlas_path, paths.boundaries_file_path, image_scales, memory=args.memory, ) @region_labels.mouse_move_callbacks.append def display_region_name(layer, event): display_brain_region_name(layer, structures_df) else: raise FileNotFoundError( f"The directory: '{args.amap_directory}' does not " f"appear to be complete. Please ensure this is the correct " f"directory and that amap has completed." )
def load_registration(self): self.status_label.setText("Loading...") self.region_labels = display_registration( self.viewer, self.registration_paths.registered_atlas_path, self.registration_paths.boundaries_file_path, self.image_scales, memory=memory, ) self.structures_df = load_structures_as_df(get_structures_path()) self.status_label.setText("Ready") @self.region_labels.mouse_move_callbacks.append def display_region_name(layer, event): display_brain_region_name(layer, self.structures_df)
def load_amap_directory(self): self.select_nii_file() self.registration_directory = self.downsampled_file.parent self.status_label.setText(f"Loading ...") self.paths = Paths(self.registration_directory, self.downsampled_file) if not self.paths.tmp__inverse_transformed_image.exists(): print( f"The image: '{self.downsampled_file}' has not been transformed into standard " f"space, and so must be transformed before segmentation.\n") transform_image_to_standard_space( self.registration_directory, image_to_transform_fname=self.downsampled_file, output_fname=self.paths.tmp__inverse_transformed_image, log_file_path=self.paths.tmp__inverse_transform_log_path, error_file_path=self.paths.tmp__inverse_transform_error_path, ) else: print("Registered image exists, skipping registration\n") self.registered_image = prepare_load_nii( self.paths.tmp__inverse_transformed_image, memory=memory) self.base_layer = display_channel( self.viewer, self.registration_directory, self.paths.tmp__inverse_transformed_image, memory=memory, name="Image in standard space", ) self.initialise_image_view() self.structures_df = load_structures_as_df(get_structures_path()) self.load_button.setMinimumWidth(0) self.load_atlas_button.setVisible(True) self.save_button.setVisible(True) self.initialise_region_segmentation() self.initialise_track_tracing() self.status_label.setText(f"Ready")
def xml_crop(args, df_query="name"): args = prep_atlas_conf(args) if args.structures_file_path is None: args.structures_file_path = get_structures_path() reference_struct_df = pd.read_csv(get_structures_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(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 = 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!")
from neuro.atlas_tools import paths as reg_paths from neuro.atlas_tools.misc import get_voxel_volume, get_atlas_pixel_sizes import napari import numpy as np amap_output_dir = ( "/media/adam/Storage/cellfinder/analysis/inj_segment/CT_CX_142_2_test") region_acronym = "RSP" visual_check = False left_hemisphere_value = 2 right_hemisphere_value = 1 properties_to_fetch = ["area", "bbox", "centroid"] structures_reference_df = load_structures_as_df(get_structures_path()) region_value = int(structures_reference_df[structures_reference_df["acronym"] == region_acronym]["id"]) region_search_string = "/" + str(region_value) + "/" sub_regions = structures_reference_df[structures_reference_df[ "structure_id_path"].str.contains(region_search_string)] amap_output_dir = Path(amap_output_dir) annotations_image = load_any(amap_output_dir / reg_paths.ANNOTATIONS) midpoint = int(annotations_image.shape[0] // 2) hemispheres_image = load_any(amap_output_dir / reg_paths.HEMISPHERES) sub_region_values = list(sub_regions["id"])
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 = nii.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 = 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}")
def main(): print("Starting amap viewer") args = parser().parse_args() structures_path = get_structures_path() structures_df = load_structures_as_df(structures_path) if not args.memory: print( "By default amap_vis does not load data into memory. " "To speed up visualisation, use the '-m' flag. Be aware " "this will make the viewer slower to open initially." ) paths = Paths(args.amap_directory) with napari.gui_qt(): v = napari.Viewer(title="amap viewer") if ( Path(paths.registered_atlas_path).exists() and Path(paths.boundaries_file_path).exists() ): if args.raw: image_scales = display_raw(v, args) else: if Path(paths.downsampled_brain_path).exists(): image_scales = display_downsampled(v, args, paths) else: raise FileNotFoundError( f"The downsampled image: " f"{paths.downsampled_brain_path} could not be found. " f"Please ensure this is the correct " f"directory and that amap has completed. " ) labels = display_registration( v, paths.registered_atlas_path, paths.boundaries_file_path, image_scales, memory=args.memory, ) @labels.mouse_move_callbacks.append def get_connected_component_shape(layer, event): val = layer.get_value() if val != 0 and val is not None: try: region = atlas_value_to_name(val, structures_df) msg = f"{region}" except UnknownAtlasValue: msg = "Unknown region" else: msg = "No label here!" layer.help = msg else: raise FileNotFoundError( f"The directory: '{args.amap_directory}' does not " f"appear to be complete. Please ensure this is the correct " f"directory and that amap has completed." )