Example #1
0
def landing_analysis_for_crash_comparison(dataset, keys=None):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    if keys is None:
        classified_keys = fa.get_classified_keys(dataset)
        keys = classified_keys['straight']
        keys = dataset.trajecs.keys()
        
    for key in keys:
        trajec = dataset.trajecs[key]
        ftp = np.arange(trajec.frames[0]-25, trajec.frames[-1]).tolist()
        ax.plot( np.log(trajec.angle_subtended_by_post[ftp]), trajec.speed[ftp], color='black', linewidth=0.5, alpha=0.05)

    keys_to_highlight = ['2_29065', '2_31060', '8_10323', '6_715']
    for key in keys:
    
        color = 'gray'
        dotcolor = 'blue'
        if key in keys_to_highlight:
            color = 'black'
            dotcolor = 'purple'
    
        trajec = dataset.trajecs[key]
        ftp = np.arange(trajec.frames[0]-25, trajec.frames[-1]).tolist()
        ax.plot( np.log(trajec.angle_subtended_by_post[ftp]), trajec.speed[ftp], color=color, linewidth=0.5, alpha=1)
        ax.plot( np.log(trajec.angle_at_deceleration), trajec.speed_at_deceleration, '.', color=dotcolor, alpha=1)
        
    fit, Rsq, x, y, yminus, yplus = fa.get_angle_vs_speed_curve(dataset, plot=False)
    ax.plot( x, y, color='purple')
    ax.fill_between(x, yplus, yminus, color='purple', linewidth=0, alpha=0.2)
        
    fa.fix_angle_log_spine(ax, histograms=False)
    fig.savefig('landing_for_crash_comparison.pdf', format='pdf')
Example #2
0
def crash_analysis(dataset, dataset_landing, keys=None):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    keys = dataset.trajecs.keys()
        
    for key in keys:
        trajec = dataset.trajecs[key]
        ftp = np.arange(trajec.frames[0], trajec.frames[-1]).tolist()
        
        color = 'gray'
        dotcolor = 'blue'
        if key == '20101111_C001H001S0045':
            color = 'purple'
            dotcolor = 'purple'
            
        if trajec.angle_at_deceleration*180/np.pi > 90:
            print key
        
        ax.plot( np.log(trajec.angle_subtended_by_post[ftp]), trajec.speed[ftp], color=color, linewidth=0.5, alpha=1)
        ax.plot( np.log(trajec.angle_at_deceleration), trajec.speed_at_deceleration, '.', color=dotcolor, alpha=1)
        
    fit, Rsq, x, y, yminus, yplus = fa.get_angle_vs_speed_curve(dataset_landing, plot=False)
    ax.plot( x, y, color='purple')
    ax.fill_between(x, yplus, yminus, color='purple', linewidth=0, alpha=0.2)
    
    fa.fix_angle_log_spine(ax, histograms=False)
    fig.savefig('crash_spagetti.pdf', format='pdf')
