Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.unique() method of Rotation does not return correct indexing for inversion #272

Merged
merged 10 commits into from Feb 11, 2022
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -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
==========================

Expand Down
7 changes: 6 additions & 1 deletion orix/quaternion/rotation.py
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions orix/tests/quaternion/test_rotation.py
Expand Up @@ -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",
[
Expand Down