Example #1
0
def find_data_with_stimulus(filenamelist):
    """Open files passed in `filenamelist` and check for background
    and probe stimulus data. The files must be data files, not network
    files. Return a list of open file handles of those that are
    good"""
    files_with_stim = []
    files_to_close = []
    for filename in filenamelist:
        try:
            fh = h5.File(filename, 'r')
        except IOError:
            print filename, 'could not be opened'
            continue
        try:
            bgtimes = analyzer.get_bgtimes(fh)
            probetimes = analyzer.get_probetimes(fh)
            if (0 in bgtimes.shape) or (0 in probetimes.shape):
                files_to_close.append(fh)
            else:
                files_with_stim.append(fh)
        except KeyError:
            print filename, 'does not have stimulus data'
            files_to_close.append(fh)
    for fh in files_to_close:
        print 'File without stimulus info:', fh.filename
        fh.close()
    return files_with_stim
Example #2
0
def get_stim_aligned_spike_times(fhandles, cellnames):
    """Collect the spike times for each cell from all the files with
    respect to the stimuli.

    Parameteres
    -----------

    fhandles: list
    open data file handles.

    cellnames: list
    list of cell names to be checked.

    Returns
    -------
    
    A pair containing dicts of list of arrays of spike times following
    each stimulus presentation: one for background alone and one for
    background + probe. All the spike times are with respect to the
    preceding stimulus.
    """
    plot = 'all'
    bg_spikes = defaultdict(list)
    probe_spikes = defaultdict(list)
    for fh in fhandles:
        stiminfo = dict(fh['runconfig/stimulus'][:])
        simtime = float(dict(fh['runconfig/scheduling'])['simtime'])        
        stim_onset = float(stiminfo['onset'])
        interval = float(stiminfo['bg_interval'])
        stimwidth = float(stiminfo['pulse_width'])
        spike_times = get_spike_times(fh, cellnames)
        bgtimes = analyzer.get_bgtimes(fh)
        probetimes = analyzer.get_probetimes(fh)
        print 'Background times', bgtimes
        print 'Probe times', probetimes
        bgbins = [(bgtimes[ii], bgtimes[ii]+interval+stimwidth) for ii in range(0, len(bgtimes), 2)]
        print bgbins
        probebins = [(pt, pt+interval+stimwidth) for pt in probetimes]
        print probebins
        # Probe stimulus is designed to align with every alternet bg
        # stmulus.
        cell_no = 1
        for cell, spikes in spike_times.items():
            bg = [spikes[(spikes >= bin[0]) & (spikes < bin[1])]
                  for bin in bgbins]
            bg_spikes[cell] += bg
            probe = [spikes[(spikes >= bin[0]) & (spikes < (bin[1]))]
                     for bin in probebins]
            probe_spikes[cell] += probe
            for x in bg:
                print 'bg', x
            for x in probe:
                print 'probe', x
            if plot is not None:
                x = np.concatenate(bg)
                plt.plot(x, np.ones(len(x))*cell_no, 'bx')
                x = np.concatenate(probe)
                plt.plot(x, np.ones(len(x))*cell_no, 'r+')
            if plot == 'each':
                plt.title('%s: %s' % (fh.filename, cell))
                plt.show()
            elif plot == 'all':
                cell_no += 1
        if plot == 'all':
            plt.title(fh.filename)
            plt.show()
    return (bg_spikes, probe_spikes)