pyzernike.xy_zernike_parameter_derivative#

pyzernike.xy_zernike_parameter_derivative(x: ndarray, y: ndarray, n: Integral | Sequence[Integral] | ndarray, m: Integral | Sequence[Integral] | ndarray, parameter: str | Sequence[str] | ndarray, Rx: Real | None = 1.0, Ry: Real | None = 1.0, x0: Real | None = 0.0, y0: Real | None = 0.0, alpha: Real | None = 0.0, h: Real | None = 0.0, default: int | float = nan, *, precompute: bool = True, normalize: bool = False, to_list: bool = True) → ndarray | List[ndarray][source]#

Compute the first order derivatives of the Zernike polynomials on an elliptic annulus [1] with respect to the parameters \((x_0, y_0, R_x, R_y, \alpha, h)\) of the annulus, at fixed Cartesian coordinates \((x, y)\).

../_images/elliptic_annulus_domain.png

The parameters of the elliptic annulus domain.#

The Zernike polynomials on the elliptic annulus are \(Zxy_{n}^{m}(x, y) = Z_{n}^{m}\left(\rho_{eq}, \theta_{eq}\right)\) (see pyzernike.xy_zernike_polynomial()). These derivatives are used to refine the domain of definition, e.g. to fit the parameters of the annulus with a Gauss-Newton algorithm: for a surface \(W = \sum_j c_j Zxy_{j}\), \(\frac{\partial W}{\partial p} = \sum_j c_j \frac{\partial Zxy_{j}}{\partial p}\). The residual and Jacobian functions of such a fit are directly given by pyzernike.xy_zernike_least_squares().

Let \(u = X / R_x\) and \(v = Y / R_y\), where \(X = c (x - x_0) + s (y - y_0)\) and \(Y = -s (x - x_0) + c (y - y_0)\) are the coordinates in the frame of the ellipse (\(c = \cos\alpha\), \(s = \sin\alpha\)). The polynomial depends on \((x_0, y_0, R_x, R_y, \alpha)\) only through \((u, v)\), so with \(Z_u = \partial_u Zxy_{n}^{m}\) and \(Z_v = \partial_v Zxy_{n}^{m}\):

\[\frac{\partial Zxy_{n}^{m}}{\partial x_0} = -\frac{c}{R_x} Z_u + \frac{s}{R_y} Z_v \quad \text{and} \quad \frac{\partial Zxy_{n}^{m}}{\partial y_0} = -\frac{s}{R_x} Z_u - \frac{c}{R_y} Z_v\]
\[\frac{\partial Zxy_{n}^{m}}{\partial R_x} = -\frac{u}{R_x} Z_u \quad \text{and} \quad \frac{\partial Zxy_{n}^{m}}{\partial R_y} = -\frac{v}{R_y} Z_v \quad \text{and} \quad \frac{\partial Zxy_{n}^{m}}{\partial \alpha} = \frac{R_y}{R_x} v Z_u - \frac{R_x}{R_y} u Z_v\]

The parameter \(h\) appears only in \(\rho_{eq} = \frac{r - h}{1 - h}\) (\(r = \sqrt{u^2 + v^2}\)), so:

\[\frac{\partial Zxy_{n}^{m}}{\partial h} = \frac{\rho_{eq} - 1}{1 - h} \frac{\partial Z_{n}^{m}}{\partial \rho}(\rho_{eq}, \theta_{eq})\]

\(Z_u\) and \(Z_v\) are computed as in pyzernike.xy_zernike_polynomial(), without singularity at the center of the ellipse.

  • If \(|m| > n\) or \(n - m\) is odd, the derivative is zero.

  • Where the point is outside the elliptic annulus (\(\rho_{eq}\) outside \([0, 1]\)), or the coordinates are not finite, the output is set to default.

Note

The derivatives are those of the value of the polynomial at a fixed point inside the annulus. The domain itself depends on the parameters: when they change, points may enter or leave the annulus, which is not differentiable. During an optimization, keep the set of points fixed within an iteration (or discard the points close to the boundary), and keep \(h\) in \([0, 1)\) (the derivative with respect to \(h\) at \(h = 0\) is the right derivative). The parameters can not be estimated together with the coefficients for a full ellipse (see the page Fitting).

Shape of the inputs and of the output. n, m and parameter can each be a single value or a sequence (list, tuple or 1D numpy array):

  • If all of them are single values, a single derivative is computed and returned as an array with the shape of x.

  • If at least one of them is a sequence, the single values are repeated to the length \(N\) of the sequences (which must all have the same length), and \(N\) derivatives are computed. They are returned as a list of \(N\) arrays with the shape of x (to_list=True), or as a single array of shape \((N, \dots)\) (to_list=False).

