Exemplo n.º 1
0
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 get_average_local_polarization(t, number_of_neighbours = 1):
    if t.number_of_individuals == 1:
        return('nan')
    else:
       indices = ttsocial.give_indices(t.s, number_of_neighbours)
       en = ttsocial.restrict(t.e,indices)[...,1:,:]
       local_polarization = tt.norm(tt.collective.polarization(en))
       return np.nanmean(local_polarization, axis = -1) 
Exemplo n.º 3
0
def local_polarization(tr, n=1, x=1):
    if n < x:
        return (np.nan, np.nan)
    if tr.number_of_individuals == 1:
        return (np.nan, np.nan)
    if x >= tr.number_of_individuals:
        return (np.nan, np.nan)
    else:
        indices = ttsocial.give_indices(
            tr.s, n)  # indices of the closest n neighbors
        a = ttsocial.restrict(
            tr.e, indices
        )  # normalized velocity (direction) vectors of focal individual and closest n neighbors
        b = np.multiply(
            a[:, :, 0, :],
            a[:, :,
              x, :])  #polarization between focal and xth closest individual
        c = np.sum(b, axis=2)  #dot product of vectors.
        lp = tt.norm(tt.collective.polarization(a))
        #return(c)
        return (np.nanmean(lp), np.nanmean(c))
def local_polarization_func(tr, n=1, x=1):  #shape returns tr.s.shape -2
    if n < x:
        return (np.nan, np.nan)
    if tr.number_of_individuals == 1:
        return (np.nan, np.nan)
    if x >= tr.number_of_individuals:
        return (np.nan, np.nan)
    else:
        indices = ttsocial.neighbour_indices(
            tr.s[1:-1], n)  # indices of the closest n neighbors
        a = ttsocial.restrict(
            tr.e[1:-1], indices
        )  # normalized velocity (direction) vectors of focal individual and closest n neighbors
        b = np.multiply(
            a[:, :, 0, :],
            a[:, :,
              x, :])  #polarization between focal and xth closest individual
        c = np.sum(b, axis=2)  #dot product of vectors.
        lp = tt.norm(tt.collective.polarization(a))
        #return(c)
        return (np.nanmean(lp[~filter_speed(tr, 5).mask]),
                np.nanmean(c[~filter_speed(tr, 5).mask]))
Exemplo n.º 5
0
def local_polarization(tr, frames, n=1, x=1):  #uses filtered data
    if n < x:
        return (np.nan)
    if tr.number_of_individuals == 1:
        return (np.nan)
    if x >= tr.number_of_individuals:
        return (np.nan)
    else:
        indices = ttsocial.neighbour_indices(
            filter_low_pass(tr), n)  # indices of the closest n neighbors
        a = ttsocial.restrict(
            filter_direction_low_pass(tr)[1:-1], indices
        )  # normalized velocity (direction) vectors of focal individual and closest n neighbors
        b = np.multiply(
            a[:, :, 0, :],
            a[:, :,
              x, :])  #polarization between focal and xth closest individual
        b_mask = np.ma.array(b, mask=filter_direction_low_pass(tr)[1:-1].mask)
        c = np.nansum(b_mask, axis=2)  #dot product of vectors.
        #lp = tt.norm(tt.collective.polarization(a))
        #return(c)
        return (np.nanmean(c[frames]))
def get_average_aligment_score(t, number_of_neighbours = 3):
    indices = ttsocial.give_indices(t.s, number_of_neighbours)
    en = ttsocial.restrict(t.e,indices)[...,1:,:]
    alignment = np.nanmean(tt.dot(np.expand_dims(t.e,2), en), axis = -1)
    return np.nanmedian(alignment, axis = -1)