(strange) gzip/nibabel error

276 views
Skip to first unread message

Cris Lanting

unread,
May 12, 2014, 4:54:55 AM5/12/14
to nipy...@googlegroups.com
Hi All,

Not sure whether I'm doing something wrong here but I get an gzip error with gzipped nii files (*.nii.gz). I've been testing the scripts locally (mac osx) and I want to run them on a (heavy duty) linux system. It seems that the file are ok - fslinfo does not complain, and it works fine with decompressed files. Could it be a endian-like error or am I missing something?

For completeness here's a minimal example that triggers the error:

import os                                    
import os.path as op
import numpy as np
import nibabel as nb

file = 'rs1.nii.gz'
img = nb.load(file)

Results in: 

In [23]: img = nb.load(file)
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
/data/local_dir/tamac/Connectivity/n3806/<ipython-input-23-592347cf629c> in <module>()
----> 1 img = nb.load(file)

/usr/lib/pymodules/python2.7/nibabel/loadsave.pyc in load(filename)
     52         else:
     53             klass =  spm2.Spm2AnalyzeImage
---> 54     return klass.from_filename(filename)
     55 
     56 

/usr/lib/pymodules/python2.7/nibabel/spatialimages.pyc in from_filename(klass, filename)
    409     def from_filename(klass, filename):
    410         file_map = klass.filespec_to_file_map(filename)
--> 411         return klass.from_file_map(file_map)
    412 
    413     @classmethod

/usr/lib/pymodules/python2.7/nibabel/analyze.pyc in from_file_map(klass, file_map)
    868         hdr_fh, img_fh = klass._get_fileholders(file_map)
    869         hdrf = hdr_fh.get_prepare_fileobj(mode='rb')
--> 870         header = klass.header_class.from_fileobj(hdrf)
    871         if hdr_fh.fileobj is None: # was filename
    872             hdrf.close()

/usr/lib/pymodules/python2.7/nibabel/nifti1.pyc in from_fileobj(klass, fileobj, endianness, check)
    566     @classmethod
    567     def from_fileobj(klass, fileobj, endianness=None, check=True):
--> 568         raw_str = fileobj.read(klass.template_dtype.itemsize)
    569         hdr = klass(raw_str, endianness, check)
    570         # Read next 4 bytes to see if we have extensions.  The nifti standard


/usr/lib/python2.7/gzip.pyc in read(self, size)
    254             try:
    255                 while size > self.extrasize:
--> 256                     self._read(readsize)
    257                     readsize = min(self.max_read_chunk, readsize * 2)
    258             except EOFError:

/usr/lib/python2.7/gzip.pyc in _read(self, size)
    289 
    290             self._init_read()
--> 291             self._read_gzip_header()
    292             self.decompress = zlib.decompressobj(-zlib.MAX_WBITS)
    293             self._new_member = False

/usr/lib/python2.7/gzip.pyc in _read_gzip_header(self)
    183         magic = self.fileobj.read(2)
    184         if magic != '\037\213':
--> 185             raise IOError, 'Not a gzipped file'
    186         method = ord( self.fileobj.read(1) )
    187         if method != 8:

IOError: Not a gzipped file

Any insight?

Cheers,
Cris

Arman Eshaghi

unread,
May 12, 2014, 5:10:07 AM5/12/14
to nipy...@googlegroups.com
Hi Cris,

What will happen if you give absolute path instead of a relative one? 

All the best,
Arman


--

---
You received this message because you are subscribed to the Google Groups "NiPy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nipy-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cris Lanting

unread,
May 12, 2014, 5:15:28 AM5/12/14
to nipy...@googlegroups.com
Tried that and it results in the same error I'm afraid.

Cheers,
Cris

Michael Waskom

unread,
May 18, 2014, 12:14:18 AM5/18/14
to nipy...@googlegroups.com
Try

cp file.nii.gz file.nii

And see if you can read "file.nii".

My guess is that whatever program wrote the file didn't zip it up based on the filename?

Cris Lanting

unread,
May 19, 2014, 5:11:26 AM5/19/14
to nipy...@googlegroups.com
Michael,

Tried it but doesn't work - it seems gzipped; for some reason changing the files to nifti (using fslchfiletype) however does resolve the issue.
The files were converted using dcm2nii (nii.gz) and then renamed (copied) to adhere to a datastructure '/data/subject1/file1.nii.gz' instead of
'/data/subject1file1.nii.gz' etc.

I'm still not sure what's causing the problem, but we have a work-around.

Cheers
Cris

Cris Lanting

unread,
Jun 17, 2014, 10:42:13 AM6/17/14
to nipy...@googlegroups.com
Adding to this: it seems that it is a system-specific issue, meaning that you might run into this error when converting the data on a mac osx system, transferring it to a linux server and trying to run a nipype script using nibabe on this datal. Interestingly, when converting a file in .nii.gz format to .nii and back to .nii.gz using fslchfiletype, nibabel is happy again.

Cheers,
Cris

Yaroslav Halchenko

