tracktable.applications.anomaly_detection module

Module contents

tracktable.applications.anomaly_detection - Determine if trajectories are anomalous based on the number of historical trajectories that pass by the same area.

The anomaly_detection() and R-Tree related functions are the primary driver functions for anomaly detection.

tracktable.applications.anomaly_detection.anomaly_detection(trajectories_to_analyze, historical_trajectories=[], historical_points_rtree=None, point_idx_to_traj_idx={}, num_historical_trajectories=0, nearness_radius=5, consider_direction=False, num_control_points=4, start_fraction=0, end_fraction=1, anomaly_threshold=0, include_segments=False, ram_limited=False, filename=None)[source]

Analyzes a list of trajectories against a list of historical trajectories to detect anomalies. Anomalies are any trajectories with no historical trajectories that pass by them.

“Passing by” a trajectory is determined by examining num_control_points points equally-spaced along the trajectory and finding historical trajectories within a (2*nearness_radius) cube of all control points.

Parameters

trajectories_to_analyze (list) – list of Tracktable trajectorys We will compare each of these trajectories to all of the historical trajectories to determine anomalous behavior.

Keyword Arguments
  • historical_trajectories (list) – list of Tracktable trajectorys, opt. We will compare each trajectory in trajectories_to_analyze to these trajectories to determine anomalous behavior. If not specified, the historical_points_rtree must be given. (Default: [])

  • historical_points_rtree (Tracktable RTree object) – We will compare each trajectory in trajectories_to_analyze to these trajectory points to determine anomalous behavior. If not specified, historical_trajectories must be given so that an rtree can be created. (Default: None)

  • point_idx_to_traj_idx (dict) – A quick lookup to check what trajectory index a point index from the rtree corresponds to. If not specified, will be created. (Default: {})

  • num_historical_trajectories (int) – The total number of historical trajectories. (Default: 0)

  • nearness_radius (float) – The inradius, in km, of the cubes centered at each control point. Only trajectories within all of these cubes will be considered passersby. (Default: 5)

  • consider_direction (bool) – CURRENTLY UNABLE TO CONSIDER DIRECTION If true, we will only consider a historical trajectory to be a passerby if it is traveling the same direction as the trajectory being analyzed. (Default: False)

  • num_control_points (int) – The number of equally-spaced points to sample along each trajectory when looking for passersby. (Default: 4)

  • start_fraction (float) – The fraction along the trajectory where you want to start sampling control points when looking for passersby. (Default: 0)

  • end_fraction (float) – The fraction along the trajectory where you want to stop sampling control points when looking for passersby. (Default: 1)

  • anomaly_threshold (int) – Trajectories with total passersby equal to or less than this number will be considered anomalous. (Default: 0)

  • include_segments (bool) – Flag to find historical passersby with by spliting the trajectories into segments, requires additional computational resources (Default: False)

  • ram_limited (bool) – Create an rtree from points WITHOUT creating a points list by adding points to the tree one at a time, helps build rtrees on resource constrained systems. (Default: False)

  • filename (bool) – CURRENTLY UNABLE TO OUTPUT ANOMALIES TO FILE. File to output anomalies to. (Default: False)

Returns

List that contains the index for every trajectory index that passes by the given trajectory.

tracktable.applications.anomaly_detection.count_anomalies(nearby_trajectories)[source]
Given a dictionary with test trajectory indices as keys and passerby

historical trajectory indices as values (as a list), count how many test trajectories have no historical trajectories passing by.

Parameters

nearby_trajectories (list) – nearby trajectories that pass by the anomalous trajectories.

Returns

Number of anomalous trajectories

tracktable.applications.anomaly_detection.create_points_list(trajectories)[source]

Create a list of points for a list of trajectories.

Parameters

trajectories (list) – List of trajectories to get points of.

Returns

List of points

tracktable.applications.anomaly_detection.create_points_list_and_lookup(trajectories, create_lookup=True)[source]

Create a list and lookup of points for a list of trajectories.

Parameters

trajectories (list) – List of trajectories to get points of.

Keyword Arguments

create_lookup (bool) – Create a dictionary for reverse-lookup of trajectory index. (Default: True)

Returns

List of points and lookup

tracktable.applications.anomaly_detection.create_rtree(trajectories=None, reader=None, ram_limited=False)[source]

Create an rtree.

Keyword Arguments
  • trajectories (list) – trajectories to create an rtree from. (Default: None)

  • reader (Tracktable trajectory reader) – Trajectory reader to create an rtree from. (Default: None)

  • ram_limited (bool) – Create an R-Tree without storing the trajectory points. (Default: False)

Returns

R-Tree of points and trajectory index

tracktable.applications.anomaly_detection.get_trajectory_segments(trajectory)[source]

Represents a trajectory by a list of segments

Parameters

trajectory (Trajectory) – Trajectory to get the segments of.

Returns

list of trajectory segments

tracktable.applications.anomaly_detection.points_to_rtree(points)[source]

Put points into R-Tree.

Parameters

points (list) – List of points to put into an R-Tree.

Returns

R-Tree of points

tracktable.applications.anomaly_detection.traj_reader_to_rtree(reader)[source]

Create an rtree given the trajectory reader, avoiding the need to store any tracks.

Parameters

reader (Tracktable trajectory reader) – Trajectory reader to create an rtree from.

Returns

R-Tree of points and trajectory index

tracktable.applications.anomaly_detection.trajectories_to_rtree(trajectories, create_lookup=True)[source]

Put trajectories in R-Tree.

Parameters

trajectories (list) – List of trajectories to put into R-Tree.

Keyword Arguments

create_lookup (bool) – Create a dictionary for reverse-lookup of trajectory index. (Default: True)

Returns

R-Tree of points and lookup

tracktable.applications.anomaly_detection.trajectories_to_rtree_ram_limited(trajectories)[source]

Create an rtree from points WITHOUT creating a points list by adding points to the tree one at a time.

Parameters

trajectories (list) – trajectories to create an rtree from.

Returns

R-Tree of points and trajectory index