Example #1
0
def getPhases(_mdn, offset=0):
    """
    what is the inferred phase when ground truth phase is 0
    """
    TR = _mdn.shape[0]
    ph = _N.array(_mdn)

    gk = gauKer(2)
    gk /= _N.sum(gk)

    mdn = _mdn
    itr = 0

    for tr in xrange(TR):
        itr += 1
        cv = _N.convolve(mdn[tr] - _N.mean(mdn[tr]), gk, mode="same")

        ht_mdn = _ssig.hilbert(cv)
        ph[tr] = (_N.arctan2(ht_mdn.imag, ht_mdn.real) + _N.pi) / (2 * _N.pi)
        if offset != 0:
            ph[tr] += offset
            inds = _N.where(ph[tr] >= 1)[0]
            ph[tr, inds] -= 1

    return ph
Example #2
0
def kernel_NGS(dat, SHUF=0, kerwin=3):
    _td = _rt.return_hnd_dat(dat)
    Tgame= _td.shape[0]
    cprobs = _N.zeros((3, 3, Tgame-1))

    stay_win, dn_win, up_win, stay_tie, dn_tie, up_tie, stay_los, dn_los, up_los, win_cond, tie_cond, los_cond  = _rt.get_ME_WTL(_td, 0, Tgame)

    gk = gauKer(kerwin)
    gk /= _N.sum(gk)
    all_cnd_tr = _N.zeros((3, 3, Tgame-1))
    ker_all_cnd_tr = _N.ones((3, 3, Tgame-1))*-100

    all_cnd_tr[0, 0, stay_win] = 1
    all_cnd_tr[0, 1, dn_win] = 1
    all_cnd_tr[0, 2, up_win] = 1
    all_cnd_tr[1, 0, stay_tie] = 1
    all_cnd_tr[1, 1, dn_tie] = 1
    all_cnd_tr[1, 2, up_tie] = 1
    all_cnd_tr[2, 0, stay_los] = 1
    all_cnd_tr[2, 1, dn_los] = 1
    all_cnd_tr[2, 2, up_los] = 1

    for iw in range(3):
        if iw == 0:
            cond = _N.sort(win_cond)
        elif iw == 1:
            cond = _N.sort(tie_cond)
        elif iw == 2:
            cond = _N.sort(los_cond)

        for it in range(3):
            print(all_cnd_tr[iw, it, cond])
            ker_all_cnd_tr[iw, it, cond] = _N.convolve(all_cnd_tr[iw, it, cond], gk, mode="same")
            for n in range(1, Tgame-1):
                if ker_all_cnd_tr[iw, it, n] == -100:
                    ker_all_cnd_tr[iw, it, n] = ker_all_cnd_tr[iw, it, n-1]
            n = 0
            while ker_all_cnd_tr[iw, it, n] == -100:
                n += 1
            ker_all_cnd_tr[iw, it, 0:n] = ker_all_cnd_tr[iw, it, n]

    for iw in range(3):
        for_cond = _N.sum(ker_all_cnd_tr[iw], axis=0)
        for it in range(3):
            print(ker_all_cnd_tr[iw, it].shape)
            ker_all_cnd_tr[iw, it] /= for_cond
    
    return ker_all_cnd_tr
Example #3
0
def initBernoulli(model, k, F0, TR, N, y, fSigMax, smpx, Bsmpx):  ############
    if model == "bernoulli":
        w = 5
        wf = gauKer(w)
        gk = _N.empty((TR, N + 1))
        fgk = _N.empty((TR, N + 1))
        for m in xrange(TR):
            gk[m] = _N.convolve(y[m], wf, mode="same")
            gk[m] = gk[m] - _N.mean(gk[m])
            gk[m] /= 5 * _N.std(gk[m])
            fgk[m] = bpFilt(15, 100, 1, 135, 500, gk[m])  #  we want
            fgk[m, :] /= 2 * _N.std(fgk[m, :])

            smpx[m, 2:, 0] = fgk[m, :]
            for n in xrange(2 + k - 1, N + 1 + 2):  # CREATE square smpx
                smpx[m, n, 1:] = smpx[m, n - k + 1:n, 0][::-1]
            for n in xrange(2 + k - 2, -1, -1):  # CREATE square smpx
                smpx[m, n, 0:k - 1] = smpx[m, n + 1, 1:k]
                smpx[m, n, k - 1] = _N.dot(F0, smpx[m, n:n + k,
                                                    k - 2])  # no noise

            Bsmpx[m, 0, :] = smpx[m, :, 0]
Example #4
0
def getGLMphases(TR, t0, t1, est, X, spkHist, dat, gkW=20, useRefr=True):
    params = _N.array(est.params)

    stT = spkHist.LHbin * (spkHist.nLHBins + 1
                           )  #  first stT spikes used for initial history
    ocifs = _N.empty(
        (spkHist.endTR - spkHist.startTR, spkHist.t1 - spkHist.t0 - stT))
    dt = 0.001

    ##

    sur = "refr"
    if not useRefr:
        params[spkHist.endTR:spkHist.endTR +
               spkHist.LHbin] = params[spkHist.endTR + spkHist.LHbin]
        sur = "NOrefr"

    for tr in xrange(spkHist.endTR - spkHist.startTR):
        ocifs[tr] = _N.exp(_N.dot(X[tr], params)) / dt

    gk = _flt.gauKer(gkW)
    gk /= _N.sum(gk)

    cglmAll = _N.zeros((TR, t1 - t0))

    for tr in xrange(spkHist.startTR, TR):  #  spkHist.statTR usually 0
        _gt = dat[stT:, tr * 3]
        gt = _N.convolve(_gt, gk, mode="same")
        gt /= _N.std(gt)

        glm = (ocifs[tr] - _N.mean(ocifs[tr])) / _N.std(ocifs[tr])
        cglm = _N.convolve(glm, gk, mode="same")
        cglm /= _N.std(cglm)

        cglmAll[tr, stT:] = cglm

    return stT, cglmAll
Example #5
0
def createModEnvelopes(TR,
                       N,
                       t0,
                       t1,
                       jitterTiming,
                       jitterLength,
                       gkW=0,
                       weak=0.1):
    """
    Generate a wave packet.  Amplitude envelope is trapezoidal
    """
    out = _N.empty((TR, N))

    if gkW > 0:
        gk = gauKer(gkW)

    AM = _N.empty(N)

    for tr in xrange(TR):
        w = int(((t1 - t0) + jitterLength * _N.random.randn()) / 2.)
        m = int(0.5 * (t1 + t0) + jitterTiming * _N.random.randn())

        if m - w > 0:
            AM[m - w:m + w] = 1
            AM[0:m - w] = weak
        else:
            AM[0:m + w] = 1

        AM[m + w:] = weak
        if gkW > 0:
            out[tr] = _N.convolve(AM, gk, mode="same")
        else:
            out[tr] = AM
        out[tr] /= _N.max(out[tr])

    return out
Example #6
0
def createOscPacket(f0,
                    f0VAR,
                    N,
                    dt0,
                    TR,
                    tM0,
                    tS0,
                    tJ=0,
                    Bf=[0.99],
                    amp=1,
                    stdf=None,
                    stda=None,
                    sig=0.1,
                    smoothKer=0):
    """
    Generate a wave packet.  Amplitude envelope is trapezoidal
    """
    out = _N.empty((TR, N))

    if stdf == None:
        x, y = createDataAR(100000, Bf, sig, sig)
        stdf = _N.std(
            x)  #  choice of 4 std devs to keep phase monotonically increasing

    trm = 100
    dts = _N.empty(N - 1)

    t = _N.empty(N)
    t[0] = 0

    gkC = gauKer(int(tS0 * 0.2))  #  slight envelope
    if smoothKer > 0:
        gk = gauKer(smoothKer)

    AM = _N.empty(N)

    for tr in xrange(TR):
        ph0 = _N.random.rand() * 2 * _N.pi

        tM = tM0 + int(tJ * _N.random.randn())
        tS = tS0 + int(tJ * _N.random.randn())
        bGood = False
        while not bGood:
            f, dum = createDataAR(N + trm, Bf, sig, sig, trim=trm)

            fn = 1 + (f / (3 * stdf)
                      )  #  going above 4 stdf very rare.  |xn| is almost < 1

            if _N.min(fn) > 0:
                bGood = True

        for i in xrange(1, N):
            dt = dt0 * fn[i]
            t[i] = t[i - 1] + dt
            dts[i - 1] = dt

        y = _N.sin(2 * _N.pi * (f0 + f0VAR[tr]) * t + ph0)

        t0 = tM - tS if (tM - tS > 0) else 0
        t1 = tM + tS if (tM + tS < N) else N
        AM[0:t0] = 0.15 + 0.03 * _N.random.randn(t0)
        AM[t0:t1] = 1 + 0.2 * _N.random.randn(t1 - t0)
        AM[t1:] = 0.15 + 0.03 * _N.random.randn(N - t1)
        AMC = _N.convolve(AM, gkC, mode="same")

        if smoothKer > 0:
            out[tr] = _N.convolve(y * AMC * amp, gk, mode="same")
        else:
            out[tr] = y * AMC * amp

    return out
Example #7
0
def _histPhase0_phaseInfrdAll(TR,
                              N,
                              x,
                              _mdn,
                              t0=None,
                              t1=None,
                              bRealDat=False,
                              trials=None,
                              fltPrms=None,
                              maxY=None,
                              yticks=None,
                              fn=None,
                              normed=False,
                              surrogates=1,
                              shftPhase=0,
                              color=None):
    """
    what is the inferred phase when ground truth phase is 0
    """
    pInfrdAt0 = []

    if (fltPrms is not None) and (not bRealDat):
        _fx = _N.empty((TR, N))
        for tr in xrange(TR):
            if len(fltPrms) == 2:
                _fx[tr] = lpFilt(fltPrms[0], fltPrms[1], 500, x[tr])
            elif len(fltPrms) == 4:
                # 20, 40, 10, 55 #(fpL, fpH, fsL, fsH, nyqf, y):
                _fx[tr] = bpFilt(fltPrms[0], fltPrms[1], fltPrms[2],
                                 fltPrms[3], 500, x[tr])
    else:
        _fx = x

    gk = gauKer(1)
    gk /= _N.sum(gk)

    if trials is None:
        trials = _N.arange(TR)
        TR = TR
    else:
        TR = len(trials)
        trials = _N.array(trials)

    #trials, TR = range(mARp.TR), mARp.TR if (trials is None) else trials, len(trials)

    nPh0s = _N.zeros(TR)
    t1 = t1 - t0  #  mdn already size t1-t0
    t0 = 0

    mdn = _mdn
    fx = _fx
    if _mdn.shape[0] != t1 - t0:
        mdn = _mdn[:, t0:t1]
    if _fx.shape[0] != t1 - t0:
        fx = _fx[:, t0:t1]

    itr = 0

    phs = []  #  phase 0 of inferred is at what phase of GT or LFP?
    cSpkPhs = []
    sSpkPhs = []

    for tr in trials:
        itr += 1
        cv = _N.convolve(mdn[tr, t0:t1] - _N.mean(mdn[tr, t0:t1]),
                         gk,
                         mode="same")

        ht_mdn = _ssig.hilbert(cv)
        ht_fx = _ssig.hilbert(fx[tr, t0:t1] - _N.mean(fx[tr, t0:t1]))
        ph_mdn = (_N.arctan2(ht_mdn.imag, ht_mdn.real) + _N.pi) / (2 * _N.pi)
        ph_mdn = _N.mod(ph_mdn + shftPhase, 1)
        ph_fx = (_N.arctan2(ht_fx.imag, ht_fx.real) + _N.pi) / (2 * _N.pi)
        ph_fx = _N.mod(ph_fx + shftPhase, 1)
        #  phase = 0 is somewhere in middle

        inds = _N.where((ph_mdn[0:t1 - t0 - 1] < 1)
                        & (ph_mdn[0:t1 - t0 - 1] > 0.5)
                        & (ph_mdn[1:t1 - t0] < 0.25))[0]
        cSpkPhs.append(_N.cos(2 * _N.pi * ph_fx[inds + t0]))
        sSpkPhs.append(_N.sin(2 * _N.pi * ph_fx[inds + t0]))
        phs.append(ph_fx[inds + t0])

        #for i in xrange(t0-t0, t1-t0-1):
        #    if (ph_mdn[i] < 1) and (ph_mdn[i] > 0.5) and (ph_mdn[i+1] < -0.5):
        #        pInfrdAt0.append(ph_fx[i]/2.)

    return figCircularDistribution(phs,
                                   cSpkPhs,
                                   sSpkPhs,
                                   trials,
                                   surrogates=surrogates,
                                   normed=normed,
                                   fn=fn,
                                   maxY=maxY,
                                   yticks=yticks,
                                   color=color,
                                   xlabel=xlabel)
Example #8
0
segN_mm[1] = _N.array([_N.min(lindist[seg2]), _N.max(lindist[seg2])])
seg3 = _N.where(tFilled == 3)[0]
segN_mm[2] = _N.array([_N.min(lindist[seg3]), _N.max(lindist[seg3])])
seg4 = _N.where(tFilled == 4)[0]
segN_mm[3] = _N.array([_N.min(lindist[seg4]), _N.max(lindist[seg4])])
seg5 = _N.where(tFilled == 5)[0]
segN_mm[4] = _N.array([_N.min(lindist[seg5]), _N.max(lindist[seg5])])

trans = _N.array([-1] + (_N.where(_N.diff(tFilled) != 0)[0]).tolist()) + 1

#  1, 2, 3, 4, 5
#  (seg)0, (seg)1       10, 11    20, 21   30, 31   40, 41   50, 51


#  10  outbound  11  inbound
gk = _flt.gauKer(20)
gk /= _N.sum(gk)
#  first, break it into inbound, outbound.
flindist = _N.convolve(lindist, gk, mode="same")

for isg in xrange(len(trans) - 1):
    tSeg0 = trans[isg]  #  time when we moved into this segment
    tSeg1 = trans[isg + 1]  #  time when we moved into this segment
    iseg = tFilled[tSeg0] - 1  #
    outb = tFilled[tSeg0] * 10
    inb = outb + 1
    av = _N.mean(segN_mm[iseg])
    if flindist[tSeg0] < av and flindist[tSeg1] > av:  # OUTBOUND
        tsFilled[tSeg0:tSeg1] = outb
    elif flindist[tSeg0] > av and flindist[tSeg1] < av:  # INBOUND
        tsFilled[tSeg0:tSeg1] = inb
