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_log_left(self):
# Reference point is a translation (no rotational part)
# so that the jacobian of the left-translation of the Lie group
# is the 6x6 identity matrix
metric = self.metrics_all['left_canonical']
rot_vec_base_point = gs.array([0., 0., 0.])
translation_base_point = gs.array([4., 0., 0.])
transfo_base_point = gs.concatenate(
[rot_vec_base_point, translation_base_point], axis=0)
# Point is a translation (no rotational part)
# Expect the difference of the translation
# by the translation of the reference point
rot_vec = gs.array([0., 0., 0.])
translation = gs.array([-1., -1., -1.2])
point = gs.concatenate(
[rot_vec, translation], axis=0)
expected = gs.concatenate(
[gs.array([0., 0., 0.]), gs.array([-5., -1., -1.2])],
axis=0)
result = metric.log(base_point=transfo_base_point,
point=point)
self.assertAllClose(result, expected)
"""Run K-medoids on the sphere."""
n_samples = 50
dim = 2
n_clusters = 2
manifold = Hypersphere(dim)
metric = manifold.metric
# Generate data on north pole
cluster_1 = manifold.random_von_mises_fisher(kappa=50, n_samples=n_samples)
# Generate data on south pole
cluster_2 = manifold.random_von_mises_fisher(kappa=50, n_samples=n_samples)
for point in cluster_2:
point[2] = -point[2]
data = gs.concatenate((cluster_1, cluster_2), axis=0)
kmedoids = RiemannianKMedoids(metric=metric,
n_clusters=n_clusters)
centroids = kmedoids.fit(data)
labels = kmedoids.predict(data)
plt.figure(2)
colors = ['red', 'blue']
ax = visualization.plot(
data,
space='S2',
marker='.',
color='black')
for i in range(n_clusters):
def main():
"""Plot the result of a KNN classification on the sphere."""
sphere = Hypersphere(dim=2)
sphere_distance = sphere.metric.dist
n_labels = 2
n_samples_per_dataset = 10
n_targets = 200
dataset_1 = sphere.random_von_mises_fisher(
kappa=10,
n_samples=n_samples_per_dataset)
dataset_2 = - sphere.random_von_mises_fisher(
kappa=10,
n_samples=n_samples_per_dataset)
training_dataset = gs.concatenate((dataset_1, dataset_2), axis=0)
labels_dataset_1 = gs.zeros([n_samples_per_dataset], dtype=gs.int64)
labels_dataset_2 = gs.ones([n_samples_per_dataset], dtype=gs.int64)
labels = gs.concatenate((labels_dataset_1, labels_dataset_2))
target = sphere.random_uniform(n_samples=n_targets)
neigh = KNearestNeighborsClassifier(
n_neighbors=2,
distance=sphere_distance)
neigh.fit(training_dataset, labels)
target_labels = neigh.predict(target)
plt.figure(0)
ax = plt.subplot(111, projection='3d')
plt.title('Training set')
sphere_plot = visualization.Sphere()
sphere_plot.draw(ax=ax)
Parameters
----------
quaternion : array-like, shape=[..., 4]
Returns
-------
tait_bryan_angles : array-like, shape=[..., 3]
"""
w, x, y, z = gs.hsplit(quaternion, 4)
angle_1 = gs.arctan2(y * z + w * x,
1. / 2. - (x ** 2 + y ** 2))
angle_2 = gs.arcsin(- 2. * (x * z - w * y))
angle_3 = gs.arctan2(x * y + w * z,
1. / 2. - (y ** 2 + z ** 2))
tait_bryan_angles = gs.concatenate(
[angle_1, angle_2, angle_3], axis=1)
return tait_bryan_angles
def tait_bryan_angles_from_quaternion_intrinsic_zyx(self, quaternion):
assert self.n == 3, ('The quaternion representation'
' and the Tait-Bryan angles representation'
' do not exist'
' for rotations in %d dimensions.' % self.n)
quaternion = gs.to_ndarray(quaternion, to_ndim=2)
w, x, y, z = gs.hsplit(quaternion, 4)
angle_1 = gs.arctan2(y * z + w * x,
1. / 2. - (x ** 2 + y ** 2))
angle_2 = gs.arcsin(- 2. * (x * z - w * y))
angle_3 = gs.arctan2(x * y + w * z,
1. / 2. - (y ** 2 + z ** 2))
tait_bryan_angles = gs.concatenate(
[angle_1, angle_2, angle_3], axis=1)
return tait_bryan_angles
@staticmethod
def convert_to_klein_coordinates(points):
poincare_coords = points[:, 1:] / (1 + points[:, :1])
poincare_radius = gs.linalg.norm(
poincare_coords, axis=1)
poincare_angle = gs.arctan2(
poincare_coords[:, 1], poincare_coords[:, 0])
klein_radius = 2 * poincare_radius / (1 + poincare_radius ** 2)
klein_angle = poincare_angle
coords_0 = gs.expand_dims(
klein_radius * gs.cos(klein_angle), axis=1)
coords_1 = gs.expand_dims(
klein_radius * gs.sin(klein_angle), axis=1)
klein_coords = gs.concatenate([coords_0, coords_1], axis=1)
return klein_coords
loss_rot_vec = loss(y_pred, y_true)
grad_rot_vec = grad(y_pred, y_true)
logging.info('The loss between the poses using rotation '
'vectors is: {}'.format(loss_rot_vec))
logging.info('The Riemannian gradient is: {}'.format(grad_rot_vec))
angle = gs.array(gs.pi / 6)
cos = gs.cos(angle / 2)
sin = gs.sin(angle / 2)
u = gs.array([1., 2., 3.])
u = u / gs.linalg.norm(u)
scalar = gs.array(cos)
vec = sin * u
translation = gs.array([5., 6., 7.])
y_pred_quaternion = gs.concatenate([[scalar], vec, translation], axis=0)
angle = gs.array(gs.pi / 7)
cos = gs.cos(angle / 2)
sin = gs.sin(angle / 2)
u = gs.array([1., 2., 3.])
u = u / gs.linalg.norm(u)
scalar = gs.array(cos)
vec = sin * u
translation = gs.array([4., 5., 6.])
y_true_quaternion = gs.concatenate([[scalar], vec, translation], axis=0)
loss_quaternion = loss(y_pred_quaternion, y_true_quaternion,
representation='quaternion')
grad_quaternion = grad(y_pred_quaternion, y_true_quaternion,
representation='quaternion')
logging.info('The loss between the poses using quaternions is: {}'.format(
means,
variances,
plot_precision=DEFAULT_PLOT_PRECISION,
save_path='',
metric=None):
"""Plot Gaussian Mixture Model."""
x_axis_samples = gs.linspace(-1, 1, plot_precision)
y_axis_samples = gs.linspace(-1, 1, plot_precision)
x_axis_samples, y_axis_samples = gs.meshgrid(x_axis_samples,
y_axis_samples)
z_axis_samples = gs.zeros((plot_precision, plot_precision))
for z_index, _ in enumerate(z_axis_samples):
x_y_plane_mesh = gs.concatenate((
gs.expand_dims(x_axis_samples[z_index], -1),
gs.expand_dims(y_axis_samples[z_index], -1)),
axis=-1)
mesh_probabilities = PoincareBall.\
weighted_gmm_pdf(
mixture_coefficients,
x_y_plane_mesh,
means,
variances,
metric)
z_axis_samples[z_index] = mesh_probabilities.sum(-1)
fig = plt.figure('Learned Gaussian Mixture Model '
'via Expectation Maximisation on Poincaré Disc')
for i in range(n_vecs):
mask_i_float = get_mask_i_float(i, n_vecs)
basis_vec_1 = gs.array([1., 0., 0.])
basis_vec_2 = gs.array([0., 1., 0.])
basis_vec_3 = gs.array([0., 0., 1.])
cross_prod_1 = gs.cross(basis_vec_1, vec[i])
cross_prod_2 = gs.cross(basis_vec_2, vec[i])
cross_prod_3 = gs.cross(basis_vec_3, vec[i])
cross_prod_1 = gs.to_ndarray(cross_prod_1, to_ndim=2)
cross_prod_2 = gs.to_ndarray(cross_prod_2, to_ndim=2)
cross_prod_3 = gs.to_ndarray(cross_prod_3, to_ndim=2)
cross_prod_i = gs.concatenate(
[cross_prod_1, cross_prod_2, cross_prod_3], axis=0)
#print(gs.shape(n_vecs))
#n_vecs = gs.array([n_vecs])
#print(gs.shape(n_vecs))
#cross_prod_i = gs.to_ndarray(cross_prod_i, to_ndim=3)
#cross_prod_i = gs.tile(cross_prod_i, (n_vecs, 1, 1))
skew_mat += gs.einsum(
'n,ij->nij', mask_i_float, cross_prod_i)
else:
upper_triangle_indices = gs.triu_indices(mat_dim, k=1)
for i in range(n_vecs):
skew_mat[i][upper_triangle_indices] = vec[i]
skew_mat[i] = skew_mat[i] - skew_mat[i].transpose()
assert gs.ndim(skew_mat) == 3
def convert_to_half_plane_coordinates(points):
disk_coords = points[:, 1:] / (1 + points[:, :1])
disk_x = disk_coords[:, 0]
disk_y = disk_coords[:, 1]
denominator = (disk_x ** 2 + (1 - disk_y) ** 2)
coords_0 = gs.expand_dims(2 * disk_x / denominator, axis=1)
coords_1 = gs.expand_dims(
(1 - disk_x ** 2 - disk_y ** 2) / denominator, axis=1)
half_plane_coords = gs.concatenate(
[coords_0, coords_1], axis=1)
return half_plane_coords