예제 #1
0
파일: registration.py 프로젝트: conge/cmp
def nlin_regT12b0():
""" 
**Nonlinear Registration**
--------------------------

If "nonlinear registration" is selected as registration mode, then FSL nonlinear registration tool FNIRT is used to perform this step. Default parameters (which can be modified) generally give good results in most cases. Also, this operation expects a T2 fMRI image with the same resolution as functional images.
A two steps procedure is performed:

First, a linear registration if performed between the T1 and the T2; these images don’t share the same contrast but there are no distortions between them, so a linear registration will suffice.
Then, a nonlinear registration will try to align the T2 image and the b0 one, and then the two transformations are concatenated. To preserve edges, a intermediate skull-stripping step is performed on both images by using BET.We use the nonlinear registration approach in order to mitigate the nonlinear distortions which are present in diffusion images. Future versions of the pipeline will account for other methods to achieve this (e.g. fieldmaps).

INPUTS:
^^^^^^^
    
* Diffusion_b0_resampled.nii.gz: b0 image from diffution data, resampled to 2x2x2 voxel size
* T1.nii.gz: T1 weighted anatomical image
* T2.nii.gz: T2* weighted anatomical image for calibration.
    
OUTPUTS:
^^^^^^^^

* FSL style registation matrix: (T1-TO-B0.mat)
* Registered b0 image (b0-TO-orig.nii.gz)
        
Parameters:
^^^^^^^^^^^
* initial affine: where to get the initial affine matrix to start registration with (defaut: --init-header:--dti; alternative option: --init-fsl)
* data type: the data type of the input data (default: dsi; alternative options: --dti, --qbal)
        
Example:
^^^^^^^^

    .. code-block:: bash
        Step 1, linear transformation from T1 to b0:
        >>> flirt -in "T1.nii.gz" -ref "T2.nii.gz" -nosearch -dof 6 -cost mutualinfo -out "T1-TO-T2.nii.gz" -omat "T1-TO-T2.mat"
    	>>> flirt -in "T2.nii.gz" -ref "Diffusion_b0_resampled.nii.gz" -nosearch -dof 12 -cost normmi -out "T2-To-B0.nii.gz" -omat "T2-To-B0.mat"
    	>>> convert_xfm -concat "T2-TO-b0.mat" "T1-TO-T2.mat" -omat T1-TO-b0.mat
    	>>> flirt -in "T1.nii.gz" -ref "Diffusion_b0_resampled.nii.gz" -applyxfm -init T1-TO-b0.mat -out T1-TO-b0.nii.gz -interp sinc
    	Step 2, non-linear transformation from T1 to b0
    	>>> bet T2.nii.gz T2-brain -m -n -R -f 0.35 -g 0.15
    	>>> bet Diffusion_b0_resampled.nii.gz b0-brain -m -n -R -f 0.2 -g 0.2
    	>>> fnirt -v --in=T2.nii.gz --aff=T2-TO-b0.mat --ref=Diffusion_b0_resampled.nii.g --iout=T2-TO-b0_warped.nii.gz --cout=2-TO-b0_warp.nii.gz --fout=T2-TO-b0_warp-field.nii.gz --inmask=T2-brain-mask.nii.gz --refmask=b0-brain-mask.nii.gz --subsamp=8,4,2,2 --miter==5,5,5,5 --lambda=240,120,90,30 --splineorder=3 --applyinmask=0,0,1,1 --applyrefmask=0,0,1,1
    
References:
^^^^^^^^^^^

`Grevea & Fischl, 2009 <http://www.sciencedirect.com/science/article/pii/S1053811909006752>`_


"""
    log.info("T1 -> b0: Non-linear registration")
    log.info("=================================")
    
    nifti_dir = gconf.get_nifti()
    
    #===========================================================================
    log.info('[SUB-STEP 1] LINEAR register "T2" onto "b0_resampled"')
    #===========================================================================
    
    ##############
    log.info('[1.1] linear register "T1" onto "T2"')
    
    fli_cmp = 'flirt -in "%s" -ref "%s" -nosearch -dof 6 -cost mutualinfo -out "%s" -omat "%s"' % (
            op.join(nifti_dir, "T1.nii.gz"),
            op.join(nifti_dir, "T2.nii.gz"),
            op.join(nifti_dir, "T1-TO-T2.nii.gz"),
            op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat"),
            )
    runCmd( fli_cmp, log )
    
    if not op.exists(op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat")):
        log.error("T1-TO-T2.mat Problem with FLIRT. Unable to find linear transformation 'T1-TO-T2.mat'.")        
        
    ##############
    log.info('[1.2] -> linear register "T2" onto "b0_resampled"')
    
    fli_cmp = 'flirt -in "%s" -ref "%s" -nosearch -dof 12 -cost normmi -out "%s" -omat "%s"' % (
            op.join(nifti_dir, "T2.nii.gz"),
            op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
            op.join(nifti_dir, "T2-TO-b0.nii.gz"),
            op.join(gconf.get_nifti_trafo(), "T2-TO-b0.mat"),
            )
    runCmd( fli_cmp, log )
    
    if not op.exists(op.join(nifti_dir, "T2-TO-b0.nii.gz")):
        log.error("T2-TO-b0.nii.gz" "Problem with FLIRT. Unable to find linear transformation 'T2-TO-b0.mat'.")
       
    ##############
    log.info('[1.3] -> apply the linear registration "T1" --> "b0" (for comparison)')
    
    con_cmp = 'convert_xfm -concat "%s" "%s" -omat "%s"' % (op.join(gconf.get_nifti_trafo(), "T2-TO-b0.mat"),
                                                            op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat"),
                                                            op.join(gconf.get_nifti_trafo(), "T1-TO-b0.mat"))
    runCmd( con_cmp, log )
    
    fli_cmp = 'flirt -in "%s" -ref "%s" -applyxfm -init "%s" -out "%s" -interp sinc' % (
            op.join(nifti_dir, "T1.nii.gz"),
            op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
            op.join(gconf.get_nifti_trafo(), "T1-TO-b0.mat"),
            op.join(nifti_dir, "T1-TO-b0.nii.gz"),
            )
    runCmd( fli_cmp, log )
    
    if not op.exists(op.join(nifti_dir, "T1-TO-b0.nii.gz")):
        log.error("T1-TO-b0.nii.gz Problem with FLIRT. Unable to find linear transformation 'T1-TO-b0.mat'.")
    
    
       
    #===========================================================================
    log.info("[SUB-STEP 2] Create BINARY MASKS for nonlinear registration")
    #===========================================================================

    log.info("[2.1] -> create a T2 brain mask")

    if not gconf.nlin_reg_bet_T2_param == '':
        param = gconf.nlin_reg_bet_T2_param
    else:
        param = '-f 0.35 -g 0.15'
        
    #rm -f "T2-brain-mask.nii.gz" > /dev/null
    infile = op.join(nifti_dir, "T2.nii.gz")
    outfile = op.join(nifti_dir, "T2-brain")
    bet_cmd = 'bet "%s" "%s" -m -n -R %s' % (infile, outfile, param)
    runCmd( bet_cmd, log ) 
    
    if not op.exists(op.join(nifti_dir, "T2-brain_mask.nii.gz")):
        log.error("T2-brain_mask.nii.gz Problem with BET. Unable to extract the skull.")
        
        
    src = op.join(nifti_dir, "T2-brain_mask.nii.gz")
    dst = op.join(nifti_dir, "T2-brain-mask.nii.gz")
    mymove(src,dst,log)        

    log.info("[2.2] -> create a DSI_b0 brain mask")
    
    if not gconf.nlin_reg_bet_b0_param == '':
        param = gconf.nlin_reg_bet_b0_param
    else:
        param = '-f 0.2 -g 0.2'
        
    #    rm -f "b0-brain-mask.nii.gz"
    
    infile = op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz")
    outfile = op.join(nifti_dir, "b0-brain")
    bet_cmd = 'bet "%s" "%s" -m -n -R %s' % (infile, outfile, param)
    runCmd( bet_cmd, log ) 
    
    if not op.exists(op.join(nifti_dir, "b0-brain_mask.nii.gz")):
        log.error("b0-brain_mask.nii.gz Problem with BET. Unable to extract the skull.")
        
    
    src = op.join(nifti_dir, "b0-brain_mask.nii.gz")
    dst = op.join(nifti_dir, "b0-brain-mask.nii.gz")
    mymove(src,dst,log)

    log.info("BET has finished. Check the result with FSLVIEW.")

    #===========================================================================
    log.info('[SUB-STEP 3] NONLINEAR register "T2" onto "b0_resampled"')
    #===========================================================================
    
    log.info('Start NONLINEAR registration with brain-masks')
    
    log.info("'Started FNIRT to find 'T2 --> b0' nonlinear transformation at ")

#    rm -f "T2-TO-b0_warp"*.*
#    rm -f "T2_to_${TP}/T2_to_DSI_b0_resampled.log"

    
    if not gconf.nlin_reg_fnirt_param == '':
        param = gconf.nlin_reg_fnirt_param
    else:
        param = '--subsamp=8,4,2,2 --miter==5,5,5,5 --lambda=240,120,90,30 --splineorder=3 --applyinmask=0,0,1,1 --applyrefmask=0,0,1,1'
    
    tup = (op.join(nifti_dir, "T2.nii.gz"),
         op.join(gconf.get_nifti_trafo(), "T2-TO-b0.mat"),
         op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
         op.join(nifti_dir, "T2-TO-b0_warped.nii.gz"),
         op.join(nifti_dir, "T2-TO-b0_warp.nii.gz"),
         op.join(nifti_dir, "T2-TO-b0_warp-field.nii.gz"),
         op.join(nifti_dir, "T2-brain-mask.nii.gz"),
         op.join(nifti_dir, "b0-brain-mask.nii.gz"),
         param)

    fn_cmd = 'fnirt -v --in="%s" --aff="%s" --ref="%s" --iout="%s" --cout="%s" --fout="%s" --inmask="%s" --refmask="%s" %s' % tup
    runCmd( fn_cmd, log )
        
    if not op.exists(op.join(nifti_dir, "T2-TO-b0_warped.nii.gz")):
        log.error("Problem with FNIRT. Unable to find nonlinear transformation 'T2-TO-b0_warp.nii.gz'.")
        
    
    log.info('[3.2] -> apply the warp found for "T2" also onto "T1"')

    # rm -f "T1-TO-b0_warped".*
    tup = (op.join(nifti_dir, "T1.nii.gz"),
           op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat"),
           op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
           op.join(nifti_dir, "T2-TO-b0_warp.nii.gz"),
           op.join(nifti_dir, "T1-TO-b0_warped.nii.gz"))
    
    app_cmd = 'applywarp --in="%s" --premat="%s" --ref="%s" --warp="%s" --out="%s"' % tup
    runCmd( app_cmd, log )
        
    if not op.exists(op.join(nifti_dir, "T1-TO-b0_warped.nii.gz")):
        log.error("T1-TO-b0_warped.nii.gz" "Problems with APPLYWARP. Unable to apply nonlinear transformation 'T2-TO-b0_warp.nii.gz'.")
            
    # check the results
    log.info('APPLYWARP finished. Check the result with FSLVIEW.')
예제 #2
0
def nlin_regT12b0():
    log.info("T1 -> b0: Non-linear registration")
    log.info("=================================")
    
    nifti_dir = gconf.get_nifti()
    
    #===========================================================================
    log.info('[SUB-STEP 1] LINEAR register "T2" onto "b0_resampled"')
    #===========================================================================
    
    ##############
    log.info('[1.1] linear register "T1" onto "T2"')
    
    fli_cmp = 'flirt -in "%s" -ref "%s" -nosearch -dof 6 -cost mutualinfo -out "%s" -omat "%s"' % (
            op.join(nifti_dir, "T1.nii.gz"),
            op.join(nifti_dir, "T2.nii.gz"),
            op.join(nifti_dir, "T1-TO-T2.nii.gz"),
            op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat"),
            )
    runCmd( fli_cmp, log )
    
    if not op.exists(op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat")):
        log.error("T1-TO-T2.mat Problem with FLIRT. Unable to find linear transformation 'T1-TO-T2.mat'.")        
        
    ##############
    log.info('[1.2] -> linear register "T2" onto "b0_resampled"')
    
    fli_cmp = 'flirt -in "%s" -ref "%s" -nosearch -dof 12 -cost normmi -out "%s" -omat "%s"' % (
            op.join(nifti_dir, "T2.nii.gz"),
            op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
            op.join(nifti_dir, "T2-TO-b0.nii.gz"),
            op.join(gconf.get_nifti_trafo(), "T2-TO-b0.mat"),
            )
    runCmd( fli_cmp, log )
    
    if not op.exists(op.join(nifti_dir, "T2-TO-b0.nii.gz")):
        log.error("T2-TO-b0.nii.gz" "Problem with FLIRT. Unable to find linear transformation 'T2-TO-b0.mat'.")
       
    ##############
    log.info('[1.3] -> apply the linear registration "T1" --> "b0" (for comparison)')
    
    con_cmp = 'convert_xfm -concat "%s" "%s" -omat "%s"' % (op.join(gconf.get_nifti_trafo(), "T2-TO-b0.mat"),
                                                            op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat"),
                                                            op.join(gconf.get_nifti_trafo(), "T1-TO-b0.mat"))
    runCmd( con_cmp, log )
    
    fli_cmp = 'flirt -in "%s" -ref "%s" -applyxfm -init "%s" -out "%s" -interp sinc' % (
            op.join(nifti_dir, "T1.nii.gz"),
            op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
            op.join(gconf.get_nifti_trafo(), "T1-TO-b0.mat"),
            op.join(nifti_dir, "T1-TO-b0.nii.gz"),
            )
    runCmd( fli_cmp, log )
    
    if not op.exists(op.join(nifti_dir, "T1-TO-b0.nii.gz")):
        log.error("T1-TO-b0.nii.gz Problem with FLIRT. Unable to find linear transformation 'T1-TO-b0.mat'.")
    
    
       
    #===========================================================================
    log.info("[SUB-STEP 2] Create BINARY MASKS for nonlinear registration")
    #===========================================================================

    log.info("[2.1] -> create a T2 brain mask")

    if not gconf.nlin_reg_bet_T2_param == '':
        param = gconf.nlin_reg_bet_T2_param
    else:
        param = '-f 0.35 -g 0.15'
        
    #rm -f "T2-brain-mask.nii.gz" > /dev/null
    infile = op.join(nifti_dir, "T2.nii.gz")
    outfile = op.join(nifti_dir, "T2-brain")
    bet_cmd = 'bet "%s" "%s" -m -n -R %s' % (infile, outfile, param)
    runCmd( bet_cmd, log ) 
    
    if not op.exists(op.join(nifti_dir, "T2-brain_mask.nii.gz")):
        log.error("T2-brain_mask.nii.gz Problem with BET. Unable to extract the skull.")
        
        
    src = op.join(nifti_dir, "T2-brain_mask.nii.gz")
    dst = op.join(nifti_dir, "T2-brain-mask.nii.gz")
    mymove(src,dst,log)        

    log.info("[2.2] -> create a DSI_b0 brain mask")
    
    if not gconf.nlin_reg_bet_b0_param == '':
        param = gconf.nlin_reg_bet_b0_param
    else:
        param = '-f 0.2 -g 0.2'
        
    #    rm -f "b0-brain-mask.nii.gz"
    
    infile = op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz")
    outfile = op.join(nifti_dir, "b0-brain")
    bet_cmd = 'bet "%s" "%s" -m -n -R %s' % (infile, outfile, param)
    runCmd( bet_cmd, log ) 
    
    if not op.exists(op.join(nifti_dir, "b0-brain_mask.nii.gz")):
        log.error("b0-brain_mask.nii.gz Problem with BET. Unable to extract the skull.")
        
    
    src = op.join(nifti_dir, "b0-brain_mask.nii.gz")
    dst = op.join(nifti_dir, "b0-brain-mask.nii.gz")
    mymove(src,dst,log)

    log.info("BET has finished. Check the result with FSLVIEW.")

    #===========================================================================
    log.info('[SUB-STEP 3] NONLINEAR register "T2" onto "b0_resampled"')
    #===========================================================================
    
    log.info('Start NONLINEAR registration with brain-masks')
    
    log.info("'Started FNIRT to find 'T2 --> b0' nonlinear transformation at ")

#    rm -f "T2-TO-b0_warp"*.*
#    rm -f "T2_to_${TP}/T2_to_DSI_b0_resampled.log"

    
    if not gconf.nlin_reg_fnirt_param == '':
        param = gconf.nlin_reg_fnirt_param
    else:
        param = '--subsamp=8,4,2,2 --miter==5,5,5,5 --lambda=240,120,90,30 --splineorder=3 --applyinmask=0,0,1,1 --applyrefmask=0,0,1,1'
    
    tup = (op.join(nifti_dir, "T2.nii.gz"),
         op.join(gconf.get_nifti_trafo(), "T2-TO-b0.mat"),
         op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
         op.join(nifti_dir, "T2-TO-b0_warped.nii.gz"),
         op.join(nifti_dir, "T2-TO-b0_warp.nii.gz"),
         op.join(nifti_dir, "T2-TO-b0_warp-field.nii.gz"),
         op.join(nifti_dir, "T2-brain-mask.nii.gz"),
         op.join(nifti_dir, "b0-brain-mask.nii.gz"),
         param)

    fn_cmd = 'fnirt -v --in="%s" --aff="%s" --ref="%s" --iout="%s" --cout="%s" --fout="%s" --inmask="%s" --refmask="%s" %s' % tup
    runCmd( fn_cmd, log )
        
    if not op.exists(op.join(nifti_dir, "T2-TO-b0_warped.nii.gz")):
        log.error("Problem with FNIRT. Unable to find nonlinear transformation 'T2-TO-b0_warp.nii.gz'.")
        
    
    log.info('[3.2] -> apply the warp found for "T2" also onto "T1"')

    # rm -f "T1-TO-b0_warped".*
    tup = (op.join(nifti_dir, "T1.nii.gz"),
           op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat"),
           op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
           op.join(nifti_dir, "T2-TO-b0_warp.nii.gz"),
           op.join(nifti_dir, "T1-TO-b0_warped.nii.gz"))
    
    app_cmd = 'applywarp --in="%s" --premat="%s" --ref="%s" --warp="%s" --out="%s"' % tup
    runCmd( app_cmd, log )
        
    if not op.exists(op.join(nifti_dir, "T1-TO-b0_warped.nii.gz")):
        log.error("T1-TO-b0_warped.nii.gz" "Problems with APPLYWARP. Unable to apply nonlinear transformation 'T2-TO-b0_warp.nii.gz'.")
            
    # check the results
    log.info('APPLYWARP finished. Check the result with FSLVIEW.')
예제 #3
0
def nlin_regT12b0():
    log.info("T1 -> b0: Non-linear registration")
    log.info("=================================")

    nifti_dir = gconf.get_nifti()

    #===========================================================================
    log.info('[SUB-STEP 1] LINEAR register "T2" onto "b0_resampled"')
    #===========================================================================

    ##############
    log.info('[1.1] linear register "T1" onto "T2"')

    fli_cmp = 'flirt -in "%s" -ref "%s" -nosearch -dof 6 -cost mutualinfo -out "%s" -omat "%s"' % (
        op.join(nifti_dir, "T1.nii.gz"),
        op.join(nifti_dir, "T2.nii.gz"),
        op.join(nifti_dir, "T1-TO-T2.nii.gz"),
        op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat"),
    )
    runCmd(fli_cmp, log)

    if not op.exists(op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat")):
        log.error(
            "T1-TO-T2.mat Problem with FLIRT. Unable to find linear transformation 'T1-TO-T2.mat'."
        )

    ##############
    log.info('[1.2] -> linear register "T2" onto "b0_resampled"')

    fli_cmp = 'flirt -in "%s" -ref "%s" -nosearch -dof 12 -cost normmi -out "%s" -omat "%s"' % (
        op.join(nifti_dir, "T2.nii.gz"),
        op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
        op.join(nifti_dir, "T2-TO-b0.nii.gz"),
        op.join(gconf.get_nifti_trafo(), "T2-TO-b0.mat"),
    )
    runCmd(fli_cmp, log)

    if not op.exists(op.join(nifti_dir, "T2-TO-b0.nii.gz")):
        log.error(
            "T2-TO-b0.nii.gz"
            "Problem with FLIRT. Unable to find linear transformation 'T2-TO-b0.mat'."
        )

    ##############
    log.info(
        '[1.3] -> apply the linear registration "T1" --> "b0" (for comparison)'
    )

    con_cmp = 'convert_xfm -concat "%s" "%s" -omat "%s"' % (
        op.join(gconf.get_nifti_trafo(), "T2-TO-b0.mat"),
        op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat"),
        op.join(gconf.get_nifti_trafo(), "T1-TO-b0.mat"))
    runCmd(con_cmp, log)

    fli_cmp = 'flirt -in "%s" -ref "%s" -applyxfm -init "%s" -out "%s" -interp sinc' % (
        op.join(nifti_dir, "T1.nii.gz"),
        op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
        op.join(gconf.get_nifti_trafo(), "T1-TO-b0.mat"),
        op.join(nifti_dir, "T1-TO-b0.nii.gz"),
    )
    runCmd(fli_cmp, log)

    if not op.exists(op.join(nifti_dir, "T1-TO-b0.nii.gz")):
        log.error(
            "T1-TO-b0.nii.gz Problem with FLIRT. Unable to find linear transformation 'T1-TO-b0.mat'."
        )

    #===========================================================================
    log.info("[SUB-STEP 2] Create BINARY MASKS for nonlinear registration")
    #===========================================================================

    log.info("[2.1] -> create a T2 brain mask")

    if not gconf.nlin_reg_bet_T2_param == '':
        param = gconf.nlin_reg_bet_T2_param
    else:
        param = '-f 0.35 -g 0.15'

    #rm -f "T2-brain-mask.nii.gz" > /dev/null
    infile = op.join(nifti_dir, "T2.nii.gz")
    outfile = op.join(nifti_dir, "T2-brain")
    bet_cmd = 'bet "%s" "%s" -m -n -R %s' % (infile, outfile, param)
    runCmd(bet_cmd, log)

    if not op.exists(op.join(nifti_dir, "T2-brain_mask.nii.gz")):
        log.error(
            "T2-brain_mask.nii.gz Problem with BET. Unable to extract the skull."
        )

    src = op.join(nifti_dir, "T2-brain_mask.nii.gz")
    dst = op.join(nifti_dir, "T2-brain-mask.nii.gz")
    mymove(src, dst, log)

    log.info("[2.2] -> create a DSI_b0 brain mask")

    if not gconf.nlin_reg_bet_b0_param == '':
        param = gconf.nlin_reg_bet_b0_param
    else:
        param = '-f 0.2 -g 0.2'

    #    rm -f "b0-brain-mask.nii.gz"

    infile = op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz")
    outfile = op.join(nifti_dir, "b0-brain")
    bet_cmd = 'bet "%s" "%s" -m -n -R %s' % (infile, outfile, param)
    runCmd(bet_cmd, log)

    if not op.exists(op.join(nifti_dir, "b0-brain_mask.nii.gz")):
        log.error(
            "b0-brain_mask.nii.gz Problem with BET. Unable to extract the skull."
        )

    src = op.join(nifti_dir, "b0-brain_mask.nii.gz")
    dst = op.join(nifti_dir, "b0-brain-mask.nii.gz")
    mymove(src, dst, log)

    log.info("BET has finished. Check the result with FSLVIEW.")

    #===========================================================================
    log.info('[SUB-STEP 3] NONLINEAR register "T2" onto "b0_resampled"')
    #===========================================================================

    log.info('Start NONLINEAR registration with brain-masks')

    log.info("'Started FNIRT to find 'T2 --> b0' nonlinear transformation at ")

    #    rm -f "T2-TO-b0_warp"*.*
    #    rm -f "T2_to_${TP}/T2_to_DSI_b0_resampled.log"

    if not gconf.nlin_reg_fnirt_param == '':
        param = gconf.nlin_reg_fnirt_param
    else:
        param = '--subsamp=8,4,2,2 --miter==5,5,5,5 --lambda=240,120,90,30 --splineorder=3 --applyinmask=0,0,1,1 --applyrefmask=0,0,1,1'

    tup = (op.join(nifti_dir, "T2.nii.gz"),
           op.join(gconf.get_nifti_trafo(), "T2-TO-b0.mat"),
           op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
           op.join(nifti_dir, "T2-TO-b0_warped.nii.gz"),
           op.join(nifti_dir, "T2-TO-b0_warp.nii.gz"),
           op.join(nifti_dir, "T2-TO-b0_warp-field.nii.gz"),
           op.join(nifti_dir, "T2-brain-mask.nii.gz"),
           op.join(nifti_dir, "b0-brain-mask.nii.gz"), param)

    fn_cmd = 'fnirt -v --in="%s" --aff="%s" --ref="%s" --iout="%s" --cout="%s" --fout="%s" --inmask="%s" --refmask="%s" %s' % tup
    runCmd(fn_cmd, log)

    if not op.exists(op.join(nifti_dir, "T2-TO-b0_warped.nii.gz")):
        log.error(
            "Problem with FNIRT. Unable to find nonlinear transformation 'T2-TO-b0_warp.nii.gz'."
        )

    log.info('[3.2] -> apply the warp found for "T2" also onto "T1"')

    # rm -f "T1-TO-b0_warped".*
    tup = (op.join(nifti_dir, "T1.nii.gz"),
           op.join(gconf.get_nifti_trafo(), "T1-TO-T2.mat"),
           op.join(nifti_dir, "Diffusion_b0_resampled.nii.gz"),
           op.join(nifti_dir, "T2-TO-b0_warp.nii.gz"),
           op.join(nifti_dir, "T1-TO-b0_warped.nii.gz"))

    app_cmd = 'applywarp --in="%s" --premat="%s" --ref="%s" --warp="%s" --out="%s"' % tup
    runCmd(app_cmd, log)

    if not op.exists(op.join(nifti_dir, "T1-TO-b0_warped.nii.gz")):
        log.error(
            "T1-TO-b0_warped.nii.gz"
            "Problems with APPLYWARP. Unable to apply nonlinear transformation 'T2-TO-b0_warp.nii.gz'."
        )

    # check the results
    log.info('APPLYWARP finished. Check the result with FSLVIEW.')