예제 #1
0
파일: selectUI.py 프로젝트: gic888/gdblocks
def rrps(cells):
    """
    cells: CellDoc -> (cns:N-[ of s, sns:N-[ of s,
                        hists:{cond1 cond2:N,N-# of x})

    cns is a list of cell names, sns is a list of stimulus names, hists is
    a dictionary of arrays such that hists[c][i,j] is the fraction of total
    responses of cell cns[i] in condition c which occured in respons to stimulus
    sns[j].

    """
    cns = cells.keys(0, 'cell', sort=True)
    stims = stimnames(cells[cns[0]])
    hists = {'cond1': np.zeros((len(cns), len(stims))),
             'cond2': np.zeros((len(cns), len(stims)))}
    for i, cn in enumerate(cns):
        for cond in ['cond1', 'cond2']:
            nts = len(flat(cells[cn][cond]['evts']))
            if not nts:
                continue
            for sno, sname in enumerate(stims):
                evts = rfromstim(cells[cn], cond, sno)
                if evts:
                    rr = float(len(flat(evts))) / float(nts)
                else:
                    rr = 0
                hists[cond][i, sno] = rr
            #hists[cond][i,:] = np.argsort(hists[cond][i,:])
    return (cns, stims, hists)
예제 #2
0
파일: rdj.py 프로젝트: gic888/gdblocks
def showav(d, fig=1, yps=50, btop=100000, bw=2000):
    bins = np.arange(0, btop, bw)
    f = plt.figure(fig)
    plt.clf()
    cnds = dconds(d)
    n = len(cnds)
    yl = 0
    for i, c in enumerate(cnds):
        sp = plt.subplot(1, n, i + 1)
        sp.xaxis.set_visible(False)
        if i > 0:
            sp.yaxis.set_visible(False)
            sp.xaxis.set_visible(False)
        plt.title(c)
        l = len(d[c]['evts'])
        for j in range(l):
            evts = d[c]['evts'][j]
            if evts:
                x = np.array(evts)
                y = np.zeros_like(x) + j
                plt.plot(x, y, marker='.', color='r', linestyle='None')
        z = nhplt(flat(d[c]['evts']), bins, color='r', bottom=l + .2 * yps)
        yl = max(yl, l + 1.3 * yps)
        if d[c]['avgs']:
            z = nhplt(flat(d[c]['avgs']), bins, color='b', bottom=l + .1 * yps)
        if d[c]['dj']:
            for j in range(l):
                evts = d[c]['dj'][j]
                if evts:
                    x = np.array(evts)
                    y = np.zeros_like(x) + j
                    plt.plot(x, y, marker='.', color='g', linestyle='None')
            z = nhplt(flat(d[c]['dj']), bins, color='g', bottom=l + .1 * yps)
        if d[c]['avg'] != None:
            avl = int(l / 2.0)
            if len(d[c]['avg']) == 0:
                plt.axhspan(avl, avl + 1, color='b')
            else:
                x = np.array(d[c]['avg'])
                y = np.zeros_like(x) + avl
                plt.plot(x, y, marker='o', color='b', markersize=10.0, linestyle='None')
                if d[c]['avgvar'] != None:
                    av = d[c]['avgvar']
                    a = [0] + list(x) + [max(flat(d[c]['evts']))]
                    nx = [(a[i] + a[i - 1]) / 2.0 for i in range(1, len(a))]
                    ny = np.zeros_like(nx) + avl
                    ye = ([0] * len(av), [v[4] * yps for v in av])
                    plt.errorbar(nx, ny, yerr=ye, color='k', marker='.',
                                 markersize=4.0, linestyle='None', elinewidth=3)
                    av = av[:-1]
                    xmc = np.array([v[1] for v in av])
                    xe = [v[2] for v in av]
                    ymc = np.array([v[3] * yps for v in av])
                    plt.errorbar(x + xmc, y + ymc, xerr=xe, marker='s', color='b',
                                 markersize=6.0, linestyle='None', elinewidth=3)
    for i in range(len(cnds)):
        sp = plt.subplot(1, n, i + 1)
        plt.ylim([0, yl])
    f.canvas.draw()
