Ejemplo n.º 1
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]
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def oscPer(setname,
           fltPrms=[5, 13, 1, 20],
           t0=None,
           t1=None,
           tr0=0,
           tr1=None,
           trials=None,
           fn=None,
           showHist=True,
           osc=None):
    """
    find period of oscillation
    """

    if t0 is None:
        t0 = 0
    if t1 is None:
        t1 = Na

    if setname is not None:
        _dat = _N.loadtxt(resFN("xprbsdN.dat", dir=setname))

        #  modulation histogram.  phase @ spike
        Na, cols = _dat.shape

        p = _re.compile("^\d{6}")  # starts like "exptDate-....."
        m = p.match(setname)

        bRealDat = True
        COLS = 4
        sub = 1

        if m == None:
            bRealDat = False
            COLS = 3
            sub = 0

        TR = cols / COLS
        if trials is None:
            if tr1 is None:
                tr1 = TR
            trials = _N.arange(tr0, tr1)

        N = t1 - t0
        dat = _dat[t0:t1, :]
    elif osc is not None:
        TR, N = osc.shape
        trials = _N.arange(TR)

        bRealDat = False
        COLS = 3
        sub = 0
        dat = _N.empty((N, COLS * TR))
        dat[:, sub::COLS] = osc.T

    Ts = []
    for tr in trials:
        x = dat[:, tr * COLS + sub]

        if fltPrms is None:
            fx = x
        elif len(fltPrms) == 2:
            fx = lpFilt(fltPrms[0], fltPrms[1], 500, x)
        else:  # 20, 40, 10, 55    #(fpL, fpH, fsL, fsH, nyqf, y):
            fx = bpFilt(fltPrms[0], fltPrms[1], fltPrms[2], fltPrms[3], 500, x)

        intvs = _N.where((fx[1:] < 0) & (fx[0:-1] >= 0))

        Ts.extend(_N.diff(intvs[0]))

    if showHist:
        fig = _plt.figure()
        _plt.hist(Ts, bins=range(min(Ts) - 1, max(Ts) + 1))
    mn = _N.mean(Ts)
    std = _N.std(Ts)
    print "mean Hz %(f).3f    cv: %(cv).3f" % {
        "cv": (std / mn),
        "f": (1000 / mn)
    }
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
def modhistAll(setname,
               shftPhase=0,
               fltPrms=[3.3, 11, 1, 15],
               t0=None,
               t1=None,
               tr0=0,
               tr1=None,
               trials=None,
               fn=None,
               maxY=None,
               yticks=None,
               normed=False,
               surrogates=1,
               color=None,
               nofig=False,
               flatten=False,
               filtCol=0,
               xlabel=None,
               smFnt=20,
               bgFnt=22):
    """
    shftPhase from 0 to 1.  
    yticks should look like [[0.5, 1, 1.5], ["0.5", "1", "1.5"]]
    """

    _dat = _N.loadtxt(resFN("xprbsdN.dat", dir=setname))
    #  modulation histogram.  phase @ spike
    Na, cols = _dat.shape

    t0 = 0 if (t0 is None) else t0
    t1 = Na if (t1 is None) else t1

    dat = _dat[t0:t1, :]
    N = t1 - t0

    p = _re.compile("^\d{6}")  # starts like "exptDate-....."
    m = p.match(setname)

    bRealDat, COLS, sub, phC = True, 4, 2, 3

    if m == None:
        bRealDat, COLS, sub = False, 3, 1

    print "realDat is %s" % str(bRealDat)

    TR = cols / COLS
    tr1 = TR if (tr1 is None) else tr1

    trials = _N.arange(tr0, tr1) if (trials is None) else trials
    if type(trials) == list:
        trials = _N.array(trials)

    phs = []
    cSpkPhs = []
    sSpkPhs = []

    for tr in trials:
        if fltPrms is not None:
            x = dat[:, tr * COLS]
            if len(fltPrms) == 2:
                fx = lpFilt(fltPrms[0], fltPrms[1], 500, x)
            elif len(fltPrms) == 4:
                # 20, 40, 10, 55 #(fpL, fpH, fsL, fsH, nyqf, y):
                fx = bpFilt(fltPrms[0], fltPrms[1], fltPrms[2], fltPrms[3],
                            500, x)
        else:
            fx = dat[:, tr * COLS + filtCol]

        ht_x = _ssig.hilbert(fx)
        ph_x = (_N.arctan2(ht_x.imag, ht_x.real) + _N.pi) / (2 * _N.pi)
        ph_x = _N.mod(ph_x + shftPhase, 1)

        ispks = _N.where(dat[:, tr * COLS + (COLS - sub)] == 1)[0]
        cSpkPhs.append(_N.cos(2 * _N.pi * ph_x[ispks]))
        sSpkPhs.append(_N.sin(2 * _N.pi * ph_x[ispks]))
        phs.append(ph_x[ispks])

    if nofig:
        if not flatten:
            return phs
        else:
            fl = []
            for i in xrange(len(phs)):
                fl.extend(phs[i])
            return fl
    return figCircularDistribution(phs,
                                   cSpkPhs,
                                   sSpkPhs,
                                   trials,
                                   surrogates=surrogates,
                                   normed=normed,
                                   fn=fn,
                                   maxY=maxY,
                                   yticks=yticks,
                                   setname=setname,
                                   color=color,
                                   xlabel=xlabel,
                                   smFnt=smFnt,
                                   bgFnt=bgFnt)
Ejemplo n.º 6
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)