Skip to content

Commit

Permalink
added intermediate crudecorrected aseg filepath in correct_chirality …
Browse files Browse the repository at this point in the history
…function and additional argument iteration plus corresponding logic to handle each iteration approporiately and generate cleaner filenames
  • Loading branch information
LuciMoore committed Feb 15, 2024
1 parent a0d92f5 commit 0a80704
Showing 1 changed file with 78 additions and 38 deletions.
116 changes: 78 additions & 38 deletions src/postbibsnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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
Expand All @@ -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


Expand Down

0 comments on commit 0a80704

Please sign in to comment.