Exemplo n.º 1
0
def plotSvm(event=0,suf=''):
    ''' plots the grid search results '''
    print 'plotSvm started'
    plt.figure()
    infos=[]
    for vp in [1,2,3,4]:
        path,inpath,figpath=initPath(vp,event)
        fns=os.listdir(inpath+'svm%s/'%suf)
        fns=filter(lambda s: s.endswith('.log'),fns)
        dat=[]
        for fn in fns:
            f=open(inpath+'svm%s/'%suf+fn,'r')
            txt=f.read()
            f.close()
            txt='\n'+txt
            txt=txt.rsplit('%')
            for tx in txt[:-1]:
                lines=tx.rsplit('\n')
                #print len(lines),len(lines[0]),lines[0]
                b= float(lines[1].rsplit('=')[1])
                C= float(lines[2].rsplit('=')[1])
                f= float(lines[-1].rsplit('=')[1])/100.
                dat.append([b,C,f])
           
        dat=np.array(dat)
        betas=np.unique(dat[:,0]).tolist()
        Cs= np.unique(dat[:,1]).tolist()
        fun=np.zeros((len(betas),len(Cs)))#*np.nan
        for d in dat.tolist():
            fun[betas.index(d[0]),Cs.index(d[1])]=d[2]

        inc=(betas[1]-betas[0])
        betas.append(betas[-1]+inc)
        Cs.append(Cs[-1]+inc)
        betas=np.array(betas)-inc/2.;Cs=np.array(Cs)-inc/2.
        am= (fun==np.max(fun)).nonzero()
        iam=np.argmin(am[1])
        opt=[betas[am[0][iam]]+inc/2.,Cs[am[1][iam]]+inc/2.]

        # sanity check
        oi=np.logical_and(opt[0]==dat[:,0],opt[1]==dat[:,1])
        assert np.max(fun)==dat[oi.nonzero()[0][0],2]
        np.save(inpath+'svm%s/opt'%suf,opt)
        nf=getWeights(vp,event,suf)
        chnc=max(nf[0],nf[1])/float(nf[0]+nf[1])
        infos.append([vp,np.max(fun)*100,chnc*100]+nf.tolist()
            +[nf[2]/float(nf[0]+nf[1])*100,opt[1],opt[0]])
        plt.subplot(2,2,vp)
        plt.pcolor(betas,Cs,fun.T,cmap='hot')
        plt.xlabel('beta');plt.ylabel('C')
        plt.xlim([betas[0],betas[-1]]);plt.ylim([Cs[0],Cs[-1]])
        plt.colorbar()
        plt.plot(opt[0],opt[1],'rx',mew=2)
        plt.title('b=%.1f, C=%.1f,fm=%.2f,ch=%.2f'%(opt[0],opt[1],np.max(fun),chnc))
    plt.savefig(figpath+'svm%sfitEv%d.png'%(suf,event))
    from matustools.matusplotlib import ndarray2latextable
    ndarray2latextable(np.array(infos),decim=[0,2,2,0,0,0,2,1,1])
    return infos
Exemplo n.º 2
0
def tabSampleSize():
    res=[]
    for vp in range(1,5):
        res.append([])
        for ev in [97,0,1]:   
            path=inpath+'vp%03d/E%d/'%(vp,ev)
            sc=np.load(path+'X/score.npy')
            res[-1].append(sc.shape[0])
        ti=np.load(inpath+'vp%03d/'%vp+'ti.npy')
        res[-1].append(ti.shape[0])
    res=np.array(res)
    ndarray2latextable(res,decim=0)
Exemplo n.º 3
0
def railIdeal():
    T=68; P=64
    t=np.linspace(-0.8,0,T);p=np.linspace(-5,5,P)
    fn=inpath+'vp%03d/E%d/X/coeff.npy'%(999,1)
    pc=_getPC(np.load(fn),0)
    if pc.mean()>=0.4: pc=1-pc
    D= pc.T[:,31:33,:].mean(1)
    D/=D.sum();
    x0=np.array((3,-12,0.1,7,-12,0.1))
    xopt=fmin(func=_fun,x0=x0,args=(D,t,p,False)).tolist()
    xopt.append(abs(xopt[0]-xopt[3]))
    ndarray2latextable(np.array(xopt,ndmin=2),decim=2)
