Skip to content

Commit

Permalink
Improve quaternion tests, add custom skip decorators
Browse files Browse the repository at this point in the history
Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
  • Loading branch information
hakonanes committed Sep 15, 2024
1 parent c30e891 commit bc23f08
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
32 changes: 24 additions & 8 deletions orix/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@
import numpy as np
import pytest

from orix import constants
from orix.crystal_map import CrystalMap, PhaseList, create_coordinate_arrays
from orix.quaternion import Rotation

# --------------------------- pytest hooks --------------------------- #


def pytest_sessionstart(session): # pragma: no cover
plt.rcParams["backend"] = "agg"


@pytest.fixture
def rotations():
return Rotation([(2, 4, 6, 8), (-1, -3, -5, -7)])

# -------------------- Control of test selection --------------------- #

@pytest.fixture()
def eu():
return np.random.rand(10, 3)
skipif_numpy_quaternion_present = pytest.mark.skipif(
constants.installed["numpy-quaternion"], reason="numpy-quaternion installed"
)

skipif_numpy_quaternion_missing = pytest.mark.skipif(
not constants.installed["numpy-quaternion"], reason="numpy-quaternion not installed"
)

# ---------------------------- IO fixtures --------------------------- #

Expand Down Expand Up @@ -1164,7 +1167,7 @@ def crystal_map(crystal_map_input):


# ---------- Rotation representations for conversion tests ----------- #
# NOTE to future test writers on unittest data:
# NOTE: to future test writers on unittest data:
# All the data below can be recreated using 3Drotations, which is
# available at
# https://github.com/marcdegraef/3Drotations/blob/master/src/python.
Expand Down Expand Up @@ -1372,6 +1375,19 @@ def euler_angles():
# ------- End of rotation representations for conversion tests ------- #


# ------------------------ Geometry fixtures ------------------------- #


@pytest.fixture
def rotations():
return Rotation([(2, 4, 6, 8), (-1, -3, -5, -7)])


@pytest.fixture()
def eu():
return np.random.rand(10, 3)


@pytest.fixture(autouse=True)
def import_to_namespace(doctest_namespace):
"""Make :mod:`numpy` and :mod:`matplotlib.pyplot` available in
Expand Down
28 changes: 17 additions & 11 deletions orix/tests/quaternion/test_quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ def quaternion(request):
return Quaternion(request.param)


@pytest.fixture
def identity():
return Quaternion((1, 0, 0, 0))


@pytest.fixture(
params=[
(0.881, 0.665, 0.123, 0.517),
Expand Down Expand Up @@ -105,8 +100,8 @@ def test_mul(self, quaternion, something):
assert np.allclose(q1.c, qa * sc - qb * sd + qc * sa + qd * sb)
assert np.allclose(q1.d, qa * sd + qb * sc - qc * sb + qd * sa)

def test_mul_identity(self, quaternion, identity):
assert np.allclose((quaternion * identity).data, quaternion.data)
def test_mul_identity(self, quaternion):
assert np.allclose((quaternion * Quaternion.identity()).data, quaternion.data)

def test_no_multiplicative_inverse(self, quaternion, something):
q1 = quaternion * something
Expand Down Expand Up @@ -162,10 +157,21 @@ def test_dot_outer(self, quaternion, something):
],
)
def test_multiply_vector(self, quaternion, vector, expected):
q = Quaternion(quaternion)
v = Vector3d(vector)
v_new = q * v
assert np.allclose(v_new.data, expected)
Q1 = Quaternion(quaternion)
v1 = Vector3d(vector)
v2 = Q1 * v1
assert np.allclose(v2.data, expected)

def test_multiply_vector_float32(self):
Q1 = Quaternion.random()
v1 = Vector3d.random()

Q2 = Quaternion(Q1)
Q2._data = Q2._data.astype(np.float32)

v2 = Q1 * v1
v3 = Q2 * v1
assert np.allclose(v3.data, v2.data, atol=1e-6)

def test_abcd_properties(self):
quat = Quaternion([2, 2, 2, 2])
Expand Down
30 changes: 30 additions & 0 deletions orix/tests/test_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2018-2024 the orix developers
#
# This file is part of orix.
#
# orix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# orix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with orix. If not, see <http://www.gnu.org/licenses/>.

from orix import constants

from .conftest import skipif_numpy_quaternion_missing, skipif_numpy_quaternion_present


class TestConstants:
@skipif_numpy_quaternion_present
def test_numpy_quaternion_not_installed(self):
assert not constants.installed["numpy-quaternion"]

@skipif_numpy_quaternion_missing
def test_numpy_quaternion_installed(self):
assert constants.installed["numpy-quaternion"]

0 comments on commit bc23f08

Please sign in to comment.