- Timestamp:
- 08/19/09 00:31:31 (3 years ago)
- Files:
-
- 1 modified
-
branches/interval/src/signals/spikes.py (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/interval/src/signals/spikes.py
r418 r419 54 54 class SpikeTrain(object): 55 55 """ 56 SpikeTrain(spikes_times, t_start=None, t_stop=None )56 SpikeTrain(spikes_times, t_start=None, t_stop=None, interval=None) 57 57 This class defines a spike train as a list of times events. 58 58 … … 63 63 t_start - beginning of the SpikeTrain (if not, this is infered) 64 64 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 65 74 66 75 Examples: … … 77 86 ## Constructor and key methods to manipulate the SpikeTrain objects ## 78 87 ####################################################################### 79 def __init__(self, spike_times, interval=None):88 def __init__(self, spike_times, t_start=None, t_stop=None, interval=None): 80 89 """ 81 90 Constructor of the SpikeTrain object … … 84 93 SpikeTrain 85 94 """ 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 101 97 self.spike_times = self.interval.slice_times(numpy.array(spike_times, numpy.float32)) 102 98 103 # We sort the spike_times. May be slowe r, but is necessary by the way for quite a104 # lot of methods... 99 # We sort the spike_times. May be slowe, but required for 100 # several analysis methods 105 101 self.spike_times = numpy.sort(self.spike_times, kind="quicksort") 106 # Here we deal with the t_start and t_stop values if the SpikeTrain107 # is empty, with only one element or several elements, if we108 # need to guess t_start and t_stop109 # no element : t_start = 0, t_stop = 0.1110 # 1 element : t_start = time, t_stop = time + 0.1111 # several : t_start = min(time), t_stop = max(time)112 113 102 114 103 def __str__(self): … … 245 234 return numpy.diff(self.spike_times) 246 235 247 def mean_rate(self, interval=None):236 def mean_rate(self, t_start=None, t_stop=None, interval=None): 248 237 """ 249 238 Returns the mean firing rate between t_start and t_stop, in Hz … … 256 245 34.2 257 246 """ 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): 267 255 """ 268 256 Return the coefficient of variation of the isis. … … 283 271 284 272 """ 285 isi = self.isi( )273 isi = self.isi(t_start, t_stop, interval) 286 274 if len(isi) > 0: 287 275 return numpy.std(isi)/numpy.mean(isi) … … 290 278 return numpy.nan 291 279 292 def cv_kl(self, bins=100 ):280 def cv_kl(self, bins=100, t_start=None, t_stop=None, interval=None): 293 281 """ 294 282 Provides a measure for the coefficient of variation to describe the … … 313 301 314 302 """ 315 isi = self.isi( ) / 1000.303 isi = self.isi(t_start, t_stop, interval) / 1000. 316 304 if len(isi) < 2: 317 305 logging.debug("Warning, a CV can't be computed because there are not enough spikes") … … 332 320 333 321 334 def fano_factor_isi(self ):322 def fano_factor_isi(self, t_start=None, t_stop=None, interval=None): 335 323 """ 336 324 Return the fano factor of this spike trains ISI. … … 343 331 isi, cv_isi 344 332 """ 345 isi = self.isi( )333 isi = self.isi(t_start, t_stop, interval) 346 334 if len(isi) > 0: 347 335 fano = numpy.var(isi)/numpy.mean(isi) … … 371 359 return axis 372 360 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={}): 374 362 """ 375 363 Generate a raster plot with the SpikeTrain in a subwindow of interest, … … 390 378 """ 391 379 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) 397 384 else: 398 385 spikes = self.spike_times … … 731 718 raise Exception(errmsg) 732 719 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 735 759 736 760 class SpikeList(object):