예제 #3
0
파일: rdj.py 프로젝트: gic888/gdblocks
def sfate(evts, q, tst=None, track=None, near=False):
    ef = flat(evts)
    tfs = []
    if tst:
        for e in evts:
            tfs.extend(vpd_trans(e, tst, q))
            ntrans = len(evts)
            holes = [0] * (len(tst) + 1)
    else:
        n = len(evts)
        ntrans = 0
        for i in range(n - 1):
            for j in range(i, n):
                ntrans += 1
                tfs.extend(vpd_trans(evts[i], evts[j], q))
        holes = False
    if track == None:
        if tst:
            track = tst
        else:
            track = sorted(set(ef))
    elif type(track) == int:
        ef = flat(evts)
        track = np.linspace(min(ef), max(ef), track)
    track = list(track)
    fates = [[] for _ in range(len(track))]
    for tf in tfs:
        if near:
            tf = list(tf)
            for i in range(len(tf)):
                if tf[i] != -1:
                    tf[i] = track[nearest(track, [tf[i]])[0]]
        if tf[0] == -1:
            try:
                id = track.index(tf[1])
                fates[id].append(np.inf)
            except ValueError:
                pass
        elif tf[1] == -1:
            try:
                id = track.index(tf[0])
                fates[id].append(-np.inf)
            except ValueError:
                if holes:
                    id = np.searchsorted(track, tf[0])
                    holes[id] += 1
        else:
            try:
                id = track.index(tf[0])
                fates[id].append(tf[1] - tf[0])
            except ValueError:
                pass
            try:
                id = track.index(tf[1])
                fates[id].append(tf[0] - tf[1])
            except ValueError:
                pass
    return (track, fates, ntrans, holes)
예제 #4
0
def srate(d):
    rate = float(len(flat(d['evts']))) / len(d['evts'])
    mrate = rate
    st = np.array(d['stims'])
    for s in np.unique(st):
        evts = [d['evts'][i] for i in np.nonzero(st == s)[0]]
        r = float(len(flat(evts))) / len(evts)
        mrate = max(r, mrate)
    return {'max_rate': mrate, 'mean_rate': rate}
예제 #5
0
파일: tests.py 프로젝트: gic888/gdblocks
def addpois(d, conds, l=200):
    td = gd.Doc()
    for k in conds:
        td[k] = d[k]
        arate = float(len(flat(d[k]['evts']))) / len(d[k]['evts'])
        arate = arate / (l / 1e6)
        #report (arate)
        stims = d[k]['stims']
        mi = AC(l, arate)
        td[k + '_hp'] = {'stims': stims, 'evts': [mi(st) for st in stims]}
        td[k + '_hp.rate'] = arate
    td['docsource'] = d['docsource']
    return td
















    #
예제 #6
0
파일: explore.py 프로젝트: gic888/gdblocks
def histpl(d, k, bw=4.0, rng=(0, 200.0)):
    evts = d[k + '.evts']

    evts = np.array(flat(evts))
    evts = evts / 1000.0
    nb = (rng[1] - rng[0]) / bw

    plt.hist(evts, nb, rng)
예제 #7
0
파일: explore.py 프로젝트: gic888/gdblocks
def _rpp(d):
    npres = 0
    nspks = 0
    for k in d:
        if d[k]['evts']:
            npres += len(d[k]['evts'])
            nspks += len(flat(d[k]['evts']))
    if npres == 0:
        return -1
    return float(nspks) / float(npres)
예제 #8
0
파일: rdj.py 프로젝트: gic888/gdblocks
def optq(evts, mode='med-vdps', reps=1, minq=1000, qstep=1000):
    q = minq - qstep
    expt = round(float(len(flat(evts))) / len(evts))
    nm = 0
    while nm < expt:
        print(nm, expt, q)
        q = q + qstep
        m = calcav(evts, mode, q, reps, True)
        nm = len(m)
    return q
