Skip to content

Commit 204aa89

Browse files
committed
new base for MRTrix3 interfaces, new nthreads input
1 parent 8a5fd28 commit 204aa89

11 files changed

+121
-97
lines changed

nipype/interfaces/mrtrix3/base.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
# -*- coding: utf-8 -*-
4+
5+
"""
6+
Change directory to provide relative paths for doctests
7+
>>> import os
8+
>>> filepath = os.path.dirname(os.path.realpath(__file__ ))
9+
>>> datadir = os.path.realpath(os.path.join(filepath,
10+
... '../../testing/data'))
11+
>>> os.chdir(datadir)
12+
13+
"""
14+
import os
15+
import os.path as op
16+
17+
from nipype.interfaces.base import (
18+
CommandLineInputSpec, CommandLine, traits, TraitedSpec, File,
19+
InputMultiPath)
20+
21+
from nipype.utils.filemanip import split_filename
22+
from nipype.interfaces.traits_extension import isdefined
23+
24+
from ... import logging
25+
logger = logging.getLogger('interface')
26+
27+
28+
class MRTrix3BaseInputSpec(CommandLineInputSpec):
29+
nthreads = traits.Int(
30+
argstr='-nthreads %d', desc='number of threads. if zero, the number'
31+
' of available cpus will be used')
32+
# DW gradient table import options
33+
grad_file = File(exists=True, argstr='-grad %s',
34+
desc='dw gradient scheme (MRTrix format')
35+
grad_fsl = traits.Tuple(
36+
File(exists=True), File(exists=True), argstr='-fslgrad %s %s',
37+
desc='(bvecs, bvals) dw gradient scheme (FSL format')
38+
bval_scale = traits.Enum(
39+
'yes', 'no', argstr='-bvalue_scaling %s',
40+
desc='specifies whether the b - values should be scaled by the square'
41+
' of the corresponding DW gradient norm, as often required for '
42+
'multishell or DSI DW acquisition schemes. The default action '
43+
'can also be set in the MRtrix config file, under the '
44+
'BValueScaling entry. Valid choices are yes / no, true / '
45+
'false, 0 / 1 (default: true).')
46+
47+
48+
class MRTrix3Base(CommandLine):
49+
50+
def _format_arg(self, name, trait_spec, value):
51+
if name == 'nthreads' and value == 0:
52+
value = 1
53+
try:
54+
from multiprocessing import cpu_count
55+
value = cpu_count()
56+
except:
57+
logger.warn('Number of threads could not be computed')
58+
pass
59+
return trait_spec.argstr % value
60+
61+
return super(MRTrix3Base, self)._format_arg(name, trait_spec, value)

nipype/interfaces/mrtrix3/preprocess.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,23 @@
1414
import os
1515
import os.path as op
1616

17+
from base import MRTrix3BaseInputSpec, MRTrix3Base
1718
from nipype.interfaces.base import (
1819
CommandLineInputSpec, CommandLine, traits, TraitedSpec, File)
1920

2021
from nipype.utils.filemanip import split_filename
2122
from nipype.interfaces.traits_extension import isdefined
2223

2324

24-
class ResponseSDInputSpec(CommandLineInputSpec):
25+
class ResponseSDInputSpec(MRTrix3BaseInputSpec):
2526
in_file = File(exists=True, argstr='%s', mandatory=True, position=-2,
2627
desc='input diffusion weighted images')
2728

2829
out_file = File(
2930
'response.txt', argstr='%s', mandatory=True, position=-1,
3031
usedefault=True, desc='output file containing SH coefficients')
3132

