[Neuroimaging] Reading mgz files in NiBabel 2.3.0
ts+ml at rcmd.org
ts+ml at rcmd.org
Sun Jan 6 11:16:43 EST 2019
Dear list members,
I'm trying to load a FreeSurfer MGZ file (its header to be precise) using nibabel 2.3.0:
import nibabel.freesurfer.mghformat as fsmgh
def read_mgh(mgh_file_name):
with open(mgh_file_name, 'rb') as mgh_file_handle:
header = fsmgh.MGHHeader.from_fileobj(mgh_file_handle)
...
This works fine if the file is in MGH format, but NOT if it is gzipped (MGZ format). In that case, you get the following exception in the last line:
klass = <class 'nibabel.freesurfer.mghformat.MGHHeader'>, fileobj = <closed file '/home/spirit/develop/brainload/tests/brainload/../test_data/subject1/mri/orig.mgz', mode 'rb' at 0x7f2448ef2e40>
check = True
@classmethod
def from_fileobj(klass, fileobj, check=True):
'''
classmethod for loading a MGH fileobject
'''
# We need the following hack because MGH data stores header information
# after the data chunk too. We read the header initially, deduce the
# dimensions from the header, skip over and then read the footer
# information
hdr_str = fileobj.read(klass._hdrdtype.itemsize)
hdr_str_to_np = np.ndarray(shape=(), dtype=klass._hdrdtype,
buffer=hdr_str)
if not np.all(hdr_str_to_np['dims']):
> raise MGHError('Dimensions of the data should be non-zero')
E MGHError: Dimensions of the data should be non-zero
env/local/lib/python2.7/site-packages/nibabel/freesurfer/mghformat.py:165: MGHError
So my question is: how should I read the file? Am I supposed to unpack it myself before reading, or is there some other function for this in nibabel which I overlooked?
The files in question are available here in case you want them:
- The MGZ file, it fails with this one: http://rcmd.org/tmp/orig.mgz
- The same file, manually unzipped using gunzip in the shell. The code works with this one: http://rcmd.org/tmp/orig.mgh
Thanks in advance,
--
Tim
More information about the Neuroimaging
mailing list