Skip to content

interpolate

InterpolationError

Bases: ValueError

Exception for interpolation-related errors.

Source code in src/rastr/gis/interpolate.py
11
12
class InterpolationError(ValueError):
    """Exception for interpolation-related errors."""

interpn_kernel(points, values, *, xi, kernel=None)

Interpolate scattered data to new points, with optional kernel transformation.

For example, you could provide a kernel to transform cartesian coordinate points to polar coordinates before interpolation, giving interpolation which follows the circular pattern of the data.

Parameters:

Name Type Description Default
points ndarray

Array of shape (n_points, n_dimensions) representing the input points.

required
values ndarray

Array of shape (n_points,) representing the values at each input point.

required
xi ndarray

Array of shape (m_points, n_dimensions) representing the points to interpolate to.

required
kernel Callable[[ndarray], ndarray] | None

Optional function to transform points (and xi) before interpolation.

None
Source code in src/rastr/gis/interpolate.py
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
47
48
49
50
51
52
53
54
def interpn_kernel(
    points: np.ndarray,
    values: np.ndarray,
    *,
    xi: np.ndarray,
    kernel: Callable[[np.ndarray], np.ndarray] | None = None,
) -> np.ndarray:
    """Interpolate scattered data to new points, with optional kernel transformation.

    For example, you could provide a kernel to transform cartesian coordinate points
    to polar coordinates before interpolation, giving interpolation which follows the
    circular pattern of the data.

    Args:
        points: Array of shape (n_points, n_dimensions) representing the input points.
        values: Array of shape (n_points,) representing the values at each input point.
        xi: Array of shape (m_points, n_dimensions) representing the points to
            interpolate to.
        kernel: Optional function to transform points (and xi) before interpolation.
    """
    from scipy.interpolate import LinearNDInterpolator
    from scipy.spatial import QhullError

    if kernel is not None:
        xi = kernel(xi)
        points = kernel(points)
    try:
        interpolator = LinearNDInterpolator(
            points=points, values=values, fill_value=np.nan
        )
    except QhullError as err:
        msg = (
            "Failed to interpolate. This may be due to insufficient or "
            "degenerate input points. Ensure that the (x, y) points are not all "
            "collinear (i.e. that the convex hull is non-degenerate)."
        )
        raise InterpolationError(msg) from err

    grid_values = np.array(interpolator(xi))
    return grid_values