Smoothing and Analysis

Smoothing

Smoothing can be performed using smooth_sg().

traja.trajectory.smooth_sg(trj: traja.frame.TrajaDataFrame, w: int = None, p: int = 3)[source]

Returns DataFrame of trajectory after Savitzky-Golay filtering.

Parameters:
  • trj (TrajaDataFrame) – Trajectory
  • w (int) – window size (Default value = None)
  • p (int) – polynomial order (Default value = 3)
Returns:

Trajectory

Return type:

trj (TrajaDataFrame)

>> df = traja.generate()
>> traja.smooth_sg(df, w=101).head()
           x          y  time
0 -11.194803  12.312742  0.00
1 -10.236337  10.613720  0.02
2  -9.309282   8.954952  0.04
3  -8.412910   7.335925  0.06
4  -7.546492   5.756128  0.08
https://raw.githubusercontent.com/justinshenk/traja/master/docs/images/smoothed.png

Length

Length of trajectory can be calculated using length().

traja.trajectory.length(trj: traja.frame.TrajaDataFrame) → float[source]

Calculates the cumulative length of a trajectory.

Parameters:trj (TrajaDataFrame) – Trajectory
Returns:length (float)
>> df = traja.generate()
>> traja.length(df)
2001.142339606066

Distance

Net displacement of trajectory (start to end) can be calculated using distance().

traja.trajectory.distance(trj: traja.frame.TrajaDataFrame) → float[source]

Calculates the distance from start to end of trajectory, also called net distance, displacement, or bee-line from start to finish.

Parameters:trj (TrajaDataFrame) – Trajectory
Returns:distance (float)
>> df = traja.generate()
>> traja.distance(df)
117.01507823153617

Displacement

Displacement (distance travelled) can be calculated using calc_displacement().

traja.trajectory.calc_displacement(trj: traja.frame.TrajaDataFrame, lag=1)[source]

Returns a Series of float displacement between consecutive indices.

Parameters:
  • trj (TrajaDataFrame) – Trajectory
  • lag (int) – time steps between displacement calculation
Returns:

Displacement series.

Return type:

displacement (pandas.Series)

>>> df = traja.TrajaDataFrame({'x':[0,1,2],'y':[1,2,3]})
>>> traja.calc_displacement(df)
0         NaN
1    1.414214
2    1.414214
Name: displacement, dtype: float64

Derivatives

traja.trajectory.get_derivatives(trj: traja.frame.TrajaDataFrame)[source]

Returns derivatives displacement, displacement_time, speed, speed_times, acceleration, acceleration_times as dictionary.

Parameters:trj (TrajaDataFrame) – Trajectory
Returns:Derivatives
Return type:derivs (DataFrame)
>> df = traja.TrajaDataFrame({'x':[0,1,2],'y':[1,2,3],'time':[0.,0.2,0.4]})
>> df.traja.get_derivatives() 
   displacement  displacement_time     speed  speed_times  acceleration  acceleration_times
0           NaN                0.0       NaN          NaN           NaN                 NaN
1      1.414214                0.2  7.071068          0.2           NaN                 NaN
2      1.414214                0.4  7.071068          0.4           0.0                 0.4

Speed Intervals

traja.trajectory.speed_intervals(trj: traja.frame.TrajaDataFrame, faster_than: float = None, slower_than: float = None) → pandas.core.frame.DataFrame[source]

Calculate speed time intervals.

Returns a dictionary of time intervals where speed is slower and/or faster than specified values.

Parameters:
  • faster_than (float, optional) – Minimum speed threshold. (Default value = None)
  • slower_than (float or int, optional) – Maximum speed threshold. (Default value = None)
Returns:

result (DataFrame) – time intervals as dataframe

Note

Implementation ported to Python, heavily inspired by Jim McLean’s trajr package.

>> df = traja.generate()
>> intervals = traja.speed_intervals(df, faster_than=100)
>> intervals.head()
   start_frame  start_time  stop_frame  stop_time  duration
0            1        0.02           3       0.06      0.04
1            4        0.08           8       0.16      0.08
2           10        0.20          11       0.22      0.02
3           12        0.24          15       0.30      0.06
4           17        0.34          18       0.36      0.02