Skip to content

Commit

Permalink
Remove loops for 'hughes' and 'chiaverini' (they vectorize 3d arrays …
Browse files Browse the repository at this point in the history
…already). Add handling of wrong input in method 'from_DCM()'.
  • Loading branch information
Mayitzin committed Sep 6, 2023
1 parent a1c0157 commit 708c3b8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions ahrs/common/quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2588,29 +2588,31 @@ def from_DCM(self, DCM: np.ndarray, method: str='chiaverini', inplace: bool = Tr
array([0.94371436, 0.26853582, 0.14487813, 0.12767944])
"""
# Handle input
if method.lower() not in ['chiaverini', 'hughes', 'itzhack', 'sarabandi', 'shepperd']:
raise ValueError(f"Method '{method}' not available. Options are: 'chiaverini', 'hughes', 'itzhack', 'sarabandi', and 'shepperd'.")
_assert_iterables(DCM, 'Direction Cosine Matrices')
# Allocate local quaternion array
quaternion_array = np.zeros((DCM.shape[0], 4))
try:
if method.lower() == 'hughes':
for i, R in enumerate(DCM):
quaternion_array[i] = hughes(R)
quaternion_array = hughes(DCM)
if method.lower() == 'chiaverini':
for i, R in enumerate(DCM):
quaternion_array[i] = chiaverini(R)
quaternion_array = chiaverini(DCM)
if method.lower() == 'shepperd':
for i, R in enumerate(DCM):
quaternion_array[i] = shepperd(R)
if method.lower() == 'itzhack':
version = kw.get('version', 3)
for i, R in enumerate(DCM):
quaternion_array[i] = itzhack(R, version=version)
q = itzhack(self.A, version=kw.get('version', 3))
if method.lower() == 'sarabandi':
threshold = kw.get('threshold', 0.0)
for i, R in enumerate(DCM):
quaternion_array[i] = sarabandi(R, eta=threshold)
except RuntimeWarning:
failed_DCM = DCM[i]
msg = f"Method '{method}' failed at DCM:\n{failed_DCM}\n"
msg = f"Method '{method}' failed at DCM[{i}]:\n{failed_DCM}\n"
raise RuntimeError(msg)
quaternion_array /= np.linalg.norm(quaternion_array, axis=1)[:, None]
if inplace:
Expand Down

0 comments on commit 708c3b8

Please sign in to comment.