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
650 stars 164 forks source link

Segfault when opening empty file image_read #737

Closed emilljungberg closed 1 week ago

emilljungberg commented 1 week ago

Describe the bug When attempting to open a file that is empty, image_read creates a segmentation fault instead of throwing an error. I've had this occurring when trying to open a download that was empty or when there was a fault in saving an image. One could of course expect the user to check that the file they are trying to open is working, but this could be an easy check. This could be the issue occurring in #223

To reproduce Example where I create an empty file and try to read it:

import ants

with open('test.nii.gz', 'w') as f:
    f.close()

ants.image_read('test.nii.gz')

>>> [1]    71191 segmentation fault  python3

Suggested fix Could there be a catch in the image_read function similar to the nibable.load function? When I try the same in nibabel I get an error. They catch the error with os.stat(filename).

import nibabel as nib
nib.load('test.nii.gz')

>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/emil/miniforge3/envs/fw/lib/python3.9/site-packages/nibabel/loadsave.py", line 105, in load
    raise ImageFileError(f"Empty file: '{filename}'")
nibabel.filebasedimages.ImageFileError: Empty file: 'test.nii.gz'

ANTsPy installation

cookpa commented 1 week ago

Good idea, I'm working on a fix in the C++ ANTs code that will address this

cookpa commented 1 week ago

Actually I'm a little concerned that fixing in ANTs might have side effects over there, so will follow up with that separately.

I think #738 will stop seg faults from ants.image_read