@@ -318,11 +318,7 @@ 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
- assert nb .aff2axcodes (bold_img ) == label_orient == tuple ('LAS' )
321
+ bold_img = _reorient_image (bold_img , target_img = label_img )
326
322
327
323
bold_data = bold_img .get_fdata (dtype = 'float32' )
328
324
timepoints = bold_img .shape [3 ]
@@ -430,7 +426,7 @@ def _create_cifti_image(bold_file, label_file, bold_surfs, annotation_files, tr,
430
426
return Path .cwd () / out_file
431
427
432
428
433
- def _reorient_image (img , orient_img , orient_target ):
429
+ def _reorient_image (img , * , target_img = None , orientation = None ):
434
430
"""
435
431
Coerce an image to a target orientation.
436
432
@@ -439,29 +435,49 @@ def _reorient_image(img, orient_img, orient_target):
439
435
440
436
Parameters
441
437
----------
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
447
444
448
445
.. testsetup::
449
446
>>> img = nb.load(Path(test_data) / 'testRobustMNINormalizationRPTMovingWarpedImage.nii.gz')
447
+ >>> las_img = img.as_reoriented([[0, -1], [1, 1], [2, 1]])
450
448
451
449
Examples
452
450
--------
453
- >>> nimg = _reorient_image(img, ('R', 'A', 'S'), ('L', 'A', 'S') )
451
+ >>> nimg = _reorient_image(img, target_img=las_img )
454
452
>>> nb.aff2axcodes(nimg.affine)
455
453
('L', 'A', 'S')
456
454
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')
458
460
Traceback (most recent call last):
459
461
...
460
462
NotImplementedError: Cannot reorient ...
461
463
464
+ >>> _reorient_image(img)
465
+ Traceback (most recent call last):
466
+ ...
467
+ RuntimeError: No orientation ...
468
+
462
469
"""
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
464
479
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