2D polynomials and vector fields.

Interface follows object oriented paradigm.

Important functions are documented in Polynomial and PolyVectorField classes.

Currently supported polynomial types are Cartesian, Legendre, Zernike, and Chebyshev.

Examples

The standard usage to create a vector field is

>>> vf = PolyVectorField(Legendre(3))


set its parameter list

>>> vf.set_params(np.random.randn(vf.paramcount))


and evaluate it on a grid:

>>> grid = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
>>> values = vf.model(grid)

class mascado.distortions.polynomials.Cartesian(degree)[source]

Concrete implementation of Polynomial.

full_model(points, *params)[source]
vandermonde(points)[source]
class mascado.distortions.polynomials.Chebyshev(degree)[source]

Concrete implementation of Polynomial.

full_model(points, *params)[source]
vandermonde(points)[source]
class mascado.distortions.polynomials.CoeffPolynomial(degree)[source]

Abstract class for 2D Polynomials representated as coefficient matrix.

Parameters: degree (int) – Maximum degree $$k$$.

Notes

A coefficient matrix is a square matrix where rows denote the degree of the x polynomial and columns the degree of the y polynomial. In the simplest case (Cartesian polynomials) $$1+3x^2+0.5xy$$ is represented as

$\begin{split}\begin{bmatrix} 1 & 0 & 0 \\ 0 & 0.5 & 0 \\ 3 & 0 & 0 \end{bmatrix}\end{split}$

The coefficient matrix is an upper left triangular matrix, because ther lower right triangle represents terms of higher degree.

coeff_to_params(coeff)[source]

Convert coefficient matrix to parameter list.

make_single_degree_subpoly(degree)[source]

Concrete implementation.

params_to_coeff(params)[source]

Convert parameter list to coefficient matrix.

static upper_left_triangular_indices(degree)[source]

Returns indices you can use to convert a flattened array back to an upper left triangular matrix.

Parameters: degree (int) Indices to upper left triangle of a (degree+1, degree+1)-matrix. 2-tuple of (m,)-shaped arrays

Examples

Convert a flattened parameter list to a coefficient matrix:

>>> idxs = upper_left_triangular_indices(degree)
>>> mat = np.zeros((degree+1, degree+1))
>>> mat[idxs] = paramlist

static upper_left_triangular_mask(degree)[source]

Make mask that covers the upper left trangle of a matrix.

Parameters: degree (int) (degree+1, degree+1)-shaped bool array
class mascado.distortions.polynomials.Legendre(degree)[source]

Concrete implementation of Polynomial.

full_model(points, *params)[source]
vandermonde(points)[source]
class mascado.distortions.polynomials.PolyVectorField(xpoly, ypoly=None)[source]

Bases: object

Polynomial vector field mapping from 2D to 2D.

Two polynomials make up the two components of the vector field.

Parameters: xpoly (Polynomial) ypoly (Polynomial) – Optional. If left out or set to None, a copy of xpoly is used. xpoly (Polynomial) – ypoly (Polynomial) – paramcount (int) – Total number of parameters.
copy()[source]

Copy vector field and polynomials with same parameter lists.

Returns: New vector field. mascado.distortions.polynomials.PolyVectorField
full_model(points, *params)[source]

Evaluate model with given parameters.

Parameters: points ((N,2)-shaped array) – x,y points where to evaluate field. params (list) – Concatenated list of parameters for both polynomials. Values at given points. (N,2)-shaped array
get_degree()[source]

Return degree of vector field.

Raises: ValueError – If the degree is not the same for both polynomials.
get_params()[source]

Get internal parameter list.

Returns: Parameter list. Returns None if one or both of the polynomials has no internal parameters. list
make_single_degree_subpoly(degree)[source]

Make new vector field containing terms of a single degree only.

Parameters: degree (int) New vector field of given degree. PolyVectorField
model(points)[source]

Evaluate model with internal parameters.

Parameters: points ((N,2)-shaped array) – x,y points where to evaluate field. Values at given points. (N,2)-shaped array
set_params(params)[source]

Set parameter list for both polynomials.

Parameters: params (list) – Concatenated list of parameters for polynomial in x-direction and then parameters for the y-direction.
vandermonde(points)[source]

Calculate Vandermonde matrix.

