NIPY logo

Site Navigation

NIPY Community

Table Of Contents

Previous topic

core.reference.slices

Next topic

interfaces.matlab

This Page

core.transforms.affines

Module: core.transforms.affines

Inheritance diagram for nipy.core.transforms.affines:

Functions working on affine transformation matrices.

The orientation functions below also exist in nibabel as orientations.py

Class

OrientationError

class nipy.core.transforms.affines.OrientationError

Bases: exceptions.Exception

__init__()

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

args
message

Functions

nipy.core.transforms.affines.apply_orientation(arr, ornt)

Apply transformations implied by ornt to the first n axes of the array arr

Parameters :

arr : array-like of data with ndim >= n

ornt : (n,2) orientation array

orientation transform. ornt[N,1]` is flip of axis N of the array implied by `shape`, where 1 means no flip and -1 means flip.  For example, if ``N==0 and ornt[0,1] == -1, and there’s an array arr of shape shape, the flip would correspond to the effect of np.flipud(arr). ornt[:,0] is the transpose that needs to be done to the implied array, as in arr.transpose(ornt[:,0])

Returns :

t_arr : ndarray

data array arr transformed according to ornt

nipy.core.transforms.affines.flip_axis(arr, axis=0)

Flip contents of axis in array arr

flip_axis is the same transform as np.flipud, but for any axis. For example flip_axis(arr, axis=0) is the same transform as np.flipud(arr), and flip_axis(arr, axis=1) is the same transform as np.fliplr(arr)

Parameters :

arr : array-like

axis : int, optional

axis to flip. Default axis == 0

Returns :

farr : array

Array with axis axis flipped

Examples

>>> a = np.arange(6).reshape((2,3))
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> flip_axis(a, axis=0)
array([[3, 4, 5],
       [0, 1, 2]])
>>> flip_axis(a, axis=1)
array([[2, 1, 0],
       [5, 4, 3]])
nipy.core.transforms.affines.from_matrix_vector(matrix, vector)

Combine a matrix and vector into a homogeneous affine

Combine a rotation matrix and translation vector into a transform in homogeneous coordinates.

Parameters :

matrix : array

An NxM array representing the the linear part of the transform. A transform from an M-dimensional space to an N-dimensional space.

vector : array

A 1xN array representing the translation.

Returns :

xform : array

An N+1xM+1 transform matrix.

See also

to_matrix_vector

nipy.core.transforms.affines.io_orientation(affine, tol=None)

Orientation of input axes in terms of output axes for affine

Valid for an affine transformation from m dimensions to n dimensions (affine.shape == (n+1, m+1)).

The calculated orientations can be used to transform associated arrays to best match the output orientations. If n > m, then some of the output axes should be considered dropped in this orientation.

Parameters :

affine : (n+1,m+1) ndarray-like

Transformation affine from m inputs to n outputs. Usually this will be a shape (4,4) matrix, transforming 3 inputs to 3 outputs, but the code also handles the more general case

tol : {None, float}, optional

threshold below which SVD values of the affine are considered zero. If tol is None, and S is an array with singular values for affine, and eps is the epsilon value for datatype of S, then tol set to S.max() * eps.

Returns :

orientations : (n,2) ndarray

one row per input axis, where the first value in each row is the closest corresponding output axis, and the second value is 1 if the input axis is in the same direction as the corresponding output axis, , and -1 if it is in the opposite direction, to the corresponding output axis. If a row is [np.nan, np.nan], which can happen when n > m, then this row should be considered dropped.

nipy.core.transforms.affines.orientation_affine(ornt, shape)

Affine transform resulting from transforms implied in ornt

Imagine you have an array arr of shape shape, and you apply the transforms implied by ornt (more below), to get tarr. tarr may have a different shape shape_prime. This routine returns the affine that will take a array coordinate for tarr and give you the corresponding array coordinate in arr.

Parameters :

ornt : (n,2) ndarray

orientation transform. ornt[N,1]` is flip of axis N of the array implied by `shape`, where 1 means no flip and -1 means flip.  For example, if ``N==0 and ornt[0,1] == -1, and there’s an array arr of shape shape, the flip would correspond to the effect of np.flipud(arr). ornt[:,0] is the transpose that needs to be done to the implied array, as in arr.transpose(ornt[:,0])

shape : length n sequence

shape of array you may transform with ornt

Returns :

transformed_affine : (n+1,n+1) ndarray

An array arr (shape shape) might be transformed according to ornt, resulting in a transformed array tarr. transformed_affine is the transform that takes you from array coordinates in tarr to array coordinates in arr.

nipy.core.transforms.affines.to_matrix_vector(transform)

Split a transform into its matrix and vector components.

The tranformation must be represented in homogeneous coordinates and is split into its rotation matrix and translation vector components.

Parameters :

transform : array

NxM transform matrix in homogeneous coordinates representing an affine transformation from an (N-1)-dimensional space to an (M-1)-dimensional space. An example is a 4x4 transform representing rotations and translations in 3 dimensions. A 4x3 matrix can represent a 2-dimensional plane embedded in 3 dimensional space.

Returns :

matrix, vector : array

The matrix and vector components of the transform matrix. For an NxM transform, matrix will be N-1xM-1 and vector will be 1xN-1.