Skip to content

Commit

Permalink
better tests
Browse files Browse the repository at this point in the history
Signed-off-by: neuronflow <[email protected]>
  • Loading branch information
neuronflow committed Apr 1, 2024
1 parent 4ec1221 commit dcb1ab1
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 91 deletions.
93 changes: 93 additions & 0 deletions tests/registrator_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import os
import shutil
import unittest
from abc import ABC, abstractmethod

from auxiliary.turbopath import turbopath


class RegistratorBase(unittest.TestCase):

@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.{self.matrix_extension}"
)

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 added tests/test_data/input/ants_matrix.mat
Binary file not shown.
Binary file added tests/test_data/input/ereg_matrix.mat
Binary file not shown.
Binary file removed tests/test_data/input/matrix.mat
Binary file not shown.
4 changes: 0 additions & 4 deletions tests/test_data/input/matrix.txt

This file was deleted.

4 changes: 4 additions & 0 deletions tests/test_data/input/niftyreg_matrix.txt
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 added tests/test_data/input/tcia_example_t1.nii.gz
Binary file not shown.
88 changes: 1 addition & 87 deletions tests/test_registrators.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,10 @@
import os
import shutil
import unittest
from abc import abstractmethod

from auxiliary.turbopath import turbopath
from registrator_base import RegistratorBase

from brainles_preprocessing.registration.ANTs.ANTs import ANTsRegistrator
from brainles_preprocessing.registration.eReg.eReg import eRegRegistrator
from brainles_preprocessing.registration.niftyreg.niftyreg import NiftyRegRegistrator


class RegistratorBase(unittest.TestCase):
@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 + "/bet_tcia_example_t1c_mask.nii.gz"

self.matrix = self.output_dir + "/matrix"
self.transform_matrix = input_dir + f"/matrix.{self.matrix_extension}"

def tearDown(self):
# Clean up created files if they exist
shutil.rmtree(self.output_dir)

def test_register_creates_output_files(self):
transformed_image = self.output_dir + "/registered_image.nii.gz"
log_file = self.output_dir + "/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 + "/transformed_image.nii.gz"
log_file = self.output_dir + "/transformation.log"

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.",
)


# TODO also test transform


class TestANTsRegistrator(RegistratorBase):
def get_registrator(self):
return ANTsRegistrator()
Expand Down

0 comments on commit dcb1ab1

Please sign in to comment.