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()
def test_neighbour_indices_vs_adjacency_matrix_in_frame(num_neighbours): t = np.load(cons.test_raw_trajectories_path, allow_pickle=True) tt.interpolate_nans(t) num_frames = t.shape[0] num_individuals = t.shape[1] frame = t[random.randrange(0, num_frames)] nb_indices = neighbour_indices_in_frame( frame, num_neighbours=num_neighbours ) assert nb_indices.shape == (num_individuals, num_neighbours + 1) adj_matrix = adjacency_matrix_in_frame( frame, num_neighbours=num_neighbours ) assert adj_matrix.shape == (num_individuals,) * 2 # When there is an index in neighbour_indices output, the # corresponding elment in the adjacency_matrix must be True for _ in range(5): individual = random.randrange(0, num_individuals) indices_neighbours = nb_indices[individual, :] indices_no_neighbours = [ i for i in range(num_individuals) if i not in indices_neighbours ] assert np.all(adj_matrix[individual, indices_neighbours]) assert not np.any(adj_matrix[individual, indices_no_neighbours])
def setup_class(self, max_length=11): t = np.load(cons.test_raw_trajectories_path, allow_pickle=True)[:max_length] tt.interpolate_nans(t) # Modifying the first individual for super-straight movement for i in range(t.shape[0]): t[i, 0, :] = i self.t = t
def test_convex_hull_vs_alpha_border(): t = np.load(cons.test_raw_trajectories_path, allow_pickle=True) tt.interpolate_nans(t) convex_hull = in_convex_hull(t) alpha_border = in_alpha_border(t) in_alpha_border_not_in_convex_hull = np.logical_and( np.logical_not(alpha_border), convex_hull ) assert not np.any(in_alpha_border_not_in_convex_hull)
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)
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
import os import numpy as np import matplotlib as mpl import trajectorytools as tt import trajectorytools.plot as ttplot import trajectorytools.animation as ttanimation import trajectorytools.socialcontext as ttsocial 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) [s_, v_] = tt.smooth_several(t, derivatives=[0, 1]) 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()