Skip to content

Factory

The factory module provides DonutFactory and a set of coordinate-transform utilities for mapping between pupil and focal planes.

DonutFactory

danish.factory.DonutFactory module-attribute

DonutFactory = DonutInverseFactory

Coordinate transforms

danish.factory.pupil_to_focal

pupil_to_focal(u, v, *, Z=None, aberrations=None, R_outer=1.0, R_inner=0.0, focal_length=None, x_offset=None, y_offset=None)

Transform pupil coordinates to focal coordinates.

Parameters:

Name Type Description Default
u array of float

Pupil coordinates in meters.

required
v array of float

Pupil coordinates in meters.

required
Z Zernike

Aberrations in meters.

None
aberrations array of float

Aberrations in meters.

None
R_outer float

Annulus outer and inner radii in meters.

1.0
R_inner float

Annulus outer and inner radii in meters.

1.0
focal_length float

Focal length in meters.

None
x_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series.

None
y_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series.

None

Returns:

Type Description
x, y : array of float

Focal coordinates in meters.

Source code in danish/factory.py
def pupil_to_focal(
    u, v, *,
    Z=None, aberrations=None, R_outer=1.0, R_inner=0.0,
    focal_length=None,
    x_offset=None, y_offset=None
):
    """Transform pupil coordinates to focal coordinates.

    Parameters
    ----------
    u, v : array of float
        Pupil coordinates in meters.
    Z : galsim.zernike.Zernike, optional
        Aberrations in meters.
    aberrations : array of float, optional
        Aberrations in meters.
    R_outer, R_inner : float, optional
        Annulus outer and inner radii in meters.
    focal_length : float
        Focal length in meters.
    x_offset, y_offset : galsim.zernike.Zernike, optional
        Additional focal plane offsets (in meters) represented as Zernike
        series.

    Returns
    -------
    x, y : array of float
        Focal coordinates in meters.
    """
    if Z is None:
        Z = galsim.zernike.Zernike(
            aberrations, R_outer=R_outer, R_inner=R_inner
        )
    if focal_length is None:
        raise ValueError("Missing focal length")

    return _pupil_to_focal(
        u, v, Z,
        focal_length=focal_length,
        x_offset=x_offset, y_offset=y_offset
    )

danish.factory.focal_to_pupil

focal_to_pupil(x, y, *, Z=None, aberrations=None, R_outer=1.0, R_inner=0.0, focal_length=None, x_offset=None, y_offset=None, prefit_order=F2P_PREFIT_ORDER, maxiter=F2P_MAXITER, tol=F2P_TOL, strict=F2P_STRICT)

Transform focal coordinates to pupil coordinates.

Parameters:

Name Type Description Default
x array of float

Focal coordinates in meters.

required
y array of float

Focal coordinates in meters.

required
Z Zernike

Aberrations in meters.

None
aberrations array of float

Aberrations in meters.

None
R_outer float

Annulus outer and inner radii in meters.

1.0
R_inner float

Annulus outer and inner radii in meters.

1.0
focal_length float

Focal length in meters.

None
x_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series.

None
y_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series.

None
prefit_order int

Order of prefit used to get good initial guesses for coordinate transformation.

F2P_PREFIT_ORDER
maxiter int

Number of Newton iterations to attempt before failing.

F2P_MAXITER
tol float

Tolerance for successful coordinate transformation.

F2P_TOL
strict

If True, then raise a RuntimeError if any coordinates could not be mapped. If False, then return NaN for unmappable coordinates.

F2P_STRICT

Returns:

Type Description
u, v : array of float

Pupil coordinates in meters.

Source code in danish/factory.py
def focal_to_pupil(
    x, y, *,
    Z=None, aberrations=None, R_outer=1.0, R_inner=0.0,
    focal_length=None,
    x_offset=None, y_offset=None,
    prefit_order=F2P_PREFIT_ORDER, maxiter=F2P_MAXITER, tol=F2P_TOL, strict=F2P_STRICT
):
    """Transform focal coordinates to pupil coordinates.

    Parameters
    ----------
    x, y : array of float
        Focal coordinates in meters.
    Z : galsim.zernike.Zernike, optional
        Aberrations in meters.
    aberrations : array of float, optional
        Aberrations in meters.
    R_outer, R_inner : float, optional
        Annulus outer and inner radii in meters.
    focal_length : float
        Focal length in meters.
    x_offset, y_offset : galsim.zernike.Zernike, optional
        Additional focal plane offsets (in meters) represented as Zernike
        series.
    prefit_order : int
        Order of prefit used to get good initial guesses for coordinate
        transformation.
    maxiter : int
        Number of Newton iterations to attempt before failing.
    tol : float
        Tolerance for successful coordinate transformation.
    strict: bool
        If True, then raise a RuntimeError if any coordinates could not be
        mapped.
        If False, then return NaN for unmappable coordinates.

    Returns
    -------
    u, v : array of float
        Pupil coordinates in meters.
    """
    if Z is None:
        Z = galsim.zernike.Zernike(
            aberrations, R_outer=R_outer, R_inner=R_inner
        )
    if focal_length is None:
        raise ValueError("Missing focal length")

    return _focal_to_pupil(
        x, y, Z,
        focal_length=focal_length,
        x_offset=x_offset, y_offset=y_offset,
        prefit_order=prefit_order,
        maxiter=maxiter,
        tol=tol, strict=strict
    )

