Working with trajectory data in python

This notebook shows some examples of loading and working with anTraX results in python. The notebook can be downloaded from https://github.com/Social-Evolution-and-Behavior/anTraX/blob/master/docs/analysis_nb.ipynb.

In [1]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
In [2]:
from antrax import *
from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
idx = pd.IndexSlice

Load data from a tracked experiment

We will use data from dataset J16. Replace the path to the correct path on your system. By default, data from all IDs ('ants') and all movies will be loaded, but it can be changed using the arguments.

In [3]:
expdir = '/Users/asaf/Box Sync/anTraX/datasets2upload/J16'
ex = axExperiment(expdir, session='antrax_demo')
ad = axAntData(ex, movlist=[1], antlist=['GO','PP'])

The data is loaded into the antdata object, and is stored in the pandas table antdata.data. By default, the values of the xy coordinates, the orientation angle, and the assignment type are loaded:

In [4]:
ad.data.head()
Out[4]:
ant GO PP
feature x y or ass_type x y or ass_type
frame
1 0.038876 0.063685 NaN 4.0 0.038876 0.063685 NaN 4.0
2 0.038865 0.063683 NaN 4.0 0.038865 0.063683 NaN 4.0
3 0.038875 0.063679 NaN 4.0 0.038875 0.063679 NaN 4.0
4 0.038876 0.063675 NaN 4.0 0.038876 0.063675 NaN 4.0
5 0.038863 0.063664 NaN 4.0 0.038863 0.063664 NaN 4.0

The 'ass_type' column indicates the type of assigment:

(1) single-animal tracklet assigned by the blob classifier

(2) single-animal tracklet assigned by the propagation algorithm

(3) multi-animal tracklet assigned by the propagation algorithm

(4) soft assigment (tracklet that is the only possible one for that ID, but was not assigned by the propagation)

(5) interpolation of coordinates, not corresponding to an actual blob

(NaN) no coordinates

See the anTraX publication for more details.

Plot the trajectories of two ants, with the background image as background

In [5]:
fig, ax = plt.subplots(1, 1, figsize=(10 ,10))

# load the background image
bg = ex.get_bg()

# get the scale parameter to convert real world units to pixels
scale = ex.prmtrs['geometry_rscale']

# show the image
ax.imshow(bg)

# plot trajectories on image
for ant in ['PP', 'GO']:
    x = ad.data.loc[:, idx[ant, 'x']]/scale
    y = ad.data.loc[:, idx[ant, 'y']]/scale
    ax.plot(x, y, alpha=0.75)

# hide axis
ax.axis('off');