def test_inner_product_matrix_vector(self, default_point_type): euclidean = Euclidean(3) minkowski = Minkowski(3) space = ProductManifold(manifolds=[euclidean, minkowski]) point = space.random_point(1) expected = gs.eye(6) expected[3, 3] = -1 result = space.metric.metric_matrix(point) self.assertAllClose(result, expected)
def setUp(self): gs.random.seed(1234) self.space_matrix = ProductManifold( manifolds=[Hypersphere(dim=2), Hyperboloid(dim=2)], default_point_type='matrix') self.space_vector = ProductManifold( manifolds=[Hypersphere(dim=2), Hyperboloid(dim=5)], default_point_type='vector')
def test_inner_product_matrix_matrix(self): euclidean = Euclidean(3) minkowski = Minkowski(3) space = ProductManifold(manifolds=[euclidean, minkowski], default_point_type='matrix') point = space.random_uniform(1) result = space.metric.inner_product_matrix(point) expected = gs.eye(6) expected[3, 3] = -1 self.assertAllClose(result, expected)
def setup_method(self): gs.random.seed(1234) self.space_matrix = ProductManifold( manifolds=[Hypersphere(dim=2), Hyperboloid(dim=2)], default_point_type="matrix", ) self.space_vector = ProductManifold( manifolds=[Hypersphere(dim=2), Hyperboloid(dim=3)], default_point_type="vector", )
def test_inner_product_matrix_matrix(self): space = ProductManifold(manifolds=[ Hypersphere(dimension=2).embedding_manifold, Hyperbolic(dimension=2).embedding_manifold ], default_point_type='matrix') point = space.random_uniform(1) result = space.metric.inner_product_matrix(point) expected = gs.identity(6) expected[3, 3] = -1 self.assertAllClose(result, expected)
def setUp(self): gs.random.seed(1234) self.sphere = Hypersphere(dimension=2) self.hyperbolic = Hyperbolic(dimension=5) self.space = ProductManifold(manifolds=[self.sphere, self.hyperbolic])
def test_dist_log_then_exp_norm(self, manifolds, default_point_type, n_samples, einsum_str, expected): space = ProductManifold(manifolds=manifolds, default_point_type=default_point_type) point = space.random_point(n_samples) base_point = space.random_point(n_samples) logs = space.metric.log(point, base_point) normalized_logs = gs.einsum( einsum_str, 1.0 / space.metric.norm(logs, base_point), logs, ) point = space.metric.exp(normalized_logs, base_point) result = space.metric.dist(point, base_point) self.assertAllClose(result, expected)
def regularize_test_data(self): smoke_data = [ dict( manifold=smoke_manifolds_1, default_point_type="vector", point=ProductManifold( smoke_manifolds_1, default_point_type="vector").random_point(5), ), dict( manifold=smoke_manifolds_1, default_point_type="matrix", point=ProductManifold( smoke_manifolds_1, default_point_type="matrix").random_point(5), ), ] return self.generate_tests(smoke_data)
def inner_product_matrix_test_data(self): smoke_data = [ dict( metric=smoke_metrics_2, default_point_type="vector", point=ProductManifold( smoke_manifolds_1, default_point_type="vector").random_point(5), base_point=ProductManifold( smoke_manifolds_1, default_point_type="vector").random_point(5), ), dict( manifold=smoke_metrics_2, default_point_type="matrix", point=ProductManifold( smoke_manifolds_2, default_point_type="matrix").random_point(5), base_point=ProductManifold( smoke_manifolds_2, default_point_type="matrix").random_point(5), ), ] return self.generate_tests(smoke_data)
class TestProductManifold(geomstats.tests.TestCase): def setup_method(self): gs.random.seed(1234) self.space_matrix = ProductManifold( manifolds=[Hypersphere(dim=2), Hyperboloid(dim=2)], default_point_type="matrix", ) self.space_vector = ProductManifold( manifolds=[Hypersphere(dim=2), Hyperboloid(dim=3)], default_point_type="vector", ) def test_dimension(self): expected = 5 result = self.space_vector.dim self.assertAllClose(result, expected) def test_random_and_belongs_matrix(self): n_samples = 1 data = self.space_matrix.random_point(n_samples) result = self.space_matrix.belongs(data) self.assertTrue(result) n_samples = 5 data = self.space_matrix.random_point(n_samples) result = self.space_matrix.belongs(data) expected = gs.array([True] * n_samples) self.assertAllClose(result, expected) def test_random_and_belongs_vector(self): n_samples = 5 data = self.space_vector.random_point(n_samples) result = self.space_vector.belongs(data) expected = gs.array([True] * n_samples) self.assertAllClose(result, expected) @geomstats.tests.np_and_autograd_only def test_exp_log_vector(self): n_samples = 5 expected = self.space_vector.random_point(n_samples) base_point = self.space_vector.random_point(n_samples) logs = self.space_vector.metric.log(expected, base_point) result = self.space_vector.metric.exp(logs, base_point) self.assertAllClose(result, expected) @geomstats.tests.np_autograd_and_torch_only def test_exp_log_matrix(self): n_samples = 5 expected = self.space_matrix.random_point(n_samples) base_point = self.space_matrix.random_point(n_samples) logs = self.space_matrix.metric.log(expected, base_point) result = self.space_matrix.metric.exp(logs, base_point) self.assertAllClose(result, expected, atol=1e-5) @geomstats.tests.np_and_autograd_only def test_dist_log_exp_norm_vector(self): n_samples = 5 point = self.space_vector.random_point(n_samples) base_point = self.space_vector.random_point(n_samples) logs = self.space_vector.metric.log(point, base_point) normalized_logs = gs.einsum( "..., ...j->...j", 1.0 / self.space_vector.metric.norm(logs, base_point), logs, ) point = self.space_vector.metric.exp(normalized_logs, base_point) result = self.space_vector.metric.dist(point, base_point) expected = gs.ones(n_samples) self.assertAllClose(result, expected) @geomstats.tests.np_autograd_and_torch_only def test_dist_log_exp_norm_matrix(self): n_samples = 10 point = self.space_matrix.random_point(n_samples) base_point = self.space_matrix.random_point(n_samples) logs = self.space_matrix.metric.log(point, base_point) normalized_logs = gs.einsum( "..., ...jl->...jl", 1.0 / self.space_matrix.metric.norm(logs, base_point), logs, ) point = self.space_matrix.metric.exp(normalized_logs, base_point) result = self.space_matrix.metric.dist(point, base_point) expected = gs.ones((n_samples,)) self.assertAllClose(result, expected) @geomstats.tests.np_autograd_and_torch_only def test_inner_product_matrix_matrix(self): euclidean = Euclidean(3) minkowski = Minkowski(3) space = ProductManifold( manifolds=[euclidean, minkowski], default_point_type="matrix" ) point = space.random_point(1) result = space.metric.metric_matrix(point) expected = gs.eye(6) expected[3, 3] = -1 self.assertAllClose(result, expected) @geomstats.tests.np_autograd_and_torch_only def test_inner_product_matrix_vector(self): euclidean = Euclidean(3) minkowski = Minkowski(3) space = ProductManifold( manifolds=[euclidean, minkowski], default_point_type="vector" ) point = space.random_point(1) expected = gs.eye(6) expected[3, 3] = -1 result = space.metric.metric_matrix(point) self.assertAllClose(result, expected) def test_regularize_vector(self): expected = self.space_vector.random_point(5) result = self.space_vector.regularize(expected) self.assertAllClose(result, expected) def test_regularize_matrix(self): expected = self.space_matrix.random_point(5) result = self.space_matrix.regularize(expected) self.assertAllClose(result, expected) @geomstats.tests.np_autograd_and_torch_only def test_inner_product_matrix(self): n_samples = 1 expected = self.space_matrix.random_point(n_samples) base_point = self.space_matrix.random_point(n_samples) logs = self.space_matrix.metric.log(expected, base_point) result = self.space_matrix.metric.inner_product(logs, logs) expected = self.space_matrix.metric.squared_dist(base_point, expected) self.assertAllClose(result, expected) n_samples = 5 expected = self.space_matrix.random_point(n_samples) base_point = self.space_matrix.random_point(n_samples) logs = self.space_matrix.metric.log(expected, base_point) result = self.space_matrix.metric.inner_product(logs, logs) expected = self.space_matrix.metric.squared_dist(base_point, expected) self.assertAllClose(result, expected) @geomstats.tests.np_autograd_and_torch_only def test_projection_and_belongs_vector(self): space = self.space_vector shape = (2, space.dim + 2) result = helper.test_projection_and_belongs(space, shape, atol=gs.atol * 100) for res in result: self.assertTrue(res) @geomstats.tests.np_autograd_and_torch_only def test_projection_and_belongs_matrix(self): space = self.space_matrix shape = (2, len(space.manifolds), space.manifolds[0].dim + 1) result = helper.test_projection_and_belongs(space, shape, atol=gs.atol * 100) for res in result: self.assertTrue(res) def test_to_tangent_is_tangent_vector(self): space = self.space_vector result = helper.test_to_tangent_is_tangent(space, atol=gs.atol) for res in result: self.assertTrue(res) def test_to_tangent_is_tangent_matrix(self): space = self.space_matrix result = helper.test_to_tangent_is_tangent(space, atol=gs.atol) for res in result: self.assertTrue(res)
class TestProductManifold(geomstats.tests.TestCase): def setUp(self): gs.random.seed(1234) self.space_matrix = ProductManifold( manifolds=[Hypersphere(dim=2), Hyperboloid(dim=2)], default_point_type='matrix') self.space_vector = ProductManifold( manifolds=[Hypersphere(dim=2), Hyperboloid(dim=5)], default_point_type='vector') def test_dimension(self): expected = 7 result = self.space_vector.dim self.assertAllClose(result, expected) def test_random_and_belongs_matrix(self): n_samples = 1 data = self.space_matrix.random_point(n_samples) result = self.space_matrix.belongs(data) self.assertTrue(result) n_samples = 5 data = self.space_matrix.random_point(n_samples) result = self.space_matrix.belongs(data) expected = gs.array([True] * n_samples) self.assertAllClose(result, expected) def test_random_and_belongs_vector(self): n_samples = 5 data = self.space_vector.random_point(n_samples) result = self.space_vector.belongs(data) expected = gs.array([True] * n_samples) self.assertAllClose(result, expected) @geomstats.tests.np_only def test_exp_log_vector(self): n_samples = 5 expected = self.space_vector.random_point(n_samples) base_point = self.space_vector.random_point(n_samples) logs = self.space_vector.metric.log(expected, base_point) result = self.space_vector.metric.exp(logs, base_point) self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_exp_log_matrix(self): n_samples = 5 expected = self.space_matrix.random_point(n_samples) base_point = self.space_matrix.random_point(n_samples) logs = self.space_matrix.metric.log(expected, base_point) result = self.space_matrix.metric.exp(logs, base_point) self.assertAllClose(result, expected, atol=1e-5) @geomstats.tests.np_only def test_dist_log_exp_norm_vector(self): n_samples = 5 point = self.space_vector.random_point(n_samples) base_point = self.space_vector.random_point(n_samples) logs = self.space_vector.metric.log(point, base_point) normalized_logs = gs.einsum( '..., ...j->...j', 1. / self.space_vector.metric.norm(logs, base_point), logs) point = self.space_vector.metric.exp(normalized_logs, base_point) result = self.space_vector.metric.dist(point, base_point) expected = gs.ones(n_samples) self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_dist_log_exp_norm_matrix(self): n_samples = 10 point = self.space_matrix.random_point(n_samples) base_point = self.space_matrix.random_point(n_samples) logs = self.space_matrix.metric.log(point, base_point) normalized_logs = gs.einsum( '..., ...jl->...jl', 1. / self.space_matrix.metric.norm(logs, base_point), logs) point = self.space_matrix.metric.exp(normalized_logs, base_point) result = self.space_matrix.metric.dist(point, base_point) expected = gs.ones((n_samples, )) self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_inner_product_matrix_matrix(self): euclidean = Euclidean(3) minkowski = Minkowski(3) space = ProductManifold(manifolds=[euclidean, minkowski], default_point_type='matrix') point = space.random_point(1) result = space.metric.metric_matrix(point) expected = gs.eye(6) expected[3, 3] = -1 self.assertAllClose(result, expected) @geomstats.tests.np_only def test_inner_product_matrix_vector(self): euclidean = Euclidean(3) minkowski = Minkowski(3) space = ProductManifold(manifolds=[euclidean, minkowski], default_point_type='vector') point = space.random_point(1) expected = gs.eye(6) expected[3, 3] = -1 result = space.metric.metric_matrix(point) self.assertAllClose(result, expected) @geomstats.tests.np_only def test_regularize_vector(self): expected = self.space_vector.random_point(5) result = self.space_vector.regularize(expected) self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_regularize_matrix(self): expected = self.space_matrix.random_point(5) result = self.space_matrix.regularize(expected) self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_inner_product_matrix(self): n_samples = 1 expected = self.space_matrix.random_point(n_samples) base_point = self.space_matrix.random_point(n_samples) logs = self.space_matrix.metric.log(expected, base_point) result = self.space_matrix.metric.inner_product(logs, logs) expected = self.space_matrix.metric.squared_dist(base_point, expected) self.assertAllClose(result, expected) n_samples = 5 expected = self.space_matrix.random_point(n_samples) base_point = self.space_matrix.random_point(n_samples) logs = self.space_matrix.metric.log(expected, base_point) result = self.space_matrix.metric.inner_product(logs, logs) expected = self.space_matrix.metric.squared_dist(base_point, expected) self.assertAllClose(result, expected)
class TestProductManifoldMethods(geomstats.tests.TestCase): def setUp(self): gs.random.seed(1234) self.space_matrix = ProductManifold( manifolds=[Hypersphere(dimension=2), Hyperbolic(dimension=2)], default_point_type='matrix') self.space_vector = ProductManifold( manifolds=[Hypersphere(dimension=2), Hyperbolic(dimension=5)], default_point_type='vector') def test_dimension(self): expected = 7 result = self.space_vector.dimension self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_random_and_belongs_matrix(self): n_samples = 5 data = self.space_matrix.random_uniform(n_samples) result = self.space_matrix.belongs(data) expected = gs.array([[True] * n_samples]).transpose(1, 0) self.assertAllClose(result, expected) @geomstats.tests.np_only def test_random_and_belongs_vector(self): n_samples = 5 data = self.space_vector.random_uniform(n_samples) result = self.space_vector.belongs(data) expected = gs.array([[True] * n_samples]).transpose(1, 0) self.assertAllClose(result, expected) @geomstats.tests.np_only def test_exp_log_vector(self): n_samples = 5 expected = self.space_vector.random_uniform(n_samples) base_point = self.space_vector.random_uniform(n_samples) logs = self.space_vector.metric.log(expected, base_point) result = self.space_vector.metric.exp(logs, base_point) self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_exp_log_matrix(self): n_samples = 5 expected = self.space_matrix.random_uniform(n_samples) base_point = self.space_matrix.random_uniform(n_samples) logs = self.space_matrix.metric.log(expected, base_point) result = self.space_matrix.metric.exp(logs, base_point) self.assertAllClose(result, expected) @geomstats.tests.np_only def test_dist_vector(self): n_samples = 5 point = self.space_vector.random_uniform(n_samples) base_point = self.space_vector.random_uniform(n_samples) logs = self.space_vector.metric.log(point, base_point) logs = gs.einsum('..., ...j->...j', 1. / self.space_vector.metric.norm(logs, base_point), logs) point = self.space_vector.metric.exp(logs, base_point) result = self.space_vector.metric.dist(point, base_point) expected = gs.ones(n_samples) self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_dist_matrix(self): n_samples = 5 point = self.space_matrix.random_uniform(n_samples) base_point = self.space_matrix.random_uniform(n_samples) logs = self.space_matrix.metric.log(point, base_point) logs = gs.einsum('..., ...j->...j', 1. / self.space_matrix.metric.norm(logs, base_point), logs) point = self.space_matrix.metric.exp(logs, base_point) result = self.space_matrix.metric.dist(point, base_point) expected = gs.ones((n_samples, 1)) self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_inner_product_matrix_matrix(self): space = ProductManifold(manifolds=[ Hypersphere(dimension=2).embedding_manifold, Hyperbolic(dimension=2).embedding_manifold ], default_point_type='matrix') point = space.random_uniform(1) result = space.metric.inner_product_matrix(point) expected = gs.identity(6) expected[3, 3] = -1 self.assertAllClose(result, expected) @geomstats.tests.np_only def test_inner_product_matrix_vector(self): space = ProductManifold(manifolds=[ Hypersphere(dimension=2).embedding_manifold, Hyperbolic(dimension=2).embedding_manifold ], default_point_type='vector') point = space.random_uniform(1) expected = gs.identity(6) expected[3, 3] = -1 result = space.metric.inner_product_matrix(point) self.assertAllClose(result, expected) @geomstats.tests.np_only def test_regularize_vector(self): expected = self.space_vector.random_uniform(5) result = self.space_vector.regularize(expected) self.assertAllClose(result, expected) @geomstats.tests.np_and_pytorch_only def test_regularize_matrix(self): expected = self.space_matrix.random_uniform(5) result = self.space_matrix.regularize(expected) self.assertAllClose(result, expected)
class ProductRiemannianMetricTestData(_RiemannianMetricTestData): n_list = random.sample(range(2, 3), 1) default_point_list = ["vector", "matrix"] manifolds_list = [[Hypersphere(dim=n), Hyperboloid(dim=n)] for n in n_list] metrics_list = [[Hypersphere(dim=n).metric, Hyperboloid(dim=n).metric] for n in n_list] metric_args_list = list(zip(metrics_list, default_point_list)) shape_list = [(n + 1, n + 1) if default_point == "matrix" else (2 * (n + 1), ) for n, default_point in zip(n_list, default_point_list)] space_list = [ ProductManifold(manifolds, None, default_point_type) for manifolds, default_point_type in zip(manifolds_list, default_point_list) ] n_points_list = random.sample(range(2, 5), 1) n_tangent_vecs_list = random.sample(range(2, 5), 1) n_points_a_list = random.sample(range(2, 5), 1) n_points_b_list = [1] alpha_list = [1] * 1 n_rungs_list = [1] * 1 scheme_list = ["pole"] * 1 def inner_product_matrix_test_data(self): smoke_data = [ dict( metric=smoke_metrics_2, default_point_type="vector", point=ProductManifold( smoke_manifolds_1, default_point_type="vector").random_point(5), base_point=ProductManifold( smoke_manifolds_1, default_point_type="vector").random_point(5), ), dict( manifold=smoke_metrics_2, default_point_type="matrix", point=ProductManifold( smoke_manifolds_2, default_point_type="matrix").random_point(5), base_point=ProductManifold( smoke_manifolds_2, default_point_type="matrix").random_point(5), ), ] return self.generate_tests(smoke_data) def exp_shape_test_data(self): return self._exp_shape_test_data(self.metric_args_list, self.space_list, self.shape_list) def log_shape_test_data(self): return self._log_shape_test_data(self.metric_args_list, self.space_list) def squared_dist_is_symmetric_test_data(self): return self._squared_dist_is_symmetric_test_data( self.metric_args_list, self.space_list, self.n_points_a_list, self.n_points_b_list, atol=gs.atol * 1000, ) def exp_belongs_test_data(self): return self._exp_belongs_test_data( self.metric_args_list, self.space_list, self.shape_list, self.n_tangent_vecs_list, belongs_atol=gs.atol * 1000, ) def log_is_tangent_test_data(self): return self._log_is_tangent_test_data( self.metric_args_list, self.space_list, self.n_points_list, is_tangent_atol=1e-1, ) def geodesic_ivp_belongs_test_data(self): return self._geodesic_ivp_belongs_test_data( self.metric_args_list, self.space_list, self.shape_list, self.n_points_list, belongs_atol=gs.atol * 1000, ) def geodesic_bvp_belongs_test_data(self): return self._geodesic_bvp_belongs_test_data( self.metric_args_list, self.space_list, self.n_points_list, belongs_atol=gs.atol * 1000, ) def log_then_exp_test_data(self): return self._log_then_exp_test_data( self.metric_args_list, self.space_list, self.n_points_list, rtol=gs.rtol * 1000, atol=1e-1, ) def exp_then_log_test_data(self): return self._exp_then_log_test_data( self.metric_args_list, self.space_list, self.shape_list, self.n_tangent_vecs_list, amplitude=10, rtol=gs.rtol * 1000, atol=1e-1, ) def exp_ladder_parallel_transport_test_data(self): return self._exp_ladder_parallel_transport_test_data( self.metric_args_list, self.space_list, self.shape_list, self.n_tangent_vecs_list, self.n_rungs_list, self.alpha_list, self.scheme_list, ) def exp_geodesic_ivp_test_data(self): return self._exp_geodesic_ivp_test_data( self.metric_args_list, self.space_list, self.shape_list, self.n_tangent_vecs_list, self.n_points_list, rtol=gs.rtol * 100000, atol=gs.atol * 100000, ) def parallel_transport_ivp_is_isometry_test_data(self): return self._parallel_transport_ivp_is_isometry_test_data( self.metric_args_list, self.space_list, self.shape_list, self.n_tangent_vecs_list, is_tangent_atol=gs.atol * 1000, atol=gs.atol * 1000, ) def parallel_transport_bvp_is_isometry_test_data(self): return self._parallel_transport_bvp_is_isometry_test_data( self.metric_args_list, self.space_list, self.shape_list, self.n_tangent_vecs_list, is_tangent_atol=gs.atol * 1000, atol=gs.atol * 1000, ) def dist_is_symmetric_test_data(self): return self._dist_is_symmetric_test_data( self.metric_args_list, self.space_list, self.n_points_a_list, self.n_points_b_list, ) def dist_is_positive_test_data(self): return self._dist_is_positive_test_data( self.metric_args_list, self.space_list, self.n_points_a_list, self.n_points_b_list, ) def squared_dist_is_positive_test_data(self): return self._squared_dist_is_positive_test_data( self.metric_args_list, self.space_list, self.n_points_a_list, self.n_points_b_list, ) def dist_is_norm_of_log_test_data(self): return self._dist_is_norm_of_log_test_data( self.metric_args_list, self.space_list, self.n_points_a_list, self.n_points_b_list, ) def dist_point_to_itself_is_zero_test_data(self): return self._dist_point_to_itself_is_zero_test_data( self.metric_args_list, self.space_list, self.n_points_list) def inner_product_is_symmetric_test_data(self): return self._inner_product_is_symmetric_test_data( self.metric_args_list, self.space_list, self.shape_list, self.n_tangent_vecs_list, ) def inner_product_matrix_vector_test_data(self): random_data = [ dict(default_point_type="matrix"), dict(default_point_type="vector"), ] return self.generate_tests([], random_data) def dist_log_then_exp_norm_test_data(self): smoke_data = [ dict( space=smoke_manifolds_1, default_point_type="vector", n_samples=10, einsum_str="..., ...j->...j", expected=gs.ones(10), ), dict( space=smoke_manifolds_1, default_point_type="matrix", n_samples=10, einsum_str="..., ...jl->...jl", expected=gs.ones(10, ), ), ] return self.generate_tests(smoke_data)