17
17
_ensure_trans , read_ras_mni_t , Transform )
18
18
from .surface import read_surface , _read_mri_surface
19
19
from .utils import (verbose , _validate_type , _check_fname , _check_option ,
20
- get_subjects_dir , _require_version , logger )
20
+ get_subjects_dir , _import_nibabel , logger )
21
21
22
22
23
23
def _check_subject_dir (subject , subjects_dir ):
@@ -33,9 +33,7 @@ def _check_subject_dir(subject, subjects_dir):
33
33
34
34
def _get_aseg (aseg , subject , subjects_dir ):
35
35
"""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' )
39
37
subjects_dir = Path (get_subjects_dir (subjects_dir , raise_error = True ))
40
38
if not aseg .endswith ('aseg' ):
41
39
raise RuntimeError (
@@ -50,18 +48,6 @@ def _get_aseg(aseg, subject, subjects_dir):
50
48
return aseg , aseg_data
51
49
52
50
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
-
65
51
def _reorient_image (img , axcodes = 'RAS' ):
66
52
"""Reorient an image to a given orientation.
67
53
@@ -84,7 +70,7 @@ def _reorient_image(img, axcodes='RAS'):
84
70
-----
85
71
.. versionadded:: 0.24
86
72
"""
87
- import nibabel as nib
73
+ nib = _import_nibabel ( 'reorient MRI image' )
88
74
orig_data = np .array (img .dataobj ).astype (np .float32 )
89
75
# reorient data to RAS
90
76
ornt = nib .orientations .axcodes2ornt (
@@ -229,8 +215,7 @@ def get_volume_labels_from_aseg(mgz_fname, return_colors=False,
229
215
230
216
.. versionadded:: 0.9.0
231
217
"""
232
- import nibabel as nib
233
-
218
+ nib = _import_nibabel ('load MRI atlas data' )
234
219
mgz_fname = _check_fname (
235
220
mgz_fname , overwrite = "read" , must_exist = True , name = "mgz_fname"
236
221
)
@@ -486,7 +471,7 @@ def estimate_head_mri_t(subject, subjects_dir=None, verbose=None):
486
471
487
472
def _ensure_image_in_surface_RAS (image , subject , subjects_dir ):
488
473
"""Check if the image is in Freesurfer surface RAS space."""
489
- import nibabel as nib
474
+ nib = _import_nibabel ( 'load a volume image' )
490
475
if not isinstance (image , nib .spatialimages .SpatialImage ):
491
476
image = nib .load (image )
492
477
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):
614
599
# This is equivalent but 100x slower, so only use nibabel if we need to
615
600
# (later):
616
601
if use_nibabel :
617
- import nibabel
618
- hdr = nibabel .load (path ).header
602
+ nib = _import_nibabel ()
603
+ hdr = nib .load (path ).header
619
604
n_orig = hdr .get_vox2ras ()
620
605
t_orig = hdr .get_vox2ras_tkr ()
621
606
dims = hdr .get_data_shape ()
0 commit comments