Example #1
0
def getcell(cellid, dname='bicIC', sheet='fulldatasheet.ods', tab='sheet1.table', cellidlab='cell',
            mouseid='data', testn='test', cond='condition', pstdirp='Mouse ', redundantdirs=True, condgroup=int,
            stimchan='stim.ch0', shift=True):
    '''
    Return a data document for a single cell. Parameters are the same as for condition, except for cellid, which
    is an integer indicating which cell to get. As long as "fulldatasheet.ods" is present in the named "dname", and
    lists all available cells in Zach/Graham format, then no parameters other than cellid and dname should need to be
    changed.
    '''
    dname = os.path.join(BASEDIR, dname)
    ds = gio.read(os.path.join(dname, sheet))[tab]
    l = list(ds[0, :])
    try:
        cellcol = [i for (i, s) in enumerate(l) if s.lower().startswith(cellidlab)][0]
        mousecol = [i for (i, s) in enumerate(l) if s.lower().startswith(mouseid)][0]
        testcol = [i for (i, s) in enumerate(l) if s.lower().startswith(testn)][0]
        condcol = [i for (i, s) in enumerate(l) if s.lower().startswith(cond)][0]
    except IndexError:
        raise KeyError('Data sheet doesnt contain the specified columns')
    clines = [(ds[i, mousecol], ds[i, testcol], ds[i, condcol]) for i in range(ds.shape[0]) if
              ds[i, cellcol] == str(cellid)]
    if not clines:
        raise KeyError('No tests in this data sheet for this cell')
    if not all([clines[i][0] == clines[0][0] for i in range(1, len(clines))]):
        report('WARNING: single cell reported in multiple recording tracks !!??')
        sd = gd.Doc()
        for mn in set([clines[i][0] for i in range(len(clines))]):
            pstn = pstdirp + mn + '.pst'
            if redundantdirs:
                pstn = os.path.join(pstdirp + mn, pstn)
            pstn = os.path.join(dname, pstn)
            sd = sd.fuse(gio.read(pstn))
    else:
        pstn = pstdirp + clines[0][0] + '.pst'
        if redundantdirs:
            pstn = os.path.join(pstdirp + clines[0][0], pstn)
        pstn = os.path.join(dname, pstn)
        sd = gio.read(pstn)
    d = gd.Doc()
    for l in clines:
        tests = _numOrRan(l[1])
        for it in tests:
            otn = 'test%i' % it
            trd = traces(sd, otn, stimchan, shift)
            for trn in trd:
                tr = trd[trn]
                tr['condition'] = condgroup(l[2])
                tr['cell'] = cellid
                nn = "te%itr%i" % (it, trn)
                d.set(nn, tr)
    return d
Example #2
0
def getTests(dsheet=None, bdir=basedir):
    files = getDsheet(dsheet)
    doc = gd.Doc()
    testk = re.compile('test(\d+)')
    for f in files:
        ns = "track%s" % f
        mouse = numOrRan(f)[0]
        dfpath = os.path.join(bdir, pathprefix + str(mouse), pathprefix + f)
        pstpath = os.path.join(dfpath, pathprefix + f + '.pst')
        if not os.path.isfile(pstpath):
            report("No data for %s" % f)
            continue
        else:
            report("found data for %s" % f)
        ld = gio.read(pstpath)
        tids = {}
        for tests in files[f]:
            ids = numOrRan(tests[testLab])
            for i in ids:
                tids[i] = tests
        found = set()
        for k in ld:
            m = testk.match(k)
            if m:
                i = int(m.groups()[0])
                if i in tids:
                    doc[ns + '.' + k] = ld[k]
                    found.add(i)
        for i in found:
            n = ns + '.test%i.' % i
            for k in tids[i]:
                doc[n + k] = tids[i][k]
    return doc
Example #3
0
def reduce(opts, args):
    '''
    Generates a document with a reduced representation of cell I/O properties,
    and save this to disk. Usage is:

    reduce [-d path] outfilename

    outfilename specifies where to save the output. The type of this file is
    inferred from its extension (if there is no "." in outfilename, ".gic" is
    used by default).

    If -d is specified, 'path' should be a file path to a document of the
    sort written by the "get" subcommand. Specifying this saves the time
    required to do the raw data extraction, and is also required if you
    wanted to pass non-default arguments to get. If -d is ommitted, the
    data document is constructed as though you called "get" with default
    options (but this document is not saved to a file)

    '''
    fname = args[0]
    if 'd' in opts:
        doc = gio.read(opts['d'])
    else:
        doc = getTests()
    doc = reducedDoc(doc)
    if not '.' in fname:
        fname = fname + ".gic"
    gio.write(doc, fname)
