dtype={'ID': str, 'video_code': str})
    exemplar_info = exemplar_info[exemplar_info['clean']]
    exemplars = BoutData.from_directory(exemplar_info, mapping_experiment.subdirs['kinematics'],
                                        check_tail_lengths=False, tail_columns_only=True)
    exemplars = exemplars.map(eigenfish, whiten=True, mean=mean_tail, std=std_tail)
    exemplars = exemplars.list_bouts(values=True, ndims=n_dims)

    # Set paths
    output_directory = create_folder(experiment.subdirs['analysis'], 'distance_matrices')

    # Import experiment bouts
    experiment_bouts = import_bouts(experiment.directory)
    experiment_bouts = experiment_bouts.map(eigenfish, whiten=True, mean=mean_tail, std=std_tail)

    # 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)
예제 #2
0
from datasets.blumenkohl import experiment as blu
from datasets.lakritz import experiment as lak
from behaviour_analysis.manage_files import create_folder
from behaviour_analysis.miscellaneous import print_heading, Timer
from behaviour_analysis.analysis.stimulus_mapping import BoutStimulusMapper
import os
import pandas as pd

if __name__ == "__main__":

    for experiment in (blu, lak):

        print_heading(os.path.basename(experiment.directory))
        stimulus_map_directory = create_folder(experiment.subdirs['analysis'],
                                               'stimulus_maps')

        mapped_bouts = pd.read_csv(os.path.join(experiment.subdirs['analysis'],
                                                'mapped_bouts.csv'),
                                   index_col=0,
                                   dtype={
                                       'ID': str,
                                       'video_code': str
                                   })

        # Calculate stimulus maps for each fish in parallel
        timer = Timer()
        timer.start()
        mapper = BoutStimulusMapper(mapped_bouts,
                                    experiment,
                                    stimulus_map_directory,
                                    n_threads=20)
예제 #3
0
from datasets.virtual_prey import fish_data, analysis_directory
from behaviour_analysis.analysis.eye_convergence import EyeConvergenceAnalyser
from behaviour_analysis.miscellaneous import find_contiguous, print_heading, print_subheading
import numpy as np
import os


