From cf38aa93b31ad0fd967a844a2e4d927d7a77a21b Mon Sep 17 00:00:00 2001 From: Marcel Rosier Date: Tue, 26 Aug 2025 10:41:50 +0200 Subject: [PATCH 1/3] Fix border issue during registration for ANTs --- .../registration/ANTs/ANTs.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/brainles_preprocessing/registration/ANTs/ANTs.py b/brainles_preprocessing/registration/ANTs/ANTs.py index a1a9ea1..85e76bb 100644 --- a/brainles_preprocessing/registration/ANTs/ANTs.py +++ b/brainles_preprocessing/registration/ANTs/ANTs.py @@ -101,24 +101,30 @@ def register( fixed_image = ants.image_read(str(fixed_image_path)) moving_image = ants.image_read(str(moving_image_path)) + registration_result = ants.registration( fixed=fixed_image, moving=moving_image, **registration_kwargs, ) - transformed_image = registration_result["warpedmovout"] # Ensure output directories exist transformed_image_path.parent.mkdir(parents=True, exist_ok=True) matrix_path.parent.mkdir(parents=True, exist_ok=True) - ants.image_write(transformed_image, str(transformed_image_path)) - shutil.copyfile( src=registration_result["fwdtransforms"][0], dst=str(matrix_path), ) + self.transform( + fixed_image_path=fixed_image_path, + moving_image_path=moving_image_path, + transformed_image_path=transformed_image_path, + matrix_path=matrix_path, + log_file_path=log_file_path, + ) + end_time = datetime.datetime.now() # TODO nicer logging @@ -195,6 +201,11 @@ def transform( fixed_image = ants.image_read(str(fixed_image_path)) moving_image = ants.image_read(str(moving_image_path)) + if "defaultvalue" not in transform_kwargs: + transform_kwargs["defaultvalue"] = ( + moving_image.min() + ) # set out of view voxels by default to min value, i.e. air/ bg + # Ensure output directory exist transformed_image_path.parent.mkdir(parents=True, exist_ok=True) From e726269eeff160bbe79334825946ddb712058262 Mon Sep 17 00:00:00 2001 From: Marcel Rosier Date: Tue, 26 Aug 2025 11:45:00 +0200 Subject: [PATCH 2/3] Add padding option to reg/ resample scripts and set value by default to min of moving img --- .../registration/niftyreg/niftyreg.py | 11 ++++++++++- .../niftyreg/niftyreg_scripts/rigid_reg.sh | 6 ++++-- .../niftyreg/niftyreg_scripts/transform.sh | 6 ++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/brainles_preprocessing/registration/niftyreg/niftyreg.py b/brainles_preprocessing/registration/niftyreg/niftyreg.py index 65ca668..8c1f19c 100644 --- a/brainles_preprocessing/registration/niftyreg/niftyreg.py +++ b/brainles_preprocessing/registration/niftyreg/niftyreg.py @@ -8,6 +8,7 @@ import numpy as np from auxiliary.runscript import ScriptRunner from auxiliary.turbopath import turbopath +from auxiliary.io import read_image from brainles_preprocessing.registration.registrator import Registrator @@ -79,12 +80,16 @@ def register( matrix_path = Path(matrix_path).with_suffix(".txt") + # read moving image to get padding value + padding_value = float(read_image(moving_image_path).min()) + input_params = [ turbopath(niftyreg_executable), turbopath(fixed_image_path), turbopath(moving_image_path), turbopath(transformed_image_path), str(matrix_path), + str(padding_value), ] # Call the run method to execute the script and capture the output in the log file @@ -199,13 +204,17 @@ def transform( else: transform_path = Path(matrix_path).with_suffix(".txt") + # read moving image to get padding value + padding_value = float(read_image(moving_image_path).min()) + input_params = [ turbopath(niftyreg_executable), turbopath(fixed_image_path), turbopath(moving_image_path), turbopath(transformed_image_path), str(transform_path), - interpolator, # interpolation method, 3 is Cubic + str(interpolator), # interpolation method, 3 is Cubic + str(padding_value), ] # Call the run method to execute the script and capture the output in the log file diff --git a/brainles_preprocessing/registration/niftyreg/niftyreg_scripts/rigid_reg.sh b/brainles_preprocessing/registration/niftyreg/niftyreg_scripts/rigid_reg.sh index b0381ea..c97cd32 100644 --- a/brainles_preprocessing/registration/niftyreg/niftyreg_scripts/rigid_reg.sh +++ b/brainles_preprocessing/registration/niftyreg/niftyreg_scripts/rigid_reg.sh @@ -10,8 +10,8 @@ file_exists() { } # Check if the correct number of arguments is provided -if [ "$#" -ne 5 ]; then - echo "Usage: $0 " +if [ "$#" -ne 6 ]; then + echo "Usage: $0 " exit 1 fi @@ -21,6 +21,7 @@ fixed_image="$2" moving_image="$3" transformed_image="$4" transformation_matrix="$5" +padding_value="$6" # Validate the existence of input files if ! file_exists "$fixed_image"; then @@ -41,6 +42,7 @@ registration_options=( "-flo" "$moving_image" "-res" "$transformed_image" "-aff" "$transformation_matrix" + "-pad" "$padding_value" ) # Perform rigid-only registration with NiftyReg diff --git a/brainles_preprocessing/registration/niftyreg/niftyreg_scripts/transform.sh b/brainles_preprocessing/registration/niftyreg/niftyreg_scripts/transform.sh index 1771dd5..85c69c9 100644 --- a/brainles_preprocessing/registration/niftyreg/niftyreg_scripts/transform.sh +++ b/brainles_preprocessing/registration/niftyreg/niftyreg_scripts/transform.sh @@ -10,8 +10,8 @@ file_exists() { } # Check if the correct number of arguments is provided -if [ "$#" -ne 6 ]; then - echo "Usage: $0 " +if [ "$#" -ne 7 ]; then + echo "Usage: $0 " exit 1 fi @@ -22,6 +22,7 @@ moving_image="$3" transformed_image="$4" transformation_matrix="$5" interpolation_method="$6" +padding_value="$7" # Validate the existence of input files if ! file_exists "$fixed_image"; then @@ -51,6 +52,7 @@ resample_command=( -trans "$transformation_matrix" -res "$transformed_image" -inter "$interpolation_method" + -pad "$padding_value" ) if file_exists "$niftyreg_path"; then From 829fc324ab670162cd2e3c184a746b6301e9725b Mon Sep 17 00:00:00 2001 From: Marcel Rosier Date: Tue, 26 Aug 2025 11:53:53 +0200 Subject: [PATCH 3/3] Add note to docstring --- brainles_preprocessing/registration/ANTs/ANTs.py | 1 + brainles_preprocessing/registration/niftyreg/niftyreg.py | 1 + 2 files changed, 2 insertions(+) diff --git a/brainles_preprocessing/registration/ANTs/ANTs.py b/brainles_preprocessing/registration/ANTs/ANTs.py index 85e76bb..fa0808e 100644 --- a/brainles_preprocessing/registration/ANTs/ANTs.py +++ b/brainles_preprocessing/registration/ANTs/ANTs.py @@ -153,6 +153,7 @@ def transform( ) -> None: """ Apply a transformation using ANTs. + By default the padding value corresponds to the minimum of the moving image. Can be adjusted using the defaultvalue parameter. Args: fixed_image_path (str or Path): Path to the fixed image. diff --git a/brainles_preprocessing/registration/niftyreg/niftyreg.py b/brainles_preprocessing/registration/niftyreg/niftyreg.py index 8c1f19c..3be4aa5 100644 --- a/brainles_preprocessing/registration/niftyreg/niftyreg.py +++ b/brainles_preprocessing/registration/niftyreg/niftyreg.py @@ -160,6 +160,7 @@ def transform( ) -> None: """ Apply a transformation using NiftyReg. + By default the padding value corresponds to the minimum of the moving image. Args: fixed_image_path (str): Path to the fixed image.