Show
Ignore:
Timestamp:
04/12/10 15:37:12 (2 years ago)
Author:
apdavison
Message:

SpikeTrain.time_slice() now (again) accepts a sequence of t_start values and a sequence of t_stop values, if you wish to extract multiple slices.

Files:
1 modified

Legend:

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

    r443 r446  
    186186     
    187187     
    188     def merge(self, spiketrain): 
     188    def merge(self, spiketrain, relative=False): 
    189189        """ 
    190190        Add the spike times from a spiketrain to the current SpikeTrain 
     
    192192        Inputs: 
    193193            spiketrain - The SpikeTrain that should be added 
     194            relative - if True, relative_times() is called on both spiketrains before merging 
    194195         
    195196        Examples: 
     
    204205                500 
    205206        """ 
     207        if relative: 
     208            self.relative_times() 
     209            spiketrain.relative_times() 
    206210        self.spike_times = numpy.insert(self.spike_times, self.spike_times.searchsorted(spiketrain.spike_times), \ 
    207211                                        spiketrain.spike_times) 
     
    464468    def time_slice(self, t_start, t_stop): 
    465469        """  
    466         Return a new SpikeTrain obtained by slicing between t_start and t_stop. The new  
    467         t_start and t_stop values of the returned SpikeTrain are the one given as arguments 
     470        Return a new SpikeTrain obtained by slicing between t_start and t_stop, 
     471        where t_start and t_stop may either be single values or sequences of 
     472        start and stop times. 
    468473         
    469474        Inputs: 
     
    477482            >> spk.t_stop 
    478483                100 
    479         """ 
    480         spikes = numpy.extract((self.spike_times >= t_start) & (self.spike_times <= t_stop), self.spike_times) 
     484            >>> spk = spktrain.time_slice([20,70], [40,90]) 
     485            >>> spk.t_start 
     486                20 
     487            >>> spk.t_stop 
     488                90 
     489            >>> len(spk.time_slice(41, 69)) 
     490                0 
     491        """ 
     492        if hasattr(t_start, '__len__'): 
     493            if len(t_start) != len(t_stop): 
     494                raise ValueError("t_start has %d values and t_stop %d. They must be of the same length." % (len(t_start), len(t_stop))) 
     495            mask = False 
     496            for t0,t1 in zip(t_start, t_stop): 
     497                mask = mask | ((self.spike_times >= t0) & (self.spike_times <= t1)) 
     498            t_start = t_start[0] 
     499            t_stop = t_stop[-1] 
     500        else: 
     501            mask = (self.spike_times >= t_start) & (self.spike_times <= t_stop) 
     502        spikes = numpy.extract(mask, self.spike_times) 
    481503        return SpikeTrain(spikes, t_start, t_stop) 
    482504 
     
    536558         
    537559        Note that the SpikeTrain object itself is modified, t_start  
    538         is substracted to spike_times, t_start and t_stop 
     560        is subtracted from spike_times, t_start and t_stop 
    539561        """ 
    540562        if self.t_start != 0: