8000 ENH: Add a utility to calculate obliquity of affines by oesteban · Pull Request #815 · nipy/nibabel · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add a utility to calculate obliquity of affines #815

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

Merged
merged 6 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
enh: address @matthew-brett's comments
  • Loading branch information
oesteban committed Sep 23, 2019
commit 253a256d18d75664001ef3d9872c25952675b7f4
26 changes: 17 additions & 9 deletions nibabel/affines.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
""" Utility routines for working with points and affine transforms
"""

from math import acos, pi as PI
import numpy as np

from functools import reduce
Expand Down Expand Up @@ -299,17 +297,27 @@ def voxel_sizes(affine):
return np.sqrt(np.sum(top_left ** 2, axis=0))


def obliquity(affine, degrees=False):
def obliquity(affine):
r"""
Estimate the obliquity an affine's axes represent, in degrees from plumb.
Estimate the *obliquity* an affine's axes represent.

The term *obliquity* is defined here as the rotation of those axes with
respect to the cardinal axes.
This implementation is inspired by `AFNI's implementation
<https://github.com/afni/afni/blob/b6a9f7a21c1f3231ff09efbd861f8975ad48e525/src/thd_coords.c#L660-L698>`_

Parameters
----------
affine : 2D array-like
Affine transformation array. Usually shape (4, 4), but can be any 2D
array.

Returns
-------
angles : 1D array-like
The *obliquity* of each axis with respect to the cardinal axes, in radians.

"""
vs = voxel_sizes(affine)
fig_merit = (affine[:-1, :-1] / vs[np.newaxis, ...]).max(axis=1).min()
radians = abs(acos(fig_merit))
if not degrees:
return radians
return radians * 180 / PI
best_cosines = np.abs((affine[:-1, :-1] / vs).max(axis=1))
return np.arccos(best_cosines)
6 changes: 4 additions & 2 deletions nibabel/tests/test_affines.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ def test_voxel_sizes():

def test_obliquity():
"""Check the calculation of inclination of an affine axes."""
from math import pi
aligned = np.diag([2.0, 2.0, 2.3, 1.0])
aligned[:-1, -1] = [-10, -10, -7]
R = from_matvec(euler2mat(x=0.09, y=0.001, z=0.001), [0.0, 0.0, 0.0])
oblique = R.dot(aligned)
assert_almost_equal(obliquity(aligned), 0.0)
assert_almost_equal(obliquity(oblique, degrees=True), 5.1569948883)
assert_almost_equal(obliquity(aligned), [0.0, 0.0, 0.0])
assert_almost_equal(obliquity(oblique) * 180 / pi,
[0.0810285, 5.1569949, 5.1569376])
0