Skip to content

Commit

Permalink
Test unigradicon-warp on a labelmap (#20)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
HastingsGreer authored Nov 20, 2024
1 parent 8a11d35 commit dba576a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test_readme_works.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
29 changes: 27 additions & 2 deletions src/unigradicon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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():
Expand All @@ -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:
Expand All @@ -383,4 +386,26 @@ def warp_command():
use_reference_image=True,
reference_image=fixed
)
itk.imwrite(warped_moving_image, args.warped_moving_out)

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

0 comments on commit dba576a

Please sign in to comment.