Skip to content

Commit

Permalink
enh: review comments
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Markiewicz <[email protected]>
  • Loading branch information
oesteban and effigies committed Feb 15, 2022
1 parent 8683d67 commit db02ecb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
7 changes: 7 additions & 0 deletions nitransforms/io/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Read/write linear transforms."""
from pathlib import Path
import numpy as np
from nibabel import load as loadimg
from scipy.io.matlab.miobase import get_matfile_version
Expand Down Expand Up @@ -174,3 +175,9 @@ def _read_mat(byte_stream):
else:
raise TransformFileError("Not a Matlab file.")
return reader.get_variables()


def _ensure_image(img):
if isinstance(img, (str, Path)):
return loadimg(img)
return img
33 changes: 13 additions & 20 deletions nitransforms/io/fsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
import os
import warnings
import numpy as np
from numpy.linalg import inv
from pathlib import Path
from nibabel import load as _nbload
from nibabel.affines import voxel_sizes

from .base import BaseLinearTransformList, LinearParameters, TransformFileError
from .base import (
BaseLinearTransformList,
LinearParameters,
TransformFileError,
_ensure_image,
)


class FSLLinearTransform(LinearParameters):
Expand Down Expand Up @@ -41,14 +46,14 @@ def from_ras(cls, ras, moving=None, reference=None):

# Adjust for reference image offset and orientation
refswp, refspc = _fsl_aff_adapt(reference)
pre = reference.affine.dot(np.linalg.inv(refspc).dot(np.linalg.inv(refswp)))
pre = reference.affine.dot(inv(refspc).dot(inv(refswp)))

# Adjust for moving image offset and orientation
movswp, movspc = _fsl_aff_adapt(moving)
post = np.linalg.inv(movswp).dot(movspc.dot(np.linalg.inv(moving.affine)))
post = inv(movswp).dot(movspc.dot(inv(moving.affine)))

# Compose FSL transform
mat = np.linalg.inv(np.swapaxes(post.dot(ras.dot(pre)), 0, 1))
mat = inv(np.swapaxes(post.dot(ras.dot(pre)), 0, 1))

tf = cls()
tf.structarr["parameters"] = mat.T
Expand Down Expand Up @@ -84,19 +89,13 @@ def to_ras(self, moving=None, reference=None):
moving = _ensure_image(moving)

refswp, refspc = _fsl_aff_adapt(reference)
pre = reference.affine.dot(np.linalg.inv(refspc).dot(np.linalg.inv(refswp)))

pre = refswp @ refspc @ inv(reference.affine)
# Adjust for moving image offset and orientation
movswp, movspc = _fsl_aff_adapt(moving)
post = np.linalg.inv(movswp).dot(movspc.dot(np.linalg.inv(moving.affine)))

post = moving.affine @ inv(movspc) @ inv(movswp)
mat = self.structarr["parameters"].T

return (
np.linalg.inv(post)
@ np.swapaxes(np.linalg.inv(mat), 0, 1)
@ np.linalg.inv(pre)
)
return post @ np.swapaxes(inv(mat), 0, 1) @ pre


class FSLLinearTransformArray(BaseLinearTransformList):
Expand Down Expand Up @@ -187,9 +186,3 @@ def _fsl_aff_adapt(space):
swp[0, 0] = -1.0
swp[0, 3] = (space.shape[0] - 1) * zooms[0]
return swp, np.diag(zooms)


def _ensure_image(img):
if isinstance(img, (str, Path)):
return _nbload(img)
return img
2 changes: 1 addition & 1 deletion nitransforms/tests/test_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_loadsave_itk(tmp_path, data_path, testdata_path):
)


@pytest.mark.xfail(reason="Not fully implemented")
# @pytest.mark.xfail(reason="Not fully implemented")
@pytest.mark.parametrize("fmt", ["itk", "fsl", "afni", "lta"])
def test_loadsave(tmp_path, data_path, testdata_path, fmt):
"""Test idempotency."""
Expand Down

0 comments on commit db02ecb

Please sign in to comment.