def testCircleToCMonomodalDiffeomorphic(lambdaParam):
    import numpy as np
    import tensorFieldUtils as tf
    import matplotlib.pyplot as plt
    import registrationCommon as rcommon
    fname0='data/circle.png'
    #fname0='data/C_trans.png'
    fname1='data/C.png'
    circleToCDisplacementName='circleToCDisplacement.npy'
    circleToCDisplacementInverseName='circleToCDisplacementInverse.npy'
    nib_moving=plt.imread(fname0)
    nib_fixed=plt.imread(fname1)
    moving=nib_moving[:,:,0]
    fixed=nib_fixed[:,:,1]
    moving=(moving-moving.min())/(moving.max() - moving.min())
    fixed=(fixed-fixed.min())/(fixed.max() - fixed.min())
    level=3
    maskMoving=moving>0
    maskFixed=fixed>0
    movingPyramid=[img for img in rcommon.pyramid_gaussian_2D(moving, level, np.ones_like(maskMoving))]
    fixedPyramid=[img for img in rcommon.pyramid_gaussian_2D(fixed, level, np.ones_like(maskFixed))]
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacementList=[]
    maxOuterIter=[10,50,100,100,100,100,100,100,100]
    if(os.path.exists(circleToCDisplacementName)):
        displacement=np.load(circleToCDisplacementName)
        inverse=np.load(circleToCDisplacementInverseName)
    else:
        displacement, inverse=estimateMonomodalDiffeomorphicField2DMultiScale(movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0,displacementList)
        np.save(circleToCDisplacementName, displacement)
        np.save(circleToCDisplacementInverseName, inverse)    
    X1,X0=np.mgrid[0:displacement.shape[0], 0:displacement.shape[1]]
    detJacobian=rcommon.computeJacobianField(displacement)
    plt.figure()
    plt.imshow(detJacobian)
    CS=plt.contour(X0,X1,detJacobian,levels=[0.0], colors='b')
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('det(J(displacement))')
    print 'J range:', '[', detJacobian.min(), detJacobian.max(),']'
    #directInverse=np.array(tf.invert_vector_field(displacement, 0.5, 1000, 1e-7))
    directInverse=np.array(tf.invert_vector_field_fixed_point(displacement, 100, 1e-7))
    detJacobianInverse=rcommon.computeJacobianField(directInverse)
    plt.figure()
    plt.imshow(detJacobianInverse)
    CS=plt.contour(X0,X1,detJacobianInverse, levels=[0.0],colors='w')
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('det(J(displacement^-1))')
    print 'J^-1 range:', '[', detJacobianInverse.min(), detJacobianInverse.max(),']'
    #directInverse=rcommon.invert_vector_field_fixed_point(displacement, 1000, 1e-7)
    residual, stats=tf.compose_vector_fields(displacement, inverse)
    residual=np.array(residual)
    directResidual,stats=tf.compose_vector_fields(displacement, directInverse)
    directResidual=np.array(directResidual)
#    warpPyramid=[rcommon.warpImage(movingPyramid[i], displacementList[i]) for i in range(level+1)]
#    rcommon.plotOverlaidPyramids(warpPyramid, fixedPyramid)
#    rcommon.overlayImages(warpPyramid[0], fixedPyramid[0])
    rcommon.plotDiffeomorphism(displacement, inverse, residual, 'inv-joint', 7)
    rcommon.plotDiffeomorphism(displacement, directInverse, directResidual, 'inv-direct', 7)
    tf.write_double_buffer(displacement.reshape(-1), 'displacement.bin')
