def plot_residency_time_histogram(config, dataset, save_figure_path=''):
    odor_stimulus = 'on'
    threshold_distance_min = 0.05
    
    
    # no odor
    keys_odor_off = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=10, threshold_distance=-1, odor_stimulus='none', upwind_only=True, threshold_distance_min=0.1, odor=True, post_behavior='landing')
    residency_time_odor_off = []
    for key in keys_odor_off:
        trajec = dataset.trajecs[key]
        if trajec.residency_time is not None:
            residency_time_odor_off.append(trajec.residency_time)
            
    # odor on, odor experienced
    keys_odor_on_true = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=10, threshold_distance=-1, odor_stimulus='on', upwind_only=True, threshold_distance_min=0.1, odor=True, post_behavior='landing')
    residency_time_odor_on_true = []
    for key in keys_odor_on_true:
        trajec = dataset.trajecs[key]
        if trajec.residency_time is not None:
            residency_time_odor_on_true.append(trajec.residency_time)
        
    # odor on, odor not experienced
    keys_odor_on_false = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=10, threshold_distance=-1, odor_stimulus='on', upwind_only=False, threshold_distance_min=0.1, odor=False, post_behavior='landing')
    residency_time_odor_on_false = []
    for key in keys_odor_on_false:
        trajec = dataset.trajecs[key]
        if trajec.residency_time is not None:
            residency_time_odor_on_false.append(trajec.residency_time)
    

    data = [np.array(residency_time_odor_off), np.array(residency_time_odor_on_true), np.array(residency_time_odor_on_false)]
    colors = ['black', 'red', 'blue']
    
    nbins = 30 # note: if show_smoothed=True with default butter filter, nbins needs to be > ~15 

    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    fpl.histogram(ax, data, bins=nbins, bin_width_ratio=0.8, colors=colors, edgecolor='none', bar_alpha=1, curve_fill_alpha=0.4, curve_line_alpha=0, curve_butter_filter=[3,0.3], return_vals=False, show_smoothed=True, normed=True, normed_occurences=False, bootstrap_std=False, exponential_histogram=False)
    
    #xticks = [0,.02,.04,.06,.08,.15]
    fpl.adjust_spines(ax, ['left', 'bottom'])
    #ax.set_xlim(xticks[0], xticks[-1])
    ax.set_xlabel('residency time, frames')
    ax.set_ylabel('Occurences, normalized')
    ax.set_title('Residency time')
    
    
    if save_figure_path == '':
        save_figure_path = os.path.join(config.path, config.figure_path, 'activity/')
    fig.set_size_inches(8,8)
    figname = save_figure_path + 'residency_time_on_post_histogram' + '.pdf'
    fig.savefig(figname, format='pdf')
def plot_distance_histogram(config, dataset, save_figure_path=''):
    
    # no odor
    keys_no_odor = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=10, odor_stimulus='none', threshold_distance_min=0.1, odor=True)
    data_no_odor = []
    for key in keys_no_odor:
        trajec = dataset.trajecs[key]
        if np.max(trajec.distance_to_post) > 0.1:
            for f, d in enumerate(trajec.distance_to_post):
                if trajec.positions[f,0] > 0:
                    data_no_odor.append(d)
        
    # odor on, odor experienced
    keys_odor_on_true = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=10, odor_stimulus='on', threshold_distance_min=0.1, odor=True)
    data_odor_on_true = []
    for key in keys_odor_on_true:
        trajec = dataset.trajecs[key]
        if np.max(trajec.distance_to_post) > 0.1:
            for f, d in enumerate(trajec.distance_to_post):
                if trajec.positions[f,0] > 0:
                    data_odor_on_true.append(d)
    
    print len(data_no_odor)
    print len(data_odor_on_true)
    
    data = [np.array(data_no_odor), np.array(data_odor_on_true)]
    colors = ['black', 'red']
    nbins = 30
    bins = np.linspace(0,0.1,nbins)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    fpl.histogram(ax, data, bins=bins, bin_width_ratio=0.8, colors=colors, edgecolor='none', bar_alpha=1, curve_fill_alpha=0.4, curve_line_alpha=0, curve_butter_filter=[3,0.3], return_vals=False, show_smoothed=True, normed=True, normed_occurences=False, bootstrap_std=True, exponential_histogram=False, n_bootstrap_samples=10000, smoothing_range=[0.005,0.1])
    
    xticks = [0,.02,.04,.06,.08,.1]
    fpl.adjust_spines(ax, ['left', 'bottom'], xticks=xticks)
    ax.set_xlim(xticks[0], xticks[-1])
    ax.set_xlabel('Distance to post, m')
    ax.set_ylabel('Occurences, normalized')
    title_text = 'Distance to post. red (upwind, odor) N = ' + str(len(keys_odor_on_true)) + '; black (upwind, no odor) N = ' + str(len(keys_no_odor))
    ax.set_title(title_text)
    
    
    if save_figure_path == '':
        save_figure_path = os.path.join(config.path, config.figure_path, 'activity/')
    fig.set_size_inches(8,8)
    figname = save_figure_path + 'distance_to_post_histogram' + '.pdf'
    fig.savefig(figname, format='pdf')
