Skip to content

Commit 399cc41

Browse files
committed
ENH: Reorient volumetric data to LAS
1 parent bfffaf8 commit 399cc41

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

niworkflows/interfaces/cifti.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ 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+
321326
bold_data = bold_img.get_fdata(dtype='float32')
322327
timepoints = bold_img.shape[3]
323328
label_data = np.asanyarray(label_img.dataobj).astype('int16')
@@ -422,3 +427,23 @@ def _create_cifti_image(bold_file, label_file, bold_surfs, annotation_files, tr,
422427
out_file = "{}.dtseries.nii".format(split_filename(bold_file)[1])
423428
ci.save(img, out_file)
424429
return Path.cwd() / out_file
430+
431+
432+
def _reorient_image(img, orient_img, orient_target):
433+
"""
434+
Coerce an image to a target orientation.
435+
436+
.. note::
437+
Only RAS -> LAS conversion is currently supported
438+
439+
Parameters
440+
----------
441+
img : `spatialimage`
442+
orient_img : tuple
443+
axis direction codes
444+
orient_target : tuple
445+
axis direction codes
446+
"""
447+
if orient_img == tuple('RAS') and orient_target == tuple('LAS'): # RAS -> LAS
448+
return img.as_reoriented([[0, -1], [1, 1], [2, 1]])
449+
raise NotImplementedError(f"Cannot reorient {orient_img} to {orient_target}.")

0 commit comments

Comments
 (0)