Skip to content

Interpolation

interpolation

interpolate

interpolate(
    trajectory: Trajectory,
    index: list | ndarray,
    method: InterpolationMethod = LINEAR,
    inplace: bool = True,
    max_gap_size: float = float("inf"),
) -> Trajectory

Interpolates a trajectory to specified timestamps using the given method.

Parameters:

  • trajectory (Trajectory) –

    Trajectory to interpolate

  • index (list | ndarray) –

    Interpolation index

  • method (InterpolationMethod, default: LINEAR ) –

    Interpolation method. Defaults to InterpolationMethod.LINEAR.

  • inplace (bool, default: True ) –

    Perform in-place interpolation. Defaults to True.

  • max_gap_size (float, default: float('inf') ) –

    Maximum gap in the index over which to interpolate. Defaults to infinity.

Returns:

  • Trajectory ( Trajectory ) –

    Interpolated trajectory

Source code in trajectopy\processing\interpolation.py
def interpolate(
    trajectory: Trajectory,
    index: list | np.ndarray,
    method: InterpolationMethod = InterpolationMethod.LINEAR,
    inplace: bool = True,
    max_gap_size: float = float("inf"),
) -> "Trajectory":
    """Interpolates a trajectory to specified timestamps using the given method.

    Args:
        trajectory (Trajectory): Trajectory to interpolate
        index (list | np.ndarray): Interpolation index
        method (InterpolationMethod, optional): Interpolation method. Defaults to InterpolationMethod.LINEAR.
        inplace (bool, optional): Perform in-place interpolation. Defaults to True.
        max_gap_size (float, optional): Maximum gap in the index over which to interpolate. Defaults to infinity.

    Returns:
        Trajectory: Interpolated trajectory
    """
    if method == InterpolationMethod.LINEAR:
        return _interpolate_linear(trajectory, index, inplace, max_gap_size)
    else:
        raise ValueError(f"Interpolation method '{method}' is not supported.")

inside_gaps

inside_gaps(
    index: ndarray,
    observed_index: ndarray,
    max_gap_size: float,
) -> ndarray

Returns which values of an index lie inside gaps of the observations that are larger than max_gap_size.

Such values are not interpolated. Values at the edges of a gap are, since the observations on both sides of an edge are available.

Parameters:

  • index (ndarray) –

    Index values to interpolate at.

  • observed_index (ndarray) –

    Ascending index of the observations.

  • max_gap_size (float) –

    Maximum gap in the observed index over which to interpolate.

Returns:

  • ndarray –

    np.ndarray: Boolean mask, True for values of index that lie strictly inside a gap.

Source code in trajectopy\processing\interpolation.py
def inside_gaps(index: np.ndarray, observed_index: np.ndarray, max_gap_size: float) -> np.ndarray:
    """Returns which values of an index lie inside gaps of the observations that are larger than max_gap_size.

    Such values are not interpolated. Values at the edges of a gap are, since the observations on both
    sides of an edge are available.

    Args:
        index (np.ndarray): Index values to interpolate at.
        observed_index (np.ndarray): Ascending index of the observations.
        max_gap_size (float): Maximum gap in the observed index over which to interpolate.

    Returns:
        np.ndarray: Boolean mask, True for values of index that lie strictly inside a gap.
    """
    gaps = np.diff(observed_index) > max_gap_size
    in_gap = np.zeros(len(index), dtype=bool)
    for g_start, g_end in zip(observed_index[:-1][gaps], observed_index[1:][gaps]):
        in_gap |= (index > g_start) & (index < g_end)
    return in_gap