# Path: lfd/rapprentice/registration.py
# DISCRETIZATION_LEVEL = 40
# MAX_CP_DIST = .05
# class Transformation(): # ParallelPython can only handle old-style classes (i.e. don't inherit Transformation from object)
# class ThinPlateSpline(Transformation):
# class Affine(Transformation):
# class Composition(Transformation):
#     def transform_points(self, x_ma):
#     def compute_jacobian(self, x_ma):
#     def transform_vectors(self, x_ma, v_ma):
#     def transform_bases(self, x_ma, rot_mad, orthogonalize=True, orth_method = "cross"):
#     def transform_hmats(self, hmat_mAD):
#     def compute_numerical_jacobian(self, x_d, epsilon=0.0001):
#     def __init__(self, d=3):
#     def transform_points(self, x_ma):
#     def compute_jacobian(self, x_ma):
#     def __init__(self, lin_ag, trans_g):
#     def transform_points(self, x_ma):
#     def compute_jacobian(self, x_ma):
#     def __init__(self, fs):
#     def transform_points(self, x_ma):
#     def compute_jacobian(self, x_ma):
# def fit_ThinPlateSpline(x_na, y_ng, bend_coef=.1, rot_coef = 1e-5, wt_n=None):
# def fit_ThinPlateSpline_RotReg(x_na, y_ng, bend_coef = .1, rot_coefs = (0.01,0.01,0.0025),scale_coef=.01):
# def loglinspace(a,b,n):
# def unit_boxify(x_na):    
# def unscale_tps_3d(f, src_params, targ_params):
# def unscale_tps(f, src_params, targ_params):
# def tps_rpm(x_nd, y_md, n_iter = 20, reg_init = .1, reg_final = .001, rad_init = .1, rad_final = .005, 
#     rot_reg=1e-4, plotting = False, f_init = None, plot_cb = None, return_corr=False):
# def fit_rotation(tps_fn, src_pts, tgt_pts):
# def tps_rpm_bij(x_nd, y_md, n_iter=20, reg_init=.1, reg_final=.001, rad_init=.1, rad_final=.005,
#              rot_reg = 1e-3, plotting = False, plot_cb = None, x_weights = None, y_weights = None, 
#              outlierprior = .1, outlierfrac = 2e-1, vis_cost_xy = None, return_corr=False):
# def tps_reg_cost(f):
# def logmap(m):
# def balance_matrix3(prob_nm, max_iter, row_priors, col_priors, outlierfrac, r_N = None):
# def balance_matrix(prob_nm, p, max_iter=20, ratio_err_tol=1e-3):
# def calc_correspondence_matrix(x_nd, y_md, r, p, max_iter=20):
# def nan2zero(x):
# def fit_score(src, targ, dist_param):
# def orthogonalize3_cross(mats_n33):
# def orthogonalize3_svd(x_k33):
# def orthogonalize3_qr(_x_k33):
# 
# Path: lfd/rapprentice/math_utils.py
# def interp2d(x,xp,yp):
# def interp_mat(x, xp):
# def normalize(x):
# def normr(x):
# def normc(x):
# def norms(x,ax):
# def intround(x):
# def deriv(x):
# def linspace2d(start,end,n):
# def remove_duplicate_rows(mat):
# def invertHmat(hmat):
#     T = len(x)
#     R = hmat[:3,:3]
# 
# Path: lfd/rapprentice/registration.py
# def loglinspace(a,b,n):
#     "n numbers between a to b (inclusive) with constant ratio between consecutive numbers"
#     return np.exp(np.linspace(np.log(a),np.log(b),n))    
# 
# class ThinPlateSpline(Transformation):
#     """
#     members:
#         x_na: centers of basis functions
#         w_ng: 
#         lin_ag: transpose of linear part, so you take x_na.dot(lin_ag)
#         trans_g: translation part
#     
#     """
#     def __init__(self, d=3):
#         "initialize as identity"
#         self.x_na = np.zeros((0,d))
#         self.lin_ag = np.eye(d)
#         self.trans_g = np.zeros(d)
#         self.w_ng = np.zeros((0,d))
# 
#     def transform_points(self, x_ma):
#         y_ng = tps_eval(x_ma, self.lin_ag, self.trans_g, self.w_ng, self.x_na)
#         return y_ng
# 
#     def compute_jacobian(self, x_ma):
#         grad_mga = tps_grad(x_ma, self.lin_ag, self.trans_g, self.w_ng, self.x_na)
#         return grad_mga
# 
# def fit_ThinPlateSpline(x_na, y_ng, bend_coef=.1, rot_coef = 1e-5, wt_n=None):
#     """
#     x_na: source cloud
#     y_nd: target cloud
#     smoothing: penalize non-affine part
#     angular_spring: penalize rotation
#     wt_n: weight the points        
#     """
#     f = ThinPlateSpline()
#     f.lin_ag, f.trans_g, f.w_ng = tps_fit3(x_na, y_ng, bend_coef, rot_coef, wt_n)
#     f.x_na = x_na
#     return f        


import numpy as np
import scipy.spatial.distance as ssd
import tps
import knot_classifier
import IPython as ipy
    import argparse, h5py, os
    import matplotlib.pyplot as plt
    import registration
    import time
from lfd.rapprentice import registration, math_utils
from lfd.rapprentice.registration import loglinspace, ThinPlateSpline, fit_ThinPlateSpline
    from rapprentice import clouds, plotting_plt
#!/usr/bin/env python

from __future__ import division


N_ITER_CHEAP = 14
EM_ITER_CHEAP = 1

def rgb2lab(rgb):
    return xyz2lab(rgb2xyz(rgb))