Example #9
0
    def gibbs(self, ITERS, K, ep1=0, ep2=None, savePosterior=True, gtdiffusion=False, Mdbg=None, doSepHash=True, use_spc=True, nz_pth=0., ignoresilence=False, use_omp=False):
        """
        gtdiffusion:  use ground truth center of place field in calculating variance of center.  Meaning of diffPerMin different
        """
        print "gibbs"
        oo = self
        twpi     = 2*_N.pi
        pcklme   = {}

        ep2 = oo.epochs if (ep2 == None) else ep2
        oo.epochs = ep2-ep1

        ######################################  GRID for calculating
        ####  #  points in sum.  
        ####  #  points in uniform sampling of exp(x)p(x)   (non-spike interals)
        ####  #  points in sampling of f  for conditional posterior distribution
        ####  #  points in sampling of q2 for conditional posterior distribution
        ####  NSexp, Nupx, fss, q2ss

        #  numerical grid
        ux = _N.linspace(oo.xLo, oo.xHi, oo.Nupx, endpoint=False)   # uniform x position
        q2x    = _N.exp(_N.linspace(_N.log(1e-7), _N.log(100), oo.q2ss))  #  5 orders of
        d_q2x  = _N.diff(q2x)
        q2x_m1 = _N.array(q2x[0:-1])
        lq2x    = _N.log(q2x)
        iq2x    = 1./q2x
        q2xr     = q2x.reshape((oo.q2ss, 1))
        iq2xr     = 1./q2xr
        sqrt_2pi_q2x   = _N.sqrt(twpi*q2x)
        l_sqrt_2pi_q2x = _N.log(sqrt_2pi_q2x)

        freeClstr = None
        gk     = gauKer(100) # 0.1s  smoothing of motion
        gk     /= _N.sum(gk)
        xf     = _N.convolve(oo.dat[:, 0], gk, mode="same")
        oo.dat[:, 0] = xf + nz_pth*_N.random.randn(len(oo.dat[:, 0]))
        x      = oo.dat[:, 0]
        mks    = oo.dat[:, 2:]

        f_q2_rate = (oo.diffusePerMin**2)/60000.  #  unit of minutes  

        ######################################  PRECOMPUTED

        tau_l0 = oo.t_hlf_l0/_N.log(2)
        tau_q2 = oo.t_hlf_q2/_N.log(2)

        for epc in xrange(ep1, ep2):
            t0 = oo.intvs[epc]
            t1 = oo.intvs[epc+1]
            if epc > 0:
                tm1= oo.intvs[epc-1]
                #  0  10 30     20 - 5  = 15    0.5*((10+30) - (10+0)) = 15
                dt = 0.5*((t1+t0) - (t0+tm1))

            dt = (t1-t0)*0.5
            xt0t1 = _N.array(x[t0:t1])
            posbins  = _N.linspace(oo.xLo, oo.xHi, oo.Nupx+1)
            #  _N.sum(px)*(xbns[1]-xbns[0]) = 1
            px, xbns = _N.histogram(xt0t1, bins=posbins, normed=True)   

            Asts    = _N.where(oo.dat[t0:t1, 1] == 1)[0]   #  based at 0
            Ants    = _N.where(oo.dat[t0:t1, 1] == 0)[0]

            if epc == ep1:   ###  initialize
                labS, labH, lab, flatlabels, M, MF, hashthresh, nHSclusters = gAMxMu.initClusters(oo, K, x, mks, t0, t1, Asts, doSepHash=doSepHash, xLo=oo.xLo, xHi=oo.xHi, oneCluster=oo.oneCluster)  # nHSclusters  is # of clusters in hash and signal 

                signalClusters = _N.where(flatlabels < nHSclusters[0])[0]
                Mwowonz = M if not oo.nzclstr else M + 1
                #######   containers for GIBBS samples iterations
                smp_sp_prms = _N.zeros((3, ITERS, M))  
                smp_mk_prms = [_N.zeros((K, ITERS, M)), 
                               _N.zeros((K, K, ITERS, M))]
                smp_sp_hyps = _N.zeros((6, ITERS, M))
                smp_mk_hyps = [_N.zeros((K, ITERS, M)), 
                               _N.zeros((K, K, ITERS, M)),
                               _N.zeros((1, ITERS, M)), 
                               _N.zeros((K, K, ITERS, M))]
                oo.smp_sp_prms = smp_sp_prms
                oo.smp_mk_prms = smp_mk_prms
                oo.smp_sp_hyps = smp_sp_hyps
                oo.smp_mk_hyps = smp_mk_hyps

                if oo.nzclstr:
                    smp_nz_l0     = _N.zeros(ITERS)
                    smp_nz_hyps = _N.zeros((2, ITERS))

                #  list of freeClstrs
                freeClstr = _N.empty(M, dtype=_N.bool)   #  Actual cluster
                freeClstr[:] = False

                l0, f, q2, u, Sg = gAMxMu.declare_params(M, K, nzclstr=oo.nzclstr)   #  nzclstr not INITED, sized to include noise cluster if needed
                _l0_a, _l0_B, _f_u, _f_q2, _q2_a, _q2_B, _u_u, _u_Sg, _Sg_nu, \
                    _Sg_PSI = gAMxMu.declare_prior_hyp_params(M, MF, K, x, mks, Asts, t0)    #  hyper params don't include noise cluster
                gAMxMu.init_params_hyps(oo, M, MF, K, l0, f, q2, u, Sg, Asts, t0, x, mks, flatlabels, nzclstr=oo.nzclstr, signalClusters=signalClusters)

                ######  the hyperparameters for f, q2, u, Sg, l0 during Gibbs
                #  f_u_, f_q2_, q2_a_, q2_B_, u_u_, u_Sg_, Sg_nu, Sg_PSI_, l0_a_, l0_B_

                if oo.nzclstr:
                    nz_l0_intgrd   = _N.exp(-0.5*ux*ux / q2[Mwowonz-1])
                    _nz_l0_a       = 0.001
                    _nz_l0_B       = 0.1

            NSexp   = t1-t0    #  length of position data  #  # of no spike positions to sum
            xt0t1 = _N.array(x[t0:t1])

            nSpks    = len(Asts)
            gz   = _N.zeros((ITERS, nSpks, Mwowonz), dtype=_N.bool)
            oo.gz=gz
            print "spikes %d" % nSpks

            #dSilenceX = (NSexp/float(oo.Nupx))*(oo.xHi-oo.xLo)
            dSilenceX = NSexp*(xbns[1]-xbns[0])  # dx of histogram

            xAS  = x[Asts + t0]   #  position @ spikes
            mAS  = mks[Asts + t0]   #  position @ spikes
            xASr = xAS.reshape((1, nSpks))
            #mASr = mAS.reshape((nSpks, 1, K))
            mASr = mAS.reshape((1, nSpks, K))
            econt = _N.empty((Mwowonz, nSpks))
            rat   = _N.zeros((Mwowonz+1, nSpks))

            qdrMKS = _N.empty((Mwowonz, nSpks))
            ################################  GIBBS ITERS ITERS ITERS

            #  linalgerror
            #_iSg_Mu = _N.einsum("mjk,mk->mj", _N.linalg.inv(_u_Sg), _u_u)

            clusSz = _N.zeros(M, dtype=_N.int)

            _iu_Sg = _N.array(_u_Sg)

            for m in xrange(M):
                _iu_Sg[m] = _N.linalg.inv(_u_Sg[m])

            ttA = _tm.time()
            for iter in xrange(ITERS):
                iSg = _N.linalg.inv(Sg)
                if (iter % 5) == 0:    
                    print "iter  %d" % iter

                gAMxMu.stochasticAssignment(oo, iter, M, Mwowonz, K, l0, f, q2, u, Sg, _f_u, _u_u, Asts, t0, mASr, xASr, rat, econt, gz, qdrMKS, freeClstr, hashthresh, ((epc > 0) and (iter == 0)))

        #         ###############  FOR EACH CLUSTER

                for m in xrange(M):
                    minds = _N.where(gz[iter, :, m] == 1)[0]
                    sts  = Asts[minds] + t0
                    nSpksM   = len(sts)
                    clusSz[m] = nSpksM

                    ###############  CONDITIONAL l0

                    #  _ss.gamma.rvs.  uses k, theta  k is 1/B (B is our thing)
                    iiq2 = 1./q2[m]
                    # xI = (xt0t1-f[m])*(xt0t1-f[m])*0.5*iiq2
                    # BL  = (oo.dt/_N.sqrt(twpi*q2[m]))*_N.sum(_N.exp(-xI))

                    #  l0_intgrd   (M x Nupx)
                    l0_intgrd   = _N.exp(-0.5*(f[m] - ux)*(f[m]-ux) * iiq2)  
                    l0_exp_px   = _N.sum(l0_intgrd*px) * dSilenceX

                    BL  = (oo.dt/_N.sqrt(twpi*q2[m]))*l0_exp_px


                    #    #  keep mode same after discount
                    #  a' - 1 / B' = MODE  # mode is a - 1 / B
                    #  B' = (a' - 1) / MODE
                    #  discount a
                    #if (epc > 0) and oo.adapt and (_l0_a[m] > 1.1):
                    if (epc > 0) and oo.adapt:
                        _md_nd= _l0_a[m] / _l0_B[m]
                        _Dl0_a = _l0_a[m] * _N.exp(-dt/tau_l0)
                        _Dl0_B = _Dl0_a / _md_nd
                    else:
                        _Dl0_a = _l0_a[m]
                        _Dl0_B = _l0_B[m]

                    #  a'/B' = a/B
                    #  B' = (B/a)a'
                    aL  = nSpksM
                    l0_a_ = aL + _Dl0_a
                    l0_B_ = BL + _Dl0_B


                    # print "------------------"
                    # print "liklhd  BL   %(B).3f     f   %(f).3f   a %(a)d    B/a  %(ba).3f" % {"B" : BL, "f" : f[m], "ba" : (aL/ BL), "a" : aL}
                    # print "prior   BL   %(B).3f     f   %(f).3f   a %(a)d    B/a  %(ba).3f" % {"B" : l0_B_, "f" : f[m], "ba" : (l0_a_/ l0_B_), "a" : l0_a_}
                    # print (len(xt0t1)*oo.dt)
                    # print "******************"
                    
                    #print "%(1).5f   %(2).5f" % {"1" : l0_a_, "2" : l0_B_}

                    try:
                        l0[m] = _ss.gamma.rvs(l0_a_, scale=(1/l0_B_))  #  check
                    except ValueError:
                        print "fail"
                        print "M:        %d" % M
                        print "_l0_a[m]  %.3f" % _l0_a[m]
                        print "_l0_B[m]  %.3f" % _l0_B[m]
                        print "l0_a_     %.3f" % l0_a_
                        print "l0_B_     %.3f" % l0_B_
                        print "aL        %.3f" % aL
                        print "BL        %.3f" % BL
                        print "_Dl0_a    %.3f" % _Dl0_a
                        print "_Dl0_B    %.3f" % _Dl0_B
                        raise

                    ###  l0 / _N.sqrt(twpi*q2) is f*dt used in createData2
                    smp_sp_prms[oo.ky_p_l0, iter, m] = l0[m]
                    smp_sp_hyps[oo.ky_h_l0_a, iter, m] = l0_a_
                    smp_sp_hyps[oo.ky_h_l0_B, iter, m] = l0_B_
                    mcs = _N.empty((M, K))   # cluster sample means

                    if nSpksM >= K:
                        u_Sg_ = _N.linalg.inv(_iu_Sg[m] + nSpksM*iSg[m])
                        clstx    = mks[sts]

                        mcs[m]       = _N.mean(clstx, axis=0)
                        #u_u_ = _N.einsum("jk,k->j", u_Sg_, _N.dot(_N.linalg.inv(_u_Sg[m]), _u_u[m]) + nSpksM*_N.dot(iSg[m], mcs[m]))
                        #u_u_ = _N.einsum("jk,k->j", u_Sg_, _N.dot(_iu_Sg[m], _u_u[m]) + nSpksM*_N.dot(iSg[m], mcs[m]))
                        # hyp
                        ########  POSITION
                        ##  mean of posterior distribution of cluster means
                        #  sigma^2 and mu are the current Gibbs-sampled values

                        ##  mean of posterior distribution of cluster means
                    else:
                        u_Sg_ = _N.array(_u_Sg[m])

                        u_u_ = _N.array(_u_u[m])
                    u[m] = _N.random.multivariate_normal(u_u_, u_Sg_)

                    smp_mk_prms[oo.ky_p_u][:, iter, m] = u[m]
                    smp_mk_hyps[oo.ky_h_u_u][:, iter, m] = u_u_
                    smp_mk_hyps[oo.ky_h_u_Sg][:, :, iter, m] = u_Sg_

                    """
                    ############################################
                    """
                    ###############  CONDITIONAL f
                    #q2pr = _f_q2[m] if (_f_q2[m] > q2rate) else q2rate
                    if (epc > 0) and oo.adapt:
                        q2pr = _f_q2[m] + f_q2_rate * dt
                    else:
                        q2pr = _f_q2[m]
                    if nSpksM > 0:  #  spiking portion likelihood x prior
                        fs  = (1./nSpksM)*_N.sum(xt0t1[sts-t0])
                        fq2 = q2[m]/nSpksM
                        U   = (fs*q2pr + _f_u[m]*fq2) / (q2pr + fq2)
                        FQ2 = (q2pr*fq2) / (q2pr + fq2)
                    else:
                        U   = _f_u[m]
                        FQ2 = q2pr

                    FQ    = _N.sqrt(FQ2)
                    fx    = _N.linspace(U - FQ*15, U + FQ*15, oo.fss)

                    if use_spc:
                        fxr     = fx.reshape((oo.fss, 1))
                        fxrux = -0.5*(fxr-ux)*(fxr-ux)  # 
                        f_intgrd  = _N.exp((fxrux*iiq2))   #  integrand
                        f_exp_px = _N.sum(f_intgrd*px, axis=1) * dSilenceX
                        s = -(l0[m]*oo.dt/_N.sqrt(twpi*q2[m])) * f_exp_px  #  a function of x
                    else:
                        s = 0
                    funcf   = -0.5*((fx-U)*(fx-U))/FQ2 + s
                    funcf   -= _N.max(funcf)
                    condPosF= _N.exp(funcf)

                    norm    = 1./_N.sum(condPosF)
                    f_u_    = norm*_N.sum(fx*condPosF)
                    f_q2_   = norm*_N.sum(condPosF*(fx-f_u_)*(fx-f_u_))
                    f[m]    = _N.sqrt(f_q2_)*_N.random.randn() + f_u_
                    smp_sp_prms[oo.ky_p_f, iter, m] = f[m]
                    smp_sp_hyps[oo.ky_h_f_u, iter, m] = f_u_
                    smp_sp_hyps[oo.ky_h_f_q2, iter, m] = f_q2_

                    #ttc1g = _tm.time()
                    #############  VARIANCE, COVARIANCE
                    if nSpksM >= K:
                        ##  dof of posterior distribution of cluster covariance
                        Sg_nu_ = _Sg_nu[m, 0] + nSpksM
                        ##  dof of posterior distribution of cluster covariance
                        ur = u[m].reshape((1, K))
                        Sg_PSI_ = _Sg_PSI[m] + _N.dot((clstx - ur).T, (clstx-ur))
                        Sg[m] = s_u.sample_invwishart(Sg_PSI_, Sg_nu_)
                    else:
                        Sg_nu_ = _Sg_nu[m, 0] 
                        ##  dof of posterior distribution of cluster covariance
                        ur = u[m].reshape((1, K))
                        Sg_PSI_ = _Sg_PSI[m]
                        Sg[m] = s_u.sample_invwishart(Sg_PSI_, Sg_nu_)

                    ##############  SAMPLE COVARIANCES

                    ##  dof of posterior distribution of cluster covariance

                    smp_mk_prms[oo.ky_p_Sg][:, :, iter, m] = Sg[m]
                    smp_mk_hyps[oo.ky_h_Sg_nu][0, iter, m] = Sg_nu_
                    smp_mk_hyps[oo.ky_h_Sg_PSI][:, :, iter, m] = Sg_PSI_

                    # ###############  CONDITIONAL q2
                    #xI = (xt0t1-f)*(xt0t1-f)*0.5*iq2xr

                    if use_spc:
                        q2_intgrd = _N.exp(-0.5*(f[m] - ux)*(f[m]-ux) * iq2xr)
                        q2_exp_px = _N.sum(q2_intgrd*px, axis=1) * dSilenceX

                        # function of q2
                        s = -((l0[m]*oo.dt)/sqrt_2pi_q2x)*q2_exp_px
                    else:
                        s = 0
                    #  B' / (a' - 1) = MODE   #keep mode the same after discount
                    #  B' = MODE * (a' - 1)
                    if (epc > 0) and oo.adapt:
                        _md_nd= _q2_B[m] / (_q2_a[m] + 1)
                        _Dq2_a = _q2_a[m] * _N.exp(-dt/tau_q2)
                        _Dq2_B = _Dq2_a / _md_nd
                    else:
                        _Dq2_a = _q2_a[m]
                        _Dq2_B = _q2_B[m]

                    if nSpksM > 0:
                        ##  (1/sqrt(sg2))^S
                        ##  (1/x)^(S/2)   = (1/x)-(a+1)
                        ##  -S/2 = -a - 1     -a = -S/2 + 1    a = S/2-1
                        xI = (xt0t1[sts-t0]-f[m])*(xt0t1[sts-t0]-f[m])*0.5
                        SL_a = 0.5*nSpksM - 1   #  spiking part of likelihood
                        SL_B = _N.sum(xI)  #  spiking part of likelihood
                        #  spiking prior x prior
                        sLLkPr = -(_q2_a[m] + SL_a + 2)*lq2x - iq2x*(_q2_B[m] + SL_B)
                    else:
                        sLLkPr = -(_q2_a[m] + 1)*lq2x - iq2x*_q2_B[m]


                    sat = sLLkPr + s
                    sat -= _N.max(sat)
                    condPos = _N.exp(sat)
                    q2_a_, q2_B_ = ig_prmsUV(q2x, sLLkPr, s, d_q2x, q2x_m1, ITER=1, nSpksM=nSpksM, clstr=m, l0=l0[m])

                    # sat = sLLkPr + s
                    # sat -= _N.max(sat)
                    # condPos = _N.exp(sat)
                    # q2_a_, q2_B_ = ig_prmsUV(q2x, condPos, d_q2x, q2x_m1, ITER=1)
                    q2[m] = _ss.invgamma.rvs(q2_a_ + 1, scale=q2_B_)  #  check


                    #q2[m] = 1.1**2

                    #print ((1./nSpks)*_N.sum((xt0t1[sts]-f)*(xt0t1[sts]-f)))

                    if q2[m] < 0:
                        print "********  q2[%(m)d] = %(q2).3f" % {"m" : m, "q2" : q2[m]}

                    smp_sp_prms[oo.ky_p_q2, iter, m]   = q2[m]
                    smp_sp_hyps[oo.ky_h_q2_a, iter, m] = q2_a_
                    smp_sp_hyps[oo.ky_h_q2_B, iter, m] = q2_B_
                    
                    if q2[m] < 0:
                        print "^^^^^^^^  q2[%(m)d] = %(q2).3f" % {"m" : m, "q2" : q2[m]}
                        print q2[m]
                        print smp_sp_prms[oo.ky_p_q2, 0:iter+1, m]
                    iiq2 = 1./q2[m]

                    #ttc1h = _tm.time()
                    

                #  nz clstr.  fixed width
                if oo.nzclstr:
                    nz_l0_exp_px   = _N.sum(nz_l0_intgrd*px) * dSilenceX
                    BL  = (oo.dt/_N.sqrt(twpi*q2[Mwowonz-1]))*nz_l0_exp_px

                    minds = len(_N.where(gz[iter, :, Mwowonz-1] == 1)[0])
                    l0_a_ = minds + _nz_l0_a
                    l0_B_ = BL    + _nz_l0_B

                    l0[Mwowonz-1]  = _ss.gamma.rvs(l0_a_, scale=(1/l0_B_)) 
                    smp_nz_l0[iter]       = l0[Mwowonz-1]
                    smp_nz_hyps[0, iter]  = l0_a_
                    smp_nz_hyps[1, iter]  = l0_B_

            ttB = _tm.time()
            print (ttB-ttA)

            ###  THIS LEVEL:  Finished Gibbs iters for epoch
            gAMxMu.finish_epoch(oo, nSpks, epc, ITERS, gz, l0, f, q2, u, Sg, _f_u, _f_q2, _q2_a, _q2_B, _l0_a, _l0_B, _u_u, _u_Sg, _Sg_nu, _Sg_PSI, smp_sp_hyps, smp_sp_prms, smp_mk_hyps, smp_mk_prms, freeClstr, M, K)
            #  MAP of nzclstr
            if oo.nzclstr:
                frm = int(0.7*ITERS)
                _nz_l0_a              = _N.median(smp_nz_hyps[0, frm:])
                _nz_l0_B              = _N.median(smp_nz_hyps[1, frm:])
            pcklme["smp_sp_hyps"] = smp_sp_hyps
            pcklme["smp_mk_hyps"] = smp_mk_hyps
            pcklme["smp_sp_prms"] = smp_sp_prms
            pcklme["smp_mk_prms"] = smp_mk_prms
            pcklme["sp_prmPstMd"] = oo.sp_prmPstMd
            pcklme["mk_prmPstMd"] = oo.mk_prmPstMd
            pcklme["intvs"]       = oo.intvs
            pcklme["occ"]         = gz
            pcklme["nz_pth"]         = nz_pth
            pcklme["M"]           = M
            pcklme["Mwowonz"]           = Mwowonz
            if Mwowonz > M:  # or oo.nzclstr == True
                pcklme["smp_nz_l0"]  = smp_nz_l0
                pcklme["smp_nz_hyps"]= smp_nz_hyps
                
            dmp = open(resFN("posteriors_%d.dmp" % epc, dir=oo.outdir), "wb")
            pickle.dump(pcklme, dmp, -1)
            dmp.close()
