BIDS Validator error: TR mismatch between NIFTI header and JSON file, and BIDS Validator is somehow finding TR=0. Best solution?

The first thing I’ll note is that I don’t think you’ll have trouble running fMRIPrep on this, as is. We strictly use the TR defined in the JSON sidecar, and ignore that in the header. The validator is basically warning you that two BIDS apps that use different strategies could produce different results. So it’s still worth fixing.

I would approach this by using pybids to find all files, check if their TRs mismatch, and update individually:

from bids import grabbids
import nibabel

def set_tr(img, tr):
    header = img.header.copy()
    zooms = header.get_zooms()[:3] + (tr,)
    header.set_zooms(zooms)
    return img.__class__(img.get_data().copy(), img.affine, header)

def sync_tr(bids_root):
    layout = grabbids.BIDSLayout(bids_root)
    for nii in layout.get(extensions=['.nii', '.nii.gz']):
        metadata.= layout.get_metadata(nii.filename)
        if 'RepetitionTime' in metadata:
            img = nb.load(nii.filename)
            if img.header.get_zooms()[3:] != (metadata['RepetitionTime'],):
                fixed_img = set_tr(img, metadata['RepetitionTime'])
                fixed_img.to_filename(nii.filename)

sync_tr('CamCan')

(I would definitely make a copy of the dataset before trying this… I haven’t tested this code.)

2 Likes