From 4c903b363e5dbcaee23c8f518e3b7fb747b9b915 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Tue, 12 Dec 2023 10:00:32 +0100 Subject: [PATCH 1/9] hdbet test Signed-off-by: neuronflow --- .../{test_brain_extractor.py => test_hdbet_brain_extractor.py} | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) rename tests/{test_brain_extractor.py => test_hdbet_brain_extractor.py} (97%) diff --git a/tests/test_brain_extractor.py b/tests/test_hdbet_brain_extractor.py similarity index 97% rename from tests/test_brain_extractor.py rename to tests/test_hdbet_brain_extractor.py index 85c963a..3dfc446 100644 --- a/tests/test_brain_extractor.py +++ b/tests/test_hdbet_brain_extractor.py @@ -11,7 +11,7 @@ class TestHDBetExtractor(unittest.TestCase): def setUp(self): test_data_dir = turbopath(__file__).parent + "/test_data" input_dir = test_data_dir + "/input" - self.output_dir = test_data_dir + "/temp_output" + self.output_dir = test_data_dir + "/temp_output_hdbet" os.makedirs(self.output_dir, exist_ok=True) self.brain_extractor = HDBetExtractor() @@ -30,7 +30,6 @@ def tearDown(self): # Clean up created files if they exist shutil.rmtree(self.output_dir) - def test_extract_creates_output_files(self): # we try to run the fastest possible skullstripping on GPU self.brain_extractor.extract( From 1db1a30c745ff73d6c767c2ab1f67c88cec66a94 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Tue, 12 Dec 2023 10:05:45 +0100 Subject: [PATCH 2/9] nicer format Signed-off-by: neuronflow --- tests/test_hdbet_brain_extractor.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_hdbet_brain_extractor.py b/tests/test_hdbet_brain_extractor.py index 3dfc446..bed1e7a 100644 --- a/tests/test_hdbet_brain_extractor.py +++ b/tests/test_hdbet_brain_extractor.py @@ -15,8 +15,10 @@ def setUp(self): os.makedirs(self.output_dir, exist_ok=True) self.brain_extractor = HDBetExtractor() + self.input_image_path = input_dir + "/tcia_example_t1c.nii.gz" self.input_brain_mask_path = input_dir + "/bet_tcia_example_t1c_mask.nii.gz" + self.masked_image_path = self.output_dir + "/bet_tcia_example_t1c.nii.gz" self.brain_mask_path = self.output_dir + "/bet_tcia_example_t1c_mask.nii.gz" self.masked_again_image_path = ( From 1742c180fad8ca6ae5546d45cbe6961c0ba9c44a Mon Sep 17 00:00:00 2001 From: neuronflow Date: Tue, 12 Dec 2023 10:21:44 +0100 Subject: [PATCH 3/9] typo Signed-off-by: neuronflow --- tests/test_hdbet_brain_extractor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_hdbet_brain_extractor.py b/tests/test_hdbet_brain_extractor.py index bed1e7a..a79d2a5 100644 --- a/tests/test_hdbet_brain_extractor.py +++ b/tests/test_hdbet_brain_extractor.py @@ -33,7 +33,7 @@ def tearDown(self): shutil.rmtree(self.output_dir) def test_extract_creates_output_files(self): - # we try to run the fastest possible skullstripping on GPU + # we try to run the fastest possible skullstripping on CPU self.brain_extractor.extract( input_image_path=self.input_image_path, masked_image_path=self.masked_image_path, From 63c1340200246c3c2e51f54910e6c500a0c39043 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Tue, 12 Dec 2023 10:30:58 +0100 Subject: [PATCH 4/9] introduce typing Signed-off-by: neuronflow --- .../registration/niftyreg.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/brainles_preprocessing/registration/niftyreg.py b/brainles_preprocessing/registration/niftyreg.py index 9516d4d..73fac0b 100644 --- a/brainles_preprocessing/registration/niftyreg.py +++ b/brainles_preprocessing/registration/niftyreg.py @@ -42,11 +42,11 @@ def __init__( def register( self, - fixed_image, - moving_image, - transformed_image, - matrix, - log_file, + fixed_image: str, + moving_image: str, + transformed_image: str, + matrix: str, + log_file: str, ): """ Register images using NiftyReg. @@ -85,11 +85,11 @@ def register( def transform( self, - fixed_image, - moving_image, - transformed_image, - matrix, - log_file, + fixed_image: str, + moving_image: str, + transformed_image: str, + matrix: str, + log_file: str, ): """ Apply a transformation using NiftyReg. From 65f5076f698f6bfbbc9797b5a38bd2c9932a0b66 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Tue, 12 Dec 2023 10:35:38 +0100 Subject: [PATCH 5/9] new: add todo Signed-off-by: neuronflow --- brainles_preprocessing/registration/registrator.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/brainles_preprocessing/registration/registrator.py b/brainles_preprocessing/registration/registrator.py index 95e8f23..8fa8901 100644 --- a/brainles_preprocessing/registration/registrator.py +++ b/brainles_preprocessing/registration/registrator.py @@ -3,8 +3,9 @@ class Registrator(ABC): - def __init__(self, backend): - self.backend = backend + # TODO probably the init here should be removed? + # def __init__(self, backend): + # self.backend = backend @abstractmethod def register( From 551c7c50a268810e57189fede6281344916ab0a4 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Tue, 12 Dec 2023 10:38:04 +0100 Subject: [PATCH 6/9] new: with comments Signed-off-by: neuronflow --- .../registration/registrator.py | 52 ++++++++++++++----- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/brainles_preprocessing/registration/registrator.py b/brainles_preprocessing/registration/registrator.py index 8fa8901..43377c7 100644 --- a/brainles_preprocessing/registration/registrator.py +++ b/brainles_preprocessing/registration/registrator.py @@ -1,5 +1,5 @@ -# TODO add typing and docs from abc import ABC, abstractmethod +from typing import Any class Registrator(ABC): @@ -10,21 +10,47 @@ class Registrator(ABC): @abstractmethod def register( self, - fixed_image, - moving_image, - transformed_image, - matrix, - log_file, - ): + fixed_image: Any, + moving_image: Any, + transformed_image: Any, + matrix: Any, + log_file: str, + ) -> None: + """ + Abstract method for registering images. + + Args: + fixed_image (Any): The fixed image for registration. + moving_image (Any): The moving image to be registered. + transformed_image (Any): The resulting transformed image after registration. + matrix (Any): The transformation matrix applied during registration. + log_file (str): The path to the log file for recording registration details. + + Returns: + None + """ pass @abstractmethod def transform( self, - fixed_image, - moving_image, - transformed_image, - matrix, - log_file, - ): + fixed_image: Any, + moving_image: Any, + transformed_image: Any, + matrix: Any, + log_file: str, + ) -> None: + """ + Abstract method for transforming images. + + Args: + fixed_image (Any): The fixed image to be transformed. + moving_image (Any): The moving image to be transformed. + transformed_image (Any): The resulting transformed image. + matrix (Any): The transformation matrix applied during transformation. + log_file (str): The path to the log file for recording transformation details. + + Returns: + None + """ pass From 86f08f9444b108ea6778161be3259b4e3c767d25 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Tue, 12 Dec 2023 10:50:17 +0100 Subject: [PATCH 7/9] introduce _path as in the bet Signed-off-by: neuronflow --- brainles_preprocessing/modality.py | 18 +++--- .../registration/niftyreg.py | 60 +++++++++---------- .../registration/registrator.py | 36 +++++------ 3 files changed, 57 insertions(+), 57 deletions(-) diff --git a/brainles_preprocessing/modality.py b/brainles_preprocessing/modality.py index 1131cea..0fa7b54 100644 --- a/brainles_preprocessing/modality.py +++ b/brainles_preprocessing/modality.py @@ -94,11 +94,11 @@ def register( registered_log = os.path.join(registration_dir, f"{moving_image_name}.log") registrator.register( - fixed_image=fixed_image_path, - moving_image=self.current, + fixed_image_path=fixed_image_path, + moving_image_path=self.current, transformed_image=registered, - matrix=registered_matrix, - log_file=registered_log, + matrix_path=registered_matrix, + log_file_path=registered_log, ) self.current = registered return registered_matrix @@ -133,11 +133,11 @@ def transform( transformed_log = os.path.join(registration_dir, f"{moving_image_name}.log") registrator.transform( - fixed_image=fixed_image_path, - moving_image=self.current, - transformed_image=transformed, - matrix=transformation_matrix, - log_file=transformed_log, + fixed_image_path=fixed_image_path, + moving_image_path=self.current, + transformed_image_path=transformed, + matrix_path=transformation_matrix, + log_file_path=transformed_log, ) self.current = transformed diff --git a/brainles_preprocessing/registration/niftyreg.py b/brainles_preprocessing/registration/niftyreg.py index 73fac0b..c702c90 100644 --- a/brainles_preprocessing/registration/niftyreg.py +++ b/brainles_preprocessing/registration/niftyreg.py @@ -42,25 +42,25 @@ def __init__( def register( self, - fixed_image: str, - moving_image: str, - transformed_image: str, - matrix: str, - log_file: str, + fixed_image_path: str, + moving_image_path: str, + transformed_image_path: str, + matrix_path: str, + log_file_path: str, ): """ Register images using NiftyReg. Args: - fixed_image (str): Path to the fixed image. - moving_image (str): Path to the moving image. - transformed_image (str): Path to the transformed image (output). - matrix (str): Path to the transformation matrix (output). - log_file (str): Path to the log file. + fixed_image_path (str): Path to the fixed image. + moving_image_path (str): Path to the moving image. + transformed_image_path (str): Path to the transformed image (output). + matrix_path (str): Path to the transformation matrix (output). + log_file_path (str): Path to the log file. """ runner = ScriptRunner( script_path=self.registration_script, - log_path=log_file, + log_path=log_file_path, ) niftyreg_executable = str( @@ -69,10 +69,10 @@ def register( input_params = [ turbopath(niftyreg_executable), - turbopath(fixed_image), - turbopath(moving_image), - turbopath(transformed_image), - turbopath(matrix), + turbopath(fixed_image_path), + turbopath(moving_image_path), + turbopath(transformed_image_path), + turbopath(matrix_path), ] # Call the run method to execute the script and capture the output in the log file @@ -85,25 +85,25 @@ def register( def transform( self, - fixed_image: str, - moving_image: str, - transformed_image: str, - matrix: str, - log_file: str, + fixed_image_path: str, + moving_image_path: str, + transformed_image_path: str, + matrix_path: str, + log_file_path: str, ): """ Apply a transformation using NiftyReg. Args: - fixed_image (str): Path to the fixed image. - moving_image (str): Path to the moving image. - transformed_image (str): Path to the transformed image (output). - matrix (str): Path to the transformation matrix. - log_file (str): Path to the log file. + fixed_image_path (str): Path to the fixed image. + moving_image_path (str): Path to the moving image. + transformed_image_path (str): Path to the transformed image (output). + matrix_path (str): Path to the transformation matrix. + log_file_path (str): Path to the log file. """ runner = ScriptRunner( script_path=self.transformation_script, - log_path=log_file, + log_path=log_file_path, ) niftyreg_executable = str( @@ -112,10 +112,10 @@ def transform( input_params = [ turbopath(niftyreg_executable), - turbopath(fixed_image), - turbopath(moving_image), - turbopath(transformed_image), - turbopath(matrix), + turbopath(fixed_image_path), + turbopath(moving_image_path), + turbopath(transformed_image_path), + turbopath(matrix_path), ] # Call the run method to execute the script and capture the output in the log file diff --git a/brainles_preprocessing/registration/registrator.py b/brainles_preprocessing/registration/registrator.py index 43377c7..56fb749 100644 --- a/brainles_preprocessing/registration/registrator.py +++ b/brainles_preprocessing/registration/registrator.py @@ -10,21 +10,21 @@ class Registrator(ABC): @abstractmethod def register( self, - fixed_image: Any, - moving_image: Any, - transformed_image: Any, - matrix: Any, - log_file: str, + fixed_image_path: Any, + moving_image_path: Any, + transformed_image_path: Any, + matrix_path: Any, + log_file_path: str, ) -> None: """ Abstract method for registering images. Args: - fixed_image (Any): The fixed image for registration. - moving_image (Any): The moving image to be registered. - transformed_image (Any): The resulting transformed image after registration. - matrix (Any): The transformation matrix applied during registration. - log_file (str): The path to the log file for recording registration details. + fixed_image_path (Any): The fixed image for registration. + moving_image_path (Any): The moving image to be registered. + transformed_image_path (Any): The resulting transformed image after registration. + matrix_path (Any): The transformation matrix applied during registration. + log_file_path (str): The path to the log file for recording registration details. Returns: None @@ -34,9 +34,9 @@ def register( @abstractmethod def transform( self, - fixed_image: Any, - moving_image: Any, - transformed_image: Any, + fixed_image_path: Any, + moving_image_path: Any, + transformed_image_path: Any, matrix: Any, log_file: str, ) -> None: @@ -44,11 +44,11 @@ def transform( Abstract method for transforming images. Args: - fixed_image (Any): The fixed image to be transformed. - moving_image (Any): The moving image to be transformed. - transformed_image (Any): The resulting transformed image. - matrix (Any): The transformation matrix applied during transformation. - log_file (str): The path to the log file for recording transformation details. + fixed_image_path (Any): The fixed image to be transformed. + moving_image_path (Any): The moving image to be transformed. + transformed_image_path (Any): The resulting transformed image. + matrix_path (Any): The transformation matrix applied during transformation. + log_file_path (str): The path to the log file for recording transformation details. Returns: None From b457a82f866a92f77678d6a16bbe7682c6cdc5f1 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Tue, 12 Dec 2023 10:50:31 +0100 Subject: [PATCH 8/9] reg, not functional yet Signed-off-by: neuronflow --- tests/test_niftyreg_registrator.py | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/test_niftyreg_registrator.py diff --git a/tests/test_niftyreg_registrator.py b/tests/test_niftyreg_registrator.py new file mode 100644 index 0000000..a16316a --- /dev/null +++ b/tests/test_niftyreg_registrator.py @@ -0,0 +1,58 @@ +import os +import shutil +import unittest + +from auxiliary.turbopath import turbopath + +from brainles_preprocessing.registration.niftyreg import NiftyRegRegistrator + + +class TestHDBetExtractor(unittest.TestCase): + def setUp(self): + test_data_dir = turbopath(__file__).parent + "/test_data" + input_dir = test_data_dir + "/input" + self.output_dir = test_data_dir + "/temp_output_niftyreg" + os.makedirs(self.output_dir, exist_ok=True) + + self.registrator = NiftyRegRegistrator() + + self.fixed_image = input_dir + "/tcia_example_t1c.nii.gz" + self.moving_image = input_dir + "/bet_tcia_example_t1c_mask.nii.gz" + + self.transformed_image = self.output_dir + "/transformed_image.nii.gz" + self.matrix = self.output_dir + "/matrix.txt" + self.log_file = self.output_dir + "/registration.log" + + def tearDown(self): + # Clean up created files if they exist + shutil.rmtree(self.output_dir) + + def test_register_creates_output_files(self): + # we try to run the fastest possible skullstripping on GPU + self.registrator.register( + fixed_image_path=self.fixed_image, + moving_image_path=self.moving_image, + transformed_image_path=self.transformed_image, + matrix_path=self.matrix, + log_file_path=self.log_file, + ) + # TODO generate and also test for presence of log file + + self.assertTrue( + os.path.exists(self.transformed_image), "Masked image file was not created." + ) + self.assertTrue( + os.path.exists(self.brain_mask_path), + "Brain mask image file was not created.", + ) + + def test_apply_mask_creates_output_file(self): + self.brain_extractor.apply_mask( + input_image_path=self.input_image_path, + mask_image_path=self.input_brain_mask_path, + masked_image_path=self.masked_again_image_path, + ) + self.assertTrue( + os.path.exists(self.masked_again_image_path), + "Output image file was not created in apply_mask.", + ) From da440a977dcb04b402166b9c8c228e319e1626f6 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Tue, 12 Dec 2023 10:54:33 +0100 Subject: [PATCH 9/9] first working test Signed-off-by: neuronflow --- tests/test_niftyreg_registrator.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/test_niftyreg_registrator.py b/tests/test_niftyreg_registrator.py index a16316a..2315960 100644 --- a/tests/test_niftyreg_registrator.py +++ b/tests/test_niftyreg_registrator.py @@ -7,7 +7,7 @@ from brainles_preprocessing.registration.niftyreg import NiftyRegRegistrator -class TestHDBetExtractor(unittest.TestCase): +class TestNiftyRegRegistrator(unittest.TestCase): def setUp(self): test_data_dir = turbopath(__file__).parent + "/test_data" input_dir = test_data_dir + "/input" @@ -36,23 +36,21 @@ def test_register_creates_output_files(self): matrix_path=self.matrix, log_file_path=self.log_file, ) - # TODO generate and also test for presence of log file self.assertTrue( - os.path.exists(self.transformed_image), "Masked image file was not created." + os.path.exists(self.transformed_image), + "transformed file was not created.", ) + self.assertTrue( - os.path.exists(self.brain_mask_path), - "Brain mask image file was not created.", + os.path.exists(self.matrix), + "matrix file was not created.", ) - def test_apply_mask_creates_output_file(self): - self.brain_extractor.apply_mask( - input_image_path=self.input_image_path, - mask_image_path=self.input_brain_mask_path, - masked_image_path=self.masked_again_image_path, - ) self.assertTrue( - os.path.exists(self.masked_again_image_path), - "Output image file was not created in apply_mask.", + os.path.exists(self.log_file), + "log file was not created.", ) + + +# TODO also test transform