tracktable.applications.prediction module

Module contents

tracktable.applications.prediction - Predict trajectory end point

We can use a database of already-seen trajectories to try to find matches for a newly identified trajectory. We do this by breaking up our already-seen trajectories into fragments, computing a feature vector for each one, and storing that feature vector in an R-tree. Then, when we want to predict where a new trajectory might be going, we compute a feature vector for it and look for the nearest neighbor in our R-tree.

Like many prediction methods, this is not guaranteed to find the right answer every time (especially when observing trajectories that don’t behave like anything else). The exact success rate depends on the quality of the match between the new trajectory and history.

class tracktable.applications.prediction.InputFile(file)[source]

Bases: object

File-like object.

get_bytes_and_reset()[source]
next()
read(size=- 1)[source]
tracktable.applications.prediction.align(rtree, all_points, trajectories, observed_trajectory, neighbor_distance)[source]

Find historical trajectories that are close to at least one sample point of the observed trajectory

Parameters
  • rtree (R-Tree) – an rtree containing all of the points from historical trajectories

  • all_points (list) – a list of all the points in the rtree

  • trajectories (list) – a list of all the historical trajectories

  • observed_trajectory (Tracktable trajectory) – Trajectory

  • neighbor_distance (int) – points within this distance to the observed trajectory are considered close to it/nearby

Returns

a dictionary of historical trajectories that align with the observed trajectory. Maps trajectory indices in trajectories to the number of sample points the historical trajectory is close to

tracktable.applications.prediction.find_relevant_trajectories_location(points, prediction_dictionary)[source]

Finds the trajectory objects associated with the historical trajectories that were well aligned with the observed trajectory

Parameters
  • points (dict) – the points dictionary returned from predict_location (first argument of the list)

  • prediction_dictionary (dict) – dictionary returned from processing historical trajectories

Returns

A list of relevant trajectories

tracktable.applications.prediction.find_relevant_trajectories_origin_destination(results, prediction_dictionary)[source]

Finds the trajectory objects associated with the historical trajectories that were well aligned with the observed trajectory

Parameters
  • results (dict) – a results dictionary returned from predict_origin_destination

  • prediction_dictionary (dict) – dictionary returned from processing historical trajectories

Returns

A list of relevant trajectories

tracktable.applications.prediction.find_same_direction_trajectories(observed_trajectory, historical_trajectories, prediction_dictionary)[source]

Find the historical trajectories that go in the same direction as the observed trajectory

Parameters
  • observed_trajectory (Tracktable trajectory) – Trajectory

  • historical_trajectories (list) – list of historical trajectories

  • prediction_dictionary (dict) – prediction dictionary object returned from process_historical data

Returns

a list of trajectory indices of trajectories that go in the same direction as the observed trajectory

tracktable.applications.prediction.find_weights_origin_destination(observed_trajectory, historical_trajectories, prediction_dictionary, weight_function)[source]

If an origin/destination prediction is being made, find the weights for each origin destination

Parameters
  • observed_trajectory (Tracktable trajectory) – Trajectory

  • historical_trajectories (list) – list of historical trajectories

  • prediction_dictionary (dict) – prediction dictionary object returned from process_historical data

  • weight_function (function) – function that takes one parameter, distance, and returns a weight inversely related to distance

Returns

a list. The first element is a list of origin/destination pairs sorted by weight, the second is a dictionary mapping origin/destination pairs to historical trajectory indices, the third is a dictionary mapping origin/destination pairs to weights

tracktable.applications.prediction.find_weights_trajectories(observed_trajectory, historical_trajectories, prediction_dictionary, weight_function)[source]

If an location prediction is being made, find the weights for each historical trajectory

Parameters
  • observed_trajectory (Tracktable trajectory) – Trajectory

  • historical_trajectories (list) – list of historical trajectories

  • prediction_dictionary (dict) – prediction dictionary object returned from process_historical data

  • weight_function (function) – function that takes one parameter, distance, and returns a weight inversely related to distance

Returns

