def animate_and_follow(individual=0, num_neighbours=15):
    test_trajectories_file = os.path.join(dir_of_data, 'test_trajectories.npy')
    t = np.load(test_trajectories_file)
    tt.interpolate_nans(t)
    tt.center_trajectories_and_normalise(t)
    s_ = tt.smooth(t)
    v_ = tt.smooth_velocity(t)

    e_ = tt.normalise(v_[:, individual, :])
    s = tt.center_in_individual(s_, individual)
    s = tt.fixed_to_comoving(s, e_)
    v = tt.fixed_to_comoving(v_, e_)
    center = tt.fixed_to_comoving(-s_[:, [individual], :], e_)

    indices = ttsocial.give_indices(s, num_neighbours)
    sn = ttsocial.restrict(s, indices, individual)
    vn = ttsocial.restrict(v, indices, individual)

    sf = s[:, [individual], :]
    vf = v[:, [individual], :]

    anim = ttanimation.scatter_vectors(s, velocities=v, k=10)
    anim += ttanimation.scatter_ellipses(s, velocities=v, color='c')
    anim += ttanimation.scatter_ellipses(sn, velocities=vn, color='b')
    anim += ttanimation.scatter_ellipses(sf, velocities=vf, color='r')
    anim += ttanimation.scatter_circle(center)
    anim.prepare()
    anim.show()
Example #2
0
    def from_positions(cls, t, interpolate_nans=True, smooth_params=None):
        """Trajectory from positions

        :param t: Positions nd.array.
        :param interpolate_nans: whether to interpolate NaNs
        :param smooth_params: Arguments for smoothing (see tt.smooth)
        """
        if smooth_params is None:
            smooth_params = {"sigma": -1, "only_past": False}
        # Interpolate trajectories
        if interpolate_nans:
            tt.interpolate_nans(t)

        displacement = np.array([0.0, 0.0])

        # Smooth trajectories
        if smooth_params["sigma"] > 0:
            t_smooth = tt.smooth(t, **smooth_params)
        else:
            t_smooth = t

        trajectories = {}

        if smooth_params.get("only_past", False):
            [
                trajectories["_s"],
                trajectories["_v"],
                trajectories["_a"],
            ] = tt.velocity_acceleration_backwards(t_smooth)
        else:
            [
                trajectories["_s"],
                trajectories["_v"],
                trajectories["_a"],
            ] = tt.velocity_acceleration(t_smooth)

        # TODO: Organise the params dictionary more hierarchically
        # Maybe in the future add a "how_construct" key being a dictionary
        # with the classmethod called, path, interpolate_nans,
        # and smooth_params
        params = {
            "displacement": displacement,  # Units: pixels
            "length_unit": 1,  # Units: pixels
            "length_unit_name": "px",
            "time_unit": 1,  # In frames
            "time_unit_name": "frames",
            "interpolate_nans": interpolate_nans,
            "smooth_params": smooth_params,
            "construct_method": "from_positions",
        }

        return cls(trajectories, params)
Example #3
0
    def from_positions(cls,
                       t,
                       interpolate_nans=True,
                       smooth_sigma=0,
                       only_past=True,
                       unit_length=None,
                       frame_rate=None,
                       arena_radius=None):
        trajectories = Namespace()
        trajectories.raw = t.copy()
        if interpolate_nans:
            tt.interpolate_nans(t)

        radius, center_x, center_y, unit_length = \
            tt.center_trajectories_and_normalise(t, unit_length=unit_length,
                                                 forced_radius=arena_radius)
        if smooth_sigma > 0:
            t_smooth = tt.smooth(t, sigma=smooth_sigma, only_past=only_past)
        else:
            t_smooth = t

        if only_past:
            [trajectories.s, trajectories.v, trajectories.a] = \
                tt.velocity_acceleration_backwards(t_smooth)
        else:
            [trajectories.s, trajectories.v, trajectories.a] = \
                tt.velocity_acceleration(t_smooth)

        trajectories.speed = tt.norm(trajectories.v)
        trajectories.acceleration = tt.norm(trajectories.a)
        trajectories.distance_to_center = tt.norm(trajectories.s)
        trajectories.e = tt.normalise(trajectories.v)
        trajectories.tg_acceleration = tt.dot(trajectories.a, trajectories.e)
        trajectories.curvature = tt.curvature(trajectories.v, trajectories.a)
        trajectories.normal_acceleration = \
            np.square(trajectories.speed)*trajectories.curvature
        traj = cls(trajectories)
        traj.params = {
            "frame_rate": frame_rate,
            "center_x": center_x,  # Units: unit_length
            "center_y": center_y,  # Units: unit length
            "radius": radius,  # Units: unit length
            "unit_length": unit_length
        }  # Units: pixels
        return traj
Example #4
0
import os

import matplotlib as mpl
import numpy as np

import trajectorytools as tt
import trajectorytools.animation as ttanimation
from trajectorytools.constants import dir_of_data

if __name__ == "__main__":
    test_trajectories_file = os.path.join(dir_of_data, "test_trajectories.npy")
    t = np.load(test_trajectories_file)
    tt.center_trajectories_and_normalise(t)
    tt.interpolate_nans(t)

    t = tt.smooth(t, sigma=0.5)
    s_, v_, a_ = tt.velocity_acceleration(t)

    speed = tt.norm(v_)
    colornorm = mpl.colors.Normalize(vmin=speed.min(),
                                     vmax=speed.max(),
                                     clip=True)
    mapper = mpl.cm.ScalarMappable(norm=colornorm, cmap=mpl.cm.hot)
    color = mapper.to_rgba(speed)
    anim1 = ttanimation.scatter_vectors(s_, velocities=v_, k=10)
    anim2 = ttanimation.scatter_ellipses_color(s_, v_, color)

    anim = anim1 + anim2
    anim.prepare()
    anim.show()