unread,
Jun 17, 2014, 10:55:31 AM6/17/14
to nipy...@googlegroups.com
actually my wild guess is that may be those files somehow managed to be
not compressed at all (e.g. just renamed into .nii.gz from original
.nii). If there would be such a cross-platform problem -- we would have
heard about this issue long before. Do you still have access to those
files? what would be output of running 'file' command on them?

yeap -- here is my protocol and sample of errors you would get if you
try to open non-gzipped .nii.gz (your case) and actual gzipped .nii

% python -c 'import nibabel as nib; print nib.load("1.nii.gz")' | head
<class 'nibabel.nifti1.Nifti1Image'>
data shape (91, 109, 91)
affine:
[[ -2. 0. 0. 90.]
[ 0. 2. 0. -126.]
[ 0. 0. 2. -72.]
[ 0. 0. 0. 1.]]
metadata:
<class 'nibabel.nifti1.Nifti1Header'> object, endian='<'
sizeof_hdr : 348
% mv 1.nii.gz 1.nii
% python -c 'import nibabel as nib; print nib.load("1.nii")' | head
sizeof_hdr should be 348; set sizeof_hdr to 348
data code -4149 not recognized; not attempting fix
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/nibabel/loadsave.py", line 54, in load
return klass.from_filename(filename)
File "/usr/lib/pymodules/python2.7/nibabel/spatialimages.py", line 411, in from_filename
return klass.from_file_map(file_map)
File "/usr/lib/pymodules/python2.7/nibabel/analyze.py", line 870, in from_file_map
header = klass.header_class.from_fileobj(hdrf)
File "/usr/lib/pymodules/python2.7/nibabel/nifti1.py", line 569, in from_fileobj
hdr = klass(raw_str, endianness, check)
File "/usr/lib/pymodules/python2.7/nibabel/nifti1.py", line 552, in __init__
check)
File "/usr/lib/pymodules/python2.7/nibabel/analyze.py", line 248, in __init__
super(AnalyzeHeader, self).__init__(binaryblock, endianness, check)
File "/usr/lib/pymodules/python2.7/nibabel/wrapstruct.py", line 172, in __init__
self.check_fix()
File "/usr/lib/pymodules/python2.7/nibabel/wrapstruct.py", line 357, in check_fix
report.log_raise(logger, error_level)
File "/usr/lib/pymodules/python2.7/nibabel/batteryrunners.py", line 275, in log_raise
raise self.error(self.problem_msg)
nibabel.spatialimages.HeaderDataError: data code -4149 not recognized
% zcat 1.nii > 1_unzipped.nii
% python -c 'import nibabel as nib; print nib.load("1_unzipped.nii")' | head
<class 'nibabel.nifti1.Nifti1Image'>
data shape (91, 109, 91)
affine:
[[ -2. 0. 0. 90.]
[ 0. 2. 0. -126.]
[ 0. 0. 2. -72.]
[ 0. 0. 0. 1.]]
metadata:
<class 'nibabel.nifti1.Nifti1Header'> object, endian='<'
sizeof_hdr : 348
% mv 1_unzipped.nii 1_unzipped.nii.gz
% python -c 'import nibabel as nib; print nib.load("1_unzipped.nii.gz")' | head
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/nibabel/loadsave.py", line 54, in load
return klass.from_filename(filename)
File "/usr/lib/pymodules/python2.7/nibabel/spatialimages.py", line 411, in from_filename
return klass.from_file_map(file_map)
File "/usr/lib/pymodules/python2.7/nibabel/analyze.py", line 870, in from_file_map
header = klass.header_class.from_fileobj(hdrf)
File "/usr/lib/pymodules/python2.7/nibabel/nifti1.py", line 568, in from_fileobj
raw_str = fileobj.read(klass.template_dtype.itemsize)
File "/usr/lib/python2.7/gzip.py", line 261, in read
self._read(readsize)
File "/usr/lib/python2.7/gzip.py", line 296, in _read
self._read_gzip_header()
File "/usr/lib/python2.7/gzip.py", line 190, in _read_gzip_header
raise IOError, 'Not a gzipped file'
IOError: Not a gzipped file
% file 1_unzipped.nii.gz 1.nii
1_unzipped.nii.gz: data
1.nii: gzip compressed data, from Unix
Yaroslav O. Halchenko, Ph.D.
http://neuro.debian.net http://www.pymvpa.org http://www.fail2ban.org
Research Scientist, Psychological and Brain Sciences Dept.
Dartmouth College, 419 Moore Hall, Hinman Box 6207, Hanover, NH 03755
Phone: +1 (603) 646-9834 Fax: +1 (603) 646-1419
WWW: http://www.linkedin.com/in/yarik

Cris Lanting

unread,
Jun 17, 2014, 12:34:09 PM6/17/14
to nipy...@googlegroups.com
Ha! Of course...thanks Yaroslav!

I think the error is human - I think one of my student just renamed the files after conversion using MRICron (with setting *nii* instead of *nii.gz)....
Interesting to see that apparently fslchfiletype handles this difference quite well!

Cheers,
Cris
Reply all
Reply to author
Forward
0 new messages