예제 #9
0
파일: rdj.py 프로젝트: gic888/gdblocks
def rburst(n=200, prate=10):
    dur = 100000
    d = gd.Doc()
    mp = T.AC(dur, prate, 1, 3)
    d['condpois'] = T.draw_single(mp, 0, n)
    m = T.BURST(dur, 1, 15000, 1, sd=1000, latsd=2000, mfst=10000, sprob=.8, slp=2, mpt=3)
    b = T.draw_single(m, 0, n)
    rt = 1e6 * len(flat(b['evts'])) / float(len(b['evts'])) / dur
    print('burst1 rate', rt)
    p = T.draw_single(mp, 0, n)
    rt = 1e6 * len(flat(p['evts'])) / float(len(p['evts'])) / dur
    print('pois rate', rt)
    d['condb1'] = combinedraws(b, p)
    m = T.BURST(dur, 1, 10000, 1, sd=2000, latsd=2000, mfst=15000, sprob=.9, slp=3, mpt=2)
    b = T.draw_single(m, 0, n)
    rt = 1e6 * len(flat(b['evts'])) / float(len(b['evts'])) / dur
    print('burst2 rate', rt)
    p = T.draw_single(mp, 0, n)
    d['condb2'] = combinedraws(b, p)
    return d
예제 #10
0
def spikes_from(cond, stim, flatten=True):
    """
    return a flat list of every spike in cond['evts'] resulting from stmulus
    stim (int).

    """
    resps = [cond['evts'][i] for i in range(len(cond['evts']))
             if cond['stims'][i] == stim]
    if flatten:
        return flat(resps)
    else:
        return resps
예제 #11
0
파일: rdj.py 프로젝트: gic888/gdblocks
def ll_et(evts, bw=2000):
    nt = len(evts)
    evts = flat(evts)
    spp = float(len(evts)) / nt
    bins = cover(evts, bw)
    h = np.histogram(evts, bins=bins, normed=True)
    h0 = h[0] * spp * bw
    zi = np.searchsorted(h[1], evts, 'right') - 1
    #	for i in range(len(h0)):
    #		print(h[0][i], (zi==i).sum())
    ll2 = np.log(h0[zi]).sum()
    return (ll2 / nt, (h0, h[1]))
예제 #12
0
파일: rdj.py 프로젝트: gic888/gdblocks
def nburst(n=100, rt=15.0, dur=100, prec=15, sd=2000, latsd=5000, slp=2, mpt=3, sprob=.9):
    dur = int(dur * 1000)
    prec = int(prec * 1000)
    m = T.BURST(dur, rt, prec, 1, sd=sd, latsd=latsd, sprob=sprob, slp=slp, mpt=mpt)
    d = gd.Doc()
    d['condburst'] = T.draw_single(m, 0, n)
    rt = d['condburst.evts']
    rt = len(flat(rt)) / float(len(rt))
    rt = rt * 1e6 / dur
    print(rt)
    m = T.AC(dur, rt, prec, 3)
    d['condhpois'] = T.draw_single(m, 1, n)
    return d
예제 #13
0
파일: rdj.py 프로젝트: gic888/gdblocks
def dj_hist(evts, q, bw=2000, nsteps=4, insp=0):
    ll, sph = ll_et(evts, bw)
    ll = ll / float(len(flat(evts)))
    while True:
        start = sph[1][0]
        end = sph[1][-1]
        tp = transprob_hist(evts, q, bw)
        nevts = []
        for e in evts:
            ne = []
            for st in e:
                bid = nearest(tp[:, 0], [st])[0]
                sm, ssd, dp = tp[bid, 1:-1]
                #be careful, if the implementation of "cover" used by transprob_hist and
                #ll_et isn't exactly the same, this doesn't work
                sp = sph[0][bid]
                #check for deletion
                if (1 - sp) * dp * insp > sp:
                    continue
                shifts = np.arange(-nsteps, nsteps + 1) * bw
                shifts = np.array([s for s in shifts if s + st > start and s + st < end])
                lc = [hdraw(sph, st + s) * gausact(sm, ssd, s, bw) for s in shifts]
                opts = shifts[np.argmax(lc)]
                ne.append(st + opts)
            nevts.append(sorted(ne))
        nnspk = len(flat(nevts))
        if not nnspk:
            break
        nll, sph = ll_et(nevts, bw)
        nll = nll / nnspk
        if nll <= ll:
            break
        evts = nevts
        print(ll, '->', nll)
        ll = nll
    return evts
