Skip to content

Commit 823b68a

Browse files
authored
Merge pull request #765 from ANTsX/clone_dtype
Allow numpy datatypes in image_clone
2 parents 5d54638 + 94851ae commit 823b68a

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

ants/core/ants_image_io.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def image_clone(image, pixeltype=None):
259259
image : ANTsImage
260260
image to clone
261261
262-
dtype : string (optional)
262+
pixeltype : string (optional)
263263
new datatype for image
264264
265265
Returns
@@ -460,8 +460,8 @@ def clone(image, pixeltype=None):
460460
461461
Arguments
462462
---------
463-
dtype: string (optional)
464-
if None, the dtype will be the same as the cloned ANTsImage. Otherwise,
463+
pixeltype: string (optional)
464+
if None, the pixeltype will be the same as the cloned ANTsImage. Otherwise,
465465
the data will be cast to this type. This can be a numpy type or an ITK
466466
type.
467467
Options:
@@ -478,7 +478,11 @@ def clone(image, pixeltype=None):
478478
pixeltype = image.pixeltype
479479

480480
if pixeltype not in _supported_ptypes:
481-
raise ValueError('Pixeltype %s not supported. Supported types are %s' % (pixeltype, _supported_ptypes))
481+
# check if the pixeltype is a numpy type
482+
if pixeltype in _supported_ntypes:
483+
pixeltype = _npy_to_itk_map[pixeltype]
484+
else:
485+
raise ValueError('Pixeltype %s not supported. Supported types are %s' % (pixeltype, _supported_ptypes))
482486

483487
if image.has_components and (not image.is_rgb):
484488
comp_imgs = ants.split_channels(image)

tests/test_core_ants_image.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def setUp(self):
2727
img2d = ants.image_read(ants.get_ants_data('r16'))
2828
img3d = ants.image_read(ants.get_ants_data('mni'))
2929
self.imgs = [img2d, img3d]
30-
self.pixeltypes = ['unsigned char', 'unsigned int', 'float']
30+
self.pixeltypes = ['unsigned char', 'unsigned int', 'float', 'double']
31+
self.numpy_pixeltypes = ['uint8', 'uint32', 'float32', 'float64']
3132

3233
def tearDown(self):
3334
pass
@@ -138,10 +139,10 @@ def test_clone(self):
138139
#self.setUp()
139140
for img in self.imgs:
140141
orig_ptype = img.pixeltype
141-
for ptype in self.pixeltypes:
142+
for ptype in [*self.pixeltypes, *self.numpy_pixeltypes]:
142143
imgclone = img.clone(ptype)
143144

144-
self.assertEqual(imgclone.pixeltype, ptype)
145+
self.assertIn(ptype, [imgclone.dtype, imgclone.pixeltype])
145146
self.assertEqual(img.pixeltype, orig_ptype)
146147
# test physical space consistency
147148
self.assertTrue(ants.image_physical_space_consistency(img, imgclone))
@@ -530,7 +531,7 @@ def setUp(self):
530531
img2d = ants.image_read(ants.get_ants_data('r16')).clone('float')
531532
img3d = ants.image_read(ants.get_ants_data('mni')).clone('float')
532533
self.imgs = [img2d, img3d]
533-
self.pixeltypes = ['unsigned char', 'unsigned int', 'float']
534+
self.pixeltypes = ['unsigned char', 'unsigned int', 'float', 'double']
534535

535536
def tearDown(self):
536537
pass

0 commit comments

Comments
 (0)