Beispiel #1
0
def maybe_get_ax(*args, **kwargs):
    """
    It used to be that the first argument of prettyplotlib had to be the 'ax'
    object, but that's not the case anymore.

    @param args:
    @type args:
    @param kwargs:
    @type kwargs:
    @return:
    @rtype:
    """

    if 'ax' in kwargs:
        ax = kwargs.pop('ax')
    elif len(args) == 0:
        fig = plt.gcf()
        ax = plt.gca()
    elif isinstance(args[0], mpl.axes.Axes):
        ax = args[0]
        args = args[1:]
    else:
        ax = plt.gca()
    return ax, args, dict(kwargs)
Beispiel #2
0
        print dist

    OU0 = K_ou(xs)
    OU1 = IterateOnce(OU0, K_ou, dx,alpha=0.1)
    dist = SquaredDistance(OU0,OU1)
    while dist > 1e-10:
        OU0 = OU1
        OU1 = IterateOnce(OU0, K_ou, dx,alpha =0.1)
        dist = SquaredDistance(OU0,OU1)
        print dist

    fig, (ax1,ax2,ax3) = ppl.subplots(3,1)
    #ax1 = plt.subplot(311)
    #ax2 = plt.subplot(312)
    #ax3 = plt.subplot(313)
    plt.gcf().suptitle("Posterior kernels")

    xs = xs[:6000]
    OU1 = OU1[:6000]
    matern1 = matern1[:6000]
    rbf1 = rbf1[:6000]

    ppl.plot(xs,K_ou(xs),label=r'OU prior kernel',ax=ax1)
    ppl.plot(xs,OU1,label = r'OU posterior kernel',ax=ax1)
    ppl.legend(ax1)
    ppl.plot(xs,K_matern(xs),label=r'Matern prior kernel',ax=ax2)
    ppl.plot(xs,matern1, label=r'Matern posterior kernel',ax=ax2)
    ppl.legend(ax2)
    ppl.plot(xs,K_rbf(xs),label=r'RBF prior kernel',ax=ax3)
    ppl.plot(xs,rbf1,label=r'RBF posterior kernel',ax=ax3)
    ppl.legend(ax3)
code.reset()

[mp,varp,spsp,sp,msep,parts,ws] = pf.particle_filter(code, env, timewindow=timewindow,
                                                     dt=dt, nparticles=nparticles,
                                                     mode='v', testf=(lambda x:x))
if gaussian:
    print "MSE of gaussian filter %f"% mseg
print "MSE of particle filter %f"% msep

if plotting:
    
    #matplotlib.rcParams['font.size']=10
    
    plt.close()    
    plt.figure()
    ax1 = plt.gcf().add_subplot(2,1,1)
    times = np.arange(0.0,dt*timewindow,dt)
    if gaussian:    
        ax1.plot(times,sg,'r',label='Signal')
        if sum(sum(spsg)) !=0:
            (ts,neurs) = np.where(spsg == 1)
            spiketimes = times[ts]
            thetas = [code.neurons[i].theta for i in neurs]
        else:
            spiketimes = []
            thetas = []
        
        ax1.plot(spiketimes,thetas,'yo',label='Spike times')
        ax1.plot(times,mg,'b',label='Mean prediction')
        ax1.set_title('Gaussian Filter')
        ax1.set_ylabel('Signal space')
Beispiel #4
0
env_rng.seed(env_seed)
code_rng.seed(code_seed)

env.reset(np.array([0.0]))
code.reset()
#[mp,varp,spsp,sp,msep,parts,ws] = pf.particle_filter(code,env,timewindow=timewindow,dt=dt,nparticles=nparticles,mode = 'v',testf = (lambda x:x))
[s,msep,spikecount,m,st,sptrain,sptimes] = pf.fast_particle_filter(code,env,timewindow=timewindow,dt=dt,nparticles=nparticles,mode = 'v',testf = (lambda x:x),dense=True)
if gaussian:
    print "MSE of gaussian filter %f"% mseg
print "MSE of particle filter %f"% msep


times = np.arange(0.0,dt*timewindow,dt)
plt.figure()

ax1 = plt.gcf().add_subplot(1,1,1)
ax1.plot(times,s,'r',label = 'True Sate')

#m = np.average(particles,weights=weights,axis=1)
#st = np.std(particles,weights=weights,axis=1)
#ext = (0.0,dt*timewindow,code.neurons[-1].theta,code.neurons[0].theta)
#plt.imshow(rates.T,extent=ext,cmap = cm.gist_yarg,aspect = 'auto',interpolation ='nearest')
thetas = [code.neurons[i].theta for i in sptrain]
ax1.plot(times[sptimes],thetas,'yo',label='Observed Spikes')
ax1.plot(times,m,'b',label='Posterior Mean')
ax1.plot(times,m-st,'gray',times,m+st,'gray')
#ax2 = plt.gcf().add_subplot(1,2,2)
#ax2.plot(times,s)
plt.xlabel('Time (in seconds)')
plt.ylabel('Space (in cm)')
plt.legend()
Beispiel #5
0
    ppl.plot(ts, Xcrit2[0,:,0], label=r'$\omega$ = 2.0', ax=axcrit)
    ppl.plot(ts, Xcrit4[0,:,0], label=r'$\omega$ = 4.0', ax=axcrit)
    p = ppl.legend(axcrit)
    fr = p.get_frame()
    fr.set_alpha(0.4)
    axcrit.set_title("Critical")

    A_over = numpy.array([[0.0,1.0],[-2.0,-5.0]])
    H_over = numpy.array([[0.0,0.0],[0.0,1.5]])
    ts,Xover2 = sample(X0,A_over, H_over, dt, N, 1)
    ts,Xover1 = sample(X0,numpy.array([[0.0,1.0],[-1.0,-2.5]]), 0.5*H_over, dt,N,1)
    ts,Xover4 = sample(X0,numpy.array([[0.0,1.0],[-4.0,-10.0]]), 2.0*H_over, dt,N,1)
    ppl.plot(ts, Xover1[0,:,0], label=r'$\omega$ = 1.0', ax=axover)
    ppl.plot(ts, Xover2[0,:,0], label=r'$\omega$ = 2.0', ax=axover)
    ppl.plot(ts, Xover4[0,:,0], label=r'$\omega$ = 4.0', ax=axover)
    p = ppl.legend(axover)
    fr = p.get_frame()
    fr.set_alpha(0.4)
    axover.set_title("Overdamped")

    axover.set_xlabel('Time [s]')
    axcrit.set_ylabel('Position [cm]')
    axou.set_ylabel('Position [cm]')
    axcrit.set_xlabel('Time [s]')

    plt.gcf().suptitle('Linear Stochastic Processes')

    plt.savefig('../figures/figure_5_2.eps')
    plt.savefig('../figures/figure_5_2.pdf')