Skip to content

Sorting

sorting

sort_spatially

sort_spatially(
    trajectory: Trajectory,
    sorting_settings: SortingSettings = SortingSettings(),
    inplace: bool = True,
) -> Trajectory

Sorts the trajectory spatially. This is only useful for trajectories that describe a closed loop without intersections.

Parameters:

  • trajectory (Trajectory) –

    Trajectory to sort.

  • sorting_settings (SortingSettings, default: SortingSettings() ) –

    Sorting settings.

  • inplace (bool, default: True ) –

    Whether to sort the trajectory in-place. Defaults to True.

Returns:

  • Trajectory ( Trajectory ) –

    Sorted trajectory.

Source code in trajectopy\processing\sorting.py
def sort_spatially(
    trajectory: Trajectory, sorting_settings: SortingSettings = SortingSettings(), inplace: bool = True
) -> Trajectory:
    """
    Sorts the trajectory spatially. This is only useful for trajectories
    that describe a closed loop without intersections.

    Args:
        trajectory (Trajectory): Trajectory to sort.
        sorting_settings (SortingSettings): Sorting settings.
        inplace (bool, optional): Whether to sort the trajectory in-place. Defaults to True.

    Returns:
        Trajectory: Sorted trajectory.

    """
    sort_idx, arc_lengths = _sort_xyz(xyz=trajectory.positions.xyz, settings=sorting_settings)
    arg_sort_sort_idx = np.argsort(sort_idx)
    trajectory = trajectory.mask(sorted(sort_idx), inplace=inplace)
    trajectory.path_lengths = arc_lengths[arg_sort_sort_idx]
    trajectory.sorting = Sorting.PATH_LENGTH
    return trajectory

divide_into_laps

divide_into_laps(
    trajectory: Trajectory,
    sorting_settings: SortingSettings = SortingSettings(),
    return_lap_indices: bool = False,
) -> Union[
    List[Trajectory], Tuple[List[Trajectory], ndarray]
]

Divides the trajectory into laps. This is only useful for trajectories that describe a closed loop without intersections.

Parameters:

  • trajectory (Trajectory) –

    Trajectory to divide.

  • sorting_settings (SortingSettings, default: SortingSettings() ) –

    Sorting settings.

  • return_lap_indices (bool, default: False ) –

    Whether to return lap indices. Defaults to False.

Returns:

  • Union[List[Trajectory], Tuple[List[Trajectory], ndarray]]

    List[Trajectory]: List of trajectories, each representing a lap.

Source code in trajectopy\processing\sorting.py
def divide_into_laps(
    trajectory: Trajectory, sorting_settings: SortingSettings = SortingSettings(), return_lap_indices: bool = False
) -> Union[List[Trajectory], Tuple[List[Trajectory], np.ndarray]]:
    """
    Divides the trajectory into laps. This is only useful for trajectories
    that describe a closed loop without intersections.

    Args:
        trajectory (Trajectory): Trajectory to divide.
        sorting_settings (SortingSettings): Sorting settings.
        return_lap_indices (bool, optional): Whether to return lap indices. Defaults to False.

    Returns:
        List[Trajectory]: List of trajectories, each representing a lap.

    """
    if trajectory.sorting != Sorting.PATH_LENGTH:
        trajectory = sort_spatially(trajectory=trajectory, sorting_settings=sorting_settings, inplace=False)
    else:
        trajectory = trajectory.copy()

    arc_length_diffs = np.diff(trajectory.path_lengths)
    arc_length_threshold = 0.95 * np.max(trajectory.path_lengths)
    lap_indices = np.r_[0, np.where(np.abs(arc_length_diffs) > arc_length_threshold)[0], len(trajectory)]

    # divide into laps
    laps = []
    for i in range(len(lap_indices) - 1):
        lap = trajectory.mask(np.arange(lap_indices[i], lap_indices[i + 1]), inplace=False)
        laps.append(lap)

    return laps if (not return_lap_indices) else (laps, lap_indices)