Example #10
0
#frg      = "38-50"
#frg      = "30-45"
#frg      = "10-18"
#frg      = "25-30"
lags_sec=30
slideby_sec = slideby/300
lags = int(lags_sec / slideby_sec)+1  #  (slideby/300)*lags
xticksD = [-20, -10, 0, 10, 20]
time_lags = _N.linspace(-(slideby/300)*lags, lags*(slideby/300), 2*lags+1)
xticksD = [-20, -10, 0, 10, 20]

iexpt = -1

#time_lags = _N.linspace(-(slideby/300)*lags, lags*(slideby/300), 2*lags+1)

gk = gauKer(2)

pf_x = _N.arange(-15, 16)
all_x= _N.arange(-141, 142)

ch_w_CM, rm_chs, list_ch_names, ch_types = datconf.getConfig(datconf._RPS)
arr_ch_names = _N.array(list_ch_names)

for key in key_dats:
    iexpt += 1
    f12 = frg.split("-")
    f1 = f12[0]
    f2 = f12[1]
    allWFs   = []

    vs = "%(a)d%(g)d" % {"a" : armv_ver, "g" : gcoh_ver}
Example #11
0
    def setParams(self):
        oo = self
        # #generate initial values of parameters
        oo._d = _kfardat.KFARGauObsDat(oo.TR, oo.N, 1)
        oo._d.copyData(oo.y)

        #  baseFN_inter   baseFN_comps   baseFN_comps

        oo.smpx        = _N.zeros((oo.TR, oo.N + 1))   #  start at 0 + u
        oo.ws          = _N.empty((oo.TR, oo._d.N+1), dtype=_N.float)

        if oo.q20 is None:
            oo.q20         = 0.00077
        oo.q2          = _N.ones(oo.TR)*oo.q20

        oo.F0          = _N.array([0.9])
        ########  Limit the amplitude to something reasonable
        xE, nul = createDataAR(oo.N, oo.F0, oo.q20, 0.1)
        mlt  = _N.std(xE) / 0.5    #  we want amplitude around 0.5
        oo.q2          /= mlt*mlt
        xE, nul = createDataAR(oo.N, oo.F0, oo.q2[0], 0.1)

        w  =  5
        wf =  gauKer(w)
        gk = _N.empty((oo.TR, oo.N+1))
        fgk= _N.empty((oo.TR, oo.N+1))
        for m in xrange(oo.TR):
            gk[m] =  _N.convolve(oo.y[m], wf, mode="same")
            gk[m] =  gk[m] - _N.mean(gk[m])
            gk[m] /= 5*_N.std(gk[m])
            fgk[m] = bpFilt(15, 100, 1, 135, 500, gk[m])   #  we want
            fgk[m, :] /= 2*_N.std(fgk[m, :])

            if oo.noAR:
                oo.smpx[m, 0] = 0
            else:
                oo.smpx[m] = fgk[m]

        oo.s_lrn   = _N.empty((oo.TR, oo.N+1))
        oo.sprb   = _N.empty((oo.TR, oo.N+1))
        oo.lrn_scr1   = _N.empty(oo.N+1)
        oo.lrn_iscr1   = _N.empty(oo.N+1)
        oo.lrn_scr2   = _N.empty(oo.N+1)
        oo.lrn_scr3   = _N.empty(oo.N+1)
        oo.lrn_scld   = _N.empty(oo.N+1)

        if oo.bpsth:
            oo.B = patsy.bs(_N.linspace(0, (oo.t1 - oo.t0)*oo.dt, (oo.t1-oo.t0)), df=oo.dfPSTH, knots=oo.kntsPSTH, include_intercept=True)    #  spline basis

            if oo.dfPSTH is None:
                oo.dfPSTH = oo.B.shape[1] 
            oo.B = oo.B.T    #  My convention for beta

            if oo.aS is None:
                oo.aS = _N.linalg.solve(_N.dot(oo.B, oo.B.T), _N.dot(oo.B, _N.ones(oo.t1 - oo.t0)*0.01))   #  small amplitude psth at first
            oo.u_a            = _N.zeros(oo.dfPSTH)
        else:
            oo.B = patsy.bs(_N.linspace(0, (oo.t1 - oo.t0)*oo.dt, (oo.t1-oo.t0)), df=4, include_intercept=True)    #  spline basis

            oo.B = oo.B.T    #  My convention for beta
            oo.aS = _N.zeros(4)

            #oo.u_a            = _N.ones(oo.dfPSTH)*_N.mean(oo.us)
            oo.u_a            = _N.zeros(oo.dfPSTH)
