def fixation_frequencies(range):
    
    groups, data, grp, segs = get_pursuits_and_data_by_range(range)
    
    plt.figure()
    for sid, d in groupby(data, 'session_id'):
        seg = [segs[i] for i in np.flatnonzero(groups.session_id == sid)]
        seg = np.hstack(seg).view(np.recarray)
        seg = seg[(seg.t1 - seg.t0) > 0.1] # take out saccades
        laps = groups[groups['session_id'] == sid]
        laps = laps['lap']
        mean_pursuits = 1.0*len(seg)/len(laps)
        mean_vel = np.mean(d['c_vel'])
        
        plt.plot(mean_vel, mean_pursuits, '.k')
        
        print "%s: pursuits %i, mean_per_lap %f, mean_vel: %f" % (sid, len(seg), mean_pursuits, mean_vel)
    plt.show()
def yaw_variation(is_control=True):
    
    ci = [1,3]
    #is_control = False
    
    data, segs = get_pooled(ci)
    data, segs = filter_by_treatment(data, segs, is_control)
    
    smeans = []    
    for sid, d in groupby(data, 'session_id'):
        yaw = toyota_yaw_rate_in_degrees(d['c_yaw'])
        mean = np.mean(yaw)
        std = np.std(yaw)
        print sid, mean, std
        smeans.append((sid, mean))
    #mean = np.mean(smeans)
    #std = np.std(smeans)
    #print 'mean: ' + str(mean) + ' std: ' + str(std)
    return smeans
def pursuit_vs_yaw(range):
    groups, data, grp, segs = get_pursuits_and_data_by_range(range)
    pdf_out = PdfPages('/tmp/pursuit_vs_yaw.pdf')
    
    bin_size = 0.5
    
    means = []
    slopes = []
    noises = []
    for sid, d in groupby(data, 'session_id'):

        seg = [segs[i] for i in np.flatnonzero(groups.session_id == sid)]
        seg = np.hstack(seg).view(np.recarray)
        #seg = seg[(seg.t1 - seg.t0) > 0.2] # take out saccades
        seg = seg[seg.n > 12]
        
        # filter outliers from gaze landings
        values = np.vstack((seg.d0[:,0], seg.d0[:,1]))
        fkde = gaussian_kde(values)
        x, y = np.mgrid[np.min(values[0]):np.max(values[0]):bin_size, 
                        np.min(values[1]):np.max(values[1]):bin_size]
        grid = np.vstack((x.ravel(), y.ravel()))
        z = fkde(grid)
        reshaped = z.reshape(x.shape)
        medianhdr = histogram_hdr(z, bin_size**2, 0.8)
        seg = [s for s in seg if fkde((s.d0[0], s.d0[1])) > medianhdr]
        seg = np.hstack(seg).view(np.recarray)
        
        # mean yaw within each pursuit
        withinPursuitYaw = []
        for s in seg:
            slice = d[(d['ts'] >= s['t0']) &
                      (d['ts'] <= s['t1'])]
            ym = np.mean(toyota_yaw_rate_in_degrees(slice['c_yaw']))
            withinPursuitYaw.append(ym)
        withinPursuitYaw = np.array(withinPursuitYaw)
        
        dt = seg.t1 - seg.t0
        valid = dt > 0
        dt = dt[valid]
        gspeeds = -((seg.d1[:,0] - seg.d0[:,0]) / dt)
        noise = withinPursuitYaw / 2 - gspeeds
        
        plt.figure()
        # raw scatter
        fit = np.polyfit(withinPursuitYaw, gspeeds, 1)
        rng = np.linspace(10, 20, len(withinPursuitYaw))
        rline = np.polyval(fit, rng)
        model = np.poly1d([0.5, 0])
        plt.subplot(221)
        plt.plot(withinPursuitYaw, gspeeds, '.k', alpha=0.2)
        plt.plot(rng, rline, '-b')
        plt.plot([10,20], [np.polyval(model, 10), np.polyval(model, 20)], '-g')
        plt.ylim(-50,50)
        plt.xlim(5,25)
        plt.xlabel('mean yaw deg/s')
        plt.ylabel('gaze speed deg/s')
        plt.annotate('spearman r: %f' % scipy.stats.spearmanr(withinPursuitYaw, gspeeds)[0], (10,-10))
        plt.annotate('slope: %f' % fit[0], (10,-20))
        plt.title(sid)
        
        # noise kde + pdf
        plt.subplot(222)
        kde = gaussian_kde(noise)
        rng = np.linspace(np.min(noise), np.max(noise), len(noise))
        plt.plot(rng, kde(rng), '-b', label='kde')
        plt.plot(rng, scipy.stats.norm.pdf(rng, loc=np.mean(noise), scale=np.std(noise)), '-r', label='pdf')
        plt.hist(noise, bins=30, normed=True, color='green')
        plt.xlabel('mean yaw / 2 - gaze speed')
        plt.legend(loc='upper right')
        
        # pursuit landings scatter & density map
        plt.subplot(223)
        plt.contour(x,y,reshaped, [medianhdr])
        plt.plot(seg.d0[:,0], seg.d0[:,1], ',k')
        plt.xlim(-60,60)
        plt.ylim(-20,20)
        
        pdf_out.savefig()
        plt.close()
        
        print '%i \nspearman r: %f' % (sid, scipy.stats.spearmanr(withinPursuitYaw, gspeeds)[0])
        
        # how many fixations are from right to left (74-90 degrees)
        #validspeed = gspeeds < 0
        #print '%f of fixations from right to left' % (1.0 * len(gspeeds[validspeed]) / len(gspeeds))
    
        print 'mean of yaws / gaze speeds %f' % (np.mean(gspeeds / withinPursuitYaw))#, scipy.stats.mode(np.around(withinPursuitYaw / gspeeds, 1))[0])
        print 'mean of mean yaw / 2 - gaze speed: %f' % (np.mean(withinPursuitYaw / 2 - gspeeds))
        print 'noise shapiro-wilk w: %f p: %f' % scipy.stats.shapiro(noise)
        #print 'median hdr %f' % medianhdr
        
        #print 'mean of yaws: %f' % np.mean(withinPursuitYaw)
        #print 'mean of gspeeds: %f' % np.mean(gspeeds)
        
        means.append((np.mean(withinPursuitYaw), np.mean(gspeeds)))
        slopes.append(fit)
        noises.append(noise)

    pdf_out.close()
    return (means, slopes, noises)