def test_atlas_value_to_structure_id(): structure_df = load_structures_as_df(structures_csv) assert structure_id_100 == structures_tree.atlas_value_to_structure_id( 100, structure_df) with pytest.raises(structures_tree.UnknownAtlasValue): structures_tree.atlas_value_to_structure_id(100000, structure_df)
def test_atlas_value_to_name(): structure_df = load_structures_as_df(structures_csv) assert "Interpeduncular nucleus" == structures_tree.atlas_value_to_name( 100, structure_df) with pytest.raises(structures_tree.UnknownAtlasValue): structures_tree.atlas_value_to_name(100000, structure_df)
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 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 test_load_structures_df(): structures = load_structures_as_df(structures_csv) assert len(structures) == 1299 assert (structures.keys() == Index(structure_headers)).all() structures_test = list(structures.iloc[100].array) assert structures_test[1] == structures_line_100[1] assert structures_test[2] == structures_line_100[2] assert structures_test[3] == structures_line_100[3]
def analysis_run(args, file_name="summary_cell_counts.csv"): args = prep_atlas_conf(args) 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(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 calculate_volumes( atlas_path, hemispheres_path, atlas_structures_path, registration_config, output_file, left_hemisphere_value=2, right_hemisphere_value=1, ): ( unique_vals_left, unique_vals_right, counts_left, counts_right, ) = get_lateralised_atlas( atlas_path, hemispheres_path, left_hemisphere_value=left_hemisphere_value, right_hemisphere_value=right_hemisphere_value, ) structures_reference_df = load_structures_as_df(atlas_structures_path) voxel_volume = get_voxel_volume(registration_config) voxel_volume_in_mm = voxel_volume / (1000**3) df = initialise_df( "structure_name", "left_volume_mm3", "right_volume_mm3", "total_volume_mm3", ) for atlas_value in unique_vals_left: if atlas_value is not 0: # outside brain try: df = add_structure_volume_to_df( df, atlas_value, structures_reference_df, unique_vals_left, unique_vals_right, counts_left, counts_right, voxel_volume_in_mm, ) except UnknownAtlasValue: print( "Value: {} is not in the atlas structure reference file. " "Not calculating the volume".format(atlas_value)) df.to_csv(output_file, index=False)
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 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." )