-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from BrainLesion/introduce_ereg
Introduce_ereg
- Loading branch information
Showing
15 changed files
with
223 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# TODO add typing and docs | ||
import os | ||
|
||
from ereg.registration import RegistrationClass | ||
|
||
from brainles_preprocessing.registration.registrator import Registrator | ||
|
||
|
||
class eRegRegistrator(Registrator): | ||
def __init__( | ||
self, | ||
# TODO define default | ||
configuration_file: str | None = None, | ||
): | ||
""" | ||
# TODO | ||
""" | ||
self.configuration_file = configuration_file | ||
|
||
def register( | ||
self, | ||
fixed_image_path: str, | ||
moving_image_path: str, | ||
transformed_image_path: str, | ||
matrix_path: str, | ||
log_file_path: str = None, | ||
) -> None: | ||
""" | ||
Register images using eReg. | ||
Args: | ||
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. | ||
""" | ||
# TODO do we need to handle kwargs? | ||
registrator = RegistrationClass( | ||
configuration_file=self.configuration_file, | ||
) | ||
|
||
matrix_path = _add_mat_suffix(matrix_path) | ||
|
||
registrator.register( | ||
target_image=fixed_image_path, | ||
moving_image=moving_image_path, | ||
output_image=transformed_image_path, | ||
transform_file=matrix_path, | ||
log_file=log_file_path, | ||
) | ||
|
||
def transform( | ||
self, | ||
fixed_image_path: str, | ||
moving_image_path: str, | ||
transformed_image_path: str, | ||
matrix_path: str, | ||
log_file_path: str = None, | ||
) -> None: | ||
""" | ||
Apply a transformation using eReg. | ||
Args: | ||
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. | ||
""" | ||
# TODO do we need to handle kwargs? | ||
registrator = RegistrationClass( | ||
configuration_file=self.configuration_file, | ||
) | ||
|
||
matrix_path = _add_mat_suffix(matrix_path) | ||
|
||
registrator.resample_image( | ||
target_image=fixed_image_path, | ||
moving_image=moving_image_path, | ||
output_image=transformed_image_path, | ||
transform_file=matrix_path, | ||
log_file=log_file_path, | ||
) | ||
|
||
|
||
def _add_mat_suffix(filename: str) -> str: | ||
""" | ||
Adds a ".mat" suffix to the filename if it doesn't have any extension. | ||
Parameters: | ||
filename (str): The filename to check and potentially modify. | ||
Returns: | ||
str: The filename with ".mat" suffix added if needed. | ||
""" | ||
base, ext = os.path.splitext(filename) | ||
if not ext: | ||
filename += ".mat" | ||
return filename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# TODO add typing and docs | ||
import os | ||
|
||
from auxiliary.runscript import ScriptRunner | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import os | ||
from abc import abstractmethod | ||
|
||
from auxiliary.turbopath import turbopath | ||
import shutil | ||
|
||
|
||
class RegistratorBase: | ||
|
||
@abstractmethod | ||
def get_registrator(self): | ||
pass | ||
|
||
@abstractmethod | ||
def get_method_and_extension(self): | ||
pass | ||
|
||
def setUp(self): | ||
self.registrator = self.get_registrator() | ||
self.method_name, self.matrix_extension = self.get_method_and_extension() | ||
|
||
test_data_dir = turbopath(__file__).parent + "/test_data" | ||
input_dir = test_data_dir + "/input" | ||
self.output_dir = test_data_dir + f"/temp_output_{self.method_name}" | ||
os.makedirs(self.output_dir, exist_ok=True) | ||
|
||
self.fixed_image = input_dir + "/tcia_example_t1c.nii.gz" | ||
self.moving_image = input_dir + "/tcia_example_t1.nii.gz" | ||
|
||
self.matrix = self.output_dir + f"/{self.method_name}_matrix" | ||
self.transform_matrix = input_dir + f"/{self.method_name}_matrix" | ||
|
||
def tearDown(self): | ||
# Clean up created files if they exist | ||
shutil.rmtree(self.output_dir) | ||
# pass | ||
|
||
def test_register_creates_output_files(self): | ||
transformed_image = ( | ||
self.output_dir + f"/{self.method_name}_registered_image.nii.gz" | ||
) | ||
log_file = self.output_dir + f"/{self.method_name}_registration.log" | ||
|
||
self.registrator.register( | ||
fixed_image_path=self.fixed_image, | ||
moving_image_path=self.moving_image, | ||
transformed_image_path=transformed_image, | ||
matrix_path=self.matrix, | ||
log_file_path=log_file, | ||
) | ||
|
||
self.assertTrue( | ||
os.path.exists(transformed_image), | ||
"transformed file was not created.", | ||
) | ||
|
||
self.assertTrue( | ||
os.path.exists(f"{self.matrix}.{self.matrix_extension}"), | ||
"matrix file was not created.", | ||
) | ||
|
||
self.assertTrue( | ||
os.path.exists(log_file), | ||
"log file was not created.", | ||
) | ||
|
||
def test_transform_creates_output_files(self): | ||
transformed_image = ( | ||
self.output_dir + f"/{self.method_name}_transformed_image.nii.gz" | ||
) | ||
log_file = self.output_dir + f"/{self.method_name}_transformation.log" | ||
|
||
print("tf m:", self.transform_matrix) | ||
self.registrator.transform( | ||
fixed_image_path=self.fixed_image, | ||
moving_image_path=self.moving_image, | ||
transformed_image_path=transformed_image, | ||
matrix_path=self.transform_matrix, | ||
log_file_path=log_file, | ||
) | ||
|
||
self.assertTrue( | ||
os.path.exists(transformed_image), | ||
"transformed file was not created.", | ||
) | ||
|
||
self.assertTrue( | ||
os.path.exists(log_file), | ||
"log file was not created.", | ||
) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0.9888756 0.1354215 0.06153017 -1.314461 | ||
-0.1397797 0.9874795 0.07311541 -1.147088 | ||
-0.05085838 -0.08090271 0.9954236 2.731441 | ||
0 0 0 1 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters