Repository navigation
Expand file tree
/
Copy pathspiketrain.py
More file actions
74 lines (60 loc) · 2.48 KB
/
Copy pathspiketrain.py
File metadata and controls
74 lines (60 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Code ported from nept: https://github.com/vandermeerlab/nept/blob/master/nept/core/spiketrain.py
import numpy as np
class SpikeTrain:
"""A set of spike times associated with an individual putative neuron.
Parameters
----------
time : np.array
label : str or None, optional
Information pertaining to the source of the spiketrain.
Attributes
----------
time : np.array
With shape (n_samples,).
label : str or None
Information pertaining to the source of the spiketrain.
"""
def __init__(self, time, label=None):
time = np.squeeze(time).astype(float)
if time.shape == ():
time = time[..., np.newaxis]
if time.ndim != 1:
raise ValueError("time must be a vector")
if label is not None and not isinstance(label, str):
raise ValueError("label must be a string")
self.time = np.sort(time)
self.label = label
def __getitem__(self, idx):
return SpikeTrain(self.time[idx], self.label)
@property
def n_spikes(self):
"""(int) Number of samples."""
return self.time.size
def time_slice(self, t_starts, t_stops):
"""Creates a new object corresponding to the time slice of
the original between (and including) times t_start and t_stop. Setting
either parameter to None uses infinite endpoints for the time interval.
Parameters
----------
spiketrain : nept.SpikeTrain
t_starts : float or list or None
t_stops : float or list or None
Returns
-------
sliced_spiketrain : nept.SpikeTrain
"""
if isinstance(t_starts, (int, float)) or t_starts is None:
t_starts = [t_starts]
if any(element is None for element in t_starts):
t_starts = [min(self.time) if t_start is None else t_start for t_start in t_starts]
if isinstance(t_stops, (int, float)) or t_stops is None:
t_stops = [t_stops]
if any(element is None for element in t_stops):
t_stops = [max(self.time) if t_start is None else t_start for t_start in t_stops]
if len(t_starts) != len(t_stops):
raise ValueError("must have same number of start and stop times")
indices = []
for t_start, t_stop in zip(t_starts, t_stops):
indices.append((self.time >= t_start) & (self.time <= t_stop))
indices = np.any(np.column_stack(indices), axis=1)
return self[indices]