Ejemplo n.º 1
0
def compute_SVD(T, S):
    T_USVs, T_USVa = SVD(T)
    S_USVs, S_USVa = SVD(S)
    Ts = np.diag(T_USVs[1])[:10]
    Ta = np.diag(T_USVa[1])[:20:2]
    Ss = np.diag(S_USVs[1])[:10]
    Sa = np.diag(S_USVa[1])[:20:2]
    return np.array([Ts, Ta, Ss, Sa])
Ejemplo n.º 2
0
def compare_transition_modes(m1, m2, exact=False, n_permutations=1000):
    """Compare the transitions matrices between two groups of animals.

    Parameters
    ----------
    m1, m2: np.ndarray, shape (n_animals, n_states, n_states)
        Transition matrices for the two groups of animals
    exact : bool, default False
        Whether to compute every possible permutation
    n_permutations
        Number of permutations to compute if exact is False

    Returns
    -------
    dot_products : np.ndarray, shape (N, 3)
        N x 3 matrix containing dot products between first two symmetric and first antisymmetric transition modes across
        N permutations of animals
    """
    # Number of animals in test group
    n_test = len(m1)
    # Concatenate transition matrices
    all_transitions = np.concatenate([m1, m2], axis=0)
    idxs = np.arange(len(all_transitions))
    # Output
    dot_products = []
    # Iterate through different permutations of animals
    i = 0
    for group1, group2 in permute(idxs,
                                  n_test,
                                  exact=exact,
                                  n_permutations=n_permutations):
        if i % 10 == 0:
            print i
        # Sum transition matrices
        T1 = all_transitions[group1].sum(axis=0)
        T2 = all_transitions[group2].sum(axis=0)
        # Compute the SVD
        USVs1, USVa1 = SVD(T1)
        USVs2, USVa2 = SVD(T2)
        # Compare first two symmetric transition modes
        s1_dot = np.abs(np.dot(USVs1[0, :, 0], USVs2[0, :, 0]))
        s2_dot = np.abs(np.dot(USVs1[0, :, 1], USVs2[0, :, 1]))
        # Compare first antisymmetric transition mode
        a1_dot_a = np.abs(np.dot(USVa1[0, :, 0], USVa2[0, :, 0]))
        a1_dot_b = np.abs(np.dot(USVa1[0, :, 0], USVa2[0, :, 1]))
        a1_dot = max(a1_dot_a, a1_dot_b)
        a2_dot_a = np.abs(np.dot(USVa1[0, :, 1], USVa2[0, :, 0]))
        a2_dot_b = np.abs(np.dot(USVa1[0, :, 1], USVa2[0, :, 1]))
        a2_dot = max(a2_dot_a, a2_dot_b)
        # Append to output
        dot_products.append((s1_dot, s2_dot, a1_dot, a2_dot))
        i += 1
    return np.array(dot_products)
Ejemplo n.º 3
0
    transition_directory = create_folder(experiment.subdirs['analysis'],
                                         'transitions')
    for condition, fish_info in experiment.data.groupby('condition'):
        print condition
        condition_directory = create_folder(transition_directory, condition)
        condition_bouts = mapped_bouts[mapped_bouts['ID'].isin(
            fish_info['ID'])]
        # Compute the transition matrix for each fish
        T = fish_transition_matrices(condition_bouts,
                                     state_col='exemplar',
                                     n_states=len(isomap),
                                     shuffle=False)
        np.save(os.path.join(condition_directory, 'transition_matrices.npy'),
                T)
        print T.shape
        # Redistribute transitions
        W = generate_weights(isomap, bandwidth=40.)
        WTW = redistribute_transitions(T, W)
        np.save(
            os.path.join(condition_directory,
                         'smoothed_transition_matrices.npy'), WTW)
        # Sum transitions over all fish
        T_all = T.sum(axis=0)
        WTW_all = redistribute_transitions(T_all, W)
        np.save(os.path.join(condition_directory, 'T.npy'), T_all)
        np.save(os.path.join(condition_directory, 'WTW.npy'), WTW_all)
        # SVD
        USVs, USVa = SVD(WTW_all)
        np.save(os.path.join(condition_directory, 'USVs.npy'), USVs)
        np.save(os.path.join(condition_directory, 'USVa.npy'), USVa)
Ejemplo n.º 4
0
                     'smoothed_transition_matrices.npy'))
    unilateral_matrices = np.load(
        os.path.join(transition_directory, 'unilateral',
                     'smoothed_transition_matrices.npy'))

    # timer = Timer()
    # timer.start()
    # dot_products = compare_transition_modes(unilateral_matrices, control_matrices, exact=False, n_permutations=1000)
    # np.save(os.path.join(transition_directory, 'compare_control_unilateral.npy'), dot_products)
    # print timer.convert_time(timer.stop())
    # print '\n'

    T1 = np.sum(control_matrices, axis=0)
    T2 = np.sum(unilateral_matrices, axis=0)

    USVs1, USVa1 = SVD(T1)
    USVs2, USVa2 = SVD(T2)

    # Compare first two symmetric transition modes
    s1_dot = np.abs(np.dot(USVs1[0, :, 0], USVs2[0, :, 0]))
    s2_dot = np.abs(np.dot(USVs1[0, :, 1], USVs2[0, :, 1]))
    # Compare first antisymmetric transition mode
    a1_dot_a = np.abs(np.dot(USVa1[0, :, 0], USVa2[0, :, 0]))
    a1_dot_b = np.abs(np.dot(USVa1[0, :, 0], USVa2[0, :, 1]))
    a1_dot = max(a1_dot_a, a1_dot_b)
    a2_dot_a = np.abs(np.dot(USVa1[0, :, 1], USVa2[0, :, 0]))
    a2_dot_b = np.abs(np.dot(USVa1[0, :, 1], USVa2[0, :, 1]))
    a2_dot = max(a2_dot_a, a2_dot_b)

    print s1_dot, s2_dot
    print a1_dot, a2_dot
from datasets.main_dataset import experiment
from behaviour_analysis.analysis.transitions import SVD
import numpy as np
import os

if __name__ == "__main__":

    transition_directory = os.path.join(experiment.subdirs['analysis'], 'transitions')
    WTW = np.load(os.path.join(transition_directory, 'WTW.npy'))
    USVs, USVa = SVD(WTW)
    np.save(os.path.join(transition_directory, 'USVs.npy'), USVs)
    np.save(os.path.join(transition_directory, 'USVa.npy'), USVa)