Skip to main content

Traja is a trajectory analysis and visualization tool

Project description

traja |Python-ver| |Travis| |PyPI| |RTD| |Gitter| |Black| |License| |Binder| |Codecov| |DOI|

.. |Python-ver| image:: :target: :alt: Python 3.6+

.. |Travis| image:: :target:

.. |PyPI| image:: :target:

.. |Gitter| image:: :target:

.. |RTD| image:: :target: :alt: Documentation Status

.. |Black| image:: :target:

.. |License| image:: :target: :alt: License: MIT

.. |Binder| image:: :target:

.. |Codecov| image:: :target:

.. |DOI| image:: :target:

traja is a Python library for trajectory analysis. It extends the capability of pandas DataFrame specific for animal trajectory analysis in 2D, and provides convenient interfaces to other geometric analysis packages (eg, R and shapely).


The traja Python package is a toolkit for the numerical characterization and analysis of the trajectories of moving animals. Trajectory analysis is applicable in fields as diverse as optimal foraging theory, migration, and behavioral mimicry (e.g. for verifying similarities in locomotion). A trajectory is simply a record of the path followed by a moving animal. Traja operates on trajectories in the form of a series of locations (as x, y coordinates) with times. Trajectories may be obtained by any method which provides this information, including manual tracking, radio telemetry, GPS tracking, and motion tracking from videos.

The goal of this package (and this document) is to aid biological researchers, who may not have extensive experience with Python, to analyze trajectories without being handicapped by a limited knowledge of Python or programming. However, a basic understanding of Python is useful.

If you use traja in your publications, please cite the repo

.. code-block::

  author       = {Justin Shenk and
                  Rüdiger Busche},
  title        = {justinshenk/traja: v0.1.1},
  month        = jun,
  year         = 2019,
  doi          = {10.5281/zenodo.3237827},
  url          = {}

Installation and setup

To install traja with conda, run

conda install -c conda-forge traja

or with pip

pip install traja.

Run the graphical user interface with python

Import traja into your Python script or via the Python command-line with import traja.

Trajectories with traja

traja stores trajectories in pandas DataFrames, allowing any pandas functions to be used.

Load trajectory with x,y and time coordinates:

.. code-block:: python

import traja

df = traja.read_file('coords.csv')

Once a DataFrame is loaded, use the .traja accessor to access the visualization and analysis methods:

.. code-block:: python

df.traja.plot(title='Cage trajectory')

Analyze Trajectory

.. csv-table:: The following functions are available via traja.trajectory.[method] :header: "Function", "Description" :widths: 30, 80

"calc_derivatives", "Calculate derivatives of x, y values " "calc_turn_angles", "Calculate turn angles w.r.t. x-axis " "transitions", "Calculate first-order Markov model for transitions between grid bins" "generate", "Generate random walk" "resample_time", "Resample to consistent step_time intervals" "rediscretize_points", "Rediscretize points to given step length"

Random walk

Generate random walks with

.. code-block:: python

df = traja.generate(n=1000, step_length=2)

.. image:: :alt: walk_screenshot.png


Rediscretize the trajectory into consistent step lengths with traja.trajectory.rediscretize where the R parameter is the new step length.

.. code-block:: python

rt = df.traja.rediscretize(R=5000)

.. image:: :alt: rediscretized

Resample time

traja.trajectory.resample_time allows resampling trajectories by a step_time.

Flow Plotting

.. code-block:: python

df = traja.generate()

.. image:: :alt: 3D plot

.. code-block:: python

traja.plot_quiver(df, bins=32)

.. image:: :alt: quiver plot

.. code-block:: python

traja.plot_contour(df, filled=False, quiver=False, bins=32)

.. image:: :alt: contour plot

.. code-block:: python

traja.plot_contour(df, filled=False, quiver=False, bins=32)

.. image:: :alt: contour plot filled

.. code-block:: python

traja.plot_contour(df, bins=32, contourfplot_kws={'cmap':'coolwarm'})

.. image:: :alt: streamplot


traja code implementation and analytical methods (particularly rediscretize_points) are heavily inspired by Jim McLean's R package trajr <>__. Many thanks to Jim for his feedback.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for traja, version 0.2.3
Filename, size File type Python version Upload date Hashes
Filename, size traja-0.2.3.tar.gz (77.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page