Changeset 212

Show
Ignore:
Timestamp:
10/03/08 16:21:08 (3 months ago)
Author:
pierre
Message:

Fix some bugs, add a select_ids method() to the SpikeList object to allow the selection of cells matching only a particular criteria. Need to test all the methods that compute averaged values on pairs of cells, like pairwise_cc, pairwise_pearson_corrcoeff, ....

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/cleanup/src/signals.py

    r210 r212  
    904904            self.spiketrains[id].add_offset(offset) 
    905905     
     906    def first_spike_time(self): 
     907        """ 
     908        Get the time of the first real spike in the SpikeList 
     909        """ 
     910        first_spike = self.t_stop 
     911        is_empty    = True 
     912        for id in self.id_list(): 
     913            if len(self.spiketrains[id]) > 0: 
     914                is_empty = False 
     915                if self.spiketrains[id].spike_times[0] < first_spike: 
     916                    first_spike = self.spiketrains[id].spike_times[0] 
     917        if is_empty: 
     918            raise Exception("No spikes can be found in the SpikeList object !") 
     919        else: 
     920            return first_spike 
     921     
     922    def last_spike_time(self): 
     923        """ 
     924        Get the time of the last real spike in the SpikeList 
     925        """ 
     926        last_spike = self.t_start 
     927        is_empty    = True 
     928        for id in self.id_list(): 
     929            if len(self.spiketrains[id]) > 0: 
     930                is_empty = False 
     931                if self.spiketrains[id].spike_times[-1] > last_spike: 
     932                    last_spike = self.spiketrains[id].spike_times[-1] 
     933        if is_empty: 
     934            raise Exception("No spikes can be found in the SpikeList object !") 
     935        else: 
     936            return last_spike 
     937     
     938     
     939    def select_ids(self, criteria=None): 
     940        """ 
     941        Return the list of all the cells in the SpikeList that will match the criteria 
     942        expressed with the following syntax.  
     943         
     944        Inputs :  
     945            criteria - a string that can be evaluated on a SpikeTrain object, where the  
     946                       SpikeTrain should be named ``cell''. 
     947         
     948        Exemples: 
     949            >>> spklist.select_ids("cell.mean_rate() > 0") (all the active cells) 
     950            >>> spklist.select_ids("cell.mean_rate() == 0") (all the silent cells) 
     951            >>> spklist.select_ids("len(cell.spiketimes) > 10") 
     952            >>> spklist.select_ids("mean(cell.isi()) < 1") 
     953        """ 
     954        selected_ids = [] 
     955        for id in self.id_list(): 
     956            cell = self.spiketrains[id] 
     957            if eval(criteria): 
     958                selected_ids.append(id) 
     959        return selected_ids 
     960 
     961     
    906962    def save(self, filename, method="text"): 
    907963        """ 
     
    9861042            if len(isi) > 1: 
    9871043                cvs_isi.append(numpy.std(isi)/numpy.mean(isi)) 
    988         return cvs_isi 
     1044        if len(cvs_isi) > 0: 
     1045            return cvs_isi 
     1046        else: 
     1047            raise Exception("No isi can be computed in the SpikeList !") 
    9891048 
    9901049 
     
    13091368        """ 
    13101369        subplot = self.__getdisplay__(display) 
     1370         
     1371        if spklist == None: 
     1372            spklist = self 
    13111373         
    13121374        if type(pairs) == int: 
     
    14541516 
    14551517        """ 
     1518        if spklist == None: 
     1519            spklist = self 
    14561520         
    14571521        if type(id_list) == int: 
     
    14841548                       if None, this is the one calling the function 
    14851549        """ 
     1550        if spklist == None: 
     1551            spklist = self 
     1552         
    14861553        if type(id_list) == int: 
    14871554            spk1 = self.id_slice(id_list) 
     
    15111578        """ 
    15121579        firing_rate = self.firing_rate(time_bin) 
    1513         return numpy.var(numpy.sum(firing_rate, axis=0)/self.N
     1580        return numpy.var(numpy.sum(firing_rate, axis=0)/len(self)
    15141581 
    15151582    def mean_rate_covariance(self, spikelist, time_bin): 
     
    15301597            raise Exception("Error, both SpikeLists should share common t_start, t_stop and dt") 
    15311598        frate_1 = self.firing_rate(time_bin) 
    1532         frate_1 = numpy.sum(frate_1, axis=0)/self.N 
     1599        frate_1 = numpy.sum(frate_1, axis=0)/len(self) 
    15331600        frate_2 = spikelist.firing_rate(time_bin) 
    1534         frate_2 = numpy.sum(frate_2, axis=0)/spikelist.N 
     1601        frate_2 = numpy.sum(frate_2, axis=0)/len(spikelist) 
    15351602        N = len(frate_1) 
    15361603        cov = numpy.sum(frate_1*frate_2)/N-numpy.sum(frate_1)*numpy.sum(frate_2)/(N*N) 
     
    15831650            min_id = numpy.min(spk.id_list) 
    15841651            max_id = numpy.max(spk.id_list) 
    1585             length = (t_stop - t_start) 
     1652            length = t_stop - t_start 
    15861653            subplot.axis([t_start-0.05*length, t_stop+0.05*length, min_id-2, max_id+2]) 
    15871654            pylab.draw() 
     
    16741741 
    16751742 
    1676     #################################################################### 
    1677     ### TOO SPECIFIC METHOD ? 
    1678     ### Better documentation 
    1679     #################################################################### 
    1680     def pw_corr_pearson(self,edge,bins,number_of_neuron_pairs):  
    1681         """ 
    1682         TODO: document/test this function 
    1683  
    1684         """ 
    1685         #bins = edge[1]-edge[0] 
    1686         cor = numpy.zeros((number_of_neuron_pairs,)) 
    1687         [neuron_ids_array, spike_times_array] = self.as_list_id_list_time() 
    1688          
    1689         # Pairwise correlation 
    1690         neuron_ids_unique = numpy.unique(neuron_ids_array) 
    1691          
    1692         if len(neuron_ids_unique)==0: 
    1693             return (0,0) 
    1694          
    1695         for count in range(number_of_neuron_pairs): 
    1696             # draw two neuron radomly out of the neuron_id_unique pool 
    1697             neuron_1_tmp = neuron_ids_unique[numpy.floor(numpy.random.uniform()*len(neuron_ids_unique))] 
    1698             neuron_2_tmp = neuron_ids_unique[numpy.floor(numpy.random.uniform()*len(neuron_ids_unique))] 
    1699             # get the spike_times of the neurons 
    1700             spike_times_1_tmp = self.spiketrains[neuron_1_tmp].spike_times 
    1701             spike_times_2_tmp = self.spiketrains[neuron_2_tmp].spike_times 
    1702  
    1703             # hist of the spiketrains 
    1704             n1_hist = numpy.histogram(spike_times_1_tmp,bins=bins,range=edge) 
    1705             n2_hist = numpy.histogram(spike_times_2_tmp,bins=bins,range=edge) 
     1743    def pairwise_pearson_corrcoeff(self, pairs, spklist=None, time_bin=1.):  
     1744        """ 
     1745        Function to return the mean and the variance of the pearson correlation coefficient.  
     1746        For more details, see Kumar et al, .... 
     1747         
     1748        Inputs: 
     1749            pairs    - an int to specify the number of pairs used to estimate the correlation coefficient 
     1750            time_bin - The time bin used to gather the spikes 
     1751        """ 
     1752        if spklist == None: 
     1753            spklist = self 
     1754         
     1755        if type(pairs) == int: 
     1756            spk1 = self.id_slice(pairs) 
     1757            spk2 = spklist.id_slice(pairs) 
     1758            N    = pairs 
     1759        else: 
     1760            pairs  = numpy.array(pairs) 
     1761            N      = len(pairs[:,0]) 
     1762            spk1   = self.id_slice(pairs[:,0]) 
     1763            spk2   = spklist.id_slice(pairs[:,1]) 
     1764         
     1765        cor          = numpy.zeros(N, float) 
     1766         
     1767        ## We have to extract only the non silent cells, to avoied problems 
     1768        non_silent = spk1.select_ids("cell.mean_rate() > 0") 
     1769        spk1 = spk1.id_slice(non_silent) 
     1770        non_silent = spk2.select_ids("cell.mean_rate() > 0") 
     1771        spk2 = spk2.id_slice(non_silent) 
     1772         
     1773        for count in range(N): 
     1774            # draw two neuron randomly out of the neuron_id_unique pool 
     1775            neuron_1 = self.id_list()[numpy.floor(numpy.random.uniform()*len(self))] 
     1776            neuron_2 = self.id_list()[numpy.floor(numpy.random.uniform()*len(self))] 
     1777 
     1778            # get the spike_times of the selected neurons 
     1779            spk_1 = self.spiketrains[neuron_1] 
     1780            spk_2 = self.spiketrains[neuron_2] 
     1781 
     1782            # get the histogram of the spiketrains 
     1783            n1_hist = spk_1.time_histogram(time_bin) 
     1784            n2_hist = spk_2.time_histogram(time_bin) 
    17061785 
    17071786            # correlation 
    17081787            # TODO: normalize the cor, look in 1.6 in Kumar et al. 
    17091788            # bruederle: the function corrcoeff actually implements the definition in Kumar 1.6 
    1710             cov = numpy.corrcoef(n1_hist[0],n2_hist[0])[1][0] 
    1711             #print n1_hist[0] 
    1712             #print n2_hist[0] 
     1789            cov = numpy.corrcoef(n1_hist,n2_hist)[1][0] 
    17131790 
    17141791            # bruederle: the expression 'cov' is already, per definition, the pearson correlation coefficient  
     
    17201797 
    17211798        cor_coef_mean = cor.mean() 
    1722         cor_coef_std = cor.std() 
    1723         return cor_coef_mean, cor_coef_std 
     1799        cor_coef_std  = cor.std() 
     1800        return (cor_coef_mean, cor_coef_std) 
     1801 
    17241802 
    17251803    ####################################################################