File: tff_concatenate_tractograms.py

package info (click to toggle)
python-trx-python 0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: python: 3,394; makefile: 66
file content (74 lines) | stat: -rwxr-xr-x 2,609 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Concatenate multiple tractograms into one.

If the data_per_point or data_per_streamline is not the same for all
tractograms, the data must be deleted first.
"""

import argparse
import os

from trx.io import load, save
from trx.trx_file_memmap import TrxFile, concatenate


def _build_arg_parser():
    p = argparse.ArgumentParser(description=__doc__,
                                formatter_class=argparse.RawTextHelpFormatter)

    p.add_argument('in_tractograms', nargs='+',
                   help='Tractogram filename. Format must be one of \n'
                        'trk, tck, vtk, fib, dpy, trx.')
    p.add_argument('out_tractogram',
                   help='Filename of the concatenated tractogram.')

    p.add_argument('--delete_dpv', action='store_true',
                   help='Delete the dpv if it exists. '
                        'Required if not all input has the same metadata.')
    p.add_argument('--delete_dps', action='store_true',
                   help='Delete the dps if it exists. '
                        'Required if not all input has the same metadata.')
    p.add_argument('--delete_groups', action='store_true',
                   help='Delete the groups if it exists. '
                        'Required if not all input has the same metadata.')
    p.add_argument('--reference',
                   help='Reference anatomy for tck/vtk/fib/dpy file\n'
                        'support (.nii or .nii.gz).')
    p.add_argument('-f', dest='overwrite', action='store_true',
                   help='Force overwriting of the output files.')

    return p


def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    if os.path.isfile(args.out_tractogram) and not args.overwrite:
        raise IOError('{} already exists, use -f to overwrite.'.format(
            args.out_tractogram))

    trx_list = []
    has_group = False
    for filename in args.in_tractograms:
        tractogram_obj = load(filename, args.reference)

        if not isinstance(tractogram_obj, TrxFile):
            tractogram_obj = TrxFile.from_sft(tractogram_obj)
        elif len(tractogram_obj.groups):
            has_group = True
        trx_list.append(tractogram_obj)

    trx = concatenate(trx_list, delete_dpv=args.delete_dpv,
                      delete_dps=args.delete_dps,
                      delete_groups=args.delete_groups or not has_group,
                      check_space_attributes=True,
                      preallocation=False)
    save(trx, args.out_tractogram)


if __name__ == "__main__":
    main()