Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ants.atropos probimgs = [iio2.image_read(probsout[0])] #554

Closed
rachelglenn opened this issue Feb 26, 2024 · 8 comments
Closed

ants.atropos probimgs = [iio2.image_read(probsout[0])] #554

rachelglenn opened this issue Feb 26, 2024 · 8 comments

Comments

@rachelglenn
Copy link

rachelglenn commented Feb 26, 2024

I am trying to use ants.atropos on an image and I occasionally get the following error with probsout list being empty:

import ants
img = ants.image_read('image.nii.gz')
img = ants.resample_image(img, (64,64), 1, 0)
mask = ants.image_read('mask.nii.gz')
seg = ants.atropos( a = img, m = '[0.2,1x1]', c = '[2,0]', i = 'kmeans[3]', x = mask )

check tdir /tmp/ antsrcs6skj16prob*.nii.gz check probsout [] Traceback (most recent call last): File "/rsrch1/ip/rglenn1/data/cervical_patients/python_ants_image_seg.py", line 38, in <module> seg2 = ants.atropos( a = new_img, m = '[0.2,1x1x1]', c = '[10,0]', x = mask ) # i = 'kmeans[2]', File "/rsrch1/ip/rglenn1/support_packages/miniconda/conda_gpu4/envs/my_tf-gpu/lib/python3.9/site-packages/ants/segmentation/atropos.py", line 150, in atropos probimgs = [iio2.image_read(probsout[0])] IndexError: list index out of range
Any suggestions?

@cookpa
Copy link
Member

cookpa commented Feb 26, 2024

This is strange, if there are truly no probability images generated, it should complain specifically about that.

When you say occasionally, do you mean that it fails on particular data, or occasionally fails with the same data?

What ANTsPy version do you have? What OS?

The error output doesn't match the code you've provided, does it depend on the particular settings in the call to atropos?

If you can make a reproducible example with data I will try it out.

@rachelglenn
Copy link
Author

rachelglenn commented Feb 27, 2024

I am using ants version 0.3.8. If I don't read in the mask and generate one from the image using get_mask(img) it works without an error on all my data. However, if I try to use a mask that I contoured from the image, on some patients it fails. I am not able to provide the image at this time. Yes, there was a typo it is 1x1x1 in the code.
seg = ants.atropos( a = img, m = '[0.2,1x1x1]', c = '[2,0]', i = 'kmeans[3]', x = mask )

Why is it failing. I have tried going and putting print statements in the atropos code to determine why. It looks like it uses pybind11 for the c++ wrappers. It is hard to troubleshoot the c++ code in this way. Any suggestions?

@cookpa
Copy link
Member

cookpa commented Feb 27, 2024

Can you try adding verbose=1? That may give some clues

@rachelglenn
Copy link
Author

rachelglenn commented Feb 29, 2024

Adding in verbose, I think that I understand why, there is an issue with my mask. The maximum value is very small.

`check mask 0.0 9.1837e-41

Running Atropos for 3-dimensional images.

Progress:

itk::ExceptionObject (0x18215430)
Location: "unknown"
File: /project/itksource/Modules/Numerics/Statistics/include/itkStatisticsAlgorithm.hxx
Line: 263
Description: ITK ERROR: Attempting to compute bounds of a sample list containing no measurement vectors

/rsrch1/ip/rglenn1/support_packages/miniconda/conda_gpu4/envs/tensorflow/lib/python3.9/site-packages/ants/segmentation/atropos.py:142: UserWarning:

ERROR: Non-zero exit status!

Traceback (most recent call last):
File "/rsrch1/ip/rglenn1/data/cervical_patients/python_ants_image_seg.py", line 45, in
seg2 = ants.atropos( a = new_img, m = '[0.000001,1x1x1]', c = '[5,0]', i = 'kmeans[2]', x = mask, verbose=1)
File "/rsrch1/ip/rglenn1/support_packages/miniconda/conda_gpu4/envs/tensorflow/lib/python3.9/site-packages/ants/segmentation/atropos.py", line 146, in atropos
probimgs = [iio2.image_read(probsout[0])]
IndexError: list index out of range
`

@cookpa
Copy link
Member

cookpa commented Feb 29, 2024

Thanks - I see why it's not reporting this error properly, and will work on that.

I'm still not clear about the workflow here. The parameters referenced in the error message, seg2 = ants.atropos( a = new_img, m = '[0.000001,1x1x1]', c = '[5,0]', i = 'kmeans[2]', x = mask, verbose=1) don't match any of the code in your previous posts.

I understand you can't share the data, but could you post a complete series of commands to produce the error? Starting with reading in the image and mask.

Please also print the image and mask to the terminal, eg

>>> im = ants.image_read(ants.get_data('mni'))
>>> im
ANTsImage (RPI)
	 Pixel Type : float (float32)
	 Components : 1
	 Dimensions : (182, 218, 182)
	 Spacing    : (1.0, 1.0, 1.0)
	 Origin     : (-90.0, 126.0, -72.0)
	 Direction  : [ 1.  0.  0.  0. -1.  0.  0.  0.  1.]

One thing we need to check is that the images are in the same physical space.

@rachelglenn
Copy link
Author

rachelglenn commented Feb 29, 2024