danish.factory.pupil_focal_jacobian

pupil_focal_jacobian(u, v, *, Z=None, aberrations=None, R_outer=1.0, R_inner=0.0, focal_length=None, x_offset=None, y_offset=None)

Compute Jacobian of the pupil-to-focal coordinate transform.

Parameters:

Name Type Description Default
u array of float

Pupil coordinates in meters.

required
v array of float

Pupil coordinates in meters.

required
Z Zernike

Aberrations in meters.

None
aberrations array of float

Aberrations in meters.

None
R_outer float

Annulus outer and inner radii in meters.

1.0
R_inner float

Annulus outer and inner radii in meters.

1.0
focal_length float

Focal length in meters.

None
x_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series.

None
y_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series.

None

Returns:

Type Description
dxdu, dxdv, dydu, dydv : array of float

Jacobian of focal coordinates with respect to pupil coordinates.

Source code in danish/factory.py
def pupil_focal_jacobian(
    u, v, *,
    Z=None, aberrations=None, R_outer=1.0, R_inner=0.0,
    focal_length=None,
    x_offset=None, y_offset=None
):
    """Compute Jacobian of the pupil-to-focal coordinate transform.

    Parameters
    ----------
    u, v : array of float
        Pupil coordinates in meters.
    Z : galsim.zernike.Zernike, optional
        Aberrations in meters.
    aberrations : array of float, optional
        Aberrations in meters.
    R_outer, R_inner : float, optional
        Annulus outer and inner radii in meters.
    focal_length : float
        Focal length in meters.
    x_offset, y_offset : galsim.zernike.Zernike, optional
        Additional focal plane offsets (in meters) represented as Zernike
        series.

    Returns
    -------
    dxdu, dxdv, dydu, dydv : array of float
        Jacobian of focal coordinates with respect to pupil coordinates.
    """
    if Z is None:
        Z = galsim.zernike.Zernike(
            aberrations, R_outer=R_outer, R_inner=R_inner
        )
    if focal_length is None:
        raise ValueError("Missing focal length")

    return _pupil_focal_jacobian(
        u, v, Z,
        focal_length=focal_length,
        x_offset=x_offset, y_offset=y_offset
    )

danish.factory.pupil_focal_coef_jacobian

pupil_focal_coef_jacobian(u, v, *, nterm, R_outer=1.0, R_inner=0.0, focal_length, pixel_scale=1.0)

Derivative of projected focal position with respect to each Zernike.

pupil_to_focal evaluates the gradient of a Zernike series, and that series is linear in its coefficients, so the projected position of a fixed pupil point is linear in the aberrations. Its derivative is therefore a constant matrix: independent of the aberrations, fixed by the pupil points alone. Evaluating it once lets a fitter replace a Zernike evaluation per model call with a single matrix product, and gives the exact d(position)/d(coefficient) that an analytic Jacobian needs.

Parameters:

Name Type Description Default
u array of float

Pupil coordinates in meters.

required
v array of float

Pupil coordinates in meters.

required
nterm int

Number of leading Zernike coefficients to differentiate with respect to. Only these get a column; the derivative with respect to the rest of the series is not returned because a fitter holds them fixed. See pupil_to_focal_basis, which pairs these columns with the position those fixed coefficients project to.

required
R_outer float

Annulus outer and inner radii in meters.

1.0
R_inner float

Annulus outer and inner radii in meters.

1.0
focal_length float

Focal length in meters.

required
pixel_scale float

Pixel scale in meters. Defaults to 1.0, i.e. focal coordinates in meters rather than pixels.

1.0

Returns:

Type Description
dxdz, dydz : array of float, shape (len(u), nterm)

Derivative of focal x and y with respect to each Zernike coefficient. Column 0 is zero: Noll indexing starts at 1, so aberrations[0] is unused.

