Skip to content

Commit

Permalink
ENH: Throw exception if image cannot be read (#738)
Browse files Browse the repository at this point in the history
* ENH: Throw exception if image cannot be read

Non-existent files are caught but empty / bad files caused a seg fault
  • Loading branch information
cookpa authored Nov 19, 2024
1 parent f079ba6 commit db33946
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
13 changes: 6 additions & 7 deletions ants/core/ants_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
ita = _image_type_map[itype]
pa = _ptype_type_map[p]
_image_read_dict[itype][p][d] = "imageRead%s%s%i" % (ita, pa, d)

def from_numpy(
data, origin=None, spacing=None, direction=None, has_components=False, is_rgb=False
):
Expand Down Expand Up @@ -89,16 +89,16 @@ def from_numpy(
ANTsImage
image with given data and any given information
"""

# this is historic but should be removed once tests can pass without it
if data.dtype.name == 'float64':
data = data.astype('float32')

# if dtype is not supported, cast to best available
best_dtype = infer_dtype(data.dtype)
if best_dtype != data.dtype:
data = data.astype(best_dtype)

img = _from_numpy(data.T.copy(), origin, spacing, direction, has_components, is_rgb)
return img

Expand Down Expand Up @@ -248,7 +248,6 @@ def image_header_info(filename):
retval["direction"] = np.round(retval["direction"], 4)
return retval


def image_clone(image, pixeltype=None):
"""
Clone an ANTsImage
Expand Down Expand Up @@ -493,7 +492,7 @@ def clone(image, pixeltype=None):
libfn = get_lib_fn('antsImageClone%s'%fn_suffix)
pointer_cloned = libfn(image.pointer)
return ants.from_pointer(pointer_cloned)

copy = clone

@image_method
Expand Down Expand Up @@ -525,6 +524,6 @@ def new_image_like(image, data):
return from_numpy(data, origin=image.origin,
spacing=image.spacing, direction=image.direction,
has_components=image.has_components)

def from_numpy_like(data, image):
return new_image_like(image, data)
5 changes: 5 additions & 0 deletions src/antsImageHeaderInfo.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ nb::dict antsImageHeaderInfo( std::string fname )
itk::ImageIOFactory::CreateImageIO(
fname.c_str(), itk::ImageIOFactory::ReadMode);

if ( !imageIO )
{
throw std::runtime_error("Could not create ImageIO object for file " + fname);
}

imageIO->SetFileName(fname);
imageIO->ReadImageInformation();

Expand Down
8 changes: 7 additions & 1 deletion tests/test_core_ants_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,14 @@ def test_image_read_write(self):
nptest.assert_allclose(img.numpy(), img2.numpy())

# non-existant file
with self.assertRaises(Exception):
with self.assertRaises(ValueError):
tmpfile = mktemp(suffix='.nii.gz')
ants.image_read(tmpfile)

# Test empty file
with self.assertRaises(RuntimeError):
tmpfile = mktemp(suffix='.nii.gz')
open(tmpfile, 'a').close()
ants.image_read(tmpfile)


Expand Down

0 comments on commit db33946

Please sign in to comment.