1010""" Utilities to load and save image objects """
1111
1212import numpy as np
13+ import warnings
1314
14- from .filename_parser import types_filenames , splitext_addext
15+ from .filename_parser import splitext_addext
1516from .openers import ImageOpener
16- from .analyze import AnalyzeImage
17- from .spm2analyze import Spm2AnalyzeImage
18- from .nifti1 import Nifti1Image , Nifti1Pair , header_dtype as ni1_hdr_dtype
19- from .nifti2 import Nifti2Image , Nifti2Pair
20- from .minc1 import Minc1Image
21- from .minc2 import Minc2Image
22- from .freesurfer import MGHImage
2317from .spatialimages import ImageFileError
24- from .imageclasses import class_map , ext_map
18+ from .imageclasses import all_image_classes
2519from .arrayproxy import is_proxy
2620
2721
@@ -40,9 +34,17 @@ def load(filename, **kwargs):
4034 img : ``SpatialImage``
4135 Image of guessed type
4236 '''
43- return guessed_image_type (filename ).from_filename (filename , ** kwargs )
37+ sniff = None
38+ for image_klass in all_image_classes :
39+ is_valid , sniff = image_klass .path_maybe_image (filename , sniff )
40+ if is_valid :
41+ return image_klass .from_filename (filename , ** kwargs )
4442
43+ raise ImageFileError ('Cannot work out file type of "%s"' %
44+ filename )
4545
46+
47+ @np .deprecate
4648def guessed_image_type (filename ):
4749 """ Guess image type from file `filename`
4850
@@ -56,39 +58,16 @@ def guessed_image_type(filename):
5658 image_class : class
5759 Class corresponding to guessed image type
5860 """
59- froot , ext , trailing = splitext_addext (filename , ('.gz' , '.bz2' ))
60- lext = ext .lower ()
61- try :
62- img_type = ext_map [lext ]
63- except KeyError :
64- raise ImageFileError ('Cannot work out file type of "%s"' %
65- filename )
66- if lext in ('.mgh' , '.mgz' , '.par' ):
67- klass = class_map [img_type ]['class' ]
68- elif lext == '.mnc' :
69- # Look for HDF5 signature for MINC2
70- # https://www.hdfgroup.org/HDF5/doc/H5.format.html
71- with ImageOpener (filename ) as fobj :
72- signature = fobj .read (4 )
73- klass = Minc2Image if signature == b'\211 HDF' else Minc1Image
74- elif lext == '.nii' :
75- with ImageOpener (filename ) as fobj :
76- binaryblock = fobj .read (348 )
77- ft = which_analyze_type (binaryblock )
78- klass = Nifti2Image if ft == 'nifti2' else Nifti1Image
79- else : # might be nifti 1 or 2 pair or analyze of some sort
80- files_types = (('image' , '.img' ), ('header' , '.hdr' ))
81- filenames = types_filenames (filename , files_types )
82- with ImageOpener (filenames ['header' ]) as fobj :
83- binaryblock = fobj .read (348 )
84- ft = which_analyze_type (binaryblock )
85- if ft == 'nifti2' :
86- klass = Nifti2Pair
87- elif ft == 'nifti1' :
88- klass = Nifti1Pair
89- else :
90- klass = Spm2AnalyzeImage
91- return klass
61+ warnings .warn ('guessed_image_type is deprecated' , DeprecationWarning ,
62+ stacklevel = 2 )
63+ sniff = None
64+ for image_klass in all_image_classes :
65+ is_valid , sniff = image_klass .path_maybe_image (filename , sniff )
66+ if is_valid :
67+ return image_klass
68+
69+ raise ImageFileError ('Cannot work out file type of "%s"' %
70+ filename )
9271
9372
9473def save (img , filename ):
@@ -105,25 +84,38 @@ def save(img, filename):
10584 -------
10685 None
10786 '''
87+
88+ # Save the type as expected
10889 try :
10990 img .to_filename (filename )
11091 except ImageFileError :
11192 pass
11293 else :
11394 return
95+
96+ # Be nice to users by making common implicit conversions
11497 froot , ext , trailing = splitext_addext (filename , ('.gz' , '.bz2' ))
98+ lext = ext .lower ()
99+
115100 # Special-case Nifti singles and Pairs
116- if type (img ) == Nifti1Image and ext in ('.img' , '.hdr' ):
101+ # Inline imports, as this module really shouldn't reference any image type
102+ from .nifti1 import Nifti1Image , Nifti1Pair
103+ from .nifti2 import Nifti2Image , Nifti2Pair
104+ if type (img ) == Nifti1Image and lext in ('.img' , '.hdr' ):
117105 klass = Nifti1Pair
118- elif type (img ) == Nifti2Image and ext in ('.img' , '.hdr' ):
106+ elif type (img ) == Nifti2Image and lext in ('.img' , '.hdr' ):
119107 klass = Nifti2Pair
120- elif type (img ) == Nifti1Pair and ext == '.nii' :
108+ elif type (img ) == Nifti1Pair and lext == '.nii' :
121109 klass = Nifti1Image
122- elif type (img ) == Nifti2Pair and ext == '.nii' :
110+ elif type (img ) == Nifti2Pair and lext == '.nii' :
123111 klass = Nifti2Image
124- else :
125- img_type = ext_map [ext ]
126- klass = class_map [img_type ]['class' ]
112+ else : # arbitrary conversion
113+ valid_klasses = [klass for klass in all_image_classes
114+ if ext in klass .valid_exts ]
115+ if not valid_klasses : # if list is empty
116+ raise ImageFileError ('Cannot work out file type of "%s"' %
117+ filename )
118+ klass = valid_klasses [0 ]
127119 converted = klass .from_image (img )
128120 converted .to_filename (filename )
129121
@@ -214,6 +206,7 @@ def read_img_data(img, prefer='scaled'):
214206 return hdr .raw_data_from_fileobj (fileobj )
215207
216208
209+ @np .deprecate
217210def which_analyze_type (binaryblock ):
218211 """ Is `binaryblock` from NIfTI1, NIfTI2 or Analyze header?
219212
@@ -241,13 +234,16 @@ def which_analyze_type(binaryblock):
241234 * if ``sizeof_hdr`` is 348 or byteswapped 348 assume Analyze
242235 * Return None
243236 """
244- hdr = np .ndarray (shape = (), dtype = ni1_hdr_dtype , buffer = binaryblock )
245- bs_hdr = hdr .byteswap ()
246- sizeof_hdr = hdr ['sizeof_hdr' ]
247- bs_sizeof_hdr = bs_hdr ['sizeof_hdr' ]
237+ warnings .warn ('which_analyze_type is deprecated' , DeprecationWarning ,
238+ stacklevel = 2 )
239+ from .nifti1 import header_dtype
240+ hdr_struct = np .ndarray (shape = (), dtype = header_dtype , buffer = binaryblock )
241+ bs_hdr_struct = hdr_struct .byteswap ()
242+ sizeof_hdr = hdr_struct ['sizeof_hdr' ]
243+ bs_sizeof_hdr = bs_hdr_struct ['sizeof_hdr' ]
248244 if 540 in (sizeof_hdr , bs_sizeof_hdr ):
249245 return 'nifti2'
250- if hdr ['magic' ] in (b'ni1' , b'n+1' ):
246+ if hdr_struct ['magic' ] in (b'ni1' , b'n+1' ):
251247 return 'nifti1'
252248 if 348 in (sizeof_hdr , bs_sizeof_hdr ):
253249 return 'analyze'
0 commit comments