#NEWLINE# def tps_rpm_bij( x_nd , y_md , f_solver_factory = None , g_solver_factory = None , n_iter = settings . N_ITER , em_iter = settings . EM_ITER , reg_init = settings . REG [ 0 ] , reg_final = settings . REG [ 1 ] , rad_init = settings . RAD [ 0 ] , rad_final = settings . RAD [ 1 ] , rot_reg = settings . ROT_REG , outlierprior = settings . OUTLIER_PRIOR , outlierfrac = settings . OURLIER_FRAC , prior_prob_nm = None , callback = None ) : #NEWLINE# #INDENT# ( _ , d ) = x_nd . shape #NEWLINE# regs = loglinspace ( reg_init , reg_final , n_iter ) #NEWLINE# rads = loglinspace ( rad_init , rad_final , n_iter ) #NEWLINE# f = ThinPlateSpline ( d ) #NEWLINE# scale = ( ( np . max ( y_md , axis = 0 ) - np . min ( y_md , axis = 0 ) ) / ( np . max ( x_nd , axis = 0 ) - np . min ( x_nd , axis = 0 ) ) ) #NEWLINE# f . lin_ag = np . diag ( scale ) #NEWLINE# f . trans_g = ( np . median ( y_md , axis = 0 ) - ( np . median ( x_nd , axis = 0 ) * scale ) ) #NEWLINE# g = ThinPlateSpline ( d ) #NEWLINE# g . lin_ag = np . diag ( ( 1.0 / scale ) ) #NEWLINE# g . trans_g = ( - np . diag ( ( 1.0 / scale ) ) . dot ( f . trans_g ) ) #NEWLINE# ( n , _ ) = x_nd . shape #NEWLINE# ( m , _ ) = y_md . shape #NEWLINE# x_priors = ( np . ones ( n ) * outlierprior ) #NEWLINE# y_priors = ( np . ones ( m ) * outlierprior ) #NEWLINE# if ( f_solver_factory is None ) : #NEWLINE# #INDENT# fsolve = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# fsolve = f_solver_factory . get_solver ( x_nd , rot_reg ) #NEWLINE# #UNINDENT# if ( g_solver_factory is None ) : #NEWLINE# #INDENT# gsolve = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# gsolve = g_solver_factory . get_solver ( y_md , rot_reg ) #NEWLINE# #UNINDENT# for ( i , ( reg , rad ) ) in enumerate ( zip ( regs , rads ) ) : #NEWLINE# #INDENT# for i_em in range ( em_iter ) : #NEWLINE# #INDENT# xwarped_nd = f . transform_points ( x_nd ) #NEWLINE# ywarped_md = g . transform_points ( y_md ) #NEWLINE# fwddist_nm = ssd . cdist ( xwarped_nd , y_md , 'sqeuclidean' ) #NEWLINE# invdist_nm = ssd . cdist ( x_nd , ywarped_md , 'sqeuclidean' ) #NEWLINE# prob_nm = np . exp ( ( ( - ( ( ( 1 / n ) * fwddist_nm ) + ( ( 1 / m ) * invdist_nm ) ) ) / ( ( 2 * rad ) * ( ( 1 / n ) + ( 1 / m ) ) ) ) ) #NEWLINE# if ( prior_prob_nm != None ) : #NEWLINE# #INDENT# prob_nm *= prior_prob_nm #NEWLINE# #UNINDENT# ( corr_nm , _ , _ ) = balance_matrix3 ( prob_nm , 10 , x_priors , y_priors , outlierfrac ) #NEWLINE# ( xtarg_nd , wt_n ) = prepare_fit_ThinPlateSpline ( x_nd , y_md , corr_nm ) #NEWLINE# ( ytarg_md , wt_m ) = prepare_fit_ThinPlateSpline ( x_nd , y_md , corr_nm , fwd = False ) #NEWLINE# if ( fsolve is None ) : #NEWLINE# #INDENT# f = ThinPlateSpline . create_from_optimization ( x_nd , xtarg_nd , reg , rot_reg , wt_n ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# fsolve . solve ( wt_n , xtarg_nd , reg , f ) #NEWLINE# #UNINDENT# if ( gsolve is None ) : #NEWLINE# #INDENT# g = ThinPlateSpline . create_from_optimization ( y_md , ytarg_md , reg , rot_reg , wt_m ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# gsolve . solve ( wt_m , ytarg_md , reg , g ) #NEWLINE# #UNINDENT# if callback : #NEWLINE# #INDENT# callback ( i , i_em , x_nd , y_md , xtarg_nd , corr_nm , wt_n , f , g , corr_nm , rad ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return ( f , g , corr_nm )