Пример #1
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()
Пример #2
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
Пример #3
0
Файл: fsl.py Проект: MPDean/dipy
def warp_displacements(ffa, flaff, fdis, fref, ffaw, order=1):
    ''' Warp an image using fsl displacements

    Parameters
    ------------
    ffa : filename of nifti to be warped
    flaff : filename of .mat  (flirt)
    fdis :  filename of displacements (fnirtfileutils)
    fref : filename of reference volume e.g. (FMRIB58_FA_1mm.nii.gz)
    ffaw : filename for the output warped image
    '''
    refaff = nib.load(fref).get_affine()
    disdata = nib.load(fdis).get_data()
    imgfa = nib.load(ffa)
    fadata = imgfa.get_data()
    fazooms = imgfa.get_header().get_zooms()
    # from fa index to ref index
    res = flirt2aff_files(flaff, ffa, fref)
    # from ref index to fa index
    ires = np.linalg.inv(res)
    # create the 4d volume which has the indices for the reference image
    reftmp = np.zeros(disdata.shape)
    '''
    #create the grid indices for the reference
    #refinds = np.ndindex(disdata.shape[:3])
    for ijk_t in refinds:
        i,j,k = ijk_t
        reftmp[i,j,k,0]=i
        reftmp[i,j,k,1]=j
        reftmp[i,j,k,2]=k
    '''
    # same as commented above but much faster
    reftmp[..., 0] = np.arange(disdata.shape[0])[:, newaxis, newaxis]
    reftmp[..., 1] = np.arange(disdata.shape[1])[newaxis, :, newaxis]
    reftmp[..., 2] = np.arange(disdata.shape[2])[newaxis, newaxis, :]

    # affine transform from reference index to the fa index
    A = np.dot(reftmp, ires[:3, :3].T) + ires[:3, 3]
    # add the displacements but first devide them by the voxel sizes
    A2 = A + disdata / fazooms
    # hold the displacements' shape reshaping
    di, dj, dk, dl = disdata.shape
    # do the interpolation using map coordinates
    # the list of points where the interpolation is done given by the reshaped
    # in 2D A2 (list of 3d points in fa index)
    W = mc(fadata, A2.reshape(di * dj * dk, dl).T, order=order).reshape(di, dj,
                                                                        dk)
    # save the warped image
    Wimg = nib.Nifti1Image(W, refaff)
    nib.save(Wimg, ffaw)
Пример #4
0
def warp_displacements(ffa, flaff, fdis, fref, ffaw, order=1):
    ''' Warp an image using fsl displacements

    Parameters
    ------------
    ffa : filename of nifti to be warped
    flaff : filename of .mat  (flirt)
    fdis :  filename of displacements (fnirtfileutils)
    fref : filename of reference volume e.g. (FMRIB58_FA_1mm.nii.gz)
    ffaw : filename for the output warped image
    '''
    refaff = nib.load(fref).affine
    disdata = nib.load(fdis).get_data()
    imgfa = nib.load(ffa)
    fadata = imgfa.get_data()
    fazooms = imgfa.header.get_zooms()
    # from fa index to ref index
    res = flirt2aff_files(flaff, ffa, fref)
    # from ref index to fa index
    ires = np.linalg.inv(res)
    # create the 4d volume which has the indices for the reference image
    reftmp = np.zeros(disdata.shape)
    '''
    #create the grid indices for the reference
    #refinds = np.ndindex(disdata.shape[:3])
    for ijk_t in refinds:
        i,j,k = ijk_t
        reftmp[i,j,k,0]=i
        reftmp[i,j,k,1]=j
        reftmp[i,j,k,2]=k
    '''
    # same as commented above but much faster
    reftmp[..., 0] = np.arange(disdata.shape[0])[:, newaxis, newaxis]
    reftmp[..., 1] = np.arange(disdata.shape[1])[newaxis, :, newaxis]
    reftmp[..., 2] = np.arange(disdata.shape[2])[newaxis, newaxis, :]

    # affine transform from reference index to the fa index
    A = np.dot(reftmp, ires[:3, :3].T) + ires[:3, 3]
    # add the displacements but first devide them by the voxel sizes
    A2 = A + disdata / fazooms
    # hold the displacements' shape reshaping
    di, dj, dk, dl = disdata.shape
    # do the interpolation using map coordinates
    # the list of points where the interpolation is done given by the reshaped
    # in 2D A2 (list of 3d points in fa index)
    W = mc(fadata, A2.reshape(di * dj * dk, dl).T, order=order).reshape(di, dj,
                                                                        dk)
    # save the warped image
    Wimg = nib.Nifti1Image(W, refaff)
    nib.save(Wimg, ffaw)
