Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_random_uniform(self):
point = self.space.random_uniform()
self.assertAllClose(gs.shape(point), (self.dimension + 1,))
result = metric.dist(point_id, n_point_2)
self.assertAllClose(gs.shape(result), (n_samples,))
if geomstats.tests.tf_backend():
break
# n points 1 and identity
result = metric.dist(n_point_1, point_id)
self.assertAllClose(gs.shape(result), (n_samples,))
# one point 1 and n points 2
result = metric.dist(one_point_1, n_point_2)
self.assertAllClose(gs.shape(result), (n_samples,))
# n points 1 and one point 2
result = metric.dist(n_point_1, one_point_2)
self.assertAllClose(gs.shape(result), (n_samples,))
# n points 1 and n points 2
result = metric.dist(n_point_1, n_point_2)
self.assertAllClose(gs.shape(result), (n_samples,))
def test_log_vectorization(self):
n_samples = self.n_samples
for metric_type in self.metrics:
metric = self.metrics[metric_type]
one_point = self.group.random_uniform(n_samples=1)
one_base_point = self.group.random_uniform(n_samples=1)
n_point = self.group.random_uniform(n_samples=n_samples)
n_base_point = self.group.random_uniform(n_samples=n_samples)
# Test with the 1 base point, and several different points
result = metric.log(n_point, one_base_point)
self.assertAllClose(
gs.shape(result),
(n_samples, *self.group.get_point_type_shape()))
if geomstats.tests.tf_backend():
break
# Test with the several base point, and 1 point
result = metric.log(one_point, n_base_point)
self.assertAllClose(
gs.shape(result),
(n_samples, *self.group.get_point_type_shape()))
# Test with the same number n of base point and point
result = metric.log(n_point, n_base_point)
self.assertAllClose(
gs.shape(result),
(n_samples, *self.group.get_point_type_shape()))
def test_l2_metric_dist_vectorization(self):
"""Test the vectorization of dist."""
n_samples = self.n_discretized_curves
curves_ab = self.l2_metric_s2.geodesic(self.curve_a, self.curve_b)
curves_bc = self.l2_metric_s2.geodesic(self.curve_b, self.curve_c)
curves_ab = curves_ab(self.times)
curves_bc = curves_bc(self.times)
result = self.l2_metric_s2.dist(
curves_ab, curves_bc)
self.assertAllClose(gs.shape(result), (n_samples,))
one_point_2 = self.group.regularize(one_point_2)
n_point_1 = self.group.random_uniform(n_samples=n_samples)
n_point_2 = self.group.random_uniform(n_samples=n_samples)
n_point_1 = self.group.regularize(n_point_1)
n_point_2 = self.group.regularize(n_point_2)
# Identity and n points 2
result = metric.dist(point_id, n_point_2)
self.assertAllClose(gs.shape(result), (n_samples,))
if geomstats.tests.tf_backend():
break
# n points 1 and identity
result = metric.dist(n_point_1, point_id)
self.assertAllClose(gs.shape(result), (n_samples,))
# one point 1 and n points 2
result = metric.dist(one_point_1, n_point_2)
self.assertAllClose(gs.shape(result), (n_samples,))
# n points 1 and one point 2
result = metric.dist(n_point_1, one_point_2)
self.assertAllClose(gs.shape(result), (n_samples,))
# n points 1 and n points 2
result = metric.dist(n_point_1, n_point_2)
self.assertAllClose(gs.shape(result), (n_samples,))
"""Test of SPDMetricAffine.exp with power=1 and vectorization."""
n_samples = self.n_samples
one_base_point = self.space.random_uniform(n_samples=1)
n_base_point = self.space.random_uniform(n_samples=n_samples)
n_tangent_vec_same_base = self.space.random_tangent_vec_uniform(
n_samples=n_samples, base_point=one_base_point)
n_tangent_vec = self.space.random_tangent_vec_uniform(
n_samples=n_samples, base_point=n_base_point)
metric = self.metric_affine
# Test with the 1 base_point, and several different tangent_vecs
result = metric.exp(n_tangent_vec_same_base, one_base_point)
self.assertAllClose(
gs.shape(result), (n_samples, self.space.n, self.space.n))
# Test with the same number of base_points and tangent_vecs
result = metric.exp(n_tangent_vec, n_base_point)
self.assertAllClose(
gs.shape(result), (n_samples, self.space.n, self.space.n))
def test_inner_product_vectorization_single_sample(self):
one_point_a = gs.array([[0., 1.]])
one_point_b = gs.array([[2., 10.]])
result = self.metric.inner_product(one_point_a, one_point_b)
expected = gs.array([10.])
self.assertAllClose(gs.shape(result), (1,))
self.assertAllClose(result, expected)
one_point_a = gs.array([[0., 1.]])
one_point_b = gs.array([2., 10.])
result = self.metric.inner_product(one_point_a, one_point_b)
expected = gs.array([10.])
self.assertAllClose(gs.shape(result), (1,))
self.assertAllClose(result, expected)
one_point_a = gs.array([0., 1.])
one_point_b = gs.array([[2., 10.]])
result = self.metric.inner_product(one_point_a, one_point_b)
expected = gs.array([10.])
self.assertAllClose(gs.shape(result), (1,))
def test_load_cities(self):
"""Test that the cities coordinates belong to the sphere."""
sphere = Hypersphere(dim=2)
data, _ = data_utils.load_cities()
self.assertAllClose(gs.shape(data), (50, 3))
tokyo = data[0]
self.assertAllClose(
tokyo, gs.array([0.61993792, -0.52479018, 0.58332859]))
result = sphere.belongs(data)
self.assertTrue(gs.all(result))
def test_l2_metric_exp_vectorization(self):
"""Test the vectorization of exp."""
curves_ab = self.l2_metric_s2.geodesic(self.curve_a, self.curve_b)
curves_bc = self.l2_metric_s2.geodesic(self.curve_b, self.curve_c)
curves_ab = curves_ab(self.times)
curves_bc = curves_bc(self.times)
tangent_vecs = self.l2_metric_s2.log(
point=curves_bc, base_point=curves_ab)
result = self.l2_metric_s2.exp(
tangent_vec=tangent_vecs,
base_point=curves_ab)
self.assertAllClose(gs.shape(result), gs.shape(curves_ab))
In 3D, compute the skew-symmetric matrix,known as the cross-product of
a vector, associated to the vector `vec`.
In nD, fill a skew-symmetric matrix with the values of the vector.
Parameters
----------
vec : array-like, shape=[..., dim]
Vector.
Returns
-------
skew_mat : array-like, shape=[..., n, n]
Skew-symmetric matrix.
"""
n_vecs, vec_dim = gs.shape(vec)
if self.n == 2:
vec = gs.tile(vec, [1, 2])
vec = gs.reshape(vec, (n_vecs, 2))
id_skew = gs.array(
gs.tile([[[0., 1.], [-1., 0.]]], (n_vecs, 1, 1)))
skew_mat = gs.einsum(
'...ij,...i->...ij', gs.cast(id_skew, gs.float32), vec)
elif self.n == 3:
levi_civita_symbol = gs.tile([[
[[0., 0., 0.],
[0., 0., 1.],
[0., -1., 0.]],
[[0., 0., -1.],