예제 #14
0
def superpose(io, n, israte=False, ct=.3):
    if israte:
        rt = float(len(flat(io['evts']))) / len(io['evts'])
        n, rem = divmod(n / rt, 1)
        if rem > ct:
            n += 1
        if n < 2:
            return io
    ss = stimsets(io)
    stims = []
    evts = []
    for s in ss:
        for g in groupevts(ss[s], n):
            stims.append(s)
            evts.append(g)
    return {'stims': stims, 'evts': evts, 'ncomb': n}
예제 #15
0
 def run(self, pars, out, messages):
     d = pars['doc']
     xvar = pars['xvar']
     r = pars['r']
     out.patch(gd.Doc({'xvar': xvar, 'docsource': d['docsource']}))
     for cond in d.keys(0, 'cond'):
         out[cond] = {'nspikes': len(flat(d[cond]['evts'])),
                      'npres': len(d[cond]['evts'])}
         sc = r.findall({'_params.io': '=->%s' % cond})
         if sc:
             out[cond]['params'] = r[sc[0]]['_params']
         m = {}
         for k in sc:
             x = r[k]['_params'][xvar]
             m[x] = r[k]['mi'] + [r[k]['stiment']]
         out[cond]['x'] = np.array(sorted(m))
         out[cond]['y'] = np.array([m[i] for i in out[cond]['x']])
예제 #16
0
def response_densities(cond, mcent=8, nrep=3, stims=None,
                       sppt=163000, dsd=10000):
    """
    return a list l such that l[i] = is a gmm of the responses to stimulus i.

    Since these are 1D models, the underlying mixmod call always uses PkLkCk,
    but mcent specifies the max number of centers to try, nrep the number of
    repeats to use.

    stims may be a list, which restricts the set of stimuli that are modeled
    (by default, it is all presented stimuli).

    If the number of spikes evoked by a stimulus is very small, mixmod errors
    will result. This function will not try to calculate a model based on fewer
    than 2 spikes per center, reguardless of the value of mcent. For response
    groups with at least two spikes, mcent may be reduced. Responses with no
    spikes are modeled with a 0-component model (the dictionary
    {'components':(), 'support':sppt}. mixmod.evaluate on such a model will
    return a uniform probability if 1/sppt, so this behavior is equivalent to a
    uniform prior over a region of size sppt. Responses with exactly 1 spike are
    modeled as a single center, with mean at the time of that spike, and
    standard deviation specified by the free parameter dsd.

    This function adds the key "responses" to the resulting model dictionaries,
    containing the source spike trains

    """
    if stims is None:
        stims = set(cond['stims'])
    l = [None for _ in range(max(stims) + 1)]
    for s in stims:
        sts = spikes_from(cond, s, False)
        sf = flat(sts)
        if len(sf) > 1:
            mc = int(min(mcent, np.ceil(len(sf) / 2.0)))
            l[s] = mmcall(np.array(sf)[:, np.newaxis], range(1, mc + 1),
                          reps=nrep)
        elif sf:
            l[s] = {'proportions': (1.0,), 'c0': {'cov': ((dsd ** 2,),),
                                                  'mean': (sf[0],)},
                    'bic': 0, 'components': ('c0',), 'partition': (0,)}
        else:
            l[s] = {'components': (), 'support': sppt}
        l[s]['responses'] = sts

    return l