Exemplo n.º 4
0
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])
Exemplo n.º 5
0
def plotPC1rail():
    T=68;P=64
    t=np.linspace(-0.8,0.8,T);p=np.linspace(-5,5,P)
    tm=np.repeat(t[np.newaxis,:],P,axis=0)
    pm=np.repeat(p[:,np.newaxis],T,axis=1)
    fig=figure(size=3,aspect=0.7)
    fig.tight_layout()
    bnds=[(1,None),(None,0),(None,None)];est=[]
    for ev in [0,1]:
        for vp in range(1,5):
            fn=inpath+'vp%03d/E%d/'%(vp,ev)+'X/coeff.npy'
            pc=_getPC(np.load(fn),0)
            if pc.mean()>=0.4: pc=1-pc
            inc=0
            D= pc.T[:,(31+inc):(33+inc),:].mean(1)
            D/=D.sum();
            subplot(2,4,ev*4+vp)
            plt.pcolor(p,t,D.T,cmap='gray')
            # below we set the initial guess 
            x0=np.array((5,-12,0.2))
            xopt=fmin(func=_fun,x0=x0,args=(D,t,p,False))
            est.append(xopt.tolist())
            plt.plot(xopt[0]-xopt[1]*t,t,'r',lw=1,alpha=0.4)
            plt.grid(True,'both');
            plt.xlim([p[0],p[-1]]);plt.ylim([t[0],t[-1]]);
            ax=plt.gca();ax.set_axisbelow(False)
            ax.set_xticks([-4,-2,0,2,4])
            ax.set_yticks(np.linspace(-0.8,0.8,5))
            if not ev: ax.set_xticklabels([])
            if vp>1: ax.set_yticklabels([])
            else:
                ax.set_yticklabels(np.linspace(-0.8,0.8,5))
                plt.ylabel(['ES','CS1'][ev]+'\nTime to saccade in sec.')
            #if i==1: plt.text(2,-1,FIG[3][2],size=8)
            #if i==1: plt.xlabel(FIG[3][2])
            #else: plt.ylabel('subject %d'%(i+1))
            if not ev: plt.title(FIG[3][3]+str(vp))
    print FIG[2][1][0]
    plt.subplot(2,4,6);plt.text(-9,-1.15,FIG[2][1][0])
    plt.subplots_adjust(wspace=-1)
    plt.savefig(figpath+'Pixel'+os.path.sep+'pcSacRail',
                dpi=DPI,bbox_inches='tight')
    est=np.array(est)
    print est.ndim, est
    if est.ndim>2: est=np.squeeze(est)
    est[:,1]*=(t[-1]-t[0])/0.8
    print ndarray2latextable(est,decim=2)
    print np.corrcoef(est[:,1],est[:,2])[0,1]
Exemplo n.º 6
0
def tabLatent(ev,pcs=5):
    dat=[]
    if ev==1: vps=range(1,5)+[999]
    else: vps=range(1,5)
    for vp in vps:
        path=inpath+'vp%03d/E%d/X/'%(vp,ev)
        dat.append(np.load(path+'latent.npy')[:pcs]*100)
    return ndarray2latextable(np.array(dat),decim=1)
Exemplo n.º 7
0
def tabLatent(ev,pcs=5):
    '''pcs - number of principal components that will be displayed'''
    dat=[]
    if ev==1: vps=range(1,5)+[999]
    else: vps=range(1,5)
    for vp in vps:
        path=inpath+'vp%03d/E%d/X/'%(vp,ev)
        dat.append(np.load(path+'latent.npy')[:pcs]*100)
    return ndarray2latextable(np.array(dat),decim=1)
Exemplo n.º 8
0
def codingComparison():
    res=[];
    for vp in [2,1]:
        initVP(vpl=vp,evl=0)
        si1=np.load(path+'sicoder1.npy').tolist()
        si2=np.load(path+'sicoder2.npy').tolist()
        sel=[]
        for i in range(len(si1)):
            for j in range(i+1,len(si1)):
                if si1[i][0]==si1[j][0] and si1[i][-1]==si1[j][-1] and si1[i][-2]==si1[j][-2]:
                    sel.append(j)
        print sel
        j=-1
        for a in si1:
            j+=1
            if j in sel: continue
            for b in si2:
                if a[0]==b[0] and a[-1]==b[-1] and a[-2]==b[-2]: res.append([a[14],b[14]])
        print vp,len(res),len(si1),len(si2)
    def analyze(dat):
        tp=np.logical_and(dat[:,0],dat[:,1]).sum()
        fn=np.logical_and(~dat[:,0],dat[:,1]).sum()
        tn=np.logical_and(~dat[:,0],~dat[:,1]).sum()
        fp=np.logical_and(dat[:,0],~dat[:,1]).sum()
        acc=(tn+tp)/float(dat.shape[0])
        sens=tp/float(tp+fn)
        spec=tn/float(fp+tn)
        corr=(tp*tn-fp*fn)/np.sqrt((tp+fp)*(tp+fn)*(tn+fp)*(tn+fn))
        return [acc,sens,spec,corr]
    #np.save(path+'ress',res)
    out=[];res=np.array(res)
    for dat in [res>0,res==0,res==1]: out.append(analyze(dat))
    res=res[np.logical_or(res[:,1]==0,res[:,1]==1),:]
    out.append(analyze(res==1))
    np.save(path+'codingComparison',out)
    from matustools.matusplotlib import ndarray2latextable
    print ndarray2latextable(np.array(out))
