@@ -318,6 +318,11 @@ def _create_cifti_image(bold_file, label_file, bold_surfs, annotation_files, tr,
318
318
warnings .warn ("Resampling bold volume to match label dimensions" )
319
319
bold_img = resample_to_img (bold_img , label_img )
320
320
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
+
321
326
bold_data = bold_img .get_fdata (dtype = 'float32' )
322
327
timepoints = bold_img .shape [3 ]
323
328
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,
422
427
out_file = "{}.dtseries.nii" .format (split_filename (bold_file )[1 ])
423
428
ci .save (img , out_file )
424
429
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