def decode(self, t0, t1): oo = self ## each oo.pX_Nm[t0] = 1. / oo.Nx if oo.dbgMvt: oo.pX_Nm[t0, 20:30] = 151/5. A = _N.trapz(oo.pX_Nm[t0], dx=oo.dxp) oo.pX_Nm[t0] /= A oo.intgrd= _N.empty(oo.Nx) oo.intgrd2d= _N.empty((oo.Nx, oo.Nx)) oo.intgrl = _N.empty(oo.Nx) oo.xTrs = _N.zeros((oo.Nx, oo.Nx)) # Gaussian x = _N.linspace(-oo.xA, oo.xA, oo.Nx) ## (xk - a xk1) i = 0 if not oo.bWmaze: for x1 in x: j = 0 for x0 in x: # j is current position oo.xTrs[i, j] = _N.exp(-((x1-oo.Fp*x0)**2)/(2*oo.q2p)) j += 1 i += 1 else: grdsz = (12./oo.Nx) spdGrdUnts = _N.diff(oo.pos) / grdsz # unit speed ( per ms ) in grid units # avg. time it takes to move 1 grid is 1 / _N.mean(_N.abs(spdGrdUnts)) # p(i+1, i) = 1/<avg spdGrdUnts> p1 = _N.mean(_N.abs(spdGrdUnts))*1 # assume Nx is even #k2 = 0.02 k2 = 0.2 k3 = 0.1 for i in xrange(0, oo.Nx/2): oo.xTrs[i, i] = 1-p1 if i > 0: oo.xTrs[i-1, i] = p1 if i > 1: ## next nearest neighbor oo.xTrs[i-2, i] = p1*k2 oo.xTrs[i+1, i] = p1*k2*k3 elif i == 1: oo.xTrs[oo.Nx/2-1, 1] = p1*k2/2 oo.xTrs[oo.Nx/2, 1] = p1*k2/2 oo.xTrs[i+1, i] = p1*k2*k3 oo.xTrs[oo.Nx/2-1, 0] = p1/2 oo.xTrs[oo.Nx/2, 0] = p1/2 for i in xrange(oo.Nx/2, oo.Nx): oo.xTrs[i, i] = 1-p1 if i < oo.Nx - 1: oo.xTrs[i+1, i] = p1 if i < oo.Nx - 2: oo.xTrs[i-1, i] = p1*k2*k3 oo.xTrs[i+2, i] = p1*k2 elif i == oo.Nx-2: oo.xTrs[i-1, i] = p1*k2*k3 oo.xTrs[oo.Nx/2-1, oo.Nx-2] = p1*k2/2 oo.xTrs[oo.Nx/2, oo.Nx-2] = p1*k2/2 oo.xTrs[oo.Nx/2-1, oo.Nx-1] = p1/2 oo.xTrs[oo.Nx/2, oo.Nx-1] = p1/2 #oo.xTrs[:, j] += _N.mean(oo.xTrs[:, j])*0.01 for i in xrange(oo.Nx): A = _N.trapz(oo.xTrs[:, i])*((2.*oo.xA)/oo.Nx) oo.xTrs[:, i] /= A # keep in mind that k_{k-1} is not treated as a value with a correct answer. # integrate over all possible values of x_{k-1} # Need value of integrand for all x_{k-1} # I will perform integral L times for each time step # multiply integral with p(\Delta N_k, m_k | x_k) pNkmk0 = _N.exp(-oo.dt * oo.Lam_xk) # one for each tetrode pNkmk = _N.ones(oo.Nx) dims = _N.ones(oo.mdim, dtype=_N.int)*oo.Nm fxdMks = _N.empty((oo.Nx, oo.mdim+1)) # for each pos, a fixed mark fxdMks[:, 0] = oo.xp pNkmk = _N.empty((oo.Nx, oo.nTets)) tStart = _tm.time() if oo.kde: occ = 1./oo.iocc iBx2 = 1. / (oo.Bx * oo.Bx) sptl = [] for nt in xrange(oo.nTets): sptl.append(-0.5*iBx2*(oo.xpr - oo.tr_pos[nt])**2) # this piece doesn't need to be evalu if not oo.kde: for nt in xrange(oo.nTets): oo.mvNrm[nt].iSgs = _N.linalg.inv(oo.mvNrm[nt].covs) oo.mvNrm[nt].i2pidcovs = 1/_N.sqrt(2*_N.pi*_N.linalg.det(oo.mvNrm[nt].covs)) oo.mvNrm[nt].i2pidcovsr= oo.mvNrm[nt].i2pidcovs.reshape((oo.mvNrm[nt].M, 1)) for t in xrange(t0+1,t1): # start at 1 because initial condition #tt1 = _tm.time() for nt in xrange(oo.nTets): oo.Lklhd[nt, t] = pNkmk0[:, nt] # build likelihood if (oo.marks[t, nt] is not None) and (not oo.dbgMvt): nSpks = len(oo.marks[t, nt]) for ns in xrange(nSpks): fxdMks[:, 1:] = oo.marks[t, nt][ns] if oo.kde: #(atMark, fld_x, tr_pos, tr_mks, all_pos, mdim, Bx, cBm, bx) #_ku.kerFr(fxdMks[0, 1:], fxdMks[:, 0], oo.tr_pos, oo.tr_mks, oo.mvpos, oo.mdim, oo.Bx, oo.cBm, oo.bx) oo.Lklhd[nt, t] *= _ku.kerFr(fxdMks[0, 1:], sptl[nt], oo.tr_marks[nt], oo.mdim, oo.Bx, oo.Bm, oo.bx)* oo.iocc*oo.dt else: oo.Lklhd[nt, t] *= oo.mvNrm[nt].evalAtFxdMks_new(fxdMks)*oo.lmd0[nt] * oo.iocc * oo.dt ttt1 =0 ttt2 =0 ttt3 =0 #tt2 = _tm.time() # transition convolved with previous posterior _N.multiply(oo.xTrs, oo.pX_Nm[t-1], out=oo.intgrd2d) oo.intgrl = _N.trapz(oo.intgrd2d, dx=oo.dxp, axis=1) #for ixk in xrange(oo.Nx): # above trapz over 2D array # oo.intgrl[ixk] = _N.trapz(oo.intgrd2d[ixk], dx=oo.dxp) #tt3 = _tm.time() oo.pX_Nm[t] = oo.intgrl * _N.product(oo.Lklhd[:, t], axis=0) A = _N.trapz(oo.pX_Nm[t], dx=oo.dxp) oo.pX_Nm[t] /= A #tt4 = _tm.time() #print "%(1).3e %(2).3e %(3).3e" % {"1" : (tt2-tt1), "2" : (tt3-tt2), "3" : (tt4-tt3)} #print "%(1).3e %(2).3e" % {"1" : ttt1, "2" : ttt2} tEnd = _tm.time() print "decode %(1).3e" % {"1" : (tEnd-tStart)}
def decodeKDE(self, t0, t1): """ decode activity from [t0:t1] """ oo = self oo.init_pX_Nm(t0) # flat pX_Nm init cond at start of decode oo.LmdMargOvrMrks(0, t0) ## each # k_{k-1} is not treated as a value with a correct answer. # integrate over all possible values of x_{k-1} # Need value of integrand for all x_{k-1} # I will perform integral L times for each time step # multiply integral with p(\Delta N_k, m_k | x_k) pNkmk0 = _N.exp(-oo.dt * oo.Lam_MoMks) # one for each tetrode pNkmk = _N.ones(oo.Nx) fxdMks = _N.empty((oo.Nx, oo.mdim+1)) # fixed mark for each field pos. fxdMks[:, 0] = oo.xp pNkmk = _N.empty((oo.Nx, oo.nTets)) tStart = _tm.time() ibx2 = 1. / (oo.bx * oo.bx) sptl = [] dens = [] for nt in xrange(oo.nTets): sptl.append(-0.5*ibx2*(oo.xpr - oo.tr_pos[nt])**2) # this piece doesn't need to be evalu for t in xrange(t0+1,t1): # start at 1 because initial condition #tt1 = _tm.time() for nt in xrange(oo.nTets): oo.Lklhd[nt, t] = pNkmk0[:, nt] if (oo.mkpos[nt][t, 1] == 1): fxdMks[:, 1:] = oo.mkpos[nt][t, 2:] #(atMark, fld_x, tr_pos, tr_mks, all_pos, mdim, Bx, cBm, bx) #dens.append(_ku.kerFr(fxdMks[0, 1:], sptl[nt], oo.tr_marks[nt], oo.mdim, oo.Bx, oo.Bm, oo.bx, oo.dxp, oo.occ)) oo.Lklhd[nt, t] *= _ku.kerFr(fxdMks[0, 1:], sptl[nt], oo.tr_marks[nt], oo.mdim, oo.Bx, oo.Bm, oo.bx, oo.dxp, oo.occ) ttt1 =0 ttt2 =0 ttt3 =0 #tt2 = _tm.time() # transition convolved with previous posterior _N.multiply(oo.xTrs, oo.pX_Nm[t-1], out=oo.intgrd2d) oo.intgrl = _N.trapz(oo.intgrd2d, dx=oo.dxp, axis=1) #for ixk in xrange(oo.Nx): # above trapz over 2D array # oo.intgrl[ixk] = _N.trapz(oo.intgrd2d[ixk], dx=oo.dxp) #tt3 = _tm.time() oo.pX_Nm[t] = oo.intgrl * _N.product(oo.Lklhd[:, t], axis=0) A = _N.trapz(oo.pX_Nm[t], dx=oo.dxp) oo.pX_Nm[t] /= A #tt4 = _tm.time() #print "%(1).3e %(2).3e %(3).3e" % {"1" : (tt2-tt1), "2" : (tt3-tt2), "3" : (tt4-tt3)} #print "%(1).3e %(2).3e" % {"1" : ttt1, "2" : ttt2} tEnd = _tm.time() print "decode %(1).3e" % {"1" : (tEnd-tStart)}