Changeset 490 for trunk

Show
Ignore:
Timestamp:
08/23/11 15:18:35 (9 months ago)
Author:
emuller
Message:

Added AnalogSignal?.slice_exclude_events to cut out (remove) spikes

Location:
trunk
Files:
2 modified

Legend:

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

    r485 r490  
    386386                result[index] = self.time_slice(t_start_new, t_stop_new) 
    387387        return result 
    388          
    389              
    390  
     388 
     389    def slice_exclude_events(self,events,t_min=100,t_max=100): 
     390        """ 
     391        yields new AnalogSignals with events cutout (useful for removing spikes). 
     392 
     393        Events should be sorted in chronological order 
     394 
     395        Inputs: 
     396            events  - Can be a SpikeTrain object (and events will be the spikes) or just a list  
     397                      of times 
     398            t_min   - Time (>0) to cut the signal before an event, in ms (default 100) 
     399            t_max   - Time (>0) to cut the signal after an event, in ms  (default 100) 
     400         
     401        Examples: 
     402            >> res = aslist.slice_by_events([100,200,300], t_min=0, t_max =10) 
     403            >> print len(res) 
     404                4 
     405        """ 
     406        if isinstance(events, SpikeTrain): 
     407            events = events.spike_times 
     408        else: 
     409            assert numpy.iterable(events), "events should be a SpikeTrain object or an iterable object" 
     410        assert (t_min >= 0) and (t_max >= 0), "t_min and t_max should be greater than 0" 
     411        assert len(events) > 0, "events should not be empty and should contained at least one element" 
     412         
     413        t_last = self.t_start 
     414        for spike in events: 
     415            t_min_local = numpy.max([t_last, spike-t_min]) 
     416            t_max_local = numpy.min([self.t_stop, spike+t_max]) 
     417            if t_min_local>t_last: 
     418                yield self.time_slice(t_last, t_min_local) 
     419            t_last = t_max_local 
     420 
     421        if t_last<self.t_stop: 
     422            yield self.time_slice(t_last, self.t_stop) 
    391423 
    392424 
  • trunk/test/test_analogs.py

    r375 r490  
    5656        res2 = sig.slice_by_events(analogs.SpikeTrain([0, 50, 100]),t_min=0, t_max=50) 
    5757        assert len(res1) == 3 and len(res2) == 3 
     58 
     59    def testSliceExcludeEvents(self): 
     60        sig = analogs.AnalogSignal(numpy.sin(numpy.arange(10000.)), 0.1) 
     61        # check something in the middle splits in 2 
     62        res0 = list(sig.slice_exclude_events([500.0],t_min=0., t_max=0.)) 
     63        assert len(res0)==2 
     64        assert res0[0].t_stop == res0[1].t_start 
     65        assert res0[0].t_start == sig.t_start 
     66        assert res0[1].t_stop == sig.t_stop 
     67 
     68        # check that event at t_start yields only one slice, removing t_max 
     69        res1 = list(sig.slice_exclude_events([0.0],t_min=10., t_max=10.)) 
     70        assert len(res1)==1 
     71        assert res1[0].t_start == sig.t_start+10.0 
     72        assert res1[0].duration() == 990.0       
     73 
     74 
     75        # check something in the middle splits in 2 
     76        res2 = list(sig.slice_exclude_events([250.0, 750.0], t_min=50., t_max=50.)) 
     77        assert len(res2)==3 
     78        assert res2[0].duration() == 200.0 
     79        assert res2[1].duration() == 400.0 
     80        assert res2[2].duration() == 200.0 
     81 
     82        # check behaviour at the end (t_stop) 
     83        res3 = list(sig.slice_exclude_events([1000.0], t_min=50., t_max=50.)) 
     84        assert len(res3)==1 
     85        assert res3[0].duration() == 950.0 
     86 
     87         
    5888         
    5989    def testThresholdDetection(self):