Example #4
0
def csv(opts, args):
    '''
    Write processed cell data to csv files.

    usage is:
    csv [-d path] [-t] cid [cid ...]

    If -d is specified, 'path' should be a file path to a document of the
    sort written by the "get" or "reduce" subcommand. Specifying this saves the
    time required to do the raw data extraction, and is also required if you
    wanted to pass non-default arguments to get. If -d is ommitted, the data
    document is constructed as though you called "get" and then "reduce" with
    default options (but this document is not saved to a file). If -d is
    specified, this function will check for an element "params" in the
    document. If it is present, the document is assumed to be reduced.
    Otherwise, it is assumed to be raw. This works if the document was
    generated by this module, but may not always be safe. If you have problems,
    call "reduce" explicitly to produce the target document.

    If -t is specified, the data will be for "tone" stimuli (tuning
    curve type tests), and if not, the data will include controls
    and tests using file type stimuli (calls).

    The "cid"s should be numbers, such as 1, 2 , 3. You may specify any
    list of them. The result will be 4 csv files written to disk in the
    current directory for each number. These will have names:
    cellNnodrug_spikes.csv, cellNnodrug_trials.csv, cellNwithdrug_spikes.cvs,
    and cellNwithdrug_trials.csv, where "N" is the cid.

    The "withdrug" and "nodrug" files specify different experimental
    conditions ("with" contains data from tests where conditionLab is "yes"
    and "no" contains test where it was "no". conditionLab is currently
    'Bic + Strych'). The "trials" files have exactly one line for each trial
    recorded. This contains, in the first column, the trial ID (a number that
    should increase by exactly 1 on each successive line), and in column 2 the
    stimulus ID used in that trial (a number that should be between 0 and 16
    inclusive, is not monatonically increasing, and will probably remain the
    same for 20 or 30 consecutive trials).

    The "spikes" files contain one line for each recorded spike, and
    contain, in the first column, the trial ID (which corresponds to the
    same number in the trials file, so allows you to look up the stimulus
    used), and in the second column the spike time (in microseconds after
    recording onset).

    '''
    tones = 't' in opts
    if 'd' in opts:
        doc = gio.read(opts['d'])
    else:
        doc = getTests()
    if not 'n0.evts' in doc:
        doc = reducedDoc(doc)
    for cid in args:
        cid = int(cid)
        sgs = sgroups(doc, cid, tones)
        sgroups2csv(sgs, "cell" + str(cid))
Example #5
0
def getDsheet(dsheet=None):
    if not dsheet:
        dsheet = dsheet_path
    tab = gio.read(dsheet)['sheet1.table']
    lab = dict([(str(tab[0, i]), i) for i in range(tab.shape[1]) if tab[0, i]])
    files = {}
    for row in range(1, tab.shape[0]):
        cid = tab[row, lab[cellIdLab]].strip()
        if cid in ['', 'Not complete']:
            continue
        mouse = tab[row, lab[mouseNoLab]]
        if not mouse in files:
            files[mouse] = []
        files[mouse].append(dict([(l, tab[row, lab[l]]) for l in lab]))
    return files
Example #6
0
def save_spectrogram(stim, stim_info, npts=500, ar=.15, time_window=(0, 200), save="pdf"):
    infilename = os.path.join(ti.STIMDIR, stim_info['file'])
    stim_name = os.path.splitext(stim_info['file'])[0]
    data = gio.read(infilename)['data'][:, 0]
    fs = SPEC['fs'] / 1000.0
    lead_pad = np.zeros((0 - time_window[0]) * fs)
    tail_pad = np.zeros((time_window[1] - stim_info['duration']) * fs)
    data = np.concatenate([lead_pad, data, tail_pad])
    y = int(round(npts * ar))
    f = get_fig()
    plt.subplot(111, frameon=False)
    vis.make_spectrogram(data, npts, **SPEC)
    a = f.get_axes()[0]
    a.xaxis.set_visible(False)
    a.yaxis.set_visible(False)
    f.subplots_adjust(left=0, right=1, bottom=0, top=1)
    f.canvas.draw()
    outfile = os.path.join(OUTDIR, "{}_{}_spectrogram.{}".format(stim_name, stim, save))
    f.savefig(open(outfile, 'wb'), format=save)
    return outfile
