This repository provides a clean Python implementation of DB-TRAD, a density-based algorithm for detecting anomalous 2D trajectories using the Hausdorff neighborhood. It includes:
- A minimal, dependency-light library (
src/db_trad/) implementing:- directed and undirected Hausdorff distances for polylines
- the DB-TRAD neighborhood queries and anomaly labeling
- A synthetic trajectory dataset generator consistent with the paper’s setup
- Reproducible experiments & figures (precision/recall vs. training set size)
- A command‑line interface (
scripts/run_experiment.py) to run sweeps and export results - Unit tests for core computations
Attribution / Credits
This implementation is inspired by and gives due credit to the paper:
“A Density-Based Algorithm for Detecting Anomalies in Trajectories” — Srikanth Baride, Rajesh P. Barnwal, CSIR-CMERI.
Please cite the original paper if you use this code or ideas in your work.
python -m venv .venv && source .venv/bin/activate # on Windows: .venv\\Scripts\\activate
pip install -e .
python scripts/run_experiment.py --seed 42
python scripts/run_experiment.py --n-subsets 20 --eps 0.6 0.7 0.8 --min-trs 20 25 --make-plotsThe script writes results into examples/outputs/ including CSVs and Matplotlib PNGs (and SVGs).
import numpy as np
from db_trad.db_trad import db_trad_labels
from db_trad.datasets import make_synthetic_subsets
subsets = make_synthetic_subsets(n_subsets=1, seed=7)
trajectories, true_labels = subsets[0]
pred = db_trad_labels(trajectories, eps=0.7, min_trs=20)- Hausdorff distance between polylines is implemented as the max of the two directed distances (max–min pointwise Euclidean), matching the definition used in the paper.
- The hausdorff-neighborhood uses a threshold
eps; inliers are those with at leastmin_trsneighbors withineps. Border trajectories have fewer thanmin_trsneighbors but lie in the neighborhood of an inlier. Others are labeled anomalous. - The synthetic data generator mirrors the paper’s configuration (five dense clusters + anomalies) but is provided here as reproducible NumPy code with fixed seeds.
python scripts/run_experiment.py --n-subsets 50 --eps 0.5 0.6 0.7 0.8 0.9 --min-trs 20 25 --make-plotsThis produces:
precision_vs_training_size.png/.svgrecall_vs_training_size.png/.svg- a sample panel plotting representative trajectories and anomaly labels
MIT (for this implementation). Please check the original paper for its own licensing terms.
If you use this implementation, please cite the following paper:
@inproceedings{barnwal2016density,
title={A density-based algorithm for detecting anomalous trajectories},
author={Barnwal, Rajesh P and Baride, S and Majumder, S and Ghosh, Soumya K},
booktitle={2016 International Conference on Microelectronics, Computing and Communications (MicroCom)},
pages={1--4},
year={2016},
organization={IEEE}
}