1717 _ensure_trans , read_ras_mni_t , Transform )
1818from .surface import read_surface , _read_mri_surface
1919from .utils import (verbose , _validate_type , _check_fname , _check_option ,
20- get_subjects_dir , _require_version , logger )
20+ get_subjects_dir , _import_nibabel , logger )
2121
2222
2323def _check_subject_dir (subject , subjects_dir ):
@@ -33,9 +33,7 @@ def _check_subject_dir(subject, subjects_dir):
3333
3434def _get_aseg (aseg , subject , subjects_dir ):
3535 """Check that the anatomical segmentation file exists and load it."""
36- _require_version ('nibabel' , 'load aseg' , '2.1.0' )
37- import nibabel as nib
38-
36+ nib = _import_nibabel ('load aseg' )
3937 subjects_dir = Path (get_subjects_dir (subjects_dir , raise_error = True ))
4038 if not aseg .endswith ('aseg' ):
4139 raise RuntimeError (
@@ -50,18 +48,6 @@ def _get_aseg(aseg, subject, subjects_dir):
5048 return aseg , aseg_data
5149
5250
53- def _import_nibabel (why = 'use MRI files' ):
54- try :
55- import nibabel as nib
56- except ImportError as exp :
57- msg = 'nibabel is required to %s, got:\n %s' % (why , exp )
58- else :
59- msg = ''
60- if msg :
61- raise ImportError (msg )
62- return nib
63-
64-
6551def _reorient_image (img , axcodes = 'RAS' ):
6652 """Reorient an image to a given orientation.
6753
@@ -84,7 +70,7 @@ def _reorient_image(img, axcodes='RAS'):
8470 -----
8571 .. versionadded:: 0.24
8672 """
87- import nibabel as nib
73+ nib = _import_nibabel ( 'reorient MRI image' )
8874 orig_data = np .array (img .dataobj ).astype (np .float32 )
8975 # reorient data to RAS
9076 ornt = nib .orientations .axcodes2ornt (
@@ -229,8 +215,7 @@ def get_volume_labels_from_aseg(mgz_fname, return_colors=False,
229215
230216 .. versionadded:: 0.9.0
231217 """
232- import nibabel as nib
233-
218+ nib = _import_nibabel ('load MRI atlas data' )
234219 mgz_fname = _check_fname (
235220 mgz_fname , overwrite = "read" , must_exist = True , name = "mgz_fname"
236221 )
@@ -486,7 +471,7 @@ def estimate_head_mri_t(subject, subjects_dir=None, verbose=None):
486471
487472def _ensure_image_in_surface_RAS (image , subject , subjects_dir ):
488473 """Check if the image is in Freesurfer surface RAS space."""
489- import nibabel as nib
474+ nib = _import_nibabel ( 'load a volume image' )
490475 if not isinstance (image , nib .spatialimages .SpatialImage ):
491476 image = nib .load (image )
492477 image = nib .MGHImage (image .dataobj .astype (np .float32 ), image .affine )
@@ -614,8 +599,8 @@ def _read_mri_info(path, units='m', return_img=False, use_nibabel=False):
614599 # This is equivalent but 100x slower, so only use nibabel if we need to
615600 # (later):
616601 if use_nibabel :
617- import nibabel
618- hdr = nibabel .load (path ).header
602+ nib = _import_nibabel ()
603+ hdr = nib .load (path ).header
619604 n_orig = hdr .get_vox2ras ()
620605 t_orig = hdr .get_vox2ras_tkr ()
621606 dims = hdr .get_data_shape ()
0 commit comments