Skip to content

Commit b960228

Browse files
committed
Merge pull request #89 from matthew-brett/mgh-mgz-refactor
RF - refactor ability to load compressed .mgz file
2 parents 4cbd963 + 9aea1d5 commit b960228

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

nibabel/freesurfer/mghformat.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
Author: Krish Subramaniam
1212
'''
13+
from os.path import splitext
1314
import numpy as np
1415

1516
from nibabel.volumeutils import allopen, array_to_file, array_from_file, \
@@ -427,7 +428,7 @@ def writeftr_to(self, fileobj):
427428
class MGHImage(SpatialImage):
428429
header_class = MGHHeader
429430
files_types = (('image', '.mgh'),)
430-
_compressed_exts = ('.mgz',)
431+
_compressed_exts = (('.gz',))
431432

432433
ImageArrayProxy = ArrayProxy
433434

@@ -437,24 +438,10 @@ def get_header(self):
437438

438439
@classmethod
439440
def filespec_to_file_map(klass, filespec):
440-
''' Method reimplemented from nibabel.spatialimages because it does not
441-
allow enforce_extensions in types_filenames() to be False.
442-
This is needed because MGH's compressed format is not filename.mgh.gz,
443-
but it is filename.mgz.. types_filenames() removes the .mgz and gives
444-
an error because it then doesn't find the file 'filename' in the system
445-
'''
446-
try:
447-
filenames = types_filenames(filespec,
448-
klass.files_types,
449-
trailing_suffixes=klass._compressed_exts,
450-
enforce_extensions=False)
451-
except TypesFilenamesError:
452-
raise ImageFileError('Filespec "%s" does not look right for '
453-
'class %s ' % (filespec, klass))
454-
file_map = {}
455-
for key, fname in filenames.items():
456-
file_map[key] = FileHolder(filename=fname)
457-
return file_map
441+
""" Check for compressed .mgz format, then .mgh format """
442+
if splitext(filespec)[1] == '.mgz':
443+
return dict(image=FileHolder(filename=filespec))
444+
return super(MGHImage, klass).filespec_to_file_map(filespec)
458445

459446
@classmethod
460447
def from_file_map(klass, file_map):

nibabel/freesurfer/tests/test_mghformat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,20 @@ def bad_dtype_mgh():
122122
def test_bad_dtype_mgh():
123123
# Now test the above function
124124
assert_raises(MGHError, bad_dtype_mgh)
125+
126+
127+
def test_filename_exts():
128+
# Test acceptable filename extensions
129+
v = np.ones((7, 13, 3, 22)).astype(np.uint8)
130+
# form a MGHImage object using data
131+
# and the default affine matrix (Note the "None")
132+
img = MGHImage(v, None)
133+
# Check if these extensions allow round trip
134+
for ext in ('.mgh', '.mgz', '.mgh.gz'):
135+
with InTemporaryDirectory():
136+
fname = 'tmpname' + ext
137+
save(img, fname)
138+
# read from the tmp file and see if it checks out
139+
img_back = load(fname)
140+
assert_array_equal(img_back.get_data(), v)
141+
del img_back

0 commit comments

Comments
 (0)