Esempio n. 1
0
def get_waveforms(fn_mwk, fn_nev, npts=N_SNIPPET_PTS, c_v=VOLT_CONV,
                  verbose=True):
    wv = {}
    wvsq = {}
    nelec = cl.defaultdict(int)
    iimg0 = None

    # -- collect snippets for each image presentation
    # override_elecs: process only these electrodes. 1-based.
    for arg in ss.getspk(fn_mwk, fn_nev):
        # -- preps
        wav = np.array(arg['wav']['unsorted']) * c_v      # unsorted raw snippet
        wavsq = wav * wav
        ch = arg['ch']; t_rel = arg['t_rel']
        t0 = arg['t_imgonset']; iid = arg['imgid']; iimg = arg['iimg']
        if verbose:
            if iimg0 == None: iimg0 = iimg
            if iimg != iimg0:
                n = nelec[1]
                print 'At: %d ([1]: %f)' % (iimg, np.mean(np.sqrt(wvsq[1]/n - wv[1]*wv[1]/n/n)))
                iimg0 = iimg
        # -- main
        nelec[ch] += 1
        if not wv.has_key(ch): wv[ch] = wav
        else: wv[ch] += wav
        if not wvsq.has_key(ch): wvsq[ch] = wavsq
        else: wvsq[ch] += wavsq
        
    for ch in nelec:
        wv[ch] /= nelec[ch]
        wvsq[ch] /= nelec[ch]
     
    return wv, wvsq
Esempio n. 2
0
def get_firrate(fn_mwk, fn_out, proc_cluster=PROC_CLUSTER, max_clus=MAX_CLUS, \
        movie_begin_fname=None, **kwargs):
    """Get spiking data around stimuli presented."""

    # some housekeeping things...
    kwargs['verbose'] = 2
    t_start0 = kwargs['t_start0']
    t_stop0 = kwargs['t_stop0']

    # all_spike[chn_id][img_id]: when the neurons spiked?
    all_spike = {}
    all_foffset = {}
    clus_info = {}

    frame_onset = {}
    movie_iid = None
    movie_onsets = []
    movie_onset0 = 0

    for info in getspk(fn_mwk, fn_nev=None, **kwargs):
        # -- get the metadata. this must be called before other clauses
        if info['type'] == 'preamble':
            actvelecs = info['actvelecs']
            t_adjust = info['t_adjust']

        # -- do some housekeeping things once per each img
        elif info['type'] == 'begin':
            t0 = info['t_imgonset']
            iid = info['imgid']

            # process movie if requested
            if movie_begin_fname is not None:
                # begin new clip?
                if movie_begin_fname in iid:
                    # was there previous clip?
                    if movie_iid is not None:
                        if movie_iid not in frame_onset:
                            frame_onset[movie_iid] = []
                        frame_onset[movie_iid].append(movie_onsets)
                    # init for new clip
                    movie_onsets = []
                    iid = movie_iid = iid.replace(movie_begin_fname, '')
                    movie_onset0 = t0
                    movie_onsets.append(0)
                elif movie_iid is not None:
                    movie_onsets.append(t0 - movie_onset0)
                    continue

            # prepare the t_rel & foffset
            actvunits = {}
            t_rel = {}
            foffset = {}

            for ch in actvelecs:
                # if no clustering info is used...
                if not proc_cluster:
                    t_rel[ch] = []
                    foffset[ch] = []
                    continue
                # if clustering info is used...
                cids = range(max_clus)
                actvunits[ch] = cids
                for cid in cids:
                    t_rel[(ch, cid)] = []
                    foffset[(ch, cid)] = []

        # -- put actual spiking info
        elif info['type'] == 'spike':
            ch = info['ch']

            if proc_cluster:
                cid = info['cluster_id']
                key = (ch, cid)
            else:
                key = ch

            t_rel[key].append(int(info['t_rel']))
            foffset[key].append(int(info['pos']))
            # update the clus_info and n_cluster
            if proc_cluster and key not in clus_info:
                cvalue = info['cluster_value']
                clus_info[key] = cvalue
                if cvalue['nclusters'] > max_clus:
                    raise ValueError, '*** Unit %s: max_cluster(=%d) is '\
                            'smaller than the actual number of clusters(=%d)!'\
                            % (str(key), max_clus, cvalue['nclusters'])

        # -- finalize info for the image
        elif info['type'] == 'end':
            for el in actvelecs:
                if proc_cluster:
                    cids = actvunits[el]
                else:
                    cids = [-1]
                for cid in cids:
                    if proc_cluster:
                        key = (el, cid)
                    else:
                        key = el
                    if key not in all_spike:
                        # not using defaultdict here:
                        # all_spike[key] = defaultdict(list)
                        all_spike[key] = {}
                        all_foffset[key] = {}
                    if iid not in all_spike[key]:
                        all_spike[key][iid] = []
                        all_foffset[key][iid] = []
                    all_spike[key][iid].append(t_rel[key])
                    all_foffset[key][iid].append(foffset[key])

    # -- done!
    # flush movie data
    if movie_iid is not None:
        if movie_iid not in frame_onset:
            frame_onset[movie_iid] = []
        frame_onset[movie_iid].append(movie_onsets)

    # finished calculation....
    f = open(fn_out, 'w')
    out = {'all_spike': all_spike,
           't_start': t_start0,
           't_stop': t_stop0,
           't_adjust': t_adjust,
           'actvelecs': actvelecs}
    if proc_cluster:
        out['clus_info'] = clus_info
        out['max_clus'] = max_clus
    if movie_begin_fname is not None:
        out['frame_onset'] = frame_onset
    pk.dump(out, f)

    # put all_foffset into the 2nd half to speed up reading
    out2 = {'all_foffset': all_foffset}
    pk.dump(out2, f)

    f.close()
