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
628 stars 161 forks source link

Resample_images() crashes kernel #429

Closed ninea-anasovi closed 1 year ago

ninea-anasovi commented 1 year ago

function resample_images crashes kernel

I am using python 3.9.4 Jupiter Notebook Visual studio code

Libraries that I use are: Matplotlib Nibabel Antspyx

First of all I could not install antspy with: pip install antspyx As numpy verson was 1.24.2 So I had to donwgrade my python.

After that I needed rigit co-registration of 2 3D images

It didn't worked so I tried to see ants.get_data('r16') and I realised it was 2D and tried to copy the code from documentation to test:

fi = ants.image_read(ants.get_ants_data('r16'))

mi = ants.image_read(ants.get_ants_data('r64'))

print(fi)

print(mi)

fi = ants.resample_image(fi, (60,60), 1, 0)

mi = ants.resample_image(mi, (60,60), 1, 0)

mytx = ants.registration(fixed=fi,moving=mi,type_of_transform ='Rigid' )

This Crashes kernel and I can not do anything unless I reload my VS code.

image

If I remove 2 lines with resample_image() then this is the error I see:

image

Please help me solve this issue.

Os: Windows 10

dipterix commented 1 year ago

(I'm not ANTs devs, just a user encounter the same issue)

Same here on Windows. Not only resample_image, but also many other functions raise errors, mostly XXXX failed with error code 1 or something like file 000002BE70950FA0 does not exist.

After tracing back to the source (searching keyword "failed with error code"), I found all these failed functions call ANTs c++ with system calls. For example in ResampleImage

libfn = utils.get_lib_fn('ResampleImage')
libfn(processed_args)

or in antsRegistration

https://github.com/ANTsX/ANTsPy/blob/38260eb58f983541b2a614b5274975bc5d4f7d1f/ants/registration/interface.py#L218-L222

The argument processed_args contains ANTsImage, which seems to be turned into memory pointers (I guess?) when passing to system calls. With verbose=True, ANTsPy prints out the command:

antsRegistration -d 2 -r [000001F898A756B0,000001F898A75920,1] -m mattes[000001F898A756B0,000001F898A75920,1,32,regular,0.2] -t Affine[0.25] -c 2100x1200x1200x0 -s 3x2x1x0 -f 4x2x2x1 -x [NA,NA] -m mattes[000001F898A756B0,000001F898A75920,1,32] -t SyN[0.200000,3.000000,0.000000] -c [40x20x0,1e-7,8] -s 2x1x0 -f 4x2x1 -u 1 -z 1 -o [C:/Users/RUNNER~1/AppData/Local/Temp/Rtmp6VIw8G/working_dir/RtmpOGBhDO/file16ec59566cdb,000001F898A75930,000001F898A75890] -x [NA,NA] --float 1 --write-composite-transform 0 -v 1

Next moment, I saw this error

 file 000001F898A756B0 does not exist . 
Exception Object caught: 

itk::ExceptionObject (0000007EB4F99020)
Location: "unknown" 

However, somehow Windows does not read in the image/transform pointers correctly.

By the way, i'm using Python 3.8.16 on AMD 64x

stnava commented 1 year ago

Your diagnosis of the problem is correct … and we don’t develop on windows ….

A solution that may work is to figure out what windows uses as a pointer address … and then implement a platform specific address system.

I have no idea if this would be portable or work across different window systems …

On Sun, Feb 26, 2023 at 12:45 PM Dipterix Wang @.***> wrote:

(I'm not ANTs devs, just a user encounter the same issue)

Same here on Windows. Not only resample_image, but also many other functions raise errors, mostly XXXX failed with error code 1 or something like file 000002BE70950FA0 does not exist.

After tracing back to the source (searching keyword "failed with error code"), I found all these failed functions call ANTs c++ with system calls. For example in ResampleImage

libfn = utils.get_lib_fn('ResampleImage') libfn(processed_args)

or in antsRegistration

https://github.com/ANTsX/ANTsPy/blob/38260eb58f983541b2a614b5274975bc5d4f7d1f/ants/registration/interface.py#L218-L222

The argument processed_args contains ANTsImage, which seems to be turned into memory pointers (I guess?) when passing to system calls. With verbose=True, ANTsPy prints out the command:

antsRegistration -d 2 -r [000001F898A756B0,000001F898A75920,1] -m mattes[000001F898A756B0,000001F898A75920,1,32,regular,0.2] -t Affine[0.25] -c 2100x1200x1200x0 -s 3x2x1x0 -f 4x2x2x1 -x [NA,NA] -m mattes[000001F898A756B0,000001F898A75920,1,32] -t SyN[0.200000,3.000000,0.000000] -c [40x20x0,1e-7,8] -s 2x1x0 -f 4x2x1 -u 1 -z 1 -o [C:/Users/RUNNER~1/AppData/Local/Temp/Rtmp6VIw8G/working_dir/RtmpOGBhDO/file16ec59566cdb,000001F898A75930,000001F898A75890] -x [NA,NA] --float 1 --write-composite-transform 0 -v 1

Next moment, I saw this error

file 000001F898A756B0 does not exist . Exception Object caught:

itk::ExceptionObject (0000007EB4F99020) Location: "unknown"

However, somehow Windows does not read in the image/transform pointers correctly.

By the way, i'm using Python 3.8.16 on AMD 64x

— Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsPy/issues/429#issuecomment-1445418303, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACPE7UVPX4LG6MVL5SN3KTWZOJFNANCNFSM6AAAAAAU3IQXX4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

--

brian

dipterix commented 1 year ago

Gotcha, if I save the files to the file system, can I call libfn from Python with file paths as arguments instead of pointers? I saw https://github.com/ANTsX/ANTs/blob/e62eca0988c3ad3cd151ef380465e230b95891a7/Scripts/newAntsExample.sh

However, I don't know how to implement.

dipterix commented 1 year ago

@ninea-anasovi I created a patch to deal with this issue for my package: https://github.com/dipterix/rpyANTs/blob/main/inst/patches/win_patch.py

Basically it allows the functions to store the data in temporary files instead of keeping them in memory. You can run this code via python import or just copy-paste them after importing ants:

Please edit accordingly:

import ants

# now import win_patch.py to apply patch. You might want edit the following line
import win_patch.py
cookpa commented 1 year ago

I believe this should be fixed by #442