32-
# DW gradient table import options
33-
grad_file = File(exists=True, argstr='-grad %s',
34-
desc='dw gradient scheme (MRTrix format')
35-
grad_fsl = traits.Tuple(
36-
File(exists=True), File(exists=True), argstr='-fslgrad %s %s',
37-
desc='(bvecs, bvals) dw gradient scheme (FSL format')
38-
bval_scale = traits.Enum(
39-
'yes', 'no', argstr='-bvalue_scaling %s',
40-
desc=('specifies whether the b - values should be scaled by the square'
41-
' of the corresponding DW gradient norm, as often required for '
42-
'multishell or DSI DW acquisition schemes. The default action '
43-
'can also be set in the MRtrix config file, under the '
44-
'BValueScaling entry. Valid choices are yes / no, true / '
45-
'false, 0 / 1 (default: true).'))
46-
4733
# DW Shell selection options
48-
4934
shell = traits.List(traits.Float, sep=',', argstr='-shell %s',
5035
desc='specify one or more dw gradient shells')
5136
in_mask = File(exists=True, argstr='-mask %s',
@@ -92,7 +77,7 @@ class ResponseSDOutputSpec(TraitedSpec):
9277
out_sf = File(desc=('mask containing single-fibre voxels'))
9378

9479

95-
class ResponseSD(CommandLine):
80+
class ResponseSD(MRTrix3Base):
9681

9782
"""
9883
Generate an appropriate response function from the image data for

nipype/interfaces/mrtrix3/reconst.py

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import os
1515
import os.path as op
1616

17+
from base import MRTrix3BaseInputSpec, MRTrix3Base
1718
from nipype.interfaces.base import (
1819
CommandLineInputSpec, CommandLine, traits, TraitedSpec, File,
1920
InputMultiPath)
@@ -22,7 +23,7 @@
2223
from nipype.interfaces.traits_extension import isdefined
2324

2425

25-
class FitTensorInputSpec(CommandLineInputSpec):
26+
class FitTensorInputSpec(MRTrix3BaseInputSpec):
2627
in_file = File(exists=True, argstr='%s', mandatory=True, position=-2,
2728
desc='input diffusion weighted images')
2829
out_file = File(
@@ -42,27 +43,12 @@ class FitTensorInputSpec(CommandLineInputSpec):
4243
'magnitude of the tensor elements (default = 5000). This '
4344
'only applies to the non-linear methods'))
4445

45-
# DW gradient table import options
46-
grad_file = File(exists=True, argstr='-grad %s',
47-
desc='dw gradient scheme (MRTrix format')
48-
grad_fsl = traits.Tuple(
49-
File(exists=True), File(exists=True), argstr='-fslgrad %s %s',
50-
desc='(bvecs, bvals) dw gradient scheme (FSL format')
51-
bval_scale = traits.Enum(
52-
'yes', 'no', argstr='-bvalue_scaling %s',
53-
desc=('specifies whether the b - values should be scaled by the square'
54-
' of the corresponding DW gradient norm, as often required for '
55-
'multishell or DSI DW acquisition schemes. The default action '
56-
'can also be set in the MRtrix config file, under the '
57-
'BValueScaling entry. Valid choices are yes / no, true / '
58-
'false, 0 / 1 (default: true).'))
59-
6046

6147
class FitTensorOutputSpec(TraitedSpec):
6248
out_file = File(exists=True, desc='the output DTI file')
6349

6450

65-
class FitTensor(CommandLine):
51+
class FitTensor(MRTrix3Base):
6652

6753
"""
6854
Convert diffusion-weighted images to tensor images
@@ -91,7 +77,7 @@ def _list_outputs(self):
9177
return outputs
9278

9379

94-
class EstimateFODInputSpec(CommandLineInputSpec):
80+
class EstimateFODInputSpec(MRTrix3BaseInputSpec):
9581
in_file = File(exists=True, argstr='%s', mandatory=True, position=-3,
9682
desc='input diffusion weighted images')
9783
response = File(
@@ -103,21 +89,6 @@ class EstimateFODInputSpec(CommandLineInputSpec):
10389
usedefault=True, desc=('the output spherical harmonics coefficients'
10490
' image'))
10591

106-
# DW gradient table import options
107-
grad_file = File(exists=True, argstr='-grad %s',
108-
desc='dw gradient scheme (MRTrix format')
109-
grad_fsl = traits.Tuple(
110-
File(exists=True), File(exists=True), argstr='-fslgrad %s %s',
111-
desc='(bvecs, bvals) dw gradient scheme (FSL format')
112-
bval_scale = traits.Enum(
113-
'yes', 'no', argstr='-bvalue_scaling %s',
114-
desc=('specifies whether the b - values should be scaled by the square'
115-
' of the corresponding DW gradient norm, as often required for '
116-
'multishell or DSI DW acquisition schemes. The default action '
117-
'can also be set in the MRtrix config file, under the '
118-
'BValueScaling entry. Valid choices are yes / no, true / '
119-
'false, 0 / 1 (default: true).'))
120-
12192
# DW Shell selection options
12293
shell = traits.List(traits.Float, sep=',', argstr='-shell %s',
12394
desc='specify one or more dw gradient shells')
@@ -130,34 +101,35 @@ class EstimateFODInputSpec(CommandLineInputSpec):
130101
in_dirs = File(
131102
exists=True, argstr='-directions %s',
132103
desc=('specify the directions over which to apply the non-negativity '
133-
'constraint (by default, the built-in 300 direction set is used). '
134-
'These should be supplied as a text file containing the [ az el ] '
135-
'pairs for the directions.'))
104+
'constraint (by default, the built-in 300 direction set is '
105+
'used). These should be supplied as a text file containing the '
106+
'[ az el ] pairs for the directions.'))
136107
sh_filter = File(
137108
exists=True, argstr='-filter %s',
138109
desc=('the linear frequency filtering parameters used for the initial '
139110
'linear spherical deconvolution step (default = [ 1 1 1 0 0 ]). '
140-
'These should be supplied as a text file containing the filtering '
141-
'coefficients for each even harmonic order.'))
111+
'These should be supplied as a text file containing the '
112+
'filtering coefficients for each even harmonic order.'))
142113

