def test_transform(num_free_param=10,co_ran=.1):


    
    #x1 =  np.linspace(0,2000,num=1000) - 1000
    #x1 = np.concatenate([x1, x1, x1, x1, x1, x1, x1, x1, x1, x1])
    #y1 = np.zeros(x1.shape)
    #for i in range(10):
    #    for j in range(1000):
    #        y1[i*1000 + j] = i*200.0-1000

    #print y1

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


    coeff_trans_x = np.random.randn(num_free_param)*co_ran
    coeff_trans_y = np.random.randn(num_free_param)*co_ran

    del_x = high_order.poly(np.array([x1,y1]), coeff_trans_x) 
    del_y = high_order.poly(np.array([x1,y1]), coeff_trans_y)

    xref = x1 + del_x
    yref = y1 + del_y

    c_x, c_y = high_order.fit_poly(x1, y1, del_x,  del_y , len(coeff_trans_x))

    print 'Transform Coefficents (given)' 
    print  coeff_trans_x
    print coeff_trans_y
    print 'Calculated Coefficients'
    print c_x
    print c_y 
    return x1, y1, xref, yref 
def coeff_err(num_poly_param=10, size_rand_co=.1, trials=1000, num_points=100):
    '''
    calulates then plots the fracinoal error in polynomial coefficients copmared to known transform
    '''

    first_time = True
    co_x_frac = []
    co_y_frac = []
    
    for i in range(trials):
        x = (np.random.random(num_points) -.5 ) * 4096
        y = (np.random.random(num_points) -.5 ) * 4096

        co_x =  np.random.random(num_poly_param) * size_rand_co 
        co_y =  np.random.random(num_poly_param) * size_rand_co 

        xref = high_order.poly(np.array([x,y]) , co_x)
        yref = high_order.poly(np.array([x,y]) , co_y)

        c_x, c_y = high_order.fit_poly(x,y,xref,yref,num_poly_param)
        for ii in range(num_poly_param):
            if first_time:
                co_x_frac.append([])
                co_y_frac.append([])
    
            
            co_x_frac[ii].append(np.abs((co_x[ii]-c_x[ii])/co_x[ii]))
            co_y_frac[ii].append(np.abs((co_y[ii]-c_y[ii])/co_y[ii]))
                
        first_time = False
        
                
    co_x_frac = np.median(np.array(co_x_frac), axis=1)
    co_y_frac = np.median(np.array(co_y_frac), axis=1)

    plt.plot(co_x_frac[1:])
    plt.plot(co_y_frac[1:])

    plt.show()
def test_total(num_poly=10, num_knots=4, noise=0.1, smooth=False):
    '''
    Function that performs transformation between x, y and xref yref then fits that difference using polynomial fit followed by spline
    num_poly is the number of polynomial terms used, 10 gets all quadratic terms
    num_knots is th enumber of knots in the slpine along each axis, that is 4 means there is a total of 16 knots 
    noise is the sigma (in pixels) used to 
    '''


    xref = np.random.rand(10000) * 4096 - 2048
    yref = np.random.rand(10000) * 4096 - 2048 


    '''
    these star lists will be put through both the known tranformation (rotation and translation) and also put through the derived polynomial and spline fits as a check
    '''
    x_dim_ref = np.random.rand(1000) * 4096 - 2048
    y_dim_ref = np.random.rand(1000) * 4096 - 2048 

     
    
    trans = transforms.Affine2D()
    trans.rotate_deg(75.0)
    trans.translate(184, -45)
    
    cooref = np.array([xref, yref]).T
    
    coo1 = trans.transform(cooref) 
    coo_dim = trans.transform(np.array([x_dim_ref,y_dim_ref]).T)
    
    x_dim1 = coo_dim[:,0] + noise*np.random.randn(len(x_dim_ref))
    y_dim1 = coo_dim[:,1] + noise*np.random.randn(len(y_dim_ref))
    
    x1 = coo1[:,0] + noise*np.random.randn(len(xref))
    y1 = coo1[:,1] + noise*np.random.randn(len(yref))

    c_x, c_y = high_order.fit_poly(x1, y1, xref,  yref, num_poly)

    x_poly = high_order.poly(np.array([x1,y1]), c_x)
    y_poly = high_order.poly(np.array([x1,y1]), c_y)

    #if np.sum(np.abs(x_poly-xref)) < 1 and  np.sum(np.abs(y_poly-yref)) < len(xref):
    #    print 'Polynomial Fit was sufficient'
    #    return  c_x, c_y

    
    '''
    Now do spline fit between the polynomial fit and reference, to get rid of residual
    '''

    dx_sp, spline_dx = high_order.fit_spline(x_poly, y_poly, xref-x_poly, num_knots=num_knots, smooth=smooth)
    dy_sp, spline_dy = high_order.fit_spline(x_poly, y_poly, yref-y_poly, num_knots=num_knots, smooth=smooth)

    x_sp, spline_x = high_order.fit_spline(x_poly, y_poly, xref, num_knots=num_knots, smooth=smooth)
    y_sp, spline_y = high_order.fit_spline(x_poly, y_poly, yref, num_knots=num_knots, smooth=smooth)

    
    assert np.sum(np.abs(x_sp-(x_poly+dx_sp)))/len(x_poly) < noise
    assert np.sum(np.abs(y_sp-(y_poly+dy_sp)))/len(y_poly) < noise

    assert np.sum(np.abs(x_sp - xref))/len(x_sp) < noise
    assert np.sum(np.abs(y_sp - yref))/len(y_sp) < noise

    x_dim_poly = high_order.poly(np.array([x_dim1,y_dim1]), c_x)
    y_dim_poly = high_order.poly(np.array([x_dim1,y_dim1]), c_y)

    assert np.sum(np.abs(x_dim_poly - x_dim_ref)) / len(x_dim_ref) < noise
    assert np.sum(np.abs(y_dim_poly - y_dim_ref)) / len(y_dim_ref) < noise

    x_dim_sp = spline_x.ev(x_dim_poly, y_dim_poly)
    y_dim_sp = spline_y.ev(x_dim_poly,y_dim_poly)

    assert np.sum(np.abs(x_dim_sp - x_dim_ref)) / len(x_dim_ref) < noise
    assert np.sum(np.abs(y_dim_sp - y_dim_ref)) / len(y_dim_ref) < noise

    
    
    
    return x_sp, y_sp, xref, yref, spline_x, spline_y , c_x, c_y, x1, y1