Skip to content

FIX: Import nibabel reorientation bug fix #2912

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 1 commit into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 6 additions & 10 deletions nipype/interfaces/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ..utils.filemanip import fname_presuffix
from .base import (SimpleInterface, TraitedSpec, BaseInterfaceInputSpec,
traits, File)
from .. import LooseVersion


class RescaleInputSpec(BaseInterfaceInputSpec):
Expand Down Expand Up @@ -186,8 +187,8 @@ def _run_interface(self, runtime):
transform = ornt_transform(orig_ornt, targ_ornt)
affine_xfm = inv_ornt_aff(transform, orig_img.shape)

# Check can be eliminated when minimum nibabel version >= 2.2
if hasattr(orig_img, 'as_reoriented'):
# Check can be eliminated when minimum nibabel version >= 2.4
if LooseVersion(nb.__version__) >= LooseVersion('2.4.0'):
reoriented = orig_img.as_reoriented(transform)
else:
reoriented = _as_reoriented_backport(orig_img, transform)
Expand All @@ -212,7 +213,7 @@ def _run_interface(self, runtime):


def _as_reoriented_backport(img, ornt):
"""Backport of img.as_reoriented as of nibabel 2.2.0"""
"""Backport of img.as_reoriented as of nibabel 2.4.0"""
import numpy as np
import nibabel as nb
from nibabel.orientations import inv_ornt_aff
Expand All @@ -225,13 +226,8 @@ def _as_reoriented_backport(img, ornt):

if isinstance(reoriented, nb.Nifti1Pair):
# Also apply the transform to the dim_info fields
new_dim = list(reoriented.header.get_dim_info())
for idx, value in enumerate(new_dim):
# For each value, leave as None if it was that way,
# otherwise check where we have mapped it to
if value is None:
continue
new_dim[idx] = np.where(ornt[:, 0] == idx)[0]
new_dim = [None if orig_dim is None else int(ornt[orig_dim, 0])
for orig_dim in img.header.get_dim_info()]

reoriented.header.set_dim_info(*new_dim)

Expand Down
4 changes: 2 additions & 2 deletions nipype/interfaces/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from ..image import _as_reoriented_backport, _orientations
from ... import LooseVersion

nibabel22 = LooseVersion(nb.__version__) >= LooseVersion('2.2.0')
nibabel24 = LooseVersion(nb.__version__) >= LooseVersion('2.4.0')


@pytest.mark.skipif(not nibabel22,
@pytest.mark.skipif(not nibabel24,
reason="Old nibabel - can't directly compare")
def test_reorientation_backport():
pixdims = ((1, 1, 1), (2, 2, 3))
Expand Down