Exemplo n.º 9
0
def plotBTpc(vpn=range(1,5),pcaEv=97):
    ''' vpn - list with subject ids
        pcaEv - id of the catch-up saccade, 0=exploration saccade,
            97 - 200 ms before button press'''
    #dat=plotBTavg(MOVIE=False)
    T=68#dat[0][0].shape[-1]
    P=64#dat[0][0].shape[0]
    t=np.linspace(-0.8,0,T);p=np.linspace(-5,5,P)
    tm=np.repeat(t[np.newaxis,:],P,axis=0)
    pm=np.repeat(p[:,np.newaxis],T,axis=1)
    rows=len(vpn)
    #cols=len(dat[0])
    fig=figure(size=2,aspect=0.75)
    fig.tight_layout()
    #m=[-251,-201,-151,-101,-51,-1]
    bnds=[(1,None),(None,0),(None,None)]
    est=[]
    for i in range(rows+2):
        #for k in [1]:
        #est.append([])
        #j=4# 200 ms 
        fn=inpath+'vp%03d/E%d/'%(vpn[max(0,i-2)],pcaEv)+'X/coeff.npy'
        if i==1: pc=(_getPC(np.load(fn),0)+_getPC(np.load(fn),1))/2.
        elif i==0: pc=(_getPC(np.load(fn),0)-_getPC(np.load(fn),1)+1)/2.
        else: pc=_getPC(np.load(fn),0)
        if pc.mean()>=0.4: pc=1-pc
        inc=[-2,2,0,0,0,0][i]
        D= pc.T[:,(31+inc):(33+inc),:].mean(1)
        D/=D.sum();
        subplot(2,3,i+1)
        plt.pcolor(p,t,D.T,cmap='gray')
        # below we set the initial guess 
        if i==0: x0=np.array((1,-12,0.3,-2,-12,0.1))
        elif i==1:x0=np.array((3,-12,0.1,0,-12,0.1))
        elif i==2: x0=np.array((0,-12,0.1))
        else: x0=np.array((3,-12,0.1,-2,-12,0.1))
        xopt=fmin(func=_fun,x0=x0,args=(D,t,p,False))
        est.append(xopt.tolist())
        plt.plot(xopt[0]-xopt[1]*t,t,'r',lw=1,alpha=0.4)
        if x0.size==6: plt.plot(xopt[3]-xopt[4]*t,t,'r',lw=1,alpha=0.4)
        elif x0.size==3:est[-1].extend([np.nan]*3)
        else: raise ValueError
        est[-1].append(abs(est[-1][0]-est[-1][3]))
        plt.grid(True,'both');
        plt.xlim([p[0],p[-1]]);plt.ylim([t[0],t[-1]]);
        ax=plt.gca();ax.set_axisbelow(False)
        ax.set_xticks([-4,-2,0,2,4])
        ax.set_yticks(np.linspace(-0.8,0,5))
        if i%3!=0: ax.set_yticklabels([])
        else:
            ax.set_yticklabels(np.linspace(-1,-0.2,5))
        if i<3: ax.set_xticklabels([])
        if i==0: plt.text(-9,-0.3,FIG[3][1],size=8,rotation='vertical')
        if i==4: plt.xlabel(FIG[3][2])
        #else: plt.ylabel('subject %d'%(i+1))
        #if i==0: plt.title(str(m[j]*2+2))
        plt.text(2.1,-0.75,FIG[3][3]+str(vpn[max(i-2,0)])+['a','b',''][min(i,2)],color='w')
    plt.subplots_adjust(wspace=-1)
    plt.savefig(figpath+FIG[3][0]+'%d'%pcaEv,
                dpi=DPI,bbox_inches='tight')
    est=np.array(est)
    #for k in [2,5]: est[:,k]=est[:,k]/np.sin(np.arctan(1/-est[:,k-1]))
    print est.shape
    if est.ndim>2: est=np.squeeze(est)
    print ndarray2latextable(est,decim=2)
