Skip to content

Commit

Permalink
revert back to original mask index functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ncullen93 committed May 15, 2024
1 parent beb891b commit d349a3a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 27 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
7 changes: 3 additions & 4 deletions ants/core/ants_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,9 @@ def __getitem__(self, idx):
])

if isinstance(idx, ANTsImage):
arr = self.numpy()
other = idx.numpy()
arr[other == 0] = 0
return self.new_image_like(arr)
if not image_physical_space_consistency(self, idx):
raise ValueError('images do not occupy same physical space')
return self.numpy().__getitem__(idx.numpy().astype('bool'))

ndim = len(idx)
sizes = list(self.shape)
Expand Down
11 changes: 3 additions & 8 deletions ants/core/ants_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,11 @@ 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 @@ -321,7 +317,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.numpy()[mask.numpy().astype('bool')]
return x[mask]

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


Expand Down Expand Up @@ -651,4 +646,4 @@ def image_write(image, filename, ri=False):
image.to_file(filename)

if ri:
return image
return image
2 changes: 1 addition & 1 deletion tests/test_core_ants_image_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_2d_vector(self):
img_v2 = img_v[:10,:10]

self.assertTrue(ants.allclose(img2, ants.split_channels(img_v2)[0]))
#

def test_2d_vector_multi(self):
img = ants.image_read(ants.get_data('r16'))
img2 = img[:10,:10]
Expand Down
13 changes: 6 additions & 7 deletions tests/test_core_ants_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,20 @@ 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.numpy(),-1))
img2 = ants.make_image(mask, voxval=np.expand_dims(arr,-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=np.random.randn((100)))
#with self.assertRaises(Exception):
# # wrong number of non-zero voxels
# img3 = ants.make_image(img, voxval=arr)


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].numpy()
data = data[data > 0]
data = img[imgmask]
dataflat = data.reshape(1,-1)
mat = np.vstack([dataflat,dataflat]).astype('float32')
imglist = ants.matrix_to_images(mat, imgmask)
Expand Down Expand Up @@ -348,4 +347,4 @@ def test_image_read_write(self):


if __name__ == '__main__':
run_tests()
run_tests()

0 comments on commit d349a3a

Please sign in to comment.