-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #606 from MarcCote/enh_conversion_scripts
ENH: adding TRK <=> TCK streamlines conversion scripts
- Loading branch information
Showing
6 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!python | ||
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
# vi: set ft=python sts=4 ts=4 sw=4 et: | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
# | ||
# See COPYING file distributed along with the NiBabel package for the | ||
# copyright and license terms. | ||
# | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
""" | ||
Convert tractograms (TCK -> TRK). | ||
""" | ||
|
||
from nibabel.cmdline.tck2trk import main | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!python | ||
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
# vi: set ft=python sts=4 ts=4 sw=4 et: | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
# | ||
# See COPYING file distributed along with the NiBabel package for the | ||
# copyright and license terms. | ||
# | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
""" | ||
Convert tractograms (TRK -> TCK). | ||
""" | ||
|
||
from nibabel.cmdline.trk2tck import main | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
Convert tractograms (TCK -> TRK). | ||
""" | ||
import os | ||
import argparse | ||
|
||
import nibabel as nib | ||
|
||
from nibabel.streamlines import Field | ||
from nibabel.orientations import aff2axcodes | ||
|
||
|
||
def parse_args(): | ||
DESCRIPTION = "Convert tractograms (TCK -> TRK)." | ||
parser = argparse.ArgumentParser(description=DESCRIPTION) | ||
parser.add_argument("anatomy", | ||
help="reference anatomical image (.nii|.nii.gz.") | ||
parser.add_argument("tractograms", metavar="tractogram", nargs="+", | ||
help="list of tractograms (.tck).") | ||
parser.add_argument("-f", "--force", action="store_true", | ||
help="overwrite existing output files.") | ||
|
||
args = parser.parse_args() | ||
return args, parser | ||
|
||
|
||
def main(): | ||
args, parser = parse_args() | ||
|
||
try: | ||
nii = nib.load(args.anatomy) | ||
except Exception: | ||
parser.error("Expecting anatomical image as first agument.") | ||
|
||
for tractogram in args.tractograms: | ||
tractogram_format = nib.streamlines.detect_format(tractogram) | ||
if tractogram_format is not nib.streamlines.TckFile: | ||
print("Skipping non TCK file: '{}'".format(tractogram)) | ||
continue | ||
|
||
filename, _ = os.path.splitext(tractogram) | ||
output_filename = filename + '.trk' | ||
if os.path.isfile(output_filename) and not args.force: | ||
msg = "Skipping existing file: '{}'. Use -f to overwrite." | ||
print(msg.format(output_filename)) | ||
continue | ||
|
||
# Build header using infos from the anatomical image. | ||
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) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Convert tractograms (TRK -> TCK). | ||
""" | ||
|
||
import os | ||
import argparse | ||
|
||
import nibabel as nib | ||
|
||
|
||
def parse_args(): | ||
DESCRIPTION = "Convert tractograms (TRK -> TCK)." | ||
parser = argparse.ArgumentParser(description=DESCRIPTION) | ||
parser.add_argument("tractograms", metavar="tractogram", nargs="+", | ||
help="list of tractograms (.trk).") | ||
parser.add_argument("-f", "--force", action="store_true", | ||
help="overwrite existing output files.") | ||
|
||
args = parser.parse_args() | ||
return args, parser | ||
|
||
|
||
def main(): | ||
args, parser = parse_args() | ||
for tractogram in args.tractograms: | ||
tractogram_format = nib.streamlines.detect_format(tractogram) | ||
if tractogram_format is not nib.streamlines.TrkFile: | ||
print("Skipping non TRK file: '{}'".format(tractogram)) | ||
continue | ||
|
||
filename, _ = os.path.splitext(tractogram) | ||
output_filename = filename + '.tck' | ||
if os.path.isfile(output_filename) and not args.force: | ||
msg = "Skipping existing file: '{}'. Use -f to overwrite." | ||
print(msg.format(output_filename)) | ||
continue | ||
|
||
trk = nib.streamlines.load(tractogram) | ||
nib.streamlines.save(trk.tractogram, output_filename) |
This file contains 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
This file contains 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