<s> import numpy as np <EOL> from menpo . shape import PointCloud , TriMesh <EOL> from menpo . transform import TransformChain , Translation , Scale <EOL> from menpo . transform . thinplatesplines import ThinPlateSplines <EOL> from menpo . transform . piecewiseaffine import PiecewiseAffine <EOL> def chain_tps_before_tps_test ( ) : <EOL> a = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> tps_one = ThinPlateSplines ( a , b ) <EOL> tps_two = ThinPlateSplines ( b , a ) <EOL> chain = tps_one . compose_before ( tps_two ) <EOL> assert ( isinstance ( chain , TransformChain ) ) <EOL> points = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> chain_res = chain . apply ( points ) <EOL> manual_res = tps_two . apply ( tps_one . apply ( points ) ) <EOL> assert ( np . all ( chain_res . points == manual_res . points ) ) <EOL> def chain_tps_after_tps_test ( ) : <EOL> a = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> tps_one = ThinPlateSplines ( a , b ) <EOL> tps_two = ThinPlateSplines ( b , a ) <EOL> chain = tps_one . compose_after ( tps_two ) <EOL> assert ( isinstance ( chain , TransformChain ) ) <EOL> points = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> chain_res = chain . apply ( points ) <EOL> manual_res = tps_one . apply ( tps_two . apply ( points ) ) <EOL> assert ( np . all ( chain_res . points == manual_res . points ) ) <EOL> def chain_pwa_before_tps_test ( ) : <EOL> a_tm = TriMesh ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> pwa = PiecewiseAffine ( a_tm , b ) <EOL> tps = ThinPlateSplines ( b , a_tm ) <EOL> chain = pwa . compose_before ( tps ) <EOL> assert ( isinstance ( chain , TransformChain ) ) <EOL> def chain_pwa_after_tps_test ( ) : <EOL> a_tm = TriMesh ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> pwa = PiecewiseAffine ( a_tm , b ) <EOL> tps = ThinPlateSplines ( b , a_tm ) <EOL> chain = pwa . compose_after ( tps ) <EOL> assert ( isinstance ( chain , TransformChain ) ) <EOL> def chain_tps_before_pwa_test ( ) : <EOL> a_tm = TriMesh ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> pwa = PiecewiseAffine ( a_tm , b ) <EOL> tps = ThinPlateSplines ( b , a_tm ) <EOL> chain = tps . compose_before ( pwa ) <EOL> assert ( isinstance ( chain , TransformChain ) ) <EOL> def chain_tps_after_pwa_test ( ) : <EOL> a_tm = TriMesh ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> pwa = PiecewiseAffine ( a_tm , b ) <EOL> tps = ThinPlateSplines ( b , a_tm ) <EOL> chain = tps . compose_after ( pwa ) <EOL> assert ( isinstance ( chain , TransformChain ) ) <EOL> def compose_tps_after_translation_test ( ) : <EOL> a = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> t = Translation ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> tps = ThinPlateSplines ( a , b ) <EOL> chain = tps . compose_after ( t ) <EOL> assert ( isinstance ( chain , TransformChain ) ) <EOL> def manual_no_op_chain_test ( ) : <EOL> points = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> t = Translation ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> chain = TransformChain ( [ t , t . pseudoinverse ( ) ] ) <EOL> points_applied = chain . apply ( points ) <EOL> assert ( np . allclose ( points_applied . points , points . points ) ) <EOL> def chain_compose_before_tps_test ( ) : <EOL> a = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> tps = ThinPlateSplines ( a , b ) <EOL> t = Translation ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> s = Scale ( [ <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> chain = TransformChain ( [ t , s ] ) <EOL> chain_mod = chain . compose_before ( tps ) <EOL> points = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> manual_res = tps . apply ( s . apply ( t . apply ( points ) ) ) <EOL> chain_res = chain_mod . apply ( points ) <EOL> assert ( np . all ( manual_res . points == chain_res . points ) ) <EOL> def chain_compose_after_tps_test ( ) : <EOL> a = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> tps = ThinPlateSplines ( a , b ) <EOL> t = Translation ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> s = Scale ( [ <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> chain = TransformChain ( [ t , s ] ) <EOL> chain_mod = chain . compose_after ( tps ) <EOL> points = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> manual_res = s . apply ( t . apply ( tps . apply ( points ) ) ) <EOL> chain_res = chain_mod . apply ( points ) <EOL> assert ( np . all ( manual_res . points == chain_res . points ) ) <EOL> def chain_compose_before_inplace_tps_test ( ) : <EOL> a = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> tps = ThinPlateSplines ( a , b ) <EOL> t = Translation ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> s = Scale ( [ <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> chain = TransformChain ( [ t , s ] ) <EOL> chain . compose_before_inplace ( tps ) <EOL> points = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> manual_res = tps . apply ( s . apply ( t . apply ( points ) ) ) <EOL> chain_res = chain . apply ( points ) <EOL> assert ( np . all ( manual_res . points == chain_res . points ) ) <EOL> def chain_compose_after_inplace_tps_test ( ) : <EOL> a = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> tps = ThinPlateSplines ( a , b ) <EOL> t = Translation ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> s = Scale ( [ <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> chain = TransformChain ( [ t , s ] ) <EOL> chain . compose_after_inplace ( tps ) <EOL> points = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> manual_res = s . apply ( t . apply ( tps . apply ( points ) ) ) <EOL> chain_res = chain . apply ( points ) <EOL> assert ( np . all ( manual_res . points == chain_res . points ) ) <EOL> def chain_compose_after_inplace_chain_test ( ) : <EOL> a = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> b = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> t = Translation ( [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> s = Scale ( [ <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> chain_1 = TransformChain ( [ t , s ] ) <EOL> chain_2 = TransformChain ( [ s . pseudoinverse ( ) , t . pseudoinverse ( ) ] ) <EOL> chain_1 . compose_before_inplace ( chain_2 ) <EOL> points = PointCloud ( np . random . random ( [ <NUM_LIT:10> , <NUM_LIT:2> ] ) ) <EOL> chain_res = chain_1 . apply ( points ) <EOL> assert ( np . allclose ( points . points , chain_res . points ) ) </s>
