Skip to content

Commit

Permalink
ENH: Add python_unittest/test_ElastixRegistrationMethod.py
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Dekker committed Oct 17, 2024
1 parent 191cd87 commit 25d3170
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions test/python_unittest/test_ElastixRegistrationMethod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import itk
import unittest

class ElastixRegistrationMethodTestCase(unittest.TestCase):
"""Tests itk.ElastixRegistrationMethod"""

def test_SimilarityTransform(self) -> None:
"""Tests SimilarityTransform"""

def set_pixels_of_object(image, index_value: int, size_value: int) -> None:
for i in range(index_value, index_value + size_value):
for j in range(index_value, index_value + size_value):
image.SetPixel([i, j], 1)

PixelType = itk.F
ImageDimension = 2
ImageType = itk.Image[PixelType, ImageDimension]
SizeType = itk.Size[ImageDimension]
IndexType = itk.Index[ImageDimension]

fixed_object_size_value = 32
fixed_object_index_value = fixed_object_size_value
fixed_image_size_value = 4 * fixed_object_size_value

moving_object_size_value = 40
moving_object_index_value = moving_object_size_value
moving_image_size_value = 4 * moving_object_size_value

fixed_image = ImageType.New(Regions=SizeType.Filled(fixed_image_size_value))
fixed_image.AllocateInitialized()
set_pixels_of_object(fixed_image, fixed_object_index_value, fixed_object_size_value)

moving_image = ImageType.New(Regions=SizeType.Filled(moving_image_size_value))
moving_image.AllocateInitialized()
set_pixels_of_object(moving_image, moving_object_index_value, moving_object_size_value)

parameter_map = {
"ImageSampler": ("Full",),
"MaximumNumberOfIterations": ("50",),
"Metric": ("AdvancedNormalizedCorrelation",),
"Optimizer": ("AdaptiveStochasticGradientDescent",),
"Transform": ("SimilarityTransform",),
}
registration = itk.ElastixRegistrationMethod[ImageType, ImageType].New(
FixedImage=fixed_image,
MovingImage=moving_image,
ParameterObject=itk.ParameterObject.New(ParameterMap = parameter_map) )
registration.Update()
transform_parameter_object = registration.GetTransformParameterObject()
transform_parameters = transform_parameter_object.GetParameter(0, "TransformParameters")
self.assertEqual(len(transform_parameters), 4)
self.assertAlmostEqual(float(transform_parameters[0]), float(moving_object_size_value) / float(fixed_object_size_value), 1)

if __name__ == "__main__":
unittest.main()

0 comments on commit 25d3170

Please sign in to comment.