Manually specifying number of volumes in bold scans that ended early

Greetings!
We have a BOLD dataset, and on one particular task (music), many of the scans were ended early; therefore, there is a mismatch between the number of volumes contained in the NIFTI header and the number of volumes actually collected. I’m (predictably) receiving an error since the workflow is expecting a larger file that it’s actually getting (shown below).

[screen capture redacted]

My question is this: when fMRIprep takes in a bold_file (looks like the documentation is here?), how precisely does it glean the number of volumes contained in the NIFTI? From the header? Basically, I need to specify a certain number of volumes to fMRIprep that is different than what is contained in the header; if fMRIprep gets this number from some field in the header, I could simply look into changing the header itself. I just wanted to verify that this is the case, and where exactly in the header it gets this information.

Note: I see that there is a skip_vols variable, but that looks like just vols at the beginning of the dataset; with our data, the vols that need to be skipped are at the end (and are at a different spot for every scan).

Thank you for your help!

This isn’t something that you’re going to specify in fMRIPrep. These files are malformed and will need to be fixed to correctly describe the amount of data available. You can do this in Python:

import numpy as np
import nibabel as nb
orig = nb.load(fname) 
header = orig.header.copy()
header['dim'][4] = ACTUAL_NUMBER_OF_VOLUMES
dataobj = nb.arrayproxy.ArrayProxy(fname, header)

Coercing to a numpy array will ensure that the amount of data described by the header is there:

data = np.array(dataobj)

Then save:

nb.save(orig.__class__(data, None, header), fname)
3 Likes

This worked beautifully–thanks so much! Hopefully our lab won’t continue this practice of ending scans early…

Hi @effigies–it turns out the result isn’t exactly what I had hoped for. Although just header[‘dim’][4] is changed, the image itself has moved in the bounding box such that a bit of the brain is cut off. In addition, the voxel intensity has scaled down by a factor of ~2.

When I compare the header info between the original and “fixed” image, only the number of volumes is changed, so I’m a bit perplexed why these other properties have changed too. Any ideas?

I may also try changing the number of volumes in Pynifti and see if that works better. Will update when I do.

Thanks.

To update on this, effigies’s above solution worked in the context of fMRIprep; although the voxel intensities and relative position within the bounding box were changed, fMRIprep corrected for these.