Manipulations

Common interface for transforms.

class nitransforms.manip.TransformChain(transforms=None)[source]

Implements the concatenation of transforms.

append(x)[source]

Concatenate one element to the chain.

Example

>>> chain = TransformChain(transforms=TransformBase())
>>> chain.append((TransformBase(), TransformBase()))
>>> len(chain)
3
asaffine(indices=None)[source]

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.]])
>>> chain = TransformChain(transforms=[
...     Affine.from_matvec(vec=(1, 2, 3)),
...     Affine.from_matvec(mat=[[0, 1, 0], [0, 0, 1], [1, 0, 0]]),
... ])
>>> chain.asaffine()
array([[0., 1., 0., 2.],
       [0., 0., 1., 3.],
       [1., 0., 0., 1.],
       [0., 0., 0., 1.]])
>>> np.allclose(
...     chain.map((4, -2, 1)),
...     chain.asaffine().map((4, -2, 1)),
... )
True
Parameters:

indices (numpy.array_like) – The indices of the values to extract.

classmethod from_filename(filename, fmt='X5', reference=None, moving=None)[source]

Load a transform file.

insert(i, x)[source]

Insert an item at a given position.

Example

>>> chain = TransformChain(transforms=[TransformBase(), TransformBase()])
>>> chain.insert(1, TransformBase())
>>> len(chain)
3
>>> chain.insert(1, TransformChain(chain))
>>> len(chain)
6
map(x, inverse=False)[source]

Apply a succession of transforms, e.g., \(y = f_3(f_2(f_1(f_0(x))))\).

Example

>>> chain = TransformChain(transforms=[TransformBase(), TransformBase()])
>>> chain([(0., 0., 0.), (1., 1., 1.), (-1., -1., -1.)])
[(0.0, 0.0, 0.0), (1.0, 1.0, 1.0), (-1.0, -1.0, -1.0)]
>>> chain([(0., 0., 0.), (1., 1., 1.), (-1., -1., -1.)], inverse=True)
[(0.0, 0.0, 0.0), (1.0, 1.0, 1.0), (-1.0, -1.0, -1.0)]
>>> TransformChain()((0., 0., 0.))  
Traceback (most recent call last):
TransformError:
property ndim

Get the number of dimensions.

property transforms

Get the internal list of transforms.

nitransforms.manip.load(filename, fmt='X5', reference=None, moving=None)

Load a transform file.