pypeit.core.transform module

Provide basic coordinate tranformation functions.

pypeit.core.transform.affine_transform_matrix(scale=None, rotation=None, translation=None)[source]

Construct a two-dimensional affine transformation matrix in homogeneous coordinates.

The coordinate convention is to assume the coordinates are ordered in an array with the Cartesian \(x\) in the first column and Cartesian \(y\) in the second column. See the examples below.

This function currently does not allow for a shear term. The transformation is returned as a numpy.ndarray in homogeneous coordinates.

If no arguments are provided, this simply returns an identity matrix.

Otherwise, the order of operations is to scale, rotate, and then translate (shift). I.e., the following should pass:

t = affine_transform_matrix(translation=[2,1])
r = affine_transform_matrix(rotation=np.pi/3)
a1 = affine_transform_matrix(rotation=np.pi/3, translation=[2,1])
assert np.array_equal(t @ r, a1)

s = affine_transform_matrix(scale=(1.5, 2.))
a2 = affine_transform_matrix(scale=(1.5, 2.), rotation=np.pi/3, translation=[2,1])
assert np.array_equal(t @ r @ s, a2)
Parameters:
  • scale (float, array-like, optional) – Scale factor to apply to each axis. Can be a single float to apply to both axes, or a two-element array-like with the scaling for each axis.

  • rotation (float, optional) – Counter-clockwise rotation in radians. The ordinate is aligned with the first axis, and the abcissa is aligned with the second axis; see coordinate_transform_2d(). If None, the rotation is 0.

  • translation (array-like, optional) – An array with two elements that provide the shift to apply in each dimension.

Returns:

A \(3\times3\) affine-transformation matrix.

Return type:

numpy.ndarray

Examples

Rotate the unit box by 45 degrees:

>>> import numpy as np
>>> from pypeit.core import transform
>>> # Cartesian:     x   y
>>> coo = np.array([[0., 0.],
                    [1., 0.],
                    [1., 1.],
                    [0., 1.]])
>>> tform = transform.affine_transform_matrix(rotation=np.radians(45.))
>>> tform
array([[ 0.70710678, -0.70710678,  0.        ],
       [ 0.70710678,  0.70710678,  0.        ],
       [ 0.        ,  0.        ,  1.        ]])
>>> transform.coordinate_transform_2d(coo, tform)
array([[ 0.00000000e+00,  0.00000000e+00],
       [ 7.07106781e-01,  7.07106781e-01],
       [ 1.11022302e-16,  1.41421356e+00],
       [-7.07106781e-01,  7.07106781e-01]])

Rotation about the center by first shifting, then rotating, then shifting back.

>>> shift = affine_transform_matrix(translation=[-0.5,-0.5])
>>> _tform = np.linalg.inv(shift) @ tform @ shift
>>> transform.coordinate_transform_2d(coo, _tform)
array([[ 0.5       , -0.20710678],
       [ 1.20710678,  0.5       ],
       [ 0.5       ,  1.20710678],
       [-0.20710678,  0.5       ]])
pypeit.core.transform.affine_transform_series(steps)[source]

Construct an affine transform matrix from a set of transformation steps executed in series.

Each step in the transformation is provided by a call to pypeit.core.transform.affine_transform_matrix(). The order of the steps should be provided in the order they should be preformed. The following should pass:

steps = [dict(scale=(1.5,2)), dict(rotation=np.pi/3), dict(translation=[2,1])]
a1 = affine_transform_series(steps)
a2 = affine_transform_matrix(scale=steps[0]['scale'], rotation=steps[1]['rotation'],
                             translation=steps[2]['translation'])
assert np.array_equal(a1, a2)
Parameters:

steps (array-like) – A list of dictionaries with each dictionary containing the keyword arguments for a single call to pypeit.core.transform.affine_transform_matrix().

Returns:

A \(3\times3\) affine-transformation matrix.

Return type:

numpy.ndarray

pypeit.core.transform.coordinate_transform_2d(coo, matrix, inverse=False)[source]

Apply a 2D coordinate transformation using an affine-transformation matrix in homologous coordinates.

Parameters:
  • coo (array-like) – Coordinates to transform. Shape must be \((N,2)\), where \(N\) is the number of coordinates, Cartesian \(x\) is in the first column (coo[:,0]), and Cartesian \(y\) is in the second column (coo[:,1]).

  • matrix (numpy.ndarray) – The :math:3times 3` affine-transformation matrix. See affine_transform_matrix().

  • inverse (bool, optional) – By default, the function performs the active transformation; i.e., applying the transformation to the coordinates, moving them within the existing coordinate frame. Set inverse to true to instead perform the passive transformation; i.e., transforming the coordinate axes and providing the new coordinates in the transformed (e.g., rotated) frame.

Returns:

Transformed coordinates with shape \((N,2)\).

Return type:

numpy.ndarray