Skip to content

Commit

Permalink
add deformation field
Browse files Browse the repository at this point in the history
  • Loading branch information
adamltyson committed Jul 13, 2020
1 parent b4a0291 commit 7e07d8a
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 146 deletions.
File renamed without changes.
123 changes: 123 additions & 0 deletions brainreg/backend/niftyreg/paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os
from imlib.general.system import ensure_directory_exists


class NiftyRegPaths:
def __init__(self, niftyreg_directory):
self.niftyreg_directory = niftyreg_directory
ensure_directory_exists(self.niftyreg_directory)
self.niftyreg_process_directory = os.path.join(
niftyreg_directory, "process"
)
ensure_directory_exists(self.niftyreg_process_directory)
self.make_reg_paths()

def make_reg_paths(self):
self.downsampled_brain = self.make_reg_path("downsampled.nii")
self.downsampled_brain_standard_space = self.make_reg_path(
"downsampled_standard.nii"
)

self.brain_filtered = self.make_reg_path("brain_filtered.nii")

self.hemispheres = self.make_reg_path("hemispheres.nii")

self.annotations = self.make_reg_path("annotations.nii")

self.downsampled_filtered = self.make_reg_path(
"downsampled_filtered.nii"
)
self.registered_atlas_path = self.make_reg_path("registered_atlas.nii")
self.hemispheres_atlas_path = self.make_reg_path(
"registered_hemispheres.nii"
)

self.affine_registered_atlas_brain_path = self.make_reg_path(
"affine_registered_atlas_brain.nii"
)
self.freeform_registered_atlas_brain_path = self.make_reg_path(
"freeform_registered_atlas_brain.nii"
)
self.inverse_freeform_registered_atlas_brain_path = self.make_reg_path(
"inverse_freeform_registered_brain.nii"
)

self.registered_atlas_img_path = self.make_reg_path(
"registered_atlas.nii"
)
self.registered_hemispheres_img_path = self.make_reg_path(
"registered_hemispheres.nii"
)

self.affine_matrix_path = self.make_reg_path("affine_matrix.txt")
self.invert_affine_matrix_path = self.make_reg_path(
"invert_affine_matrix.txt"
)

self.control_point_file_path = self.make_reg_path(
"control_point_file.nii"
)
self.inverse_control_point_file_path = self.make_reg_path(
"inverse_control_point_file.nii"
)

self.deformation_field = self.make_reg_path("deformation_field.nii")
(
self.deformation_log_file_path,
self.deformation_error_file_path,
) = self.compute_reg_log_file_paths("deformation")

(
self.affine_log_file_path,
self.affine_error_path,
) = self.compute_reg_log_file_paths("affine")
(
self.freeform_log_file_path,
self.freeform_error_file_path,
) = self.compute_reg_log_file_paths("freeform")
(
self.inverse_freeform_log_file_path,
self.inverse_freeform_error_file_path,
) = self.compute_reg_log_file_paths("inverse_freeform")
(
self.segmentation_log_file,
self.segmentation_error_file,
) = self.compute_reg_log_file_paths("segment")
(
self.inverse_transform_log_file,
self.inverse_transform_error_file,
) = self.compute_reg_log_file_paths("inverse_transform")
(
self.invert_affine_log_file,
self.invert_affine_error_file,
) = self.compute_reg_log_file_paths("invert_affine")

def make_reg_path(self, basename):
"""
Compute the absolute path of the destination file to
self.registration_output_folder.
:param str basename:
:return: The path
:rtype: str
"""
return os.path.join(self.niftyreg_process_directory, basename)

def compute_reg_log_file_paths(self, basename):
"""
Compute the path of the log and err file for the step corresponding
to basename
:param str basename:
:return: log_file_path, error_file_path
"""

log_file_template = os.path.join(
self.niftyreg_process_directory, "{}.log"
)
error_file_template = os.path.join(
self.niftyreg_process_directory, "{}.err"
)
log_file_path = log_file_template.format(basename)
error_file_path = error_file_template.format(basename)
return log_file_path, error_file_path
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(
else:
self.n_processes = None

self.dataset_img_path = paths.tmp__downsampled_filtered
self.dataset_img_path = paths.downsampled_filtered
self.brain_of_atlas_img_path = paths.brain_filtered
self.atlas_img_path = paths.annotations
self.hemispheres_img_path = paths.hemispheres
Expand All @@ -44,7 +44,7 @@ def _prepare_affine_reg_cmd(self):
self.brain_of_atlas_img_path,
self.dataset_img_path,
self.paths.affine_matrix_path,
self.paths.tmp__affine_registered_atlas_brain_path,
self.paths.affine_registered_atlas_brain_path,
)

