Last active
June 16, 2022 07:51
-
-
Save MarcCote/ebf0ae83a9202eb96d1dfbb949cd98af to your computer and use it in GitHub Desktop.
Script that converts TCK to TRK using https://github.com/MarcCote/nibabel/tree/streamlines_tck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Hi MarcCote,
Thank you for the suggestion. I have tried again and both are seemed to provide consistent output. This time I was able to show my track generated from ROIs after couples of twist on trackvis GUI as opposed to whole-brain tractography.
Thanks again,
Thomas