@@ -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
245250class N4BiasFieldCorrectionOutputSpec (TraitedSpec ):
246251 output_image = File (exists = True , desc = 'Warped image' )
252+ bias_image = File (exists = True , desc = 'Estimated bias' )
247253
248254
249255class 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
0 commit comments