Skip to content

Commit de68635

Browse files
committed
Move code for conversion scripts into nibabel.cmdline
1 parent eae3598 commit de68635

File tree

4 files changed

+109
-89
lines changed

4 files changed

+109
-89
lines changed

bin/nib-tck2trk

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,17 @@
11
#!python
22
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
4-
4+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
5+
#
6+
# See COPYING file distributed along with the NiBabel package for the
7+
# copyright and license terms.
8+
#
9+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
510
"""
611
Convert tractograms (TCK -> TRK).
712
"""
8-
import os
9-
import argparse
10-
11-
import nibabel as nib
12-
13-
from nibabel.streamlines import Field
14-
from nibabel.orientations import aff2axcodes
15-
16-
17-
def parse_args():
18-
DESCRIPTION = "Convert tractograms (TCK -> TRK)."
19-
parser = argparse.ArgumentParser(description=DESCRIPTION)
20-
parser.add_argument("anatomy",
21-
help="reference anatomical image (.nii|.nii.gz.")
22-
parser.add_argument("tractograms", metavar="tractogram", nargs="+",
23-
help="list of tractograms (.tck).")
24-
parser.add_argument("-f", "--force", action="store_true",
25-
help="overwrite existing output files.")
26-
27-
args = parser.parse_args()
28-
return args, parser
29-
30-
31-
def main():
32-
args, parser = parse_args()
33-
34-
try:
35-
nii = nib.load(args.anatomy)
36-
except Exception:
37-
parser.error("Expecting anatomical image as first agument.")
38-
39-
for tractogram in args.tractograms:
40-
tractogram_format = nib.streamlines.detect_format(tractogram)
41-
if tractogram_format is not nib.streamlines.TckFile:
42-
print("Skipping non TCK file: '{}'".format(tractogram))
43-
continue
44-
45-
filename, _ = os.path.splitext(tractogram)
46-
output_filename = filename + '.trk'
47-
if os.path.isfile(output_filename) and not args.force:
48-
msg = "Skipping existing file: '{}'. Use -f to overwrite."
49-
print(msg.format(output_filename))
50-
continue
51-
52-
# Build header using infos from the anatomical image.
53-
header = {}
54-
header[Field.VOXEL_TO_RASMM] = nii.affine.copy()
55-
header[Field.VOXEL_SIZES] = nii.header.get_zooms()[:3]
56-
header[Field.DIMENSIONS] = nii.shape[:3]
57-
header[Field.VOXEL_ORDER] = "".join(aff2axcodes(nii.affine))
5813

59-
tck = nib.streamlines.load(tractogram)
60-
nib.streamlines.save(tck.tractogram, output_filename, header=header)
14+
from nibabel.cmdline.tck2trk import main
6115

6216

6317
if __name__ == '__main__':

bin/nib-trk2tck

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,17 @@
11
#!python
22
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
4-
4+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
5+
#
6+
# See COPYING file distributed along with the NiBabel package for the
7+
# copyright and license terms.
8+
#
9+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
510
"""
611
Convert tractograms (TRK -> TCK).
712
"""
813

9-
import os
10-
import argparse
11-
12-
import nibabel as nib
13-
14-
15-
def parse_args():
16-
DESCRIPTION = "Convert tractograms (TRK -> TCK)."
17-
parser = argparse.ArgumentParser(description=DESCRIPTION)
18-
parser.add_argument("tractograms", metavar="tractogram", nargs="+",
19-
help="list of tractograms (.trk).")
20-
parser.add_argument("-f", "--force", action="store_true",
21-
help="overwrite existing output files.")
22-
23-
args = parser.parse_args()
24-
return args, parser
25-
26-
27-
def main():
28-
args, parser = parse_args()
29-
for tractogram in args.tractograms:
30-
tractogram_format = nib.streamlines.detect_format(tractogram)
31-
if tractogram_format is not nib.streamlines.TrkFile:
32-
print("Skipping non TRK file: '{}'".format(tractogram))
33-
continue
34-
35-
filename, _ = os.path.splitext(tractogram)
36-
output_filename = filename + '.tck'
37-
if os.path.isfile(output_filename) and not args.force:
38-
msg = "Skipping existing file: '{}'. Use -f to overwrite."
39-
print(msg.format(output_filename))
40-
continue
41-
42-
trk = nib.streamlines.load(tractogram)
43-
nib.streamlines.save(trk.tractogram, output_filename)
14+
from nibabel.cmdline.trk2tck import main
4415

