Skip to content

Instantly share code, notes, and snippets.

@ThomasHMAC
Forked from MarcCote/tck2trk.py
Created May 4, 2020 21:08
Show Gist options
  • Save ThomasHMAC/2c7f6575c066b5ca61e61dc57bf522ff to your computer and use it in GitHub Desktop.
Save ThomasHMAC/2c7f6575c066b5ca61e61dc57bf522ff to your computer and use it in GitHub Desktop.
Script that converts TCK to TRK using https://github.com/MarcCote/nibabel/tree/streamlines_tck
import os
import argparse
import nibabel as nib
from nibabel.streamlines import Field
from nibabel.orientations import aff2axcodes
def build_argparser():
DESCRIPTION = "Convert tractograms (TCK -> TRK)."
p = argparse.ArgumentParser(description=DESCRIPTION)
p.add_argument('anatomy', help='reference anatomy (.nii|.nii.gz.')
p.add_argument('tractograms', metavar='tractogram', nargs="+", help='list of tractograms (.tck).')
p.add_argument('-f', '--force', action="store_true", help='overwrite existing output files.')
return p
def main():
parser = build_argparser()
args = parser.parse_args()
try:
nii = nib.load(args.anatomy)
except:
parser.error("Expecting anatomy image as first agument.")
for tractogram in args.tractograms:
if nib.streamlines.detect_format(tractogram) is not nib.streamlines.TckFile:
print("Skipping non TCK file: '{}'".format(tractogram))
continue
output_filename = tractogram[:-4] + '.trk'
if os.path.isfile(output_filename) and not args.force:
print("Skipping existing file: '{}'. Use -f to overwrite.".format(output_filename))
continue
header = {}
header[Field.VOXEL_TO_RASMM] = nii.affine.copy()
header[Field.VOXEL_SIZES] = nii.header.get_zooms()[:3]
header[Field.DIMENSIONS] = nii.shape[:3]
header[Field.VOXEL_ORDER] = "".join(aff2axcodes(nii.affine))
tck = nib.streamlines.load(tractogram)
nib.streamlines.save(tck.tractogram, output_filename, header=header)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment