From d9a6086728cbc7a1f5cf238a63d6d87d4c41b861 Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Sat, 11 Feb 2023 11:43:00 -0500 Subject: [PATCH 01/11] Create register_pair --- bin/register_pair | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 bin/register_pair diff --git a/bin/register_pair b/bin/register_pair new file mode 100644 index 0000000..c25b664 --- /dev/null +++ b/bin/register_pair @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +import argparse + +import icon_registration as icon +import icon_registration.itk_wrapper +import itk +import numpy as np +import torch +import importlib + +parser = argparse.ArgumentParser() +parser.add_argument("--model", required=True) +parser.add_argument("--weights_path") +parser.add_argument("--fixed_image", required=True) +parser.add_argument("--moving_image", required=True) +parser.add_argument("--transform_out") +parser.add_argument("--displacement_image_out") +parser.add_argument("--warped_moving_out") +parser.add_argument("--finetune", action='store_true') + +args = parser.parse_args() + +model_package_name = ".".join(args.model.split(".")[:-1]) +model_name = args.model.split(".")[-1] + +model_package = importlib.import(model_package_name) +net = getattr(model_package, model_name)() + +if args.weights_path): + weights = torch.load(args.weights_path) + net.regis_net.load_state_dict(weights) + +fixed_image = itk.imread(args.fixed_image) +moving_image = itk.imread(args.moving_image) + +phi, _ = icon.itk_wrapper.register_pair( + net, moving_image, fixed_image, finetune_steps=90 if args.finetune else None +) + +if args.transform_out: + itk.transformwrite([phi], args.transform_out) + +if args.displacement_image_out: + filter = itk.TransformToDisplacementFieldFilter[ + itk.itkImagePython.itkImageVF33, itk.D + ].New() + decorator = itk.DataObjectDecorator[itk.Transform[itk.D, 3, 3]].New() + decorator.Set(phi) + filter.SetInput(decorator) + filter.SetReferenceImage(fixed_image) + filter.SetUseReferenceImage(True) + filter.Update() + itk.imwrite(filter.GetOutput(), args.displacement_image_out) + +if args.warped_moving_out: + warped = itk.resample_image_filter( + moving_image, + use_reference_image=True, + reference_image=fixed_image, + transform=phi, + interpolator=itk.LinearInterpolateImageFunction.New(moving_image), + ) + itk.imwrite(warped, args.warped_moving_out) + + From fdcebee3c0bfdbd2fb66ef7698e0fe21df1517b8 Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Sat, 11 Feb 2023 12:21:36 -0500 Subject: [PATCH 02/11] Create test_console_scripts.py --- test/test_console_scripts.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test/test_console_scripts.py diff --git a/test/test_console_scripts.py b/test/test_console_scripts.py new file mode 100644 index 0000000..ad9f14d --- /dev/null +++ b/test/test_console_scripts.py @@ -0,0 +1,20 @@ +import unittest +import subprocess +import icon_registration.test_utils + +class TestConsoleScripts(unittest.TestCase): + def test_register_pair_script(self): + test_utils.download_test_data() + subprocess.run( + [ + "icon_register_pair", + "--fixed_image", + test_utils.TEST_DATA_DIR / "brain_test_data" / "2_T1w_acpc_dc_restore_brain.nii.gz", + "--moving_image", + test_utils.TEST_DATA_DIR / "brain_test_data" / "8_T1w_acpc_dc_restore_brain.nii.gz", + "--model", + "icon_registration.pretrained_models.brain_registration_model" + "--warped_image_out", + "warped.nii.gz" + ] + ) From 46a302fab45aee71d9aa5116549f5ff684edcbd6 Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Sat, 11 Feb 2023 12:23:28 -0500 Subject: [PATCH 03/11] Update setup.cfg --- setup.cfg | 3 +++ 1 file changed, 3 insertions(+) diff --git a/setup.cfg b/setup.cfg index e3c360d..d1a8e9d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,6 +20,9 @@ package_dir = packages = find: python_requires = >=3.4 +scripts = + bin/register_pair + install_requires = torch torchvision From 62fbc742a9fb1ac4474dd040f8c4c3e8fcfc7c78 Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Sat, 11 Feb 2023 12:27:36 -0500 Subject: [PATCH 04/11] Update test_console_scripts.py --- test/test_console_scripts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_console_scripts.py b/test/test_console_scripts.py index ad9f14d..0ef4d06 100644 --- a/test/test_console_scripts.py +++ b/test/test_console_scripts.py @@ -1,6 +1,6 @@ import unittest import subprocess -import icon_registration.test_utils +from icon_registration import test_utils class TestConsoleScripts(unittest.TestCase): def test_register_pair_script(self): From 5fb44c15f183119dce3295cad7f53f5dbfbc65cd Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Sat, 11 Feb 2023 12:31:36 -0500 Subject: [PATCH 05/11] Rename register_pair to icon_register_pair --- bin/{register_pair => icon_register_pair} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bin/{register_pair => icon_register_pair} (100%) diff --git a/bin/register_pair b/bin/icon_register_pair similarity index 100% rename from bin/register_pair rename to bin/icon_register_pair From 33834c4c8f1c99fa1f1e18aeda1b7a97c0c22704 Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Sat, 11 Feb 2023 12:34:13 -0500 Subject: [PATCH 06/11] fix usage of importlib --- bin/icon_register_pair | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/icon_register_pair b/bin/icon_register_pair index c25b664..55845e1 100644 --- a/bin/icon_register_pair +++ b/bin/icon_register_pair @@ -24,10 +24,10 @@ args = parser.parse_args() model_package_name = ".".join(args.model.split(".")[:-1]) model_name = args.model.split(".")[-1] -model_package = importlib.import(model_package_name) +model_package = importlib.import_module(model_package_name) net = getattr(model_package, model_name)() -if args.weights_path): +if args.weights_path: weights = torch.load(args.weights_path) net.regis_net.load_state_dict(weights) From 108b19ae3eb6e8721f75cf412d6388673a2a5282 Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Sat, 11 Feb 2023 12:39:17 -0500 Subject: [PATCH 07/11] Update setup.cfg --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index d1a8e9d..4e2c564 100644 --- a/setup.cfg +++ b/setup.cfg @@ -21,7 +21,7 @@ packages = find: python_requires = >=3.4 scripts = - bin/register_pair + bin/icon_register_pair install_requires = torch From 84a389cd985801282af3c6499472b9987c5d2f7f Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Sat, 11 Feb 2023 14:03:48 -0500 Subject: [PATCH 08/11] Update icon_register_pair --- bin/icon_register_pair | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bin/icon_register_pair b/bin/icon_register_pair index 55845e1..c1f3787 100644 --- a/bin/icon_register_pair +++ b/bin/icon_register_pair @@ -34,6 +34,11 @@ if args.weights_path: fixed_image = itk.imread(args.fixed_image) moving_image = itk.imread(args.moving_image) +# We want images normalized to 0.0, 1.0 +for image in fixed_image, moving_image: + assert(np.abs(np.max(itk.get_array_from_image(image)) - 1.0) < 0.2) + assert(np.abs(np.min(itk.get_array_from_image(image))) < 0.2) + phi, _ = icon.itk_wrapper.register_pair( net, moving_image, fixed_image, finetune_steps=90 if args.finetune else None ) From 86d6cccd48390f8a8b3f913c29b8922b75b937a0 Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Mon, 20 Feb 2023 12:24:58 -0500 Subject: [PATCH 09/11] Update icon_register_pair --- bin/icon_register_pair | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/icon_register_pair b/bin/icon_register_pair index c1f3787..7ffc5fb 100644 --- a/bin/icon_register_pair +++ b/bin/icon_register_pair @@ -36,8 +36,8 @@ moving_image = itk.imread(args.moving_image) # We want images normalized to 0.0, 1.0 for image in fixed_image, moving_image: - assert(np.abs(np.max(itk.get_array_from_image(image)) - 1.0) < 0.2) - assert(np.abs(np.min(itk.get_array_from_image(image))) < 0.2) + assert(np.abs(np.max(np.array(image)) - 1.0) < 0.2) + assert(np.abs(np.min(np.array(image))) < 0.2) phi, _ = icon.itk_wrapper.register_pair( net, moving_image, fixed_image, finetune_steps=90 if args.finetune else None From ee4a09b646d750749b274941aafe7be064562632 Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Mon, 20 Feb 2023 12:27:10 -0500 Subject: [PATCH 10/11] Update test_console_scripts.py --- test/test_console_scripts.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_console_scripts.py b/test/test_console_scripts.py index 0ef4d06..e004137 100644 --- a/test/test_console_scripts.py +++ b/test/test_console_scripts.py @@ -4,6 +4,7 @@ class TestConsoleScripts(unittest.TestCase): def test_register_pair_script(self): + import footsteps test_utils.download_test_data() subprocess.run( [ @@ -15,6 +16,6 @@ def test_register_pair_script(self): "--model", "icon_registration.pretrained_models.brain_registration_model" "--warped_image_out", - "warped.nii.gz" + footsteps.output_dir + "warped.nii.gz" ] ) From 35d0a258bb44f8369fe4fc0e1d09fc63a97b0e7a Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Mon, 20 Feb 2023 12:28:43 -0500 Subject: [PATCH 11/11] Update test_console_scripts.py --- test/test_console_scripts.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_console_scripts.py b/test/test_console_scripts.py index e004137..63cfa4e 100644 --- a/test/test_console_scripts.py +++ b/test/test_console_scripts.py @@ -19,3 +19,5 @@ def test_register_pair_script(self): footsteps.output_dir + "warped.nii.gz" ] ) + import itk + itk.imread(footsteps.output_dir + "warped.nii.gz")