# algorithms.registration.affine

## Module: `algorithms.registration.affine`

Inheritance diagram for `nipy.algorithms.registration.affine`:

## Classes

### `Affine`

class nipy.algorithms.registration.affine.Affine(array=None, radius=100)

  Bases: `Transform`

  __init__(array=None, radius=100)
  apply(xyz)
  as_affine(dtype='double')
  compose(other)

    Compose this transform onto another

    Parameters:
      other Transform

        transform that we compose onto

    Returns:
      composed_transform Transform

        a transform implementing the composition of self on other

  copy()
  from_matrix44(aff)

    Convert a 4x4 matrix describing an affine transform into a 12-sized vector of natural affine parameters: translation, rotation, log-scale, pre-rotation (to allow for shearing when combined with non-unitary scales). In case the transform has a negative determinant, set the _direct attribute to False.

  inv()

    Return the inverse affine transform.

  property is_direct
  property param
  param_inds = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  property pre_rotation
  property precond
  property rotation
  property scaling
  property translation

### `Affine2D`

class nipy.algorithms.registration.affine.Affine2D(array=None, radius=100)

  Bases: `Affine`

  __init__(array=None, radius=100)
  apply(xyz)
  as_affine(dtype='double')
  compose(other)

    Compose this transform onto another

    Parameters:
      other Transform

        transform that we compose onto

    Returns:
      composed_transform Transform

        a transform implementing the composition of self on other

  copy()
  from_matrix44(aff)

    Convert a 4x4 matrix describing an affine transform into a 12-sized vector of natural affine parameters: translation, rotation, log-scale, pre-rotation (to allow for shearing when combined with non-unitary scales). In case the transform has a negative determinant, set the _direct attribute to False.

  inv()

    Return the inverse affine transform.

  property is_direct
  property param
  param_inds = [0, 1, 5, 6, 7, 11]
  property pre_rotation
  property precond
  property rotation
  property scaling
  property translation

### `Rigid`

class nipy.algorithms.registration.affine.Rigid(array=None, radius=100)

  Bases: `Affine`

  __init__(array=None, radius=100)
  apply(xyz)
  as_affine(dtype='double')
  compose(other)

    Compose this transform onto another

    Parameters:
      other Transform

        transform that we compose onto

    Returns:
      composed_transform Transform

        a transform implementing the composition of self on other

  copy()
  from_matrix44(aff)

    Convert a 4x4 matrix describing a rigid transform into a 12-sized vector of natural affine parameters: translation, rotation, log-scale, pre-rotation (to allow for pre-rotation when combined with non-unitary scales). In case the transform has a negative determinant, set the _direct attribute to False.

  inv()

    Return the inverse affine transform.

  property is_direct
  property param
  param_inds = [0, 1, 2, 3, 4, 5]
  property pre_rotation
  property precond
  property rotation
  property scaling
  property translation

### `Rigid2D`

class nipy.algorithms.registration.affine.Rigid2D(array=None, radius=100)

  Bases: `Rigid`

  __init__(array=None, radius=100)
  apply(xyz)
  as_affine(dtype='double')
  compose(other)

    Compose this transform onto another

    Parameters:
      other Transform

        transform that we compose onto

    Returns:
      composed_transform Transform

        a transform implementing the composition of self on other

  copy()
  from_matrix44(aff)

    Convert a 4x4 matrix describing a rigid transform into a 12-sized vector of natural affine parameters: translation, rotation, log-scale, pre-rotation (to allow for pre-rotation when combined with non-unitary scales). In case the transform has a negative determinant, set the _direct attribute to False.

  inv()

    Return the inverse affine transform.

  property is_direct
  property param
  param_inds = [0, 1, 5]
  property pre_rotation
  property precond
  property rotation
  property scaling
  property translation

### `Similarity`

class nipy.algorithms.registration.affine.Similarity(array=None, radius=100)

  Bases: `Affine`

  __init__(array=None, radius=100)
  apply(xyz)
  as_affine(dtype='double')
  compose(other)

    Compose this transform onto another

    Parameters:
      other Transform

        transform that we compose onto

    Returns:
      composed_transform Transform

        a transform implementing the composition of self on other

  copy()
  from_matrix44(aff)

    Convert a 4x4 matrix describing a similarity transform into a 12-sized vector of natural affine parameters: translation, rotation, log-scale, pre-rotation (to allow for pre-rotation when combined with non-unitary scales). In case the transform has a negative determinant, set the _direct attribute to False.

  inv()

    Return the inverse affine transform.

  property is_direct
  property param
  param_inds = [0, 1, 2, 3, 4, 5, 6]
  property pre_rotation
  property precond
  property rotation
  property scaling
  property translation

