Transform

class menpo.transform.base.Transform[source]

Bases: object

Abstract representation of any spatial transform.

Provides a unified interface to apply the transform with apply_inplace() and apply().

All Transforms support basic composition to form a TransformChain.

There are two useful forms of composition. Firstly, the mathematical composition symbol o has the following definition:

let a(x) and b(x) be two transforms on x.
(a o b)(x) == a(b(x))

This functionality is provided by the compose_after() family of methods:

(a.compose_after(b)).apply(x) == a.apply(b.apply(x))

Equally useful is an inversion the order of composition - so that over time a large chain of transforms can be built to do a useful job, and composing on this chain adds another transform to the end (after all other preceding transforms have been performed).

For instance, let’s say we want to rescale a PointCloud p around it’s mean, and then translate it some place else. It would be nice to be able to do something like:

t = Translation(-p.centre)  # translate to centre
s = Scale(2.0)  # rescale
move = Translate([10, 0 ,0])  # budge along the x axis
t.compose(s).compose(-t).compose(move)

In Menpo, this functionality is provided by the compose_before() family of methods:

(a.compose_before(b)).apply(x) == b.apply(a.apply(x))

For native composition, see the ComposableTransform subclass and the VComposable mix-in. For inversion, see the Invertible and VInvertible mix-ins. For alignment, see the Alignment mix-in.

_apply(x, **kwargs)[source]

Applies the transform to the array x, returning the result.

This method does the actual work of transforming the data, and is the one that subclasses must implement. apply() and apply_inplace() both call this method to do that actual work.

Parameters:

x : (n_points, n_dims) ndarray

The array to be transformed.

kwargs : dict

Subclasses may need these in their _apply methods.

Returns:

transformed : (n_points, n_dims_output) ndarray

The transformed array

apply(x, **kwargs)[source]

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.

kwargs : dict

Passed through to _apply().

Returns:

transformed : type(x)

The transformed object or array

apply_inplace(x, **kwargs)[source]

Applies this transform to a Transformable x destructively.

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

Parameters:

x : Transformable

The Transformable object to be transformed.

kwargs : dict

Passed through to _apply().

Returns:

transformed : type(x)

The transformed object

compose_after(transform)[source]

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)[source]

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.

n_dims[source]

The dimensionality of the data the transform operates on.

None if the transform is not dimension specific.

Type:int or None
n_dims_output[source]

The output of the data from the transform.

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

Type:int or None