From dba576a42a939f62c97420a6571fb3dd35eda708 Mon Sep 17 00:00:00 2001 From: HastingsGreer Date: Wed, 20 Nov 2024 13:35:35 -0500 Subject: [PATCH] Test unigradicon-warp on a labelmap (#20) * Update test_readme_works.yml * Switch mask to signed int (and nii.gz for good measure) * Update test_readme_works.yml * add casting around the image warping in a way that I think is correct * Looks like string comparison is the way to go * More detailed model- cast risk only comes from unsigned long * Merge branch 'main' into test-unigradicon-warp-issue --- .github/workflows/test_readme_works.yml | 4 ++++ src/unigradicon/__init__.py | 29 +++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_readme_works.yml b/.github/workflows/test_readme_works.yml index b63515c..e1add7b 100644 --- a/.github/workflows/test_readme_works.yml +++ b/.github/workflows/test_readme_works.yml @@ -24,6 +24,7 @@ jobs: pip install -e . wget https://www.hgreer.com/assets/slicer_mirror/RegLib_C01_1.nrrd wget https://www.hgreer.com/assets/slicer_mirror/RegLib_C01_2.nrrd + wget https://www.hgreer.com/assets/RegLib_C01_1_foreground_mask.nii.gz - name: Test run: | unigradicon-register --fixed=RegLib_C01_2.nrrd --fixed_modality=mri --moving=RegLib_C01_1.nrrd --moving_modality=mri \ @@ -32,3 +33,6 @@ jobs: --transform_out=trans.hdf5 --warped_moving_out=warped_C01_1.nrrd --io_iterations=3 unigradicon-warp --fixed=RegLib_C01_2.nrrd --moving=RegLib_C01_1.nrrd \ --transform=trans.hdf5 --warped_moving_out=warped_2_C01_1.nrrd --nearest_neighbor + unigradicon-warp --fixed=RegLib_C01_2.nrrd --moving=RegLib_C01_1_foreground_mask.nii.gz \ + --transform=trans.hdf5 --warped_moving_out=warped_2_C01_1.nrrd --nearest_neighbor + diff --git a/src/unigradicon/__init__.py b/src/unigradicon/__init__.py index 3395c82..032b86b 100644 --- a/src/unigradicon/__init__.py +++ b/src/unigradicon/__init__.py @@ -337,7 +337,7 @@ def main(): itk.transformwrite([phi_AB], args.transform_out) if args.warped_moving_out: - moving = itk.CastImageFilter[type(moving), itk.Image[itk.F, 3]].New()(moving) + moving, maybe_cast_back = maybe_cast(moving) interpolator = itk.LinearInterpolateImageFunction.New(moving) warped_moving_image = itk.resample_image_filter( moving, @@ -346,6 +346,7 @@ def main(): use_reference_image=True, reference_image=fixed ) + warped_moving_image = maybe_cast_back(warped_moving_image) itk.imwrite(warped_moving_image, args.warped_moving_out) def warp_command(): @@ -370,6 +371,8 @@ def warp_command(): else: phi_AB = itk.transformread(args.transform)[0] + moving, maybe_cast_back = maybe_cast(moving) + if args.linear: interpolator = itk.LinearInterpolateImageFunction.New(moving) elif args.nearest_neighbor: @@ -383,4 +386,26 @@ def warp_command(): use_reference_image=True, reference_image=fixed ) - itk.imwrite(warped_moving_image, args.warped_moving_out) \ No newline at end of file + + warped_moving_image = maybe_cast_back(warped_moving_image) + + itk.imwrite(warped_moving_image, args.warped_moving_out) + +def maybe_cast(img: itk.Image): + """ + If an itk image is of a type that can't be used with InterpolateImageFunctions, cast it + and be able to cast it back + """ + maybe_cast_back = lambda x: x + + if str((type(img), itk.D)) not in itk.NearestNeighborInterpolateImageFunction.GetTypesAsList(): + + if type(img) in (itk.Image[itk.ULL, 3], itk.Image[itk.UL, 3]): + raise Exception("Label maps of type unsigned long may have values that cannot be represented in a double") + + maybe_cast_back = itk.CastImageFilter[itk.Image[itk.D, 3], type(img)].New() + + img = itk.CastImageFilter[type(img), itk.Image[itk.D, 3]].New()(img) + + return img, maybe_cast_back +