Skip to content

[ENH] Add copy_header option to N4BiasFieldCorrection #2034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions nipype/interfaces/ants/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ class N4BiasFieldCorrectionInputSpec(ANTSCommandInputSpec):
' to file.'), xor=['bias_image'])
bias_image = File(desc='Filename for the estimated bias.',
hash_files=False)
copy_header = traits.Bool(False, mandatory=True, usedefault=True,
desc='copy headers of the original image into the '
'output (corrected) file')


class N4BiasFieldCorrectionOutputSpec(TraitedSpec):
Expand Down Expand Up @@ -384,6 +387,23 @@ def _list_outputs(self):
self._gen_filename('bias_image'))
return outputs

def _run_interface(self, runtime, correct_return_codes=(0,)):
runtime = super(N4BiasFieldCorrection, self)._run_interface(
runtime, correct_return_codes)

if self.inputs.copy_header:
import nibabel as nb
refnii = nb.load(self.inputs.input_image)
hdr = refnii.header.copy()
out_file = self._gen_filename('output_image')
nii = nb.load(out_file)
hdr.set_data_dtype(nii.header.get_data_dtype())
nb.Nifti1Image(nii.get_data(), refnii.affine, hdr).to_filename(
out_file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does N4BiasFieldCorrection always use NIfTI images, or are we making this assumption? To be more general, this could be:

if self.inputs.copy_header and runtime.returncode in correct_return_codes:
    import nibabel as nb
    in_img = nb.load(self.inputs.input_image)
    out_file = self._gen_filename('output_image')
    out_img = nb.load(out_file, mmap=False)
    new_img = out_img.__class__(out_img.get_data(), in_img.affine, in_img.header)
    new_img.set_data_dtype(out_img.get_data_dtype())
    new_img.to_filename(out_file)

A couple additional notes:

  • Should make sure we ran successfully before reading/overwriting the output
  • mmap=False on nb.load() - See Feature Request: Detect when writing to open mmap nibabel#492
  • No need to copy header (that's done in image creation)
  • I prefer to do get/set_data_dtype on the image itself, rather than the header, even though they're equivalent. Since an image contains a reference to the header and not the other way around, if some image class needs (in a future nibabel version) to modify the data portion of the image during this call, it can. Your call on this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N4BiasFieldCorrection virtually accepts any of the file formats supported by ITK. So I guess it is worth checking the image type (Analyze or Nifti) before copying the header. @satra any thoughts?


return runtime



class CorticalThicknessInputSpec(ANTSCommandInputSpec):
dimension = traits.Enum(3, 2, argstr='-d %d', usedefault=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def test_N4BiasFieldCorrection_inputs():
),
convergence_threshold=dict(requires=['n_iterations'],
),
copy_header=dict(mandatory=True,
usedefault=True,
),
dimension=dict(argstr='-d %d',
usedefault=True,
),
Expand Down