def landing_spagetti_plots(dataset, gain=[170000], keys=None):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    fig2 = plt.figure()
    ax2 = fig2.add_subplot(111)
    
    fig3 = plt.figure()
    ax3 = fig3.add_subplot(111)
    
    if keys is None:
        classified_keys = fa.get_classified_keys(dataset)
        keys = classified_keys['straight']
        #keys = dataset.trajecs.keys()
        
    #keys_to_highlight = [keys[2], keys[0], keys[4], keys[6]]
    keys_to_highlight = ['2_29065', '2_31060', '8_10323', '6_715']
    points_at_deceleration = True
        
    for key in keys:
        trajec = dataset.trajecs[key]
        ftp = np.arange(trajec.frames[0]-25, trajec.frame_of_landing-1).tolist()
        #ftp = np.arange(trajec.frame_at_deceleration-10, trajec.frame_of_landing-1).tolist()
        if key in keys_to_highlight:
            color = 'black'
            zorder = 10
        else:
            color = 'blue'
            zorder = 1
        ax.plot( np.log(trajec.angle_subtended_by_post[ftp]), trajec.speed[ftp], color=color, linewidth=0.5, alpha=1, zorder=zorder)
        if points_at_deceleration and key in keys_to_highlight:
            ax.plot( np.log(trajec.angle_at_deceleration), trajec.speed_at_deceleration, '.', color='black', alpha=1, zorder=zorder)
        
        ax3.plot( np.log(trajec.angle_subtended_by_post[ftp]), trajec.speed[ftp], color='black', linewidth=0.5, alpha=1)
        
        if 0:
            # simulate
            angle, speed, expansion = sim_deceleration(trajec, gain)
            if speed[0] > 0.1:        
                ax2.plot( np.log(angle), speed, color='black', linewidth=0.5, alpha=1)
                ax3.plot( np.log(angle), speed, color='black', linewidth=0.5, alpha=1)
                f = np.where( np.abs(speed-speed[0])>0 )[0][0]
                
                #ax2.plot( np.log(angle[f]), speed[f], '.', color='blue')
            
    fit, Rsq, x, y, yminus, yplus = fa.get_angle_vs_speed_curve(dataset, plot=False)
    ax2.plot( x, y, color='purple', alpha=1)
    ax2.fill_between(x, yplus, yminus, color='purple', linewidth=0, alpha=0.2)

    ax.plot( x, y, color='purple', alpha=1)
    ax.fill_between(x, yplus, yminus, color='purple', linewidth=0, alpha=0.2)
        
    fa.fix_angle_log_spine(ax, histograms=False)
    fig.savefig('deceleration_real.pdf', format='pdf')
    
    fa.fix_angle_log_spine(ax2, histograms=False)
    fig2.savefig('deceleration_sim.pdf', format='pdf')
    
    fa.fix_angle_log_spine(ax3, histograms=False)
    fig3.savefig('deceleration_comparison.pdf', format='pdf')
def neural_threshold_tti_vs_rsdet_models(dataset_landing, save_plot=True, movie_dataset=None, ttc=None):
    
    distfig = plt.figure()
    distax = distfig.add_subplot(111)
    
    radius = 0.009565
    a = np.linspace(0,2.5,100)
    
    fit, Rsq, x, y, yminus, yplus = fa.get_angle_vs_speed_curve(dataset_landing, plot=False, plot_sample_trajecs=False, post_type=['checkered', 'checkered_angled', 'black', 'black_angled'], filename=None, keys=None, tti=None, color_code_posts=False)
    std = np.mean(yplus - y)
    
    # RSDET model
    m = fit[0]
    b = fit[1]
    vel = (m*np.log(a)+b)
    
    
    print std, fit
    
    expthreshold = expansion(vel, a=a)
    
    expthreshold_plus = expansion(vel+std, a=a)
    expthreshold_minus = expansion(vel-std, a=a)
    distax.plot( np.log(a), expthreshold, color='purple')
    distax.fill_between(np.log(a), expthreshold_plus, expthreshold_minus, color='purple', linewidth=0, alpha=0.3)
    
    # True time-to-contact model
    if ttc is None:
        ttc, ttc_threshold = match_ttc_to_rsdet(ttc0=0.13, ttc_threshold0=0)
    ttc_threshold = 0
    expthreshold_ttc = expansion_from_timetocontact(ttc, a) + ttc_threshold
    distax.plot( np.log(a), expthreshold_ttc, ':', color='purple')
    
    
    
    
    # plot a sample constant velocity trajectory
    vels = [0.2, 0.4, 0.8]
    
    for vel in vels:
        fps = 5000.0
        x = np.arange(.2, 0.0, -vel/fps)
        d = x+radius
        a = 2*np.arcsin(radius / (d))
        #exp = 2/np.sqrt(1-(radius/(d))**2) * (radius/(d)**2) * vel
        exp = expansion(vel, a=a)
        indices = np.where(exp<12)[0].tolist()
        distax.plot( np.log(a[indices]), exp[indices], color='gray', linewidth=0.5)
    
    
    # plot parameters    
    fa.fix_angle_log_spine(distax, histograms=False, set_y=False)
    ylim_max = 1000
    distax.set_ylim(0,ylim_max/180.*np.pi)
    rad_ticks_y = np.linspace(0,ylim_max*np.pi/180.,5,endpoint=True)
    deg_tick_strings_y = [str(s) for s in np.linspace(0,ylim_max,5,endpoint=True)]
    for i, s in enumerate(deg_tick_strings_y):
        deg_tick_strings_y[i] = s.split('.')[0]
    distax.set_yticks(rad_ticks_y)
    distax.set_yticklabels(deg_tick_strings_y)
    distax.set_ylabel('Expansion, deg/s')
    
    if save_plot:
        distfig.savefig('neural_threshold_distance.pdf', format='pdf')
        
    return