def testInversion_invertible():
    displacement_clean=tf.create_invertible_displacement_field(256, 256, 0.5, 8)
    detJacobian=rcommon.computeJacobianField(displacement_clean)
    plt.figure()
    plt.imshow(detJacobian)
    print 'Range:', detJacobian.min(), detJacobian.max()
    X1,X0=np.mgrid[0:displacement_clean.shape[0], 0:displacement_clean.shape[1]]
    CS=plt.contour(X0,X1,detJacobian,levels=[0.0], colors='b')
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('det(J(displacement))')
    displacement=displacement_clean+np.random.normal(0.0, 1.1, displacement_clean.shape)
    #displacement=np.array(displacement_clean)
    #inverse=rcommon.invert_vector_field_fixed_point(displacement, 100, 1e-7)
    #inverse=np.array(tf.invert_vector_field(displacement, 0.1, 100, 1e-7))
    lambdaParam=5.0
    #########Jacobi##########
    inverse=np.array(tf.invert_vector_field(displacement, lambdaParam, 100, 1e-6))
    residual, stats=tf.compose_vector_fields(displacement_clean, inverse)
    residual=np.array(residual)
    print 'Jacobi. Max:',stats[0], '. Mean:',stats[1],'Std:',stats[2]
    [d,invd,res, detJ]=rcommon.plotDiffeomorphism(displacement, inverse, residual, 'Jacobi', 7)
    #########Fixed point######
    inverse=np.array(tf.invert_vector_field_fixed_point(displacement, 100, 1e-6))
    residual, stats=tf.compose_vector_fields(displacement_clean, inverse)
    residual=np.array(residual)
    print 'Fixed point. Max:',stats[0], '. Mean:',stats[1],'Std:',stats[2]
    [d,invd,res, detJ]=rcommon.plotDiffeomorphism(displacement, inverse, residual, 'Fixed point', 7)
    #########TV-L2###########
    inverse=np.array(tf.invert_vector_field_tv_l2(displacement, lambdaParam, 3000, 1e-6))
    residual, stats=tf.compose_vector_fields(displacement_clean, inverse)
    residual=np.array(residual)
    print 'TV-L2. Max:',stats[0], '. Mean:',stats[1],'Std:',stats[2]
    [d,invd,res, detJ]=rcommon.plotDiffeomorphism(displacement, inverse, residual, 'TV-L2', 7)
def testInversion_invertible():
    displacement_clean = tf.create_invertible_displacement_field(
        256, 256, 0.5, 8)
    detJacobian = rcommon.computeJacobianField(displacement_clean)
    plt.figure()
    plt.imshow(detJacobian)
    print 'Range:', detJacobian.min(), detJacobian.max()
    X1, X0 = np.mgrid[0:displacement_clean.shape[0],
                      0:displacement_clean.shape[1]]
    CS = plt.contour(X0, X1, detJacobian, levels=[0.0], colors='b')
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('det(J(displacement))')
    displacement = displacement_clean + np.random.normal(
        0.0, 1.1, displacement_clean.shape)
    #displacement=np.array(displacement_clean)
    #inverse=rcommon.invert_vector_field_fixed_point(displacement, 100, 1e-7)
    #inverse=np.array(tf.invert_vector_field(displacement, 0.1, 100, 1e-7))
    lambdaParam = 5.0
    #########Jacobi##########
    inverse = np.array(
        tf.invert_vector_field(displacement, lambdaParam, 100, 1e-6))
    residual, stats = tf.compose_vector_fields(displacement_clean, inverse)
    residual = np.array(residual)
    print 'Jacobi. Max:', stats[0], '. Mean:', stats[1], 'Std:', stats[2]
    [d, invd, res, detJ] = rcommon.plotDiffeomorphism(displacement, inverse,
                                                      residual, 'Jacobi', 7)
    #########Fixed point######
    inverse = np.array(
        tf.invert_vector_field_fixed_point(displacement, 100, 1e-6))
    residual, stats = tf.compose_vector_fields(displacement_clean, inverse)
    residual = np.array(residual)
    print 'Fixed point. Max:', stats[0], '. Mean:', stats[1], 'Std:', stats[2]
    [d, invd, res,
     detJ] = rcommon.plotDiffeomorphism(displacement, inverse, residual,
                                        'Fixed point', 7)
    #########TV-L2###########
    inverse = np.array(
        tf.invert_vector_field_tv_l2(displacement, lambdaParam, 3000, 1e-6))
    residual, stats = tf.compose_vector_fields(displacement_clean, inverse)
    residual = np.array(residual)
    print 'TV-L2. Max:', stats[0], '. Mean:', stats[1], 'Std:', stats[2]
    [d, invd, res, detJ] = rcommon.plotDiffeomorphism(displacement, inverse,
                                                      residual, 'TV-L2', 7)
