@@ -1279,45 +1279,37 @@ def set_data_dtype(self, dtype):
1279
1279
def get_shape (self ):
1280
1280
return self ._data .shape
1281
1281
1282
- @staticmethod
1283
- def _get_open_files (file_map , mode = 'rb' ):
1284
- ''' Utility method to open necessary files for read/write
1285
-
1286
- This method is to allow for formats (nifti single in particular)
1287
- that may have the same file for header and image
1288
- '''
1289
- hdrf = file_map ['header' ].get_prepare_fileobj (mode = mode )
1290
- imgf = file_map ['image' ].get_prepare_fileobj (mode = mode )
1291
- return hdrf , imgf
1292
-
1293
- def _close_filenames (self , file_map , hdrf , imgf ):
1294
- ''' Utility method to close any files no longer required
1295
-
1296
- Called by the image writing routines.
1297
-
1298
- This method is to allow for formats (nifti single in particular)
1299
- that may have the same file for header and image
1300
- '''
1301
- if file_map ['header' ].fileobj is None : # was filename
1302
- hdrf .close ()
1303
- if file_map ['image' ].fileobj is None : # was filename
1304
- imgf .close ()
1305
-
1306
1282
@classmethod
1307
1283
def from_file_map (klass , file_map ):
1308
1284
''' class method to create image from mapping in `file_map ``
1309
1285
'''
1310
- hdrf , imgf = klass ._get_open_files (file_map , 'rb' )
1286
+ hdr_fh , img_fh = klass ._get_fileholders (file_map )
1287
+ hdrf = hdr_fh .get_prepare_fileobj (mode = 'rb' )
1311
1288
header = klass .header_class .from_fileobj (hdrf )
1289
+ if hdr_fh .fileobj is None : # was filename
1290
+ hdrf .close ()
1312
1291
affine = header .get_best_affine ()
1313
1292
hdr_copy = header .copy ()
1293
+ imgf = img_fh .fileobj
1294
+ if imgf is None :
1295
+ imgf = img_fh .filename
1314
1296
data = klass .ImageArrayProxy (imgf , hdr_copy )
1315
1297
img = klass (data , affine , header , file_map = file_map )
1316
1298
img ._load_cache = {'header' : hdr_copy ,
1317
1299
'affine' : affine .copy (),
1318
1300
'file_map' : copy_file_map (file_map )}
1319
1301
return img
1320
1302
1303
+ @staticmethod
1304
+ def _get_fileholders (file_map ):
1305
+ """ Return fileholder for header and image
1306
+
1307
+ Allows single-file image types to return one fileholder for both types.
1308
+ For Analyze there are two fileholders, one for the header, one for the
1309
+ image.
1310
+ """
1311
+ return file_map ['header' ], file_map ['image' ]
1312
+
1321
1313
def _write_header (self , header_file , header , slope , inter ):
1322
1314
''' Utility routine to write header
1323
1315
@@ -1384,10 +1376,15 @@ def to_file_map(self, file_map=None):
1384
1376
self .update_header ()
1385
1377
hdr = self .get_header ()
1386
1378
slope , inter , mn , mx = hdr .scaling_from_data (data )
1387
- hdrf , imgf = self ._get_open_files (file_map , 'wb' )
1379
+ hdr_fh , img_fh = self ._get_fileholders (file_map )
1380
+ hdrf = hdr_fh .get_prepare_fileobj (mode = 'wb' )
1381
+ imgf = img_fh .get_prepare_fileobj (mode = 'wb' )
1388
1382
self ._write_header (hdrf , hdr , slope , inter )
1389
1383
self ._write_image (imgf , data , hdr , slope , inter , mn , mx )
1390
- self ._close_filenames (file_map , hdrf , imgf )
1384
+ if hdr_fh .fileobj is None : # was filename
1385
+ hdrf .close ()
1386
+ if img_fh .fileobj is None : # was filename
1387
+ imgf .close ()
1391
1388
self ._header = hdr
1392
1389
self .file_map = file_map
1393
1390
0 commit comments