## "Fossies" - the Fresh Open Source Software Archive

### Source code changes of the file "scipy/spatial/transform/tests/test_rotation.py" betweenscipy-1.6.2.tar.xz and scipy-1.6.3.tar.xz

About: SciPy are tools for mathematics, science, and engineering (for Python).

test_rotation.py  (scipy-1.6.2.tar.xz):test_rotation.py  (scipy-1.6.3.tar.xz)
import pytest import pytest
import numpy as np import numpy as np
from numpy.testing import assert_equal, assert_array_almost_equal from numpy.testing import assert_equal, assert_array_almost_equal
from numpy.testing import assert_allclose from numpy.testing import assert_allclose
from scipy.spatial.transform import Rotation, Slerp from scipy.spatial.transform import Rotation, Slerp
from scipy.stats import special_ortho_group from scipy.stats import special_ortho_group
from itertools import permutations from itertools import permutations
import pickle import pickle
import copy import copy
def test_generic_quat_matrix(): def test_generic_quat_matrix():
x = np.array([[3, 4, 0, 0], [5, 12, 0, 0]]) x = np.array([[3, 4, 0, 0], [5, 12, 0, 0]])
r = Rotation.from_quat(x) r = Rotation.from_quat(x)
skipping to change at line 656 skipping to change at line 656
p_mat = p.as_matrix() p_mat = p.as_matrix()
q_mat = q.as_matrix() q_mat = q.as_matrix()
res1 = np.dot(p_mat, q_mat) res1 = np.dot(p_mat, q_mat)
res2 = np.dot(q_mat, p_mat) res2 = np.dot(q_mat, p_mat)
eye = np.eye(3) eye = np.eye(3)
assert_array_almost_equal(res1, eye) assert_array_almost_equal(res1, eye)
assert_array_almost_equal(res2, eye) assert_array_almost_equal(res2, eye)
x = Rotation.from_quat(.normal(size=(1, 4))) x = Rotation.from_quat(rnd.normal(size=(1, 4)))
y = x.inv() y = x.inv()
x_matrix = x.as_matrix() x_matrix = x.as_matrix()
y_matrix = y.as_matrix() y_matrix = y.as_matrix()
result1 = np.einsum('...ij,...jk->...ik', x_matrix, y_matrix) result1 = np.einsum('...ij,...jk->...ik', x_matrix, y_matrix)
result2 = np.einsum('...ij,...jk->...ik', y_matrix, x_matrix) result2 = np.einsum('...ij,...jk->...ik', y_matrix, x_matrix)
eye3d = np.empty((1, 3, 3)) eye3d = np.empty((1, 3, 3))
eye3d[:] = np.eye(3) eye3d[:] = np.eye(3)
skipping to change at line 681 skipping to change at line 681
n = 10 n = 10
assert_allclose(Rotation.identity(n).magnitude(), 0) assert_allclose(Rotation.identity(n).magnitude(), 0)
assert_allclose(Rotation.identity(n).inv().magnitude(), 0) assert_allclose(Rotation.identity(n).inv().magnitude(), 0)
def test_single_identity_magnitude(): def test_single_identity_magnitude():
assert Rotation.identity().magnitude() == 0 assert Rotation.identity().magnitude() == 0
assert Rotation.identity().inv().magnitude() == 0 assert Rotation.identity().inv().magnitude() == 0
def test_identity_invariance(): def test_identity_invariance():
n = 10 n = 10
p = Rotation.random(n) p = Rotation.random(n, random_state=0)
result = p * Rotation.identity(n) result = p * Rotation.identity(n)
assert_array_almost_equal(p.as_quat(), result.as_quat()) assert_array_almost_equal(p.as_quat(), result.as_quat())
result = result * p.inv() result = result * p.inv()
assert_array_almost_equal(result.magnitude(), np.zeros(n)) assert_array_almost_equal(result.magnitude(), np.zeros(n))
def test_single_identity_invariance(): def test_single_identity_invariance():
n = 10 n = 10
p = Rotation.random(n) p = Rotation.random(n, random_state=0)
result = p * Rotation.identity() result = p * Rotation.identity()
assert_array_almost_equal(p.as_quat(), result.as_quat()) assert_array_almost_equal(p.as_quat(), result.as_quat())
result = result * p.inv() result = result * p.inv()
assert_array_almost_equal(result.magnitude(), np.zeros(n)) assert_array_almost_equal(result.magnitude(), np.zeros(n))
def test_magnitude(): def test_magnitude():
r = Rotation.from_quat(np.eye(4)) r = Rotation.from_quat(np.eye(4))
result = r.magnitude() result = r.magnitude()
skipping to change at line 919 skipping to change at line 919
x = np.array([[1, 2, 3], [4, 5, 6]]) x = np.array([[1, 2, 3], [4, 5, 6]])
y = x.copy() y = x.copy()
r, rmsd = Rotation.align_vectors(x, y) r, rmsd = Rotation.align_vectors(x, y)
assert_array_almost_equal(r.as_matrix(), np.eye(3)) assert_array_almost_equal(r.as_matrix(), np.eye(3))
assert_allclose(rmsd, 0, atol=1e-6) assert_allclose(rmsd, 0, atol=1e-6)
def test_align_vectors_no_noise(): def test_align_vectors_no_noise():
rnd = np.random.RandomState(0) rnd = np.random.RandomState(0)
c = Rotation.from_quat(rnd.normal(size=4)) c = Rotation.from_quat(rnd.normal(size=4))
b = .normal(size=(5, 3)) b = rnd.normal(size=(5, 3))
a = c.apply(b) a = c.apply(b)
est, rmsd = Rotation.align_vectors(a, b) est, rmsd = Rotation.align_vectors(a, b)
assert_allclose(c.as_quat(), est.as_quat()) assert_allclose(c.as_quat(), est.as_quat())
assert_allclose(rmsd, 0, atol=1e-7) assert_allclose(rmsd, 0, atol=1e-7)
def test_align_vectors_improper_rotation(): def test_align_vectors_improper_rotation():
# Tests correct logic for issue #10444 # Tests correct logic for issue #10444
x = np.array([[0.89299824, -0.44372674, 0.0752378], x = np.array([[0.89299824, -0.44372674, 0.0752378],
[0.60221789, -0.47564102, -0.6411702]]) [0.60221789, -0.47564102, -0.6411702]])
skipping to change at line 954 skipping to change at line 954
est2, rmsd2, cov2 = Rotation.align_vectors(a, b, 2 * np.ones(5), True) est2, rmsd2, cov2 = Rotation.align_vectors(a, b, 2 * np.ones(5), True)
assert_allclose(est1.as_matrix(), est2.as_matrix()) assert_allclose(est1.as_matrix(), est2.as_matrix())
assert_allclose(np.sqrt(2) * rmsd1, rmsd2) assert_allclose(np.sqrt(2) * rmsd1, rmsd2)
assert_allclose(cov1, cov2) assert_allclose(cov1, cov2)
def test_align_vectors_noise(): def test_align_vectors_noise():
rnd = np.random.RandomState(0) rnd = np.random.RandomState(0)
n_vectors = 100 n_vectors = 100
rot = Rotation.from_euler('xyz', rnd.normal(size=3)) rot = Rotation.from_euler('xyz', rnd.normal(size=3))
vectors = .normal(size=(n_vectors, 3)) vectors = rnd.normal(size=(n_vectors, 3))
result = rot.apply(vectors) result = rot.apply(vectors)
# The paper adds noise as independently distributed angular errors # The paper adds noise as independently distributed angular errors
tolerance = 1.5 * sigma tolerance = 1.5 * sigma
noise = Rotation.from_rotvec( noise = Rotation.from_rotvec(
.normal( rnd.normal(
size=(n_vectors, 3), size=(n_vectors, 3),
scale=sigma scale=sigma
) )
) )
# Attitude errors must preserve norm. Hence apply individual random # Attitude errors must preserve norm. Hence apply individual random
# rotations to each vector. # rotations to each vector.
noisy_result = noise.apply(result) noisy_result = noise.apply(result)
est, rmsd, cov = Rotation.align_vectors(noisy_result, vectors, est, rmsd, cov = Rotation.align_vectors(noisy_result, vectors,
skipping to change at line 1013 skipping to change at line 1013
with pytest.raises(ValueError, with pytest.raises(ValueError,
match="Expected `weights` to be 1 dimensional"): match="Expected `weights` to be 1 dimensional"):
Rotation.align_vectors([[1, 2, 3]], [[1, 2, 3]], weights=[[1]]) Rotation.align_vectors([[1, 2, 3]], [[1, 2, 3]], weights=[[1]])
with pytest.raises(ValueError, with pytest.raises(ValueError,
match="Expected `weights` to have number of values"): match="Expected `weights` to have number of values"):
Rotation.align_vectors([[1, 2, 3]], [[1, 2, 3]], weights=[1, 2]) Rotation.align_vectors([[1, 2, 3]], [[1, 2, 3]], weights=[1, 2])
def test_random_rotation_shape(): def test_random_rotation_shape():
(4,)) rnd = np.random.RandomState(0)
(4,)) assert_equal(Rotation.random(random_state=rnd).as_quat().shape, (4,))
assert_equal(Rotation.random(None, random_state=rnd).as_quat().shape, (4,))
(1, 4)) assert_equal(Rotation.random(1, random_state=rnd).as_quat().shape, (1, 4))
(5, 4)) assert_equal(Rotation.random(5, random_state=rnd).as_quat().shape, (5, 4))
def test_slerp(): def test_slerp():
rnd = np.random.RandomState(0) rnd = np.random.RandomState(0)
key_rots = Rotation.from_quat(rnd.uniform(size=(5, 4))) key_rots = Rotation.from_quat(rnd.uniform(size=(5, 4)))
key_quats = key_rots.as_quat() key_quats = key_rots.as_quat()
key_times = [0, 1, 2, 3, 4] key_times = [0, 1, 2, 3, 4]
interpolator = Slerp(key_times, key_rots) interpolator = Slerp(key_times, key_rots)
skipping to change at line 1140 skipping to change at line 1141
assert_allclose(delta.magnitude(), 0, atol=1e-16) assert_allclose(delta.magnitude(), 0, atol=1e-16)
def test_multiplication_stability(): def test_multiplication_stability():
qs = Rotation.random(50, random_state=0) qs = Rotation.random(50, random_state=0)
rs = Rotation.random(1000, random_state=1) rs = Rotation.random(1000, random_state=1)
for q in qs: for q in qs:
rs *= q * rs rs *= q * rs
assert_allclose(np.linalg.norm(rs.as_quat(), axis=1), 1) assert_allclose(np.linalg.norm(rs.as_quat(), axis=1), 1)
def test_rotation_within_numpy_array(): def test_rotation_within_numpy_array():
single = single = Rotation.random(random_state=0)
multiple = multiple = Rotation.random(2, random_state=1)
array = np.array(single) array = np.array(single)
assert_equal(array.shape, ()) assert_equal(array.shape, ())
array = np.array(multiple) array = np.array(multiple)
assert_equal(array.shape, (2,)) assert_equal(array.shape, (2,))
assert_allclose(array[0].as_matrix(), multiple[0].as_matrix()) assert_allclose(array[0].as_matrix(), multiple[0].as_matrix())
assert_allclose(array[1].as_matrix(), multiple[1].as_matrix()) assert_allclose(array[1].as_matrix(), multiple[1].as_matrix())
array = np.array([single]) array = np.array([single])
End of changes. 10 change blocks.
13 lines changed or deleted 14 lines changed or added