Changeset 429

Show
Ignore:
Timestamp:
08/25/09 16:48:59 (3 years ago)
Author:
lestebanez
Message:

AnalogSignal? : port to intervals.

Location:
branches/interval/src/signals
Files:
2 modified

Legend:

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

    r390 r429  
    8080        self.signal  = numpy.array(signal, float) 
    8181        self.dt      = float(dt) 
    82         if t_start is None: 
    83             t_start = 0 
    84         self.t_start = float(t_start) 
    85         self.t_stop  = t_stop 
    86         # If t_stop is not None, we test that the signal has the correct number 
    87         # of elements 
    88         if self.t_stop is not None: 
    89             if abs(self.t_stop-self.t_start - self.dt * len(self.signal)) > 0.1*self.dt: 
    90                 raise Exception("Inconsistent arguments: t_start=%g, t_stop=%g, dt=%g implies %d elements, actually %d" % ( 
    91                                     t_start, t_stop, dt, int(round((t_stop-t_start)/float(dt))), len(signal))) 
    92         else: 
    93             self.t_stop = self.t_start + len(self.signal)*self.dt 
     82        self.interval = self.create_intervals(t_start, t_stop, interval) 
     83        self.init_times() 
    9484 
    9585        # TODO raise an error if some data is outside [t_start, t_stop] ? 
    96         # TODO return an exception if self.t_stop < self.t_start (when not empty) 
     86        if not (self.t_start >= 0) and (self.t_stop >= 0) : 
     87            raise ValueError("t_start and t_stop should be greater than 0") 
    9788        if self.t_start >= self.t_stop: 
    9889            raise Exception("Incompatible time interval for the creation of the AnalogSignal. t_start=%s, t_stop=%s" % (self.t_start, self.t_stop)) 
     90 
     91    def create_intervals(self, t_start=None, t_stop=None, interval=None): 
     92        if hasattr(self, 'interval') and t_start is None and t_stop is None and interval is None: 
     93            interval_out = self.interval.copy() 
     94        elif interval is not None: 
     95            # interval is fully defined by the user 
     96            if type(interval) == Interval: 
     97                interval_out = interval.copy() 
     98            else : 
     99                interval_out = Interval(interval)  
     100        else: 
     101            if t_start is None and t_stop is None : 
     102                try: 
     103                    t_start = numpy.min(self.spike_times) 
     104                except Exception: 
     105                    print "AnalogSignal error in guessing t_start (first spike), spikes may be empty !" 
     106                    t_start = 0 
     107                try: 
     108                    t_stop  = numpy.max(self.spike_times) 
     109                except Exception: 
     110                    print "AnalogSignal error in guessing t_stop (last spike), spikes may be empty !" 
     111                    t_stop = t_start+self.dt 
     112                interval_out = Interval([[t_start, t_stop]]) 
     113            elif t_start is None and t_stop is not None: 
     114                try: 
     115                    t_start  = numpy.min(self.spike_times) 
     116                except Exception: 
     117                    print "AnalogSignal error in guessing t_start (first spike), spikes may be empty !" 
     118                    t_start = 0 
     119                interval_out = Interval([[t_start, t_stop]]) 
     120            elif t_start is not None and t_stop is not None: 
     121                interval_out = Interval([[t_start, t_stop]]) 
     122            elif t_start is not None and t_stop is None: 
     123                if type(t_start) == float or type(t_start) == int: 
     124                    try: 
     125                        t_stop  = numpy.max(self.spike_times) 
     126                    except Exception: 
     127                        print "AnalogSignal error in guessing t_stop (last spike), spikes may be empty !" 
     128                        t_stop = t_start+self.dt 
     129                    interval_out = Interval([[t_start, t_stop]])  
     130                else : 
     131                    if type(t_start) == Interval: 
     132                        interval_out = t_start.copy() 
     133                    else : 
     134                        interval_out = Interval(t_start) 
     135        return interval_out 
     136 
    99137 
    100138    def __getslice__(self, i, j): 
     
    157195                1100 
    158196        """ 
    159         self.t_start += offset 
    160         self.t_stop  += offset 
     197        self.interval.offset_full(offset) 
     198        self.init_times() 
    161199 
    162200     
     
    165203        Return the time parameters of the AnalogSignal (t_start, t_stop, dt) 
    166204        """ 
     205        self.init_times() 
    167206        return (self.t_start, self.t_stop, self.dt) 
    168207     
     
    231270        result = [] 
    232271        for itv in interval.interval_data : 
    233             result.append(self.signal[intv[0]/self.dt:intv[1]/self.dt]) 
    234  
     272            result.append(self.signal[(intv[0]-self.t_start)/self.dt:(intv[1]-t_start)/self.dt]) 
     273        return result 
    235274 
    236275    def threshold_detection(self, threshold=None, format=None,sign='above'): 
     
    258297            cutout = numpy.where(self.signal < threshold)[0] 
    259298             
    260         if len(cutout) <= 0: 
     299        if len(cutout) == 0: 
    261300            events = [] 
    262301        else: 
  • branches/interval/src/signals/spikes.py

    r428 r429  
    122122                    t_start = numpy.min(self.spike_times) 
    123123                except Exception: 
    124                     print "SpikeList error in guessing t_start (first spike), spikes may be empty !" 
     124                    print "SpikeTrain error in guessing t_start (first spike), spikes may be empty !" 
    125125                    t_start = 0 
    126126                try: 
    127127                    t_stop  = numpy.max(self.spike_times) 
    128128                except Exception: 
    129                     print "SpikeList error in guessing t_stop (last spike), spikes may be empty !" 
     129                    print "SpikeTrain error in guessing t_stop (last spike), spikes may be empty !" 
    130130                    t_stop = t_start+0.1 
    131131                interval_out = Interval([[t_start, t_stop]]) 
     
    134134                    t_start  = numpy.min(self.spike_times) 
    135135                except Exception: 
    136                     print "SpikeList error in guessing t_start (first spike), spikes may be empty !" 
     136                    print "SpikeTrain error in guessing t_start (first spike), spikes may be empty !" 
    137137                    t_start = 0 
    138138                interval_out = Interval([[t_start, t_stop]]) 
     
    144144                        t_stop  = numpy.max(self.spike_times) 
    145145                    except Exception: 
    146                         print "SpikeList error in guessing t_stop (last spike), spikes may be empty !" 
     146                        print "SpikeTrain error in guessing t_stop (last spike), spikes may be empty !" 
    147147                        t_stop = t_start+0.1 
    148148                    interval_out = Interval([[t_start, t_stop]])  
     
    10241024        Return the time parameters of the SpikeList (t_start, t_stop) 
    10251025        """ 
    1026         return (self.interval.t_start(), self.interval.t_stop()) 
     1026        self.init_times() 
     1027        return (self.t_start, self.t_stop) 
    10271028 
    10281029    def init_times(self):