[Neuroimaging] [nibabel] reading corrupt *.nii.gz files

Dimitri Papadopoulos Orfanos dimitri.papadopoulos at cea.fr
Wed Sep 30 15:35:28 CEST 2015

Dear all,

The following code emits an exception:
    import nibabel
    NIFTI_FILE = 'FLAIR.nii.gz'
    img = nibabel.load(NIFTI_FILE)
    import nibabel
        data = img.get_data()
    except zlib.error as err:

The outptut is:
    Error -3 while decompressing: invalid code lengths set

The reason is that NIfTI file FLAIR.nii.gz is corrupted:
    $ gunzip

    gzip: FLAIR.nii.gz: invalid compressed data--format violated

While in many such situations throwing an exception is the right thing
to do, there are cases where I would like to override the error and read
whatever data is available. For example it could be useful to be able to
display the image the same way FSLView does (see
http://www.pictureshack.us/images/10675_FLAIR.png). Also please note
that PyNIfTI does read the corrupted file without raising an error.

We have hundreds such corrupted FLAIR files, probably due to a bug in
older versions of dcm2nii.

So here are my questions:
* Shouldn't ninabel catch the zlib exception and raise its own (more
user-friendly) exception?
* Is there a way to avoid the exception and read whatever data is
available in the corrupted file?
* If there is currently no way to avoid the exception, would it be
acceptable to add such an option to nibabel?


More information about the Neuroimaging mailing list