Example #5
0
def landing(dataset_landing, movie_dataset, speed=0.2):

    behavior = 'landing'
    
    
    fps = 1000.
    dt = 1/fps
    r = 0.009565
    radius = r
    pos0 = [-0.2, 0]
    vel = [speed, 0]
    dvda = -0.2
    
    nf = 5000
    positions = np.zeros([nf, 2])
    positions[0] = pos0
    velocities = np.zeros([nf, 2])
    velocities[0] = vel
    speed = np.zeros([nf])
    speed[0] = np.linalg.norm(velocities[0])
    distance = np.zeros([nf])
    angle_subtended_by_post = np.zeros([nf])
    leg_ext = np.zeros([nf])
    frames = [0]
    frame_at_deceleration = None
    deceleration_initiated = False
    
    for f in range(1,nf): 
        if np.linalg.norm(positions[f-1])-radius <= 0.0001:
            landed = True
        else:
            landed = False
            
            
        if not landed:
            frames.append(f)
            positions[f] = positions[f-1] + velocities[f-1]*dt
            distance[f] = np.linalg.norm(positions[f]) - radius
            angle_subtended_by_post[f] = 2*np.arcsin( radius / (distance[f]+radius) )
            
            if f>5:
                #velocities[f,0] = -.21*np.log(angle_subtended_by_post[f])+.2
                da = np.log(angle_subtended_by_post[f])-np.log(angle_subtended_by_post[f-1])
                
                a = angle_subtended_by_post
                af = np.min([a[f], 3])
                exp0 = (a[f]-a[f-1])/dt #/ (-2.*np.tan(a[f]/2.))
                exp1 = (a[f-1]-a[f-2])/dt #/ (-2.*np.tan(a[f-1]/2.))
                
                m = -0.21/radius
                b = 0.159/radius
                expthreshold = (m*np.log(af)+b)*(2*np.tan(af/2.)*np.sin(af/2.))
        
                exp0 -= expthreshold
                exp1 -= expthreshold
                
                exp0 = np.max([exp0, 0])
                exp1 = np.max([exp1, 0])
                
                #c = -1*exp0 / 3500.
                
                dda = (exp1-exp0)/dt
                c = dda / 150000.
                print dda, velocities[f-1,0]
                
                c = np.min([c,0])
                v = np.max([speed[f-1] + c, 0.0])
                
                velocities[f,0] = v
            else:
                velocities[f] = velocities[f-1]

            speed[f] = np.linalg.norm(velocities[f])
            if speed[f] > -0.21*np.log(angle_subtended_by_post[f])+0.159:
                deceleration_initiated = True
                if frame_at_deceleration is None:
                    frame_at_deceleration = f
            else:
                deceleration_initiated = False
                
            if angle_subtended_by_post[f]*180/np.pi > 70 or np.isnan(angle_subtended_by_post[f]):
                leg_ext[f] = 1

    
    
            
            
    fig2 = plt.figure()  
    ax2 = fig2.add_subplot(111)
    
    fit, Rsq, x, y, yminus, yplus = fa.get_angle_vs_speed_curve(dataset_landing, plot=False)
    ax2.plot( x, y, color='blue', alpha=0.3)
    ax2.fill_between(x, yplus, yminus, color='blue', linewidth=0, alpha=0.2)
    
    angle_at_leg_extension, bins, data_filtered, xvals = fa.leg_extension_angle_histogram(movie_dataset, plot=False)
    ax2.plot(xvals, data_filtered, color='red', alpha=0.3)
    ax2.fill_between(xvals, data_filtered, np.zeros_like(xvals), color='red', linewidth=0, alpha=0.2)
    
    ax2.plot(np.log(angle_subtended_by_post), speed, color='black')
    
    fa.fix_angle_log_spine(ax2, histograms=False) 
    
    fig2.subplots_adjust(bottom=0.3, top=0.8, right=0.9, left=0.25)
    
    filename = 'landing_cartoon_plot.pdf'
    fig2.savefig(filename, format='pdf')