Sure. I guess that I am getting a similar error with masks that have the correct values [0,1]. It looks like my mask origin is different than the patient origin and that is why I am getting an error.

This is inside a for loop on a set of patients:


for i in range(10,len(directories)):
        folder = directories[i]
        main_folder = os.path.join(base_folder, folder)
        mask_organ = os.path.join(main_folder, "OrganSegmentation.nii.gz")
        t2_image = os.path.join(main_folder, "T2.nii.gz")
        save_filename = os.path.join(main_folder, "Gel_mask_threshold.nii.gz")
        save_filename_2 = os.path.join(main_folder, "T2_bc_zdcore.nii.gz")
        print("starting patient", main_folder)
        img = ants.image_read(t2_image)
        img = ants.n3_bias_field_correction2(img)
        img = ants.reorient_image2(img, "RAI")
        img.set_direction(np.eye(3))


        mean = np.mean(img.numpy())
        std = np.std(img.numpy())
        minum = np.min(img.numpy())
        maxum = np.max(img.numpy())
        img_array = (img.numpy() - mean) / std

        new_img = img.new_image_like(img_array)

        mask = ants.image_read(mask_organ)
        mask = ants.reorient_image2(mask, "RAI")
        mask.set_direction(np.eye(3))
        print("check mask", np.min(mask.numpy()), np.max(mask.numpy()))
        if np.max(mask.numpy()) > 0.5:
            seg2 = ants.atropos( a = new_img, m = '[0.000001,2x2x2]', c = '[5,0]', i = 'kmeans[2]',  x = mask, verbose=1)
            seg = ants.kmeans_segmentation(img,10)

            seg = ants.threshold_image(seg2['segmentation'], 1.2, 2.5)
            print("save_filename", save_filename)
            ants.image_write(seg2['segmentation'], save_filename) 
            ants.image_write(new_img, save_filename_2)
        else:
            check_mask.append(t2_image)

This is the output:
`starting patient /rsrch1/ip/rglenn1/data/cervical_patients/data/2380975_04242019
check image ANTsImage (RAI)
Pixel Type : float (float32)
Components : 1
Dimensions : (320, 320, 57)
Spacing : (0.75, 0.75, 5.0)
Origin : (-109.4882, -104.8265, -352.6993)
Direction : [1. 0. 0. 0. 1. 0. 0. 0. 1.]

check mask ANTsImage (RAI)
Pixel Type : float (float32)
Components : 1
Dimensions : (320, 320, 50)
Spacing : (0.75, 0.75, 5.0)
Origin : (-90.7894, -74.7666, -36.6722)
Direction : [1. 0. 0. 0. 1. 0. 0. 0. 1.]

check mask 0.0 1.0

Running Atropos for 3-dimensional images.

Progress:

itk::ExceptionObject (0x17804560)
Location: "unknown"
File: /project/itksource/Modules/Core/Common/include/itkImageToImageFilter.hxx
Line: 218
Description: ITK ERROR: AtroposSegmentationImageFilter(0x17704e80): Inputs do not occupy the same physical space!
InputImage Origin: [-1.0948818e+02, -1.0482648e+02, -3.5269931e+02], InputImage_1 Origin: [-9.0789368e+01, -7.4766640e+01, -3.6672241e+01]
Tolerance: 7.5000000e-07

/rsrch1/ip/rglenn1/support_packages/miniconda/conda_gpu4/envs/tensorflow/lib/python3.9/site-packages/ants/segmentation/atropos.py:142: UserWarning:

ERROR: Non-zero exit status!

Traceback (most recent call last):
File "/rsrch1/ip/rglenn1/data/cervical_patients/python_ants_image_seg.py", line 49, in
seg2 = ants.atropos( a = new_img, m = '[0.000001,2x2x2]', c = '[5,0]', i = 'kmeans[2]', x = mask, verbose=1)
File "/rsrch1/ip/rglenn1/support_packages/miniconda/conda_gpu4/envs/tensorflow/lib/python3.9/site-packages/ants/segmentation/atropos.py", line 146, in atropos
probimgs = [iio2.image_read(probsout[0])]
IndexError: list index out of range
`

@cookpa
Copy link
Member

cookpa commented Feb 29, 2024

OK great, I think we've found two errors:

Description: ITK ERROR: AtroposSegmentationImageFilter(0x17704e80): Inputs do not occupy the same physical space!
InputImage Origin: [-1.0948818e+02, -1.0482648e+02, -3.5269931e+02], InputImage_1 Origin: [-9.0789368e+01, -7.4766640e+01, -3.6672241e+01]
Tolerance: 7.5000000e-07

is from the image and mask having different definitions of physical space. Their origin and direction should be the same.

This

itk::ExceptionObject (0x18215430)
Location: "unknown"
File: /project/itksource/Modules/Numerics/Statistics/include/itkStatisticsAlgorithm.hxx
Line: 263
Description: ITK ERROR: Attempting to compute bounds of a sample list containing no measurement vectors

is from the mask being all zero, or very close to zero, causing the segmentation domain to be empty.

Once the image and mask have the same headers, I recommend saving them both to disk and opening with ITK-SNAP to ensure they look correct.

@rachelglenn
Copy link
Author

Thanks for the help!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants