def get_pursuits_and_data_by_range(range):
    t1, t2 = range
    data = get_angled_range_data2(t1, t2)
    segs = get_pursuit_fits(t1, t2)
    
    grp, segs = zip(*segs)
    groups = np.rec.fromrecords(grp, names='session_id,lap')
    return groups, data, grp, segs
def get_pooled_data(cis, is_control):
    arr_desc = [('sid', int), ('bend', int), ('yaw', float), ('speed', float)]
    table = np.array([], dtype=arr_desc)
    
    #cis = [1,2,3]
    for ci in cis:
        data = get_angled_range_data2(CORNERING[ci][0], CORNERING[ci][1])
        segs = get_pursuit_fits(CORNERING[ci][0], CORNERING[ci][1])
        
        data, segs = filter_by_treatment(data, segs, is_control)
        
        data.sort(order=['session_id', 'ts'])
        grp, segs = zip(*segs)
        groups = np.rec.fromrecords(grp, names='session_id,lap')

        for (sid, lap), d in groupby_multiple(data, ('session_id', 'lap')):
            #if (np.sum(d['g_direction_q'] < 0.2) > 0.25*(len(d))): continue
            seg = [segs[k] for k in np.flatnonzero((groups.session_id == sid) & 
                                                   (groups.lap == lap))]
            if (len(seg) == 0): continue
            seg = np.hstack(seg).view(np.recarray)
            seg = seg[seg.n > 12]
            
            lap_dpoints = []
            for s in seg:
                dt = s.t1 - s.t0
                if dt == 0: continue
                gspeed = -((s.d1[0]-s.d0[0]) / dt)
                slice = d[(d['ts'] >= s.t0) &
                          (d['ts'] <= s.t1)]
                ym = np.mean(toyota_yaw_rate_in_degrees(slice['c_yaw']))
                lap_dpoints.append((ym, gspeed))
            
            lap_dpoints = np.array(lap_dpoints)
            
            if (len(lap_dpoints > 0)):
                yaw = np.mean(lap_dpoints[:,0])
                speed = np.median(lap_dpoints[:,1])
                arr = np.array([(sid,ci,yaw,speed)], dtype=arr_desc)
                table = np.append(table, arr)
    return table
def pursuit_yaw_means():
    
    pdf_out = PdfPages('/tmp/pursuit_vs_yaw_pooled.pdf')
    
    cis = [1,2,3]
    all_data = []
    all_segs = []
    for ci in cis:
        data = get_angled_range_data2(CORNERING[ci][0], CORNERING[ci][1])
        segs = get_pursuit_fits(CORNERING[ci][0], CORNERING[ci][1])
        all_data.append(data)
        all_segs.append(segs)
    data = np.hstack(all_data)
    segs = np.vstack(all_segs)
    data.sort(order=['session_id', 'ts'])
    
    grp, segs = zip(*segs)
    groups = np.rec.fromrecords(grp, names='session_id,lap')
    
    all_dpoints = []
    xrange = [12,18]
    for sid, i in groupby_i(data, 'session_id'):
        d = data[i]
        print sid
        seg = [segs[k] for k in np.flatnonzero(groups.session_id == sid)]
        seg = np.hstack(seg).view(np.recarray)
        seg = seg[seg.n > 12]
        
        dpoints = []
        for s in seg:
            dt = s.t1 - s.t0
            if dt == 0: continue
            gspeed = -((s.d1[0]-s.d0[0]) / dt)
            slice = d[(d['ts'] >= s.t0) &
                      (d['ts'] <= s.t1)]
            ym = np.mean(toyota_yaw_rate_in_degrees(slice['c_yaw']))
            naksu_x = pxx2heading(slice['naksu_x'])
            naksu_mean = (naksu_x[1] - naksu_x[0]) / dt
            dpoints.append((ym, gspeed, naksu_mean))
        
        all_dpoints.append(dpoints)
        dpoints = np.array(dpoints)
        print scipy.stats.pearsonr(dpoints[:,0], dpoints[:,1])
        plt.figure()
        plt.title(sid)
        plt.hist(dpoints[:,0], bins=50)
        #plt.xlim(xrange)
        pdf_out.savefig()
        plt.close()
    
    all_dpoints = np.array(all_dpoints)
    means = np.array([np.median(x,0) for x in all_dpoints])
    ymeans = means[:,0]
    gmeans = means[:,1]
    nmeans = means[:,2]
    
    #from utils import fitLine
    #fitLine(ymeans,gmeans)
    
    fit = np.polyfit(ymeans, gmeans, 1)
    lsr = np.poly1d(fit)
    model = np.poly1d([0.5, 0])
    
    plt.figure()
    plt.plot(ymeans,gmeans,'.k')
    plt.plot(xrange, lsr(xrange), '-b')
    plt.plot(xrange, model(xrange), '-g')
    plt.xlabel('mean yaw rate (deg/s)')
    plt.ylabel('mean gaze speed (deg/s)')
    pdf_out.savefig()
    plt.close()
    
    nfit = np.polyfit(ymeans, nmeans, 1)
    nlsr = np.poly1d(nfit)
    plt.figure()
    plt.plot(ymeans,nmeans,'.k')
    plt.plot(xrange, nlsr(xrange), '-b')
    plt.xlabel('mean yaw rate (deg/s)')
    plt.ylabel('mean change in horizontal TP coordinate during pursuit (deg/s)')
    pdf_out.savefig()
    plt.close()
    
    pdf_out.close()
    
    print '\nspearman r: %f, p %f' % scipy.stats.spearmanr(ymeans, gmeans)
    print 'pearson r: %f, p %f' % scipy.stats.pearsonr(ymeans,gmeans)
    print 'least-squares fit: %f %f' % (fit[0], fit[1])
    print 'yaw/TP location least-squares fit: %f %f' % (nfit[0], nfit[1])