This repository contains a collection of Python scripts for processing and visualizing drone trajectories. The scripts perform various operations such as segmenting trajectories into input and output segments, resampling trajectories, and plotting them. This is useful for training models for trajectory prediction in 3D.
Before running the scripts, you need to install the required Python packages. You can install them using pip
:
pip install numpy scipy pandas matplotlib
Or if you prefer using a requirements.txt
file:
pip install -r requirements.txt
In this case, the requirements.txt
file should contain:
numpy
scipy
pandas
matplotlib
The expected file format for the trajectory files is CSV or TXT with the following columns:
timestamp
: The time at which the sample was taken.tx
: The x-coordinate of the drone at the given timestamp.ty
: The y-coordinate of the drone at the given timestamp.tz
: The z-coordinate of the drone at the given timestamp.
-
Segment Array of points in 3D, equally spaced in time.
-
Input segment A segment that precedes an output segment. Can be used as an input to a model to be trained.
-
inp_seg_len
Input segment lenght. The number of points that constitute an input segment. -
Output segment A segment that comes after another. This can be the output of a model, given the corresponding input segment`.
-
out_seg_len
Input segment lenght. The number of points that constitute an output segment.
This script processes a single drone trajectory to create input and output segments. The segments are saved in .npz
format.
python process_single_trajectory.py <trajectory_file> <input_segment_length> <output_segment_length> [--compute_velocity]
trajectory_file
: Path to the CSV or TXT file containing the drone position trajectory.input_segment_length
: Length of each input segment.output_segment_length
: Length of each output segment.--compute_velocity
: Optional flag to compute velocity segments instead of position.
This script processes multiple drone trajectories in a directory to create input and output segments for each trajectory. The segments are concatenated and saved in a single .npz
file.
python process_multiple_trajectories.py <directory_path> <input_segment_length> <output_segment_length> [--compute_velocity]
directory_path
: Path to the directory containing multiple drone trajectory files.input_segment_length
: Length of each input segment.output_segment_length
: Length of each output segment.--compute_velocity
: Optional flag to compute velocity segments instead of position.
-
input_segments:
- Shape:
(3, inp_seg_len, total_num_input_segments)
- Description: This is a 3D array where each "slice" along the third dimension represents a segment of the input trajectory. The first dimension corresponds to the three coordinates
tx
,ty
, andtz
. The second dimension corresponds to the time steps in each segment.
- Shape:
-
output_segments:
- Shape:
(3, out_seg_len, total_num_output_segments)
- Description: Similar to
input_segments
, but for the output segments.
- Shape:
-
num_input_segments:
- Shape: Scalar
- Description: Total number of input segments processed across all files.
-
num_output_segments:
- Shape: Scalar
- Description: Total number of output segments processed across all files.
-
inp_seg_len:
- Shape: Scalar
- Description: The length of each input segment.
-
out_seg_len:
- Shape: Scalar
- Description: The length of each output segment.
-
knots_input_i (for each segment i):
- Shape:
(inp_seg_len + k + 1,)
wherek
is the degree of the spline (3 in our case). - Description: Knots of the spline representation for the i-th input segment.
- Shape:
-
coeffs_input_i (for each segment i):
- Shape:
(inp_seg_len,)
- Description: Coefficients of the spline representation for the i-th input segment.
- Shape:
-
knots_output_i (for each segment i):
- Shape:
(out_seg_len + k + 1,)
- Description: Knots of the spline representation for the i-th output segment.
- Shape:
-
coeffs_output_i (for each segment i):
- Shape:
(out_seg_len,)
- Description: Coefficients of the spline representation for the i-th output segment.
- Shape:
This script takes a directory of drone trajectories and divides each trajectory into equal segments of a given length. Note that there are no input/output segments here. the script simply divides the trajectory into consecutive segments (think of a sequence of words in a sentence!).
python divide_into_segments_multiple.py <directory_path> <segment_length> [--compute_velocity]
directory_path
: Path to the directory containing multiple drone trajectory files.segment_length
: Length of each segment.--compute_velocity
: Optional flag to compute velocity segments instead of position.
The .npz
file contains multiple arrays, each corresponding to a different trajectory file. The naming convention is <filename>_segments
.
segment_length
: A scalar indicating the length of each segment.
This script resamples each drone trajectory in a directory at a given sampling time. The resampled trajectories are saved in a new directory.
python resample_trajectory.py <directory_path> <sampling_time_in_seconds>
directory_path
: Path to the directory containing multiple drone trajectory files.sampling_time_in_seconds
: The time interval for resampling in seconds.
This script plots a single drone trajectory from a CSV or TXT file. It also has an option to plot velocity vectors.
python plot_trajectory.py <trajectory_file> [--plot_velocity]
trajectory_file
: Path to the CSV or TXT file containing the drone position trajectory.--plot_velocity
: Optional flag to plot velocity vectors.
Script for Computing Position and Velocity Statistics from Trajectory Data.
This script reads trajectory data in CSV or TXT format from a specified directory. Each file should contain timestamp, tx, ty, and tz columns, representing time and 3D position data. The script computes the mean and standard deviation of positions (tx, ty, tz), calculates the velocity components (vx, vy, vz) and their magnitudes, and then computes the mean and standard deviation of the velocity magnitudes.
Additionally, it computes and prints the maximum length of all position vectors and the maximum velocity magnitudes from all trajectory files. These values are also saved in the respective .npz files.
The script also computes, saves, and prints the covariance matrices and their inverses for both position and velocity datasets, and performs a whitening transformation on the data.
The results, including position and velocity statistics and covariance matrices, are saved
in two .npz files: pos_stats.npz
and vel_stats.npz
. It also generates histograms for the positions
and velocity magnitudes.
The script also normalizes the original trajectories (using max norm, and whitening) and saves the normalized trajectories in CSV/TXT files in the parent directory of the provided path. These normalized trajectories can then be processed by process_multiple_trajectories.py
to generate normalized position and velocity datasets for direct training.
python compute_stats.py <directory_path>
Example: python compute_stats.py /path/to/directory
Where '/path/to/directory' is the path to the directory containing your CSV/TXT files.
The .gitignore
file is set to ignore any .npz
files and directories that start with "resampled".