|
29 | 29 | from nibabel import cifti2 as ci
|
30 | 30 | import numpy as np
|
31 | 31 | from nilearn.image import resample_to_img
|
32 |
| - |
33 | 32 | from nipype.utils.filemanip import split_filename
|
34 | 33 | from nipype.interfaces.base import (
|
35 | 34 | BaseInterfaceInputSpec,
|
|
41 | 40 | )
|
42 | 41 | import templateflow.api as tf
|
43 | 42 |
|
| 43 | +from niworkflows.interfaces.nibabel import reorient_image |
| 44 | + |
44 | 45 | CIFTI_SURFACES = ("fsaverage5", "fsaverage6", "fsLR")
|
45 | 46 | CIFTI_VOLUMES = ("MNI152NLin2009cAsym", "MNI152NLin6Asym")
|
46 | 47 | CIFTI_STRUCT_WITH_LABELS = { # CITFI structures with corresponding labels
|
@@ -356,8 +357,8 @@ def _create_cifti_image(
|
356 | 357 | bold_img = resample_to_img(bold_img, label_img)
|
357 | 358 |
|
358 | 359 | # ensure images match HCP orientation (LAS)
|
359 |
| - bold_img = _reorient_image(bold_img, orientation="LAS") |
360 |
| - label_img = _reorient_image(label_img, orientation="LAS") |
| 360 | + bold_img = reorient_image(bold_img, target_orientation="LAS") |
| 361 | + label_img = reorient_image(label_img, target_orientation="LAS") |
361 | 362 |
|
362 | 363 | bold_data = bold_img.get_fdata(dtype="float32")
|
363 | 364 | timepoints = bold_img.shape[3]
|
@@ -466,66 +467,3 @@ def _create_cifti_image(
|
466 | 467 | out_file = "{}.dtseries.nii".format(split_filename(bold_file)[1])
|
467 | 468 | ci.save(img, out_file)
|
468 | 469 | return Path.cwd() / out_file
|
469 |
| - |
470 |
| - |
471 |
| -def _reorient_image(img, *, target_img=None, orientation=None): |
472 |
| - """ |
473 |
| - Coerce an image to a target orientation. |
474 |
| -
|
475 |
| - .. note:: |
476 |
| - Only RAS -> LAS conversion is currently supported |
477 |
| -
|
478 |
| - Parameters |
479 |
| - ---------- |
480 |
| - img : :obj:`SpatialImage` |
481 |
| - image to be reoriented |
482 |
| - target_img : :obj:`SpatialImage`, optional |
483 |
| - target in desired orientation |
484 |
| - orientation : :obj:`str` or :obj:`tuple`, optional |
485 |
| - desired orientation, if no target image is provided |
486 |
| -
|
487 |
| - .. testsetup:: |
488 |
| - >>> img = nb.load(Path(test_data) / 'testSpatialNormalizationRPTMovingWarpedImage.nii.gz') |
489 |
| - >>> las_img = img.as_reoriented([[0, -1], [1, 1], [2, 1]]) |
490 |
| -
|
491 |
| - Examples |
492 |
| - -------- |
493 |
| - >>> nimg = _reorient_image(img, target_img=img) |
494 |
| - >>> nb.aff2axcodes(nimg.affine) |
495 |
| - ('R', 'A', 'S') |
496 |
| -
|
497 |
| - >>> nimg = _reorient_image(img, target_img=las_img) |
498 |
| - >>> nb.aff2axcodes(nimg.affine) |
499 |
| - ('L', 'A', 'S') |
500 |
| -
|
501 |
| - >>> nimg = _reorient_image(img, orientation='LAS') |
502 |
| - >>> nb.aff2axcodes(nimg.affine) |
503 |
| - ('L', 'A', 'S') |
504 |
| -
|
505 |
| - >>> _reorient_image(img, orientation='LPI') |
506 |
| - Traceback (most recent call last): |
507 |
| - ... |
508 |
| - NotImplementedError: Cannot reorient ... |
509 |
| -
|
510 |
| - >>> _reorient_image(img) |
511 |
| - Traceback (most recent call last): |
512 |
| - ... |
513 |
| - RuntimeError: No orientation ... |
514 |
| -
|
515 |
| - """ |
516 |
| - orient0 = nb.aff2axcodes(img.affine) |
517 |
| - if target_img is not None: |
518 |
| - orient1 = nb.aff2axcodes(target_img.affine) |
519 |
| - elif orientation is not None: |
520 |
| - orient1 = tuple(orientation) |
521 |
| - else: |
522 |
| - raise RuntimeError("No orientation to reorient to!") |
523 |
| - |
524 |
| - if orient0 == orient1: # already in desired orientation |
525 |
| - return img |
526 |
| - elif orient0 == tuple("RAS") and orient1 == tuple("LAS"): # RAS -> LAS |
527 |
| - return img.as_reoriented([[0, -1], [1, 1], [2, 1]]) |
528 |
| - else: |
529 |
| - raise NotImplementedError( |
530 |
| - "Cannot reorient {0} to {1}.".format(orient0, orient1) |
531 |
| - ) |
0 commit comments