a list. The first element is a list of trajectory ids sorted by weight, the second is an empty dictionary this is a place holder to keep format consistent with od weighting function, the third is a dictionary mapping trajectory ids to weights

tracktable.applications.prediction.find_well_aligned_trajectories(observed_trajectory, prediction_dictionary, neighbor_distance)[source]

Find the well-aligned trajectories for a given observed trajectory in historical data. A historical trajectory is well aligned to an observed trajectory if it is within neighbor_distance to each of the observed trajectory’s sample points

Parameters
  • observed_trajectory (Tracktable trajectory) – Trajectory

  • prediction_dictionary (dict) – prediction dictionary object returned from process_historical data

  • neighbor_distance (int) – points within this distance to the observed trajectory are considered close to it/nearby

Returns

a list of well-aligned trajectories’ indices

tracktable.applications.prediction.predict_location(observed_trajectory, prediction_dictionary, minutes, neighbor_distance=5, samples=4)[source]

Predicts the location of the trajectory in the specified amount of minutes

Parameters
  • observed_trajectory (Tracktable trajectory) – the observed trajectory for which to make a prediction

  • prediction_dictionary (dict) – prediction dictionary object returned from process_historical data

  • minutes (int) – Number of minutes forward to predict

Keyword Arguments
  • neighbor_distance (int) – points within this distance (km) to the observed trajectory are considered close to it/nearby. (Default: 5)

  • samples (int) – the number of points to represent the observed trajectory with. (Default: 4)

Returns

a list. The first element is a dictionary of predicted points, the second element is a dictionary of paths to the points, and the third is a dictionary of weights for the points (for all the keys are the trajectory_ids)

tracktable.applications.prediction.predict_origin_destination(observed_trajectory, prediction_dictionary, neighbor_distance=5, samples=4, printResults=True)[source]

Predicts the origin and destination of an observed trajectory

Parameters
  • observed_trajectory (Tracktable trajectory) – the observed trajectory for which to make a prediction

  • prediction_dictionary (dict) – prediction dictionary object returned from process_historical data

Keyword Arguments
  • printResults (bool) – Flag to print the results of prediction to the logger (Default: True)

  • neighbor_distance (int) – points within this distance (km) to the observed trajectory are considered close to it/nearby. (Default: 5)

  • samples (int) – the number of points to represent the observed trajectory with. (Default: 4)

Returns

a dictionary of results

tracktable.applications.prediction.process_historical_trajectories(data_file, raw_data=None, separation_time=20, separation_distance=100, minimum_length=20, minimum_total_distance=200, only_commercial=True)[source]

Process historical trajectories from a file, filter them, and construct all of the data structures needed for prediction

Parameters

data_file (file) – .csv, .tsv, or .traj file containing historical trajectory information

Keyword Arguments
  • raw_data (Tracktable trajectories) – Trajectories that need processed and aren’t coming from a file. . (Default: None)

  • separation_time (int) – Maximum permissible time (in minutes) difference between adjacent points in a trajectory. (Default: 20)

  • separation_distance (int) – Maximum permissible geographic distance (km) between adjacent points in a trajectory. (Default: 100)

  • minimum_length (int) – Complete trajectories with fewer than this many points will be discarded. (Default: 20)

  • minimum_total_distance (int) – require trajectories be longer than this distance (km). (Default: 200)

  • only_commercial (bool) – True if you want to work with only commercial flights. (Default: True)

Returns

A dictionary of data structures which will be used in the prediction algorithm.

tracktable.applications.prediction.sample_trajectory(trajectory, samples)[source]

Represent a trajectory as a list of samples evenly spaced points

Parameters
  • trajectory (Tracktable trajectory) – trajectory to sample

  • samples (int) – number of points to represent the trajectory with

Returns

list of sampled points

tracktable.applications.prediction.write_trajectories(filename, relevant_trajectories)[source]

Writes relevant trajectories to a .traj file

Parameters
  • filename (str) – file to write the relevant trajectories to, must end in .traj

  • relevant_trajectories (list) – list of trajectory objects to write to file

Returns: writes relevant trajectories to the specified file