def savesetMTnosc(TR, alldat, name): # also save PSTH # u, B, singleFreqAR, dt, stNz, x, dN, prbs bfn = "cnt_data" bfn = "noscp" fmt = "% .2e " fmt *= TR _N.savetxt(resFN("%s.dat" % bfn, dir=name, create=True), alldat, fmt=fmt)
def saveset(name, noparam=False): # u, B, singleFreqAR, dt, stNz, x, dN, prbs xprbsdN = _N.empty((N + 1, 3)) xprbsdN[:, 0] = x[:] xprbsdN[:, 1] = prbs[:] xprbsdN[:, 2] = dN[:] _N.savetxt(resFN("xprbsdN.dat", dir=name, create=True), xprbsdN, fmt="%.5e") if not noparam: fp = open(resFN("params.py", dir=name, create=True), "w") fp.write("u=%.3f\n" % u) fp.write("beta=%s\n" % arrstr(beta)) fp.write("ARcoeff=_N.array(%s)\n" % str(ARcoeff)) fp.write("alfa=_N.array(%s)\n" % str(alfa)) fp.write("# ampAngRep=%s\n" % ampAngRep(alfa)) fp.write("dt=%.2e\n" % dt) fp.write("stNz=%.3e\n" % stNz) fp.write("absrefr=%d\n" % absrefr) fp.close()
def loadKnown(setname, trials=None, fn="known.dat"): fn = resFN(fn, dir=setname) if os.access(fn, os.F_OK): print "*** loaded known signal \"%s\" ***" % fn a = _N.loadtxt(fn) if trials is None: return a.T else: return a.T[trials] print "!!! NO known signal loaded !!!" if fn is not None: print "!!! Couldn't find \"%s\" !!!" % fn return None
def savesetMT(TR, alldat, model, name): # u, B, singleFreqAR, dt, stNz, x, dN, prbs bfn = "cnt_data" fmt = "" if model != "bernoulli": fmt += "% .2e " fmt += "%d " fmt *= TR if model == "bernoulli": bfn = "xprbsdN" fmt += "% .2e " fmt += "%.4e %d " fmt *= TR _N.savetxt(resFN("%s.dat" % bfn, dir=name, create=True), alldat, fmt=fmt)
def loadL2(setname, fn=None): if fn is not None: fn = resFN(fn, dir=setname) if os.access(fn, os.F_OK): print "*** loaded spike history file \"%s\" ***" % fn spkhist = _N.loadtxt(fn) print spkhist[0:50] loghist = _N.log(spkhist) print loghist[0:50] print "-----------------" tooneg = _N.where(loghist < -6)[0] loghist[tooneg] = -6 return loghist print "!!! NO history file loaded !!!" if fn is not None: print "!!! Couldn't find short-term history \"%s\" !!!" % fn return None
def savesetARpn(name): # u, B, singleFreqAR, dt, stNz, x, dN, prbs xxlxhy = _N.empty((N + 1, 4)) xxlxhy[:, 0] = x[:] xxlxhy[:, 1] = loAR[:] xxlxhy[:, 2] = hiAR[:] xxlxhy[:, 3] = y[:] _N.savetxt(datFN("xxlxhy.dat", dir=name, create=True), xxlxhy, fmt="%.5e") fp = open(resFN("params.py", dir=name, create=True), "w") fp.write("arLO=%s\n" % str(arLO)) # want to keep these as list fp.write("arHI=%s\n" % str(arHI)) fp.write("dt=%.2e\n" % dt) fp.write("stNzL=%.3e\n" % stNzL) fp.write("stNzH=%.3e\n" % stNzH) fp.write("obsNz=%.3e\n" % obsNz) fp.write("H=%s\n" % arrstr(H)) fp.close()
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 figCircularDistribution(phs, cSpkPhs, sSpkPhs, trials, setname=None, surrogates=1, normed=False, fn=None, maxY=None, yticks=None, color=None, smFnt=20, bgFnt=22, xlabel="phase"): # phase histogram ltr = len(trials) inorderTrials = _N.arange(ltr) # original trial IDs no lnger necessary R2s = _N.empty(surrogates) for srgt in xrange(surrogates): if srgt == 0: trls = inorderTrials else: trls = inorderTrials[_N.sort( _N.asarray(_N.random.rand(ltr) * ltr, _N.int))] cS = [] sS = [] for tr in trls: cS.extend(cSpkPhs[tr]) sS.extend(sSpkPhs[tr]) Nspks = len(cS) vcSpkPhs = _N.array(cS) vsSpkPhs = _N.array(sS) R2s[srgt] = _N.sqrt((1. / (Nspks * Nspks)) * (_N.sum(vcSpkPhs)**2 + _N.sum(vsSpkPhs)**2)) vPhs = _N.fromiter(itls.chain.from_iterable(phs), _N.float) fig, ax = _plt.subplots(figsize=(6, 4.2)) if color is None: ec = mC.hist1 else: ec = color _plt.hist(vPhs.tolist() + (vPhs + 1).tolist(), bins=_N.linspace(0, 2, 51), color=ec, edgecolor=ec, normed=normed) if maxY is not None: _plt.ylim(0, maxY) elif normed: _plt.ylim(0, 1) #_plt.title("R = %.3f" % _N.sqrt(R2), fontsize=smFnt) _plt.xlabel(xlabel, fontsize=bgFnt) _plt.ylabel("prob. density", fontsize=bgFnt) _plt.xticks(fontsize=smFnt) _plt.yticks(fontsize=smFnt) if yticks is not None: # plotting 2 periods, probability is halved, so boost it by 2 _plt.yticks(yticks[0], yticks[1]) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].axis.axes.tick_params(direction="out", width=2) ax.spines["bottom"].axis.axes.tick_params(direction="out", width=2) 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.19, right=0.95, top=0.92) if fn is None: fn = "modulationHistogram,R=%.3f.eps" % R2s[0] else: fn = "%(1)s,R=%(2).3f.eps" % {"1": fn, "2": R2s[0]} if setname is not None: _plt.savefig(resFN(fn, dir=setname), transparent=True) else: _plt.savefig(fn, transparent=True) _plt.close() return R2s
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 runNotes(setname, ID_q2, TR0, TR1): fp = open(resFN("notes.txt", dir=setname), "w") fp.write("ID_q2=%s\n" % str(ID_q2)) fp.write("TR0=%d\n" % TR0) fp.write("TR1=%d\n" % TR1) fp.close()
def loadDat(setname, model, t0=0, t1=None, filtered=False, phase=False): ################################## if model == "bernoulli": x_st_cnts = _N.loadtxt(resFN("xprbsdN.dat", dir=setname)) y_ch = 2 # spike channel p = _re.compile("^\d{6}") # starts like "exptDate-....." m = p.match(setname) bRealDat = True dch = 4 # # of data columns per trial if m == None: bRealDat = False dch = 3 else: flt_ch = 1 # Filtered LFP ph_ch = 3 # Hilbert Trans else: x_st_cnts = _N.loadtxt(resFN("cnt_data.dat", dir=setname)) y_ch = 1 # spks dch = 2 TR = x_st_cnts.shape[1] / dch # READ parameters of generative model from file # contains N, k, singleFreqAR, u, beta, dt, stNz n0 = t0 N = x_st_cnts.shape[0] - 1 if t1 == None: t1 = N + 1 # meaning of N changes here N = t1 - 1 - t0 if TR == 1: x = x_st_cnts[t0:t1, 0] y = x_st_cnts[t0:t1, y_ch] fx = x_st_cnts[t0:t1, 0] px = x_st_cnts[t0:t1, y_ch] x = x.reshape(1, t1 - t0) y = y.reshape(1, t1 - t0) fx = x.reshape(1, t1 - t0) px = y.reshape(1, t1 - t0) else: x = x_st_cnts[t0:t1, ::dch].T y = x_st_cnts[t0:t1, y_ch::dch].T if filtered: fx = x_st_cnts[t0:t1, flt_ch::dch].T if phase: px = x_st_cnts[t0:t1, ph_ch::dch].T if model == "bernoulli": global prbs if TR == 1: prbs = x_st_cnts[t0:t1, 1] else: prbs = x_st_cnts[t0:t1, 1::dch].T # FIXED params r = 200 n = 10 # INITIAL samples if TR > 1: mnCt = _N.mean(y, axis=1) else: mnCt = _N.array([_N.mean(y)]) # remove trials where data has no information rmTrl = [] print(TR) kpTrl = range(TR) if model == "binomial": kp = y - n * 0.5 rn = n p0 = mnCt / float(rn) # matches 1 - p of genearted u = _N.log(p0 / (1 - p0)) # -1*u generated elif model == "negative binomial": kp = (y - r) * 0.5 rn = r p0 = mnCt / (mnCt + rn) # matches 1 - p of genearted u = _N.log(p0 / (1 - p0)) # -1*u generated else: kp = y - 0.5 rn = 1 dt = 0.001 logdt = _N.log(dt) if TR > 1: ysm = _N.sum(y, axis=1) for tr in xrange(TR - 1, -1, -1): if ysm[tr] == 0: print("!!!!!!!!!!!!!! trial w/ 0 spks!!!!!!!") rmTrl.append(tr) kpTrl.pop(tr) ysm[tr] = 10 # this trial will be removed anyway u = _N.log(ysm / ((N + 1 - ysm) * dt)) + logdt else: u = _N.array( [_N.log(_N.sum(y) / ((N + 1 - _N.sum(y)) * dt)) + logdt]) TR = TR - len(rmTrl) if filtered and phase: return TR, rn, x, y, fx, px, N, kp, u, rmTrl, kpTrl elif filtered and (not phase): return TR, rn, x, y, fx, N, kp, u, rmTrl, kpTrl elif (not filtered) and phase: return TR, rn, x, y, px, N, kp, u, rmTrl, kpTrl return TR, rn, x, y, N, kp, u, rmTrl, kpTrl
def create(setname): # _plt.ioff() copyfile( "%s.py" % setname, "%(s)s/%(s)s.py" % { "s": setname, "to": setFN("%s.py" % setname, dir=setname, create=True) }) global dt, lambda2, rpsth, isis, us, csTR, etme, bGenOscUsingAR, f0VAR, f0, Bf, Ba, amp, amp_nz, dSA, dSF, psth if bGenOscUsingAR: ARcoeff = _N.empty((nRhythms, 2)) for n in xrange(nRhythms): ARcoeff[n] = (-1 * _Npp.polyfromroots(alfa[n])[::-1][1:]).real stNzs = _N.empty((TR, nRhythms)) for tr in xrange(TR): if _N.random.rand() < lowQpc: lowQs.append(tr) stNzs[tr] = nzs[:, 0] else: stNzs[tr] = nzs[:, 1] # high elif bGenOscUsingSines: if f0VAR is None: f0VAR = _N.zeros(TR) sig = 0.1 x, y = createDataAR(100000, Bf, sig, sig) stdf = _N.std( x) # choice of 4 std devs to keep phase monotonically increasing x, y = createDataAR(100000, Ba, sig, sig) stda = _N.std( x) # choice of 4 std devs to keep phase monotonically increasing # x, prbs, spks 3 columns nColumns = 3 alldat = _N.empty((N, TR * nColumns)) probNOsc = _N.empty((N, TR)) spksPT = _N.empty(TR) lowQs = [] isis = [] rpsth = [] if csTR is None: csTR = _N.ones(TR) if etme is None: etme = _N.ones((TR, N)) if us is None: us = _N.zeros(TR) elif (type(us) is float) or (type(us) is int): us = _N.zeros(TR) * us for tr in xrange(TR): if bGenOscUsingAR: #x, dN, prbs, fs, prbsNOsc = createDataPPl2(TR, N, dt, ARcoeff, psth + us[tr], stNzs[tr], lambda2=lambda2, p=1, nRhythms=nRhythms, cs=csTR[tr], etme=etme[tr]) # psth is None. Turn it off for now x, dN, prbs, fs, prbsNOsc = createDataPPl2(TR, N, dt, ARcoeff, us[tr], stNzs[tr], lambda2=lambda2, p=1, nRhythms=nRhythms, cs=csTR[tr], etme=etme[tr], offset=psth) else: xosc = createFlucOsc( f0, _N.array([f0VAR[tr]]), N, dt, 1, Bf=Bf, Ba=Ba, amp=amp, amp_nz=amp_nz, stdf=stdf, stda=stda, sig=sig, smoothKer=5, dSA=dSA, dSF=dSF ) * etme[ tr] # sig is arbitrary, but we need to keep it same as when stdf, stda measured #x, dN, prbs, fs, prbsNOsc = createDataPPl2(TR, N, dt, None, psth + us[tr], None, lambda2=lambda2, p=1, nRhythms=1, cs=csTR[tr], etme=etme[tr], x=xosc[0]) x, dN, prbs, fs, prbsNOsc = createDataPPl2(TR, N, dt, None, us[tr], None, lambda2=lambda2, p=1, nRhythms=1, cs=csTR[tr], etme=etme[tr], x=xosc, offset=psth) spksPT[tr] = _N.sum(dN) rpsth.extend(_N.where(dN == 1)[0]) alldat[:, nColumns * tr] = _N.sum(x, axis=0).T * etme[tr] * csTR[tr] alldat[:, nColumns * tr + 1] = prbs alldat[:, nColumns * tr + 2] = dN probNOsc[:, tr] = prbsNOsc isis.extend(_U.toISI([_N.where(dN == 1)[0].tolist()])[0]) savesetMT(TR, alldat, model, setname) savesetMTnosc(TR, probNOsc, setname) arfs = "" xlst = [] if bGenOscUsingAR: for nr in xrange(nRhythms): arfs += "%.1fHz " % (500 * ths[nr] / _N.pi) xlst.append(x[nr]) else: xlst.append(x[0]) sTitle = "AR2 freq %(fs)s spk Hz %(spkf).1fHz TR=%(tr)d N=%(N)d" % { "spkf": (_N.sum(spksPT) / (N * TR * 0.001)), "tr": TR, "N": N, "fs": arfs } plotWFandSpks(N - 1, dN, xlst, sTitle=sTitle, sFilename=resFN("generative", dir=setname)) fig = _plt.figure(figsize=(8, 4)) _plt.hist(isis, bins=range(100), color="black") _plt.grid() _plt.savefig(resFN("ISIhist", dir=setname)) _plt.close() fig = _plt.figure(figsize=(13, 4)) _plt.plot(spksPT, marker=".", color="black", ms=8) _plt.ylim(0, max(spksPT) * 1.1) _plt.grid() _plt.suptitle("avg. Hz %.1f" % (_N.mean(spksPT) / (N * 0.001))) _plt.savefig(resFN("spksPT", dir=setname)) _plt.close() if (lambda2 is None) and (absrefr > 0): lambda2 = _N.array([0.0001] * absrefr) if lambda2 is not None: _N.savetxt(resFN("lambda2.dat", dir=setname), lambda2, fmt="%.7f") # if we want to double bin size #lambda2db = 0.5*(lambda2[1::2] + lambda2[::2]) #_N.savetxt(resFN("lambda2db.dat", dir=setname), lambda2db, fmt="%.7f") #_plt.ion() if lowQpc > 0: _N.savetxt(resFN("lowQtrials", dir=setname), lowQs, fmt="%d")
def findMode(dmp, setname, burn, NMC, startIt=None, NB=20, NeighB=1, dir=None): smp_u = dmp["u"] smp_aS = dmp["aS"] fs = dmp["fs"] amps = dmp["amps"] amps = dmp["amps"] startIt = burn if startIt == None else startIt aus = _N.mean(smp_u[:, startIt:], axis=1) aSs = _N.mean(smp_aS[startIt:], axis=0) L = burn + NMC - startIt hist, bins = _N.histogram( fs[startIt:, 0], _N.linspace(_N.min(fs[startIt:, 0]), _N.max(fs[startIt:, 0]), NB)) indMfs = _N.where(hist == _N.max(hist))[0][0] indMfsL = max(indMfs - NeighB, 0) indMfsH = min(indMfs + NeighB + 1, NB - 1) loF, hiF = bins[indMfsL], bins[indMfsH] hist, bins = _N.histogram( amps[startIt:, 0], _N.linspace(_N.min(amps[startIt:, 0]), _N.max(amps[startIt:, 0]), NB)) indMamps = _N.where(hist == _N.max(hist))[0][0] indMampsL = max(indMamps - NeighB, 0) indMampsH = min(indMamps + NeighB + 1, NB) loA, hiA = bins[indMampsL], bins[indMampsH] fig = _plt.figure(figsize=(8, 8)) fig.add_subplot(2, 1, 1) _plt.hist(fs[startIt:, 0], bins=_N.linspace(_N.min(fs[startIt:, 0]), _N.max(fs[startIt:, 0]), NB), color="black") _plt.axvline(x=loF, color="red") _plt.axvline(x=hiF, color="red") fig.add_subplot(2, 1, 2) _plt.hist(amps[startIt:, 0], bins=_N.linspace(_N.min(amps[startIt:, 0]), _N.max(amps[startIt:, 0]), NB), color="black") _plt.axvline(x=loA, color="red") _plt.axvline(x=hiA, color="red") if dir is None: _plt.savefig(resFN("chosenFsAmps", dir=setname)) else: _plt.savefig(resFN("%s/chosenFsAmps" % dir, dir=setname)) _plt.close() indsFs = _N.where((fs[startIt:, 0] >= loF) & (fs[startIt:, 0] <= hiF)) indsAs = _N.where((amps[startIt:, 0] >= loA) & (amps[startIt:, 0] <= hiA)) asfsInds = _N.intersect1d(indsAs[0], indsFs[0]) + startIt q = _N.mean(smp_q2[0, startIt:]) #alfas = _N.mean(allalfas[asfsInds], axis=0) pcklme = [aus, q, allalfas[asfsInds], aSs] if dir is None: dmp = open(resFN("bestParams.pkl", dir=setname), "wb") else: dmp = open(resFN("%s/bestParams.pkl" % dir, dir=setname), "wb") pickle.dump(pcklme, dmp, -1) dmp.close()