@@ -226,12 +226,12 @@ class N4BiasFieldCorrectionInputSpec(ANTSCommandInputSpec):
226
226
usedefault = True ,
227
227
desc = 'image dimension (2 or 3)' )
228
228
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)' ))
231
231
mask_image = File (argstr = '--mask-image %s' )
232
232
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 )
235
235
bspline_fitting_distance = traits .Float (argstr = "--bsline-fitting [%g]" )
236
236
shrink_factor = traits .Int (argstr = "--shrink-factor %d" )
237
237
n_iterations = traits .List (traits .Int (), argstr = "--convergence [ %s" ,
@@ -240,10 +240,15 @@ class N4BiasFieldCorrectionInputSpec(ANTSCommandInputSpec):
240
240
convergence_threshold = traits .Float (argstr = ",%g]" ,
241
241
requires = ['n_iterations' ],
242
242
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.' ))
243
247
244
248
245
249
class N4BiasFieldCorrectionOutputSpec (TraitedSpec ):
246
250
output_image = File (exists = True , desc = 'Warped image' )
251
+ bias_image = File (exists = True , desc = 'Estimated bias' )
247
252
248
253
249
254
class N4BiasFieldCorrection (ANTSCommand ):
@@ -254,9 +259,11 @@ class N4BiasFieldCorrection(ANTSCommand):
254
259
iterate between deconvolving the intensity histogram by a Gaussian, remapping
255
260
the intensities, and then spatially smoothing this result by a B-spline modeling
256
261
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.
260
267
261
268
Examples
262
269
--------
@@ -270,7 +277,16 @@ class N4BiasFieldCorrection(ANTSCommand):
270
277
>>> n4.inputs.n_iterations = [50,50,30,20]
271
278
>>> n4.inputs.convergence_threshold = 1e-6
272
279
>>> 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]'
274
290
"""
275
291
276
292
_cmd = 'N4BiasFieldCorrection'
@@ -284,9 +300,36 @@ def _gen_filename(self, name):
284
300
_ , name , ext = split_filename (self .inputs .input_image )
285
301
output = name + '_corrected' + ext
286
302
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
287
310
return None
288
311
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
+
289
329
def _list_outputs (self ):
290
330
outputs = self ._outputs ().get ()
291
331
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' ))
292
335
return outputs
0 commit comments