def apply_ants_transfo(fixed_im, moving_im, search_reg=True, transfo_type='Affine', metric='MI', apply_transfo=True, transfo_name='', binary=True, path='./', inverse=0, verbose=0):
    """
    Compute and/or apply a registration using ANTs

    :param fixed_im: fixed image for the registration, type: numpy array

    :param moving_im: moving image for the registration, type: numpy array

    :param search_reg: compute (search) or load (from a file) the transformation to do, type: boolean

    :param transfo_type: type of transformation to apply, type: string

    :param apply_transfo: apply or not the transformation, type: boolean

    :param transfo_name: name of the file containing the transformation information (to load or save the transformation, type: string

    :param binary: if the image to register is binary, type: boolean

    :param path: path where to load/save the transformation

    :param inverse: apply inverse transformation

    :param verbose: verbose
    """
    import time
    res_im = None
    try:
        transfo_dir = transfo_type.lower() + '_transformations'
        if transfo_dir not in os.listdir(path):
            sct.run('mkdir ' + path + transfo_dir)
        dir_name = 'tmp_reg_' + time.strftime("%y%m%d%H%M%S") + '_' + str(time.time())
        sct.run('mkdir ' + dir_name, verbose=verbose)
        os.chdir('./' + dir_name)

        if binary:
            t = 'uint8'
        else:
            t = ''

        fixed_im_name = 'fixed_im'
        Image(param=np.asarray(fixed_im), absolutepath='./'+fixed_im_name+'.nii.gz').save(type=t)
        moving_im_name = 'moving_im'
        Image(param=np.asarray(moving_im), absolutepath='./'+moving_im_name+'.nii.gz').save(type=t)

        mat_name, inverse_mat_name = find_ants_transfo_name(transfo_type)

        if search_reg:
            reg_interpolation = 'BSpline'
            transfo_params = ''
            if transfo_type == 'BSpline':
                transfo_params = ',1'
            elif transfo_type == 'BSplineSyN':
                transfo_params = ',3,0'
                reg_interpolation = 'NearestNeighbor'
            elif transfo_type == 'SyN':
                transfo_params = ',1,1'
            gradientstep = 0.5  # 0.3
            # metric_params = ',1,4'  # for MeanSquares
            metric_params = ',1,2'  # for MI
            niter = 5
            smooth = 0
            shrink = 1
            cmd_reg = 'isct_antsRegistration -d 2 -n ' + reg_interpolation + ' -t ' + transfo_type + '[' + str(gradientstep) + transfo_params + '] ' \
                      '-m ' + metric + '[' + fixed_im_name + '.nii.gz,' + moving_im_name + '.nii.gz ' + metric_params + '] -o reg  -c ' + str(niter) + \
                      ' -s ' + str(smooth) + ' -f ' + str(shrink) + ' -v ' + str(verbose)  # + ' -r [' + fixed_im_name + '.nii.gz,' + moving_im_name + '.nii.gz ' + ',1]'

            sct.run(cmd_reg, verbose=verbose)

            sct.run('cp ' + mat_name + ' ../' + path + transfo_dir + '/'+transfo_name, verbose=verbose)
            if 'SyN' in transfo_type:
                sct.run('cp ' + inverse_mat_name + ' ../' + path + transfo_dir + '/'+transfo_name + '_inversed',
                        verbose=verbose)

        if apply_transfo:
            if not search_reg:
                sct.run('cp ../' + path + transfo_dir + '/' + transfo_name + ' ./' + mat_name, verbose=verbose)
                if 'SyN' in transfo_type:
                    sct.run('cp ../' + path + transfo_dir + '/' + transfo_name + '_inversed' + ' ./' + inverse_mat_name,
                            verbose=verbose)

            if binary or moving_im.max() == 1 or fixed_im.max() == 1:
                apply_transfo_interpolation = 'NearestNeighbor'
            else:
                apply_transfo_interpolation = 'BSpline'

            if 'SyN' in transfo_type and inverse:
                cmd_apply = 'isct_antsApplyTransforms -d 2 -i ' + moving_im_name + '.nii.gz -o ' + moving_im_name + '_moved.nii.gz ' \
                            '-n ' + apply_transfo_interpolation + ' -t [' + inverse_mat_name + '] ' \
                            '-r ' + fixed_im_name + '.nii.gz -v ' + str(verbose)

            else:
                cmd_apply = 'isct_antsApplyTransforms -d 2 -i ' + moving_im_name + '.nii.gz -o ' + moving_im_name + '_moved.nii.gz ' \
                            '-n ' + apply_transfo_interpolation + ' -t [' + mat_name + ',' + str(inverse) + '] ' \
                            '-r ' + fixed_im_name + '.nii.gz -v ' + str(verbose)

            sct.run(cmd_apply, verbose=verbose)

            res_im = Image(moving_im_name + '_moved.nii.gz')
    except Exception, e:
        sct.printv('WARNING: AN ERROR OCCURRED WHEN DOING RIGID REGISTRATION USING ANTs', 1, 'warning')
        print e
    else:
        sct.printv('Removing temporary files ...', verbose=verbose, type='normal')
        os.chdir('..')
        sct.run('rm -rf ' + dir_name + '/', verbose=verbose)

    if apply_transfo and res_im is not None:
        return res_im.data


# ----------------------------------------------------------------------------------------------------------------------
dataset/ETHPy150Open neuropoly/spinalcordtoolbox/scripts/msct_gmseg_utils.py/apply_ants_transfo/original