def prep_registration(args, sample_name="amap"): logging.info("Checking whether the atlas exists") _, atlas_files_exist = check_atlas_install() atlas_dir = os.path.join(args.install_path, "atlas") if not atlas_files_exist: if args.download_path is None: atlas_download_path = os.path.join(temp_dir_path, "atlas.tar.gz") else: atlas_download_path = os.path.join(args.download_path, "atlas.tar.gz") if not args.no_atlas: logging.warning("Atlas does not exist, downloading.") atlas_download.main(args.atlas, atlas_dir, atlas_download_path) amend_cfg(new_atlas_folder=atlas_dir, atlas=args.atlas) if args.registration_config is None: args.registration_config = source_files.source_custom_config() args.target_brain_path = args.background_planes_path[0] args.sample_name = sample_name logging.debug("Making registration directory") ensure_directory_exists(args.paths.registration_output_folder) additional_images_downsample = {} for idx, images in enumerate(args.signal_planes_paths): channel = args.signal_ch_ids[idx] additional_images_downsample[f"channel_{channel}"] = images return args, additional_images_downsample
def prep_atlas_conf(args): """ Used by cellfinder_run to source the correct registration configuration and also by cellfinder_count_summary to get atlas pixel sizes. :param args: Args possibly including "registration_config" :return: args: Args with atlas configuration for registration """ if args.atlas_config is None: if (hasattr(args, "registration_config") and args.registration_config is not None): args.atlas_config = args.registration_config else: # can get atlas pixel sizes from registration config args.atlas_config = source_files.source_custom_config() return args
def amend_cfg(new_atlas_folder=None, new_model_path=None, atlas=None): """ Updates the registration config file to point to the correct atlas path :param new_atlas_folder: """ print("Ensuring custom config file is correct") original_config = source_files.source_config() new_config = source_files.source_custom_config() if new_atlas_folder is not None: write_atlas_to_cfg(new_atlas_folder, atlas, original_config, new_config) original_config = new_config if new_model_path is not None: write_model_to_cfg(new_model_path, original_config, new_config)
def main(): start_time = datetime.now() print("Starting region volume generation") args = get_parser().parse_args() if args.atlas_config is None: args.atlas_config = source_custom_config() generate_region_volume( args.structure_names, args.atlas_path, args.output_filename, args.atlas_config, args.glass, ) print("Finished. Total time taken: {}".format(datetime.now() - start_time))
def check_atlas_install(): """ Checks whether the atlas directory exists, and whether it's empty or not. :return: Whether the directory exists, and whether the files also exist """ # TODO: make more sophisticated, check for all files that might be needed dir_exists = False files_exist = False cfg_file_path = source_files.source_custom_config() if os.path.exists(cfg_file_path): config_obj = get_config_obj(cfg_file_path) atlas_conf = config_obj["atlas"] atlas_directory = atlas_conf["base_folder"] if os.path.exists(atlas_directory): dir_exists = True if not os.listdir(atlas_directory) == []: files_exist = True return dir_exists, files_exist
def transform_cells_to_standard_space(args): if args.registration_config is None: args.registration_config = source_custom_config() reg_params = RegistrationParams( args.registration_config, 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, ) generate_deformation_field(args, reg_params) cells_only = not args.transform_all cells = get_cells(args.paths.classification_out_file, cells_only=cells_only) logging.info("Loading deformation field") deformation_field = load_any_image(args.paths.tmp__deformation_field) scales = get_scales(args, reg_params) field_scales = get_deformation_field_scales(reg_params) logging.info("Transforming cell positions") transformed_cells = transform_cell_positions(cells, deformation_field, field_scales, scales) logging.info("Saving transformed cell positions") save_cells( transformed_cells, args.paths.cells_in_standard_space, save_csv=args.save_csv, ) if not args.debug: logging.info("Removing standard space transformation temp files") tools.delete_temp(args.paths.standard_space_output_folder, args.paths)
def prep_models(args): ## if no model or weights, set default weights if args.trained_model is None and args.model_weights is None: logging.debug("No model or weights supplied, so using the default") config_file = source_files.source_custom_config() if not Path(config_file).exists(): logging.debug("Custom config does not exist, downloading models") model_path = model_download.main(args.model, args.install_path) amend_cfg(new_model_path=model_path) model_weights = get_model_weights(config_file) if model_weights is not "" and Path(model_weights).exists(): args.model_weights = model_weights else: logging.debug("Model weights do not exist, downloading") model_path = model_download.main(args.model, args.install_path) amend_cfg(new_model_path=model_path) model_weights = get_model_weights(config_file) args.model_weights = model_weights return args
def main(): start_time = datetime.now() print("Starting ROI transformation") args = get_parser().parse_args() rois = args.rois print(f"ROI file is: {rois}") if args.registration_config is None: args.registration_config = source_custom_config() atlas = Atlas(args.registration_config) source_image = args.reg_dir / SOURCE_IMAGE_NAME print(f"Source image is: {source_image}") destination_image = args.reg_dir / atlas.atlas_conf["default_brain_name"] print(f"Destination image is: {destination_image}") control_point_file = args.reg_dir / DEFAULT_CONTROL_POINT_FILE print(f"Transformation file is: {control_point_file}") if args.output_filename is None: output_filename = rois.parent / DEFAULT_OUTPUT_FILE_NAME temp_output_filename = rois.parent / DEFAULT_TEMP_FILE_NAME log_file_path = rois.parent / "roi_transform_log.txt" error_file_path = rois.parent / "roi_transform_error.txt" else: output_filename = args.output_filename temp_output_filename = (args.output_filename.parent / DEFAULT_TEMP_FILE_NAME) log_file_path = args.output_filename.parent / "roi_transform_log.txt" error_file_path = (args.output_filename.parent / "roi_transform_error.txt") if not output_filename.parent.exists(): output_filename.parent.mkdir() print(f"Output file is: {output_filename}") atlas = brainio.load_nii(str(destination_image), as_array=False) atlas_scale = atlas.header.get_zooms() atlas_pixel_sizes = cells_regions.get_atlas_pixel_sizes( args.registration_config) transformation_matrix = np.eye(4) for i, axis in enumerate(("x", "y", "z")): transformation_matrix[i, i] = atlas_pixel_sizes[axis] transform_rois( rois, source_image, destination_image, control_point_file, output_filename, temp_output_filename, log_file_path, error_file_path, roi_reference_image=args.reference_image, selem_size=args.selem_size, nii_scale=atlas_scale, transformation_matrix=transformation_matrix, debug=args.debug, z_filter_padding=args.z_filter_padding, ) print("Finished. Total time taken: {}".format(datetime.now() - start_time))
def get_atlas_config(self): if self._atlas_config is None: self._atlas_config = source_custom_config()
def get_registration_config(self): if self._registration_config is None: self._registration_config = source_custom_config()