def test_example(self): ref = ants.image_read(ants.get_ants_data('r16')) ref = ants.resample_image(ref, (50, 50), 1, 0) ref = ants.iMath(ref, 'Normalize') mi = ants.image_read(ants.get_ants_data('r27')) mi2 = ants.image_read(ants.get_ants_data('r30')) mi3 = ants.image_read(ants.get_ants_data('r62')) mi4 = ants.image_read(ants.get_ants_data('r64')) mi5 = ants.image_read(ants.get_ants_data('r85')) refmask = ants.get_mask(ref) refmask = ants.iMath(refmask, 'ME', 2) # just to speed things up ilist = [mi, mi2, mi3, mi4, mi5] seglist = [None] * len(ilist) for i in range(len(ilist)): ilist[i] = ants.iMath(ilist[i], 'Normalize') mytx = ants.registration(fixed=ref, moving=ilist[i], typeofTransform=('Affine')) mywarpedimage = ants.apply_transforms( fixed=ref, moving=ilist[i], transformlist=mytx['fwdtransforms']) ilist[i] = mywarpedimage seg = ants.threshold_image(ilist[i], 'Otsu', 3) seglist[i] = (seg) + ants.threshold_image(seg, 1, 3).morphology( operation='dilate', radius=3) r = 2 pp = ants.joint_label_fusion(ref, refmask, ilist, r_search=2, label_list=seglist, rad=[r] * ref.dimension) pp = ants.joint_label_fusion(ref, refmask, ilist, r_search=2, rad=2)
def runeachsess(subj,ses): targ = os.path.join(derivatives,"sub-"+subj,"ses-"+ses,"anat") # find path to anat folder for this subject and this session fpaths = glob.glob(targ+"/*T2w.nii.gz") # find the T1 fpaths = ''.join(fpaths) if len(fpaths) > 0: img = ants.image_read(fpaths) mask = ants.get_mask(img) dn = ants.denoise_image(img,mask,noise_model='Rician') ants.image_write(dn,fpaths[0:-7]+'_dn.nii.gz',)
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)
def test_eig_seg_example(self): mylist = [ ants.image_read(ants.get_ants_data('r16')), ants.image_read(ants.get_ants_data('r27')), ants.image_read(ants.get_ants_data('r85')) ] myseg = ants.eig_seg(ants.get_mask(mylist[0]), mylist) mylist = [ ants.image_read(ants.get_ants_data('r16')), ants.image_read(ants.get_ants_data('r27')), ants.image_read(ants.get_ants_data('r85')) ] myseg = ants.eig_seg(ants.get_mask(mylist[0]), mylist, cthresh=2) mylist = [ ants.image_read(ants.get_ants_data('r16')), ants.image_read(ants.get_ants_data('r27')), ants.image_read(ants.get_ants_data('r85')) ] myseg = ants.eig_seg(ants.get_mask(mylist[0]), mylist, apply_segmentation_to_images=True)
def test_get_neighborhood_in_mask_example(self): img = ants.image_read(ants.get_ants_data('r16')) mask = ants.get_mask(img) mat = ants.get_neighborhood_in_mask(img, mask, radius=(2, 2)) # image not ANTsImage with self.assertRaises(Exception): ants.get_neighborhood_in_mask(2, mask, radius=(2, 2)) # mask not ANTsImage with self.assertRaises(Exception): ants.get_neighborhood_in_mask(img, 2, radius=(2, 2)) # radius not right length with self.assertRaises(Exception): ants.get_neighborhood_in_mask(img, 2, radius=(2, 2, 2, 2)) # radius is just a float/int mat = ants.get_neighborhood_in_mask(img, mask, radius=2) # boundary condition == 'image' mat = ants.get_neighborhood_in_mask(img, mask, radius=(2, 2), boundary_condition='image') # boundary condition == 'mean' mat = ants.get_neighborhood_in_mask(img, mask, radius=(2, 2), boundary_condition='mean') # spatial info mat = ants.get_neighborhood_in_mask(img, mask, radius=(2, 2), spatial_info=True) # get_gradient mat = ants.get_neighborhood_in_mask(img, mask, radius=(2, 2), get_gradient=True)
def test_multiple_inputs(self): img = ants.image_read(ants.get_ants_data("r16")) img = ants.resample_image(img, (64, 64), 1, 0) mask = ants.get_mask(img) segs1 = ants.atropos(a=img, m='[0.2,1x1]', c='[2,0]', i='kmeans[3]', x=mask) # Use probabilities from k-means seg as priors segs2 = ants.atropos(a=img, m='[0.2,1x1]', c='[2,0]', i=segs1['probabilityimages'], x=mask) # multiple inputs feats = [img, ants.iMath(img, "Laplacian"), ants.iMath(img, "Grad")] segs3 = ants.atropos(a=feats, m='[0.2,1x1]', c='[2,0]', i=segs1['probabilityimages'], x=mask)
def test_get_mask_example(self): image = ants.image_read(ants.get_ants_data('r16')) mask = ants.get_mask(image)
def test_example(self): # test ANTsPy/ANTsR example img = ants.image_read(ants.get_ants_data('r16')) img = ants.resample_image(img, (64, 64), 1, 0) mask = ants.get_mask(img) ants.atropos(a=img, m='[0.2,1x1]', c='[2,0]', i='kmeans[3]', x=mask)
def test_morphology(self): fi = ants.image_read(ants.get_ants_data("r16")) mask = ants.get_mask(fi) dilated_ball = ants.morphology(mask, operation="dilate", radius=3, mtype="binary", shape="ball") eroded_box = ants.morphology(mask, operation="erode", radius=3, mtype="binary", shape="box") opened_annulus = ants.morphology( mask, operation="open", radius=5, mtype="binary", shape="annulus", thickness=2, ) ## --- ## mtype = "binary" mask = mask.clone() for operation in {"dilate", "erode", "open", "close"}: for shape in {"ball", "box", "cross", "annulus", "polygon"}: morph = ants.morphology(mask, operation=operation, radius=3, mtype=mtype, shape=shape) self.assertTrue( isinstance(morph, ants.core.ants_image.ANTsImage)) # invalid operation with self.assertRaises(Exception): ants.morphology(mask, operation="invalid-operation", radius=3, mtype=mtype, shape=shape) mtype = "grayscale" img = ants.image_read(ants.get_ants_data("r16")) for operation in {"dilate", "erode", "open", "close"}: morph = ants.morphology(img, operation=operation, radius=3, mtype=mtype) self.assertTrue(isinstance(morph, ants.core.ants_image.ANTsImage)) # invalid operation with self.assertRaises(Exception): ants.morphology(img, operation="invalid-operation", radius=3, mtype="grayscale") # invalid morphology type with self.assertRaises(Exception): ants.morphology(img, operation="dilate", radius=3, mtype="invalid-morphology") # invalid shape with self.assertRaises(Exception): ants.morphology( mask, operation="dilate", radius=3, mtype="binary", shape="invalid-shape", )
id = '2001' t1fn = rdir + 'data/LS' + id + "/unprocessed/3T/T1w_MPR1/LS"+id+"_3T_T1w_MPR1_gdc.nii.gz" # now the bold data boldfnsL = rdir + "data/LS2001/LS2001fmri/unprocessed/3T/rfMRI_REST1_LR/LS2001_3T_rfMRI_REST1_LR_gdc.nii.gz" boldfnsR = rdir + "data/LS2001/LS2001fmri/unprocessed/3T/rfMRI_REST1_RL/LS2001_3T_rfMRI_REST1_RL_gdc.nii.gz" # get the ref data reffns1 = rdir + 'data/LS2001/LS2001fmri/unprocessed/3T/rfMRI_REST1_LR/LS2001_3T_rfMRI_REST1_LR_SBRef_gdc.nii.gz' reffns2 = rdir + 'data/LS2001/LS2001fmri/unprocessed/3T/rfMRI_REST1_RL/LS2001_3T_rfMRI_REST1_RL_SBRef_gdc.nii.gz' ## Distortion correction (without a field map) i1 = ants.image_read( reffns1 ) i2 = ants.image_read( reffns2 ) und = ants.build_template( i1, ( i1, i2 ), 3, gradient_step = 0.5 ) t1 = ants.image_read( t1fn ).n3_bias_field_correction( 8 ).n3_bias_field_correction( 4 ) bmask = ants.get_mask( und, low_thresh = und.mean() * 0.75, high_thresh=1e9, cleanup = 3 ).iMath_fill_holes() # ants.plot( und, bmask, axis=2, overlay_alpha = 0.33 ) # this is a fragile approach - not really recommended - but it is quick t1mask = ants.get_mask( t1, low_thresh = t1.mean() * 1.1, high_thresh=1e9, cleanup = 5 ).iMath_fill_holes() # ants.plot( t1, t1mask, axis=2, overlay_alpha = 0.33 ) t1rig = ants.registration( und * bmask, t1 * t1mask, "BOLDRigid" ) t1reg = ants.registration( und * bmask, t1 * t1mask, "SyNOnly", initialTransform = t1rig['fwdtransforms'], synMetric = 'CC', synSampling = 2, regIterations = (5) ) ########################### # The distortion to the T1 is greatly reduced. # Brain masking # Use the BOLD mask to extract the brain from the t1
def test_morphology(self): fi = ants.image_read(ants.get_ants_data('r16')) mask = ants.get_mask(fi) dilated_ball = ants.morphology(mask, operation='dilate', radius=3, mtype='binary', shape='ball') eroded_box = ants.morphology(mask, operation='erode', radius=3, mtype='binary', shape='box') opened_annulus = ants.morphology(mask, operation='open', radius=5, mtype='binary', shape='annulus', thickness=2) ## --- ## mtype = 'binary' mask = mask.clone() for operation in {'dilate', 'erode', 'open', 'close'}: for shape in {'ball', 'box', 'cross', 'annulus', 'polygon'}: morph = ants.morphology(mask, operation=operation, radius=3, mtype=mtype, shape=shape) self.assertTrue( isinstance(morph, ants.core.ants_image.ANTsImage)) # invalid operation with self.assertRaises(Exception): ants.morphology(mask, operation='invalid-operation', radius=3, mtype=mtype, shape=shape) mtype = 'grayscale' img = ants.image_read(ants.get_ants_data('r16')) for operation in {'dilate', 'erode', 'open', 'close'}: morph = ants.morphology(img, operation=operation, radius=3, mtype=mtype) self.assertTrue(isinstance(morph, ants.core.ants_image.ANTsImage)) # invalid operation with self.assertRaises(Exception): ants.morphology(img, operation='invalid-operation', radius=3, mtype='grayscale') #invalid morphology type with self.assertRaises(Exception): ants.morphology(img, operation='dilate', radius=3, mtype='invalid-morphology') # invalid shape with self.assertRaises(Exception): ants.morphology(mask, operation='dilate', radius=3, mtype='binary', shape='invalid-shape')
def test_mask_image_example(self): myimage = ants.image_read(ants.get_ants_data('r16')) mask = ants.get_mask(myimage) myimage_mask = ants.mask_image(myimage, mask, 3) seg = ants.kmeans_segmentation(myimage, 3) myimage_mask = ants.mask_image(myimage, seg['segmentation'], (1, 3))
def test_labels_to_matrix_example(self): fi = ants.image_read(ants.get_ants_data('r16')).resample_image( (60, 60), 1, 0) mask = ants.get_mask(fi) labs = ants.kmeans_segmentation(fi, 3)['segmentation'] labmat = ants.labels_to_matrix(labs, mask)
from joblib import Parallel, delayed import multiprocessing derivatives = '/Volumes/data/prebiostress/data/derivatives' layout = BIDSLayout(derivatives) # ignore the error here # do T1 for subj in layout.get_subjects(): # loop on number of subjects print(subj) for ses in layout.get_sessions(): # loop on number of sessions targ = os.path.join(derivatives,"sub-"+subj,"ses-"+ses,"anat") # find path to anat folder for this subject and this session fpaths = glob.glob(targ+"/*T1w.nii.gz") # find the T1 fpaths = ''.join(fpaths) if len(fpaths) > 0: img = ants.image_read(fpaths) mask = ants.get_mask(img) dn = ants.denoise_image(img,mask,noise_model='Gaussian') ants.image_write(dn,fpaths[0:-7]+'_dn.nii.gz',) # do T2 def runeachsess(subj,ses): targ = os.path.join(derivatives,"sub-"+subj,"ses-"+ses,"anat") # find path to anat folder for this subject and this session fpaths = glob.glob(targ+"/*T2w.nii.gz") # find the T1 fpaths = ''.join(fpaths) if len(fpaths) > 0: img = ants.image_read(fpaths) mask = ants.get_mask(img) dn = ants.denoise_image(img,mask,noise_model='Rician') ants.image_write(dn,fpaths[0:-7]+'_dn.nii.gz',) for subj in layout.get_subjects():
def __init__(self, ants_image=[[0.0]], **options): import ants self.mask = ants.get_mask(ants_image, **options)
def get_mask(self): mask = ants.get_mask(self.get_brain()) mask = ants.iMath(mask, 'ME', 2) return mask
link = '/ROBEX_Output/' # Skull stripped data output by ROBEX mylist = os.listdir('/ROBEX_Output/') # fixed image is the template to which the registration will be fitted found in the downloaded ANTs package fixed = ants.image_read('/ANTsPy-master/data/mni.nii.gz') # This runs the entire dataset through registration for i in mylist: #registraction moving = ants.image_read('/arc/project/ex-rtam-1/BrainExt_AllData/' + i) mytx = ants.registration(fixed=fixed, moving=moving, type_of_transform='SyNAggro') mywarpedimage = ants.apply_transforms(fixed=fixed, moving=moving, transformlist=mytx['fwdtransforms']) mywarpedimage.to_file('/Output_ants_SyNAggro/Registration/' + i) #segmentation mask = ants.get_mask(mywarpedimage) img_seg = ants.atropos(a=mywarpedimage, m='[0.2,1x1x1]', c='[2,0]', i='kmeans[3]', x=mask) gm = img_seg['probabilityimages'][1] gm.to_file('/Output_ants_SyNAggro/Greymatter/' + i) continue
def test_mask_image_example(self): myimage = ants.image_read(ants.get_ants_data("r16")) mask = ants.get_mask(myimage) myimage_mask = ants.mask_image(myimage, mask, (2, 3)) seg = ants.kmeans_segmentation(myimage, 3) myimage_mask = ants.mask_image(myimage, seg["segmentation"], (1, 3))