Skip to main content

A tool to study 2D trajectories

Project description

trajectorytools is a library with some utils to study and plot 2D trajectories.

Installation

From PyPI

pip install trajectorytools

From Source

To clone this repository:

git clone https://github.com/fjhheras/trajectorytools

To install requirements:

pip install -r requirements.txt

To install the package:

pip install .

or alternatively, locally with a symlink:

pip install -e .

If you see this error: “gcc: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory” you need the GNU C++ compiler. To install it in, for example, Ubuntu and derivatives:

sudo apt install g++

Example

import numpy as np
import matplotlib as mpl

import trajectorytools as tt
import trajectorytools.animation as ttanimation
import trajectorytools.socialcontext as ttsocial
from trajectorytools.constants import test_raw_trajectories_path

# Loading test trajectories as a numpy array of locations
test_trajectories = np.load(test_raw_trajectories_path, allow_pickle=True)

# We will process the numpy array, interpolate nans and smooth it.
# To do this, we will use the Trajectories API
smooth_params = {'sigma': 1}
traj = tt.Trajectories.from_positions(test_trajectories,
                                      smooth_params=smooth_params)

# We assume a circular arena and populate center and radius keys
center, radius = traj.estimate_center_and_radius_from_locations()

# We center trajectories around the estimated center
traj.origin_to(center)

# We will normalise the location by the radius:
traj.new_length_unit(radius)

# We will change the time units to seconds. The video was recorded at 32
# fps, so we do:
traj.new_time_unit(32, 'second')

# Now we can find the smoothed trajectories, velocities and accelerations
# in traj.s, traj.v and traj.a
# We can use, for instance, the positions in traj.s and find the border of
# the group:
in_border = ttsocial.in_alpha_border(traj.s, alpha=5)

# Animation showing the fish on the border
colornorm = mpl.colors.Normalize(vmin=0,
                                 vmax=3,
                                 clip=True)
mapper = mpl.cm.ScalarMappable(norm=colornorm, cmap=mpl.cm.RdBu)
color = mapper.to_rgba(in_border)

anim1 = ttanimation.scatter_vectors(traj.s, velocities=traj.v, k=0.3)
anim2 = ttanimation.scatter_ellipses_color(traj.s, traj.v, color)
anim = anim1 + anim2

anim.prepare()
anim.show()

In the directory examples, you can find some more example scripts. Scripts use some example trajectories, which can be found in data. All example trajectories were obtained using idtracker.ai on videos recorded in de Polavieja Lab (Champalimaud Research, Lisbon)

NOTE

Note that, when using constructors like from_idtrackerai and from_positions, we need to calculate velocity and accelerations from positions. As a result, the traj object has 2 frames less than the original positions array. By default, the missing frames correspond to the first and last frames of the video. If you used the option “only_past”:True in smooth_params, the missing frames correspond to the first two frames of the video.

Project maintainers

Francisco J.H. Heras (2017-) Francisco Romero Ferrero (2017-) Dean Rance (2021-)

Contribute

We welcome contributions. The preferred way to report problems is by creating an issue. The best way to propose changes in the code is to create a pull request. Please, check our contribution guidelines and our code of conduct.

License

This project is licensed under the terms of the GNU General Public License v3.0 (See COPYING). This means that you may copy, distribute and modify the software as long as you track changes/dates in source files. However, any modifications to GPL-licensed code must also be made available under the GPL along with build & install instructions.

If you use this work in an academic context and you want to acknowledge us, please cite some of the relevant papers:

Romero-Ferrero, F., Bergomi, M. G., Hinz, R. C., Heras, F. J., & de Polavieja, G. G. (2019). idtracker.ai: tracking all individuals in small or large collectives of unmarked animals. Nature methods, 1

Heras, F. J., Romero-Ferrero, F., Hinz, R. C., & de Polavieja, G. G. (2019). Deep attention networks reveal the rules of collective motion in zebrafish. PLoS computational biology, 15(9), e1007354.

Project details


Download files

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

Source Distribution

trajectorytools-0.3.4a0.tar.gz (43.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trajectorytools-0.3.4a0-py3-none-any.whl (880.0 kB view details)

Uploaded Python 3

File details

Details for the file trajectorytools-0.3.4a0.tar.gz.

File metadata

  • Download URL: trajectorytools-0.3.4a0.tar.gz
  • Upload date:
  • Size: 43.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for trajectorytools-0.3.4a0.tar.gz
Algorithm Hash digest
SHA256 a72e0efa9b1b2f86cad5b95c3454f878286fde0468db7f8a1f4c4a661599161e
MD5 cf00fd899e5d36bdec4070d66e109f6b
BLAKE2b-256 7beca2f4f89d8884e9aa38ba0686097977b3b00169c5b825c2b12fb2f2714f26

See more details on using hashes here.

File details

Details for the file trajectorytools-0.3.4a0-py3-none-any.whl.

File metadata

  • Download URL: trajectorytools-0.3.4a0-py3-none-any.whl
  • Upload date:
  • Size: 880.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for trajectorytools-0.3.4a0-py3-none-any.whl
Algorithm Hash digest
SHA256 30e1b8437b5e6a223dbb366b9f3186d230796e989db32a0b7c5344aa68cfab7e
MD5 4730a4a9e6cd1b7f03a3f45d74cd1279
BLAKE2b-256 5f2e2ce466319ca25df7856e2b3e776c9b7c6a6a2856d09681fe128a4dc2b8de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page