| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import numpy as np |
| 6 |
|
| 7 |
|
| 8 |
class SpikeTrain(object): |
| 9 |
""" Class representing spike trains for the PySpike Module.""" |
| 10 |
|
| 11 |
def __init__(self, spike_times, edges, is_sorted=True): |
| 12 |
""" Constructs the SpikeTrain. |
| 13 |
|
| 14 |
:param spike_times: ordered array of spike times. |
| 15 |
:param edges: The edges of the spike train. Given as a pair of floats |
| 16 |
(T0, T1) or a single float T1, where then T0=0 is |
| 17 |
assumed. |
| 18 |
:param is_sorted: If `False`, the spike times will sorted by `np.sort`. |
| 19 |
|
| 20 |
""" |
| 21 |
|
| 22 |
|
| 23 |
if is_sorted: |
| 24 |
self.spikes = np.array(spike_times, dtype=float) |
| 25 |
else: |
| 26 |
self.spikes = np.sort(np.array(spike_times, dtype=float)) |
| 27 |
|
| 28 |
try: |
| 29 |
self.t_start = float(edges[0]) |
| 30 |
self.t_end = float(edges[1]) |
| 31 |
except: |
| 32 |
self.t_start = 0.0 |
| 33 |
self.t_end = float(edges) |
| 34 |
|
| 35 |
def __getitem__(self, index): |
| 36 |
""" Returns the time of the spike given by index. |
| 37 |
|
| 38 |
:param index: Index of the spike. |
| 39 |
:return: spike time. |
| 40 |
""" |
| 41 |
return self.spikes[index] |
| 42 |
|
| 43 |
def __len__(self): |
| 44 |
""" Returns the number of spikes. |
| 45 |
|
| 46 |
:return: Number of spikes. |
| 47 |
""" |
| 48 |
return len(self.spikes) |
| 49 |
|
| 50 |
def sort(self): |
| 51 |
""" Sorts the spike times of this spike train using `np.sort` |
| 52 |
""" |
| 53 |
self.spikes = np.sort(self.spikes) |
| 54 |
|
| 55 |
def copy(self): |
| 56 |
""" Returns a copy of this spike train. |
| 57 |
Use this function if you want to create a real (deep) copy of this |
| 58 |
spike train. Simple assignment `t2 = t1` does not create a copy of the |
| 59 |
spike train data, but a reference as `numpy.array` is used for storing |
| 60 |
the data. |
| 61 |
|
| 62 |
:return: :class:`.SpikeTrain` copy of this spike train. |
| 63 |
|
| 64 |
""" |
| 65 |
return SpikeTrain(self.spikes.copy(), [self.t_start, self.t_end]) |
| 66 |
|
| 67 |
def get_spikes_non_empty(self): |
| 68 |
"""Returns the spikes of this spike train with auxiliary spikes in case |
| 69 |
of empty spike trains. |
| 70 |
""" |
| 71 |
if len(self.spikes) < 1: |
| 72 |
return np.unique(np.insert([self.t_start, self.t_end], 1, |
| 73 |
self.spikes)) |
| 74 |
else: |
| 75 |
return self.spikes |