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) }
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)
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)
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