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

FIX: Indexing images returns cropped images #626

Closed ncullen93 closed 1 month ago

ncullen93 commented 1 month ago

Currently if you index an ants image it always returns a numpy array. The intuitive response to indexing an image should instead be an image. And that image should retain the spatial properties of the original image.

If you index only one dimension, that should return a 1D numpy array. And if you index on one value then you get that value.

Example:

import ants
img = ants.image_read(ants.get_data('mni'))

img[:10,:10,:10] # an ants image with shape (10,10,10)
img[10:20,10:20,10:20] # same as above but the origin has been shifted by 10 in all directions

img[:10,20,:10] # 2D image

img[:10,10,10] # 1D numpy array

img[10,10,10] # single value
ncullen93 commented 1 month ago

Also, indexing images with images works more as expected. The index image will act as a mask so any indexes in the image which are zero in the mask will be turned to zero.

Example:

import ants
img = ants.image_read(ants.get_data('r16'))
mask = img > 160
img2 = img[mask] # same shape as img but now values less than 160 have been set to 0
cookpa commented 1 month ago

mask = img > 160 img2 = img[mask] # same shape as img but now values less than 160 have been set to 0

This one might cause some confusion, I do this to mean give me the voxels inside the mask.

ncullen93 commented 1 month ago

Ok, thanks for letting me know because I was also very torn about this one. I guess that same effect can be achieved with img * mask so it makes sense to keep the original functionality. Should it always be a flattened array of values or should it maintain the dimensions?

coveralls commented 1 month ago

Coverage Status

coverage: 80.91% (+0.4%) from 80.524% when pulling 21f5f32b8a3eda096abdf7a809e46fbbf18f766a on index-to-image into 0903c42fd6cf0026949b2f9e0b90d320e8a59e91 on master.

cookpa commented 1 month ago

I'd say flat array, but would also ask @stnava and @ntustison

ncullen93 commented 1 month ago

That's the current behavior so it makes sense to keep it.