bids-examples icon indicating copy to clipboard operation
bids-examples copied to clipboard

Add example code for removing data from imaging files while retaining headers

Open tsalo opened this issue 1 month ago • 1 comments

This seems more useful to me than having 0B files.

@effigies showed me how to do this for NIfTIs (see Python code below) but we should have documentation on how to do it for a range of common imaging filetypes in the CONTRIBUTING file.

import io
import gzip
from pathlib import Path

import nibabel as nb

path = Path('/path/to/example/dset').resolve()
for file in path.rglob('*.nii.gz'):
    img = nb.load(file)
    bio = io.BytesIO()
    img.header.write_to(bio)
    header = bio.getvalue()
    # Write gzipped header for .nii.gz files
    with gzip.open(file, 'wb') as f:
        f.write(header)

tsalo avatar Oct 31 '25 15:10 tsalo

Even easier:

for file in path.rglob('*.nii.gz'):
    img = nb.load(file)
    # Write gzipped header for .nii.gz files
    with gzip.open(file, 'wb') as f:
        img.header.write_to(f)

effigies avatar Oct 31 '25 15:10 effigies