143114
neg_lambda = traits.Float(
144115
1.0, argstr='-neg_lambda %f',
145-
desc=('the regularisation parameter lambda that controls the strength of '
146-
'the non-negativity constraint'))
116+
desc=('the regularisation parameter lambda that controls the strength'
117+
' of the non-negativity constraint'))
147118
thres = traits.Float(
148119
0.0, argstr='-threshold %f',
149-
desc=('the threshold below which the amplitude of the FOD is assumed to be '
150-
'zero, expressed as an absolute amplitude'))
120+
desc=('the threshold below which the amplitude of the FOD is assumed '
121+
'to be zero, expressed as an absolute amplitude'))
151122

152-
n_iter = traits.Int(50, argstr='-niter %d', desc=('the maximum number of iterat'
153-
'ions to perform for each voxel'))
123+
n_iter = traits.Int(
124+
50, argstr='-niter %d', desc=('the maximum number of iterations '
125+
'to perform for each voxel'))
154126

155127

156128
class EstimateFODOutputSpec(TraitedSpec):
157129
out_file = File(exists=True, desc='the output response file')
158130

159131

160-
class EstimateFOD(CommandLine):
132+
class EstimateFOD(MRTrix3Base):
161133

162134
"""
163135
Convert diffusion-weighted images to tensor images

nipype/interfaces/mrtrix3/tests/test_auto_BrainMask.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def test_BrainMask_inputs():
2121
mandatory=True,
2222
position=-2,
2323
),
24+
nthreads=dict(argstr='-nthreads %d',
25+
),
2426
out_file=dict(argstr='%s',
2527
mandatory=True,
2628
position=-1,

nipype/interfaces/mrtrix3/tests/test_auto_EstimateFOD.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def test_EstimateFOD_inputs():
3131
),
3232
neg_lambda=dict(argstr='-neg_lambda %f',
3333
),
34+
nthreads=dict(argstr='-nthreads %d',
35+
),
3436
out_file=dict(argstr='%s',
3537
mandatory=True,
3638
position=-1,

nipype/interfaces/mrtrix3/tests/test_auto_FitTensor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def test_FitTensor_inputs():
2525
),
2626
method=dict(argstr='-method %s',
2727
),
28+
nthreads=dict(argstr='-nthreads %d',
29+
),
2830
out_file=dict(argstr='%s',
2931
mandatory=True,
3032
position=-1,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from nipype.testing import assert_equal
3+
from nipype.interfaces.mrtrix3.base import MRTrix3Base
4+
5+
def test_MRTrix3Base_inputs():
6+
input_map = dict(args=dict(argstr='%s',
7+
),
8+
environ=dict(nohash=True,
9+
usedefault=True,
10+
),
11+
ignore_exception=dict(nohash=True,
12+
usedefault=True,
13+
),
14+
terminal_output=dict(nohash=True,
15+
),
16+
)
17+
inputs = MRTrix3Base.input_spec()
18+
19+
for key, metadata in input_map.items():
20+
for metakey, value in metadata.items():
21+
yield assert_equal, getattr(inputs.traits()[key], metakey), value
22+

nipype/interfaces/mrtrix3/tests/test_auto_ResponseSD.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def test_ResponseSD_inputs():
3333
),
3434
max_sh=dict(argstr='-lmax %d',
3535
),
36+
nthreads=dict(argstr='-nthreads %d',
37+
),
3638
out_file=dict(argstr='%s',
3739
mandatory=True,
3840
position=-1,

nipype/interfaces/mrtrix3/tests/test_auto_Tractography.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def test_Tractography_inputs():
5656
),
5757
noprecompt=dict(argstr='-noprecomputed',
5858
),
59+
nthreads=dict(argstr='-nthreads %d',
60+
),
5961
out_file=dict(argstr='%s',
6062
mandatory=True,
6163
position=-1,

nipype/interfaces/mrtrix3/tracking.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
import os
1515
import os.path as op
1616

17+
from base import MRTrix3BaseInputSpec, MRTrix3Base
1718
from nipype.interfaces.base import (
1819
CommandLineInputSpec, CommandLine, traits, TraitedSpec, File)
1920

2021
from nipype.utils.filemanip import split_filename
2122
from nipype.interfaces.traits_extension import isdefined
2223

2324

24-
class TractographyInputSpec(CommandLineInputSpec):
25+
class TractographyInputSpec(MRTrix3BaseInputSpec):
2526
sph_trait = traits.Tuple(traits.Float, traits.Float, traits.Float,
2627
traits.Float, argstr='%f,%f,%f,%f')
2728

@@ -120,7 +121,7 @@ class TractographyInputSpec(CommandLineInputSpec):
120121
'include regions'))
121122
downsample = traits.Float(
122123
argstr='-downsample %f',
123-
desc=('downsample the generated streamlines to reduce output file size'))
124+
desc='downsample the generated streamlines to reduce output file size')
124125

125126
# Anatomically-Constrained Tractography options
126127
act_file = File(
@@ -171,7 +172,8 @@ class TractographyInputSpec(CommandLineInputSpec):
171172
' while this seeding mechanism improves the distribution of'
172173
' reconstructed streamlines density, it should NOT be used '
173174
'as a substitute for the SIFT method itself.'))
174-
max_seed_attempts = traits.Int(argstr='-max_seed_attempts %d',
175+
max_seed_attempts = traits.Int(
176+
argstr='-max_seed_attempts %d',
175177
desc=('set the maximum number of times that the tracking '
176178
'algorithm should attempt to find an appropriate tracking'
177179
' direction from a given seed point'))
@@ -180,29 +182,14 @@ class TractographyInputSpec(CommandLineInputSpec):
180182
desc=('output the seed location of all successful streamlines to'
181183
' a file'))
182184

183-
# DW gradient table import options
184-
grad_file = File(exists=True, argstr='-grad %s',
185-
desc='dw gradient scheme (MRTrix format')
186-
grad_fsl = traits.Tuple(
187-
File(exists=True), File(exists=True), argstr='-fslgrad %s %s',
188-
desc='(bvecs, bvals) dw gradient scheme (FSL format')
189-
bval_scale = traits.Enum(
190-
'yes', 'no', argstr='-bvalue_scaling %s',
191-
desc=('specifies whether the b - values should be scaled by the square'
192-
' of the corresponding DW gradient norm, as often required for '
193-
'multishell or DSI DW acquisition schemes. The default action '
194-
'can also be set in the MRtrix config file, under the '
195-
'BValueScaling entry. Valid choices are yes / no, true / '
196-
'false, 0 / 1 (default: true).'))
197-
198185

199186
class TractographyOutputSpec(TraitedSpec):
200187
out_file = File(exists=True, desc='the output filtered tracks')
201188
out_seeds = File(desc=('output the seed location of all successful'
202189
' streamlines to a file'))
203190

204191

205-
class Tractography(CommandLine):
192+
class Tractography(MRTrix3Base):
206193

207194
"""
208195
Performs streamlines tractography after selecting the appropriate

0 commit comments

Comments
 (0)