diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 95ea60d3..49aac90d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,11 @@ Added ----- - Python 3.10 support. +Fixed +----- +- The inverse indices returned from `Rotation.unique()` now correctly recreate the + original `Rotation` instance. + 2021-12-21 - version 0.8.0 ========================== diff --git a/orix/quaternion/rotation.py b/orix/quaternion/rotation.py index c20cf574..fc35292c 100644 --- a/orix/quaternion/rotation.py +++ b/orix/quaternion/rotation.py @@ -158,7 +158,12 @@ def unique(self, return_index=False, return_inverse=False, antipodal=True): axis=-1, ).round(6) _, idx, inv = np.unique(abcd, axis=0, return_index=True, return_inverse=True) - idx_sort = np.sort(idx) + idx_argsort = np.argsort(idx) + idx_sort = idx[idx_argsort] + # build inverse index map + inv_map = np.empty_like(idx_argsort) + inv_map[idx_argsort] = np.arange(idx_argsort.size) + inv = inv_map[inv] dat = rotation[idx_sort] dat.improper = rotation.improper[idx_sort] if return_index and return_inverse: diff --git a/orix/tests/quaternion/test_rotation.py b/orix/tests/quaternion/test_rotation.py index 4a27c1a2..e94d7a72 100644 --- a/orix/tests/quaternion/test_rotation.py +++ b/orix/tests/quaternion/test_rotation.py @@ -299,6 +299,13 @@ def test_kwargs_unique(rotation): rotation.unique(return_index=False, return_inverse=True) +def test_unique_inverse(): + r = Rotation.random((20,)) + u, inverse = r.unique(return_inverse=True) + m = u[inverse] * ~r + assert np.allclose(m.angle.data, 0) + + @pytest.mark.parametrize( "rotation, improper, expected, improper_expected", [