Skip to content

Instantly share code, notes, and snippets.

@MarcCote
Last active December 2, 2022 12:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MarcCote/ea6842cc4c3950f7596fc3c8a0be0154 to your computer and use it in GitHub Desktop.
Save MarcCote/ea6842cc4c3950f7596fc3c8a0be0154 to your computer and use it in GitHub Desktop.
Script that converts TRK to TCK using https://github.com/MarcCote/nibabel/tree/streamlines_tck
import os
import argparse
import nibabel as nib
def build_argparser():
DESCRIPTION = "Convert tractograms (TRK -> TCK)."
p = argparse.ArgumentParser(description=DESCRIPTION)
p.add_argument('tractograms', metavar='bundle', nargs="+", help='list of tractograms.')
p.add_argument('-f', '--force', action="store_true", help='overwrite existing output files.')
return p
def main():
parser = build_argparser()
args = parser.parse_args()
for tractogram in args.tractograms:
if nib.streamlines.detect_format(tractogram) is not nib.streamlines.TrkFile:
print("Skipping non TRK file: '{}'".format(tractogram))
continue
output_filename = tractogram[:-4] + '.tck'
if os.path.isfile(output_filename) and not args.force:
print("Skipping existing file: '{}'. Use -f to overwrite.".format(output_filename))
continue
trk = nib.streamlines.load(tractogram)
nib.streamlines.save(trk.tractogram, output_filename)
if __name__ == '__main__':
main()
@russellu
Copy link

russellu commented Jan 6, 2019

thanks for this! works perfectly, and very useful (i needed to get my .trk files into .tck so i could use mrtrix's tracks2prob)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment