Skip to content

Commit

Permalink
Merge pull request #182 from bpinsard/enh/output_displacement_field
Browse files Browse the repository at this point in the history
FIX: Output displacement fields
  • Loading branch information
oesteban authored May 16, 2024
2 parents e792cef + c41fc07 commit 527dc46
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions nitransforms/io/afni.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,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]))
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

0 comments on commit 527dc46

Please sign in to comment.