Exemplo n.º 10
0
def plotBTavg(MAX=16,MOVIE=True):
    from matustools.matusplotlib import plotGifGrid
    dat=[];T=68;P=64;est=[]
    t=np.linspace(-0.8,0,T);p=np.linspace(-5,5,P)
    figure(size=3,aspect=1)
    for vp in range(1,5):
        dat.append([])
        for event in range(-6,0):
            fn=inpath+'vp%03d/E%d/'%(vp,100+event)+'PF/PF000.npy'
            d=np.squeeze(np.load(fn))
            #print np.max(d.mean(axis=0)),np.min(d.mean(axis=0))
            dat[-1].append(d.mean(axis=0)/float(MAX))

            inc=0#[-2,2,0,0,0,0][i+1]
            D= (d.mean(axis=0)/float(MAX))
            D=np.rollaxis(D,1)
            D=D[:,(31+inc):(33+inc),:].mean(1);D/=D.sum();
            j=vp-1; i=event+6
            subplot(6,4,i*4+j+1)
            plt.pcolor(p,t,D.T,cmap='gray')
            # below we set the initial guess
            if i==3:
                if vp==1: x0=np.array((0.5,-12,0.3))
                else: x0=np.array((3,-12,0.2,-0.5,-12,0.2))
                xopt=fmin(func=_fun,x0=x0,args=(D,t,p,False))
                est.append(xopt.tolist())
                plt.plot(xopt[0]-xopt[1]*t,t,'r',lw=1,alpha=0.4)
                if vp!=1: plt.plot(xopt[3]-xopt[4]*t,t,'r',lw=1,alpha=0.4)
                else:est[-1].extend([np.nan]*3)
            plt.grid(True,'both');
            plt.xlim([p[0],p[-1]]);plt.ylim([t[0],t[-1]]);
            ax=plt.gca();ax.set_axisbelow(False)
            ax.set_xticks([-4,-2,0,2,4])
            ytck=np.linspace(-0.8,0,5)[1:]
            ax.set_yticks(ytck)
            if j>0: ax.set_yticklabels([])
            else: ax.set_yticklabels(ytck-[0.5,0.4,0.3,0.2,0.1,0.05][i])
            #if i==1: plt.text(2,-1,FIG[3][2],size=8)
            if i<5: ax.set_xticklabels([])
            #else: plt.ylabel('subject %d'%(i+1))
            #if i==0: plt.title(str(m[j]*2+2))
            #
            if i==0: plt.title(FIG[3][3]+str(j+1))
    plt.subplot(6,4,5)
    plt.text(-9,-0.5,FIG[3][1],rotation='vertical')
    plt.subplot(6,4,22)
    plt.text(-2,-1.2,FIG[3][2])
    plt.subplots_adjust(wspace=-1)
    plt.savefig(figpath+FIG[3][0]+'avg',dpi=DPI,bbox_inches='tight')
    est=np.array(est)
    print est.ndim, est
    if est.ndim>2: est=np.squeeze(est)
    print ndarray2latextable(est,decim=2)
    if not MOVIE: return dat
    lbl=[]    
    #for i in range(4):lbl.append([FIG[6][1][3]+str(i+1),20,32+i*72,-15])
    for i in range(4):lbl.append([FIG[3][3]+str(i+1),20,32+i*72,FIG[3][4]])
    #for i in range(6):lbl.append([str([500,400,300,200,100,50][i]),20,-10,30+i*72])
    for i in range(6):lbl.append([str([500,400,300,200,100,50][i]),20,-10,FIG[3][5]+i*FIG[3][6]])
    plotGifGrid(dat,fn=figpath+'buttonPressMean'+FMT,bcgclr=1,
                text=lbl,plottime=True)
    return dat
