Skip to content

cds2.interpolate

Interpolation on scipy.interpolate.

Interpolation — convenience re-export of scipy.interpolate / numpy.interp.

linear_interp(x_new, x_known, y_known)

Piecewise-linear interpolation at x_new through known points.

Source code in src/cds2/interpolate.py
21
22
23
24
25
def linear_interp(x_new: object, x_known: Sequence[float], y_known: Sequence[float]) -> np.ndarray:
    """Piecewise-linear interpolation at ``x_new`` through known points."""
    return np.asarray(
        np.interp(np.asarray(x_new, dtype=float), np.asarray(x_known), np.asarray(y_known))
    )

cubic_spline(x, y)

Natural cubic spline interpolant callable on any point in range.

Source code in src/cds2/interpolate.py
28
29
30
def cubic_spline(x: Sequence[float], y: Sequence[float]) -> spi.CubicSpline:
    """Natural cubic spline interpolant callable on any point in range."""
    return spi.CubicSpline(np.asarray(x, dtype=float), np.asarray(y, dtype=float))

pchip_interpolator(x, y)

Monotone shape-preserving PCHIP interpolant.

Source code in src/cds2/interpolate.py
33
34
35
def pchip_interpolator(x: Sequence[float], y: Sequence[float]) -> spi.PchipInterpolator:
    """Monotone shape-preserving PCHIP interpolant."""
    return spi.PchipInterpolator(np.asarray(x, dtype=float), np.asarray(y, dtype=float))

lagrange_poly(x, y)

Lagrange polynomial passing exactly through all given points.

Uses :class:scipy.interpolate.BarycentricInterpolator — the recommended replacement for the deprecated :func:scipy.interpolate.lagrange (removed in SciPy 1.20). The returned interpolator is callable and evaluates the unique interpolating polynomial at any point.

Source code in src/cds2/interpolate.py
38
39
40
41
42
43
44
45
46
def lagrange_poly(x: Sequence[float], y: Sequence[float]) -> spi.BarycentricInterpolator:
    """Lagrange polynomial passing exactly through all given points.

    Uses :class:`scipy.interpolate.BarycentricInterpolator` — the
    recommended replacement for the deprecated :func:`scipy.interpolate.lagrange`
    (removed in SciPy 1.20). The returned interpolator is callable and
    evaluates the unique interpolating polynomial at any point.
    """
    return spi.BarycentricInterpolator(np.asarray(x, dtype=float), np.asarray(y, dtype=float))

grid_interp(points, values, query_points, method='linear')

Interpolate values defined at scattered N-D points onto new locations.

Source code in src/cds2/interpolate.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def grid_interp(
    points: Sequence[Sequence[float]],
    values: Sequence[float],
    query_points: Sequence[Sequence[float]],
    method: str = "linear",
) -> np.ndarray:
    """Interpolate values defined at scattered N-D points onto new locations."""
    result = spi.griddata(
        np.asarray(points, dtype=float),
        np.asarray(values, dtype=float),
        np.asarray(query_points, dtype=float),
        method=method,
    )
    return np.asarray(result)

regular_grid_interp(grid_axes, values, query_points)

Interpolate on a regular (rectilinear) grid built from axis vectors.

Source code in src/cds2/interpolate.py
65
66
67
68
69
70
71
72
73
74
75
76
def regular_grid_interp(
    grid_axes: Sequence[Sequence[float]],
    values: object,
    query_points: object,
) -> np.ndarray:
    """Interpolate on a regular (rectilinear) grid built from axis vectors."""
    interpolator = spi.RegularGridInterpolator(
        [np.asarray(axis, dtype=float) for axis in grid_axes],
        np.asarray(values, dtype=float),
    )
    query = np.atleast_2d(np.asarray(query_points, dtype=float))
    return np.asarray(interpolator(query), dtype=float)

rbf_interp(points, values, query_points, kernel='thin_plate_spline', smoothing=0.0, neighbors=None)

Radial-basis-function interpolation for scattered N-D data.

neighbors (kNN subset per query) keeps large problems tractable at a small accuracy cost; smoothing > 0 fits an approximating surface instead of passing through the data.

Source code in src/cds2/interpolate.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def rbf_interp(
    points: Sequence[Sequence[float]],
    values: Sequence[float],
    query_points: Sequence[Sequence[float]],
    kernel: str = "thin_plate_spline",
    smoothing: float = 0.0,
    neighbors: int | None = None,
) -> np.ndarray:
    """Radial-basis-function interpolation for scattered N-D data.

    ``neighbors`` (kNN subset per query) keeps large problems tractable at a
    small accuracy cost; ``smoothing > 0`` fits an approximating surface
    instead of passing through the data.
    """
    interpolator = spi.RBFInterpolator(
        np.asarray(points, dtype=float),
        np.asarray(values, dtype=float),
        kernel=kernel,
        smoothing=smoothing,
        neighbors=neighbors,
    )
    return np.asarray(interpolator(np.asarray(query_points, dtype=float)))