if __name__ == "__main__":

    # =========
    # PREP DATA
    # =========

    print_heading('Prepping data')

    for key, data in fish_data.iteritems():

        print key

        # Calculate eye convergence as 100 ms rolling median of difference in eye angles
        tracking = data['tracking']
        tracking['convergence'] = (tracking['right_angle'] - tracking['left_angle']).rolling(window=30,
                                                                                             center=True).median()
        tracking = tracking[~np.isnan(tracking['convergence'])]
        tracking['dt'] = tracking['t'].diff()

        # Calculate velocity
        tracking['v'] = np.linalg.norm(tracking[['f0_vx', 'f0_vy']], axis=1)
        tracking['v'] = tracking['v'].rolling(window=3, center=True).median()
        tracking['v'] = tracking['v'].rolling(window=15, center=True).mean()
                           dtype={
                               'ID': str,
                               'video_code': str
                           })
    bouts_df = bouts_df.loc[bout_indices]
    bouts = BoutData.from_directory(bouts_df,
                                    experiment.subdirs['kinematics'],
                                    tail_columns_only=True,
                                    check_tail_lengths=False)

    eigenfish = np.load(paths['eigenfish'])
    mean_tail, std_tail = np.load(paths['tail_statistics'])

    transformed = bouts.map(eigenfish,
                            whiten=True,
                            mean=mean_tail,
                            std=std_tail)
    transformed_bouts = transformed.list_bouts(values=True, ndims=n_dims)

    print_heading('CALCULATING DISTANCE MATRIX - NORMAL')
    distance_matrix = calculate_distance_matrix(transformed_bouts,
                                                fs=frame_rate,
                                                flip=False)
    np.save(paths['distance_matrix_normal'], distance_matrix)

    print_heading('CALCULATING DISTANCE MATRIX - FLIPPED')
    distance_matrix = calculate_distance_matrix(transformed_bouts,
                                                fs=frame_rate,
                                                flip=True)
    np.save(paths['distance_matrix_flipped'], distance_matrix)
                               'video_code': str
                           })

    convergence_scores = pd.read_csv(convergence_scores_path,
                                     dtype={'ID': str})

    frame_rate = 500.
    window = int(0.02 * 500)

    # Import bout data
    bouts = BoutData.from_directory(bouts_df,
                                    experiment.subdirs['kinematics'],
                                    check_tail_lengths=False,
                                    tail_columns_only=False)

    print_heading('CLASSIFYING BOUTS')
    convergence_states = np.empty((len(bouts_df), 4))
    i = 0
    for idx, fish_info in convergence_scores.iterrows():
        print fish_info.ID
        for bout in bouts.list_bouts(IDs=[fish_info.ID]):
            bout_convergence = np.degrees(bout['right'] - bout['left'])
            convergence_start = bout_convergence[:window].mean()
            convergence_end = bout_convergence[-window:].mean()
            convergence_states[i, :2] = np.array(
                [convergence_start, convergence_end])
            convergence_states[i, 2:] = (np.array(
                [convergence_start, convergence_end]) >= fish_info.threshold)
            i += 1
    assert i == len(convergence_states), 'Incorrect number of bouts!'
    np.save(os.path.join(eye_convergence_directory, 'convergence_states.npy'),
예제 #6
0
    # transition_directory = os.path.join(blu.subdirs['analysis'], 'transitions')
    # control_matrices = np.load(os.path.join(transition_directory, 'het', 'transition_matrices.npy'))
    # mutant_matrices = np.load(os.path.join(transition_directory, 'mut', 'transition_matrices.npy'))
    #
    # timer = Timer()
    # timer.start()
    # dot_products = compare_transition_modes(mutant_matrices, control_matrices, exact=False, n_permutations=1000)
    # np.save(os.path.join(transition_directory, 'compare_control_mutant.npy'), dot_products)
    # print timer.convert_time(timer.stop())
    # print '\n'

    # =======
    # Lakritz
    # =======

    print_heading('Lakritz')

    transition_directory = os.path.join(lak.subdirs['analysis'], 'transitions')
    control_matrices = np.load(
        os.path.join(transition_directory, 'ctrl', 'transition_matrices.npy'))
    mutant_matrices = np.load(
        os.path.join(transition_directory, 'mut', 'transition_matrices.npy'))

    timer = Timer()
    timer.start()
    dot_products = compare_transition_modes(mutant_matrices,
                                            control_matrices,
                                            exact=True)
    np.save(os.path.join(transition_directory, 'compare_control_mutant.npy'),
            dot_products)
    print timer.convert_time(timer.stop())
예제 #7
0
                                     'eigenfish.npy'))

    # Import bouts
    capture_strike_data = BoutData.from_directory(capture_strikes, experiment.subdirs['kinematics'],
                                                  check_tail_lengths=False, tail_columns_only=True)

    # Transform
    transformed_strikes = capture_strike_data.map(eigenfish, whiten=True, mean=mean_tail, std=std_tail)
    transformed_strikes = transformed_strikes.list_bouts(values=True, ndims=3)

    # Truncate
    truncated_strikes = [bout[12:37] for bout in transformed_strikes]
    bw = 0.006  # 3 frames

    # Calculate distance matrix
    print_heading('CALCULATING CAPTURE STRIKE DISTANCE MATRIX')
    timer = Timer()
    timer.start()
    D_normal = calculate_distance_matrix(truncated_strikes, bw=bw)
    D_flipped = calculate_distance_matrix(truncated_strikes, bw=bw, flip=True)
    D = np.min([D_normal, D_flipped], axis=0)
    np.save(paths['distance_matrix'], D)
    time_taken = timer.stop()
    print 'Time taken: {}'.format(timer.convert_time(time_taken))

    # Perform embedding
    D = squareform(D)
    np.random.seed(1992)
    isomap = IsomapPrecomputed(n_neighbors=5, n_components=2)
    isomapped_strikes = isomap.fit_transform(D)
    np.save(paths['isomapped_strikes'], isomapped_strikes)
    exemplars = pd.read_csv(os.path.join(experiment.subdirs['analysis'],
                                         'exemplars.csv'),
                            index_col='bout_index',
                            dtype=dict(ID=str, video_code=str))
    exemplars = exemplars[exemplars['clean']]
    exemplar_bouts = BoutData.from_directory(exemplars,
                                             experiment.subdirs['kinematics'],
                                             check_tail_lengths=False,
                                             tail_columns_only=True)

    # Transform bouts
    transformed, pca = exemplar_bouts.transform(whiten=True)
    transformed_bouts = transformed.list_bouts(values=True, ndims=ndims)

    # Calculate distance matrices
    print_heading('CALCULATING DISTANCE MATRIX - NORMAL')
    distance_matrix_1 = calculate_distance_matrix(transformed_bouts,
                                                  fs=500.,
                                                  flip=False)

    print_heading('CALCULATING DISTANCE MATRIX - FLIPPED')
    distance_matrix_2 = calculate_distance_matrix(transformed_bouts,
                                                  fs=500.,
                                                  flip=True)

    distance_matrix = np.min([distance_matrix_1, distance_matrix_2], axis=0)
    distance_matrix = squareform(distance_matrix)
    np.save(os.path.join(output_directory, 'distance_matrix.npy'),
            distance_matrix)

    # Perform embedding