Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@harripj
Copy link
Collaborator

@harripj harripj commented Apr 18, 2024

Description of the change

ax2qu conversion function throws if the arrays are multidimensional and last axis size is 1, even though they are broadcastable.

I appreciate having a redundant axis in an array is not ideal, but sometimes it is easier to work with it to keep the input and output shapes consistent 😄.

I have looked into the code and there appears to be a lot of shape juggling because the internal functions are numba jitted and ultimately require 2d arrays. These lines in particluar seem to be the issue:

if angles.shape[-1] != 1 or angles.shape == (1,):
angles = angles.reshape(angles.shape + (1,))
axes_shape = axes.shape[:-1]
angles_shape = angles.shape[:-1]

I believe we should also check here if axes.shape[:-1] == angles.shape and this will fix the issue.

Progress of the PR

Minimal example of the bug fix or new feature

>>> from orix.quaternion import Quaternion
>>> import numpy as np

>>> axes = np.random.randn(2, 1, 3)
>>> angles = np.random.randn(*axes.shape[:-1])

>>> Quaternion.from_axes_angles(axes, angles)
ValueError: The dimensions of axes (2, 1) and angles (2,) are incompatible. The dimensions must match or one must be a singular value.

With the fix in this PR:

>>> Quaternion.from_axes_angles(axes, angles)
Quaternion (2, 1)
[[[ 0.9999 -0.0058 -0.0025 -0.0078]]

 [[ 0.9386 -0.1434 -0.1071  0.295 ]]]

For reviewers

  • The PR title is short, concise, and will make sense 1 year later.
  • New functions are imported in corresponding __init__.py.
  • New features, API changes, and deprecations are mentioned in the unreleased
    section in CHANGELOG.rst.
  • Contributor(s) are listed correctly in __credits__ in orix/__init__.py and in
    .zenodo.json.

@harripj harripj requested a review from hakonanes April 18, 2024 17:40
@harripj harripj added the bug Something isn't working label Apr 18, 2024
@pc494
Copy link
Member

pc494 commented Apr 19, 2024

This looks good to me, will leave it to @hakonanes to review and merge though.

@pc494 pc494 mentioned this pull request Apr 19, 2024
Copy link
Member

@hakonanes hakonanes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll spotted!

We can include it in the 0.12.1 release if you change the base branch to main. I'll merge #491 after this PR.

@hakonanes hakonanes added this to the v0.12.1 milestone Apr 20, 2024
@hakonanes hakonanes changed the title ax2qu raises if last dimension is 1 ax2qu does not raise anymore if last dimension is 1 Apr 20, 2024
@harripj harripj changed the base branch from develop to main April 21, 2024 02:58
@harripj
Copy link
Collaborator Author

harripj commented Apr 21, 2024

@hakonanes thanks for reviewing! Yes it would be great to include this in the patch release. I've rebased this PR.

@hakonanes hakonanes merged commit 329d981 into pyxem:main Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants