def test_obj(amp=1, order_poly=3, num_knots=5, noise=.1, lenxx = 3, lenxy = 2, lenyx=5, lenyy=8, weights=None):
    '''
    tests total transform object in high_order
    '''

    x = (np.random.random(1000) -.5 ) * 4096
    y = (np.random.random(1000) -.5 ) * 4096

    xref, yref = periodic_dist(x,y,amp, lenxx, lenxy, lenyx, lenyy)
    xref = xref + noise * np.random.randn(len(xref)) 
    yref = yref + noise * np.random.randn(len(yref))

    t = high_order.transform(x, y, xref, yref, order_poly=order_poly, num_knots=num_knots, weights=weights)

    x_u = (np.random.random(1000) -.5 ) * 4096
    y_u = (np.random.random(1000) -.5 ) * 4096

    xref_u, yref_u = periodic_dist(x_u,y_u,amp, lenxx, lenxy, lenyx, lenyy)
    xref_u = xref_u + noise * np.random.randn(len(xref_u)) 
    yref_u = yref_u + noise * np.random.randn(len(yref_u))

    xref_u_f , yref_u_f = t.evaluate(x_u, y_u)

    print 'average residual i  x is ', np.sum(np.abs(xref_u_f - xref_u)) / len(xref_u_f)
    print 'average residual i  y is ', np.sum(np.abs(yref_u_f - yref_u)) /  len(yref_u_f)
def mk_data_per(points=100,order=4):

    x1 = np.random.rand(1000)*4096 - 2048 
    y1 = np.random.rand(1000)*4096 - 2048 

    xs = np.random.rand(1000)*4096 - 2048
    ys = np.random.rand(1000)*4096 - 2048
    
    Zx =  np.cos(2 * np.pi * x1 / 4) * np.cos(2 * np.pi * y1 / 2)
    Zxs = np.cos(2 * np.pi * xs / 4) * np.cos(2 * np.pi * ys / 2)
    Zy =  np.cos(2 * np.pi * x1 / 3) * np.cos(2 * np.pi * y1 / 1)
    Zys = np.cos(2 * np.pi * xs / 3) * np.cos(2 * np.pi * ys / 1)

    xref =  x1 + Zx
    yref = y1 + Zy
    xrefs = xs + Zxs
    yrefs = ys + Zys 

    tpoly = high_order.transform(x1,y1, xref, yref, order_poly=5, fit_spline_b=False, leg=False)
    xpoly, ypoly = tpoly.evaluate(x1,y1)
    xpolys,ypolys = tpoly.evaluate(xs,ys)

    print np.sum(np.abs(xpolys-xrefs))
    print np.sum(np.abs(xpoly-xref))
    print np.sum(np.abs(xs-xrefs))

    return xpoly, ypoly
def test_polynomial_obj(num_co=10, order_poly=5, num_knots=4):
    '''
    test 5th order polynomial on spline and polnyomial fits
    '''

    '''
    keep rotation zmall, maybe translation a few pixels
    '''
    
    xref = (np.random.random(1000) -.5 ) * 4096
    yref = (np.random.random(1000) -.5 ) * 4096

    #coeff_x = (np.random.random(num_co) - .5) * .01
    #coeff_y = (np.random.random(num_co) - .5) * .01
    #pick static set instead, then tweaking once it works better
    coeff_x = np.array([20.0, .5, .2, .02 , .001 , .07, .009, .003, .004, .006, .0005,.0003,.0007,.0001,.0004])
    coeff_y = np.array([20.0, .7, .3, .06 , .004 , .09, .004, .007, .002, .005, .0007,.0005,.0003,.0002,.0005])
    
    
    x_t = high_order.poly(np.array([xref,yref]), coeff_x)
    y_t = high_order.poly(np.array([xref,yref]), coeff_y)

    t = high_order.transform(xref,yref,x_t,y_t,order_poly=order_poly, num_knots=num_knots)

    x_t_ev, y_t_ev = t.evaluate(xref, yref) 
    

    x_u = (np.random.random(2000) -.5 ) * 4096
    y_u = (np.random.random(2000) -.5 ) * 4096

    xt_u = high_order.poly(np.array([x_u,y_u]), coeff_x)
    yt_u = high_order.poly(np.array([x_u,y_u]), coeff_y)

    xt_u_m, yt_u_m = t.evaluate(x_u, y_u)

    print 'REFERENCE LISTS:average difference between transform and input in x', np.sum(np.abs(xt_u_m - xt_u))/len(xt_u)
    print 'REFERENCE LISTS: average difference between transform and input in y', np.sum(np.abs(yt_u_m - yt_u))/len(yt_u)

    print 'RANDOM LIST:average difference between transform and input in x', np.sum(np.abs(x_t_ev - x_t))/len(x_t)
    print 'RANDOM LIST:average difference between transform and input in y', np.sum(np.abs(y_t_ev - y_t))/len(y_t)

    print np.max(x_u), np.min(x_u)
    print np.max(xt_u), np.min(xt_u)

    return t, coeff_x, coeff_y
