Skip to content

Commit c1548a6

Browse files
committed
[ENH] Add copy_header option to N4BiasFieldCorrection
This N4 algorithm does not propagate the nifti headers after run. This PR allows setting the option ``copy_header=True`` and that will overwrite the N4 output with the corrected data and the original headers.
1 parent cda3fdc commit c1548a6

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

nipype/interfaces/ants/segmentation.py

Lines changed: 20 additions & 0 deletions
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,23 @@ 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:
395+
import nibabel as nb
396+
refnii = nb.load(self.inputs.input_image)
397+
hdr = refnii.header.copy()
398+
out_file = self._gen_filename('output_image')
399+
nii = nb.load(out_file)
400+
hdr.set_data_dtype(nii.header.get_data_dtype())
401+
nb.Nifti1Image(nii.get_data(), refnii.affine, hdr).to_filename(
402+
out_file)
403+
404+
return runtime
405+
406+
387407

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

nipype/interfaces/ants/tests/test_auto_N4BiasFieldCorrection.py

Lines changed: 3 additions & 0 deletions
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)