Exemplo n.º 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 range(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
Exemplo n.º 2
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 range(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
Exemplo n.º 3
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 range(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 range(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
Exemplo n.º 4
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 range(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 range(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
Exemplo n.º 5
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 range(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 range(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)