4516

4617
if __name__ == '__main__':

nibabel/cmdline/tck2trk.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Convert tractograms (TCK -> TRK).
3+
"""
4+
import os
5+
import argparse
6+
7+
import nibabel as nib
8+
9+
from nibabel.streamlines import Field
10+
from nibabel.orientations import aff2axcodes
11+
12+
13+
def parse_args():
14+
DESCRIPTION = "Convert tractograms (TCK -> TRK)."
15+
parser = argparse.ArgumentParser(description=DESCRIPTION)
16+
parser.add_argument("anatomy",
17+
help="reference anatomical image (.nii|.nii.gz.")
18+
parser.add_argument("tractograms", metavar="tractogram", nargs="+",
19+
help="list of tractograms (.tck).")
20+
parser.add_argument("-f", "--force", action="store_true",
21+
help="overwrite existing output files.")
22+
23+
args = parser.parse_args()
24+
return args, parser
25+
26+
27+
def main():
28+
args, parser = parse_args()
29+
30+
try:
31+
nii = nib.load(args.anatomy)
32+
except Exception:
33+
parser.error("Expecting anatomical image as first agument.")
34+
35+
for tractogram in args.tractograms:
36+
tractogram_format = nib.streamlines.detect_format(tractogram)
37+
if tractogram_format is not nib.streamlines.TckFile:
38+
print("Skipping non TCK file: '{}'".format(tractogram))
39+
continue
40+
41+
filename, _ = os.path.splitext(tractogram)
42+
output_filename = filename + '.trk'
43+
if os.path.isfile(output_filename) and not args.force:
44+
msg = "Skipping existing file: '{}'. Use -f to overwrite."
45+
print(msg.format(output_filename))
46+
continue
47+
48+
# Build header using infos from the anatomical image.
49+
header = {}
50+
header[Field.VOXEL_TO_RASMM] = nii.affine.copy()
51+
header[Field.VOXEL_SIZES] = nii.header.get_zooms()[:3]
52+
header[Field.DIMENSIONS] = nii.shape[:3]
53+
header[Field.VOXEL_ORDER] = "".join(aff2axcodes(nii.affine))
54+
55+
tck = nib.streamlines.load(tractogram)
56+
nib.streamlines.save(tck.tractogram, output_filename, header=header)

nibabel/cmdline/trk2tck.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Convert tractograms (TRK -> TCK).
3+
"""
4+
5+
import os
6+
import argparse
7+
8+
import nibabel as nib
9+
10+
11+
def parse_args():
12+
DESCRIPTION = "Convert tractograms (TRK -> TCK)."
13+
parser = argparse.ArgumentParser(description=DESCRIPTION)
14+
parser.add_argument("tractograms", metavar="tractogram", nargs="+",
15+
help="list of tractograms (.trk).")
16+
parser.add_argument("-f", "--force", action="store_true",
17+
help="overwrite existing output files.")
18+
19+
args = parser.parse_args()
20+
return args, parser
21+
22+
23+
def main():
24+
args, parser = parse_args()
25+
for tractogram in args.tractograms:
26+
tractogram_format = nib.streamlines.detect_format(tractogram)
27+
if tractogram_format is not nib.streamlines.TrkFile:
28+
print("Skipping non TRK file: '{}'".format(tractogram))
29+
continue
30+
31+
filename, _ = os.path.splitext(tractogram)
32+
output_filename = filename + '.tck'
33+
if os.path.isfile(output_filename) and not args.force:
34+
msg = "Skipping existing file: '{}'. Use -f to overwrite."
35+
print(msg.format(output_filename))
36+
continue
37+
38+
trk = nib.streamlines.load(tractogram)
39+
nib.streamlines.save(trk.tractogram, output_filename)

0 commit comments

Comments
 (0)