NIPY logo

Site Navigation

NIPY Community

Table Of Contents

Next topic

io.imageformats.quaternions

This Page

io.imageformats.orientations

Module: io.imageformats.orientations

Inheritance diagram for nipy.io.imageformats.orientations:

Utilities for calculating and applying affine orientations

Class

OrientationError

class nipy.io.imageformats.orientations.OrientationError

Bases: exceptions.Exception

__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

args
message

Functions

nipy.io.imageformats.orientations.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.io.imageformats.orientations.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.io.imageformats.orientations.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.io.imageformats.orientations.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.