def wm_registration(param, smooth=False, init=False):
    path, fixed_name, ext = sct.extract_fname(param.fname_fixed)
    path_moving, moving_name, ext = sct.extract_fname(param.fname_moving)
    path, fixed_seg_name, ext = sct.extract_fname(param.fname_seg_fixed)
    path_moving_seg, moving_seg_name, ext = sct.extract_fname(param.fname_seg_moving)

    # cropping in x & y directions
    fixed_name_temp = fixed_name + "_crop"
    cmd = "sct_crop_image -i " + fixed_name + ext + " -o " + fixed_name_temp + ext + " -m " + fixed_seg_name + ext + " -shift 5,5 -dim 0,1"
    sct.run(cmd)
    fixed_name = fixed_name_temp

    fixed_seg_name_temp = fixed_seg_name+"_crop"
    sct.run("sct_crop_image -i " + fixed_seg_name + ext + " -o " + fixed_seg_name_temp + ext + " -m " + fixed_seg_name + ext + " -shift 5,5 -dim 0,1")
    fixed_seg_name = fixed_seg_name_temp

    # padding the images
    moving_name_pad = moving_name+"_pad"
    fixed_name_pad = fixed_name+"_pad"
    sct.run('sct_maths -i '+path_moving+moving_name+ext+' -o '+moving_name_pad+ext+' -pad 0,0,'+str(param.padding))
    sct.run('sct_maths -i '+fixed_name+ext+' -o '+fixed_name_pad+ext+' -pad 0,0,'+str(param.padding))
    moving_name = moving_name_pad
    fixed_name = fixed_name_pad

    moving_seg_name_pad = moving_seg_name+"_pad"
    sct.run('sct_maths -i '+path_moving_seg+moving_seg_name+ext+' -o '+moving_seg_name_pad+ext+' -pad 0,0,'+str(param.padding))
    moving_seg_name = moving_seg_name_pad

    fixed_seg_name_pad = fixed_seg_name+"_pad"
    sct.run('sct_maths -i '+fixed_seg_name+ext+' -o '+fixed_seg_name_pad+ext+' -pad 0,0,'+str(param.padding))
    fixed_seg_name = fixed_seg_name_pad

    if param.metric == 'MeanSquares':
        # offset
        old_min = 0
        old_max = 1
        new_min = 100
        new_max = 200

        fixed_im = Image(fixed_name+ext)
        fixed_im.data = (fixed_im.data - old_min)*(new_max - new_min)/(old_max - old_min) + new_min
        fixed_im.save()

        moving_im = Image(moving_name+ext)
        moving_im.data = (moving_im.data - old_min)*(new_max - new_min)/(old_max - old_min) + new_min
        moving_im.save()

    if smooth:
        # smoothing the images to register
        sct.printv('\nSmoothing the images...', verbose, 'normal')
        fixed_smooth = fixed_name+'_smooth'
        moving_smooth = moving_name+'_smooth'
        sct.run('sct_maths -i '+fixed_name+ext+' -smooth 1 -o '+fixed_smooth+ext)
        sct.run('sct_maths -i '+moving_name+ext+' -smooth 1 -o '+moving_smooth+ext)
        fixed_name = fixed_smooth
        moving_name = moving_smooth

    # registration of the gray matter
    sct.printv('\nDeforming the image...', verbose, 'normal')
    moving_name_reg = moving_name+"_deformed"

    param_multimodal_reg = 'step=1,type=seg,algo=slicereg,metric=MeanSquares:step=2,type=im,algo=bsplinesyn,metric=MeanSquares,iter=5,shrink=2'
    if param.transformation == 'BSplineSyN':
        transfo_params = ',3,0'
    elif param.transformation == 'SyN':     # SyN gives bad results...
        transfo_params = ',1,1'
    else:
        transfo_params = ''

    cmd = 'sct_register_multimodal -i '+moving_name+ext+' -d '+fixed_name+ext+' -iseg '+moving_seg_name+ext+' -dseg '+fixed_seg_name+ext+' -p '+param_multimodal_reg+' -o '+moving_name_reg
    ext_multimodal = '.nii'
    '''
    cmd = 'isct_antsRegistration --dimensionality 3 --interpolation '+param.interpolation+' --transform '+param.transformation+'['+param.gradient_step+transfo_params+'] --metric '+param.metric+'['+fixed_name+ext+','+moving_name+ext+',1,4] --output ['+moving_name_reg+','+moving_name_reg+ext+']  --convergence '+param.iteration+' --shrink-factors 1x1 --smoothing-sigmas 0x0 '
    if init:
        # initialize the transformation using the intensity
        cmd += ' -r ['+fixed_name+ext+','+moving_name+ext+',1] '

    cmd += " --masks ["+fixed_seg_name+ext+","+moving_seg_name + ext + "]"
    # cmd += " -m ["+fixed_seg_name+".nii,"+moving_seg_name+".nii]"
    '''
    sct.run(cmd)

    if init:
        # transform the .mat warping field into a nifti file:
        warp0_mat = moving_name_reg+'0GenericAffine.mat'
        warp0, warp0_inv = transform_mat_to_warp(warp0_mat, moving_name_reg+ext, fixed_name+ext)
        if param.transformation == 'Affine':
            warp_tot = warp0
            inverse_warp_tot = warp0_inv
        else:
            warp1 = moving_name_reg+'1Warp.nii.gz'
            warp1_inv = moving_name_reg+'1InverseWarp.nii.gz'

            # Concatenate the warping fields from the 2 steps into a warp total and an inverse
            warp_tot = moving_name_reg+'_warp_tot'+ext
            inverse_warp_tot = moving_name_reg+'_inverse_warp_tot'+ext

            sct.run('sct_concat_transfo -w '+warp0+','+warp1+' -d '+fixed_name+ext+' -o '+warp_tot)
            sct.run('sct_concat_transfo -w '+warp1_inv+','+warp0_inv+' -d '+moving_name+ext+' -o '+inverse_warp_tot)
    else:
        warp_tot = moving_name_reg+'0Warp.nii.gz'
        inverse_warp_tot = moving_name_reg+'0InverseWarp.nii.gz'


    if param.metric == 'MeanSquares':
        # removing offset
        fixed_im = Image(fixed_name+ext)
        fixed_im.data = (fixed_im.data - new_min)*(old_max - old_min)/(new_max - new_min) + old_min
        fixed_im.save()

        moving_im = Image(moving_name_reg+ext_multimodal)
        moving_im.data = (moving_im.data - new_min)*(old_max - old_min)/(new_max - new_min) + old_min
        moving_im.save()


    # un-padding the images
    moving_name_unpad = moving_name_reg+"_unpadded"
    fixed_name_unpad = fixed_name+"_unpadded"
    sct.run("sct_crop_image -i "+moving_name_reg+ext+" -dim 2 -start "+str(int(param.padding))+" -end -"+param.padding+" -o "+moving_name_unpad+ext)
    sct.run("sct_crop_image -i "+fixed_name+ext+" -dim 2 -start "+str(int(param.padding))+" -end -"+param.padding+" -o "+fixed_name_unpad+ext)

    '''
    # warp_tot = moving_name_reg+"0Warp.nii.gz"
    # inverse_warp_tot = moving_name_reg+"0InverseWarp.nii.gz"
    warp_unpad = sct.add_suffix(warp_tot, '_unpad')  # moving_name_reg+"0Warp_unpad.nii.gz"
    inverse_warp_unpad =  sct.add_suffix(inverse_warp_tot, '_unpad')  # moving_name_reg+"0InverseWarp_unpad.nii.gz"
    sct.run("sct_crop_image -i "+warp_tot+" -dim 2 -start "+str(int(param.padding))+" -end -"+param.padding+" -o "+warp_unpad)
    sct.run("sct_crop_image -i "+inverse_warp_tot+" -dim 2 -start "+str(int(param.padding))+" -end -"+param.padding+" -o "+inverse_warp_unpad)
    '''


    path_output, file_output, ext_output = sct.extract_fname(param.fname_output)
    warp_output = file_output+"_Warp"+ext_output
    inverse_warp_output = file_output+"_InverseWarp"+ext_output


    # with multimodal_reg
    sct.run("mv warp_"+moving_name+"2"+fixed_name+".nii.gz "+warp_output)
    sct.run("mv warp_"+fixed_name+"2"+moving_name+".nii.gz "+inverse_warp_output)
    '''
    # with antsReg
    sct.run("mv "+warp_unpad+" "+warp_output)
    sct.run("mv "+inverse_warp_unpad+" "+inverse_warp_output)
    '''

    moving_name_out = file_output+ext_output
    # put the result and the reference in the same space using a registration with ANTs with no iteration:
    sct.run('isct_antsRegistration -d 3 -t Translation[0] -m MI['+fixed_name_unpad+ext+','+moving_name_unpad+ext+',1,16] -o [regAffine,'+moving_name_out+'] -n BSpline[3] -c 0 -f 1 -s 0')

    return warp_output, inverse_warp_output
dataset/ETHPy150Open neuropoly/spinalcordtoolbox/dev/sct_register_graymatter/sct_register_graymatter.py/wm_registration/original