@@ -259,6 +259,9 @@ class N4BiasFieldCorrectionInputSpec(ANTSCommandInputSpec):
259259 ' to file.' ), xor = ['bias_image' ])
260260 bias_image = File (desc = 'Filename for the estimated bias.' ,
261261 hash_files = False )
262+ copy_header = traits .Bool (False , mandatory = True , usedefault = True ,
263+ desc = 'copy headers of the original image into the '
264+ 'output (corrected) file' )
262265
263266
264267class N4BiasFieldCorrectionOutputSpec (TraitedSpec ):
@@ -384,6 +387,27 @@ def _list_outputs(self):
384387 self ._gen_filename ('bias_image' ))
385388 return outputs
386389
390+ def _run_interface (self , runtime , correct_return_codes = (0 ,)):
391+ runtime = super (N4BiasFieldCorrection , self )._run_interface (
392+ runtime , correct_return_codes )
393+
394+ if self .inputs .copy_header and runtime .returncode in correct_return_codes :
395+ self ._copy_header (self ._gen_filename ('output_image' ))
396+ if self .inputs .save_bias or isdefined (self .inputs .bias_image ):
397+ self ._copy_header (self ._gen_filename ('bias_image' ))
398+
399+ return runtime
400+
401+ def _copy_header (self , fname ):
402+ """Copy header from input image to an output image"""
403+ import nibabel as nb
404+ in_img = nb .load (self .inputs .input_image )
405+ out_img = nb .load (fname , mmap = False )
406+ new_img = out_img .__class__ (out_img .get_data (), in_img .affine ,
407+ in_img .header )
408+ new_img .set_data_dtype (out_img .get_data_dtype ())
409+ new_img .to_filename (fname )
410+
387411
388412class CorticalThicknessInputSpec (ANTSCommandInputSpec ):
389413 dimension = traits .Enum (3 , 2 , argstr = '-d %d' , usedefault = True ,
0 commit comments