Skip to content

Commit 603f424

Browse files
authored
Merge pull request #2034 from oesteban/enh/N4-header
[ENH] Add copy_header option to N4BiasFieldCorrection
2 parents 88abb23 + 720809b commit 603f424

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

nipype/interfaces/ants/segmentation.py

+24
Original file line numberDiff line numberDiff line change
@@ -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

264267
class 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

388412
class CorticalThicknessInputSpec(ANTSCommandInputSpec):
389413
dimension = traits.Enum(3, 2, argstr='-d %d', usedefault=True,

nipype/interfaces/ants/tests/test_auto_N4BiasFieldCorrection.py

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def test_N4BiasFieldCorrection_inputs():
1414
),
1515
convergence_threshold=dict(requires=['n_iterations'],
1616
),
17+
copy_header=dict(mandatory=True,
18+
usedefault=True,
19+
),
1720
dimension=dict(argstr='-d %d',
1821
usedefault=True,
1922
),

0 commit comments

Comments
 (0)