Source code in danish/factory.py
def pupil_focal_coef_jacobian(
    u, v, *,
    nterm,
    R_outer=1.0, R_inner=0.0,
    focal_length,
    pixel_scale=1.0,
):
    """Derivative of projected focal position with respect to each Zernike.

    `pupil_to_focal` evaluates the gradient of a Zernike series, and that
    series is linear in its coefficients, so the projected position of a fixed
    pupil point is linear in the aberrations.  Its derivative is therefore a
    constant matrix: independent of the aberrations, fixed by the pupil points
    alone.  Evaluating it once lets a fitter replace a Zernike evaluation per
    model call with a single matrix product, and gives the exact
    d(position)/d(coefficient) that an analytic Jacobian needs.

    Parameters
    ----------
    u, v : array of float
        Pupil coordinates in meters.
    nterm : int
        Number of leading Zernike coefficients to differentiate with respect
        to.  Only these get a column; the derivative with respect to the rest
        of the series is not returned because a fitter holds them fixed.  See
        `pupil_to_focal_basis`, which pairs these columns with the position
        those fixed coefficients project to.
    R_outer, R_inner : float, optional
        Annulus outer and inner radii in meters.
    focal_length : float
        Focal length in meters.
    pixel_scale : float, optional
        Pixel scale in meters.  Defaults to 1.0, i.e. focal coordinates in
        meters rather than pixels.

    Returns
    -------
    dxdz, dydz : array of float, shape (len(u), nterm)
        Derivative of focal x and y with respect to each Zernike coefficient.
        Column 0 is zero: Noll indexing starts at 1, so `aberrations[0]` is
        unused.
    """
    # _pupil_to_focal maps (u, v) through the gradient of the scaled series,
    # x = -focal_length * dW/du, so the derivative with respect to coefficient
    # j is just that of the jth basis polynomial.
    gradx, grady = galsim.zernike.zernikeGradBases(
        nterm - 1, u, v, R_outer=R_outer, R_inner=R_inner
    )
    scale = -focal_length/pixel_scale
    return gradx.T*scale, grady.T*scale

danish.factory.pupil_to_focal_basis

pupil_to_focal_basis(u, v, *, aberrations, nterm, R_outer=1.0, R_inner=0.0, focal_length, pixel_scale=1.0, x_offset=None, y_offset=None)

Split pupil_to_focal into a constant part and a linear map.

Returns the pieces needed to evaluate

x, y = x0 + dxdz @ ab[:nterm],  y0 + dydz @ ab[:nterm]

for any aberration vector sharing this one's tail. A fitter varies only the leading nterm coefficients, so x0/y0 -- which carry the fixed remainder of the reference wavefront plus any focal-plane offsets -- stay valid for the whole fit and the per-call work collapses to one matrix product.

Parameters:

Name Type Description Default
u array of float

Pupil coordinates in meters.

required
v array of float

Pupil coordinates in meters.

required
aberrations array of float

Reference aberrations in meters. May be longer than nterm: entries from nterm on are the fixed part and contribute to x0/y0, while the leading ones are ignored here and supplied per call instead. The split is exact, not a truncation -- nothing is dropped.

required
nterm int

Number of leading coefficients the caller intends to vary.

required
R_outer float

Annulus outer and inner radii in meters.

1.0
R_inner float

Annulus outer and inner radii in meters.

1.0
focal_length float

Focal length in meters.

required
pixel_scale float

Pixel scale in meters. Defaults to 1.0.

1.0
x_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series. Constant in the aberrations, so they land in x0/y0.

None
y_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series. Constant in the aberrations, so they land in x0/y0.

None

Returns:

Type Description
x0, y0 : array of float

Focal coordinates from the fixed part of the wavefront.

dxdz, dydz : array of float, shape (len(u), nterm)

Derivative with respect to each varied coefficient, from pupil_focal_coef_jacobian.