if self.n_processes is not None:
Expand All @@ -63,8 +63,8 @@ def register_affine(self):
try:
safe_execute_command(
self._prepare_affine_reg_cmd(),
self.paths.tmp__affine_log_file_path,
self.paths.tmp__affine_error_path,
self.paths.affine_log_file_path,
self.paths.affine_error_path,
)
except SafeExecuteCommandError as err:
raise RegistrationError(
Expand All @@ -79,7 +79,7 @@ def _prepare_freeform_reg_cmd(self):
self.brain_of_atlas_img_path,
self.dataset_img_path,
self.paths.control_point_file_path,
self.paths.tmp__freeform_registered_atlas_brain_path,
self.paths.freeform_registered_atlas_brain_path,
)

if self.n_processes is not None:
Expand All @@ -98,8 +98,8 @@ def register_freeform(self):
try:
safe_execute_command(
self._prepare_freeform_reg_cmd(),
self.paths.tmp__freeform_log_file_path,
self.paths.tmp__freeform_error_file_path,
self.paths.freeform_log_file_path,
self.paths.freeform_error_file_path,
)
except SafeExecuteCommandError as err:
raise RegistrationError(
Expand Down Expand Up @@ -131,8 +131,8 @@ def generate_inverse_affine(self):
try:
safe_execute_command(
self._prepare_invert_affine_cmd(),
self.paths.tmp__invert_affine_log_file,
self.paths.tmp__invert_affine_error_file,
self.paths.invert_affine_log_file,
self.paths.invert_affine_error_file,
)
except SafeExecuteCommandError as err:
raise RegistrationError(
Expand All @@ -148,7 +148,7 @@ def _prepare_inverse_freeform_reg_cmd(self):
self.dataset_img_path,
self.brain_of_atlas_img_path,
self.paths.inverse_control_point_file_path,
self.paths.tmp__inverse_freeform_registered_atlas_brain_path,
self.paths.inverse_freeform_registered_atlas_brain_path,
)

if self.n_processes is not None:
Expand All @@ -169,8 +169,8 @@ def register_inverse_freeform(self):
try:
safe_execute_command(
self._prepare_inverse_freeform_reg_cmd(),
self.paths.tmp__inverse_freeform_log_file_path,
self.paths.tmp__inverse_freeform_error_file_path,
self.paths.inverse_freeform_log_file_path,
self.paths.inverse_freeform_error_file_path,
)
except SafeExecuteCommandError as err:
raise RegistrationError(
Expand Down Expand Up @@ -201,6 +201,15 @@ def _prepare_inverse_registration_cmd(
)
return cmd

def _prepare_deformation_field_cmd(self, deformation_field_path):
cmd = "{} -def {} {} -ref {}".format(
self.reg_params.transform_program_path,
self.paths.control_point_file_path,
deformation_field_path,
self.paths.downsampled_filtered,
)
return cmd

def segment(self):
"""
Registers the atlas to the sample (propagates the transformation
Expand All @@ -216,8 +225,8 @@ def segment(self):
self._prepare_segmentation_cmd(
self.atlas_img_path, self.paths.registered_atlas_img_path
),
self.paths.tmp__segmentation_log_file,
self.paths.tmp__segmentation_error_file,
self.paths.segmentation_log_file,
self.paths.segmentation_error_file,
)
except SafeExecuteCommandError as err:
SegmentationError("Segmentation failed; {}".format(err))
Expand All @@ -238,8 +247,8 @@ def register_hemispheres(self):
self.hemispheres_img_path,
self.paths.registered_hemispheres_img_path,
),
self.paths.tmp__segmentation_log_file,
self.paths.tmp__segmentation_error_file,
self.paths.segmentation_log_file,
self.paths.segmentation_error_file,
)
except SafeExecuteCommandError as err:
SegmentationError("Segmentation failed; {}".format(err))
Expand All @@ -254,10 +263,23 @@ def transform_to_standard_space(self, image_path, destination_path):
self._prepare_inverse_registration_cmd(
image_path, destination_path,
),
self.paths.tmp__inverse_transform_log_file,
self.paths.tmp__inverse_transform_error_file,
self.paths.inverse_transform_log_file,
self.paths.inverse_transform_error_file,
)
except SafeExecuteCommandError as err:
raise TransformationError(
"Reverse transformation failed; {}".format(err)
)

def generate_deformation_field(self, deformation_field_path):
logging.info("Generating deformation field")
try:
safe_execute_command(
self._prepare_deformation_field_cmd(deformation_field_path),
self.paths.deformation_log_file_path,
self.paths.deformation_error_file_path,
)
except SafeExecuteCommandError as err:
raise TransformationError(
"Generation of deformation field failed ; {}".format(err)
)
28 changes: 17 additions & 11 deletions brainreg/backend/niftyreg/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import bg_space as bg
import imio

from imlib.general.system import ensure_directory_exists


from brainreg.utils import preprocess
from brainreg.paths import NiftyRegPaths
from brainreg.backend.niftyreg.brain_registration import BrainRegistration
from brainreg.backend.niftyreg.registration_params import RegistrationParams
from brainreg.backend.niftyreg.paths import NiftyRegPaths
from brainreg.backend.niftyreg.registration import BrainRegistration
from brainreg.backend.niftyreg.parameters import RegistrationParams
from brainreg.backend.niftyreg.utils import save_nii


Expand All @@ -36,7 +34,7 @@ def run_niftyreg(
):

niftyreg_directory = os.path.join(registration_output_folder, "niftyreg")
ensure_directory_exists(niftyreg_directory)

niftyreg_paths = NiftyRegPaths(niftyreg_directory)

save_nii(hemispheres, atlas_pixel_sizes, niftyreg_paths.hemispheres)
Expand All @@ -51,9 +49,7 @@ def run_niftyreg(

target_brain = preprocess.filter_image(target_brain)
save_nii(
target_brain,
atlas_pixel_sizes,
niftyreg_paths.tmp__downsampled_filtered,
target_brain, atlas_pixel_sizes, niftyreg_paths.downsampled_filtered,
)

logging.info("Registering")
Expand Down Expand Up @@ -95,6 +91,9 @@ def run_niftyreg(
niftyreg_paths.downsampled_brain_standard_space,
)

logging.info("Generating deformation field")
brain_reg.generate_deformation_field(niftyreg_paths.deformation_field)

logging.info("Exporting images as tiff")
imio.to_tiff(
imio.load_any(niftyreg_paths.registered_atlas_path),
Expand All @@ -109,6 +108,11 @@ def run_niftyreg(
paths.downsampled_brain_standard_space,
)

deformation_image = imio.load_any(niftyreg_paths.deformation_field)
imio.to_tiff(deformation_image[..., 0], paths.deformation_field_0)
imio.to_tiff(deformation_image[..., 1], paths.deformation_field_1)
imio.to_tiff(deformation_image[..., 2], paths.deformation_field_2)

if additional_images_downsample:
logging.info("Saving additional downsampled images")
for name, image in additional_images_downsample.items():
Expand All @@ -118,10 +122,12 @@ def run_niftyreg(
registration_output_folder, f"downsampled_{name}.tiff"
)
tmp_downsampled_brain_path = os.path.join(
niftyreg_directory, f"downsampled_{name}.nii"
niftyreg_paths.niftyreg_process_directory,
f"downsampled_{name}.nii",
)
tmp_downsampled_brain_standard_path = os.path.join(
niftyreg_directory, f"downsampled_standard_{name}.nii"
niftyreg_paths.niftyreg_process_directory,
f"downsampled_standard_{name}.nii",
)
downsampled_brain_standard_path = os.path.join(
registration_output_folder, f"downsampled_standard_{name}.tiff"
Expand Down
8 changes: 4 additions & 4 deletions brainreg/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def cli_parse(parser):
)

cli_parser.add_argument(
dest="registration_output_folder",
dest="brainreg_directory",
type=str,
help="Directory to save the output",
)
Expand Down Expand Up @@ -176,7 +176,7 @@ def geometry_parser(parser):

def prep_registration(args):
logging.debug("Making registration directory")
ensure_directory_exists(args.registration_output_folder)
ensure_directory_exists(args.brainreg_directory)

additional_images_downsample = {}
if args.downsample_images:
Expand Down Expand Up @@ -217,7 +217,7 @@ def run():
args, additional_images_downsample = prep_registration(args)

fancylog.start_logging(
args.registration_output_folder,
args.brainreg_directory,
program_for_log,
variables=[args],
verbose=args.debug,
Expand All @@ -237,7 +237,7 @@ def run():
atlas_orientation,
args.orientation,
args.image_paths,
args.registration_output_folder,
args.brainreg_directory,
arg_groups["NiftyReg registration backend options"],
x_pixel_um=args.x_pixel_um,
y_pixel_um=args.y_pixel_um,
Expand Down
Loading

0 comments on commit 7e07d8a

Please sign in to comment.