def warp_tracks(input_tracks_filename, input_flirt_fmatrix, input_fa_filename, output_filename = None, input_ref = '/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz'): print 'Loading fa, flirt matrix ...' img_fa = nib.load(input_fa_filename) flirt_affine= np.loadtxt(input_flirt_fmatrix) img_ref =nib.load(input_ref) #create affine matrix from flirt mat=flirt2aff(flirt_affine,img_fa,img_ref) #read tracks print 'Loading tracks ...' tensor_tracks = load_tracks(input_tracks_filename) #linear tranform for tractography tracks_warped_linear = transform_tracks(tensor_tracks,mat) if output_filename == None: filename_save = input_tracks_filename.split('.')[0]+'_linear.dpy' else: filename_save = os.path.abspath(output_filename) #save tracks_warped_linear print 'Saving warped tracks into :', filename_save dpr_linear = Dpy(filename_save, 'w') dpr_linear.write_tracks(tracks_warped_linear) dpr_linear.close() return filename_save
def warp_tracks_linearly(flirt_filename,fa_filename, tracks_filename,linear_filename): import nibabel as nib from dipy.external.fsl import flirt2aff fsl_ref = '/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz' img_fa = nib.load(fa_filename) flirt_affine= np.loadtxt(flirt_filename) img_ref =nib.load(fsl_ref) #create affine matrix from flirt mat=flirt2aff(flirt_affine,img_fa,img_ref) #read tracks tensor_tracks = load_whole_tract(tracks_filename) #linear tranform for tractography tracks_warped_linear = transform_tracks(tensor_tracks,mat) #save tracks_warped_linear dpr_linear = Dpy(linear_filename, 'w') dpr_linear.write_tracks(tracks_warped_linear) dpr_linear.close()
def warp_tracks_linearly(flirt_filename,fa_filename, tracks_filename,linear_filename): fsl_ref = '/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz' img_fa = nib.load(fa_filename) flirt_affine= np.loadtxt(flirt_filename) img_ref =nib.load(fsl_ref) #create affine matrix from flirt mat=flirt2aff(flirt_affine,img_fa,img_ref) #read tracks dpr = Dpy(tracks_filename, 'r') tensor_tracks = dpr.read_tracks() dpr.close() #linear tranform for tractography tracks_warped_linear = transform_tracks(tensor_tracks,mat) #save tracks_warped_linear dpr_linear = Dpy(linear_filename, 'w') dpr_linear.write_tracks(tracks_warped_linear) dpr_linear.close()
def humans(): no_seeds=10**6 visualize = False save_odfs = False dirname = "data/" for root, dirs, files in os.walk(dirname): if root.endswith('101_32'): base_dir = root+'/' filename = 'raw' base_filename = base_dir + filename nii_filename = base_filename + 'bet.nii.gz' bvec_filename = base_filename + '.bvec' bval_filename = base_filename + '.bval' flirt_mat = base_dir + 'DTI/flirt.mat' fa_filename = base_dir + 'DTI/fa.nii.gz' fsl_ref = '/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz' dpy_filename = base_dir + 'DTI/res_tracks_dti.dpy' print bvec_filename img = nib.load(nii_filename) data = img.get_data() affine = img.get_affine() bvals = np.loadtxt(bval_filename) gradients = np.loadtxt(bvec_filename).T # this is the unitary direction of the gradient tensors = Tensor(data, bvals, gradients, thresh=50) FA = tensors.fa() FA = tensors.fa() famask=FA>=.2 ds=DiffusionSpectrum(data,bvals,gradients,odf_sphere='symmetric642',mask=famask,half_sphere_grads=True,auto=True,save_odfs=save_odfs) gq=GeneralizedQSampling(data,bvals,gradients,1.2,odf_sphere='symmetric642',mask=famask,squared=False,save_odfs=save_odfs) ei=EquatorialInversion(data,bvals,gradients,odf_sphere='symmetric642',mask=famask,half_sphere_grads=True,auto=False,save_odfs=save_odfs,fast=True) ei.radius=np.arange(0,5,0.4) ei.gaussian_weight=0.05 ei.set_operator('laplacian') ei.update() ei.fit() ds.PK[FA<.2]=np.zeros(5) ei.PK[FA<.2]=np.zeros(5) gq.PK[FA<.2]=np.zeros(5) print 'create seeds' x,y,z,g=ei.PK.shape seeds=np.zeros((no_seeds,3)) sid=0 while sid<no_seeds: rx=(x-1)*np.random.rand() ry=(y-1)*np.random.rand() rz=(z-1)*np.random.rand() seed=np.ascontiguousarray(np.array([rx,ry,rz]),dtype=np.float64) seeds[sid]=seed sid+=1 euler = EuDX(a=FA, ind=tensors.ind(), seeds=seeds, a_low=.2) dt_tracks = [track for track in euler] euler2 = EuDX(a=ds.PK, ind=ds.IN, seeds=seeds, odf_vertices=ds.odf_vertices, a_low=.2) ds_tracks = [track for track in euler2] euler3 = EuDX(a=gq.PK, ind=gq.IN, seeds=seeds, odf_vertices=gq.odf_vertices, a_low=.2) gq_tracks = [track for track in euler3] euler4 = EuDX(a=ei.PK, ind=ei.IN, seeds=seeds, odf_vertices=ei.odf_vertices, a_low=.2) ei_tracks = [track for track in euler4] if visualize: renderer = fvtk.ren() fvtk.add(renderer, fvtk.line(tensor_tracks, fvtk.red, opacity=1.0)) fvtk.show(renderer) print 'Load images to be used for registration' img_fa =nib.load(fa_filename) img_ref =nib.load(fsl_ref) mat=flirt2aff(np.loadtxt(flirt_mat),img_fa,img_ref) del img_fa del img_ref print 'transform the tracks' dt_linear = transform_tracks(dt_tracks,mat) ds_linear = transform_tracks(ds_tracks,mat) gq_linear = transform_tracks(gq_tracks,mat) ei_linear = transform_tracks(ei_tracks,mat) print 'save tensor tracks' dpy_filename = base_dir + 'DTI/dt_linear.dpy' print dpy_filename dpr_linear = Dpy(dpy_filename, 'w') dpr_linear.write_tracks(dt_linear) dpr_linear.close() print 'save ei tracks' dpy_filename = base_dir + 'DTI/ei_linear.dpy' print dpy_filename dpr_linear = Dpy(dpy_filename, 'w') dpr_linear.write_tracks(ei_linear) dpr_linear.close() print 'save ds tracks' dpy_filename = base_dir + 'DTI/ds_linear.dpy' print dpy_filename dpr_linear = Dpy(dpy_filename, 'w') dpr_linear.write_tracks(ds_linear) dpr_linear.close() print 'save gq tracks' dpy_filename = base_dir + 'DTI/gq_linear.dpy' print dpy_filename dpr_linear = Dpy(dpy_filename, 'w') dpr_linear.write_tracks(gq_linear) dpr_linear.close() print 'save lengths' pkl_filename = base_dir + 'DTI/dt_lengths.pkl' save_pickle(pkl_filename,lengths(dt_linear)) pkl_filename = base_dir + 'DTI/ei_lengths.pkl' save_pickle(pkl_filename,lengths(ei_linear)) pkl_filename = base_dir + 'DTI/gq_lengths.pkl' save_pickle(pkl_filename,lengths(gq_linear)) pkl_filename = base_dir + 'DTI/ds_lengths.pkl' save_pickle(pkl_filename,lengths(ds_linear))
if __name__=='__main__': dname='/home/eg309/Data/orbital_phantoms/dwi_dir/subject1/' fsl_ref = '/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz' img_ref =nib.load(fsl_ref) ffa='data/subj_01/101_32/DTI/fa.nii.gz' fmat='data/subj_01/101_32/DTI/flirt.mat' img_fa =nib.load(ffa) img_ref =nib.load(fsl_ref) ref_shape=img_ref.get_data().shape mat=flirt2aff(np.loadtxt(fmat),img_fa,img_ref) ftracks_dti=dname+'dti_tracks.dpy' ftracks_dsi=dname+'dsi_tracks.dpy' ftracks_gqi=dname+'gqi_tracks.dpy' ftracks_eit=dname+'eit_tracks.dpy' ftracks=[ftracks_dti,ftracks_dsi,ftracks_gqi,ftracks_eit] #load data fraw=dname+'data.nii.gz' fbval=dname+'bvals' fbvec=dname+'bvecs' img = nib.load(fraw) data = img.get_data() affine = img.get_affine()