@@ -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,16 @@ 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.' ),
247
+ hash_files = False )
243
248
244
249
245
250
class N4BiasFieldCorrectionOutputSpec (TraitedSpec ):
246
251
output_image = File (exists = True , desc = 'Warped image' )
252
+ bias_image = File (exists = True , desc = 'Estimated bias' )
247
253
248
254
249
255
class N4BiasFieldCorrection (ANTSCommand ):
@@ -254,9 +260,11 @@ class N4BiasFieldCorrection(ANTSCommand):
254
260
iterate between deconvolving the intensity histogram by a Gaussian, remapping
255
261
the intensities, and then spatially smoothing this result by a B-spline modeling
256
262
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.
260
268
261
269
Examples
262
270
--------
@@ -270,7 +278,16 @@ class N4BiasFieldCorrection(ANTSCommand):
270
278
>>> n4.inputs.n_iterations = [50,50,30,20]
271
279
>>> n4.inputs.convergence_threshold = 1e-6
272
280
>>> 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]'
274
291
"""
275
292
276
293
_cmd = 'N4BiasFieldCorrection'
@@ -284,9 +301,36 @@ def _gen_filename(self, name):
284
301
_ , name , ext = split_filename (self .inputs .input_image )
285
302
output = name + '_corrected' + ext
286
303
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
287
311
return None
288
312
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
+
289
330
def _list_outputs (self ):
290
331
outputs = self ._outputs ().get ()
291
332
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' ))
292
336
return outputs
0 commit comments