# Compute distance matrices
    print_heading('CALCULATING DISTANCE MATRICES')
    distances = {}
    analysis_times = []
    timer = Timer()
    timer.start()
    for ID in experiment_bouts.metadata['ID'].unique():
        output_path, path_exists = create_filepath(output_directory, ID, '.npy', True)
        if path_exists:
            distances[ID] = np.load(output_path)
        if not path_exists:
            print ID + '...',
            queries = experiment_bouts.list_bouts(IDs=[ID], values=True, ndims=n_dims)
            fish_distances = calculate_distance_matrix_templates(queries, exemplars, fs=frame_rate)
            distances[ID] = fish_distances
            time_taken = timer.lap()
            analysis_times.append(time_taken)
            print timer.convert_time(time_taken)
            np.save(output_path, fish_distances)
    if len(analysis_times) > 0:
        print 'Average time: {}'.format(timer.convert_time(timer.average))

    # Assign exemplars
    mapped_bouts = experiment_bouts.metadata.copy()
    mapped_bouts['exemplar'] = None
    for ID, fish_distances in distances.iteritems():
        bout_idxs = mapped_bouts[mapped_bouts['ID'] == ID].index
        nearest_exemplar = np.argmin(fish_distances, axis=1)
        mapped_bouts.loc[bout_idxs, 'exemplar'] = nearest_exemplar
    mapped_bouts.to_csv(os.path.join(experiment.subdirs['analysis'], 'mapped_bouts.csv'))
Exemplo n.º 2
0
    timer = Timer()
    timer.start()

    # Compute the average of 100 shuffled transition matrices for each fish
    print 'Generating shuffled matrices...',
    n_shuffles = 100
    S = np.zeros((n_fish, len(isomap), len(isomap)))
    for shuffle in range(n_shuffles):
        S += fish_transition_matrices(bouts, shuffle=True, verbose=False)
    S /= n_shuffles
    S = redistribute_transitions(S, weights)
    np.save(
        os.path.join(transition_directory, 'shuffled_transition_matrices.npy'),
        S)
    print timer.convert_time(timer.lap())

    S = np.load(
        os.path.join(transition_directory, 'shuffled_transition_matrices.npy'))
    T = np.load(
        os.path.join(transition_directory, 'smoothed_transition_matrices.npy'))

    # Generate permuted matrices
    print 'Generating permutations...',
    S_permuted = np.empty((n_permutations, S.shape[1], S.shape[2]))
    T_permuted = np.empty((n_permutations, T.shape[1], T.shape[2]))
    for permutation in range(n_permutations):
        shuffled_idxs = np.random.permutation(
            np.arange(n_fish))[:(n_fish + 1) / 2]
        S_permuted[permutation] = S[shuffled_idxs].sum(axis=0)
        T_permuted[permutation] = T[shuffled_idxs].sum(axis=0)