Example #7
0
def condition(dname='bicIC', sheet='datasheet.ods', tab='sheet1.table', cellid='cell', mouseid='data',
              testn='test', cond='condition', pstdirp='Mouse ', redundantdirs=True, writefile='conditioned.gic',
              condgroup=int, stimchan='stim.ch0'):
    '''
    Return a data document containing every test specified in the data sheet
    "sheet". "dname" indicates a directory (subdirectory of BASEDIR) to search for
    the data sheet and data files (or "mouse" directories).

    tab, cellid, mouseid, testn, and cond specify the layout of the sheet (which
    table is used and what the column labels for the relevant metadata are. These
    can be left as default values if using the sort of sheet Zach Mayko and I
    (Graham Cummins) have typically been using. Pstdirp and redundantdirs specify
    the layout of the data directory structure, and again can remain at default if
    your structure looks like Christine Portfor's typical layout. Writefile
    specifies a file name to store the new document in (or omit it to not save the
    doc).

    condgroup is a function that is applied to the value of the "condition" metadata
    The default (cast to int) is OK if condition labels are integers, and all distinctly
    labeled conditions should be treated differently (otherwise, use a function that
    makes similarity classes in some way, using the raw values of "condition").

    Stimchan specifies where the relevant stimulus data is stored by Batlab.
    This code assumes a single channel stimulus on channel 0. Although this
    parameter can be changed to deal with single channels on some other
    channel, any significant change to the stimulus will probably break
    most code in this module.

    '''
    dname = os.path.join(BASEDIR, dname)
    ds = gio.read(os.path.join(dname, sheet))[tab]
    l = list(ds[0, :])
    try:
        cellcol = [i for (i, s) in enumerate(l) if s.lower().startswith(cellid)][0]
        mousecol = [i for (i, s) in enumerate(l) if s.lower().startswith(mouseid)][0]
        testcol = [i for (i, s) in enumerate(l) if s.lower().startswith(testn)][0]
        condcol = [i for (i, s) in enumerate(l) if s.lower().startswith(cond)][0]
    except IndexError:
        raise KeyError('Data sheet doesnt contain the specified columns')
    d = gd.Doc()
    dfiles = {}
    for i in range(1, ds.shape[0]):
        l = ds[i, :]
        try:
            cid = int(l[cellcol])
            mid = l[mousecol]
            cond = l[condcol]
            if condgroup:
                cond = condgroup(cond)
            tests = _numOrRan(l[testcol])
        except:
            report('failed to parse data sheet line %i' % i)
            continue
        if not mid in dfiles:
            pstn = pstdirp + mid + '.pst'
            if redundantdirs:
                pstn = os.path.join(pstdirp + mid, pstn)
            pstn = os.path.join(dname, pstn)
            try:
                dfiles[mid] = gio.read(pstn)
                report('loading %s' % pstn)
            except:
                report('failed to open data file for %s (tried path %s)' % (mid, pstn))
                dfiles[mid] = 'BAD'
        if dfiles[mid] == 'BAD':
            continue
        metas = {'animal': mid, 'condition': cond, 'cell': cid}
        for it in tests:
            otn = 'test%i' % it
            try:
                trd = traces(dfiles[mid], otn, stimchan)
            except:
                report("Cant find a test %s for penetration %s" % (otn, mid))
                continue
            for trn in trd:
                tr = trd[trn]
                tr.update(metas)
                nn = "r%s_te%itr%i" % (mid, it, trn)
                d.set(nn, tr)
    if writefile:
        gio.write(d, os.path.join(dname, writefile))
    return d
Example #8
0
    f = plt.figure(1)
    plt.clf()
    extent = [min(tints), max(tints), max(attens), min(attens)]
    aspect = .8 * (extent[1] - extent[0]) / (extent[2] - extent[3])
    plt.imshow(out.transpose(), aspect=aspect, extent=extent, interpolation='nearest')
    plt.colorbar()
    return out


def plotTC(d, tc):
    c = cases(d, stim_type=stimtones(d)[1])
    tones = allv(d, 'stim_type')
    tints = sorted([int(t.split()[1]) for t in tones])
    attens = sorted(allv(c, 'stim_atten'))


if __name__ == '__main__':
    r = gio.read('reduced.gic')
    calls, tones = stimtones(r)
    rc = cases(r, stim_type=calls)
    rastersbycell(rc, 'calls')
#	rt = cases(r, stim_type=tones)
#	print('calls')
#	manyrasters(rc)
#	print('tones')
#	manyrasters(rt, lab='tones')