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

FIX: Output displacement fields #182

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions nitransforms/io/afni.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ def from_image(cls, imgobj):

return imgobj.__class__(field, imgobj.affine, hdr)

@classmethod
def to_image(cls, imgobj):
"""Export a displacements field from a nibabel object."""

hdr = imgobj.header.copy()

warp_data = imgobj.get_fdata().reshape(imgobj.shape[:3] + (1, imgobj.shape[-1]))
oesteban marked this conversation as resolved.
Show resolved Hide resolved
warp_data[..., (0, 1)] *= -1

return imgobj.__class__(warp_data, imgobj.affine, hdr)


def _is_oblique(affine, thres=OBLIQUITY_THRESHOLD_DEG):
"""
Expand Down
11 changes: 11 additions & 0 deletions nitransforms/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ def from_image(cls, imgobj):
"""Import a displacements field from a nibabel image object."""
raise NotImplementedError

@classmethod
def to_filename(cls, img, filename):
"""Export a displacements field to a NIfTI file."""
imgobj = cls.to_image(img)
imgobj.to_filename(filename)

@classmethod
def to_image(cls, imgobj):
"""Export a displacements field image from a nitransforms image object."""
raise NotImplementedError


def _ensure_image(img):
if isinstance(img, (str, Path)):
Expand Down
11 changes: 11 additions & 0 deletions nitransforms/io/fsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ def from_image(cls, imgobj):

return imgobj.__class__(field, imgobj.affine, hdr)

@classmethod
def to_image(cls, imgobj):
"""Export a displacements field from a nibabel object."""

hdr = imgobj.header.copy()

warp_data = imgobj.get_fdata()
warp_data[..., 0] *= -1

return imgobj.__class__(warp_data, imgobj.affine, hdr)


def _fsl_aff_adapt(space):
"""
Expand Down
12 changes: 12 additions & 0 deletions nitransforms/io/itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ def from_image(cls, imgobj):

return imgobj.__class__(field, imgobj.affine, hdr)

@classmethod
def to_image(cls, imgobj):
"""Export a displacements field from a nibabel object."""

hdr = imgobj.header.copy()
hdr.set_intent("vector")

warp_data = imgobj.get_fdata().reshape(imgobj.shape[:3] + (1, imgobj.shape[-1]))
warp_data[..., (0, 1)] *= -1

return imgobj.__class__(warp_data, imgobj.affine, hdr)


class ITKCompositeH5:
"""A data structure for ITK's HDF5 files."""
Expand Down