Changeset 419 for branches

Show
Ignore:
Timestamp:
08/19/09 00:31:31 (3 years ago)
Author:
lestebanez
Message:

1 - implement a mechanism for compatibility between old 't_start', 't_stop' based syntax

and the new interval based time selection mechanism

2 - add this mechanism to most functions in SpikeTrain. Still to be done in SpikeList

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/interval/src/signals/spikes.py

    r418 r419  
    5454class SpikeTrain(object): 
    5555    """ 
    56     SpikeTrain(spikes_times, t_start=None, t_stop=None) 
     56    SpikeTrain(spikes_times, t_start=None, t_stop=None, interval=None) 
    5757    This class defines a spike train as a list of times events. 
    5858 
     
    6363        t_start     - beginning of the SpikeTrain (if not, this is infered) 
    6464        t_stop      - end of the SpikeTrain (if not, this is infered) 
     65        interval    - an Interval instance or a list of value couples that 
     66                      indicates when the spike train is defined. 
     67     
     68    Note on inputs: either the t_start/t_stop or the interval should be provided. 
     69    If both are provided, the t_start/t_stops are ignored.  
     70    If no argument name is provided, and the second argument of the function is 
     71    a list of 2-value tuples, then it is an interval. 
     72    If this second argument is a single number, it is counted as a t_start. 
     73     
    6574 
    6675    Examples: 
     
    7786    ## Constructor and key methods to manipulate the SpikeTrain objects  ## 
    7887    ####################################################################### 
    79     def __init__(self, spike_times, interval=None): 
     88    def __init__(self, spike_times, t_start=None, t_stop=None, interval=None): 
    8089        """ 
    8190        Constructor of the SpikeTrain object 
     
    8493            SpikeTrain 
    8594        """ 
    86         if interval is not None: 
    87             self.interval = interval 
    88         else: 
    89             try: 
    90                 t_start = min(spike_times) 
    91             except Exception: 
    92                 print "Error in guessing t_start (first spike), spikes may be empty !" 
    93                 t_start = 0 
    94             try: 
    95                 t_stop  = max(spike_times) 
    96             except Exception: 
    97                 print "Error in guessing t_stop (last spike), spikes may be empty !" 
    98                 t_stop = 0.1 
    99             self.interval = Interval((t_start, t_stop)) 
    100  
     95        self.interval = extract_intervals_from_function_arguments(t_start, t_stop, interval) 
     96         
    10197        self.spike_times = self.interval.slice_times(numpy.array(spike_times, numpy.float32)) 
    10298 
    103         # We sort the spike_times. May be slower, but is necessary by the way for quite a  
    104         # lot of methods... 
     99        # We sort the spike_times. May be slowe, but required for 
     100        # several analysis methods 
    105101        self.spike_times = numpy.sort(self.spike_times, kind="quicksort") 
    106         # Here we deal with the t_start and t_stop values if the SpikeTrain 
    107         # is empty, with only one element or several elements, if we 
    108         # need to guess t_start and t_stop 
    109         # no element : t_start = 0, t_stop = 0.1  
    110         # 1 element  : t_start = time, t_stop = time + 0.1 
    111         # several    : t_start = min(time), t_stop = max(time) 
    112  
    113102 
    114103    def __str__(self): 
     
    245234        return numpy.diff(self.spike_times) 
    246235 
    247     def mean_rate(self, interval=None): 
     236    def mean_rate(self, t_start=None, t_stop=None, interval=None): 
    248237        """  
    249238        Returns the mean firing rate between t_start and t_stop, in Hz 
     
    256245                34.2 
    257246        """ 
    258         if interval: 
    259             new_interval = interval.intersect(self.interval) 
    260             idx          = new_interval.slice_times(self.spike_times) 
    261         else: 
    262             idx          = self.spike_times 
    263             new_interval = self.interval 
    264         return 1000.*len(idx)/new_interval.total_duration() 
    265  
    266     def cv_isi(self): 
     247        rate_interval = extract_intervals_from_function_arguments(t_start, t_stop, interval) 
     248 
     249        rate_interval = rate_interval.intersect(self.interval) 
     250        idx           = rate_interval.slice_times(self.spike_times) 
     251 
     252        return 1000.*len(idx)/rate_interval.total_duration() 
     253 
     254    def cv_isi(self, t_start=None, t_stop=None, interval=None): 
    267255        """ 
    268256        Return the coefficient of variation of the isis. 
     
    283271             
    284272        """ 
    285         isi = self.isi() 
     273        isi = self.isi(t_start, t_stop, interval) 
    286274        if len(isi) > 0: 
    287275            return numpy.std(isi)/numpy.mean(isi) 
     
    290278            return numpy.nan 
    291279 
    292     def cv_kl(self, bins=100): 
     280    def cv_kl(self, bins=100, t_start=None, t_stop=None, interval=None): 
    293281        """ 
    294282        Provides a measure for the coefficient of variation to describe the 
     
    313301             
    314302        """ 
    315         isi = self.isi() / 1000. 
     303        isi = self.isi(t_start, t_stop, interval) / 1000. 
    316304        if len(isi) < 2: 
    317305            logging.debug("Warning, a CV can't be computed because there are not enough spikes") 
     
    332320     
    333321 
    334     def fano_factor_isi(self): 
     322    def fano_factor_isi(self, t_start=None, t_stop=None, interval=None): 
    335323        """  
    336324        Return the fano factor of this spike trains ISI. 
     
    343331            isi, cv_isi 
    344332        """ 
    345         isi = self.isi() 
     333        isi = self.isi(t_start, t_stop, interval) 
    346334        if len(isi) > 0: 
    347335            fano = numpy.var(isi)/numpy.mean(isi) 
     
    371359        return axis 
    372360 
    373     def raster_plot(self, interval=None, display=True, kwargs={}): 
     361    def raster_plot(self, t_start=None, t_stop=None, interval=None, display=True, kwargs={}): 
    374362        """ 
    375363        Generate a raster plot with the SpikeTrain in a subwindow of interest, 
     
    390378        """ 
    391379         
    392         if interval is None: 
    393             interval = self.interval 
    394          
    395         if not interval.is_equal(self.interval): 
    396             spikes  = interval.slice_times(self.spike_times) 
     380        raster_interval = extract_intervals_from_function_arguments(t_start, t_stop, interval) 
     381 
     382        if not raster_interval.is_equal(self.interval): 
     383            spikes  = raster_interval.slice_times(self.spike_times) 
    397384        else: 
    398385            spikes = self.spike_times 
     
    731718            raise Exception(errmsg) 
    732719        return F1/F0 
    733          
    734  
     720 
     721    def extract_intervals_from_function_arguments(self, t_start=None, t_stop=None, interval=None) : 
     722        if interval is not None: 
     723            # interval is fully defined by the user 
     724            if interval.__class__.__name__ == 'Interval' : 
     725                interval_out = interval 
     726            else : 
     727                interval_out = Interval(interval)  
     728        else:  
     729            if t_start == None and t_stop == None and self.interval : 
     730                # absolutely no information provided on interval, but 
     731                # self has a well defined interval 
     732                interval_out = self.interval 
     733            else : 
     734                # first analyse t_stop 
     735                if t_stop == None : 
     736                    try: 
     737                        t_stop  = max(self.spike_times) 
     738                    except Exception: 
     739                        print "Error in guessing t_stop (last spike), spikes may be empty !" 
     740                        t_stop = 0.1 
     741                 
     742                # then analyse t_start (ovewrite t_stop if t_start is actually an interval) 
     743                if t_start == None :  # no t_start 
     744                        t_start = min(self.spike_times) 
     745                    except Exception: 
     746                        print "Error in guessing t_start (first spike), spikes may be empty !" 
     747                        t_start = 0 
     748                    interval_out = Interval([[t_start, t_stop]]) 
     749                else : 
     750                    if t_start.__class__.__name == 'float' or t_start.__class__.__name == 'int' : 
     751                        # migrate the t_start/t_stop notation to an Interval  
     752                        interval_out = Interval([[t_start, t_stop]])  
     753                    else : 
     754                        if t_start.__class__.__name__ == 'Interval' : 
     755                            interval_out = interval 
     756                        else : 
     757                            interval_out = Interval(interval) 
     758        return interval_out 
    735759 
    736760class SpikeList(object):