예제 #17
0
파일: rdj.py 프로젝트: gic888/gdblocks
def showpartitions(parts, evts, av="med-vdps", aq=15000, hists=True):
    '''
    Plot one or more partitions of a set of events. These are plotted as rasters,
    with events in each group of the partition shown together in a given color.
    Different partitions are shown in different subplots. "parts" should be a
    list of arrays, each specifing a partition. "evts" is a list of event
    sequences, av may be a False value, or a method string accepted by calcav. If
    it is specified, each group in the partition is also represented by an
    average calculated using this method, and "aq" as the precision parameter. If
    "hists" is true, the groups are represented with historgrams as well as
    rasters

    '''
    f = plt.figure(1)
    plt.clf()
    for pi, part in enumerate(parts):
        plt.subplot(1, len(parts), pi + 1)
        pids = np.unique(part)
        y = 0
        pcols = {}
        for i, pid in enumerate(pids):
            pcols[pid] = (COLORS[i], y)
            pevts = [evts[i] for i in range(len(evts)) if part[i] == pid]
            for j, e in enumerate(pevts):
                x = np.array(e)
                yc = np.zeros_like(x) + y
                plt.plot(x, yc, marker='.', color=pcols[pid][0], linestyle='None')
                y += 1
            if av:
                yav = float(pcols[pid][1] + y) / 2
                x = calcav(pevts, av, aq, 1, True)
                if len(x) == 0:
                    plt.axhspan(yav - .2, yav + .2, color=pcols[pid][0])
                else:
                    x = np.array(x)
                    yc = np.zeros_like(x) + yav
                    plt.plot(x, yc, marker='o', color=pcols[pid][0], markersize=10.0, linestyle='None')
        yz = 0
        if hists:
            for pid in pids:
                pevts = [evts[i] for i in range(len(evts)) if part[i] == pid]
                z = nhplt(flat(pevts), 50, color=pcols[pid][0], bottom=y + 1, scale=y)
                yz = max(z, yz)
        plt.ylim([-1, y + 2 + yz])
    f.canvas.draw()
예제 #18
0
파일: tests.py 프로젝트: gic888/gdblocks
def addtests(d, l=200, match='cond1', q=10000, mods=(AC, TGC), sp={}):
    td = gd.Doc()
    iod = d[match]
    arate = float(len(flat(iod['evts']))) / len(iod['evts'])
    arate = arate / (l / 1e6)
    report(arate)
    stims = iod['stims']
    nstims = np.unique(stims).shape[0]
    l = int(l)
    td[match] = iod
    for k in d:
        if k.startswith('cond') and k != match:
            td[k] = d[k]
    for M in mods:
        kw = sp.get(M.name, {})
        mi = M(l, arate, q, nstims, **kw)
        td['cond' + M.name] = {'stims': stims, 'evts': [mi(st) for st in stims]}
    td['docsource'] = {'cell': d['docsource.cell'] + '+tests',
                       'pars': d['docsource.pars'] + (l, match, q)}
    return td
예제 #19
0
파일: rdj.py 프로젝트: gic888/gdblocks
def transprob_hist(evts, q, bw=2000):
    bins = cover(flat(evts), bw)
    n = len(evts)
    nt = (n ** 2 - n) / 2
    tfs = []
    for i in range(n - 1):
        for j in range(i, n):
            tfs.append(vpd_trans(evts[i], evts[j], q))
    fates = []
    for _ in range(len(bins)):
        fates.append(np.zeros((len(tfs), 2)))
    for i, trans in enumerate(tfs):
        for tf in trans:
            if tf[0] == -1:
                id = nearest(bins, [tf[1]])[0]
                fates[id][i, 1] = 1
            elif tf[1] == -1:
                id = nearest(bins, [tf[0]])[0]
                fates[id][i, 1] = 1
            else:
                id1 = nearest(bins, [tf[0]])[0]
                id2 = nearest(bins, [tf[1]])[0]
                dist = tf[1] - tf[0]
                fates[id1][i, 0] = dist
                fates[id2][i, 0] = -dist
    res = np.zeros((len(bins), 5))
    for i in range(len(bins)):
        res[i, 0] = bins[i]
        ns = float((fates[i][:, 0] != 0).sum() + (fates[i][:, 1] != 0).sum())
        res[i, 4] = ns / nt
        v = [x for x in fates[i][:, 0] if x != 0]
        if len(v) < ns:
            v = v + [0] * int(ns - len(v))
        v = np.array(v)
        res[i, 1] = v.mean()
        res[i, 2] = v.std()
        res[i, 3] = fates[i][:, 1].sum() / ns
    return res
예제 #20
0
파일: tests.py 프로젝트: gic888/gdblocks
def tr(l=100000, r=1.0):
    a = AC(l, r)
    evts = [a(0) for i in range(1000)]
    report(len(flat(evts)) / 1000.0)