Source code in danish/factory.py
def pupil_to_focal_basis(
    u, v, *,
    aberrations, nterm,
    R_outer=1.0, R_inner=0.0,
    focal_length,
    pixel_scale=1.0,
    x_offset=None, y_offset=None,
):
    """Split `pupil_to_focal` into a constant part and a linear map.

    Returns the pieces needed to evaluate

        x, y = x0 + dxdz @ ab[:nterm],  y0 + dydz @ ab[:nterm]

    for any aberration vector sharing this one's tail.  A fitter varies only
    the leading `nterm` coefficients, so `x0`/`y0` -- which carry the fixed
    remainder of the reference wavefront plus any focal-plane offsets -- stay
    valid for the whole fit and the per-call work collapses to one matrix
    product.

    Parameters
    ----------
    u, v : array of float
        Pupil coordinates in meters.
    aberrations : array of float
        Reference aberrations in meters.  May be longer than `nterm`: entries
        from `nterm` on are the fixed part and contribute to `x0`/`y0`, while
        the leading ones are ignored here and supplied per call instead.  The
        split is exact, not a truncation -- nothing is dropped.
    nterm : int
        Number of leading coefficients the caller intends to vary.
    R_outer, R_inner : float, optional
        Annulus outer and inner radii in meters.
    focal_length : float
        Focal length in meters.
    pixel_scale : float, optional
        Pixel scale in meters.  Defaults to 1.0.
    x_offset, y_offset : galsim.zernike.Zernike, optional
        Additional focal plane offsets (in meters) represented as Zernike
        series.  Constant in the aberrations, so they land in `x0`/`y0`.

    Returns
    -------
    x0, y0 : array of float
        Focal coordinates from the fixed part of the wavefront.
    dxdz, dydz : array of float, shape (len(u), nterm)
        Derivative with respect to each varied coefficient, from
        `pupil_focal_coef_jacobian`.
    """
    tail = np.array(aberrations, dtype=float)
    tail[:nterm] = 0.0
    x0, y0 = _pupil_to_focal(
        u, v,
        galsim.zernike.Zernike(tail, R_outer=R_outer, R_inner=R_inner),
        focal_length=focal_length,
        x_offset=x_offset, y_offset=y_offset,
    )
    dxdz, dydz = pupil_focal_coef_jacobian(
        u, v,
        nterm=nterm,
        R_outer=R_outer, R_inner=R_inner,
        focal_length=focal_length,
        pixel_scale=pixel_scale,
    )
    return x0/pixel_scale, y0/pixel_scale, dxdz, dydz

Pupil sampling

danish.factory.enclosed_fraction

enclosed_fraction(x, y, u, v, u0, v0, radius, *, Z=None, aberrations=None, R_outer=1.0, R_inner=0.0, focal_length=None, x_offset=None, y_offset=None, pixel_scale=None)

Compute fraction of pixels enclosed by circles defined on the pupil.

Parameters:

Name Type Description Default
x array of float

Pixel coordinates.

required
y array of float

Pixel coordinates.

required
u array of float

Pupil coordinates in meters.

required
v array of float

Pupil coordinates in meters.

required
u0 float

Pupil coordinates of circle center in meters.

required
v0 float

Pupil coordinates of circle center in meters.

required
radius float

Circle radius in meters.

required
Z Zernike

Aberrations in meters.

None
aberrations array of float

Aberrations in meters.

None
R_outer float

Annulus outer and inner radii in meters.

1.0
R_inner float

Annulus outer and inner radii in meters.

1.0
focal_length float

Focal length in meters.

None
x_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series.

None
y_offset Zernike

Additional focal plane offsets (in meters) represented as Zernike series.

None
pixel_scale float

Pixel scale in meters.

None

Returns:

Name Type Description
enclosed array of float, congruent to x or y

Each pixel's enclosed fraction between 0, 1.

Source code in danish/factory.py
def enclosed_fraction(
    x, y,
    u, v,
    u0, v0, radius, *,
    Z=None, aberrations=None, R_outer=1.0, R_inner=0.0,
    focal_length=None,
    x_offset=None, y_offset=None,
    pixel_scale=None,
):
    """Compute fraction of pixels enclosed by circles defined on the pupil.

    Parameters
    ----------
    x, y : array of float
        Pixel coordinates.
    u, v : array of float
        Pupil coordinates in meters.
    u0, v0 : float
        Pupil coordinates of circle center in meters.
    radius : float
        Circle radius in meters.
    Z : galsim.zernike.Zernike, optional
        Aberrations in meters.
    aberrations : array of float, optional
        Aberrations in meters.
    R_outer, R_inner : float, optional
        Annulus outer and inner radii in meters.
    focal_length : float
        Focal length in meters.
    x_offset, y_offset : galsim.zernike.Zernike, optional
        Additional focal plane offsets (in meters) represented as Zernike
        series.
    pixel_scale : float
        Pixel scale in meters.

    Returns
    -------
    enclosed : array of float, congruent to x or y
        Each pixel's enclosed fraction between 0, 1.
    """

    if Z is None:
        Z = galsim.zernike.Zernike(
            aberrations, R_outer=R_outer, R_inner=R_inner
        )
    if focal_length is None:
        raise ValueError("Missing focal length")
    if pixel_scale is None:
        raise ValueError("Missing pixel scale")

    Z1 = Z * focal_length if focal_length else Z

    dudx, dudy, dvdx, dvdy = _pixel_pupil_jacobian(
        u, v, Z1,
        pixel_scale=pixel_scale,
        x_offset=x_offset, y_offset=y_offset
    )

    return _enclosed_fraction(
        x, y, u, v, u0, v0, radius,
        dudx=dudx, dudy=dudy, dvdx=dvdx, dvdy=dvdy
    )