Example #12
0
def finish_epoch(oo, nSpks, epc, ITERS, gz, l0, f, q2, u, Sg, _f_u, _f_q2, _q2_a, _q2_B, _l0_a, _l0_B, _u_u, _u_Sg, _Sg_nu, _Sg_PSI, smp_sp_hyps, smp_sp_prms, smp_mk_hyps, smp_mk_prms, freeClstr, M, K):
    #  finish epoch doesn't deal with noise cluster
    tt2 = _tm.time()

    gkMAP    = gauKer(2)
    frm   = int(0.7*ITERS)  #  have to test for stationarity

    if nSpks > 0:
        #  ITERS x nSpks x M   
        occ   = _N.mean(_N.mean(gz[frm:ITERS-1], axis=0), axis=0)

    oo.smp_sp_hyps = smp_sp_hyps
    oo.smp_sp_prms = smp_sp_prms
    oo.smp_mk_hyps = smp_mk_hyps
    oo.smp_mk_prms = smp_mk_prms

    l_trlsNearMAP = []
    MAPvalues2(epc, smp_sp_prms, oo.sp_prmPstMd, frm, ITERS, M, 3, occ, gkMAP, l_trlsNearMAP)
    l0[0:M]         = oo.sp_prmPstMd[epc, oo.ky_p_l0::3]
    f[0:M]          = oo.sp_prmPstMd[epc, oo.ky_p_f::3]
    q2[0:M]         = oo.sp_prmPstMd[epc, oo.ky_p_q2::3]
    MAPvalues2(epc, smp_sp_hyps, oo.sp_hypPstMd, frm, ITERS, M, 6, occ, gkMAP, None)
    _f_u[:]       = oo.sp_hypPstMd[epc, oo.ky_h_f_u::6]
    _f_q2[:]      = oo.sp_hypPstMd[epc, oo.ky_h_f_q2::6]
    _q2_a[:]      = oo.sp_hypPstMd[epc, oo.ky_h_q2_a::6]
    _q2_B[:]      = oo.sp_hypPstMd[epc, oo.ky_h_q2_B::6]
    _l0_a[:]      = oo.sp_hypPstMd[epc, oo.ky_h_l0_a::6]
    _l0_B[:]      = oo.sp_hypPstMd[epc, oo.ky_h_l0_B::6]

    #pcklme["cp%d" % epc] = _N.array(smp_sp_prms)
    #trlsNearMAP = _N.array(list(set(trlsNearMAP_D)))+frm   #  use these trials to pick out posterior params for MARK part

    #oo.mk_prmPstMd = [ epochs, M, K
    #                      epochs, M, K, K ]

    #oo.mk_hypPstMd  = [ epochs, M, K
    #                    epochs, M, K, K
    #                    epochs, M, 1
    #                    epochs, M, K, K

    #smp_mk_prms = [   K, ITERS, M
    #                  K, K, ITERS, M
    #smp_mk_hyps = [   K, ITERS, M
    #                  K, K, ITERS, M
    #                  1, ITERS, M
    #                  K, K, ITERS, M

    ##  params and hyper parms for mark

    for m in xrange(M):
        MAPtrls = l_trlsNearMAP[m]
        if len(MAPtrls) == 0:  #  none of them.  causes nan in mean
            MAPtrls = _N.arange(frm, ITERS, 10)
        #print MAPtrls
        u[m] = _N.median(smp_mk_prms[0][:, frm:, m], axis=1)

        Sg[m] = _N.mean(smp_mk_prms[1][:, :, frm:, m], axis=2)
        oo.mk_prmPstMd[oo.ky_p_u][epc, m] = u[m]
        oo.mk_prmPstMd[oo.ky_p_Sg][epc, m]= Sg[m]
        _u_u[m]    = _N.mean(smp_mk_hyps[oo.ky_h_u_u][:, frm:, m], axis=1)
        _u_Sg[m]   = _N.mean(smp_mk_hyps[oo.ky_h_u_Sg][:, :, frm:, m], axis=2)

        _Sg_nu[m]  = _N.mean(smp_mk_hyps[oo.ky_h_Sg_nu][0, frm:, m], axis=0)
        _Sg_PSI[m] = _N.mean(smp_mk_hyps[oo.ky_h_Sg_PSI][:, :, frm:, m], axis=2)
        oo.mk_hypPstMd[oo.ky_h_u_u][epc, m]   = _u_u[m]
        oo.mk_hypPstMd[oo.ky_h_u_Sg][epc, m]  = _u_Sg[m]
        oo.mk_hypPstMd[oo.ky_h_Sg_nu][epc, m] = _Sg_nu[m]
        oo.mk_hypPstMd[oo.ky_h_Sg_PSI][epc, m]= _Sg_PSI[m]
        #print _u_Sg[m]

    u[0:M]         = oo.mk_prmPstMd[oo.ky_p_u][epc]
    Sg[0:M]        = oo.mk_prmPstMd[oo.ky_p_Sg][epc]

    ###  hack here.  If we don't reset the prior for 
    ###  what happens when a cluster is unused?
    ###  l0 -> 0, and at the same time, the variance increases.
    ###  the prior then gets pushed to large values, but
    ###  then it becomes difficult to bring it back to small
    ###  values once that cluster becomes used again.  So
    ###  we would like unused clusters to have l0->0, but keep the
    ###  variance small.  That's why we will reset a cluster

    sq25  = 5*_N.sqrt(q2)

    if M > 1:
        occ = _N.mean(_N.sum(gz[frm:], axis=1), axis=0)  # avg. # of marks assigned to this cluster
        socc = _N.sort(occ)
        minAss = (0.5*(socc[-2]+socc[-1])*0.01)  #  if we're 100 times smaller than the average of the top 2, let's consider it empty

    if oo.resetClus and (M > 1):
        for m in xrange(M):
            #  Sg and q2 are treated differently.  Even if no spikes are
            #  observed, q2 is updated, while Sg is not.  
            #  This is because NO spikes in physical space AND trajectory
            #  information contains information about the place field.
            #  However, in mark space, not observing any marks tells you
            #  nothing about the mark distribution.  That is why f, q2
            #  are updated when there are no spikes, but u and Sg are not.

            if q2[m] < 0:
                print "????????????????"
                print q2
                print "q2[%(m)d] = %(q2).3f" % {"m" : m, "q2" : q2[m]}
                print smp_sp_prms[0, :, m]
                print smp_sp_prms[1, :, m]
                print smp_sp_prms[2, :, m]
                print smp_sp_hyps[4, :, m]
                print smp_sp_hyps[5, :, m]
            if ((occ[m] < minAss) and (l0[m] / _N.sqrt(twpi*q2[m]) < 1)) or \
                                  (f[m] < oo.xLo-sq25[m]) or \
                                  (f[m] > oo.xHi+sq25[m]):
                print "resetting  cluster %(m)d   %(l0).3f  %(f).3f" % {"m" : m, "l0" : (l0[m] / _N.sqrt(twpi*q2[m])), "f" : f[m]}

                _q2_a[m] = 1e-4
                _q2_B[m] = 1e-3
                _f_q2[m] = 4
                _u_Sg[m] = _N.identity(K)*9
                _l0_a[m] = 1e-4
                freeClstr[m] = True
            else:
                freeClstr[m] = False


    rsmp_sp_prms = smp_sp_prms.swapaxes(1, 0).reshape(ITERS, 3*M, order="F")

    _N.savetxt(resFN("posParams_%d.dat" % epc, dir=oo.outdir), rsmp_sp_prms, fmt=("%.4f %.4f %.4f " * M))   #  the params for the non-noise
Example #13
0
landmarks = _N.empty((6, 2))
Nsgs      = 5
segs      = _N.empty((Nsgs, 2, 2))  
length    = _N.empty(Nsgs)
offset    = _N.array([0, 1, 2, 1, 2])

minINOUT  = 100

#  regular lindist     #  0 to 3
#  lin_inout           #  inbound outbound  0 to 6
#  lin_lr              #  -3 to 3
#  lin_lr_inout        #  -3 to 3

ii     = 0

gkRWD  = gauKer(5)
gkRWD  /= _N.sum(gkRWD)

anim1     = None
anim2     = None
day    = None
ep     = None
r      = None


seg_ts = None
inout  = None   # inbound - outbound
a_inout  = None   # inbound - outbound
lr       = None

fspd     = None
Example #14
0
def createFlucOsc(f0,
                  f0VAR,
                  N,
                  dt0,
                  TR,
                  Bf=[0.99],
                  Ba=[0.99],
                  amp=1,
                  amp_nz=0,
                  stdf=None,
                  stda=None,
                  sig=0.1,
                  smoothKer=0,
                  dSF=5,
                  dSA=5):
    """
    AR as a generative model creates oscillations where amplitude is 
    sometimes small - to the point of calling it an oscillation is not quite 
    dSA, dSF    modulations created like (AR / (dSX x stddev)).
    """
    out = _N.empty((TR, N))

    if stdf == None:
        x, y = createDataAR(100000, Bf, sig, sig)
        stdf = _N.std(
            x)  #  choice of 4 std devs to keep phase monotonically increasing
    if stda == None:
        x, y = createDataAR(100000, Ba, sig, sig)
        stda = _N.std(
            x)  #  choice of 4 std devs to keep phase monotonically increasing

    trm = 500
    dts = _N.empty(N - 1)

    t = _N.empty(N)
    t[0] = 0

    if smoothKer > 0:
        gk = gauKer(smoothKer)
    for tr in xrange(TR):
        ph0 = _N.random.rand() * 2 * _N.pi
        bGood = False
        while not bGood:
            f, dum = createDataAR(N + trm, Bf, sig, sig, trim=trm)

            fn = 1 + (f / (dSF * stdf)
                      )  #  going above 4 stdf very rare.  |xn| is almost < 1

            bGood = True
            lt0s = _N.where(fn < 0)
            print "fn < 0 at %d places" % len(lt0s[0])

            #if _N.min(fn) > 0:
            #    bGood = True

        for i in xrange(1, N):
            dt = dt0 * fn[i]
            t[i] = t[i - 1] + dt
            dts[i - 1] = dt

        y = _N.sin(2 * _N.pi * (f0 + f0VAR[tr]) * t + ph0)

        bGood = False
        while not bGood:
            a, dum = createDataAR(N + trm, Ba, sig, sig, trim=trm)
            an = a / (dSA * stda)  #  in limit of large N, std(xn) = 1

            AM = 1 + an  #  if we get fluctuations 2 stds bigger,
            bGood = True
            lt0s = _N.where(AM < 0)
            print "AM < 0 at %d places" % len(lt0s[0])
            #if _N.min(AM) > 0:
            #    bGood = True

        if smoothKer > 0:
            out[tr] = _N.convolve(y * AM * (amp + _N.random.randn() * amp_nz),
                                  gk,
                                  mode="same")
        else:
            out[tr] = y * AM * amp

    return out
Example #15
0
def done():
    """
    come here after 6 landmarks chosen
    """

    global r, seg_ts, segs, Nsgs, inout, a_inout, lindist, lin_lr, lin_inout, lin_lr_inout, lr, raw_lindist
    global scxMin, scxMax, scyMin, scyMax
    global an, day, ep

    hdir = _N.empty(2)
    vdir = _N.empty(2)
    linp = _N.empty(2)
    """
    L5       L0       L3
    ||       ||       ||
    ||       ||       ||
    5        1        3
    ||       ||       ||
    ||       ||       ||
    L4===4===L1===2===L2
    """
    scxMin, scxMax, scyMin, scyMax = get_boundaries(r)
    segs_from_landmarks(segs, landmarks, length)
    e = inout_dir(segs, Nsgs)
    a_s, b_s, c_s = slopes_of_segs(segs)

    _plt.plot([segs[0, 0, 0], segs[0, 1, 0]], [segs[0, 0, 1], segs[0, 1, 1]],
              lw=3,
              color="black")
    _plt.plot([segs[1, 0, 0], segs[1, 1, 0]], [segs[1, 0, 1], segs[1, 1, 1]],
              lw=3,
              color="black")
    _plt.plot([segs[2, 0, 0], segs[2, 1, 0]], [segs[2, 0, 1], segs[2, 1, 1]],
              lw=3,
              color="black")
    _plt.plot([segs[3, 0, 0], segs[3, 1, 0]], [segs[3, 0, 1], segs[3, 1, 1]],
              lw=3,
              color="black")
    _plt.plot([segs[4, 0, 0], segs[4, 1, 0]], [segs[4, 0, 1], segs[4, 1, 1]],
              lw=3,
              color="black")

    segsr = segs.reshape((10, 2))

    clrs = ["blue", "orange", "red", "green", "yellow", "black", "brown"]
    fillin_unobsvd(r)

    N = r.shape[0]
    seg_ts = _N.empty(N, dtype=_N.int)
    lindist = _N.empty(N)
    lin_lr = _N.empty(N)
    lin_inout = _N.empty(N)
    lin_lr_inout = _N.empty(N)
    lr = _N.ones(N, dtype=_N.int) * -3

    inout = _N.empty(N, dtype=_N.int)
    a_inout = _N.empty(N)
    gk = gauKer(30)
    gk /= _N.sum(gk)
    fx = _N.convolve(0.5 * (r[:, 1] + r[:, 3]), gk, mode="same")
    fy = _N.convolve(0.5 * (r[:, 2] + r[:, 4]), gk, mode="same")
    xp = fx
    yp = fy
    xpyp = _N.empty((N, 2))
    xpyp[:, 0] = xp
    xpyp[:, 1] = yp

    _xpyp = _N.repeat(xpyp, Nsgs * 2, axis=0)
    rxpyp = _xpyp.reshape((N, Nsgs * 2, 2))

    dv = segsr - rxpyp
    dists = _N.sum(dv * dv, axis=2)  # closest point on maze from field points
    rdists = dists.reshape((N, Nsgs, 2))
    print rdists.shape

    online = _N.empty(Nsgs, dtype=bool)
    mins = _N.empty(Nsgs)

    for n in xrange(N):
        x0 = xpyp[n, 0]
        y0 = xpyp[n, 1]
        #  xcs, ycs: pt on all line segs closest to x0, y0 (may b byond endpts)
        xcs = (b_s *
               (b_s * x0 - a_s * y0) - a_s * c_s) / (a_s * a_s + b_s * b_s)
        ycs = (-a_s *
               (b_s * x0 - a_s * y0) - b_s * c_s) / (a_s * a_s + b_s * b_s)

        find_clsest(n, x0, y0, segs, rdists, seg_ts, Nsgs, online, offset, xcs,
                    ycs, mins, linp)

    # fig = _plt.figure()
    # _plt.plot(seg_ts)
    # clean_seg_ts(seg_ts)

    # _plt.plot(seg_ts)

    raw_lindist = _N.zeros(N)
    lindist_x0y0(N, xpyp, segs, rdists, seg_ts, Nsgs, online, offset, a_s, b_s,
                 c_s, mins, linp, raw_lindist)

    smooth_lindist(raw_lindist, lindist)

    # fig = _plt.figure(figsize=(10, 4))
    # _plt.plot(lindist)
    # gk = gauKer(8)   #  don't want to make this too large.  if we just pass  through the choice point, we can miss it.
    # gk /= _N.sum(gk)
    # flindist = _N.convolve(lindist, gk, mode="same")
    # lindist  = flindist

    # rm_lindist_jumps(N, lindist, seg_ts)
    fig = _plt.figure(figsize=(10, 4))
    _plt.plot(lindist)

    spd_thr = 0.35
    a_inout_x0y0(N, a_inout, inout, r, seg_ts, spd_thr, e)
    #_plt.plot([x0, x0], [y0, y0], ms=10, marker=".", color=clr)

    make_lin_inout(N, lindist, inout, lin_inout)
    make_lin_lr(N, lr, lindist, seg_ts, r)
    build_lin_lr_inout(N, lin_lr_inout, lindist, lr, inout, gkRWD)

    #  inout
    cp_lr, cp_inout = cpify_LR_inout(lr, inout)

    sday = ("0%d" % day) if (day < 10) else ("%d" % day)
    fn = _edd.datFN("lindist.dat",
                    dir="linearize/%(an)s%(dy)s0%(ep)d" % {
                        "dy": sday,
                        "ep": (ep + 1),
                        "an": anim2
                    },
                    create=True)
    _N.savetxt(fn, lindist, fmt="%.3f")
    fn = _edd.datFN("cp_lr.dat",
                    dir="linearize/%(an)s%(dy)s0%(ep)d" % {
                        "dy": sday,
                        "ep": (ep + 1),
                        "an": anim2
                    })
    _U.savetxtWCom(
        fn,
        cp_lr,
        fmt="%d %d",
        com=
        ("# N=%d.  1st column time, 2nd column  - inout value from this time until time in next row"
         % N))
    fn = _edd.datFN("cp_inout.dat",
                    dir="linearize/%(an)s%(dy)s0%(ep)d" % {
                        "dy": sday,
                        "ep": (ep + 1),
                        "an": anim2
                    })
    _U.savetxtWCom(
        fn,
        cp_inout,
        fmt="%d %d",
        com=
        ("# N=%d.  1st column time, 2nd column  - inout value from this time until time in next row"
         % N))
    fn = _edd.datFN("lin_lr_inout.dat",
                    dir="linearize/%(an)s%(dy)s0%(ep)d" % {
                        "dy": sday,
                        "ep": (ep + 1),
                        "an": anim2
                    })
    _N.savetxt(fn, lin_lr_inout, fmt="%.3f")
    """
    """
    t0 = 0
    winsz = 1000
    t1 = 0
    iw = -1
    while t1 < N:
        iw += 1
        t0 = iw * winsz
        t1 = (iw + 1) * winsz if (iw + 1) * winsz < N else N - 1
        #btwnfigs(anim2, day, ep, t0, t1, inout, [-1.1, 1.1], seg_ts+1, [0.9, 5.1], r, 1, 2, scxMin, scxMax, scyMin, scyMax)
        btwnfigs(anim2, day, ep, t0, t1, inout, "INOUT", [-1.1, 1.1], lr, "LR",
                 [-1.1, 1.1], lin_lr_inout, "lin_lr_inout", [-6.1, 6.1], r, 1,
                 2, scxMin, scxMax, scyMin, scyMax)