See also

Parameters:
  • x (numpy.ndarray) – The x Cartesian coordinates with any shape (...).

  • y (numpy.ndarray) – The y Cartesian coordinates with the same shape as x. If x and y are floating point arrays of the same dtype (e.g. numpy.float32), this dtype is kept. Otherwise, both are converted to numpy.float64.

  • n (Union[Integral, Sequence[Integral], numpy.ndarray]) – The radial order(s) \(n\).

  • m (Union[Integral, Sequence[Integral], numpy.ndarray]) – The azimuthal frequency(ies) \(m\).

  • parameter (Union[str, Sequence[str], numpy.ndarray]) – The parameter(s) with respect to which the polynomials are derived, among "x0", "y0", "Rx", "Ry", "alpha" and "h".

  • Rx (Optional[Real], optional) – The semi-axis \(R_x\) of the ellipse along its x axis. Must be finite and strictly positive. Default is 1.0.

  • Ry (Optional[Real], optional) – The semi-axis \(R_y\) of the ellipse along its y axis. Must be finite and strictly positive. Default is 1.0.

  • x0 (Optional[Real], optional) – The x coordinate \(x_0\) of the center of the ellipse. Must be finite. Default is 0.0.

  • y0 (Optional[Real], optional) – The y coordinate \(y_0\) of the center of the ellipse. Must be finite. Default is 0.0.

  • alpha (Optional[Real], optional) – The rotation angle \(\alpha\) of the ellipse in radians. Must be finite. Default is 0.0.

  • h (Optional[Real], optional) – The ratio \(h\) of the inner to the outer semi-axes, in \([0, 1)\). Default is 0.0 (full ellipse).

  • default (Union[int, float], optional) – The value set outside the elliptic annulus or where the coordinates are not finite. Integers and floating point numbers (Python or numpy, including numpy.nan and numpy.inf) are accepted and converted to float. Booleans are not accepted. Default is numpy.nan.

  • precompute (bool, optional) – If True, the powers of \(\rho_{eq}\) and the cosine and sine terms are cached and shared between the polynomials, which is faster when several derivatives are computed at once. If False, they are recomputed for each polynomial, which uses less memory. Default is True.

  • normalize (bool, optional) – If True, the derivatives are multiplied by the normalization factors of Noll \(N_{n}^{m} = \sqrt{(2 - \delta_{m0})(n + 1)}\) (see pyzernike.zernike_normalization()). The value default is not normalized. Default is False.

  • to_list (bool, optional) – Only used when several derivatives are computed. If True, the output is a list of arrays. If False, the output is a single array of shape \((N, \dots)\). Default is True.

  • ellipse (None is accepted for all the parameters of the)

  • value. (and means the default)

Returns:

  • If n, m and parameter are single values: an array with the shape of x.

  • Otherwise, if to_list is True: a list of \(N\) arrays with the shape of x.

  • Otherwise: an array of shape \((N, \dots)\), where output[i] is the derivative of the polynomial (n[i], m[i]) with respect to parameter[i].

The dtype is the common floating point dtype of x and y (numpy.float64 by default).

Return type:

Union[numpy.ndarray, List[numpy.ndarray]]

Raises:
  • TypeError – If x or y can not be converted to a floating point array, if a parameter of the ellipse is not a real number, if default is not an integer or a floating point number, if n or m is not an integer or a sequence of integers, if parameter is not a string or a sequence of strings, or if precompute, normalize or to_list is not a boolean.

  • ValueError – If x and y do not have the same shape, if a parameter of the ellipse is not finite, if Rx or Ry is not strictly positive, if h is not in \([0, 1)\), if parameter contains an unknown name, or if the sequences do not have the same length.

References

Examples

Compute the derivative of the defocus \(Z_{2}^{0}\) with respect to the center \(x_0\) of a circle of radius 10 centered at \((1, 1)\):

import numpy
from pyzernike import xy_zernike_parameter_derivative

X, Y = numpy.meshgrid(numpy.linspace(-10, 10, 100), numpy.linspace(-10, 10, 100))
dZ_dx0 = xy_zernike_parameter_derivative(X, Y, n=2, m=0, parameter="x0", Rx=10, Ry=10, x0=1.0, y0=1.0)

Compute the gradient of \(Z_{2}^{0}\) with respect to all the parameters (n and m are repeated):

parameters = ["x0", "y0", "Rx", "Ry", "alpha", "h"]
gradient = xy_zernike_parameter_derivative(X, Y, n=2, m=0, parameter=parameters, Rx=10, Ry=10, x0=1.0, y0=1.0, to_list=False)
# array of shape (6, 100, 100)