def testCircleToCMonomodalDiffeomorphic(lambdaParam):
    import numpy as np
    import tensorFieldUtils as tf
    import matplotlib.pyplot as plt
    import registrationCommon as rcommon
    fname0 = 'data/circle.png'
    #fname0='data/C_trans.png'
    fname1 = 'data/C.png'
    circleToCDisplacementName = 'circleToCDisplacement.npy'
    circleToCDisplacementInverseName = 'circleToCDisplacementInverse.npy'
    nib_moving = plt.imread(fname0)
    nib_fixed = plt.imread(fname1)
    moving = nib_moving[:, :, 0]
    fixed = nib_fixed[:, :, 1]
    moving = (moving - moving.min()) / (moving.max() - moving.min())
    fixed = (fixed - fixed.min()) / (fixed.max() - fixed.min())
    level = 3
    maskMoving = moving > 0
    maskFixed = fixed > 0
    movingPyramid = [
        img for img in rcommon.pyramid_gaussian_2D(moving, level,
                                                   np.ones_like(maskMoving))
    ]
    fixedPyramid = [
        img for img in rcommon.pyramid_gaussian_2D(fixed, level,
                                                   np.ones_like(maskFixed))
    ]
    rcommon.plotOverlaidPyramids(movingPyramid, fixedPyramid)
    displacementList = []
    maxOuterIter = [10, 50, 100, 100, 100, 100, 100, 100, 100]
    if (os.path.exists(circleToCDisplacementName)):
        displacement = np.load(circleToCDisplacementName)
        inverse = np.load(circleToCDisplacementInverseName)
    else:
        displacement, inverse = estimateMonomodalDiffeomorphicField2DMultiScale(
            movingPyramid, fixedPyramid, lambdaParam, maxOuterIter, 0,
            displacementList)
        np.save(circleToCDisplacementName, displacement)
        np.save(circleToCDisplacementInverseName, inverse)
    X1, X0 = np.mgrid[0:displacement.shape[0], 0:displacement.shape[1]]
    detJacobian = rcommon.computeJacobianField(displacement)
    plt.figure()
    plt.imshow(detJacobian)
    CS = plt.contour(X0, X1, detJacobian, levels=[0.0], colors='b')
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('det(J(displacement))')
    print 'J range:', '[', detJacobian.min(), detJacobian.max(), ']'
    #directInverse=np.array(tf.invert_vector_field(displacement, 0.5, 1000, 1e-7))
    directInverse = np.array(
        tf.invert_vector_field_fixed_point(displacement, 100, 1e-7))
    detJacobianInverse = rcommon.computeJacobianField(directInverse)
    plt.figure()
    plt.imshow(detJacobianInverse)
    CS = plt.contour(X0, X1, detJacobianInverse, levels=[0.0], colors='w')
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('det(J(displacement^-1))')
    print 'J^-1 range:', '[', detJacobianInverse.min(), detJacobianInverse.max(
    ), ']'
    #directInverse=rcommon.invert_vector_field_fixed_point(displacement, 1000, 1e-7)
    residual, stats = tf.compose_vector_fields(displacement, inverse)
    residual = np.array(residual)
    directResidual, stats = tf.compose_vector_fields(displacement,
                                                     directInverse)
    directResidual = np.array(directResidual)
    #    warpPyramid=[rcommon.warpImage(movingPyramid[i], displacementList[i]) for i in range(level+1)]
    #    rcommon.plotOverlaidPyramids(warpPyramid, fixedPyramid)
    #    rcommon.overlayImages(warpPyramid[0], fixedPyramid[0])
    rcommon.plotDiffeomorphism(displacement, inverse, residual, 'inv-joint', 7)
    rcommon.plotDiffeomorphism(displacement, directInverse, directResidual,
                               'inv-direct', 7)
    tf.write_double_buffer(displacement.reshape(-1), 'displacement.bin')