Skip to content

Commit 39f0fd1

Browse files
committed
FIX: Split reorientation into file/in-memory functions
1 parent 33544f7 commit 39f0fd1

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

niworkflows/interfaces/nibabel.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -515,16 +515,16 @@ class ReorientImage(SimpleInterface):
515515
output_spec = ReorientImageOutputSpec
516516

517517
def _run_interface(self, runtime):
518-
self._results["out_file"] = reorient_image(
518+
self._results["out_file"] = reorient_file(
519519
self.inputs.in_file,
520520
target_file=self.inputs.target_file,
521521
target_ornt=self.inputs.target_orientation,
522522
)
523523
return runtime
524524

525525

526-
def reorient_image(
527-
in_file: str, *, target_file: str = None, target_ornt: str = None, newpath: str = None
526+
def reorient_file(
527+
in_file: str, *, target_file: str = None, target_ornt: str = None, newpath: str = None,
528528
) -> str:
529529
"""
530530
Reorient an image.
@@ -541,16 +541,14 @@ def reorient_image(
541541
import nibabel as nb
542542

543543
img = nb.load(in_file)
544-
img_axcodes = nb.aff2axcodes(img.affine)
545-
in_ornt = nb.orientations.axcodes2ornt(img_axcodes)
544+
if not target_file and not target_ornt:
545+
raise TypeError("No target orientation or file is specified.")
546546

547547
if target_file:
548548
target_img = nb.load(target_file)
549549
target_ornt = nb.aff2axcodes(target_img.affine)
550550

551-
out_ornt = nb.orientations.axcodes2ornt(target_ornt)
552-
ornt_xfm = nb.orientations.ornt_transform(in_ornt, out_ornt)
553-
reoriented = img.as_reoriented(ornt_xfm)
551+
reoriented = reorient_image(img, target_ornt)
554552

555553
if newpath is None:
556554
newpath = Path()
@@ -559,6 +557,18 @@ def reorient_image(
559557
return out_file
560558

561559

560+
def reorient_image(img: nb.spatialimages.SpatialImage, target_ornt: str):
561+
"""Reorient an image in memory."""
562+
import nibabel as nb
563+
564+
img_axcodes = nb.aff2axcodes(img.affine)
565+
in_ornt = nb.orientations.axcodes2ornt(img_axcodes)
566+
out_ornt = nb.orientations.axcodes2ornt(target_ornt)
567+
ornt_xfm = nb.orientations.ornt_transform(in_ornt, out_ornt)
568+
r_img = img.as_reoriented(ornt_xfm)
569+
return r_img
570+
571+
562572
def _gen_reference(
563573
fixed_image,
564574
moving_image,

0 commit comments

Comments
 (0)