Skip to content

Commit a29275a

Browse files
committed
ENH: N4BiasFieldCorrection new arguments
Now, it is possible to save the estimated bias field, by fully supporting the --output argument specification.
1 parent 153e0d4 commit a29275a

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

nipype/interfaces/ants/segmentation.py

Lines changed: 51 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,15 @@ 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.'))
243247

244248

245249
class N4BiasFieldCorrectionOutputSpec(TraitedSpec):
246250
output_image = File(exists=True, desc='Warped image')
251+
bias_image = File(exists=True, desc='Estimated bias')
247252

248253

249254
class N4BiasFieldCorrection(ANTSCommand):
@@ -254,9 +259,11 @@ class N4BiasFieldCorrection(ANTSCommand):
254259
iterate between deconvolving the intensity histogram by a Gaussian, remapping
255260
the intensities, and then spatially smoothing this result by a B-spline modeling
256261
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.
262+
the original N3 algorithm are described in [Tustison2010]_.
263+
264+
.. [Tustison2010] N. Tustison et al.,
265+
N4ITK: Improved N3 Bias Correction, IEEE Transactions on Medical Imaging,
266+
29(6):1310-1320, June 2010.
260267
261268
Examples
262269
--------
@@ -270,7 +277,16 @@ class N4BiasFieldCorrection(ANTSCommand):
270277
>>> n4.inputs.n_iterations = [50,50,30,20]
271278
>>> n4.inputs.convergence_threshold = 1e-6
272279
>>> 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'
280+
'N4BiasFieldCorrection --convergence [ 50x50x30x20 ,1e-06] \
281+
--bsline-fitting [300] --image-dimension 3 --input-image structural.nii \
282+
--output structural_corrected.nii --shrink-factor 3'
283+
284+
>>> n4_2 = N4BiasFieldCorrection()
285+
>>> n4_2.inputs.input_image = 'structural.nii'
286+
>>> n4_2.inputs.save_bias = True
287+
>>> n4_2.cmdline
288+
'N4BiasFieldCorrection --image-dimension 3 --input-image structural.nii \
289+
--output [structural_corrected.nii,structural_bias.nii]'
274290
"""
275291

276292
_cmd = 'N4BiasFieldCorrection'
@@ -284,9 +300,36 @@ def _gen_filename(self, name):
284300
_, name, ext = split_filename(self.inputs.input_image)
285301
output = name + '_corrected' + ext
286302
return output
303+
304+
if name == 'bias_image':
305+
output = self.inputs.bias_image
306+
if not isdefined(output):
307+
_, name, ext = split_filename(self.inputs.input_image)
308+
output = name + '_bias' + ext
309+
return output
287310
return None
288311

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

nipype/interfaces/ants/tests/test_auto_N4BiasFieldCorrection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
def test_N4BiasFieldCorrection_inputs():
66
input_map = dict(args=dict(argstr='%s',
77
),
8+
bias_image=dict(),
89
bspline_fitting_distance=dict(argstr='--bsline-fitting [%g]',
910
),
1011
convergence_threshold=dict(argstr=',%g]',
@@ -37,6 +38,10 @@ def test_N4BiasFieldCorrection_inputs():
3738
genfile=True,
3839
hash_files=False,
3940
),
41+
save_bias=dict(mandatory=True,
42+
usedefault=True,
43+
xor=['bias_image'],
44+
),
4045
shrink_factor=dict(argstr='--shrink-factor %d',
4146
),
4247
terminal_output=dict(mandatory=True,
@@ -50,7 +55,8 @@ def test_N4BiasFieldCorrection_inputs():
5055
yield assert_equal, getattr(inputs.traits()[key], metakey), value
5156

5257
def test_N4BiasFieldCorrection_outputs():
53-
output_map = dict(output_image=dict(),
58+
output_map = dict(bias_image=dict(),
59+
output_image=dict(),
5460
)
5561
outputs = N4BiasFieldCorrection.output_spec()
5662

0 commit comments

Comments
 (0)