Skip to content

Interpolation¤

interp

dLux.utils.interpolation.interp(image, knot_coords, sample_coords, method='linear', fill=0.0) ¤

General 2D interpolation wrapper around interpax.interp2d.

Parameters:

Name Type Description Default
image Array

The input 2D image to interpolate.

required
knot_coords Array

The coordinates of the sampled points in image.

required
sample_coords Array

The coordinates to interpolate onto.

required
method str = "linear"

The interpolation method.

'linear'
fill float = 0.0

Fill value used outside knot_coords.

0.0

Returns:

Name Type Description
array Array

The interpolated array.

Source code in dLux/utils/interpolation.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def interp(
    image: Array,
    knot_coords: Array,
    sample_coords: Array,
    method: str = "linear",
    fill: float = 0.0,
):
    """
    General 2D interpolation wrapper around `interpax.interp2d`.

    Parameters
    ----------
    image : Array
        The input 2D image to interpolate.
    knot_coords : Array
        The coordinates of the sampled points in `image`.
    sample_coords : Array
        The coordinates to interpolate onto.
    method : str = "linear"
        The interpolation method.
    fill : float = 0.0
        Fill value used outside `knot_coords`.

    Returns
    -------
    array: Array
        The interpolated array.
    """
    xs, ys = knot_coords
    xpts, ypts = sample_coords.reshape(2, -1)

    return ipx.interp2d(
        ypts, xpts, ys[:, 0], xs[0], image, method=method, extrap=fill
    ).reshape(sample_coords[0].shape)
scale

dLux.utils.interpolation.scale(array, npixels, ratio, method='linear') ¤

Paraxially interpolate a square array using a sampling ratio.

Parameters:

Name Type Description Default
array Array

The input field to interpolate, either in amplitude and phase, or real and imaginary.

required
npixels int

The number of pixels in the output array.

required
ratio float

The sampling scale of the input relative to the output.

required
method str = "linear"

The interpolation method.

'linear'

Returns:

Name Type Description
array Array

The interpolated array.

Source code in dLux/utils/interpolation.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def scale(array: Array, npixels: int, ratio: float, method: str = "linear") -> Array:
    """
    Paraxially interpolate a square array using a sampling ratio.

    Parameters
    ----------
    array : Array
        The input field to interpolate, either in amplitude and phase, or real
        and imaginary.
    npixels : int
        The number of pixels in the output array.
    ratio : float
        The sampling scale of the input relative to the output.
    method : str = "linear"
        The interpolation method.

    Returns
    -------
    array : Array
        The interpolated array.
    """
    # Get coords arrays
    npixels_in = array.shape[-1]
    coords_in = dlu.pixel_coords(npixels_in, 1)
    coords_out = dlu.compress_coords(
        dlu.pixel_coords(npixels, 1),
        np.array([ratio, ratio]) * npixels / npixels_in,
    )

    # Interpolate
    return interp(array, coords_in, coords_out, method)
rotate

dLux.utils.interpolation.rotate(array, angle, method='linear') ¤

Rotates a square array by the angle, using interpolation.

Parameters:

Name Type Description Default
array Array

The array to rotate.

required
angle (float | Array, radians)

The angle to rotate the array by.

required
method str = "linear"

The interpolation method.

'linear'

Returns:

Name Type Description
array Array

The rotated array.

Source code in dLux/utils/interpolation.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def rotate(array: Array, angle: Array, method: str = "linear") -> Array:
    """
    Rotates a square array by the angle, using interpolation.

    Parameters
    ----------
    array : Array
        The array to rotate.
    angle : float | Array, radians
        The angle to rotate the array by.
    method : str = "linear"
        The interpolation method.

    Returns
    -------
    array : Array
        The rotated array.
    """
    # Get coordinates
    npixels = array.shape[0]
    coords_in = dlu.nd_coords((npixels, npixels))
    coords_out = dlu.rotate_coords(coords_in, angle)

    # Interpolate
    return interp(array, coords_in, coords_out, method)