tools.affine_transform

tools.affine_transform#

bartorch.tools.affine_transform()#

Resample input on an affinely mapped grid: out(u) = input(matrix @ [u, 1]).

The matrix maps output positions to input positions, each in units of its own grid’s field of view with the origin at index n // 2: voxel p of a grid of n is at (p - n // 2) / n. A translation t moves the content by -t * n voxels, and the identity with an oshape other than the input’s rescales the field of view onto the new grid. The grid is zero outside [0, n - 1]. BART’s interpolate -A.

Parameters:
  • input (torch.Tensor) – Two or three spatial axes, after leading axes of size one only.

  • matrix (torch.Tensor) – Real (k, k + 1) or homogeneous (k + 1, k + 1), k = len(axes), acting on column vectors [u_0, ..., u_{k-1}, 1] with u_i the position along axes[i].

  • axes (tuple of int) – The last two or three axes, in the order of the matrix’s rows and columns.

  • oshape (tuple of int, default=None) – Output size of the last k axes, C order; the input’s by default.

  • order ({0, 1, 3}, default=1) – Nearest neighbour, linear, or Keys cubic; see bartorch.interpolate().

Returns:

input.shape with its last k axes of size oshape.

Return type:

torch.Tensor

Raises:

ValueError – If axes are not the last two or three, a leading axis exceeds one, or matrix has the wrong shape or a homogeneous last row other than [0, ..., 0, 1].

Examples

Swap the two axes of a square image (its transpose):

>>> affine_transform(image, torch.tensor([[0.0, 1, 0], [1, 0, 0]]), axes=(-2, -1))