Skip to content

Commit

Permalink
fix io tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ncullen93 committed May 14, 2024
1 parent 84eefb0 commit 975a919
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ find_package(Python 3.8
find_package(nanobind CONFIG REQUIRED)

# TODO: make this run only if ITK + ANTs are not already built
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
execute_process(COMMAND cmd /c ${PROJECT_SOURCE_DIR}/scripts/configure_ITK.bat)
execute_process(COMMAND cmd /c ${PROJECT_SOURCE_DIR}/scripts/configure_ANTs.bat)
else()
execute_process(COMMAND bash ${PROJECT_SOURCE_DIR}/scripts/configure_ITK.sh)
execute_process(COMMAND bash ${PROJECT_SOURCE_DIR}/scripts/configure_ANTs.sh)
endif()
#if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# execute_process(COMMAND cmd /c ${PROJECT_SOURCE_DIR}/scripts/configure_ITK.bat)
# execute_process(COMMAND cmd /c ${PROJECT_SOURCE_DIR}/scripts/configure_ANTs.bat)
#else()
# execute_process(COMMAND bash ${PROJECT_SOURCE_DIR}/scripts/configure_ITK.sh)
# execute_process(COMMAND bash ${PROJECT_SOURCE_DIR}/scripts/configure_ANTs.sh)
#endif()

# ITK
set(ITK_DIR "./itkbuild")
Expand Down
9 changes: 7 additions & 2 deletions ants/core/ants_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,15 @@ def make_image(
-------
ANTsImage
"""
if isinstance(voxval, iio.ANTsImage):
voxval = voxval.numpy()

if isinstance(imagesize, iio.ANTsImage):
img = imagesize.clone()
sel = imagesize > 0
if voxval.ndim > 1:
voxval = voxval.flatten()
voxval = voxval[voxval > 0]
if (len(voxval) == int((sel > 0).sum())) or (len(voxval) == 0):
img[sel] = voxval
else:
Expand Down Expand Up @@ -317,7 +321,7 @@ def images_to_matrix(image_list, mask=None, sigma=None, epsilon=0.5):
def listfunc(x):
if np.sum(np.array(x.shape) - np.array(mask.shape)) != 0:
x = reg.resample_image_to_target(x, mask, 2)
return x[mask]
return x.numpy()[mask.numpy().astype('bool')]

if mask is None:
mask = utils.get_mask(image_list[0])
Expand All @@ -334,7 +338,8 @@ def listfunc(x):
utils.smooth_image(img, sigma, sigma_in_physical_coordinates=True)
)
else:
data_matrix[i, :] = listfunc(img)
tmp_val = listfunc(img)
data_matrix[i, :] = tmp_val.flatten()
return data_matrix


Expand Down
11 changes: 6 additions & 5 deletions tests/test_core_ants_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,21 @@ def test_make_image(self):
self.assertTrue(ants.image_physical_space_consistency(img2,mask))

# set with arr.ndim > 1
img2 = ants.make_image(mask, voxval=np.expand_dims(arr,-1))
img2 = ants.make_image(mask, voxval=np.expand_dims(arr.numpy(),-1))
nptest.assert_allclose(img2.numpy(), (img*mask).numpy())
self.assertTrue(ants.image_physical_space_consistency(img2,mask))

#with self.assertRaises(Exception):
# # wrong number of non-zero voxels
# img3 = ants.make_image(img, voxval=arr)
with self.assertRaises(Exception):
# wrong number of non-zero voxels
img3 = ants.make_image(img, voxval=np.random.randn((100)))


def test_matrix_to_images(self):
# def matrix_to_images(data_matrix, mask):
for img in self.imgs:
imgmask = ants.image_clone( img > img.mean(), pixeltype = 'float' )
data = img[imgmask]
data = img[imgmask].numpy()
data = data[data > 0]
dataflat = data.reshape(1,-1)
mat = np.vstack([dataflat,dataflat]).astype('float32')
imglist = ants.matrix_to_images(mat, imgmask)
Expand Down

0 comments on commit 975a919

Please sign in to comment.