def digits_one_movie(m, n, theta):
    """
    Create movie of interpolation between two digits.

    Parameters
    ----------

    m, n: integer
          Index of source and target digit.

    theta: real
           Initial angle for matching problem.

    """

    c0 = DiscreteCurve()
    c0.load(DATA_FOLDER + '/%d_processed.mat' % m)
    c0.translate(-c0[0,:])

    print "*** SOURCE: %d ***" % m

    c1 = DiscreteCurve()
    c1.load(DATA_FOLDER + '/%d_processed.mat' % n)
    c1.translate(-c1[0,:])

    print "*** TARGET: %d ***" % n

    movie_name = '%d_to_%d.avi' % (m, n)
    make_movie(c0, c1, theta, movie_name, 
               m=2, dims=(-1, 1.5, -1.5, 1), numframes=100)

    print "*** COMPILED MOVIE: %s ***" % movie_name
def digits_pairwise_match():
    """
    Find discrepancy and minimizing angle for each pair of digits.

    """

    discrepancy = np.empty((10, 10))
    theta_min = np.empty((10, 10))

    for m in xrange(0, 10):
        c0 = DiscreteCurve()
        c0.load(DATA_FOLDER + '/%d_processed.mat' % m)
        c0.translate(-c0[0,:])
    
        print "*** SOURCE: %d ***" % m
    
        for n in xrange(0, 10):
            c1 = DiscreteCurve()
            c1.load(DATA_FOLDER + '/%d_processed.mat' % n)
            c1.translate(-c1[0,:])
        
            print "*** TARGET: %d ***" % n
        
            mp = MatchingProblem(c0, c1, 2)
            energies = mp.sweep_theta_range(40)
        
            E_conv, E_div = separate_energies(energies)
            if len(E_conv) == 0:
                print "*** ERROR: No convergence for %d to %d ***" % (m, n)
        
            argmin = E_conv[:, 1].argmin()
            theta, E_min = E_conv[argmin, 0:2]
        
            print "* theta_min = %f, E_min = %f *" % (theta, E_min)
        
            discrepancy[m, n] = E_min
            theta_min[m, n] = theta

    return theta_min, discrepancy