def binspace(lot, q=1): if q != 1: lot = roundst(lot, q) w = int(trange(lot)[1]) + 1 r = np.zeros((len(lot), w)) for i, t in enumerate(lot): for e in t: r[i, e] += 1 return r
def intlen(lot): ''' Estimate a recording interval for the set of responses in lot (a list of tuples of integers). Return the estimate (a tuple of 2 floats, starttime, stoptime). The estimate is made by first estimating the expected value of the interspike interval, ISI, and then returning the minimum event time minus ISI/2 as the interval start, and the maximum plus ISI/2 as the stop. ''' miv, mav = trange(lot) isi = intervals(lot).mean() return (miv - isi / 2.0, mav + isi / 2.0)
def describe(d, detail=True): ntr = 0 attr = {} mipr = np.inf mapr = -np.inf misp = np.inf masp = -np.inf fstsp = np.inf lstsp = -np.inf for k in d.keys(): tr = d[k] if not type(tr) == gd.Doc: continue ntr += 1 for sk in tr: if sk == 'evts': ts = tshape(tr[sk])[0] ran = trange(tr[sk]) fstsp = min(fstsp, ran[0]) lstsp = max(lstsp, ran[1]) mipr = min(mipr, ts[0]) mapr = max(mapr, ts[0]) if type(ts[1]) == tuple: misp = min(misp, ts[1][0]) masp = max(masp, ts[1][1]) else: misp = min(misp, ts[1]) masp = max(masp, ts[1]) else: if not sk in attr: attr[sk] = set() attr[sk].add(tr[sk]) s = ["%i traces" % (ntr,)] s.append("%i to %i presentations giving %i to %i spikes on [%i, %i]" % (mipr, mapr, misp, masp, fstsp, lstsp)) s.append('Attributes:---') for k in sorted(attr): s.append('%s in %s' % (k, _tupstr(attr[k]))) s.append('--------') if detail: for cond in allv(d, 'condition'): for sa in allv(d, 'stim_atten'): pres = cases(d, condition=[cond], stim_atten=[sa]) s.append('condition %i, attenuation %.3g : %.2g spks/presentation' % (cond, sa, _rpp(pres))) print '\n'.join(s)
def jitter(cond, sdev, insert=0, n=30): ''' add noise to all the spike trains in cond['evts'] (cond is a condition document). Each spike train is duplicated n times, and each duplicate is modified by adding random values from a normal distribution with mean 0 and std dev sdev. If insert is > 0, then it acts as a probability that a spike is added to the train as well (up to 3 spikes can be added, with probability insert, insert**2 insert**3) ''' newc = cond.new() srange = trange(cond['evts']) st = [] ev = [] for i, t in enumerate(cond['evts']): stim = cond['stims'][i] l = len(t) for j in range(n): nt = tuple([int(x) for x in t]) if l and sdev: jit = (np.random.randn(l) * sdev).astype(np.int32) nt = tuple(np.array(nt) + jit) if insert: ads = np.random.random() nns = 0 if ads < insert: nns += 1 if ads < insert ** 2: nns += 1 if ads < insert ** 3: nns += 1 if nns: new = np.random.randint(srange[0], srange[1], nns) nt = tuple(sorted(nt + tuple(new))) ev.append(nt) st.append(stim) newc['evts'] = tuple(ev) newc['stims'] = tuple(st) return newc
def wherediff(q, c1, c2=(), fold=100, sum=True): ''' calculate the vdV histograms for each distance in class c1 and (optionally) between c1 and class c2, with inverse cost q, l auto, and the specified fold. ''' ran = trange(c1 + c2) l = ran[1] + 1 nw, lo = divmod(l, fold) if lo: l = (nw + 1) * fold dvs = [] if c2: #cross class difference for s1 in c1: for s2 in c2: dvs.append(vdV(s1, s2, q, l, fold)) else: for i in range(len(c1) - 1): for s2 in c1[i + 1:]: dvs.append(vdV(c1[i], s2, q, l, fold)) if sum: dvs = np.array(dvs).sum(0) return dvs