### `Similarity2D`

class nipy.algorithms.registration.affine.Similarity2D(array=None, radius=100)

  Bases: `Similarity`

  __init__(array=None, radius=100)
  apply(xyz)
  as_affine(dtype='double')
  compose(other)

    Compose this transform onto another

    Parameters:
      other Transform

        transform that we compose onto

    Returns:
      composed_transform Transform

        a transform implementing the composition of self on other

  copy()
  from_matrix44(aff)

    Convert a 4x4 matrix describing a similarity transform into a 12-sized vector of natural affine parameters: translation, rotation, log-scale, pre-rotation (to allow for pre-rotation when combined with non-unitary scales). In case the transform has a negative determinant, set the _direct attribute to False.

  inv()

    Return the inverse affine transform.

  property is_direct
  property param
  param_inds = [0, 1, 5, 6]
  property pre_rotation
  property precond
  property rotation
  property scaling
  property translation

## Functions

nipy.algorithms.registration.affine.inverse_affine(affine)

nipy.algorithms.registration.affine.preconditioner(radius)

  Computes a scaling vector pc such that, if p=(u,r,s,q) represents affine transformation parameters, where u is a translation, r and q are rotation vectors, and s is the vector of log-scales, then all components of (p/pc) are roughly comparable to the translation component.

  To that end, we use a radius parameter which represents the ‘typical size’ of the object being registered. This is used to reformat the parameter vector (translation+rotation+scaling+pre-rotation) so that each element roughly represents a variation in mm.

nipy.algorithms.registration.affine.rotation_mat2vec(R)

  Rotation vector from rotation matrix R

  Parameters:
    R(3,3) array-like

      Rotation matrix

  Returns:
    vec(3,) array

      Rotation vector, where norm of vec is the angle `theta`, and the axis of rotation is given by `vec / theta`

nipy.algorithms.registration.affine.rotation_vec2mat(r)

  The rotation matrix is given by the Rodrigues formula:

  R = Id + sin(theta)*Sn + (1-cos(theta))*Sn^2

  with:

  > 0 -nz ny

  Sn = nz 0 -nx
    `-ny`

      nx 0

  where n = r / ||r||

  In case the angle ||r|| is very small, the above formula may lead to numerical instabilities. We instead use a Taylor expansion around theta=0:

  R = I + sin(theta)/tetha Sr + (1-cos(theta))/teta2 Sr^2

  leading to:

  R = I + (1-theta2/6)*Sr + (1/2-theta2/24)*Sr^2

  To avoid numerical instabilities, an upper threshold is applied to the angle. It is chosen to be a multiple of 2*pi, hence the resulting rotation is then the identity matrix. This strategy warrants that the output matrix is a continuous function of the input vector.

nipy.algorithms.registration.affine.slices2aff(slices)

  Return affine from start, step of sequence slices of slice objects

  Parameters:
    slices sequence of slice objects
  Returns:
    aff ndarray

      If `N = len(slices)` then affine is shape (N+1, N+1) with diagonal given by the `step` attribute of the slice objects (where None corresponds to 1), and the :N elements in the last column are given by the `start` attribute of the slice objects

  Examples

      >>> slices2aff([slice(None), slice(None)])
      array([[ 1.,  0.,  0.],
             [ 0.,  1.,  0.],
             [ 0.,  0.,  1.]])
      >>> slices2aff([slice(2, 3, 4), slice(3, 4, 5), slice(4, 5, 6)])
      array([[ 4.,  0.,  0.,  2.],
             [ 0.,  5.,  0.,  3.],
             [ 0.,  0.,  6.,  4.],
             [ 0.,  0.,  0.,  1.]])

nipy.algorithms.registration.affine.subgrid_affine(affine, slices)

  Return dot prodoct of affine and affine resulting from slices

  Parameters:
    affine array-like

      Affine to apply on right of affine resulting from slices

    slices sequence of slice objects

      Slices generating (N+1, N+1) affine from `slices2aff`, where `N = len(slices)`

  Returns:
    aff ndarray

      result of `np.dot(affine, slice_affine)` where `slice_affine` is affine resulting from `slices2aff(slices)`.

  Raises:
    ValueError if the `slice_affine` contains non-integer values

nipy.algorithms.registration.affine.threshold(x, th)

nipy.algorithms.registration.affine.to_matrix44(t)

  t is a vector of affine transformation parameters with size at least 6.

  size < 6 ==> error size == 6 ==> t is interpreted as translation + rotation size == 7 ==> t is interpreted as translation + rotation + isotropic scaling 7 < size < 12 ==> error size >= 12 ==> t is interpreted as translation + rotation + scaling + pre-rotation