Example #1
0
    def list_trials_by_type(self, session=None, include_trials='hits',
        **kwargs):
        idx = np.nonzero(self.session_names == session)[0].item()
        tdf = pandas.load(self.tdf_list[idx])
        if include_trials == 'hits':
            tdf = tdf[(tdf.outcome == 1) & (tdf.nonrandom == 0)]
        elif include_trials == 'non-hits':
            tdf = tdf[(tdf.outcome != 1) & (tdf.nonrandom == 0)]
        elif include_trials == 'all random':
            tdf = tdf[tdf.nonrandom == 0]
        else:
            raise "only hits, non-hits, and all random supported for now"

        replace_stim_numbers_with_names(tdf)
        
        mask = myutils.pick_mask(tdf, **kwargs)        
        return np.asarray(tdf.index[mask], dtype=np.int)
Example #2
0
    def get_binned_spikes3(self, spike_filter=None, trial_filter=None):
        """Generic binning function operating on self._fsd
        
        spike_filter : dataframe describing how to split fsd
            The columns are the hierarchy to split on:
                eg ['session', 'unit']
            The items are the ones to include.
            If no items, then everything is included.
            If None, then bin over everything except 'adj_time' or 'spike_time'
            
            Here we delineate every combination because it's not separable
            over session and unit (usually).
        
        trial_filter :
            How to do this filtering?
            For instance: hits only, stim_numbers 1-12, expressed as dicts
            In this case I wouldn't want to enumerate every combination
            because I can just take intersection over stim numbers and outcome.
            
            Although note we might want to combine errors and wrongports,
            for instance.
            
            It's implicit that we want to do this for each session in
            spike_filter.
        

        First the spikes are grouped over the columns in spike_filter.
        For each group, the trials are grouped over the columns in trial_filter.
        This cross-result is histogrammed.
        All results are concatenated and returned.

        The actual histogramming is done by myutils.times2bins using
        self.f_samp, t_start, t_stop, bins
        
        
        """
        input = self._fsd
        
        # default, use all columns and include all data
        if spike_filter is None:
            col_list = input.columns.tolist()
            remove_cols = ['adj_time', 'spike_time']
            for col in remove_cols:
                if col in col_list:
                    col_list.remove(col)
            spike_filter = pandas.DataFrame(columns=col_list)
        
        # Choose data from `input` by defining the following variables:
        #   `keylist` : a list of keys to include, each separately binned
        #   `grouped_data` : a dict from each key in keylist, to the data
        #   `keynames` : what to call each entry of the key in the result
        if len(spike_filter) == 0:
            # use all data
            keynames = spike_filter.columns.tolist()
            keylist = [tuple([myutils.only_one(input[col])
                for col in keynames])]
            val = input
            grouped_data = {keylist[0]: val}            
        elif len(spike_filter) == 1:
            # Optimized for the case of selecting a single unit
            d = {}
            for col in spike_filter:
                d[col] = spike_filter[col][0]            
            mask = myutils.pick_mask(input, **d)
            
            keylist = spike_filter.to_records(index=False) # length 1
            keynames = spike_filter.columns.tolist()        
            grouped_data = {keylist[0] : input.ix[mask]}
        else:
            # standard case
            g = input.groupby(spike_filter.columns.tolist())
            grouped_data = g.groups
            keylist = spike_filter.to_records(index=False)
            keynames = spike_filter.columns.tolist()
        
        # Now group the trials
        att = self.all_trials.reset_index().set_index(['session', 'trial'], drop=False)
        g2 = att.groupby(trial_filter.columns.tolist())
        #g2 = self.all_trials.groupby(trial_filter.columns.tolist())
        
        
        # Now iterate through the keys in keylist and the corresponding values
        # in grouped_data.
        rec_l = []    
        for key in keylist:
            # Take the data from this group
            subdf = grouped_data[key]
            
            for g2k, g2v in g2:
                # count trials of this type from this session
                session = myutils.only_one(subdf.session)
                n_trials = len(g2v.ix[session])
                if n_trials == 0:
                    # for example if a possible combination never actually
                    # occurred
                    continue

                # Join the spikes on the columns of trial filter
                subsubdf = subdf.join(g2v[trial_filter.columns], 
                    on=['session', 'trial'], how='inner', rsuffix='rrr')
                
                # check for already-joined columns
                for col in trial_filter.columns:
                    if col+'rrr' in subsubdf.columns:
                        assert (subsubdf[col] == subsubdf[col+'rrr']).all()
                        subsubdf.pop(col + 'rrr')
            
                # histogramming
                counts, t_vals = myutils.times2bins(
                    np.asarray(subsubdf.adj_time), return_t=True, 
                    f_samp=self.f_samp, t_start=self.t_start,
                    t_stop=self.t_stop, bins=self.bins)
                
                # Add in the keyed info (session etc), plus 
                # n_counts, n_trials, and bin
                frame_label = list(key) + list(np.array([g2k]).flatten())
                this_frame = [frame_label +
                    [count, n_trials, t_val, bin] 
                    for bin, (count, t_val) in enumerate(zip(counts, t_vals))]
                
                # append to growing list
                rec_l += this_frame
        
        # convert to new data frame, using same keyed columns plus our new ones
        cols = keynames + trial_filter.columns.tolist() + [
            'counts', 'trials', 'time', 'bin']
        newdf = pandas.DataFrame(rec_l, columns=cols)
        
        # drop the combinations that never actually occurred (future trials)
        return newdf