Exemple #1
0
def warp_reference_grid(flaff,fdis,fref,ffaw):
    ''' Warp reference space grid using fsl displacements
   
    Parameters
    ----------
    flaff: filename of .mat  (flirt)
    fdis:  filename of displacements (fnirtfileutils)
    fref: filename of reference volume e.g. (FMRIB58_FA_1mm.nii.gz)

    Returns
    -------
    warped grid in reference index space
   
    '''
   
    refaff=nib.load(fref).get_affine()   
    disdata=nib.load(fdis).get_data()
    #from fa index to ref index
    res=flirt2aff_files(flaff,ffa,fref)
    #create the 4d volume which has the indices for the reference image 
    refgrid=np.zeros(disdata.shape)
    #create the grid indices for the reference
    refgrid[...,0] = np.arange(disdata.shape[0])[:,newaxis,newaxis]
    refgrid[...,1] = np.arange(disdata.shape[1])[newaxis,:,newaxis]
    refgrid[...,2] = np.arange(disdata.shape[2])[newaxis,newaxis,:]     
    #hold the displacements' shape reshaping
    di,dj,dk,dl=disdata.shape
    N=di*dj*dk
    warpedgrid=np.zeros(disdata.shape)   
    for l in range(dl):
        warpedgrid[:,:,:,l]=mc(disdata[:,:,:,l].reshape(N,1),refgrid.reshape(N,dl).T,order=1).reshape(di,dj,dk)  

    warpedgrid = refgrid+np.dot(warpedgrid,res[:3,:3].T)+res[:3,3]

    return refgrid, warpedgrid
Exemple #2
0
def tracks_to_fmrib58(subj,fnames,data_type='fa'):    
    #affine transformation matrix (from flirt before nonlinear registration)
    faff  =dname+fnames[subj]+'_affine_transf.mat'
    #nonlinear displacements
    fdis  =dname+fnames[subj]+'_nonlin_displacements.nii.gz'#look at fa_to_fmrib58
    
    if data_type=='qa':
        #fqa_warp_dpy 
        fqa_warp  =dname+fnames[subj]+'_QA_warp.dpy'
        fwarp=fqa_warp
    if data_type=='fa':
        ffa_warp  =dname+fnames[subj]+'_FA_warp.dpy'
        fwarp=ffa_warp
    #fa
    ffa   =dname+fnames[subj]+'_bet_FA.nii.gz'
    ref_fname = '/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz'
            
    print faff
    print ffa
    print fdis
    
    im2im = flirt2aff_files(faff, ffa, ref_fname)
    dimg=ni.load(fdis)
    daff=dimg.get_affine()
    ddata=dimg.get_data()    
    
    di=ddata[:,:,:,0]#copy i
    dj=ddata[:,:,:,1]#copy j
    dk=ddata[:,:,:,2]#copy k 
    
    #WARP TRACKS IN BLOCKS
    print fwarp
    dprw=Dpy(fwarp,'r+')
    rows=len(dprw.f.root.streamlines.tracks)   
    blocks=np.round(np.linspace(0,rows,20)).astype(int)#lets work in blocks
    print rows
    for i in range(len(blocks)-1):        
        print blocks[i],blocks[i+1]        
        caboodle=dprw.f.root.streamlines.tracks[blocks[i]:blocks[i+1]]       
        ntrack=np.dot(caboodle,im2im[:3,:3].T)+im2im[:3,3] #from image vox space to mni image vox
        mci=mc(di,ntrack.T,order=1) #mapping for i
        mcj=mc(dj,ntrack.T,order=1) #mapping for j
        mck=mc(dk,ntrack.T,order=1) #mapping for k
        wtrack=ntrack+np.vstack((mci,mcj,mck)).T
        caboodlew=np.dot(wtrack,daff[:3,:3].T)+daff[:3,3]
        dprw.f.root.streamlines.tracks[blocks[i]:blocks[i+1]]=caboodlew.astype('f4')        
    
    dprw.close()
