def raw_pursuits_pdf(ci=[1], is_control=True):
    #ci = [3]
    #is_control = True
    txt = 'free' if is_control else 'tp'
    pdf_out = PdfPages('/tmp/raw_pursuits'+str(ci)+'_'+txt+'.pdf')
    
    data, segs = get_pooled(ci)
    data, segs = filter_by_treatment(data, segs, is_control)

    #print 'segs: ' + str(len(segs))
    
    grp, segs = zip(*segs)
    groups = np.rec.fromrecords(grp, names='session_id,lap')
    data = data[ data['g_direction_q'] > 0.4 ]

    for (sid, lap), d in groupby_multiple(data, ('session_id', 'lap')):
        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]

        plt.figure()
        
        ax1 = plt.subplot(211)
        plt.title(str(sid) + ', ' + str(lap))
        plt.plot(d['dist'], d['scenecam_x'], ',k')
        for s in seg:
            plt.plot([s.t0, s.t1], [s.d0[0], s.d1[0]], '-r')
        plt.ylim([-10,50])
        plt.ylabel('horiz.gaze (deg)')
        
        ax2 = plt.subplot(212, sharex=ax1)
        plt.plot(d['dist'], d['scenecam_y'], ',k')
        for s in seg:
            plt.plot([s.t0, s.t1], [s.d0[1], s.d1[1]], '-r')
        plt.ylim([-15,15])
        plt.ylabel('vert.gaze (deg)')
        plt.xlabel('ts')
        pdf_out.savefig()
        plt.close()

    pdf_out.close()
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