def plot_time_to_saccade_histogram(config, dataset, save_figure_path=''):
    threshold_odor = 100
    
    # no odor
    keys_odor_off = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=threshold_odor, threshold_distance=-1, odor_stimulus='none', upwind_only=True, threshold_distance_min=0.1, odor=True)
    time_to_saccade_odor_off = get_time_to_saccade(dataset, keys_odor_off, threshold_odor=threshold_odor)
            
    # odor on, odor experienced
    keys_odor_on_true = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=threshold_odor, threshold_distance=-1, odor_stimulus='on', upwind_only=True, threshold_distance_min=0.1, odor=True)
    time_to_saccade_odor_on_true = get_time_to_saccade(dataset, keys_odor_on_true, threshold_odor=threshold_odor)
    
    # odor on, odor not experienced
    keys_odor_on_false = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=threshold_odor, threshold_distance=-1, odor_stimulus='on', upwind_only=False, threshold_distance_min=0.1, odor=False)
    keys_ok = []
    for key in keys_odor_on_false:
        trajec = dataset.trajecs[key]
        if np.max(trajec.odor) > 0.01:
            keys_ok.append(key)
    print 'control odor on: ', len(keys_ok)
    time_to_saccade_odor_on_false = get_time_to_saccade(dataset, keys_ok, threshold_odor=0.001)
        

    data = [np.array(time_to_saccade_odor_off), np.array(time_to_saccade_odor_on_true)]
    print [len(d) for d in data]
def pdf_book(config, dataset, save_figure_path=''):
    if save_figure_path == '':
        figure_path = os.path.join(config.path, config.figure_path)
        save_figure_path=os.path.join(figure_path, 'odor_traces/')
        
    figure_path = os.path.join(path, config.figure_path)
    save_figure_path = os.path.join(figure_path, 'odor_traces/')
    pdf_name_with_path = os.path.join(save_figure_path, 'odor_heading_histogram.pdf')
    pp = PdfPages(pdf_name_with_path)

    threshold_odor=10
    threshold_distance_min=.1
    
    for odor in [True]:
        
        key_set = {}
        for odor_stimulus in config.odor_stimulus.keys():
            keys_tmp = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=threshold_odor, odor_stimulus=odor_stimulus, threshold_distance_min=threshold_distance_min, odor=odor)
            
            keys = []
            for key in keys_tmp:
                trajec = dataset.trajecs[key]
                add_key = True
                if trajec.positions[0,0] < 0.3:
                    add_key = False
                if odor:
                    frames_in_odor = np.where(trajec.odor > threshold_odor)[0]
                    if len(frames_in_odor) < 40:
                        add_key = False
                if add_key:
                    keys.append(key)
            
            if len(keys) > 0:    
                key_set.setdefault(odor_stimulus, keys)

        for odor_stimulus, keys in key_set.items():
            print 'Odor Book, Chapter: ', odor_stimulus
            
            #book_name = 'odor_headings_book_' + odor_stimulus + '_' + str(odor) + '.pdf'
            plot_odor_heading_book(pp, threshold_odor, path, config, dataset, keys=keys)
            
            
    pp.close()
def plot_odor_traces_for_odor_puffs_before_post(path, config, dataset, axis='xy'):
    keys = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=50, threshold_distance=0.1)
    plot_odor_traces(path, config, dataset, keys=keys, axis=axis)
