Skip to content

ENH: I/O of FSL displacements fields #51

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 3 commits into from
Feb 16, 2022
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
21 changes: 21 additions & 0 deletions nitransforms/io/fsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .base import (
BaseLinearTransformList,
LinearParameters,
DisplacementsField,
TransformFileError,
_ensure_image,
)
Expand Down Expand Up @@ -171,6 +172,26 @@ def from_filename(cls, filename):
return _self


class FSLDisplacementsField(DisplacementsField):
"""A data structure representing displacements fields."""

@classmethod
def from_image(cls, imgobj):
"""Import a displacements field from a NIfTI file."""
hdr = imgobj.header.copy()
shape = hdr.get_data_shape()

if len(shape) != 4 or not shape[-1] in (2, 3):
raise TransformFileError(
'Displacements field "%s" does not come from FSL.' %
imgobj.file_map['image'].filename)

field = np.squeeze(np.asanyarray(imgobj.dataobj))
field[..., 0] *= -1.0

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


def _fsl_aff_adapt(space):
"""
Adapt FSL affines.
Expand Down
17 changes: 9 additions & 8 deletions nitransforms/nonlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ def map(self, x, inverse=False):

@classmethod
def from_filename(cls, filename, fmt="X5"):
if fmt == "afni":
_factory = io.afni.AFNIDisplacementsField
elif fmt == "itk":
_factory = io.itk.ITKDisplacementsField
else:
raise NotImplementedError

return cls(_factory.from_filename(filename))
_factory = {
"afni": io.afni.AFNIDisplacementsField,
"itk": io.itk.ITKDisplacementsField,
"fsl": io.fsl.FSLDisplacementsField,
}
if fmt not in _factory:
raise NotImplementedError(f"Unsupported format <{fmt}>")

return cls(_factory[fmt].from_filename(filename))


load = DisplacementsFieldTransform.from_filename
Expand Down
Binary file not shown.
9 changes: 8 additions & 1 deletion nitransforms/tests/test_nonlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..nonlinear import DisplacementsFieldTransform, load as nlload
from ..io.itk import ITKDisplacementsField


TESTS_BORDER_TOLERANCE = 0.05
APPLY_NONLINEAR_CMD = {
"itk": """\
Expand All @@ -22,6 +23,9 @@
3dNwarpApply -nwarp {transform} -source {moving} \
-master {reference} -interp NN -prefix resampled.nii.gz
""".format,
'fsl': """\
applywarp -i {moving} -r {reference} -o resampled.nii.gz \
-w {transform} --interp=nn""".format,
}


Expand Down Expand Up @@ -59,7 +63,10 @@ def test_displacements_field1(tmp_path, get_testdata, image_orientation, sw_tool
os.chdir(str(tmp_path))
nii = get_testdata[image_orientation]
nii.to_filename("reference.nii.gz")
fieldmap = np.zeros((*nii.shape[:3], 1, 3), dtype="float32")
fieldmap = np.zeros(
(*nii.shape[:3], 1, 3) if sw_tool != "fsl" else (*nii.shape[:3], 3),
dtype="float32",
)
fieldmap[..., axis] = -10.0

_hdr = nii.header.copy()
Expand Down