Issue concatenating nifti files with nibabel and nilearn

Hey all! I am trying to concatenate a bunch (n=133) of images in nibabel. I keep getting an error:

nibabel.spatialimages.HeaderDataError: shape (64, 64, 32, 47025) does not fit in dim datatype

does this mean I have a image somewhere that has different dimensions? I am not understanding what this error is telling me.

You’ll need to save the concatenated image as nifti2.

nifti1 uses signed 16-bit integers, meaning the maximum it can go in any dimension is 32767, while you apparently have 47025 volumes. nifti2 uses 64-bit integers, which ought to be enough for anybody.

See https://nifti.nimh.nih.gov/ for reference on the two formats.

Hey effigies! Thanks for the reply. I changed my output to save as a nifti2 file. However I am getting the same error.

nibabel.spatialimages.HeaderDataError: shape (64, 64, 32, 47025) does not fit in dim datatype

Here is the command I am trying to run:

output= concat_imgs(all_func[1:100], memory_level=0, auto_resample=True, verbose=1)
outfile=os.path.join(basepath,'concatenated_imagine_gtest.nii.gz')
nib.nifti2.save(output,outfile) 

It looks like it concatenates till the 99th, then fails. Is there a different way to save it as a nifti2?

I would try:

import nibabel as nb

ni1_img = nb.concat_imgs(...)
ni2_img = nb.Nifti2Image.from_image(ni1_img)
ni2_img.to_filename(...)

I know nibabel has some ways of implicitly converting images, but I prefer to be explicit.

Hey again, new day same issue. Here is the code I ran:

output=nib.Nifti2Image.from_image(nib.concat_images(all_func[0:70],check_affines=False, axis=3))
outfile=os.path.join(basepath,'concatenated_imagine_gtest.nii')
print("trying to save")
output.to_filename(outfile)

I am getting the following output

Traceback (most recent call last):
  File "imagine_concatenate.py", line 26, in <module>
    output0= concat_imgs(all_func[0:70], memory_level=0, auto_resample=True, verbose=1)
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nilearn/_utils/niimg_conversions.py", line 476, in concat_niimgs
    return new_img_like(first_niimg, data, first_niimg.affine, copy_header=True)
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nilearn/image/image.py", line 646, in new_img_like
    return klass(data, affine, header=header)
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nibabel/nifti1.py", line 1762, in __init__
    file_map)
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nibabel/analyze.py", line 923, in __init__
    dataobj, affine, header, extra, file_map)
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nibabel/spatialimages.py", line 373, in __init__
    self.update_header()
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nibabel/nifti1.py", line 2039, in update_header
    super(Nifti1Image, self).update_header()
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nibabel/nifti1.py", line 1798, in update_header
    super(Nifti1Pair, self).update_header()
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nibabel/spatialimages.py", line 400, in update_header
    hdr.set_data_shape(shape)
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nibabel/nifti1.py", line 882, in set_data_shape
    super(Nifti1Header, self).set_data_shape(shape)
  File "/usr/share/Modules/software/CentOS-7/python_modules/2.7/lib/python2.7/site-packages/nibabel/analyze.py", line 637, in set_data_shape
    (shape,))
nibabel.spatialimages.HeaderDataError: shape (64, 64, 32, 33250) does not fit in dim datatype

Any ideas? I tried it on 51 images and it worked…
Grace

Oh, I see. It’s complaining about creating the Nifti1Image in memory before you can create the Nifti2Image. That makes sense.

The simplest approach will be the following:

# Generator so hopefully you don't have to load everything into memory all at once
ni2_funcs = (nib.Nifti2Image.from_image(nib.load(func)) for func in all_func)
ni2_concat = nib.concat_images(ni2_funcs, check_affines=False, axis=3)
ni2_concat.to_filename(outfile)

If concat_images complains about the generator, you can just change the parentheses to square braces to make a list.

Yippeee!
It worked! Many thanks and good wishes!
Grace