Skip to content

Commit 2f97593

Browse files
committed
FIX: Replace incomplete CIFTI implementation
1 parent e42a7b4 commit 2f97593

File tree

1 file changed

+4
-66
lines changed

1 file changed

+4
-66
lines changed

niworkflows/interfaces/cifti.py

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from nibabel import cifti2 as ci
3030
import numpy as np
3131
from nilearn.image import resample_to_img
32-
3332
from nipype.utils.filemanip import split_filename
3433
from nipype.interfaces.base import (
3534
BaseInterfaceInputSpec,
@@ -41,6 +40,8 @@
4140
)
4241
import templateflow.api as tf
4342

43+
from niworkflows.interfaces.nibabel import reorient_image
44+
4445
CIFTI_SURFACES = ("fsaverage5", "fsaverage6", "fsLR")
4546
CIFTI_VOLUMES = ("MNI152NLin2009cAsym", "MNI152NLin6Asym")
4647
CIFTI_STRUCT_WITH_LABELS = { # CITFI structures with corresponding labels
@@ -356,8 +357,8 @@ def _create_cifti_image(
356357
bold_img = resample_to_img(bold_img, label_img)
357358

358359
# ensure images match HCP orientation (LAS)
359-
bold_img = _reorient_image(bold_img, orientation="LAS")
360-
label_img = _reorient_image(label_img, orientation="LAS")
360+
bold_img = reorient_image(bold_img, target_orientation="LAS")
361+
label_img = reorient_image(label_img, target_orientation="LAS")
361362

362363
bold_data = bold_img.get_fdata(dtype="float32")
363364
timepoints = bold_img.shape[3]
@@ -466,66 +467,3 @@ def _create_cifti_image(
466467
out_file = "{}.dtseries.nii".format(split_filename(bold_file)[1])
467468
ci.save(img, out_file)
468469
return Path.cwd() / out_file
469-
470-
471-
def _reorient_image(img, *, target_img=None, orientation=None):
472-
"""
473-
Coerce an image to a target orientation.
474-
475-
.. note::
476-
Only RAS -> LAS conversion is currently supported
477-
478-
Parameters
479-
----------
480-
img : :obj:`SpatialImage`
481-
image to be reoriented
482-
target_img : :obj:`SpatialImage`, optional
483-
target in desired orientation
484-
orientation : :obj:`str` or :obj:`tuple`, optional
485-
desired orientation, if no target image is provided
486-
487-
.. testsetup::
488-
>>> img = nb.load(Path(test_data) / 'testSpatialNormalizationRPTMovingWarpedImage.nii.gz')
489-
>>> las_img = img.as_reoriented([[0, -1], [1, 1], [2, 1]])
490-
491-
Examples
492-
--------
493-
>>> nimg = _reorient_image(img, target_img=img)
494-
>>> nb.aff2axcodes(nimg.affine)
495-
('R', 'A', 'S')
496-
497-
>>> nimg = _reorient_image(img, target_img=las_img)
498-
>>> nb.aff2axcodes(nimg.affine)
499-
('L', 'A', 'S')
500-
501-
>>> nimg = _reorient_image(img, orientation='LAS')
502-
>>> nb.aff2axcodes(nimg.affine)
503-
('L', 'A', 'S')
504-
505-
>>> _reorient_image(img, orientation='LPI')
506-
Traceback (most recent call last):
507-
...
508-
NotImplementedError: Cannot reorient ...
509-
510-
>>> _reorient_image(img)
511-
Traceback (most recent call last):
512-
...
513-
RuntimeError: No orientation ...
514-
515-
"""
516-
orient0 = nb.aff2axcodes(img.affine)
517-
if target_img is not None:
518-
orient1 = nb.aff2axcodes(target_img.affine)
519-
elif orientation is not None:
520-
orient1 = tuple(orientation)
521-
else:
522-
raise RuntimeError("No orientation to reorient to!")
523-
524-
if orient0 == orient1: # already in desired orientation
525-
return img
526-
elif orient0 == tuple("RAS") and orient1 == tuple("LAS"): # RAS -> LAS
527-
return img.as_reoriented([[0, -1], [1, 1], [2, 1]])
528-
else:
529-
raise NotImplementedError(
530-
"Cannot reorient {0} to {1}.".format(orient0, orient1)
531-
)

0 commit comments

Comments
 (0)