def plotTrack(MOVIE=True):
    plt.close()
    vp,ev,path=initVP(4,1)
    D=np.load(path+'trackPF.npy')
    count=np.load(path+'trackPFcount.npy')
    
    FFs=[]
    for vp in range(D.shape[0]): FFs.append([[],[],[]])
    Fs=[]
    for g in range(D.shape[2]):
        Fs=[];
        for vp in range(D.shape[0]):
            Fs.append([])
            temp=np.float32(np.max(count[vp,:,g,:],1))
            if not g:print 'vp=%d, nrags=%d, prop=%.3f'%(vp+1,2,temp[2]/temp[1:].sum())
            for h in range(1,D.shape[1]):
                denom=[0.003,0.15,0.05,0.05][h]
                temp=D[vp,h,g,:,:,:]/denom
                Fs[-1].append(temp)
                temp[temp>1]=1
                if h==2:FFs[vp][[0,2,1][g]]=temp       
        lbl=[]
        for i in range(4):lbl.append([FIG[6][2]+str(i+1),20,65+i*137,FIG[6][3]])
        for i in range(3):lbl.append([['1','2','>2'][i],20,-10,85+i*135])
        if MOVIE: plotGifGrid(Fs,fn=figpath+'Coord/'+['trackPFforw',
                    'trackPFback','trackPFmid'][g]+FMT,
                    bcgclr=1,plottime=True,text=lbl,P=129,F=85)
    lbl=[]
    for i in range(4):lbl.append([FIG[6][2]+str(i+1),20,65+i*137,FIG[6][3]])
    lbl.extend([[FIG[6][1][0],20,-10,40],[FIG[6][1][1],20,-10,190],[FIG[6][1][2],20,-10,330]])
    if MOVIE: plotGifGrid(FFs,fn=figpath+'Coord/trackPF'+FMT,bcgclr=1,
                forclr=0,plottime=True,P=129,F=85,text=lbl)
    figure(size=3,aspect=1)
    for g in range(D.shape[2]):
        for vp in range(4):
            d=D[vp,2,[0,2,1][g],60:68,:,:].mean(0)
            T=d.shape[1];P=d.shape[0]
            ax=subplot(4,3,g+3*vp+1)
            t=[np.linspace(-T*1000/85,0,T),
               np.linspace(-T/2*1000/85,T/2*1000/85,T),
               np.linspace(0,T*1000/85,T)][g]
            if not g:plt.ylabel(FIG[6][2]+str(vp+1),size=18)
            if not vp:plt.title(FIG[6][1][g],size=18)
            p=np.linspace(-5,5,P)
            plt.pcolor(p,t,d.T,cmap='gray',vmax=0.05)
            plt.xlim([p[0],p[-1]])
            #if g: ax.set_yticklabels([])
            if vp<3: ax.set_xticklabels([])
            if g==1:
                plt.ylim([-500,500])
                ax.set_yticks([-500,-250,0,250,500])
            elif g==0: ax.set_yticks([-1000,-750,-500,-250,0])
            else: ax.set_yticks([0,250,500,750,1000])
            ax.set_axisbelow(False);plt.grid(True)
            ax.set_xticks([-5,-2.5,0,2.5,5])
    plt.savefig(figpath+FIG[6][0],dpi=DPI,bbox_inches='tight')
def si2tex():
    from matustools.matusplotlib import ndarray2latextable
    res=[]
    for vp in range(1,5):
        vp,ev,path=initVP(vp,0)
        si=np.load(path+'si.npy')
        out=[]
        for ev in range(6):
            out.append(np.sum(si[:,14]==ev))
        out.append(np.sum(si[:,14]>ev))
        out.append(np.sum(si[:,13]==1))
        out.extend([out[1]/float(out[0])*100, 100*out[-1]/float(out[1])])
        res.append(out)
    ndarray2latextable(np.array(res),8*[0]+[1,1])
def plotEvStats():
    clrs=['#85dd7c','#bcc4c4','#FF9797']
    clrs=['#333333','#999999','#CCCCCC']
    plt.close()
    from Coord import initVP
    res=[]
    dat=np.zeros((3,30))
    figure(size=3,aspect=0.6)
    for vp in range(1,5):
        vp,ev,path=initVP(vp,0)
        si=np.load(path+'si.npy')
        out=[]
        for ev in range(6):
            out.append(np.sum(si[:,14]==ev))
        out.append(np.sum(si[:,14]>ev))
        out.append(np.sum(si[:,13]==1))
        out.extend([out[1]/float(out[0])*100, 100*out[-1]/float(out[1])])
        res.append(out)
        print np.max(si[:,14])
        for ev in range(dat.shape[1]):
            dat[0,ev]=np.logical_and(si[:,14]==ev,si[:,13]==1).sum()
            if ev: 
                dat[2,ev]=np.logical_and(si[:,14]==ev,si[:,13]==-1).sum()
                dat[1,ev]=(si[:,14]==ev).sum()-dat[0,ev]-dat[2,ev]
            else: 
                dat[1,ev]=(si[:,14]==1).sum()
                dat[2,ev]=(si[:,14]==ev).sum()-dat[1,ev]
                
            dat[:,ev]/=dat[:,ev].sum()
        subplot(2,2,vp);plt.grid(False)
        for ev in range(20):
            a=dat[0,ev];b=dat[0,ev]+dat[1,ev]
            plt.bar(ev,a,color=clrs[0],bottom=0,width=1)
            plt.bar(ev,b,color=clrs[1],bottom=a,width=1)
            plt.bar(ev,1,color=clrs[2],bottom=b,width=1)
        ax=plt.gca()
        #if vp<3:
        #ax.set_xticks(np.arange(0,20,1)+0.5)
        plt.xlabel(FIG[4][1])
        #ax.set_xticklabels(np.arange(0,20,1))
        #else: 
        ax.set_xticks([])
        ax.set_yticks([0,0.25,0.5,0.75,1])
        plt.ylim([0,1])
        for hh in [0.25,0.5,0.75]:plt.plot([0,20],[hh,hh],'gray',lw=0.5)
        plt.text(0.15,0.8,FIG[4][2]+str(vp),color='k')
    plt.subplots_adjust(wspace=0.01,hspace=-0.2)
    plt.savefig(figpath+FIG[4][0],dpi=DPI,bbox_inches='tight')
def plotVel():
    plt.close()
    figure(size=2,aspect=0.6)
    vp,ev,path=initVP(4,1)
    tv=np.load(path+'trackVel.npy')
    T=tv.shape[2]
    for hh in range(1):
        t=[np.linspace(0,T*1000/85,T), 
           np.linspace(-T/2*1000/85,T/2*1000/85,T),
           np.linspace(-T*1000/85,0,T)][hh]
        for vp in range(4):
            #plt.subplot(3,1,hh+1)
            plt.plot(t,85*tv[vp,[0,2,1][hh],:])
            plt.ylim([10,13])
    plt.xlabel(FIG[5][1][0])
    plt.ylabel(FIG[5][1][1])
    plt.legend(FIG[5][1][2],loc=0,ncol=4)
    print FIG[5][0]
    plt.savefig(figpath+FIG[5][0],dpi=DPI,bbox_inches='tight')