Skip to content
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
1 change: 1 addition & 0 deletions nitransforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

transform
"""

from . import linear, manip, nonlinear, surface
from .linear import Affine, LinearTransformsMapping
from .nonlinear import DenseFieldTransform
Expand Down
4 changes: 2 additions & 2 deletions nitransforms/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def cli_apply(pargs):

xfm = (
nlinload(pargs.transform, fmt=fmt)
if pargs.nonlinear else
linload(pargs.transform, fmt=fmt)
if pargs.nonlinear
else linload(pargs.transform, fmt=fmt)
)

# ensure a reference is set
Expand Down
5 changes: 4 additions & 1 deletion nitransforms/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""py.test configuration."""

import os
from pathlib import Path
import numpy as np
Expand Down Expand Up @@ -64,7 +65,9 @@ def _reorient(path):
newaff = imgaff.copy()
newaff[0, 0] *= -1.0
newaff[0, 3] = imgaff.dot(np.hstack((np.array(img.shape[:3]) - 1, 1.0)))[0]
_data["LAS"] = nb.Nifti1Image(np.flip(np.asanyarray(img.dataobj), 0), newaff, img.header)
_data["LAS"] = nb.Nifti1Image(
np.flip(np.asanyarray(img.dataobj), 0), newaff, img.header
)
_data["LAS"].set_data_dtype(img.get_data_dtype())
newaff = imgaff.copy()
newaff[0, 0] *= -1.0
Expand Down
18 changes: 11 additions & 7 deletions nitransforms/io/afni.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Read/write AFNI's transforms."""

from math import pi
import numpy as np
from nibabel.affines import (
Expand Down Expand Up @@ -132,15 +133,16 @@ def to_ras(self, moving=None, reference=None):
"""Return a nitransforms' internal RAS matrix."""

pre_rotation = post_rotation = np.eye(4)
if reference is not None and _is_oblique(ref_aff := _ensure_image(reference).affine):
if reference is not None and _is_oblique(
ref_aff := _ensure_image(reference).affine
):
pre_rotation = _cardinal_rotation(ref_aff, True)
if moving is not None and _is_oblique(mov_aff := _ensure_image(moving).affine):
post_rotation = _cardinal_rotation(mov_aff, False)

return np.stack([
post_rotation @ (xfm.to_ras() @ pre_rotation)
for xfm in self.xforms
])
return np.stack(
[post_rotation @ (xfm.to_ras() @ pre_rotation) for xfm in self.xforms]
)

def to_string(self):
"""Convert to a string directly writeable to file."""
Expand All @@ -161,7 +163,9 @@ def from_ras(cls, ras, moving=None, reference=None):

pre_rotation = post_rotation = np.eye(4)

if reference is not None and _is_oblique(ref_aff := _ensure_image(reference).affine):
if reference is not None and _is_oblique(
ref_aff := _ensure_image(reference).affine
):
pre_rotation = _cardinal_rotation(ref_aff, False)
if moving is not None and _is_oblique(mov_aff := _ensure_image(moving).affine):
post_rotation = _cardinal_rotation(mov_aff, True)
Expand Down Expand Up @@ -198,7 +202,7 @@ def from_image(cls, imgobj):
hdr = imgobj.header.copy()
shape = hdr.get_data_shape()

if len(shape) != 5 or shape[-2] != 1 or not shape[-1] in (2, 3):
if len(shape) != 5 or shape[-2] != 1 or shape[-1] not in (2, 3):
raise TransformFileError(
'Displacements field "%s" does not come from AFNI.'
% imgobj.file_map["image"].filename
Expand Down
1 change: 1 addition & 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
Expand Down
16 changes: 11 additions & 5 deletions nitransforms/io/fsl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Read/write FSL's transforms."""

import os
import warnings
import numpy as np
Expand Down Expand Up @@ -41,7 +42,9 @@ def from_ras(cls, ras, moving=None, reference=None):
moving = reference

if reference is None:
raise TransformIOError("Cannot build FSL linear transform without a reference")
raise TransformIOError(
"Cannot build FSL linear transform without a reference"
)

reference = _ensure_image(reference)
moving = _ensure_image(moving)
Expand Down Expand Up @@ -78,7 +81,9 @@ def from_string(cls, string):
def to_ras(self, moving=None, reference=None):
"""Return a nitransforms internal RAS+ matrix."""
if reference is None:
raise TransformIOError("Cannot build FSL linear transform without a reference")
raise TransformIOError(
"Cannot build FSL linear transform without a reference"
)

if moving is None:
warnings.warn(
Expand Down Expand Up @@ -180,10 +185,11 @@ def from_image(cls, imgobj):
hdr = imgobj.header.copy()
shape = hdr.get_data_shape()

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

field = np.squeeze(np.asanyarray(imgobj.dataobj))
field[..., 0] *= -1.0
Expand Down
14 changes: 7 additions & 7 deletions nitransforms/io/itk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Read/write ITK transforms."""

import warnings
import numpy as np
from scipy.io import loadmat as _read_mat, savemat as _save_mat
Expand Down Expand Up @@ -138,8 +139,7 @@ def from_matlab_dict(cls, mdict, index=0):
sa = tf.structarr

affine = mdict.get(
"AffineTransform_double_3_3",
mdict.get("AffineTransform_float_3_3")
"AffineTransform_double_3_3", mdict.get("AffineTransform_float_3_3")
)

if affine is None:
Expand Down Expand Up @@ -337,7 +337,7 @@ def from_image(cls, imgobj):
hdr = imgobj.header.copy()
shape = hdr.get_data_shape()

if len(shape) != 5 or shape[-2] != 1 or not shape[-1] in (2, 3):
if len(shape) != 5 or shape[-2] != 1 or shape[-1] not in (2, 3):
raise TransformFileError(
'Displacements field "%s" does not come from ITK.'
% imgobj.file_map["image"].filename
Expand Down Expand Up @@ -412,7 +412,9 @@ def from_h5obj(cls, fileobj, check=True, only_linear=False):
# ITK uses Fortran ordering, like NIfTI, but with the vector dimension first
field = np.moveaxis(
np.reshape(
xfm[f"{typo_fallback}Parameters"], (3, *shape.astype(int)), order='F'
xfm[f"{typo_fallback}Parameters"],
(3, *shape.astype(int)),
order="F",
),
0,
-1,
Expand All @@ -422,9 +424,7 @@ def from_h5obj(cls, fileobj, check=True, only_linear=False):
hdr.set_intent("vector")
hdr.set_data_dtype("float")

xfm_list.append(
Nifti1Image(field.astype("float"), LPS @ affine, hdr)
)
xfm_list.append(Nifti1Image(field.astype("float"), LPS @ affine, hdr))
continue

raise TransformIOError(
Expand Down
Loading
Loading