Skip to content

Commit 7f35086

Browse files
committed
Merge pull request #911 from oesteban/enh/N4-output-bias
ENH: N4BiasFieldCorrection new arguments
2 parents 394ac0d + d4a989e commit 7f35086

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

nipype/interfaces/ants/segmentation.py

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ class N4BiasFieldCorrectionInputSpec(ANTSCommandInputSpec):
226226
usedefault=True,
227227
desc='image dimension (2 or 3)')
228228
input_image = File(argstr='--input-image %s', mandatory=True,
229-
desc=('image to apply transformation to (generally a '
230-
'coregistered functional)'))
229+
desc=('image to apply transformation to (generally a '
230+
'coregistered functional)'))
231231
mask_image = File(argstr='--mask-image %s')
232232
output_image = traits.Str(argstr='--output %s',
233-
desc=('output file name'), genfile=True,
234-
hash_files=False)
233+
desc=('output file name'), genfile=True,
234+
hash_files=False)
235235
bspline_fitting_distance = traits.Float(argstr="--bsline-fitting [%g]")
236236
shrink_factor = traits.Int(argstr="--shrink-factor %d")
237237
n_iterations = traits.List(traits.Int(), argstr="--convergence [ %s",
@@ -240,10 +240,16 @@ class N4BiasFieldCorrectionInputSpec(ANTSCommandInputSpec):
240240
convergence_threshold = traits.Float(argstr=",%g]",
241241
requires=['n_iterations'],
242242
position=2)
243+
save_bias = traits.Bool(False, mandatory=True, usedefault=True,
244+
desc=('True if the estimated bias should be saved'
245+
' to file.'), xor=['bias_image'])
246+
bias_image = File(desc=('Filename for the estimated bias.'),
247+
hash_files=False)
243248

244249

245250
class N4BiasFieldCorrectionOutputSpec(TraitedSpec):
246251
output_image = File(exists=True, desc='Warped image')
252+
bias_image = File(exists=True, desc='Estimated bias')
247253

248254

249255
class N4BiasFieldCorrection(ANTSCommand):
@@ -254,9 +260,11 @@ class N4BiasFieldCorrection(ANTSCommand):
254260
iterate between deconvolving the intensity histogram by a Gaussian, remapping
255261
the intensities, and then spatially smoothing this result by a B-spline modeling
256262
of the bias field itself. The modifications from and improvements obtained over
257-
the original N3 algorithm are described in the following paper: N. Tustison et
258-
al., N4ITK: Improved N3 Bias Correction, IEEE Transactions on Medical Imaging,
259-
29(6):1310-1320, June 2010.
263+
the original N3 algorithm are described in [Tustison2010]_.
264+
265+
.. [Tustison2010] N. Tustison et al.,
266+
N4ITK: Improved N3 Bias Correction, IEEE Transactions on Medical Imaging,
267+
29(6):1310-1320, June 2010.
260268
261269
Examples
262270
--------
@@ -270,7 +278,16 @@ class N4BiasFieldCorrection(ANTSCommand):
270278
>>> n4.inputs.n_iterations = [50,50,30,20]
271279
>>> n4.inputs.convergence_threshold = 1e-6
272280
>>> n4.cmdline
273-
'N4BiasFieldCorrection --convergence [ 50x50x30x20 ,1e-06] --bsline-fitting [300] --image-dimension 3 --input-image structural.nii --output structural_corrected.nii --shrink-factor 3'
281+
'N4BiasFieldCorrection --convergence [ 50x50x30x20 ,1e-06] \
282+
--bsline-fitting [300] --image-dimension 3 --input-image structural.nii \
283+
--output structural_corrected.nii --shrink-factor 3'
284+
285+
>>> n4_2 = N4BiasFieldCorrection()
286+
>>> n4_2.inputs.input_image = 'structural.nii'
287+
>>> n4_2.inputs.save_bias = True
288+
>>> n4_2.cmdline
289+
'N4BiasFieldCorrection --image-dimension 3 --input-image structural.nii \
290+
--output [structural_corrected.nii,structural_bias.nii]'
274291
"""
275292

276293
_cmd = 'N4BiasFieldCorrection'
@@ -284,9 +301,36 @@ def _gen_filename(self, name):
284301
_, name, ext = split_filename(self.inputs.input_image)
285302
output = name + '_corrected' + ext
286303
return output
304+
305+
if name == 'bias_image':
306+
output = self.inputs.bias_image
307+
if not isdefined(output):
308+
_, name, ext = split_filename(self.inputs.input_image)
309+
output = name + '_bias' + ext
310+
return output
287311
return None
288312

313+
def _format_arg(self, name, trait_spec, value):
314+
if ((name == 'output_image') and
315+
(self.inputs.save_bias or isdefined(self.inputs.bias_image))):
316+
bias_image = self._gen_filename('bias_image')
317+
output = self._gen_filename('output_image')
318+
newval = '[%s,%s]' % (output, bias_image)
319+
return trait_spec.argstr % newval
320+
321+
return super(N4BiasFieldCorrection,
322+
self)._format_arg(name, trait_spec, value)
323+
324+
def _parse_inputs(self, skip=None):
325+
if skip is None:
326+
skip = []
327+
skip += ['save_bias', 'bias_image']
328+
return super(N4BiasFieldCorrection, self)._parse_inputs(skip=skip)
329+
289330
def _list_outputs(self):
290331
outputs = self._outputs().get()
291332
outputs['output_image'] = os.path.abspath(self._gen_filename('output_image'))
333+
334+
if self.inputs.save_bias or isdefined(self.inputs.bias_image):
335+
outputs['bias_image'] = os.path.abspath(self._gen_filename('bias_image'))
292336
return outputs

nipype/interfaces/ants/tests/test_auto_N4BiasFieldCorrection.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
def test_N4BiasFieldCorrection_inputs():
66
input_map = dict(args=dict(argstr='%s',
77
),
8+
bias_image=dict(hash_files=False,
9+
),
810
bspline_fitting_distance=dict(argstr='--bsline-fitting [%g]',
911
),
1012
convergence_threshold=dict(argstr=',%g]',
@@ -37,6 +39,10 @@ def test_N4BiasFieldCorrection_inputs():
3739
genfile=True,
3840
hash_files=False,
3941
),
42+
save_bias=dict(mandatory=True,
43+
usedefault=True,
44+
xor=['bias_image'],
45+
),
4046
shrink_factor=dict(argstr='--shrink-factor %d',
4147
),
4248
terminal_output=dict(mandatory=True,
@@ -50,7 +56,8 @@ def test_N4BiasFieldCorrection_inputs():
5056
yield assert_equal, getattr(inputs.traits()[key], metakey), value
5157

5258
def test_N4BiasFieldCorrection_outputs():
53-
output_map = dict(output_image=dict(),
59+
output_map = dict(bias_image=dict(),
60+
output_image=dict(),
5461
)
5562
outputs = N4BiasFieldCorrection.output_spec()
5663

0 commit comments

Comments
 (0)