Changeset 406 for trunk

Show
Ignore:
Timestamp:
06/25/09 18:04:50 (3 years ago)
Author:
pierre
Message:

Add a sort_by() method to allow the sorting of cells in a SpikeList acording to a user defined criteria. Minors modifications in addition ;-)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/signals/spikes.py

    r404 r406  
    617617            events  - Can be a SpikeTrain object (and events will be the spikes) or just a list  
    618618                      of times 
     619            time_bin- The time bin (in ms) used to gather the spike for the psth 
    619620            t_min   - Time (>0) to average the signal before an event, in ms (default 0) 
    620621            t_max   - Time (>0) to average the signal after an event, in ms  (default 100) 
     
    876877    #def __setslice__(self, i, j): 
    877878 
     879 
     880 
    878881    def __setitem__(self, id, spktrain): 
    879882        assert isinstance(spktrain, SpikeTrain), "A SpikeList object can only contain SpikeTrain objects" 
     
    11821185     
    11831186     
    1184     def select_ids(self, criteria=None): 
     1187    def select_ids(self, criteria): 
    11851188        """ 
    11861189        Return the list of all the cells in the SpikeList that will match the criteria 
     
    12031206                selected_ids.append(id) 
    12041207        return selected_ids 
     1208 
     1209    def sort_by(self, criteria, descending=False): 
     1210        """ 
     1211        Return an array with all the ids of the cells in the SpikeList,  
     1212        sorted according to a particular criteria. 
     1213         
     1214        Inputs: 
     1215            criteria   - the criteria used to sort the cells. It should be a string 
     1216                         that can be evaluated on a SpikeTrain object, where the  
     1217                         SpikeTrain should be named ``cell''. 
     1218            descending - if True, then the cells are sorted from max to min.  
     1219             
     1220        Examples: 
     1221            >> spk.sort_by('cell.mean_rate()') 
     1222            >> spk.sort_by('cell.cv_isi()', descending=True) 
     1223            >> spk.sort_by('cell.distance_victorpurpura(target, 0.05)') 
     1224        """ 
     1225        criterias = numpy.zeros(len(self), float) 
     1226        for count, id in enumerate(self.id_list()): 
     1227            cell  = self.spiketrains[id] 
     1228            criterias[count] = eval(criteria) 
     1229        result = self.id_list()[numpy.argsort(criterias)] 
     1230        if descending: 
     1231            return result[numpy.arange(len(result)-1, -1, -1)] 
     1232        else: 
     1233            return result 
    12051234 
    12061235     
     
    15591588            if newnum: 
    15601589                axis = axis[:len(axis)-1] 
    1561             subplot.plot(axis,numpy.sum(spike_hist, axis=0)/N,**kwargs) 
     1590            subplot.plot(axis,numpy.mean(spike_hist, axis=0),**kwargs) 
    15621591            pylab.draw() 
    15631592             
    15641593 
    1565     def firing_rate(self, time_bin, display=False, kwargs={}): 
     1594    def firing_rate(self, time_bin, display=False, average=False, kwargs={}): 
    15661595        """ 
    15671596        Generate an array with all the instantaneous firing rates along time (in Hz)  
    1568         of all the SpikeTrains objects within the SpikeList. 
     1597        of all the SpikeTrains objects within the SpikeList. If average is True, it gives the 
     1598        average firing rate over the whole SpikeList 
    15691599         
    15701600        Inputs: 
    15711601            time_bin   - the time bin used to gather the data 
     1602            average    - If True, return a single vector of the average firing rate over the whole SpikeList 
    15721603            display    - if True, a new figure is created. Could also be a subplot. The averaged 
    15731604                         spike_histogram over the whole population is then plotted 
     
    15781609            spike_histogram, time_axis 
    15791610        """ 
    1580         return self.spike_histogram(time_bin, normalized=True, display=display, kwargs=kwargs) 
    1581  
     1611        result = self.spike_histogram(time_bin, normalized=True, display=display, kwargs=kwargs) 
     1612        if average: 
     1613            return numpy.mean(result, axis=0) 
     1614        else: 
     1615            return result 
    15821616 
    15831617    def fano_factor(self, time_bin): 
     
    15961630        """ 
    15971631        firing_rate = self.spike_histogram(time_bin) 
    1598         firing_rate = numpy.sum(firing_rate,axis=0) 
     1632        firing_rate = numpy.mean(firing_rate, axis=0) 
    15991633        fano        = numpy.var(firing_rate)/numpy.mean(firing_rate) 
    16001634        return fano 
     
    19732007        """ 
    19742008        firing_rate = self.firing_rate(time_bin) 
    1975         return numpy.var(numpy.sum(firing_rate, axis=0)/len(self)) 
     2009        return numpy.var(numpy.mean(firing_rate, axis=0)) 
    19762010 
    19772011    def mean_rate_covariance(self, spikelist, time_bin): 
     
    19912025        if not spikelist.time_parameters() == self.time_parameters(): 
    19922026            raise Exception("Error, both SpikeLists should share common t_start, t_stop") 
    1993         frate_1 = self.firing_rate(time_bin) 
    1994         frate_1 = numpy.sum(frate_1, axis=0)/len(self) 
    1995         frate_2 = spikelist.firing_rate(time_bin) 
    1996         frate_2 = numpy.sum(frate_2, axis=0)/len(spikelist) 
    1997         N = len(frate_1) 
     2027        frate_1 = self.firing_rate(time_bin, average=True) 
     2028        frate_2 = spikelist.firing_rate(time_bin, average=True) 
     2029        N       = len(frate_1) 
    19982030        cov = numpy.sum(frate_1*frate_2)/N-numpy.sum(frate_1)*numpy.sum(frate_2)/(N*N) 
    19992031        return cov 
     
    20622094            average - If True, return a single vector of the averaged waveform. If False,  
    20632095                      return an array of all the waveforms. 
     2096            time_bin- The time bin (in ms) used to gather the spike for the psth 
    20642097            t_min   - Time (>0) to average the signal before an event, in ms (default 0) 
    20652098            t_max   - Time (>0) to average the signal after an event, in ms  (default 100)