ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
648 stars 164 forks source link

ants.apply_transforms returns None #725

Closed hbhutta closed 2 weeks ago

hbhutta commented 2 weeks ago

Describe the bug Getting None when using ants.apply_transforms

To reproduce

import pickle
import ants

"""
Here I am loading the forward transforms from a previously completed registration
where an MRI (moving) image was registered to CT (fixed) image
"""
with open("testing/reg.pkl", "rb") as file:
    mytx = pickle.load(file)
"""
I double checked that mytx is just the serialized output of the registration. The serializing and loading is not affecting `apply_transforms` AFAIK
"""

print(mytx)

fixed = 'testing/28_proc_ct.nii'
moving = 'testing/28_proc_vent.nii'

out = ants.apply_transforms(fixed=fixed, moving=moving, transformlist=mytx['fwdtransforms'])
print(out) # None

Expected behavior

What I expect to get is a warped version of the moving image (the ventilation image) saved into the out variable. I should then be able to do ants.image_write(image=out, filename=...)

Screenshots

This is what I get when I run the above code (in a file called dummy.py): image

ANTsPy installation (please complete the following information):

Additional context The fixed image (CT) and the moving image (MRI) that were originally registered were both masks, so there were hard boundaries defined. The ventilation image on which I am trying to apply the forward transforms from the above mentioned registration, does not have hard boundaries -- in fact it is quite a fuzzy image. Would this have any impact on the return value of apply_transforms?

hbhutta commented 2 weeks ago

Oh my goodness I need to pay attention, I forgot to use ants.image_read() 🧠

cookpa commented 2 weeks ago

The transforms are strings, they are the path to temporary files, so they may not persist between sessions. You may need to save them somewhere yourself in order to use them this way

hbhutta commented 2 weeks ago

Would I need to save these temp files even if I have serialized the registration output?

cookpa commented 2 weeks ago

From your output

'fwdtransforms': ['/tmp/tmpnteshf1h1Warp.nii.gz', '/tmp/tmpnteshflh0GenericAffine.mat']

I think if you serialize this it will save a list with these two strings that you can load later, but the files on /tmp might not exist tomorrow, or after you reboot.

Also, I'm not sure antsImage objects can be serialized, because they are bound to C++ objects.