Skip to content

Commit 8451bfa

Browse files
committed
doc+sty: suggestions from review
1 parent 94f44fb commit 8451bfa

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

niworkflows/interfaces/cifti.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,7 @@ def _create_cifti_image(bold_file, label_file, bold_surfs, annotation_files, tr,
318318
warnings.warn("Resampling bold volume to match label dimensions")
319319
bold_img = resample_to_img(bold_img, label_img)
320320

321-
bold_orient = nb.aff2axcodes(bold_img.affine)
322-
label_orient = nb.aff2axcodes(label_img.affine)
323-
if bold_orient != label_orient:
324-
bold_img = _reorient_image(bold_img, bold_orient, label_orient)
325-
assert nb.aff2axcodes(bold_img) == label_orient == tuple('LAS')
321+
bold_img = _reorient_image(bold_img, target_img=label_img)
326322

327323
bold_data = bold_img.get_fdata(dtype='float32')
328324
timepoints = bold_img.shape[3]
@@ -430,7 +426,7 @@ def _create_cifti_image(bold_file, label_file, bold_surfs, annotation_files, tr,
430426
return Path.cwd() / out_file
431427

432428

433-
def _reorient_image(img, orient_img, orient_target):
429+
def _reorient_image(img, *, target_img=None, orientation=None):
434430
"""
435431
Coerce an image to a target orientation.
436432
@@ -439,29 +435,49 @@ def _reorient_image(img, orient_img, orient_target):
439435
440436
Parameters
441437
----------
442-
img : `spatialimage`
443-
orient_img : tuple
444-
axis direction codes
445-
orient_target : tuple
446-
axis direction codes
438+
img : :obj:`SpatialImage`
439+
image to be reoriented
440+
target_img : :obj:`SpatialImage`, optional
441+
target in desired orientation
442+
orientation : :obj:`str` or :obj:`tuple`, optional
443+
desired orientation, if no target image is provided
447444
448445
.. testsetup::
449446
>>> img = nb.load(Path(test_data) / 'testRobustMNINormalizationRPTMovingWarpedImage.nii.gz')
447+
>>> las_img = img.as_reoriented([[0, -1], [1, 1], [2, 1]])
450448
451449
Examples
452450
--------
453-
>>> nimg = _reorient_image(img, ('R', 'A', 'S'), ('L', 'A', 'S'))
451+
>>> nimg = _reorient_image(img, target_img=las_img)
454452
>>> nb.aff2axcodes(nimg.affine)
455453
('L', 'A', 'S')
456454
457-
>>> _reorient_image(img, ('R', 'A', 'S'), ('L', 'P', 'I'))
455+
>>> nimg = _reorient_image(img, orientation='LAS')
456+
>>> nb.aff2axcodes(nimg.affine)
457+
('L', 'A', 'S')
458+
459+
>>> _reorient_image(img, orientation='LPI')
458460
Traceback (most recent call last):
459461
...
460462
NotImplementedError: Cannot reorient ...
461463
464+
>>> _reorient_image(img)
465+
Traceback (most recent call last):
466+
...
467+
RuntimeError: No orientation ...
468+
462469
"""
463-
if orient_img == tuple('RAS') and orient_target == tuple('LAS'): # RAS -> LAS
470+
orient0 = nb.aff2axcodes(img.affine)
471+
if target_img is not None:
472+
orient1 = nb.aff2axcodes(target_img.affine)
473+
elif orientation is not None:
474+
orient1 = tuple(orientation)
475+
else:
476+
raise RuntimeError("No orientation to reorient to!")
477+
478+
if orient0 == tuple('RAS') and orient1 == tuple('LAS'): # RAS -> LAS
464479
return img.as_reoriented([[0, -1], [1, 1], [2, 1]])
465-
raise NotImplementedError(
466-
"Cannot reorient {0} to {1}.".format(orient_img, orient_target)
467-
)
480+
else:
481+
raise NotImplementedError(
482+
"Cannot reorient {0} to {1}.".format(orient0, orient1)
483+
)

0 commit comments

Comments
 (0)