Esempio n. 3
0
def get_waveform(fn_mwk, fn_nev, fn_out, movie_begin_fname=None, \
        n_samples=DEFAULT_SAMPLES_PER_SPK, n_max_spks=DEFAULT_MAX_SPKS, \
        **kwargs):
    """Get waveform data around stimuli presented for later spike sorting.
    This will give completely different output file format.
    NOTE: this function is memory intensive!  Will require approximately
    as much memory as the size of the files."""

    # -- some housekeeping things...
    kwargs['verbose'] = 2
    kwargs['only_new_t'] = True
    t_start0 = kwargs['t_start0']
    t_stop0 = kwargs['t_stop0']
    # these two are unused
    kwargs.pop('proc_cluster', None)
    kwargs.pop('max_clus', None)

    iid2idx = {}
    idx2iid = []
    ch2idx = {}
    idx2ch = []
    n_spks = 0

    # does "n_spks_lim" reach "n_max_spks"?
    b_warn_max_spks_lim = False
    # list of image presentations without spikes
    l_empty_spks = []

    for info in getspk(fn_mwk, fn_nev=fn_nev, **kwargs):
        # -- get the metadata. this must be called before other clauses
        if info['type'] == 'preamble':
            actvelecs = info['actvelecs']
            t_adjust = info['t_adjust']
            chn_info = info['chn_info']
            n_spks_lim = min(info['n_packets'], n_max_spks)
            print '* n_spks_lim =', n_spks_lim

            for ch in sorted(actvelecs):
                makeavail(ch, ch2idx, idx2ch)

            # Data for snippets ===
            # Msnp: snippet data
            # Msnp_tabs: when it spiked (absolute time)
            # Msnp_ch: which channel ID spiked?
            # Msnp_pos: corresponding file position
            Msnp = np.empty((n_spks_lim, n_samples), dtype='int16')
            Msnp_tabs = np.empty(n_spks_lim, dtype='uint64')
            Msnp_ch = np.empty(n_spks_lim, dtype='uint32')
            Msnp_pos = np.empty(n_spks_lim, dtype='uint64')

            # Data for images ===
            # Mimg: image indices in the order of presentations
            # Mimg_tabs: image onset time (absolute)
            Mimg = []
            Mimg_tabs = []

        # -- do some housekeeping things once per each img
        if info['type'] == 'begin':
            t_abs = info['t_imgonset']
            iid = info['imgid']
            i_img = info['i_img']

            makeavail(iid, iid2idx, idx2iid)
            Mimg.append(iid2idx[iid])
            Mimg_tabs.append(t_abs)
            b_no_spks = True

            # process movie if requested
            if movie_begin_fname is not None:
                raise NotImplementedError('Movies are not supported yet.')

        # -- put actual spiking info
        elif info['type'] == 'spike':
            wav = info['wavinfo']['waveform']
            t_abs = info['t_abs']
            i_ch = ch2idx[info['ch']]
            pos = info['pos']

            Msnp[n_spks] = wav
            Msnp_tabs[n_spks] = t_abs
            Msnp_ch[n_spks] = i_ch
            Msnp_pos[n_spks] = pos
            b_no_spks = False

            n_spks += 1
            if n_spks >= n_spks_lim:
                warnings.warn('n_spks exceedes n_spks_lim! '\
                    'Aborting further additions.')
                b_warn_max_spks_lim = True
                break

        elif info['type'] == 'end':
            if not b_no_spks:
                continue
            # if there's no spike at all, list the stim
            warnings.warn('No spikes are there!       ')
            l_empty_spks.append(i_img)

    # -- done!
    # finished calculation....
    Msnp = Msnp[:n_spks]
    Msnp_tabs = Msnp_tabs[:n_spks]
    Msnp_ch = Msnp_ch[:n_spks]
    Msnp_pos = Msnp_pos[:n_spks]

    Mimg = np.array(Mimg, dtype='uint32')
    Mimg_tabs = np.array(Mimg_tabs, dtype='uint64')

    filters = tbl.Filters(complevel=4, complib='blosc')
    t_int16 = tbl.Int16Atom()
    t_uint32 = tbl.UInt32Atom()
    t_uint64 = tbl.UInt64Atom()

    h5o = tbl.openFile(fn_out, 'w')
    CMsnp = h5o.createCArray(h5o.root, 'Msnp', t_int16, \
            Msnp.shape, filters=filters)
    CMsnp_tabs = h5o.createCArray(h5o.root, 'Msnp_tabs', t_uint64, \
            Msnp_tabs.shape, filters=filters)
    CMsnp_ch = h5o.createCArray(h5o.root, 'Msnp_ch', t_uint32, \
            Msnp_ch.shape, filters=filters)
    CMsnp_pos = h5o.createCArray(h5o.root, 'Msnp_pos', t_uint64, \
            Msnp_pos.shape, filters=filters)

    CMsnp[...] = Msnp
    CMsnp_tabs[...] = Msnp_tabs
    CMsnp_ch[...] = Msnp_ch
    CMsnp_pos[...] = Msnp_pos

    h5o.createArray(h5o.root, 'Mimg', Mimg)
    h5o.createArray(h5o.root, 'Mimg_tabs', Mimg_tabs)

    meta = h5o.createGroup('/', 'meta', 'Metadata')
    h5o.createArray(meta, 't_start0', t_start0)
    h5o.createArray(meta, 't_stop0', t_stop0)
    h5o.createArray(meta, 't_adjust', t_adjust)
    h5o.createArray(meta, 'chn_info_pk', pk.dumps(chn_info))
    h5o.createArray(meta, 'kwargs_pk', pk.dumps(kwargs))

    h5o.createArray(meta, 'idx2iid', idx2iid)
    h5o.createArray(meta, 'iid2idx_pk', pk.dumps(iid2idx))
    h5o.createArray(meta, 'idx2ch', idx2ch)
    h5o.createArray(meta, 'ch2idx_pk', pk.dumps(ch2idx))

    # some error signals
    h5o.createArray(meta, 'b_warn_max_spks_lim', b_warn_max_spks_lim)
    if len(l_empty_spks) > 0:
        h5o.createArray(meta, 'l_empty_spks', l_empty_spks)

    h5o.close()