Example #16
0
def suggestPSTHKnots(dt, TR, N, bindat, bnsz=10, psth_knts=10, psth_run=False):
    """
    bnsz   binsize used to calculate approximate PSTH
    """
    rszd = False
    if N % bnsz != 0:
        rszd = True
        pcs = _N.ceil(N / bnsz)
        bnsz = int(_N.floor(N / pcs))

    spkts = _U.fromBinDat(bindat, SpkTs=True)

    #  apsth needs to be same size as N.  ie N%bnsz needs to be 0
    h, bs = _N.histogram(spkts, bins=_N.linspace(0, N, (N // bnsz) + 1))

    fs = (h / (TR * bnsz * dt))
    _apsth = _N.repeat(fs, bnsz)  #    piecewise boxy approximate PSTH
    if rszd:
        apsth = _N.zeros(N)
        apsth[0:(N // bnsz) * bnsz] = _apsth
        apsth[(N // bnsz) * bnsz:] = apsth[(N // bnsz) * bnsz - 1]
    else:
        apsth = _apsth

    apsth *= dt
    gk = gauKer(5)
    gk /= _N.sum(gk)
    f_apsth = _N.convolve(apsth, gk, mode="same")
    dpsth_pctl = _N.cumsum(_N.abs(_N.diff(f_apsth)))
    dpsth_pctl /= dpsth_pctl[-1]
    dpsth_pctl[0] = 0

    ITERS = 40
    x = _N.linspace(0., N - 1, N, endpoint=False)  # in units of ms.
    r2s = _N.empty(ITERS)
    best_r2s = _N.zeros(5)

    for iknts in range(5, 6):
        allKnts = _N.empty((ITERS, iknts))
        allCoeffs = []

        tAvg = 1. / iknts
        tsMin = tAvg * 0.5
        tsMax = tAvg * 1.5

        for it in range(ITERS):
            knt_inds = _N.zeros(iknts + 1)
            bGood = False
            while not bGood:
                try:
                    #pieces  = tsMin + _N.random.rand(iknts+1)*(tsMax-tsMin)
                    rnd_pctls = _N.sort(_N.random.rand(iknts + 1))

                    #pieces  = tsMin + _N.random.rand(iknts+1)*(tsMax-tsMin)
                    for i in range(iknts + 1):
                        iHere = _N.where((rnd_pctls[i] >= dpsth_pctl[0:-1])
                                         & (rnd_pctls[i] < dpsth_pctl[1:]))[0]
                        knt_inds[i] = iHere[0]

                    # knts    = _N.empty(iknts+1)

                    # knts[0] = pieces[0]
                    # for i in range(1, iknts+1):
                    #     knts[i] = knts[i-1] + pieces[i]
                    # knts /= knts[-1]
                    # knts[0:-1] *= N
                    #knts  = _N.sort((0.1 + 0.85*_N.random.rand(iknts))*N)
                    B = patsy.bs(x,
                                 knots=(knt_inds[0:-1]),
                                 include_intercept=True)
                    iBTB = _N.linalg.inv(_N.dot(B.T, B))
                    bGood = True
                except _N.linalg.linalg.LinAlgError:
                    print("Linalg Error or Value Error in suggestPSTHKnots")
                except ValueError:
                    print("Linalg Error or Value Error in suggestPSTHKnots")

            #a     = _N.dot(iBTB, _N.dot(B.T, _N.log(apsth)))
            a = _N.dot(iBTB, _N.dot(B.T, apsth))
            #ft    = _N.exp(_N.dot(B, a))
            ft = _N.dot(B, a)
            r2s[it] = _N.dot(ft - apsth, ft - apsth)
            allKnts[it, :] = knt_inds[0:-1]
            allCoeffs.append(a)

        mnIt = _N.where(r2s == r2s.min())[0][0]
        best_r2s[iknts - 10] = r2s[mnIt]
    knts = allKnts[mnIt]
    cfs = allCoeffs[mnIt]
    B = patsy.bs(x, knots=knts, include_intercept=True)

    if psth_run:
        fig = _plt.figure()
        _plt.plot(_N.dot(B, cfs))
        _plt.plot(apsth)

    return knts, apsth, cfs
rpsm_key = rpsms.rpsm_eeg_as_key[dat]
armv_ver = 1
gcoh_ver = 3

manual_cluster = False

Fs = 300
win, slideby = _ppv.get_win_slideby(gcoh_ver)

t_offset = 0  #  ms offset behv_sig_ts
stop_early = 0  #180
ev_n = 0

gk_std = 1
gk = gauKer(gk_std)
gk /= _N.sum(gk)
show_shuffled = False
rvrs = False

process_keyval_args(globals(), sys.argv[1:])
######################################################3

srvrs = "_revrsd" if rvrs else ""
sshf = "_sh" if show_shuffled else ""
rpsmdir = getResultFN(dat)
pikdir = getResultFN("%(dir)s/v%(av)d%(gv)d" % {
    "dir": dat,
    "av": armv_ver,
    "gv": gcoh_ver
})
Example #18
0
def compare(mARp, est, X, spkHist, oscMn, dat, gkW=20, useRefr=True):
    dt = 0.001

    gk = _flt.gauKer(gkW)
    gk /= _N.sum(gk)

    TR = oscMn.shape[0]

    #  params, stT
    params = _N.array(est.params)

    stT = spkHist.LHbin * (spkHist.nLHBins + 1
                           )  #  first stT spikes used for initial history
    ocifs = _N.empty(
        (spkHist.endTR - spkHist.startTR, spkHist.t1 - spkHist.t0 - stT))
    ##

    sur = "refr"
    if not useRefr:
        params[spkHist.endTR:spkHist.endTR +
               spkHist.LHbin] = params[spkHist.endTR + spkHist.LHbin]
        sur = "NOrefr"

    for tr in xrange(spkHist.endTR - spkHist.startTR):
        ocifs[tr] = _N.exp(_N.dot(X[tr], params)) / dt

    cglmAll = _N.zeros((TR, mARp.N + 1))
    infrdAll = _N.zeros((TR, mARp.N + 1))
    xt = _N.arange(stT, mARp.N + 1)

    for tr in xrange(spkHist.startTR, TR):
        _gt = dat[stT:, tr * 3]
        gt = _N.convolve(_gt, gk, mode="same")
        gt /= _N.std(gt)

        glm = (ocifs[tr] - _N.mean(ocifs[tr])) / _N.std(ocifs[tr])
        cglm = _N.convolve(glm, gk, mode="same")
        cglm /= _N.std(cglm)

        infrd = oscMn[tr, stT:] / _N.std(oscMn[tr, stT:])
        infrd /= _N.std(infrd)

        pc1, pv1 = _ss.pearsonr(glm, gt)
        pc1c, pv1c = _ss.pearsonr(cglm, gt)
        pc2, pv2 = _ss.pearsonr(infrd, gt)

        cglmAll[tr, stT:] = cglm
        infrdAll[tr, stT:] = infrd

        fig = _plt.figure(figsize=(12, 4))
        ax = fig.add_subplot(1, 1, 1)
        _plt.plot(xt, infrd, color=myC.infrdM, lw=2)
        _plt.plot(xt, cglm, color=myC.infrdM, lw=2., ls="--")
        #_plt.plot(xt, glm, color=myC.infrdM, lw=2., ls="-.")
        _plt.plot(xt, gt, color=myC.grndTruth, lw=4)

        MINx = _N.min(infrd)
        MAXx = _N.max(infrd)

        AMP = MAXx - MINx
        ht = 0.08 * AMP
        ys1 = MINx - 0.5 * ht
        ys2 = MINx - 3 * ht

        for n in xrange(stT, mARp.N + 1):
            if mARp.y[tr, n] == 1:
                _plt.plot([n, n], [ys1, ys2], lw=2.5, color="black")
        _plt.ylim(ys2 - 0.05 * AMP, MAXx + 0.05 * AMP)

        _plt.xlim(stT, mARp.N + 1)
        mF.arbitraryAxes(ax,
                         axesVis=[False, False, False, False],
                         xtpos="bottom",
                         ytpos="none")
        mF.setLabelTicks(_plt,
                         yticks=[],
                         yticksDsp=None,
                         xlabel="time (ms)",
                         ylabel=None,
                         xtickFntSz=24,
                         xlabFntSz=26)
        fig.subplots_adjust(left=0.05, right=0.95, bottom=0.2, top=0.85)
        _plt.savefig("cmpGLMAR_%(ur)s_%(tr)d.eps" % {"tr": tr, "ur": sur})
        _plt.close()

        corrs[tr] = pc1, pc1c, pc2
    mF.histPhase0_phaseInfrd(mARp,
                             cglmAll,
                             t0=stT,
                             t1=(mARp.N + 1),
                             bRealDat=False,
                             normed=True,
                             maxY=1.8,
                             fn="smthdGLMPhaseGLM%s" % sur)
    mF.histPhase0_phaseInfrd(mARp,
                             infrdAll,
                             t0=stT,
                             t1=(mARp.N + 1),
                             bRealDat=False,
                             normed=True,
                             maxY=1.8,
                             fn="smthdGLMPhaseInfrd")

    print _N.mean(corrs[:, 0])
    print _N.mean(corrs[:, 1])
    print _N.mean(corrs[:, 2])

    fig = _plt.figure(figsize=(8, 3.5))
    ax = fig.add_subplot(1, 2, 1)
    _plt.hist(corrs[:, 1],
              bins=_N.linspace(-0.5,
                               max(corrs[:, 2]) * 1.05, 30),
              color=myC.hist1)
    mF.bottomLeftAxes(ax)
    ax = fig.add_subplot(1, 2, 2)
    _plt.hist(corrs[:, 2],
              bins=_N.linspace(-0.5,
                               max(corrs[:, 2]) * 1.05, 30),
              color=myC.hist1)
    mF.bottomLeftAxes(ax)
    fig.subplots_adjust(left=0.05,
                        bottom=0.1,
                        right=0.95,
                        top=0.88,
                        wspace=0.2,
                        hspace=0.2)

    _plt.savefig("cmpGLMAR_hist")
    _plt.close()
Example #19
0
    print("...............   %s" % datetm)
    flip_human_AI = False

    #fig = _plt.figure(figsize=(11, 11))

    for cov in [_AIconst._WTL]:  #, _AIconst._HUMRPS, _AIconst._AIRPS]:
        scov = _AIconst.sCOV[cov]
        sran = ""

        SHUFFLES = 1
        a_s = _N.zeros((len(datetms), SHUFFLES + 1))
        acs = _N.zeros((len(datetms), SHUFFLES + 1, 61))

        if gk_w > 0:
            gk = gauKer(gk_w)
            gk /= _N.sum(gk)
        sFlip = "_flip" if flip_human_AI else ""

        label = "%(wins)d%(gkw)d" % {"wins": wins, "gkw": gk_w}

        out_dir = getResultFN("%(dfn)s" % {"dfn": datetm})

        if not os.access(out_dir, os.F_OK):
            os.mkdir(out_dir)
        out_dir = getResultFN("%(dfn)s/%(lbl)s" % {
            "dfn": datetm,
            "lbl": label
        })
        if not os.access(out_dir, os.F_OK):
            os.mkdir(out_dir)
Example #20
0
def compareWF(mARp,
              ests,
              Xs,
              spkHists,
              oscMn,
              dat,
              gkW=20,
              useRefr=True,
              dspW=None):
    """
    instead of subplots, plot 3 different things with 3 largely separated y-values
    """

    glmsets = len(ests)  #  horrible hack
    TR = oscMn.shape[0]
    infrdAll = _N.zeros((TR, mARp.N + 1))
    dt = 0.001
    gk = _flt.gauKer(gkW)
    gk /= _N.sum(gk)

    paramss = []
    ocifss = []
    stTs = []

    for gs in xrange(glmsets):  #  for the 2 glm conditions
        params = _N.array(ests[gs].params)
        X = Xs[gs]
        spkHist = spkHists[gs]

        stTs.append(spkHist.LHbin *
                    (spkHist.nLHBins +
                     1))  #  first stT spikes used for initial history
        ocifss.append(
            _N.empty((spkHist.endTR - spkHist.startTR,
                      spkHist.t1 - spkHist.t0 - stTs[gs])))

        for tr in xrange(spkHist.endTR - spkHist.startTR):
            ocifss[gs][tr] = _N.exp(_N.dot(X[tr], params)) / dt

    stT = min(stTs)

    #cglmAll = _N.zeros((TR, mARp.N+1))

    xt = _N.arange(stT, mARp.N + 1)
    xts = [_N.arange(stTs[0], mARp.N + 1), _N.arange(stTs[1], mARp.N + 1)]
    lss = [":", "-"]
    lws = [3.8, 2]
    cls = [myC.infrdM]
    for tr in xrange(spkHist.startTR, TR):
        _gt = dat[stT:, tr * 3]
        gt = _N.convolve(_gt, gk, mode="same")
        gt /= _N.std(gt)

        infrd = oscMn[tr, stT:] / _N.std(oscMn[tr, stT:])
        infrd /= _N.std(infrd)
        infrdAll[tr, stT:] = infrd

        fig = _plt.figure(figsize=(12, 8))
        ax = fig.add_subplot(1, 1, 1)
        _plt.plot(xt, gt, color=myC.grndTruth, lw=4)
        #_plt.plot(xt, infrd, color="brown", lw=4)

        up1 = _N.max(gt) - _N.min(gt)
        ##  mirror
        _plt.plot(xt, gt + up1 * 1.25, color=myC.grndTruth, lw=4)
        _plt.plot(xt, infrd + up1 * 1.25, color=myC.infrdM, lw=2)

        for gs in xrange(glmsets):
            ocifs = ocifss[gs]
            glm = (ocifs[tr] - _N.mean(ocifs[tr])) / _N.std(ocifs[tr])
            cglm = _N.convolve(glm, gk, mode="same")
            cglm /= _N.std(cglm)

            _plt.plot(xts[gs], cglm, color=myC.infrdM, lw=lws[gs], ls=lss[gs])

        MINx = _N.min(infrd)
        #MAXx = _N.max(infrd)
        MAXx = _N.max(gt) + up1 * 1.35

        AMP = MAXx - MINx
        ht = 0.08 * AMP
        ys1 = MINx - 0.5 * ht
        ys2 = MINx - 3 * ht

        for n in xrange(stT, mARp.N + 1):
            if mARp.y[tr, n] == 1:
                _plt.plot([n, n], [ys1, ys2], lw=2.5, color="black")
        _plt.ylim(ys2 - 0.05 * AMP, MAXx + 0.05 * AMP)

        if dspW is None:
            _plt.xlim(stT, mARp.N + 1)
        else:
            _plt.xlim(dspW[0], dspW[1])
        mF.arbitraryAxes(ax,
                         axesVis=[False, False, False, False],
                         xtpos="bottom",
                         ytpos="none")
        mF.setLabelTicks(_plt,
                         yticks=[],
                         yticksDsp=None,
                         xlabel="time (ms)",
                         ylabel=None,
                         xtickFntSz=24,
                         xlabFntSz=26)
        fig.subplots_adjust(left=0.05, right=0.95, bottom=0.2, top=0.85)
        _plt.savefig("cmpGLMAR_%(tr)d.eps" % {"tr": tr}, transparent=True)
        _plt.close()
Example #21
0
def histPhase0_phaseInfrd(mARp,
                          _mdn,
                          t0=None,
                          t1=None,
                          bRealDat=False,
                          trials=None,
                          filtParams=None,
                          maxY=None,
                          yticks=None,
                          fn=None,
                          normed=False):
    #  what is the inferred phase when ground truth phase is 0
    pInfrdAt0 = []

    #if (filtParams is not None) and (not bRealDat):
    if not bRealDat:
        _fx = _N.empty((mARp.TR, mARp.N + 1))
        for tr in xrange(mARp.TR):
            _fx[tr] = lpFilt(30, 40, 1000, mARp.x[tr])
    else:
        _fx = mARp.x

    if bRealDat:
        _fx = mARp.fx
    gk = gauKer(1)
    gk /= _N.sum(gk)

    if trials is None:
        trials = range(mARp.TR)
        TR = mARp.TR
    else:
        TR = len(trials)
    nPh0s = _N.zeros(TR)
    #t1    = t1-t0   #  mdn already size t1-t0
    #t0    = 0

    mdn = _mdn
    fx = _fx
    #if _mdn.shape[0] != t1 - t0:
    #    mdn = _mdn[:, t0:t1]
    #if _fx.shape[0] != t1 - t0:
    #    fx = _fx[:, t0:t1]

    itr = 0

    for tr in trials:
        itr += 1
        cv = _N.convolve(mdn[tr, t0:t1] - _N.mean(mdn[tr, t0:t1]),
                         gk,
                         mode="same")
        #cv = mdn[tr, t0:t1] - _N.mean(mdn[tr, t0:t1])

        ht_mdn = _ssig.hilbert(cv)
        #ht_fx   = _ssig.hilbert(fx[tr, t0:t1] - _N.mean(fx[tr, t0:t1]))
        ht_fx = _ssig.hilbert(fx[tr, t0:t1] - _N.mean(fx[tr, t0:t1]))
        ph_mdn = _N.arctan2(ht_mdn.imag, ht_mdn.real) / _N.pi
        ph_fx = ((_N.arctan2(ht_fx.imag, ht_fx.real) / _N.pi) + 1)

        #  phase = 0 is somewhere in middle

        for i in xrange(t0 - t0, t1 - t0 - 1):
            if (ph_mdn[i] < 1) and (ph_mdn[i] > 0.5) and (ph_mdn[i + 1] <
                                                          -0.5):
                nPh0s[itr - 1] += 1
                pInfrdAt0.append(ph_fx[i] / 2.)
                pInfrdAt0.append((ph_fx[i] + 2) / 2.)

    bgFnt = 22
    smFnt = 20

    fig, ax = _plt.subplots(figsize=(6, 4.2))
    pInfrdAt0A = _N.array(pInfrdAt0[::2])  #  0 to 2
    Npts = len(pInfrdAt0A)
    R2 = (1. / (Npts * Npts)) * (_N.sum(_N.cos(2 * _N.pi * pInfrdAt0A))**2 +
                                 _N.sum(_N.sin(2 * _N.pi * pInfrdAt0A))**2)
    _plt.hist(pInfrdAt0,
              bins=_N.linspace(0, 2, 41),
              color=mC.hist1,
              edgecolor=mC.hist1,
              normed=normed)
    print "maxY!!!  %f" % maxY
    if (maxY is not None):
        _plt.ylim(0, maxY)

    _plt.xlabel("phase", fontsize=bgFnt)
    _plt.ylabel("frequency", fontsize=bgFnt)
    _plt.xticks(fontsize=smFnt)
    if yticks is not None:
        _plt.yticks(yticks)
    _plt.yticks(fontsize=smFnt)
    if yticks is not None:
        _plt.yticks(yticks)
    if normed:
        _plt.yticks([0.25, 0.5, 0.75, 1], ["0.25", "0.5", "0.75", "1"])

    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)

    ax.yaxis.set_ticks_position("left")
    ax.xaxis.set_ticks_position("bottom")
    for tic in ax.xaxis.get_major_ticks():
        tic.tick1On = tic.tick2On = False

    fig.subplots_adjust(left=0.17, bottom=0.17, right=0.95, top=0.9)
    if fn is None:
        fn = "fxPhase1_phaseInfrd,R=%.3f.eps" % _N.sqrt(R2)
    else:
        fn = "%(1)s,R=%(2).3f.eps" % {"1": fn, "2": _N.sqrt(R2)}

    _plt.savefig(fn, transparent=True)
    _plt.close()
    return pInfrdAt0, _N.sqrt(R2), nPh0s
Example #22
0
shfl_type=_SHFL_KEEP_CONT

slideby_sec = slideby/Fs
lags = int(lags_sec / slideby_sec)+1  #  (slideby/300)*lags
xticksD = [-20, -10, 0, 10, 20]
time_lags = _N.linspace(-(slideby/300)*lags, lags*(slideby/300), 2*lags+1)
xticksD = [-20, -10, 0, 10, 20]
midp = lags

####  # of sections
####  # gk size for smoothing
####  # pk_win_sec
sections = 6
int_gkw = 6
gk = gauKer(int_gkw)
#gk = gauKer(2)
gk /= _N.sum(gk)
pk_win_sec   = 2.5

prm_dir = "sec=%(sec)d_gkintw=%(gk)d_pkwin_sec=%(pws).1f" % {"sec" : sections, "gk" : int_gkw, "pws" : pk_win_sec}

print("lags   %d" % lags)
iexpt = -1

#time_lags = _N.linspace(-(slideby/300)*lags, lags*(slideby/300), 2*lags+1)


#gk = gauKer(2)

#all_x= _N.arange(-141, 142)
Example #23
0
    def gibbs(self, ITERS, K, ep1=0, ep2=None, savePosterior=True, gtdiffusion=False, doSepHash=True, use_spc=True, nz_pth=0., smth_pth_ker=100, ignoresilence=False, use_omp=False, nThrds=2):
        """
        gtdiffusion:  use ground truth center of place field in calculating variance of center.  Meaning of diffPerMin different
        """
        print "gibbs   %.5f" % _N.random.rand()
        oo = self
        oo.nThrds = nThrds
        twpi     = 2*_N.pi
        pcklme   = {}

        ep2 = oo.epochs if (ep2 == None) else ep2
        oo.epochs = ep2-ep1

        ######################################  GRID for calculating
        ####  #  points in sum.  
        ####  #  points in uniform sampling of exp(x)p(x)   (non-spike interals)
        ####  #  points in sampling of f  for conditional posterior distribution
        ####  #  points in sampling of q2 for conditional posterior distribution
        ####  NSexp, Nupx, fss, q2ss

        #  numerical grid
        ux = _N.linspace(oo.xLo, oo.xHi, oo.Nupx, endpoint=False)   # uniform x position   #  grid over 
        uxr = ux.reshape((1, oo.Nupx))
        uxrr= ux.reshape((1, 1, oo.Nupx))
        #q2x    = _N.exp(_N.linspace(_N.log(1e-7), _N.log(100), oo.q2ss))  #  5 orders of
        q2x    = _N.exp(_N.linspace(_N.log(oo.q2x_L), _N.log(oo.q2x_H), oo.q2ss))  #  5 orders of
        d_q2x  = _N.diff(q2x)
        q2x_m1 = _N.array(q2x[0:-1])
        lq2x    = _N.log(q2x)
        iq2x    = 1./q2x
        q2xr     = q2x.reshape((oo.q2ss, 1))
        iq2xr     = 1./q2xr
        q2xrr     = q2x.reshape((1, oo.q2ss, 1))
        iq2xrr     = 1./q2xrr
        d_q2xr  =  d_q2x.reshape((oo.q2ss - 1, 1))
        q2x_m1  = _N.array(q2x[0:-1])
        q2x_m1r = q2x_m1.reshape((oo.q2ss-1, 1))

        sqrt_2pi_q2x   = _N.sqrt(twpi*q2x)
        l_sqrt_2pi_q2x = _N.log(sqrt_2pi_q2x)

        freeClstr = None
        if smth_pth_ker > 0:
            gk     = gauKer(smth_pth_ker) # 0.1s  smoothing of motion
            gk     /= _N.sum(gk)
            xf     = _N.convolve(oo.dat[:, 0], gk, mode="same")
            oo.dat[:, 0] = xf + nz_pth*_N.random.randn(len(oo.dat[:, 0]))
        else:
            oo.dat[:, 0] += nz_pth*_N.random.randn(len(oo.dat[:, 0]))
        x      = oo.dat[:, 0]
        mks    = oo.dat[:, 2:]
        if nz_pth > 0:
            _N.savetxt(resFN("nzyx.txt", dir=oo.outdir), x, fmt="%.4f")

        f_q2_rate = (oo.diffusePerMin**2)/60000.  #  unit of minutes  
        
        ######################################  PRECOMPUTED

        tau_l0 = oo.t_hlf_l0/_N.log(2)
        tau_q2 = oo.t_hlf_q2/_N.log(2)

        for epc in xrange(ep1, ep2):
            print "^^^^^^^^^^^^^^^^^^^^^^^^    epoch %d" % epc

            t0 = oo.intvs[epc]
            t1 = oo.intvs[epc+1]
            if epc > 0:
                tm1= oo.intvs[epc-1]
                #  0  10 30     20 - 5  = 15    0.5*((10+30) - (10+0)) = 15
                dt = 0.5*((t1+t0) - (t0+tm1))

            dt = (t1-t0)*0.5
            xt0t1 = _N.array(x[t0:t1])
            posbins  = _N.linspace(oo.xLo, oo.xHi, oo.Nupx+1)
            #  _N.sum(px)*(xbns[1]-xbns[0]) = 1
            px, xbns = _N.histogram(xt0t1, bins=posbins, normed=True)   
            pxr      = px.reshape((1, oo.Nupx))
            pxrr     = px.reshape((1, 1, oo.Nupx))

            Asts    = _N.where(oo.dat[t0:t1, 1] == 1)[0]   #  based at 0

            if epc == ep1:   ###  initialize
                labS, labH, flatlabels, M, MF, hashthresh, nHSclusters = gAMxMu.initClusters(oo, K, x, mks, t0, t1, Asts, doSepHash=doSepHash, xLo=oo.xLo, xHi=oo.xHi, oneCluster=oo.oneCluster, nzclstr=oo.nzclstr)
 
                Mwowonz       = M+1 if oo.nzclstr else M
                #nHSclusters.append(M - nHSclusters[0]-nHSclusters[1])   #  last are free clusters that are not the noise cluster

                u_u_  = _N.empty((M, K))
                u_Sg_ = _N.empty((M, K, K))
                #######   containers for GIBBS samples iterations
                smp_sp_prms = _N.zeros((3, ITERS, M))  
                smp_mk_prms = [_N.zeros((K, ITERS, M)), 
                               _N.zeros((K, K, ITERS, M))]
                smp_sp_hyps = _N.zeros((6, ITERS, M))
                smp_mk_hyps = [_N.zeros((K, ITERS, M)), 
                               _N.zeros((K, K, ITERS, M)),
                               _N.zeros((1, ITERS, M)), 
                               _N.zeros((K, K, ITERS, M))]
                oo.smp_sp_prms = smp_sp_prms
                oo.smp_mk_prms = smp_mk_prms
                oo.smp_sp_hyps = smp_sp_hyps
                oo.smp_mk_hyps = smp_mk_hyps

                if oo.nzclstr:
                    smp_nz_l0     = _N.zeros(ITERS)
                    smp_nz_hyps = _N.zeros((2, ITERS))

                #  list of freeClstrs
                freeClstr = _N.empty(M, dtype=_N.bool)   #  Actual cluster
                freeClstr[:] = False

                l0, f, q2, u, Sg = gAMxMu.declare_params(M, K, nzclstr=oo.nzclstr)   #  nzclstr not inited  # sized to include noise cluster if needed
                _l0_a, _l0_B, _f_u, _f_q2, _q2_a, _q2_B, _u_u, _u_Sg, _Sg_nu, \
                    _Sg_PSI = gAMxMu.declare_prior_hyp_params(M, MF, K, x, mks, Asts, t0)
                fr = f[0:M].reshape((M, 1))
                gAMxMu.init_params_hyps(oo, M, MF, K, l0, f, q2, u, Sg, _l0_a, _l0_B, _f_u, _f_q2, _q2_a, _q2_B, _u_u, _u_Sg, _Sg_nu, \
                    _Sg_PSI, Asts, t0, x, mks, flatlabels, nHSclusters, nzclstr=oo.nzclstr)

                U   = _N.empty(M)
                FQ2 = _N.empty(M)
                _fxs0 = _N.tile(_N.linspace(0, 1, oo.fss), M).reshape(M, oo.fss)

                f_exp_px = _N.empty((M, oo.fss))
                q2_exp_px= _N.empty((M, oo.q2ss))

                if oo.nzclstr:
                    nz_l0_intgrd   = _N.exp(-0.5*ux*ux / q2[Mwowonz-1])
                    _nz_l0_a       = 0.001
                    _nz_l0_B       = 0.1

                ######  the hyperparameters for f, q2, u, Sg, l0 during Gibbs
                #  f_u_, f_q2_, q2_a_, q2_B_, u_u_, u_Sg_, Sg_nu, Sg_PSI_, l0_a_, l0_B_


            NSexp   = t1-t0    #  length of position data  #  # of no spike positions to sum
            xt0t1 = _N.array(x[t0:t1])

            nSpks    = len(Asts)
            gz   = _N.zeros((ITERS, nSpks, Mwowonz), dtype=_N.bool)
            oo.gz=gz
            print "spikes %d" % nSpks

            dSilenceX1 = (NSexp/float(oo.Nupx))*(oo.xHi-oo.xLo)
            dSilenceX2 = NSexp*(xbns[1]-xbns[0])  # dx of histogram
            print "-------------------------- %(1).4f  %(2).4f" % {"1" : dSilenceX1, "2" : dSilenceX2}

            dSilenceX  = dSilenceX1

            xAS  = x[Asts + t0]   #  position @ spikes
            mAS  = mks[Asts + t0]   #  position @ spikes
            xASr = xAS.reshape((1, nSpks))
            mASr = mAS.reshape((1, nSpks, K))
            econt = _N.empty((Mwowonz, nSpks))
            rat   = _N.zeros((Mwowonz+1, nSpks))

            qdrMKS = _N.empty((Mwowonz, nSpks))
            ################################  GIBBS ITERS ITERS ITERS

            clstsz = _N.zeros(M, dtype=_N.int)

            _iu_Sg = _N.array(_u_Sg)
            for m in xrange(M):
                _iu_Sg[m] = _N.linalg.inv(_u_Sg[m])

            ttA = _tm.time()

            for iter in xrange(ITERS):
                tt1 = _tm.time()
                iSg = _N.linalg.inv(Sg)

                if (iter % 100) == 0:    
                    #print "-------iter  %(i)d   %(r).5f" % {"i" : iter, "r" : _N.random.rand()}
                    print "-------iter  %(i)d" % {"i" : iter}

                gAMxMu.stochasticAssignment(oo, epc, iter, M, Mwowonz, K, l0, f, q2, u, Sg, _f_u, _u_u, _f_q2, _u_Sg, Asts, t0, mASr, xASr, rat, econt, gz, qdrMKS, freeClstr, hashthresh, ((epc > 0) and (iter == 0)), nthrds=oo.nThrds)
                #gAMxMu.stochasticAssignment(oo, iter, M, Mwowonz, K, l0, f, q2, u, Sg, _f_u, _u_u, Asts, t0, mASr, xASr, rat, econt, gz, qdrMKS, freeClstr, hashthresh, iter==0, nthrds=oo.nThrds)

                ###############  FOR EACH CLUSTER

                l_sts = []
                for m in xrange(M):   #  get the minds
                    minds = _N.where(gz[iter, :, m] == 1)[0]  
                    sts  = Asts[minds] + t0   #  sts is in absolute time
                    clstsz[m] = len(sts)
                    l_sts.append(sts)
                # for m in xrange(Mwowonz):   #  get the minds
                #     minds = _N.where(gz[iter, :, m] == 1)[0]  
                #     print "cluster %(m)d   len %(l)d    " % {"m" : m, "l" : len(minds)}
                #     print u[m]
                #     print f[m]

                #tt2 = _tm.time()
                ###############
                ###############  CONDITIONAL l0
                ###############

                #  _ss.gamma.rvs.  uses k, theta  k is 1/B (B is our thing)
                iiq2 = 1./q2[0:M]
                iiq2r= iiq2.reshape((M, 1))
                iiq2rr= iiq2.reshape((M, 1, 1))

                fr = f[0:M].reshape((M, 1))
                l0_intgrd   = _N.exp(-0.5*(fr - ux)*(fr-ux) * iiq2r)  

                sLLkPr      = _N.empty((M, oo.q2ss))
                l0_exp_px   = _N.sum(l0_intgrd*pxr, axis=1) * dSilenceX
                BL  = (oo.dt/_N.sqrt(twpi*q2[0:M]))*l0_exp_px    #  dim M

                if (epc > 0) and oo.adapt:
                    _md_nd= _l0_a / _l0_B
                    _Dl0_a = _l0_a * _N.exp(-dt/tau_l0)
                    _Dl0_B = _Dl0_a / _md_nd
                else:
                    _Dl0_a = _l0_a
                    _Dl0_B = _l0_B

                aL  = clstsz
                l0_a_ = aL + _Dl0_a
                l0_B_ = BL + _Dl0_B
                
                try:
                    #  mean is (l0_a_ / l0_B_)
                    l0[0:M] = _ss.gamma.rvs(l0_a_, scale=(1/l0_B_))  #  check
                except ValueError:
                    """
                    print l0_B_
                    print _Dl0_B
                    print BL
                    print l0_exp_px
                    print 1/_N.sqrt(twpi*q2[0:M])

                    print pxr
                    print l0_intgrd
                    """
                    _N.savetxt("fxux", (fr - ux)*(fr-ux))
                    _N.savetxt("fr", fr)
                    _N.savetxt("iiq2", iiq2)
                    _N.savetxt("l0_intgrd", l0_intgrd)
                    raise


                smp_sp_prms[oo.ky_p_l0, iter] = l0[0:M]
                smp_sp_hyps[oo.ky_h_l0_a, iter] = l0_a_
                smp_sp_hyps[oo.ky_h_l0_B, iter] = l0_B_
                mcs = _N.empty((M, K))   # cluster sample means

                #tt3 = _tm.time()

                ###############
                ###############     u
                ###############
                for m in xrange(M):
                    if clstsz[m] > 0:
                        u_Sg_[m] = _N.linalg.inv(_iu_Sg[m] + clstsz[m]*iSg[m])
                        clstx    = mks[l_sts[m]]

                        mcs[m]       = _N.mean(clstx, axis=0)
                        #u_u_[m] = _N.dot(u_Sg_[m], _N.dot(_iu_Sg[m], _u_u[m]) + clstsz[m]*_N.dot(iSg[m], mcs[m]))
                        u_u_[m] = _N.einsum("jk,k->j", u_Sg_[m], _N.dot(_iu_Sg[m], _u_u[m]) + clstsz[m]*_N.dot(iSg[m], mcs[m]))
                        # print "mean of cluster %d" % m
                        # print mcs[m]
                        # print u_u_[m]
                        # hyp
                        ########  POSITION
                        ##  mean of posterior distribution of cluster means
                        #  sigma^2 and mu are the current Gibbs-sampled values

                        ##  mean of posterior distribution of cluster means
                        # print "for cluster %(m)d with size %(sz)d" % {"m" : m, "sz" : clstsz[m]}
                        # print mcs[m]
                        # print u_u_[m]
                        # print _u_u[m]
                    else:
                        u_Sg_[m] = _N.array(_u_Sg[m])
                        u_u_[m] = _N.array(_u_u[m])

                ucmvnrms= _N.random.randn(M, K)
                C       = _N.linalg.cholesky(u_Sg_)
                u[0:M]       = _N.einsum("njk,nk->nj", C, ucmvnrms) + u_u_

                smp_mk_prms[oo.ky_p_u][:, iter] = u[0:M].T  # dim of u wrong
                smp_mk_hyps[oo.ky_h_u_u][:, iter] = u_u_.T
                smp_mk_hyps[oo.ky_h_u_Sg][:, :, iter] = u_Sg_.T

                #tt4 = _tm.time()
                ###############
                ###############  Conditional f
                ###############

                if (epc > 0) and oo.adapt:
                    q2pr = _f_q2 + f_q2_rate * dt
                else:
                    q2pr = _f_q2
                for m in xrange(M):
                    sts = l_sts[m]
                    if clstsz[m] > 0:
                        fs  = (1./clstsz[m])*_N.sum(xt0t1[sts-t0])
                        fq2 = q2[m]/clstsz[m]
                        U[m]   = (fs*q2pr[m] + _f_u[m]*fq2) / (q2pr[m] + fq2)
                        FQ2[m] = (q2pr[m]*fq2) / (q2pr[m] + fq2)
                    else:
                        U[m]   = _f_u[m]
                        FQ2[m] = q2pr[m]

                FQ    = _N.sqrt(FQ2)
                Ur    = U.reshape((M, 1))
                FQr   = FQ.reshape((M, 1))
                FQ2r  = FQ2.reshape((M, 1))

                if use_spc:
                    fxs  = _N.copy(_fxs0)
                    fxs *= (FQr*120)
                    fxs -= (FQr*60)
                    fxs += Ur

                    if use_omp:
                        M_times_N_f_intgrls_raw(fxs, ux, iiq2, dSilenceX, px, f_exp_px, M, oo.fss, oo.Nupx, oo.nThrds)
                    else:
                        fxsr     = fxs.reshape((M, oo.fss, 1))
                        fxrux = -0.5*(fxsr-uxrr)*(fxsr-uxrr)
                        #  f_intgrd    is M x fss x Nupx
                        f_intgrd  = _N.exp(fxrux*iiq2rr)   #  integrand
                        f_exp_px = _N.sum(f_intgrd*pxrr, axis=2) * dSilenceX
                        #  f_exp_px   is M x fss
                    l0r = l0[0:M].reshape((M, 1))
                    q2r = q2[0:M].reshape((M, 1))
                     #  s   is (M x fss)
                    s = -(l0r*oo.dt/_N.sqrt(twpi*q2r)) * f_exp_px  #  a function of x
                    #if (iter > ITERS - 40) and (iter % 5 == 0):
                    #    print f_exp_px
                    #    _plt.plot(fxs[0], _N.s)
                else:
                    s = _N.zeros(M)

                #  U, FQ2 is   dim(M)   
                #  fxs is M x fss
                funcf   = -0.5*((fxs-Ur)*(fxs-Ur))/FQ2r + s
                maxes   = _N.max(funcf, axis=1)   
                maxesr  = maxes.reshape((M, 1))
                funcf   -= maxesr
                condPosF= _N.exp(funcf)   #  condPosF is M x fss
                ttB = _tm.time()

                #  fxs   M x fss
                #  fxs            M x fss
                #  condPosF       M x fss
                norm    = 1./_N.sum(condPosF, axis=1)  #  sz M
                f_u_    = norm*_N.sum(fxs*condPosF, axis=1)  #  sz M
                f_u_r   = f_u_.reshape((M, 1))
                f_q2_   = norm*_N.sum(condPosF*(fxs-f_u_r)*(fxs-f_u_r), axis=1)
                f[0:M]       = _N.sqrt(f_q2_)*_N.random.randn() + f_u_
                smp_sp_prms[oo.ky_p_f, iter] = f[0:M]
                smp_sp_hyps[oo.ky_h_f_u, iter] = f_u_
                smp_sp_hyps[oo.ky_h_f_q2, iter] = f_q2_

                #tt5 = _tm.time()
                ##############
                ##############  VARIANCE, COVARIANCE
                ##############
                for m in xrange(M):
                    if clstsz[m] >= K:
                        ##  dof of posterior distribution of cluster covariance
                        Sg_nu_ = _Sg_nu[m, 0] + clstsz[m]
                        ##  dof of posterior distribution of cluster covariance
                        ur = u[m].reshape((1, K))
                        clstx    = mks[l_sts[m]]
                        Sg_PSI_ = _Sg_PSI[m] + _N.dot((clstx - ur).T, (clstx-ur))
                    else:
                        Sg_nu_ = _Sg_nu[m, 0] 
                        ##  dof of posterior distribution of cluster covariance
                        ur = u[m].reshape((1, K))
                        Sg_PSI_ = _Sg_PSI[m]
                    Sg[m] = s_u.sample_invwishart(Sg_PSI_, Sg_nu_)
                    smp_mk_hyps[oo.ky_h_Sg_nu][0, iter, m] = Sg_nu_
                    smp_mk_hyps[oo.ky_h_Sg_PSI][:, :, iter, m] = Sg_PSI_

                
                ##  dof of posterior distribution of cluster covariance

                smp_mk_prms[oo.ky_p_Sg][:, :, iter] = Sg[0:M].T

                #tt6 = _tm.time()
                ##############
                ##############  SAMPLE SPATIAL VARIANCE
                ##############
                if use_spc:
                    #  M x q2ss x Nupx  
                    #  f        M x 1    x 1
                    #  iq2xrr   1 x q2ss x 1
                    #  uxrr     1 x 1    x Nupx

                    if use_omp:  #ux variable held fixed
                        M_times_N_q2_intgrls_raw(f, ux, iq2x, dSilenceX, px, q2_exp_px, M, oo.q2ss, oo.Nupx, oo.nThrds)
                    else:
                        frr       = f.reshape((M, 1, 1))
                        q2_intgrd = _N.exp(-0.5*(frr - uxrr)*(frr-uxrr) * iq2xrr)
                        q2_exp_px = _N.sum(q2_intgrd*pxrr, axis=2) * dSilenceX

                    # function of q2

                    s = -((l0r*oo.dt)/sqrt_2pi_q2x)*q2_exp_px
                else:
                    s = _N.zeros((oo.q2ss, M))
                #  B' / (a' - 1) = MODE   #keep mode the same after discount
                #  B' = MODE * (a' - 1)
                if (epc > 0) and oo.adapt:
                    _md_nd= _q2_B / (_q2_a + 1)
                    _Dq2_a = _q2_a * _N.exp(-dt/tau_q2)
                    _Dq2_B = _Dq2_a / _md_nd
                else:
                    _Dq2_a = _q2_a
                    _Dq2_B = _q2_B

                SL_Bs = _N.empty(M)
                SL_as = _N.empty(M)

                for m in xrange(M):
                    if clstsz[m] > 0:
                        sts = l_sts[m]
                        xI = (xt0t1[sts-t0]-f[m])*(xt0t1[sts-t0]-f[m])*0.5
                        SL_a = 0.5*clstsz[m] - 1   #  spiking part of likelihood
                        SL_B = _N.sum(xI)  #  spiking part of likelihood
                        SL_Bs[m] = SL_B
                        SL_as[m] = SL_a
                        #  spiking prior x prior
                        #sLLkPr[m] = -(SL_a + 1)*lq2x - iq2x*SL_B
                        sLLkPr[m] = -(_q2_a[m] + SL_a + 2)*lq2x - iq2x*(_q2_B[m] + SL_B)
                    else:
                        sLLkPr[m] = -(_q2_a[m] + 1)*lq2x - iq2x*_q2_B[m]

                q2_a_, q2_B_ = mltpl_ig_prmsUV(q2xr, sLLkPr.T, s.T, d_q2xr, q2x_m1r, clstsz, iter, mks, t0, xt0t1, gz, l_sts, SL_as, SL_Bs, _q2_a, _q2_B, oo.q2_min, oo.q2_max)
                    
                q2[0:M] = _ss.invgamma.rvs(q2_a_ + 1, scale=q2_B_)  #  check

                tt7 = _tm.time()

                smp_sp_prms[oo.ky_p_q2, iter]   = q2[0:M]
                smp_sp_hyps[oo.ky_h_q2_a, iter] = q2_a_
                smp_sp_hyps[oo.ky_h_q2_B, iter] = q2_B_

                # print "timing start"
                # print (tt2-tt1)
                # print (tt3-tt2)
                # print (tt4-tt3)
                # print (tt5-tt4)
                # print (tt6-tt5)
                #print (tt7-tt1)
                # print "timing end"

                    
                #  nz clstr.  fixed width
                if oo.nzclstr:
                    nz_l0_exp_px   = _N.sum(nz_l0_intgrd*px) * dSilenceX
                    BL  = (oo.dt/_N.sqrt(twpi*q2[Mwowonz-1]))*nz_l0_exp_px

                    minds = len(_N.where(gz[iter, :, Mwowonz-1] == 1)[0])
                    l0_a_ = minds + _nz_l0_a
                    l0_B_ = BL    + _nz_l0_B

                    l0[Mwowonz-1]  = _ss.gamma.rvs(l0_a_, scale=(1/l0_B_)) 
                    smp_nz_l0[iter]       = l0[Mwowonz-1]
                    smp_nz_hyps[0, iter]  = l0_a_
                    smp_nz_hyps[1, iter]  = l0_B_

            ttB = _tm.time()
            print (ttB-ttA)

            gAMxMu.finish_epoch(oo, nSpks, epc, ITERS, gz, l0, f, q2, u, Sg, _f_u, _f_q2, _q2_a, _q2_B, _l0_a, _l0_B, _u_u, _u_Sg, _Sg_nu, _Sg_PSI, smp_sp_hyps, smp_sp_prms, smp_mk_hyps, smp_mk_prms, freeClstr, M, K)
            #  MAP of nzclstr
            if oo.nzclstr:
                frm = int(0.7*ITERS)
                _nz_l0_a              = _N.median(smp_nz_hyps[0, frm:])
                _nz_l0_B              = _N.median(smp_nz_hyps[1, frm:])
            pcklme["smp_sp_hyps"] = smp_sp_hyps
            pcklme["smp_mk_hyps"] = smp_mk_hyps
            pcklme["smp_sp_prms"] = smp_sp_prms
            pcklme["smp_mk_prms"] = smp_mk_prms
            pcklme["sp_prmPstMd"] = oo.sp_prmPstMd
            pcklme["mk_prmPstMd"] = oo.mk_prmPstMd
            pcklme["intvs"]       = oo.intvs
            pcklme["occ"]         = gz
            pcklme["nz_pth"]         = nz_pth
            pcklme["M"]           = M
            pcklme["Mwowonz"]           = Mwowonz
            if Mwowonz > M:  # or oo.nzclstr == True
                pcklme["nz_fs"]       = f[M]
                pcklme["nz_q2"]       = q2[M]
                pcklme["nz_Sg"]       = Sg[M]
                pcklme["nz_u"]        = u[M]
                pcklme["smp_nz_l0"]  = smp_nz_l0
                pcklme["smp_nz_hyps"]= smp_nz_hyps
                
            dmp = open(resFN("posteriors_%d.dmp" % epc, dir=oo.outdir), "wb")
            pickle.dump(pcklme, dmp, -1)
            dmp.close()
Example #24
0
_ME_WTL = 0
_ME_RPS = 1

_SHFL_KEEP_CONT = 0
_SHFL_NO_KEEP_CONT = 1
shfl_type_str = _N.array(["keep_cont", "no_keep_cont"])


def depickle(s):
    import pickle
    with open(s, "rb") as f:
        lm = pickle.load(f)
    return lm


gk_fot = gauKer(1)
gk_fot = gk_fot / _N.sum(gk_fot)


def find_osc_timescale(ACfun, lags):
    ACfun = _N.convolve(ACfun, gk_fot, mode="same")  #  smooth out AC function
    dACfun = _N.diff(ACfun)
    maxes = _N.where((dACfun[0:-1] > 0) & (dACfun[1:] <= 0))[0]
    mins = _N.where((dACfun[0:-1] < 0) & (dACfun[1:] >= 0))[0]
    mnMaxes = -1
    mnMins = -1
    mnInt = 0
    nTerms = 0
    if (len(maxes) >= 2):
        mnMaxes = _N.diff(maxes)[0]  #  intervals
        #mnMaxes = _N.mean(_N.diff(maxes))  #  intervals
Example #25
0
partIDs, incmp_dat = only_complete_data(partIDs, TO, label, SHF_NUM)
strtTr = 0
TO -= strtTr

#fig= _plt.figure(figsize=(14, 14))

SHUFFLES = 1
nMimics = _N.empty(len(partIDs), dtype=_N.int)
t0 = -5
t1 = 10
trigger_temp = _N.empty(t1 - t0)
cut = 1
all_avgs = _N.empty((len(partIDs), t1 - t0))
netwins = _N.empty(len(partIDs), dtype=_N.int)
gk = gauKer(1)
gk /= _N.sum(gk)
#gk = None

UD_diff = _N.empty((len(partIDs), 3))
corrs_all = _N.empty((3, 6))
corrs_sing = _N.empty((len(partIDs), 3, 6))

perform = _N.empty(len(partIDs))

pid = 0

ts = _N.arange(t0 - 2, t1 - 2)
signal_5_95 = _N.empty((len(partIDs), t1 - t0))

hnd_dat_all = _N.zeros((len(partIDs), TO, 4), dtype=_N.int)
Example #26
0
SHF_NUM = 0

partIDs, incmp_dat = only_complete_data(partIDs, TO, label, SHF_NUM)
strtTr = 0
TO -= strtTr

#fig= _plt.figure(figsize=(14, 14))

SHUFFLES = 50

t0 = -5
t1 = 10
cut = 1
all_avgs = _N.empty((len(partIDs), SHUFFLES + 1, t1 - t0))
netwins = _N.empty(len(partIDs), dtype=_N.int)
gk = gauKer(1)
gk /= _N.sum(gk)
#gk = None

corrs_all = _N.empty((3, 6))
corrs_sing = _N.empty((len(partIDs), 3, 6))

perform = _N.empty(len(partIDs))

pid = 0

ts = _N.arange(t0 - 2, t1 - 2)
signal_5_95 = _N.empty((len(partIDs), 4, t1 - t0))

pc_sum = _N.empty(len(partIDs))
pc_sum01 = _N.empty(len(partIDs))
Example #27
0
    def setParams(self, psth_run=False, psth_knts=10):
        oo = self
        # #generate initial values of parameters
        #oo._d = _kfardat.KFARGauObsDat(oo.TR, oo.N, oo.k)
        #oo._d.copyData(oo.y)

        oo.Ns = _N.ones(oo.TR, dtype=_N.int) * oo.N
        oo.ks = _N.ones(oo.TR, dtype=_N.int) * oo.k

        oo.F = _N.zeros((oo.k, oo.k))
        _N.fill_diagonal(oo.F[1:, 0:oo.k - 1], 1)
        oo.F[0] = _N.random.randn(oo.k) / _N.arange(1, oo.k + 1)**2
        oo.F[0, 0] = 0.8
        oo.Fs = _N.zeros((oo.TR, oo.k, oo.k))
        for tr in range(oo.TR):
            oo.Fs[tr] = oo.F
        oo.Ik = _N.identity(oo.k)
        oo.IkN = _N.tile(oo.Ik, (oo.N + 1, 1, 1))

        #  need TR
        #  pr_x[:, 0]  empty, not used
        #oo.p_x   = _N.empty((oo.TR, oo.N+1, oo.k, 1))
        oo.p_x = _N.empty((oo.TR, oo.N + 1, oo.k))
        oo.p_x[:, 0, 0] = 0
        oo.p_V = _N.empty((oo.TR, oo.N + 1, oo.k, oo.k))
        oo.p_Vi = _N.empty((oo.TR, oo.N + 1, oo.k, oo.k))
        #oo.f_x   = _N.empty((oo.TR, oo.N+1, oo.k, 1))
        oo.f_x = _N.empty((oo.TR, oo.N + 1, oo.k))
        oo.f_V = _N.empty((oo.TR, oo.N + 1, oo.k, oo.k))
        #oo.s_x   = _N.empty((oo.TR, oo.N+1, oo.k, 1))
        oo.s_x = _N.empty((oo.TR, oo.N + 1, oo.k))
        oo.s_V = _N.empty((oo.TR, oo.N + 1, oo.k, oo.k))

        _N.fill_diagonal(oo.F[1:, 0:oo.k - 1], 1)
        oo.G = _N.zeros((oo.k, 1))
        oo.G[0, 0] = 1
        oo.Q = _N.empty(oo.TR)

        #  baseFN_inter   baseFN_comps   baseFN_comps

        print("freq_lims")
        print(oo.freq_lims)
        radians = buildLims(0, oo.freq_lims, nzLimL=1., Fs=(1 / oo.dt))
        oo.AR2lims = 2 * _N.cos(radians)

        oo.smpx = _N.zeros((oo.TR, (oo.N + 1) + 2, oo.k))  #  start at 0 + u
        oo.ws = _N.empty((oo.TR, oo.N + 1), dtype=_N.float)

        #############   ADDED THIS FOR DEBUG
        #oo.F_alfa_rep = _N.array([-0.4       +0.j,          0.96999828+0.00182841j,  0.96999828-0.00182841j, 0.51000064+0.02405102j,  0.51000064-0.02405102j,  0.64524011+0.04059507j, 0.64524011-0.04059507j]).tolist()
        if oo.F_alfa_rep is None:
            oo.F_alfa_rep = initF(oo.R, oo.Cs + oo.Cn,
                                  0).tolist()  #  init F_alfa_rep
        print("F_alfa_rep*********************")
        print(oo.F_alfa_rep)

        #print(ampAngRep(oo.F_alfa_rep))
        if oo.q20 is None:
            oo.q20 = 0.00077
        oo.q2 = _N.ones(oo.TR) * oo.q20

        oo.F0 = (-1 * _Npp.polyfromroots(oo.F_alfa_rep)[::-1].real)[1:]
        oo.Fs = _N.zeros((oo.TR, oo.k, oo.k))

        oo.F[0] = oo.F0
        _N.fill_diagonal(oo.F[1:, 0:oo.k - 1], 1)

        for tr in range(oo.TR):
            oo.Fs[tr] = oo.F

        ########  Limit the amplitude to something reasonable
        xE, nul = createDataAR(oo.N, oo.F0, oo.q20, 0.1)
        mlt = _N.std(xE) / 0.5  #  we want amplitude around 0.5
        oo.q2 /= mlt * mlt
        xE, nul = createDataAR(oo.N, oo.F0, oo.q2[0], 0.1)

        w = 5
        wf = gauKer(w)
        gk = _N.empty((oo.TR, oo.N + 1))
        fgk = _N.empty((oo.TR, oo.N + 1))
        for m in range(oo.TR):
            gk[m] = _N.convolve(oo.y[m], wf, mode="same")
            gk[m] = gk[m] - _N.mean(gk[m])
            gk[m] /= 5 * _N.std(gk[m])
            fgk[m] = bpFilt(15, 100, 1, 135, 500, gk[m])  #  we want
            fgk[m, :] /= 3 * _N.std(fgk[m, :])

            if oo.noAR:
                oo.smpx[m, 2:, 0] = 0
            else:
                oo.smpx[m, 2:, 0] = fgk[m, :]

            for n in range(2 + oo.k - 1, oo.N + 1 + 2):  # CREATE square smpx
                oo.smpx[m, n, 1:] = oo.smpx[m, n - oo.k + 1:n, 0][::-1]
            for n in range(2 + oo.k - 2, -1, -1):  # CREATE square smpx
                oo.smpx[m, n, 0:oo.k - 1] = oo.smpx[m, n + 1, 1:oo.k]
                oo.smpx[m, n, oo.k - 1] = _N.dot(oo.F0,
                                                 oo.smpx[m, n:n + oo.k,
                                                         oo.k - 2])  # no noise

        if oo.bpsth:
            psthKnts, apsth, aWeights = _spknts.suggestPSTHKnots(
                oo.dt,
                oo.TR,
                oo.N + 1,
                oo.y.T,
                psth_knts=psth_knts,
                psth_run=psth_run)

            _N.savetxt("apsth.txt", apsth, fmt="%.4f")
            _N.savetxt("psthKnts.txt", psthKnts, fmt="%.4f")

            apprx_ps = _N.array(_N.abs(aWeights))
            oo.u_a = -_N.log(1 / apprx_ps - 1)

            #  For oo.u_a, use the values we get from aWeights

            oo.B = patsy.bs(_N.linspace(0, (oo.t1 - oo.t0) * oo.dt,
                                        (oo.t1 - oo.t0)),
                            knots=(psthKnts * oo.dt),
                            include_intercept=True)  #  spline basis

            oo.B = oo.B.T  #  My convention for beta
            oo.aS = _N.array(oo.u_a)
            # fig = _plt.figure(figsize=(4, 7))
            # fig.add_subplot(2, 1, 1)
            # _plt.plot(apsth)
            # fig.add_subplot(2, 1, 2)
            # _plt.plot(_N.dot(oo.B.T, aWeights))
        else:
            oo.B = patsy.bs(_N.linspace(0, (oo.t1 - oo.t0) * oo.dt,
                                        (oo.t1 - oo.t0)),
                            df=4,
                            include_intercept=True)  #  spline basis

            oo.B = oo.B.T  #  My convention for beta
            oo.aS = _N.zeros(4)