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 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!")
def main(): suppress_tf_logging(tf_suppress_log_messages) import amap.main as register from cellfinder.tools import prep start_time = datetime.now() args, what_to_run = prep.prep_cellfinder_general() if what_to_run.register: # TODO: add register_part_brain option logging.info("Registering to atlas") args, additional_images_downsample = prep.prep_registration(args) register.main( args.registration_config, args.target_brain_path, args.paths.registration_output_folder, x_pixel_um=args.x_pixel_um, y_pixel_um=args.y_pixel_um, z_pixel_um=args.z_pixel_um, orientation=args.orientation, flip_x=args.flip_x, flip_y=args.flip_y, flip_z=args.flip_z, affine_n_steps=args.affine_n_steps, affine_use_n_steps=args.affine_use_n_steps, freeform_n_steps=args.freeform_n_steps, freeform_use_n_steps=args.freeform_use_n_steps, bending_energy_weight=args.bending_energy_weight, grid_spacing_x=args.grid_spacing_x, smoothing_sigma_reference=args.smoothing_sigma_reference, smoothing_sigma_floating=args.smoothing_sigma_floating, histogram_n_bins_floating=args.histogram_n_bins_floating, histogram_n_bins_reference=args.histogram_n_bins_reference, sort_input_file=args.sort_input_file, n_free_cpus=args.n_free_cpus, save_downsampled=not (args.no_save_downsampled), additional_images_downsample=additional_images_downsample, boundaries=not (args.no_boundaries), debug=args.debug, ) else: logging.info("Skipping registration") if what_to_run.summarise: args = prep.prep_atlas_conf(args) if len(args.signal_planes_paths) > 1: base_directory = args.output_dir for idx, signal_paths in enumerate(args.signal_planes_paths): channel = args.signal_ch_ids[idx] logging.info("Processing channel: " + str(channel)) channel_directory = os.path.join( base_directory, "channel_" + str(channel) ) if not os.path.exists(channel_directory): os.makedirs(channel_directory) # prep signal channel specific args args.signal_planes_paths[0] = signal_paths # TODO: don't overwrite args.output_dir - use Paths instead args.output_dir = channel_directory args.signal_channel = channel # Run for each channel run_all(args, what_to_run) else: args.signal_channel = args.signal_ch_ids[0] run_all(args, what_to_run) logging.info( "Finished. Total time taken: {}".format(datetime.now() - start_time) )