Пример #5
0
Файл: fsl.py Проект: MPDean/dipy
def warp_displacements_tracks(fdpy, ffa, fmat, finv, fdis, fdisa, fref, fdpyw):
    """ Warp tracks from native space to the FMRIB58/MNI space

    We use here the fsl displacements. Have a look at create_displacements to
    see an example of how to use these displacements.

    Parameters
    ------------
    fdpy : filename of the .dpy file with the tractography
    ffa : filename of nifti to be warped
    fmat : filename of .mat  (flirt)
    fdis :  filename of displacements (fnirtfileutils)
    fdisa :  filename of displacements (fnirtfileutils + affine)
    finv : filename of invwarp displacements (invwarp)
    fref : filename of reference volume e.g. (FMRIB58_FA_1mm.nii.gz)
    fdpyw : filename of the warped tractography


    See also
    -----------
    dipy.external.fsl.create_displacements

    """

    # read the tracks from the image space
    dpr = Dpy(fdpy, 'r')
    T = dpr.read_tracks()
    dpr.close()

    # copy them in a new file
    dpw = Dpy(fdpyw, 'w', compression=1)
    dpw.write_tracks(T)
    dpw.close()

    # from fa index to ref index
    res = flirt2aff_files(fmat, ffa, fref)

    # load the reference img
    imgref = nib.load(fref)
    refaff = imgref.get_affine()

    # load the invwarp displacements
    imginvw = nib.load(finv)
    invwdata = imginvw.get_data()
    invwaff = imginvw.get_affine()

    # load the forward displacements
    imgdis = nib.load(fdis)
    disdata = imgdis.get_data()

    # load the forward displacements + affine
    imgdis2 = nib.load(fdisa)
    disdata2 = imgdis2.get_data()

    # from their difference create the affine
    disaff = disdata2 - disdata

    del disdata
    del disdata2

    shape = nib.load(ffa).get_data().shape

    # transform the displacements affine back to image space
    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)

    # remove the transformed affine from the invwarp displacements
    di = invwdata[:, :, :, 0] + disaff0
    dj = invwdata[:, :, :, 1] + disaff1
    dk = invwdata[:, :, :, 2] + disaff2

    dprw = Dpy(fdpyw, 'r+')
    rows = len(dprw.f.root.streamlines.tracks)
    blocks = np.round(np.linspace(0, rows, 10)).astype(int)  # lets work in
    #                                                                   blocks
    # print rows
    for i in range(len(blocks) - 1):
        # print blocks[i],blocks[i+1]
        # copy a lot of tracks together
        caboodle = dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]]
        mci = mc(di, caboodle.T, order=1)  # interpolations for i displacement
        mcj = mc(dj, caboodle.T, order=1)  # interpolations for j displacement
        mck = mc(dk, caboodle.T, order=1)  # interpolations for k displacement
        D = np.vstack((mci, mcj, mck)).T
        # go back to mni image space
        WI2 = np.dot(caboodle, res[:3, :3].T) + res[:3, 3] + D
        # and then to mni world space
        caboodlew = np.dot(WI2, refaff[:3, :3].T) + refaff[:3, 3]
        # write back
        dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]] = (
            caboodlew.astype('f4'))
    dprw.close()
Пример #6
0
def warp_displacements_tracks(fdpy, ffa, fmat, finv, fdis, fdisa, fref, fdpyw):
    """ Warp tracks from native space to the FMRIB58/MNI space

    We use here the fsl displacements. Have a look at create_displacements to
    see an example of how to use these displacements.

    Parameters
    ------------
    fdpy : filename of the .dpy file with the tractography
    ffa : filename of nifti to be warped
    fmat : filename of .mat  (flirt)
    fdis :  filename of displacements (fnirtfileutils)
    fdisa :  filename of displacements (fnirtfileutils + affine)
    finv : filename of invwarp displacements (invwarp)
    fref : filename of reference volume e.g. (FMRIB58_FA_1mm.nii.gz)
    fdpyw : filename of the warped tractography


    See also
    -----------
    dipy.external.fsl.create_displacements

    """

    # read the tracks from the image space
    dpr = Dpy(fdpy, 'r')
    T = dpr.read_tracks()
    dpr.close()

    # copy them in a new file
    dpw = Dpy(fdpyw, 'w', compression=1)
    dpw.write_tracks(T)
    dpw.close()

    # from fa index to ref index
    res = flirt2aff_files(fmat, ffa, fref)

    # load the reference img
    imgref = nib.load(fref)
    refaff = imgref.affine

    # load the invwarp displacements
    imginvw = nib.load(finv)
    invwdata = imginvw.get_data()

    # load the forward displacements
    imgdis = nib.load(fdis)
    disdata = imgdis.get_data()

    # load the forward displacements + affine
    imgdis2 = nib.load(fdisa)
    disdata2 = imgdis2.get_data()

    # from their difference create the affine
    disaff = disdata2 - disdata

    del disdata
    del disdata2

    shape = nib.load(ffa).get_data().shape

    # transform the displacements affine back to image space
    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)

    # remove the transformed affine from the invwarp displacements
    di = invwdata[:, :, :, 0] + disaff0
    dj = invwdata[:, :, :, 1] + disaff1
    dk = invwdata[:, :, :, 2] + disaff2

    dprw = Dpy(fdpyw, 'r+')
    rows = len(dprw.f.root.streamlines.tracks)
    blocks = np.round(np.linspace(0, rows, 10)).astype(int)  # lets work in
    #                                                                   blocks
    # print rows
    for i in range(len(blocks) - 1):
        # print blocks[i],blocks[i+1]
        # copy a lot of tracks together
        caboodle = dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]]
        mci = mc(di, caboodle.T, order=1)  # interpolations for i displacement
        mcj = mc(dj, caboodle.T, order=1)  # interpolations for j displacement
        mck = mc(dk, caboodle.T, order=1)  # interpolations for k displacement
        D = np.vstack((mci, mcj, mck)).T
        # go back to mni image space
        WI2 = np.dot(caboodle, res[:3, :3].T) + res[:3, 3] + D
        # and then to mni world space
        caboodlew = np.dot(WI2, refaff[:3, :3].T) + refaff[:3, 3]
        # write back
        dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]] = (
            caboodlew.astype('f4'))
    dprw.close()
Пример #7
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)
Пример #8
0
 def DespoticCO32(Tvals,n):
     intensity = mc(co32, [[fx(Tvals)],[fy(n)]])
     return(intensity)
Пример #9
0
 def DespoticCO21(Tvals,n):
     intensity = mc(co21, [[fx(Tvals)],[fy(n)]])
     return(intensity)
Пример #10
0
 def DespoticCO10(Tvals,n):
     intensity = mc(co10, [[fx(Tvals)],[fy(n)]])
     return(intensity)