/
convert.py
184 lines (148 loc) · 4.98 KB
/
convert.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- coding: utf-8 -*-
"""Provides interfaces to various commands provided by Camino-Trackvis."""
import os
from ...utils.filemanip import split_filename
from ..base import CommandLineInputSpec, CommandLine, traits, TraitedSpec, File
class Camino2TrackvisInputSpec(CommandLineInputSpec):
in_file = File(
exists=True,
argstr="-i %s",
mandatory=True,
position=1,
desc="The input .Bfloat (camino) file.",
)
out_file = File(
argstr="-o %s",
genfile=True,
position=2,
desc="The filename to which to write the .trk (trackvis) file.",
)
min_length = traits.Float(
argstr="-l %d",
position=3,
units="mm",
desc="The minimum length of tracts to output",
)
data_dims = traits.List(
traits.Int,
argstr="-d %s",
sep=",",
mandatory=True,
position=4,
minlen=3,
maxlen=3,
desc="Three comma-separated integers giving the number of voxels along each dimension of the source scans.",
)
voxel_dims = traits.List(
traits.Float,
argstr="-x %s",
sep=",",
mandatory=True,
position=5,
minlen=3,
maxlen=3,
desc="Three comma-separated numbers giving the size of each voxel in mm.",
)
# Change to enum with all combinations? i.e. LAS, LPI, RAS, etc..
voxel_order = File(
argstr="--voxel-order %s",
mandatory=True,
position=6,
desc="Set the order in which various directions were stored.\
Specify with three letters consisting of one each \
from the pairs LR, AP, and SI. These stand for Left-Right, \
Anterior-Posterior, and Superior-Inferior. \
Whichever is specified in each position will \
be the direction of increasing order. \
Read coordinate system from a NIfTI file.",
)
nifti_file = File(
argstr="--nifti %s",
exists=True,
position=7,
desc="Read coordinate system from a NIfTI file.",
)
class Camino2TrackvisOutputSpec(TraitedSpec):
trackvis = File(
exists=True, desc="The filename to which to write the .trk (trackvis) file."
)
class Camino2Trackvis(CommandLine):
""" Wraps camino_to_trackvis from Camino-Trackvis
Convert files from camino .Bfloat format to trackvis .trk format.
Example
-------
>>> import nipype.interfaces.camino2trackvis as cam2trk
>>> c2t = cam2trk.Camino2Trackvis()
>>> c2t.inputs.in_file = 'data.Bfloat'
>>> c2t.inputs.out_file = 'streamlines.trk'
>>> c2t.inputs.min_length = 30
>>> c2t.inputs.data_dims = [128, 104, 64]
>>> c2t.inputs.voxel_dims = [2.0, 2.0, 2.0]
>>> c2t.inputs.voxel_order = 'LAS'
>>> c2t.run() # doctest: +SKIP
"""
_cmd = "camino_to_trackvis"
input_spec = Camino2TrackvisInputSpec
output_spec = Camino2TrackvisOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["trackvis"] = os.path.abspath(self._gen_outfilename())
return outputs
def _gen_filename(self, name):
if name == "out_file":
return self._gen_outfilename()
else:
return None
def _gen_outfilename(self):
_, name, _ = split_filename(self.inputs.in_file)
return name + ".trk"
class Trackvis2CaminoInputSpec(CommandLineInputSpec):
""" Wraps trackvis_to_camino from Camino-Trackvis
Convert files from camino .Bfloat format to trackvis .trk format.
Example
-------
>>> import nipype.interfaces.camino2trackvis as cam2trk
>>> t2c = cam2trk.Trackvis2Camino()
>>> t2c.inputs.in_file = 'streamlines.trk'
>>> t2c.inputs.out_file = 'streamlines.Bfloat'
>>> t2c.run() # doctest: +SKIP
"""
in_file = File(
exists=True,
argstr="-i %s",
mandatory=True,
position=1,
desc="The input .trk (trackvis) file.",
)
out_file = File(
argstr="-o %s",
genfile=True,
position=2,
desc="The filename to which to write the .Bfloat (camino).",
)
append_file = File(
exists=True,
argstr="-a %s",
position=2,
desc="A file to which the append the .Bfloat data. ",
)
class Trackvis2CaminoOutputSpec(TraitedSpec):
camino = File(
exists=True, desc="The filename to which to write the .Bfloat (camino)."
)
class Trackvis2Camino(CommandLine):
_cmd = "trackvis_to_camino"
input_spec = Trackvis2CaminoInputSpec
output_spec = Trackvis2CaminoOutputSpec
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["camino"] = os.path.abspath(self._gen_outfilename())
return outputs
def _gen_filename(self, name):
if name == "out_file":
return self._gen_outfilename()
else:
return None
def _gen_outfilename(self):
_, name, _ = split_filename(self.inputs.in_file)
return name + ".Bfloat"