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

Conversation

oesteban
Copy link
Contributor

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.

Default value of copy_header is False

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.
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?

@effigies effigies dismissed their stale review May 19, 2017 00:10

Comments addressed

@effigies
Copy link
Member

I imagine we should do the same for bias_image, if it's being created?

@codecov-io
Copy link

Codecov Report

Merging #2034 into master will decrease coverage by 0.01%.
The diff coverage is 16.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2034      +/-   ##
==========================================
- Coverage   72.19%   72.18%   -0.02%     
==========================================
  Files        1132     1132              
  Lines       57032    57044      +12     
  Branches     8167     8168       +1     
==========================================
+ Hits        41175    41177       +2     
- Misses      14572    14582      +10     
  Partials     1285     1285
Flag Coverage Δ
#smoketests 72.18% <16.66%> (-0.02%) ⬇️
#unittests 69.78% <16.66%> (-0.02%) ⬇️
Impacted Files Coverage Δ
...aces/ants/tests/test_auto_N4BiasFieldCorrection.py 85.71% <ø> (ø) ⬆️
nipype/interfaces/ants/segmentation.py 73% <16.66%> (-1.23%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cda3fdc...98af18e. Read the comment docs.

@codecov-io
Copy link

codecov-io commented May 19, 2017

Codecov Report

Merging #2034 into master will decrease coverage by 0.01%.
The diff coverage is 16.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2034      +/-   ##
==========================================
- Coverage   72.19%   72.18%   -0.02%     
==========================================
  Files        1132     1132              
  Lines       57032    57044      +12     
  Branches     8167     8168       +1     
==========================================
+ Hits        41175    41177       +2     
- Misses      14572    14582      +10     
  Partials     1285     1285
Flag Coverage Δ
#smoketests 72.18% <16.66%> (-0.02%) ⬇️
#unittests 69.78% <16.66%> (-0.02%) ⬇️
Impacted Files Coverage Δ
...aces/ants/tests/test_auto_N4BiasFieldCorrection.py 85.71% <ø> (ø) ⬆️
nipype/interfaces/ants/segmentation.py 73% <16.66%> (-1.23%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cda3fdc...720809b. Read the comment docs.

@satra
Copy link
Member

satra commented May 19, 2017

@oesteban - what aspects of the header does it not copy? as in, is the output file an invalid Nifti file? and is this an issue that should be fixed in ANTs?

@oesteban
Copy link
Contributor Author

oesteban commented May 19, 2017

@satra we started here nipreps/fmriprep#497 . I don't know exactly what are the problems, I could go deeper into it.

EDIT: but the output files are valid nifti.

@effigies that would make sense for consistency

@satra
Copy link
Member

satra commented May 19, 2017

@oesteban - i looked at the issue, the voxel size bit seems related to floating point representation and is fine. the shape mismatch seems weird. so two questions

  1. does nibabel complain when the output image is loaded that there is a mismatch in the header?
  2. is there really a mismatch in shape?

i'm worried that we shouldn't be patching bugs in other software, if they are indeed bugs. has this been reported on ants? and if we patch, we need to be very clear to un-patch it when it's fixed.

@kaczmarj, @mgxd - could we create a test across different versions of ANTs for N4 and test this issue?

@satra
Copy link
Member

satra commented May 26, 2017

@oesteban - any thoughts on my comment?

@oesteban
Copy link
Contributor Author

oesteban commented May 26, 2017

@satra ANTsX/ANTs#452

We can go ahead with this patch in our settings (fmriprep and Co.) and close this PR if you prefer to wait for ANTs to solve the issue. On the other hand, they don't release very often, so this patch could be an incentive for using nipype in the meantime.

Since the major problem is the change of the qform code, I think nipype could very well use this patch.

EDIT: but overall yes, I agree we shouldn't patch problems that we identify on the underlying interfaces and rather report them to the appropriate places 👍

@oesteban
Copy link
Contributor Author

Update: since the qform issue derives from ITK, it is not likely to change (and we probably should address this in all ITK-related interfaces, to keep workflows consistent).

Then the type-casting is the user's responsibility. I can see nipype helping on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants