def setUp(self): img2d = ants.image_read(ants.get_ants_data("r16")) img3d = ants.image_read(ants.get_ants_data("mni")) self.imgs = [img2d, img3d] arr2d = np.random.randn(69, 70, 4).astype("float32") arr3d = np.random.randn(69, 70, 71, 4).astype("float32") self.comparrs = [arr2d, arr3d]
def test_example(self): # test ANTsPy/ANTsR example fixed = ants.image_read(ants.get_ants_data("r16")) moving = ants.image_read(ants.get_ants_data("r64")) fixed = ants.resample_image(fixed, (64, 64), 1, 0) moving = ants.resample_image(moving, (64, 64), 1, 0) mytx = ants.registration(fixed=fixed, moving=moving, type_of_transform="SyN") mywarpedimage = ants.apply_transforms( fixed=fixed, moving=moving, transformlist=mytx["fwdtransforms"]) # bad interpolator with self.assertRaises(Exception): mywarpedimage = ants.apply_transforms( fixed=fixed, moving=moving, transformlist=mytx["fwdtransforms"], interpolator="unsupported-interp", ) # transform doesnt exist with self.assertRaises(Exception): mywarpedimage = ants.apply_transforms( fixed=fixed, moving=moving, transformlist=["blah-blah.mat"], interpolator="unsupported-interp", )
def setUp(self): img2d = ants.image_read(ants.get_ants_data('r16')) img3d = ants.image_read(ants.get_ants_data('mni')) self.imgs = [img2d, img3d] arr2d = np.random.randn(69, 70, 4).astype('float32') arr3d = np.random.randn(69, 70, 71, 4).astype('float32') self.comparrs = [arr2d, arr3d]
def test_affine_interface(self): print("Starting affine interface registration test") fi = ants.image_read(ants.get_ants_data("r16")) mi = ants.image_read(ants.get_ants_data("r64")) with self.assertRaises(ValueError): ants.registration( fixed=fi, moving=mi, type_of_transform="Translation", aff_iterations=4, aff_shrink_factors=4, aff_smoothing_sigmas=(4, 4), ) mytx = ants.registration( fixed=fi, moving=mi, type_of_transform="Affine", aff_iterations=(4, 4), aff_shrink_factors=(4, 4), aff_smoothing_sigmas=(4, 4), ) mytx = ants.registration( fixed=fi, moving=mi, type_of_transform="Translation", aff_iterations=4, aff_shrink_factors=4, aff_smoothing_sigmas=4, )
def test_reorient_image(self): image = ants.image_read(ants.get_ants_data('r16')) ants.reorient_image(image, (1, 0)) image = ants.image_read(ants.get_ants_data('r16')) image = image.clone('unsigned int') ants.reorient_image(image, (1, 0))
def test_reorient_image(self): image = ants.image_read(ants.get_ants_data("r16")) ants.reorient_image(image, (1, 0)) image = ants.image_read(ants.get_ants_data("r16")) image = image.clone("unsigned int") ants.reorient_image(image, (1, 0))
def test_weingarten_image_curvature_example(self): image = ants.image_read(ants.get_ants_data('mni')).resample_image( (3, 3, 3)) imagecurv = ants.weingarten_image_curvature(image) image2 = ants.image_read(ants.get_ants_data('r16')).resample_image( (2, 2)) imagecurv2 = ants.weingarten_image_curvature(image2)
def setUp(self): img2d = ants.image_read(ants.get_ants_data('r16')) img3d = ants.image_read(ants.get_ants_data('mni')) metric2d = ants.new_ants_metric(precision='float', dimension=2) metric3d = ants.new_ants_metric(precision='float', dimension=3) self.imgs = [img2d, img3d] self.metrics = [metric2d, metric3d]
def test_example(self): fi = ants.image_read( ants.get_ants_data( 'r16' ) ) mi = ants.image_read( ants.get_ants_data( 'r64' ) ) mygr = ants.create_warped_grid( mi ) mytx = ants.registration(fixed=fi, moving=mi, type_of_transform=('SyN') ) mywarpedgrid = ants.create_warped_grid( mi, grid_directions=(False,True), transform=mytx['fwdtransforms'], fixed_reference_image=fi )
def test_render_surface_function_example(self): mni = ants.image_read(ants.get_ants_data('mni')) mnia = ants.image_read(ants.get_ants_data('mnia')) filename = mktemp(suffix='.html') ants.render_surface_function(mni, mnia, alphasurf=0.1, auto_open=False, filename=filename)
def setUp(self): img2d = ants.image_read(ants.get_ants_data('r16')) img3d = ants.image_read(ants.get_ants_data('mni')) tx2d = ants.create_ants_transform(precision='float', dimension=2) tx3d = ants.create_ants_transform(precision='float', dimension=3) self.imgs = [img2d, img3d] self.txs = [tx2d, tx3d] self.pixeltypes = ['unsigned char', 'unsigned int', 'float']
def test_example(self): fi = ants.image_read( ants.get_ants_data('r16')) mi = ants.image_read( ants.get_ants_data('r64')) fi = ants.resample_image(fi,(128,128),1,0) mi = ants.resample_image(mi,(128,128),1,0) mytx = ants.registration(fixed=fi , moving=mi, type_of_transform = ('SyN') ) try: jac = ants.create_jacobian_determinant_image(fi,mytx['fwdtransforms'][0],1) except: pass
def setUp(self): img2d = ants.image_read(ants.get_ants_data('r16')).clone('float') img3d = ants.image_read(ants.get_ants_data('mni')).clone('float') arr2d = np.random.randn(69,70).astype('float32') arr3d = np.random.randn(69,70,71).astype('float32') vecimg2d = ants.from_numpy(np.random.randn(69,70,4), has_components=True) vecimg3d = ants.from_numpy(np.random.randn(69,70,71,2), has_components=True) self.imgs = [img2d, img3d] self.arrs = [arr2d, arr3d] self.vecimgs = [vecimg2d, vecimg3d] self.pixeltypes = ['unsigned char', 'unsigned int', 'float']
def test_invariate_image_similarity_example(self): img1 = ants.image_read(ants.get_ants_data('r16')) img2 = ants.image_read(ants.get_ants_data('r64')) metric1 = ants.invariant_image_similarity(img1, img2, do_reflection=False) img1 = ants.image_read(ants.get_ants_data('r16')) img2 = ants.image_read(ants.get_ants_data('r64')) metric2 = ants.invariant_image_similarity(img1, img2, do_reflection=True)
def test_ndimage_to_list(self): image = ants.image_read(ants.get_ants_data('r16')) image2 = ants.image_read(ants.get_ants_data('r64')) ants.set_spacing(image, (2, 2)) ants.set_spacing(image2, (2, 2)) imageTar = ants.make_image((*image2.shape, 2)) ants.set_spacing(imageTar, (2, 2, 2)) image3 = ants.list_to_ndimage(imageTar, [image, image2]) self.assertEqual(image3.dimension, 3) ants.set_direction(image3, np.eye(3) * 2) images_unmerged = ants.ndimage_to_list(image3) self.assertEqual(len(images_unmerged), 2) self.assertEqual(images_unmerged[0].dimension, 2)
def test_registration_types(self): print('Starting long registration interface test') fi = ants.image_read(ants.get_ants_data('r16')) mi = ants.image_read(ants.get_ants_data('r64')) fi = ants.resample_image(fi, (60,60), 1, 0) mi = ants.resample_image(mi, (60,60), 1, 0) for ttype in self.transform_types: mytx = ants.registration(fixed=fi, moving=mi, type_of_transform=ttype) # with mask fimask = fi > fi.mean() mytx = ants.registration(fixed=fi, moving=mi, mask=fimask, type_of_transform=ttype) print('Finished long registration interface test')
def test_resample_returns_NaNs(self): """ Test that resampling an image doesnt cause the resampled image to have NaNs - previously caused by resampling an image of type DOUBLE """ img2d = ants.image_read(ants.get_ants_data('r16')) img2dr = ants.resample_image(img2d, (2, 2), 0, 0) self.assertTrue(np.sum(np.isnan(img2dr.numpy())) == 0) img3d = ants.image_read(ants.get_ants_data('mni')) img3dr = ants.resample_image(img3d, (2, 2, 2), 0, 0) self.assertTrue(np.sum(np.isnan(img3dr.numpy())) == 0)
def test_initialize_eigenanatomy_example(self): mat = np.random.randn(4, 100).astype('float32') init = ants.initialize_eigenanatomy(mat) img = ants.image_read(ants.get_ants_data('r16')) segs = ants.kmeans_segmentation(img, 3) init = ants.initialize_eigenanatomy(segs['segmentation'])
def test_invert_ants_transform(self): img = ants.image_read(ants.get_ants_data("r16")).clone('float') tx = ants.new_ants_transform(dimension=2) tx.set_parameters((0.9, 0, 0, 1.1, 10, 11)) img_transformed = tx.apply_to_image(img, img) inv_tx = ants.invert_ants_transform(tx) img_orig = inv_tx.apply_to_image(img_transformed, img_transformed)
def setUp(self): img2d = ants.image_read(ants.get_ants_data('r16')) img3d = ants.image_read(ants.get_ants_data('mni')) tx2d = ants.create_ants_transform(precision='float', dimension=2) tx3d = ants.create_ants_transform(precision='float', dimension=3) self.imgs = [img2d, img3d] self.txs = [tx2d, tx3d] self.pixeltypes = ['unsigned char', 'unsigned int', 'float'] self.matrix_offset_types = {'AffineTransform', 'CenteredAffineTransform', 'Euler2DTransform', 'Euler3DTransform', 'Rigid2DTransform', 'QuaternionRigidTransform', 'Similarity2DTransform', 'CenteredSimilarity2DTransform', 'Similarity3DTransform', 'CenteredRigid2DTransform', 'CenteredEuler3DTransform', 'Rigid3DTransform'}
def test_fit_bspline_object_to_scattered_data_3d_scalar_field_example( self): number_of_random_points = 10000 img = ants.image_read(ants.get_ants_data("mni")) img_array = img.numpy() row_indices = np.random.choice(range(2, img_array.shape[0]), number_of_random_points) col_indices = np.random.choice(range(2, img_array.shape[1]), number_of_random_points) dep_indices = np.random.choice(range(2, img_array.shape[2]), number_of_random_points) scattered_data = np.zeros((number_of_random_points, 1)) parametric_data = np.zeros((number_of_random_points, 3)) for i in range(number_of_random_points): scattered_data[i, 0] = img_array[row_indices[i], col_indices[i], dep_indices[i]] parametric_data[i, 0] = row_indices[i] parametric_data[i, 1] = col_indices[i] parametric_data[i, 2] = dep_indices[i] bspline_img = ants.fit_bspline_object_to_scattered_data( scattered_data, parametric_data, parametric_domain_origin=[0.0, 0.0, 0.0], parametric_domain_spacing=[1.0, 1.0, 1.0], parametric_domain_size=img.shape, number_of_fitting_levels=5, mesh_size=1)
def time_nifti_to_numpy(N_TRIALS): """ Times how fast a framework can read a nifti file and convert it to numpy """ img_paths = [ants.get_ants_data('mni')] * 10 def test_nibabel(): for img_path in img_paths: array = np.asanyarray(nib.load(img_path).dataobj) def test_itk(): for img_path in img_paths: array = itk.GetArrayFromImage(itk.imread(img_path)) def test_ants(): for img_path in img_paths: array = ants.image_read(img_path, pixeltype='float').numpy() nib_start = time.time() for i in range(N_TRIALS): test_nibabel() nib_end = time.time() print('NIBABEL TIME: %.3f seconds' % (nib_end - nib_start)) itk_start = time.time() for i in range(N_TRIALS): test_itk() itk_end = time.time() print('ITK TIME: %.3f seconds' % (itk_end - itk_start)) ants_start = time.time() for i in range(N_TRIALS): test_ants() ants_end = time.time() print('ANTS TIME: %.3f seconds' % (ants_end - ants_start))
def test_example(self): image = ants.image_read(ants.get_ants_data('r16')) image2 = ants.image_read(ants.get_ants_data('r27')) demonsMetric = ['demons', image, image2, 1, 1] ccMetric = ['CC', image, image2, 2, 1] metrics = list() metrics.append(demonsMetric) reg3 = ants.registration(image, image2, 'SyNOnly', multivariate_extras=metrics) metrics.append(ccMetric) reg2 = ants.registration(image, image2, 'SyNOnly', multivariate_extras=metrics, verbose=True)
def test_example(self): image = ants.image_read(ants.get_ants_data("r16")) image2 = ants.image_read(ants.get_ants_data("r27")) demonsMetric = ["demons", image, image2, 1, 1] ccMetric = ["CC", image, image2, 2, 1] metrics = list() metrics.append(demonsMetric) reg3 = ants.registration(image, image2, "SyNOnly", multivariate_extras=metrics) metrics.append(ccMetric) reg2 = ants.registration(image, image2, "SyNOnly", multivariate_extras=metrics, verbose=True)
def test_nibabel(self): fn = ants.get_ants_data('mni') ants_img = ants.image_read(fn) nii_mni = nib.load(fn) ants_mni = ants_img.to_nibabel() self.assertTrue((ants_mni.get_qform() == nii_mni.get_qform()).all()) temp = ants.from_nibabel(nii_mni) self.assertTrue(ants.image_physical_space_consistency(ants_img, temp))
def test_get_center_of_mass(self): fi = ants.image_read(ants.get_ants_data("r16")) com = ants.get_center_of_mass(fi) self.assertEqual(len(com), fi.dimension) fi = ants.image_read(ants.get_ants_data("r64")) com = ants.get_center_of_mass(fi) self.assertEqual(len(com), fi.dimension) fi = fi.clone("unsigned int") com = ants.get_center_of_mass(fi) self.assertEqual(len(com), fi.dimension) # 3d img = ants.image_read(ants.get_ants_data("mni")) com = ants.get_center_of_mass(img) self.assertEqual(len(com), img.dimension)
def test_vol_example(self): ch2i = ants.image_read(ants.get_ants_data("mni")) ch2seg = ants.threshold_image(ch2i, "Otsu", 3) wm = ants.threshold_image(ch2seg, 3, 3) kimg = ants.weingarten_image_curvature(ch2i, 1.5).smooth_image(1) rp = [(90, 180, 90), (90, 180, 270), (90, 180, 180)] filename = mktemp(suffix='.png') result = ants.vol(wm, [kimg], quantlimits=(0.01, 0.99), filename=filename)
def test_crop_image_example(self): fi = ants.image_read(ants.get_ants_data('r16')) cropped = ants.crop_image(fi) cropped = ants.crop_image(fi, fi, 100) # image not float type cropped = ants.crop_image(fi.clone('unsigned int')) # label image not float cropped = ants.crop_image(fi, fi.clone('unsigned int'), 100)
def test_example(self): img = ants.image_read(ants.get_ants_data('r16'), 2) img = ants.resample_image(img, (64, 64), 1, 0) mask = ants.get_mask(img) segs = ants.kmeans_segmentation(img, k=3, kmask=mask) thick = ants.kelly_kapowski(s=segs['segmentation'], g=segs['probabilityimages'][1], w=segs['probabilityimages'][2], its=45, r=0.5, m=1)
def test_decrop_image_example(self): fi = ants.image_read(ants.get_ants_data('r16')) mask = ants.get_mask(fi) cropped = ants.crop_image(fi, mask, 1) cropped = ants.smooth_image(cropped, 1) decropped = ants.decrop_image(cropped, fi) # image not float cropped = ants.crop_image(fi.clone('unsigned int'), mask, 1) # full image not float cropped = ants.crop_image(fi, mask.clone('unsigned int'), 1)