Skip to content

Commit

Permalink
enh: add doctest to asaffine()
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed Jul 18, 2022
1 parent ecdda5d commit 533a03d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions nitransforms/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from scipy import ndimage as ndi

from nibabel.loadsave import load as _nbload
from nibabel.affines import from_matvec

from nitransforms.base import (
ImageGrid,
Expand Down Expand Up @@ -218,6 +219,24 @@ def from_filename(cls, filename, fmt=None, reference=None, moving=None):
f"Could not open <{filename}> (formats tried: {', '.join(fmtlist)})."
)

@classmethod
def from_matvec(cls, mat=None, vec=None, reference=None):
"""
Create an affine from a matrix and translation pair.
Example
-------
>>> Affine.from_matvec(vec=(4, 0, 0)) # doctest: +NORMALIZE_WHITESPACE
array([[1., 0., 0., 4.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
"""
mat = mat if mat is not None else np.eye(3)
vec = vec if vec is not None else np.zeros((3,))
return cls(from_matvec(mat, vector=vec), reference=reference)

def __repr__(self):
"""
Change representation to the internal matrix.
Expand Down
12 changes: 12 additions & 0 deletions nitransforms/manip.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ def asaffine(self, indices=None):
"""
Combine a succession of linear transforms into one.
Example
------
>>> chain = TransformChain(transforms=[
... Affine.from_matvec(vec=(2, -10, 3)),
... Affine.from_matvec(vec=(-2, 10, -3)),
... ])
>>> chain.asaffine()
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
Parameters
----------
indices : :obj:`numpy.array_like`
Expand Down

0 comments on commit 533a03d

Please sign in to comment.