Exemple #3
0
def warp_tracks():
    dn='/home/eg309/Data/TEST_MR10032/subj_03/101/'
    ffa=dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_bet_FA.nii.gz'    
    finvw=dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_warps_in_bet_FA.nii.gz'    
    fqadpy=dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_QA_native.dpy'
    flaff=dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_affine_transf.mat'
    fref ='/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz'    
    fdis =dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_nonlin_displacements.nii.gz'
    fdis2 =dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_nonlin_displacements_withaff.nii.gz'
    #read some tracks
    dpr=Dpy(fqadpy,'r')
    T=dpr.read_indexed(range(150))
    dpr.close()
    
    #from fa index to ref index
    res=flirt2aff_files(flaff,ffa,fref)
    
    #load the reference img    
    imgref=ni.load(fref)
    refaff=imgref.get_affine()
    
    #load the invwarp displacements
    imginvw=ni.load(finvw)
    invwdata=imginvw.get_data()
    invwaff = imginvw.get_affine()
    
    #load the forward displacements
    imgdis=ni.load(fdis)
    disdata=imgdis.get_data()
    #load the forward displacements + affine
    imgdis2=ni.load(fdis2)
    disdata2=imgdis2.get_data()
    #from their difference create the affine
    disaff=imgdis2.get_data()-disdata  
    
    shift=np.array([disaff[...,0].mean(),disaff[...,1].mean(),disaff[...,2].mean()])
    
    shape=ni.load(ffa).get_data().shape
    
    disaff0=affine_transform(disaff[...,0],res[:3,:3],res[:3,3],shape,order=1)
    disaff1=affine_transform(disaff[...,1],res[:3,:3],res[:3,3],shape,order=1)
    disaff2=affine_transform(disaff[...,2],res[:3,:3],res[:3,3],shape,order=1)
    
    disdata0=affine_transform(disdata[...,0],res[:3,:3],res[:3,3],shape,order=1)
    disdata1=affine_transform(disdata[...,1],res[:3,:3],res[:3,3],shape,order=1)
    disdata2=affine_transform(disdata[...,2],res[:3,:3],res[:3,3],shape,order=1)
    
    #print disgrad0.shape,disgrad1.shape,disgrad2.shape
    #disdiff=np.empty(invwdata.shape)
    #disdiff[...,0]=disgrad0
    #disdiff[...,1]=disgrad1
    #disdiff[...,2]=disgrad2
    #ni.save(ni.Nifti1Image(disdiff,invwaff),'/tmp/disdiff.nii.gz')
    
    di=disdata0
    dj=disdata1
    dk=disdata2
    
    d2i=invwdata[:,:,:,0] + disaff0
    d2j=invwdata[:,:,:,1] + disaff1
    d2k=invwdata[:,:,:,2] + disaff2
    
    #di=disgrad0
    #dj=disgrad1
    #dk=disgrad2
    
    imgfa=ni.load(ffa)
    fadata=imgfa.get_data()
    faaff =imgfa.get_affine()
    
    Tw=[]
    Tw2=[]
    Tw3=[]
    
    froi='/home/eg309/Data/ICBM_Wmpm/ICBM_WMPM.nii'    
    
    roiI=get_roi(froi,3,1) #3 is GCC     
    roiI2=get_roi(froi,4,1) #4 is BCC
    roiI3=get_roi(froi,5,1) #4 is SCC
    roiI=np.vstack((roiI,roiI2,roiI3))  
  
    for t in T:
        if np.min(t[:,2])>=0:#to be removed
            mci=mc(di,t.T,order=1) #interpolations for i displacement
            mcj=mc(dj,t.T,order=1) #interpolations for j displacement
            mck=mc(dk,t.T,order=1) #interpolations for k displacement            
            D=np.vstack((mci,mcj,mck)).T                        
            WI=np.dot(t,res[:3,:3].T)+res[:3,3]+D#+ shift
            W=np.dot(WI,refaff[:3,:3].T)+refaff[:3,3]
            
            mc2i=mc(d2i,t.T,order=1) #interpolations for i displacement
            mc2j=mc(d2j,t.T,order=1) #interpolations for j displacement
            mc2k=mc(d2k,t.T,order=1) #interpolations for k displacement            
            D2=np.vstack((mc2i,mc2j,mc2k)).T                        
            WI2=np.dot(t,res[:3,:3].T)+res[:3,3]+D2 #+ shift
            W2=np.dot(WI2,refaff[:3,:3].T)+refaff[:3,3]
                        
            WI3=np.dot(t,res[:3,:3].T)+res[:3,3]
            W3=np.dot(WI3,refaff[:3,:3].T)+refaff[:3,3]
            
            Tw.append(W)
            Tw2.append(W2)
            Tw3.append(W3)
    

    from dipy.viz import fos
    r=fos.ren()
    fos.add(r,fos.line(Tw,fos.red))
    fos.add(r,fos.line(Tw2,fos.green))    
    fos.add(r,fos.line(Tw3,fos.yellow))
    fos.add(r,fos.sphere((0,0,0),10,color=fos.blue))
    fos.add(r,fos.point(roiI,fos.blue))
    fos.show(r)