Skip to content
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

[ENH] Template-based masking of EPI boldrefs #1321

Merged
merged 21 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion fmriprep/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
ReadSidecarJSON, DerivativesDataSink, BIDSDataGrabber, BIDSFreeSurferDir, BIDSInfo
)
from .images import (
IntraModalMerge, ValidateImage, TemplateDimensions, Conform
IntraModalMerge, ValidateImage, TemplateDimensions, Conform, MatchHeader
)
from .freesurfer import (
StructuralReference, MakeMidthickness, FSInjectBrainExtracted,
Expand Down
45 changes: 45 additions & 0 deletions fmriprep/interfaces/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,51 @@ def _run_interface(self, runtime):
return runtime


class MatchHeaderInputSpec(BaseInterfaceInputSpec):
reference = File(exists=True, mandatory=True,
desc='NIfTI file with reference header')
in_file = File(exists=True, mandatory=True,
desc='NIfTI file which header will be checked')


class MatchHeaderOutputSpec(TraitedSpec):
out_file = File(exists=True, desc='NIfTI file with fixed header')


class MatchHeader(SimpleInterface):
input_spec = MatchHeaderInputSpec
output_spec = MatchHeaderOutputSpec

def _run_interface(self, runtime):
refhdr = nb.load(self.inputs.reference).header.copy()
imgnii = nb.load(self.inputs.in_file)
imghdr = imgnii.header.copy()

imghdr['dim_info'] = refhdr['dim_info'] # dim_info is lost sometimes

# Set qform
qform, qcode = refhdr.get_qform(coded=True)
Copy link
Member

Choose a reason for hiding this comment

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

if not np.allclose(qform, imghdr.get_qform()):
LOGGER.warning(
'q-forms of reference and mask are substantially different')
imghdr.set_qform(qform, int(qcode))

# Set sform
sform, scode = refhdr.get_sform(coded=True)
Copy link
Member

Choose a reason for hiding this comment

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

if not np.allclose(sform, imghdr.get_sform()):
LOGGER.warning(
's-forms of reference and mask are substantially different')
imghdr.set_sform(sform, int(scode))

out_file = fname_presuffix(self.inputs.in_file, suffix='_hdr',
newpath=runtime.cwd)

imgnii.__class__(imgnii.get_data(), imgnii.affine, imghdr).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.

I don't think this will work how you want. You may want to add tests.

If imgnii.affine does not match the result of imgnii.header.get_best_affine(), it will be pushed into the header, probably as the sform. If you've just put refhdr.get_sform() into the imghdr sform matrix, then these are unlikely to match, and you'll be pushing the original affine right back in.

A better approach would be:

imgnii.__class__(imgnii.get_data(), imghdr.get_best_affine(), imghdr).to_filename(
    out_file)

self._results['out_file'] = out_file
return runtime


def reorient(in_file, newpath=None):
"""Reorient Nifti files to RAS"""
out_file = fname_presuffix(in_file, suffix='_ras', newpath=newpath)
Expand Down
10 changes: 8 additions & 2 deletions fmriprep/workflows/bold/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from niworkflows.interfaces.utils import CopyXForm

from ...engine import Workflow
from ...interfaces import ValidateImage
from ...interfaces import ValidateImage, MatchHeader


DEFAULT_MEMORY_MIN_GB = 0.01
Expand Down Expand Up @@ -239,6 +239,10 @@ def init_enhance_and_skullstrip_bold_wf(
operation='max', kernel_shape='sphere', kernel_size=3.0,
internal_datatype='char'), name='pre_mask_dilate')

# Ensure mask's header matches reference's
check_hdr = pe.Node(MatchHeader(), name='check_hdr',
run_without_submitting=True)

# Run N4 normally, force num_threads=1 for stability (images are small, no need for >1)
n4_correct = pe.Node(ants.N4BiasFieldCorrection(dimension=3, copy_header=True),
name='n4_correct', n_procs=1)
Expand Down Expand Up @@ -322,7 +326,9 @@ def init_enhance_and_skullstrip_bold_wf(
])

workflow.connect([
(pre_dilate, n4_correct, [('out_file', 'mask_image')]),
(inputnode, check_hdr, [('in_file', 'reference')]),
(pre_dilate, check_hdr, [('out_file', 'in_file')]),
(check_hdr, n4_correct, [('out_file', 'mask_image')]),
(inputnode, n4_correct, [('in_file', 'input_image')]),
(inputnode, fixhdr_unifize, [('in_file', 'hdr_file')]),
(inputnode, fixhdr_skullstrip2, [('in_file', 'hdr_file')]),
Expand Down