Package geometry :: Package manifolds :: Package tests :: Module manifold_tests
[hide private]
[frames] | no frames]

Source Code for Module geometry.manifolds.tests.manifold_tests

  1  from geometry import (tran1, tran2, tran3, so2, so3, se2, se3, S1, SE2, SE3, 
  2                        S2, 
  3      Tran1, Tran2, Tran3, T1, T2, T3, R1, R2, SO2, SO3, R3, RandomManifold, 
  4      assert_allclose) 
  5  from nose.plugins.attrib import attr 
  6  import itertools 
  7  import numpy as np 
8 9 10 -def check_geodesic_consistency(M, a, b, divisions=5):
11 ''' 12 Check that there is consistency in the geodesics. 13 14 This is a test that 15 16 x(t) = geodesic( a, b, t) 17 18 interpolates between a and b, checking 19 20 d(a, x(t)) + d(x(t), b) = d(a,b) 21 22 ''' 23 check_geodesic_consistency.description = ( 24 '%s: Checking geodesic consistency. ' 25 '(a: %s, b: %s)' 26 % (M, M.friendly(a), M.friendly(b))) 27 28 d = M.distance(a, b) 29 30 ts = np.linspace(0, 1, divisions) 31 for t in ts: 32 c = M.geodesic(a, b, t) 33 M.belongs(c) 34 d1 = M.distance(a, c) 35 d2 = M.distance(c, b) 36 37 assert_allclose(d1 + d2, d, atol=1e-7)
38
39 40 -def check_logmap1(M, a, b):
41 ''' This is a test that: 42 43 Exp_a( Log_a(b) ) = b 44 45 ''' 46 check_logmap1.description = ( 47 '%s: Checking that logmap/expmap work. ' 48 '(a: %s, b: %s)' 49 % (M, M.friendly(a), M.friendly(b))) 50 51 bv = M.logmap(a, b) 52 b2 = M.expmap(bv) 53 assert_allclose(M.distance(b, b2), 0, atol=1e-7)
54
55 56 -def check_logmap3(M, a, b):
57 check_logmap3.description = ( 58 '%s: Checking that distance is consistent with logmap/expmap ' 59 '(a: %s, b: %s)' 60 % (M, M.friendly(a), M.friendly(b))) 61 62 d = M.distance(a, b) 63 base, vel = M.logmap(a, b) 64 ratios = [0.5, 0.3] 65 for ratio in ratios: 66 b2 = M.expmap((base, vel * ratio)) 67 d2 = M.distance(a, b2) 68 assert_allclose(d * ratio, d2, atol=1e-7)
69
70 71 -def check_friendly(M, a):
72 M.friendly(a)
73
74 75 -def check_interesting_point_in_manifold(M, p):
76 check_interesting_point_in_manifold.description = '%s: %s' % (M, p) 77 M.belongs(p)
78
79 80 -def check_enough_points(M):
81 points = list(M.interesting_points()) 82 if not points: 83 raise ValueError('No test points for %s.' % M)
84
85 86 -def check_manifold_suite(M, num_random=5):
87 88 yield check_enough_points, M 89 90 points = M.interesting_points() 91 92 if isinstance(M, RandomManifold): 93 for i in range(num_random): #@UnusedVariable 94 points.append(M.sample_uniform()) 95 96 one_point_functions = [check_friendly, check_interesting_point_in_manifold] 97 two_point_functions = [check_geodesic_consistency, check_logmap1, 98 check_logmap3] 99 100 for f in one_point_functions: 101 for a in points: 102 yield f, M, a 103 104 for f in two_point_functions: 105 for a, b in itertools.product(points, points): 106 yield f, M, a, b
107
108 109 -def manifolds_to_check():
110 # import warnings 111 # warnings.warn('Some checks disabled') 112 return [ 113 SO3, SO2, 114 R1, R2, R3, 115 T1, T2, T3, 116 Tran1, Tran2, Tran3, 117 SE2, SE3, 118 S1, S2, 119 se2, se3, 120 so2, so3, 121 tran1, tran2, tran3, 122 ]
123
124 125 -def test_dimensions():
126 x = [ 127 (SO3, 3), 128 (SO2, 1), 129 (R1, 1), 130 (R2, 2), 131 (R3, 3), 132 (T1, 1), (T2, 2), (T3, 3), 133 (Tran1, 1), (Tran2, 2), (Tran3, 3), 134 (SE2, 3), (SE3, 6), 135 (S1, 1), (S2, 2), 136 (se2, 3), (se3, 6), 137 (so2, 1), (so3, 3), 138 (tran1, 1), (tran2, 2), (tran3, 3) 139 ] 140 141 for M, dim in x: 142 actual = M.get_dimension() 143 msg = 'Expected %d for %s, got %d ' % (dim, M, actual) 144 assert actual == dim, msg
145
146 147 @attr('manifolds') 148 -def test_manifolds():
149 for M in manifolds_to_check(): 150 #print('Testing %s' % M) 151 for x in check_manifold_suite(M): 152 yield x
153 154 155 if __name__ == '__main__': 156 test_manifolds() 157