# Path: lfd/rapprentice/registration.py
# DISCRETIZATION_LEVEL = 40
# MAX_CP_DIST = .05
# class Transformation(): # ParallelPython can only handle old-style classes (i.e. don't inherit Transformation from object)
# class ThinPlateSpline(Transformation):
# class Affine(Transformation):
# class Composition(Transformation):
#     def transform_points(self, x_ma):
#     def compute_jacobian(self, x_ma):
#     def transform_vectors(self, x_ma, v_ma):
#     def transform_bases(self, x_ma, rot_mad, orthogonalize=True, orth_method = "cross"):
#     def transform_hmats(self, hmat_mAD):
#     def compute_numerical_jacobian(self, x_d, epsilon=0.0001):
#     def __init__(self, d=3):
#     def transform_points(self, x_ma):
#     def compute_jacobian(self, x_ma):
#     def __init__(self, lin_ag, trans_g):
#     def transform_points(self, x_ma):
#     def compute_jacobian(self, x_ma):
#     def __init__(self, fs):
#     def transform_points(self, x_ma):
#     def compute_jacobian(self, x_ma):
# def fit_ThinPlateSpline(x_na, y_ng, bend_coef=.1, rot_coef = 1e-5, wt_n=None):
# def fit_ThinPlateSpline_RotReg(x_na, y_ng, bend_coef = .1, rot_coefs = (0.01,0.01,0.0025),scale_coef=.01):
# def loglinspace(a,b,n):
# def unit_boxify(x_na):    
# def unscale_tps_3d(f, src_params, targ_params):
# def unscale_tps(f, src_params, targ_params):
# def tps_rpm(x_nd, y_md, n_iter = 20, reg_init = .1, reg_final = .001, rad_init = .1, rad_final = .005, 
#     rot_reg=1e-4, plotting = False, f_init = None, plot_cb = None, return_corr=False):
# def fit_rotation(tps_fn, src_pts, tgt_pts):
# def tps_rpm_bij(x_nd, y_md, n_iter=20, reg_init=.1, reg_final=.001, rad_init=.1, rad_final=.005,
#              rot_reg = 1e-3, plotting = False, plot_cb = None, x_weights = None, y_weights = None, 
#              outlierprior = .1, outlierfrac = 2e-1, vis_cost_xy = None, return_corr=False):
# def tps_reg_cost(f):
# def logmap(m):
# def balance_matrix3(prob_nm, max_iter, row_priors, col_priors, outlierfrac, r_N = None):
# def balance_matrix(prob_nm, p, max_iter=20, ratio_err_tol=1e-3):
# def calc_correspondence_matrix(x_nd, y_md, r, p, max_iter=20):
# def nan2zero(x):
# def fit_score(src, targ, dist_param):
# def orthogonalize3_cross(mats_n33):
# def orthogonalize3_svd(x_k33):
# def orthogonalize3_qr(_x_k33):
# 
# Path: lfd/rapprentice/math_utils.py
# def interp2d(x,xp,yp):
# def interp_mat(x, xp):
# def normalize(x):
# def normr(x):
# def normc(x):
# def norms(x,ax):
# def intround(x):
# def deriv(x):
# def linspace2d(start,end,n):
# def remove_duplicate_rows(mat):
# def invertHmat(hmat):
#     T = len(x)
#     R = hmat[:3,:3]
# 
# Path: lfd/rapprentice/registration.py
# def loglinspace(a,b,n):
#     "n numbers between a to b (inclusive) with constant ratio between consecutive numbers"
#     return np.exp(np.linspace(np.log(a),np.log(b),n))    
# 
# class ThinPlateSpline(Transformation):
#     """
#     members:
#         x_na: centers of basis functions
#         w_ng: 
#         lin_ag: transpose of linear part, so you take x_na.dot(lin_ag)
#         trans_g: translation part
#     
#     """
#     def __init__(self, d=3):
#         "initialize as identity"
#         self.x_na = np.zeros((0,d))
#         self.lin_ag = np.eye(d)
#         self.trans_g = np.zeros(d)
#         self.w_ng = np.zeros((0,d))
# 
#     def transform_points(self, x_ma):
#         y_ng = tps_eval(x_ma, self.lin_ag, self.trans_g, self.w_ng, self.x_na)
#         return y_ng
# 
#     def compute_jacobian(self, x_ma):
#         grad_mga = tps_grad(x_ma, self.lin_ag, self.trans_g, self.w_ng, self.x_na)
#         return grad_mga
# 
# def fit_ThinPlateSpline(x_na, y_ng, bend_coef=.1, rot_coef = 1e-5, wt_n=None):
#     """
#     x_na: source cloud
#     y_nd: target cloud
#     smoothing: penalize non-affine part
#     angular_spring: penalize rotation
#     wt_n: weight the points        
#     """
#     f = ThinPlateSpline()
#     f.lin_ag, f.trans_g, f.w_ng = tps_fit3(x_na, y_ng, bend_coef, rot_coef, wt_n)
#     f.x_na = x_na
#     return f        


# Path: lfd/rapprentice/tps_registration.py
import numpy as np
import scipy.spatial.distance as ssd
import tps
import knot_classifier
import IPython as ipy
    import argparse, h5py, os
    import matplotlib.pyplot as plt
    import registration
    import time
from lfd.rapprentice import registration, math_utils
from lfd.rapprentice.registration import loglinspace, ThinPlateSpline, fit_ThinPlateSpline
    from rapprentice import clouds, plotting_plt

#!/usr/bin/env python

from __future__ import division


N_ITER_CHEAP = 14
EM_ITER_CHEAP = 1

def rgb2lab(rgb):
    return xyz2lab(rgb2xyz(rgb))


def rgb2xyz(rgb):