def mk_fake_data(points = 1000, param=15):
    '''
    go ahead and make some fake data
    '''


    x1 = np.random.rand(1000)*4096 - 2048 
    y1 = np.random.rand(1000)*4096 - 2048 

    xs = np.random.rand(1000)*4096 - 2048
    ys = np.random.rand(1000)*4096 - 2048

    co_ran = [1,10**-6,10**-6 ,10**-8,10**-8,10**-8, 10**-9,10**-9,10**-9,10**-9,10**-10,10**-10,10**-10,10**-10,10**-10]

    
     
    coeff_trans_x = np.random.randn(param)
    coeff_trans_y = np.random.randn(param)

    for i in range(len(coeff_trans_x)):
        if i ==1:
            coeff_trans_x[i] = 1.0
        elif i < 15:
            coeff_trans_x[i] = coeff_trans_x[i] * co_ran[i]
        else:
            coeff_trans_x[i] = coeff_trans_x[i] * co_ran[-1]
    

    for i in range(len(coeff_trans_y)):
        if i ==2:
            coeff_trans_y[i] = 1.0
        elif i < 15:
            coeff_trans_y[i] = coeff_trans_y[i] * co_ran[i]
        else:
            coeff_trans_y[i] = coeff_trans_y[i] * co_ran[-1]

    coeff_trans_x =[ -3.71658981e-01 ,  1.00000000e+00 , 0 , -1.20639707e-09, -1.37110890e-08 ,  8.39249153e-09 ,  1.69986169e-10 , -1.39590886e-10,-2.00797325e-09,  -3.08447522e-10,   1.50453620e-11,  -3.51821590e-11,-9.86845535e-11,   8.69664940e-11,  -3.52721864e-11]
    coeff_trans_y = [  2.58551652e-01,  0,   1.00000000e+00,   1.18005089e-09,-4.63985850e-09 ,  1.42579849e-09 ,  1.58246425e-09 , -1.40892711e-09,5.57847993e-11  , 1.16534465e-10 , -3.50121458e-11,   1.49194611e-11,5.37039418e-11,  -6.21582929e-11,   6.93338443e-12]

    # old coeff -4.40296981e-07 
    #old coeff  -1.26099851e-06

    
    
    
    xref = high_order.poly(np.array([x1,y1]), coeff_trans_x) 
    yref = high_order.poly(np.array([x1,y1]), coeff_trans_y)
    xrefs = high_order.poly(np.array([xs,ys]), coeff_trans_x) 
    yrefs = high_order.poly(np.array([xs,ys]), coeff_trans_y)


    plt.quiver(x1,y1,xref-x1,yref-y1)
    plt.show()
    print 'difference after trnasform', np.sum(np.abs(xref-x1)), np.sum(np.abs(yref-y1))
    print np.max(xref), np.min(xref)
    print np.max(yref), np.min(yref)

    #xref = x1 + del_x
    #yref = y1 + del_y


    tpoly = high_order.transform(x1,y1, xref, yref, order_poly=3, fit_spline_b=False, leg=False)
    xpoly, ypoly = tpoly.evaluate(x1,y1)
    xpolys,ypolys = tpoly.evaluate(xs,ys)


    print np.sum(np.abs(xpolys-xrefs))
    print np.sum(np.abs(ypoly-yref))
    #print np.sum(np.abs(xs-xrefs))
    print coeff_trans_x
    print coeff_trans_y
    print (coeff_trans_x[0:len(tpoly.coeff_x)]- tpoly.coeff_x) #/ coeff_trans_x[0:len(tpoly.coeff_x)]
    print (coeff_trans_y[0:len(tpoly.coeff_x)]- tpoly.coeff_y) #/ coeff_trans_y[0:len(tpoly.coeff_x)]


    tpoly = high_order.transform(x1,y1,xref,yref,leg=True)
    tpoly.c_x = np.reshape(np.random.random(25), (5,5))
    tpoly.c_y = np.reshape(np.random.random(25), (5,5))
    tpoly.leg = True
    xref, yref  = tpoly.evaluate(x1,y1)
    xrefs, yrefs = tpoly.evaluate(xs,ys)
    print np.max(xref), np.min(xref)

    tleg = high_order.transform(x1,y1,xref,yref, order_poly=5, fit_spline_b=False, leg=True)
    xleg, yleg  = tleg.evaluate(x1,y1)
    xlegs, ylegs = tleg.evaluate(xs,ys)
    
    print np.sum(np.abs(xlegs-xrefs)) / len(xlegs)
    print np.sum(np.abs(xleg-xref)) / len(xref)
    #print np.sum(np.abs(xs-xrefs))
    print  tleg.c_y
    print  tleg.c_x
    
    #print coeff_trans_y
    #print (coeff_trans_x[0:len(tpoly.coeff_x)]- tpoly.coeff_x) #/ coeff_trans_x[0:len(tpoly.coeff_x)]
    #print (coeff_trans_y[0:len(tpoly.coeff_x)]- tpoly.coeff_y) #/ coeff_trans_y[0:len(tpoly.coeff_x)]

    #plt.scatter(xpoly,ypoly)
    #plt.show()
    return xref, yref