Esempio n. 6
0
def print_stats(config, dataset):

    threshold_distance_min = 0.1
    threshold_odor = 10
    
    for odor_stimulus in config.odor_stimulus.keys():
        print
        print 'Experiment Conditions: Odor ', odor_stimulus.replace('_', ' ').title()
        print
        print 'Idependent of Odor: '
        print
        nlanding = len(fad.get_keys_with_attr(dataset, ['post_behavior','odor_stimulus'], ['landing',odor_stimulus]))
        nboomerang = len(fad.get_keys_with_attr(dataset, ['post_behavior','odor_stimulus'], ['boomerang',odor_stimulus]))
        ntakeoff = len(fad.get_keys_with_attr(dataset, ['post_behavior','odor_stimulus'], ['takeoff',odor_stimulus]))
        keys = fad.get_keys_with_attr(dataset, ['odor_stimulus'], [odor_stimulus])
        n_flies = len(keys)
        
        mean_speeds = []
        frame_length = []
        for key in keys:
            trajec = dataset.trajecs[key]
            mean_speeds.append( np.mean(trajec.speed) )
            frame_length.append( len(trajec.speed) )
        
        mean_speeds = np.array(mean_speeds)
        frame_length = np.array(frame_length)
        
        print 'num flies: ', n_flies
        print 'num landing: ', nlanding
        print 'landing ratio: ', nlanding / float(n_flies)
        print 'num boomerang: ', nboomerang
        print 'boomerang ratio: ', nboomerang / float(n_flies)
        print 'num takeoff: ', ntakeoff
        print 'mean speed: ', np.mean(mean_speeds), ' +/- ', np.std(mean_speeds)
        print 'mean trajec length (secs): ', np.mean(frame_length) / (100.), ' +/- ', np.std(frame_length) / (100.)
        
        #############################################################################################################################################################
        print
        print 'Experienced Odor: '
        print
        keys = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=threshold_odor, odor_stimulus=odor_stimulus)
        
        nlanding = len(fad.get_keys_with_attr(dataset, ['post_behavior','odor_stimulus'], ['landing',odor_stimulus], keys=keys))
        nboomerang = len(fad.get_keys_with_attr(dataset, ['post_behavior','odor_stimulus'], ['boomerang',odor_stimulus], keys=keys))
        ntakeoff = len(fad.get_keys_with_attr(dataset, ['post_behavior','odor_stimulus'], ['takeoff',odor_stimulus], keys=keys))
        keys_with_attr = fad.get_keys_with_attr(dataset, ['odor_stimulus'], [odor_stimulus], keys=keys)
        n_flies = len(keys_with_attr)
        
        mean_speeds = []
        frame_length = []
        mean_speeds_in_odor = []
        mean_speeds_not_in_odor = []
        mean_speeds_not_in_odor_after = []
        residency_time = []
        n_saccades_close_to_post = []
        frames_until_saccade = []
        heading_after_saccade = []
        
        for key in keys_with_attr:
            trajec = dataset.trajecs[key]
            mean_speeds.append( np.mean(trajec.speed) )
            frame_length.append( len(trajec.speed) )
            
            frames_in_odor = np.where(trajec.odor > threshold_odor)[0].tolist()
            frames_in_odor_and_center = []
            for f in frames_in_odor:
                if trajec.distance_to_post[f] > 0.05:
                    if np.linalg.norm(trajec.positions[f,1:]) < 0.07:
                        frames_in_odor_and_center.append(f)
                            
            frames_not_in_odor = np.where(trajec.odor < 1)[0].tolist()
            frames_not_in_odor_prior_to_odor = []
            for f in frames_not_in_odor:
                if f < frames_in_odor[0]:
                    if trajec.distance_to_post[f] > 0.05:
                        if np.linalg.norm(trajec.positions[f,1:]) < 0.07:
                            frames_not_in_odor_prior_to_odor.append(f)
                    
            frames_not_in_odor_after_to_odor = []
            for f in frames_not_in_odor:
                if f > frames_in_odor[-1]:
                    if trajec.distance_to_post[f] > 0.05:
                        if np.linalg.norm(trajec.positions[f,1:]) < 0.07:
                            frames_not_in_odor_after_to_odor.append(f)
                
            if len(frames_in_odor_and_center) > 0:
                mean_speeds_in_odor.append(np.mean(trajec.speed[frames_in_odor_and_center]))
            
            if len(frames_not_in_odor_prior_to_odor) > 0:
                mean_speeds_not_in_odor.append(np.mean(trajec.speed[frames_not_in_odor_prior_to_odor]))
            
            if len(frames_not_in_odor_after_to_odor) > 0:
                mean_speeds_not_in_odor_after.append(np.mean(trajec.speed[frames_not_in_odor_after_to_odor]))
                
            if trajec.residency_time is not None:
                residency_time.append(trajec.residency_time)
                
            ## saccade analysis
            nsacs = 0
            for sac in trajec.saccades:
                if np.min(trajec.distance_to_post[sac]) < 0.05:
                    nsacs += 1
            n_saccades_close_to_post.append(nsacs)
            
            ## after odor, how long till next saccade?
            frames_in_odor = np.where(trajec.odor > threshold_odor)[0].tolist()
            for sac in trajec.saccades:
                if sac[0] > frames_in_odor[-1]:
                    frames_until_saccade.append(sac[0] - frames_in_odor[-1])
                    heading_after_saccade.append(np.abs(trajec.heading_smooth[sac[-1]]))
                    break
        
        mean_speeds = np.array(mean_speeds)
        frame_length = np.array(frame_length)
        
        print 'num flies: ', n_flies
        print 'num landing: ', nlanding
        print 'landing ratio: ', nlanding / float(n_flies)
        print 'num boomerang: ', nboomerang
        print 'boomerang ratio: ', nboomerang / float(n_flies)
        print 'num takeoff: ', ntakeoff
        print 'mean speed: ', np.mean(mean_speeds), ' +/- ', np.std(mean_speeds)
        print 'mean speed while in odor: ', np.mean(mean_speeds_in_odor), ' +/- ', np.std(mean_speeds_in_odor)
        print 'mean speed while NOT in odor PRIOR: ', np.mean(mean_speeds_not_in_odor), ' +/- ', np.std(mean_speeds_not_in_odor)
        print 'mean speed while NOT in odor AFTER: ', np.mean(mean_speeds_not_in_odor_after), ' +/- ', np.std(mean_speeds_not_in_odor_after)
        print 'mean trajec length (secs): ', np.mean(frame_length) / (100.), ' +/- ', np.std(frame_length) / (100.)
        print 'mean residency time (sec): ', np.mean(residency_time) / (100.), ' +/- ', np.std(residency_time) / (100.), len(residency_time)
        print 'n saccades near post: ', np.mean(n_saccades_close_to_post), ' +/- ', np.std(n_saccades_close_to_post)
        print 'n sec till saccade after odor: ', np.mean(frames_until_saccade)/100., ' +/- ', np.std(frames_until_saccade)/100.
        print 'heading after saccade: ', np.mean(heading_after_saccade)/100., ' +/- ', np.std(heading_after_saccade)/100.
        print
        #############################################################################################################################################################
        print
        print 'Experienced NO Odor: '
        print
        keys = opa.get_keys_with_odor_before_post(config, dataset, threshold_odor=threshold_odor, odor_stimulus=odor_stimulus, odor=False)
        
        nlanding = len(fad.get_keys_with_attr(dataset, ['post_behavior','odor_stimulus'], ['landing',odor_stimulus], keys=keys))
        nboomerang = len(fad.get_keys_with_attr(dataset, ['post_behavior','odor_stimulus'], ['boomerang',odor_stimulus], keys=keys))
        ntakeoff = len(fad.get_keys_with_attr(dataset, ['post_behavior','odor_stimulus'], ['takeoff',odor_stimulus], keys=keys))
        keys_with_attr = fad.get_keys_with_attr(dataset, ['odor_stimulus'], [odor_stimulus], keys=keys)
        n_flies = len(keys_with_attr)
        
        mean_speeds = []
        frame_length = []
        mean_speeds_near_center = []
        residency_time = []
        n_saccades_close_to_post = []
        for key in keys_with_attr:
            trajec = dataset.trajecs[key]
            mean_speeds.append( np.mean(trajec.speed) )
            frame_length.append( len(trajec.speed) )
            
            frames = range(0,trajec.length)
            frames_to_use = []
            for f in frames:
                if trajec.distance_to_post[f] > 0.05:
                    if np.linalg.norm(trajec.positions[f,1:]) < 0.07:
                        frames_to_use.append(f)
            
            if len(frames_to_use) > 0:
                mean_speeds_near_center.append(np.mean(trajec.speed[frames_to_use]))
                
            if trajec.residency_time is not None:
                residency_time.append(trajec.residency_time)
                
            nsacs = 0
            for sac in trajec.saccades:
                if np.min(trajec.distance_to_post[sac]) < 0.05:
                    nsacs += 1
            n_saccades_close_to_post.append(nsacs)
        
        mean_speeds = np.array(mean_speeds)
        frame_length = np.array(frame_length)
        
        print 'num flies: ', n_flies
        print 'num landing: ', nlanding
        print 'landing ratio: ', nlanding / float(n_flies)
        print 'num boomerang: ', nboomerang
        print 'boomerang ratio: ', nboomerang / float(n_flies)
        print 'num takeoff: ', ntakeoff
        print 'mean speed: ', np.mean(mean_speeds), ' +/- ', np.std(mean_speeds)
        print 'mean speed while near center: ', np.mean(mean_speeds_near_center), ' +/- ', np.std(mean_speeds_near_center)
        print 'mean trajec length (secs): ', np.mean(frame_length) / (100.), ' +/- ', np.std(frame_length) / (100.)
        print 'mean residency time (sec): ', np.mean(residency_time) / (100.), ' +/- ', np.std(residency_time) / (100.), len(residency_time)
        print 'n saccades near post: ', np.mean(n_saccades_close_to_post), ' +/- ', np.std(n_saccades_close_to_post)
        print
        print '**********************************'
        print