From 0a8070493762aeb941481bb44c2a9928f468eec9 Mon Sep 17 00:00:00 2001 From: LuciMoore Date: Thu, 15 Feb 2024 15:06:09 -0800 Subject: [PATCH] added intermediate crudecorrected aseg filepath in correct_chirality function and additional argument iteration plus corresponding logic to handle each iteration approporiately and generate cleaner filenames --- src/postbibsnet.py | 116 ++++++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 38 deletions(-) diff --git a/src/postbibsnet.py b/src/postbibsnet.py index f91df39..9c7460e 100755 --- a/src/postbibsnet.py +++ b/src/postbibsnet.py @@ -170,15 +170,15 @@ def run_correct_chirality(crude_l_r_mask_nifti_fpath, l_r_mask_nifti_fpath, j_ar LOGGER.info(msg.format("Now")) nii_fpaths = correct_chirality( seg_BIBSnet_outfiles[0], segment_lookup_table_path, - crude_l_r_mask_nifti_fpath, chiral_out_dir + crude_l_r_mask_nifti_fpath, chiral_out_dir, 1 ) # Run chirality correction a second time using the refined LR mask generated from registration with template files applied to the segmentation corrected with the crude LR mask - msg = "{} running chirality correction on " + nii_fpaths["corrected"] + msg = "{} running chirality correction on " + nii_fpaths["crudecorrected"] LOGGER.info(msg.format("Now")) nii_fpaths = correct_chirality( - nii_fpaths["corrected"], segment_lookup_table_path, - l_r_mask_nifti_fpath, chiral_out_dir + nii_fpaths["crudecorrected"], segment_lookup_table_path, + l_r_mask_nifti_fpath, chiral_out_dir, 2 ) LOGGER.info(msg.format("Finished")) @@ -340,7 +340,7 @@ def copy_to_derivatives_dir(file_to_copy, derivs_dir, sub_ses, space, new_fname_ def correct_chirality(nifti_input_file_path, segment_lookup_table, - nii_fpath_LR_mask, chiral_out_dir): + nii_fpath_LR_mask, chiral_out_dir, iteration): """ Creates an output file with chirality corrections fixed. :param nifti_input_file_path: String, path to a segmentation file with @@ -351,42 +351,82 @@ def correct_chirality(nifti_input_file_path, segment_lookup_table, :param xfm_ref_img: String, path to (T1w, unless running in T2w-only mode) image to use as a reference when applying transform :param j_args: Dictionary containing all args + :param iteration: either 1 or 2 for iteration1 or iteration2 of chirality correction :return: Dict with paths to native and chirality-corrected images """ - nifti_file_paths = dict() - for which_nii in ("native-T1", "native-T2", "corrected"): - nifti_file_paths[which_nii] = os.path.join(chiral_out_dir, "_".join(( - which_nii, os.path.basename(nifti_input_file_path) - ))) + if iteration==1: + nifti_file_paths = dict() + for which_nii in ("native-T1", "native-T2", "crudecorrected"): + nifti_file_paths[which_nii] = os.path.join(chiral_out_dir, "_".join(( + which_nii, os.path.basename(nifti_input_file_path) + ))) + + free_surfer_label_to_region = get_id_to_region_mapping(segment_lookup_table) + segment_name_to_number = {v: k for k, v in free_surfer_label_to_region.items()} + img = nib.load(nifti_input_file_path) + data = img.get_data() + left_right_img = nib.load(nii_fpath_LR_mask) + left_right_data = left_right_img.get_data() + + new_data = data.copy() + data_shape = img.header.get_data_shape() + left_right_data_shape = left_right_img.header.get_data_shape() + width = data_shape[0] + height = data_shape[1] + depth = data_shape[2] + assert \ + width == left_right_data_shape[0] and height == left_right_data_shape[1] and depth == left_right_data_shape[2] + for i in range(width): + for j in range(height): + for k in range(depth): + voxel = data[i][j][k] + region = free_surfer_label_to_region[voxel] + chirality_voxel = int(left_right_data[i][j][k]) + if not (region.startswith(LEFT) or region.startswith(RIGHT)): + continue + if chirality_voxel == CHIRALITY_CONST["LEFT"] or chirality_voxel == CHIRALITY_CONST["RIGHT"]: + check_and_correct_region( + chirality_voxel == CHIRALITY_CONST["LEFT"], region, segment_name_to_number, new_data, i, j, k) + fixed_img = nib.Nifti1Image(new_data, img.affine, img.header) + nib.save(fixed_img, nifti_file_paths["crudecorrected"]) + + elif iteration==2: + # Drop "crudecorrected_" from nifti_input_file_path to make filenames cleaner + nifti_input_file_path_mod=(os.path.basename(nifti_input_file_path)).split('_', 1)[1] + nifti_file_paths = dict() + for which_nii in ("native-T1", "native-T2", "corrected"): + nifti_file_paths[which_nii] = os.path.join(chiral_out_dir, "_".join(( + which_nii, nifti_input_file_path_mod + ))) - free_surfer_label_to_region = get_id_to_region_mapping(segment_lookup_table) - segment_name_to_number = {v: k for k, v in free_surfer_label_to_region.items()} - img = nib.load(nifti_input_file_path) - data = img.get_data() - left_right_img = nib.load(nii_fpath_LR_mask) - left_right_data = left_right_img.get_data() - - new_data = data.copy() - data_shape = img.header.get_data_shape() - left_right_data_shape = left_right_img.header.get_data_shape() - width = data_shape[0] - height = data_shape[1] - depth = data_shape[2] - assert \ - width == left_right_data_shape[0] and height == left_right_data_shape[1] and depth == left_right_data_shape[2] - for i in range(width): - for j in range(height): - for k in range(depth): - voxel = data[i][j][k] - region = free_surfer_label_to_region[voxel] - chirality_voxel = int(left_right_data[i][j][k]) - if not (region.startswith(LEFT) or region.startswith(RIGHT)): - continue - if chirality_voxel == CHIRALITY_CONST["LEFT"] or chirality_voxel == CHIRALITY_CONST["RIGHT"]: - check_and_correct_region( - chirality_voxel == CHIRALITY_CONST["LEFT"], region, segment_name_to_number, new_data, i, j, k) - fixed_img = nib.Nifti1Image(new_data, img.affine, img.header) - nib.save(fixed_img, nifti_file_paths["corrected"]) + free_surfer_label_to_region = get_id_to_region_mapping(segment_lookup_table) + segment_name_to_number = {v: k for k, v in free_surfer_label_to_region.items()} + img = nib.load(nifti_input_file_path) + data = img.get_data() + left_right_img = nib.load(nii_fpath_LR_mask) + left_right_data = left_right_img.get_data() + + new_data = data.copy() + data_shape = img.header.get_data_shape() + left_right_data_shape = left_right_img.header.get_data_shape() + width = data_shape[0] + height = data_shape[1] + depth = data_shape[2] + assert \ + width == left_right_data_shape[0] and height == left_right_data_shape[1] and depth == left_right_data_shape[2] + for i in range(width): + for j in range(height): + for k in range(depth): + voxel = data[i][j][k] + region = free_surfer_label_to_region[voxel] + chirality_voxel = int(left_right_data[i][j][k]) + if not (region.startswith(LEFT) or region.startswith(RIGHT)): + continue + if chirality_voxel == CHIRALITY_CONST["LEFT"] or chirality_voxel == CHIRALITY_CONST["RIGHT"]: + check_and_correct_region( + chirality_voxel == CHIRALITY_CONST["LEFT"], region, segment_name_to_number, new_data, i, j, k) + fixed_img = nib.Nifti1Image(new_data, img.affine, img.header) + nib.save(fixed_img, nifti_file_paths["corrected"]) return nifti_file_paths