Exemplo n.º 11
0
def plotBTpt(vpn=range(1,5),pcaEv=97):
    from scipy.optimize import fmin
    def fun(x,D=None,verbose=False):
        nrlines=len(x)/3; p1=np.nan;s=0.15
        if nrlines==1: p0,v0,s0=tuple(x)
        elif nrlines==2: p0,v0,s0,p1,v1,s1=tuple(x)
        else: raise ValueError
        out=np.ones((P,T))
        dist=np.abs(pm+v0*tm-p0)/np.sqrt(1+v0**2)/s0
        out=np.maximum(1-np.power(dist,3),0)/float(nrlines)
        if nrlines==2:
            dist=np.abs(pm+v1*tm-p1)/np.sqrt(1+v1**2)/s1
            out+=np.maximum(1-np.power(dist,3),0)/float(nrlines)
        out/=out.sum()
        if D is None: return out
        fout=-np.corrcoef(D.flatten(),out.flatten())[0,1]# np.linalg.norm(D-out)**2
        if verbose: print 'p0=%.2f, v=%.2f, p1=%.2f, s=%.2f, f=%f'%(p0,v,p1,s,fout)
        return fout
    #dat=plotBTmean()
    T=68#dat[0][0].shape[-1]
    P=64#dat[0][0].shape[0]
    t=np.linspace(-0.8,0,T);p=np.linspace(-5,5,P)
    tm=np.repeat(t[np.newaxis,:],P,axis=0)
    pm=np.repeat(p[:,np.newaxis],T,axis=1)
    rows=len(vpn)
    #cols=len(dat[0])
    fig=figure(size=3,aspect=0.35)
    fig.tight_layout()
    #m=[-251,-201,-151,-101,-51,-1]
    bnds=[(1,None),(None,0),(None,None)]
    est=[]
    for i in range(-1,rows):
        #for k in [1]:
        #est.append([])
        j=4# 200 ms 
        fn=inpath+'vp%03d/E%d/'%(vpn[max(0,i)],pcaEv)+'X/coeff.npy'
        if i==0: pc=(_getPC(np.load(fn),0)+_getPC(np.load(fn),1))/2.
        elif i==-1: pc=(_getPC(np.load(fn),0)-_getPC(np.load(fn),1)+1)/2.
        else: pc=_getPC(np.load(fn),0)
        if pc.mean()>=0.4: pc=1-pc
        inc=[-2,2,0,0,0,0][i+1]
        D= pc.T[:,(31+inc):(33+inc),:].mean(1)
        #else: D=dat[i][j][31:33,:,:].mean(0)
        D/=D.sum()
        #print i,k,D.shape,D.sum()
        
        subplot(1,5,i+2)
        plt.pcolor(p,t,D.T,cmap='gray')
        # below we set the initial guess 
        if vpn[i]==999: x0=np.array((3,-12,0.1,7,-12,0.1))
        elif vpn[max(0,i)]==1:
            if i==-1: x0=np.array((1,-12,0.3,-2,-12,0.1))
            else:x0=np.array((3,-12,0.1,0,-12,0.1))
        else: x0=np.array((3,-12,0.1,-2,-12,0.1))
        xopt=fmin(func=fun,x0=x0,args=(D,False))
        est.append(xopt.tolist())
        plt.plot(xopt[0]-xopt[1]*t,t,'r',lw=1,alpha=0.4)
        #if vpn[i]!=1:
        plt.plot(xopt[3]-xopt[4]*t,t,'r',lw=1,alpha=0.4)
        #else:est[-1].extend([np.nan]*3)
        plt.grid(True,'both');
        plt.xlim([p[0],p[-1]]);plt.ylim([t[0],t[-1]]);
        ax=plt.gca();ax.set_axisbelow(False)
        ax.set_xticks([-4,-2,0,2,4])
        ax.set_yticks(np.linspace(-0.8,0,5))
        if i in set((0,1,2,3)): ax.set_yticklabels([])
        else:
            ax.set_yticklabels(np.linspace(-1,-0.2,5))
            plt.ylabel(FIG[3][1])
        #if i==1: plt.text(2,-1,FIG[3][2],size=8)
        if i==1: plt.xlabel(FIG[3][2])
        #else: plt.ylabel('subject %d'%(i+1))
        #if i==0: plt.title(str(m[j]*2+2))
        plt.text(1,-0.75,FIG[3][3]+str(vpn[max(i,0)])+['a','b',''][min(i+1,2)],color='w')
    plt.subplots_adjust(wspace=-1)
    plt.savefig(figpath+FIG[3][0]+'%d'%pcaEv,
                dpi=DPI,bbox_inches='tight')
    est=np.array(est)
    print est.ndim, est
    if est.ndim>2: est=np.squeeze(est)
    print ndarray2latextable(est,decim=2)