Operations on Trajectory objects
Let us consider a simple 2D trajectory:
traj = Trajectory(points=[[1,2], [3,3], [4,2]])
There are several ways a trajectory can be modified in yupi.
Shifting
If the trajectory needs to be shifted it can be done by adding (or substracting) a tuple (or any array-like structure) with the same dimensions of the trajectory points:
transf_traj = traj + (1, 4) # transf_traj points: [[2,6], [4,7], [5,6]]
centered = traj - traj.r[0] # centered points: [[0,0], [2,1], [3,0]]
Both operations can be made in-place when using the operators: += or
-=.
Scaling
Spatial scaling of a trajectory can be also achieved by multiplying it by a constant:
scaled_traj = traj * 3 # scaled_traj points: [[3,6], [9,9], [12,6]]
This operation can be made in-place when using the operator *=.
Indexing and slicing
Trajectory objects can also be indexing and obtain the i-th TrajectoryPoint:
traj = Trajectory(points=[[1,2], [3,3], [4,2]])
p2 = traj[2] # p2.r = [4,2]
Slicing is possible too and it is used to obtain a subtrajectory. All variance of slicing in python are possible:
traj = Trajectory(points=[[1,2], [3,3], [4,2], [4,1], [2,7]])
sub_traj_1 = traj[2:] # sub_traj_1.r = [[4,2], [4,1], [2,7]]
sub_traj_2 = traj[:-1] # sub_traj_2.r = [[1,2], [3,3], [4,2], [4,1]]
sub_traj_3 = traj[2:4] # sub_traj_3.r = [[4,2], [4,1]]
Filtering
Trajectory objects can be filtered in different ways. By
default, yupi offers an Exponential Convolutional Filter, typically used in
the context of animal trajectory analysis:
from yupi.transformations import exp_convolutional_filter
traj = Trajectory(points=[[1,2], [3,3], [4,2]])
smoothed_traj = exp_convolutional_filter(traj, 1)
Aditionally, a simple exponentially moving average filter is provided
from yupi.transformations import exp_moving_average_filter
traj = Trajectory(points=[[1,2], [3,3], [4,2]])
smoothed_traj = exp_moving_average_filter(traj, alpha=1/100)
For trajectories with non-uniform time samples, a tau parameter must be provided and
an adaptive alpha parameter is calculated as alpha = 1- exp(-dt/tau) for each time step.
Adding and subtracting
If two trajectories have the same length and dimensions they can be added or subtracted by:
traj_a = Trajectory(points=[[1,2], [3,3], [4,2]])
traj_b = Trajectory(points=[[0,0], [1,4], [2,3]])
traj_c = traj_a + traj_b # traj_c points: [[1,2], [4,7], [6,5]]