Parameters: points ((N,2)-shaped array) Vandermonde matrix. M is the total number of parameters for both polynomials. (2N,M)-shaped array

Notes

For a vector field of a x polynomial with Vandermonde matrix $$V_x$$ and a y polynomial with Vandermonde matrix $$V_y$$ the combined matrix is

$\begin{split}V = \begin{bmatrix} V_x & \mathbf{0} \\ \mathbf{0} & V_y \\ \end{bmatrix}\,.\end{split}$
class mascado.distortions.polynomials.Polynomial(degree)[source]

Bases: abc.ABC

Polynomials mapping from 2D to 1D.

Parameters: degree (int) degree (int) – Maximum degree $$k$$ of this polynomial. paramcount (int) – Number of parameters $$m$$, calculated as $$\binom{k+2}{2}$$.

Notes

A polynomial is build from a 1D basis set $$P_i(x)$$ using the parameters $$a_{ij}$$ like this:

$P(x, y) = \sum_{i=0}^k \sum_{j=0}^i a_{ij} P_j(x) P_{i-j}(y)\,.$

A flattened parameter list looks like this:

$[a_{00}, a_{01}, a_{02}, ..., a_{0k}, a_{10}, a_{11}, ... a_{k0}]\,.$
copy()[source]

Copy of this polynomial with same parameter list.

Returns: Copy Polynomial
full_model(points, *params)[source]

Evaluate model with given parameters.

This is an abstract method and has to be overwritten by subclasses. model uses this method with internal parameter list.

Parameters: points ((N,2)-shaped array) – List of x,y points where to evaluate polynomial. params ((m,)-shaped array) – Flattened list of polynomial parameters. 1D array of values (N,)-shaped array
get_params()[source]

Returns internal parameter list or None if not set.

Returns: Parameter list of length m. list or array or None
make_single_degree_subpoly(degree)[source]

Make new polynomial containing terms of one degree only.

This is an abstract method.

Parameters: degree (int) New instance of same class with some parameters zeroed out. ValueError – If no internal parameters where set.
model(points)[source]

Evaluate model with internal parameters.

Parameters: points ((N,2)-shaped array) – List of x,y points where to evaluate polynomial. 1D array of values. (N,)-shaped array ValueError – If no parameters where set. Use set_params.
set_params(params)[source]

Set flat parameters list, e.g. after optimization.

Parameters: params (list or array of length m)
vandermonde(points)[source]

Calculate Vandermonde matrix for given points.

This is an abstract method.

Parameters: points ((N,2)-shaped array) – List of x,y points where to evaluate polynomial. Vandermonde matrix. N is the number of points, m the number of parameters. (N,m)-shaped array

Notes

The Vandermonde matrix for the points $$(x_j, y_j)$$ is

$\begin{split}V = \begin{bmatrix} P_0(x_1) P_0(y_1) & \cdots & P_k(x_1) P_0(y_1) \\ \vdots & {} & \vdots \\ P_0(x_N) P_0(y_N) & \cdots & P_k(x_N) P_0(y_N) \end{bmatrix}\end{split}$

and evaluation with a given parameters is then a simple multiplication

$\begin{split}\begin{bmatrix} P(x_1, y_1) \\ \vdots \\ P(x_N, y_N) \end{bmatrix} = V \cdot \begin{bmatrix} a_0 \\ \vdots \\ a_m \end{bmatrix}\,.\end{split}$
class mascado.distortions.polynomials.Zernike(degree)[source]

Zernike polynomials.

Concrete implementation of Polynomial.

Implementation details from Hedser van Brug (1997): Efficient Cartesian representation of Zernike polynomials in computer memory. Proc. SPIE 3190

Parameters: degree (int) zernikes (list of arrays) – Precalculated coefficient matrices for single Zernike polynomials. Arranged in the same order as flattened parameter list.

Notes

Zernike polynomials are defined on the unit circle.

full_model(points, *params)[source]
vandermonde(points)[source]
zernikepoly(n, m)[source]

Build Cartesian coefficient matrix for $$Z_n^m$$.

$$m,n>=0$$ and $$m<=n$$.

Returns: Function that takes x and y coordinates and calculates result for given $$Z_n^m$$. (Wrapped numpy.polynomials.polynomial.polyval2d()) function(x, y) -> 1D float array