DifferentiablePiecewiseAffine

class menpofit.transform.DifferentiablePiecewiseAffine(source, target)[source]

Bases: CachedPWA, DL, DX

A differentiable Piecewise Affine Transformation.

This is composed of a number of triangles defined be a set of source and target vertices. These vertices are related by a common triangle list. No limitations on the nature of the triangle list are imposed. Points can then be mapped via barycentric coordinates from the source to the target space. Trying to map points that are not contained by any source triangle throws a TriangleContainmentError, which contains diagnostic information.

The transform can compute its own derivative with respect to spatial changes, as well as anchor landmark changes.

aligned_source()

The result of applying self to source

Type

PointCloud

alignment_error()

The Frobenius Norm of the difference between the target and the aligned source.

Type

float

apply(x, batch_size=None, **kwargs)

Applies this transform to x.

If x is Transformable, x will be handed this transform object to transform itself non-destructively (a transformed copy of the object will be returned).

If not, x is assumed to be an ndarray. The transformation will be non-destructive, returning the transformed version.

Any kwargs will be passed to the specific transform _apply() method.

Parameters
  • x (Transformable or (n_points, n_dims) ndarray) – The array or object to be transformed.

  • batch_size (int, optional) – If not None, this determines how many items from the numpy array will be passed through the transform at a time. This is useful for operations that require large intermediate matrices to be computed.

  • kwargs (dict) – Passed through to _apply().

Returns

transformed (type(x)) – The transformed object or array

apply_inplace(*args, **kwargs)

Deprecated as public supported API, use the non-mutating apply() instead.

For internal performance-specific uses, see _apply_inplace().

compose_after(transform)

Returns a TransformChain that represents this transform composed after the given transform:

c = a.compose_after(b)
c.apply(p) == a.apply(b.apply(p))

a and b are left unchanged.

This corresponds to the usual mathematical formalism for the compose operator, o.

Parameters

transform (Transform) – Transform to be applied before self

Returns

transform (TransformChain) – The resulting transform chain.

compose_before(transform)

Returns a TransformChain that represents this transform composed before the given transform:

c = a.compose_before(b)
c.apply(p) == b.apply(a.apply(p))

a and b are left unchanged.

Parameters

transform (Transform) – Transform to be applied after self

Returns

transform (TransformChain) – The resulting transform chain.

copy()

Generate an efficient copy of this object.

Note that Numpy arrays and other Copyable objects on self will be deeply copied. Dictionaries and sets will be shallow copied, and everything else will be assigned (no copy will be made).

Classes that store state other than numpy arrays and immutable types should overwrite this method to ensure all state is copied.

Returns

type(self) – A copy of this object

d_dl(points)[source]

The derivative of the warp with respect to spatial changes in anchor landmark points or centres, evaluated at points.

Parameters

points ((n_points, n_dims) ndarray) – The spatial points at which the derivative should be evaluated.

Returns

d_dl ((n_points, n_centres, n_dims) ndarray) – The Jacobian wrt landmark changes.

d_dl[i, k, m] is the scalar differential change that the any dimension of the i’th point experiences due to a first order change in the m’th dimension of the k’th landmark point.

Note that at present this assumes that the change in every dimension is equal.

d_dx(points)[source]

The first order derivative of the warp with respect to spatial changes evaluated at points.

Parameters

points ((n_points, n_dims) ndarray) – The spatial points at which the derivative should be evaluated.

Returns

d_dx ((n_points, n_dims, n_dims) ndarray) – The Jacobian wrt spatial changes.

d_dx[i, j, k] is the scalar differential change that the j’th dimension of the i’th point experiences due to a first order change in the k’th dimension.

It may be the case that the Jacobian is constant across space - in this case axis zero may have length 1 to allow for broadcasting.

:raises TriangleContainmentError:: If any point is outside any triangle of this PWA.

index_alpha_beta(points)

Finds for each input point the index of its bounding triangle and the alpha and beta value for that point in the triangle. Note this means that the following statements will always be true:

alpha + beta <= 1
alpha >= 0
beta >= 0

for each triangle result.

Trying to map a point that does not exist in a triangle throws a TriangleContainmentError.

Parameters

points ((K, 2) ndarray) – Points to test.

Returns

  • tri_index ((L,) ndarray) – Triangle index for each of the points, assigning each point to it’s containing triangle.

  • alpha ((L,) ndarray) – Alpha for containing triangle of each point.

  • beta ((L,) ndarray) – Beta for containing triangle of each point.

Raises

TriangleContainmentError – All points must be contained in a source triangle. Check error.points_outside_source_domain to handle this case.

pseudoinverse()

The pseudoinverse of the transform - that is, the transform that results from swapping source and target, or more formally, negating the transforms parameters. If the transform has a true inverse this is returned instead.

Type

type(self)

set_target(new_target)

Update this object so that it attempts to recreate the new_target.

Parameters

new_target (PointCloud) – The new target that this object should try and regenerate.

property has_true_inverse

The inverse is true.

Type

True

property n_dims

The number of dimensions of the target.

Type

int

property n_dims_output

The output of the data from the transform.

None if the output of the transform is not dimension specific.

Type

int or None

property n_points

The number of points on the target.

Type

int

property n_tris

The number of triangles in the triangle list.

Type

int

property source

The source PointCloud that is used in the alignment.

The source is not mutable.

Type

PointCloud

property target

The current PointCloud that this object produces.

To change the target, use set_target().

Type

PointCloud

property trilist

The triangle list.

Type

(n_tris, 3) ndarray