1
- """autocorrelation code adapted from ibllib
2
- https://github.com/int-brain-lab/ibllib
1
+ """Code adapted from International Brain Laboratory, T. (2021). ibllib [Computer software]. https://github.com/int-brain-lab/ibllib
3
2
"""
4
3
5
4
import numpy as np
6
5
7
6
8
- def _index_of (arr , lookup ):
9
- """Replace scalars in an array by their indices in a lookup table.
7
+ def _index_of (arr : np . ndarray , lookup : np . ndarray ):
8
+ """Replace scalars in an array by their indices in a lookup table."""
10
9
11
- Implicitly assume that:
12
-
13
- * All elements of arr and lookup are non-negative integers.
14
- * All elements or arr belong to lookup.
15
-
16
- This is not checked for performance reasons.
17
-
18
- """
19
- # Equivalent of np.digitize(arr, lookup) - 1, but much faster.
20
- # TODO: assertions to disable in production for performance reasons.
21
- # TODO: np.searchsorted(lookup, arr) is faster on small arrays with large
22
- # values
23
10
lookup = np .asarray (lookup , dtype = np .int32 )
24
11
m = (lookup .max () if len (lookup ) else 0 ) + 1
25
12
tmp = np .zeros (m + 1 , dtype = int )
@@ -32,7 +19,9 @@ def _index_of(arr, lookup):
32
19
33
20
def _increment (arr , indices ):
34
21
"""Increment some indices in a 1D vector of non-negative integers.
35
- Repeated indices are taken into account."""
22
+ Repeated indices are taken into account.
23
+ """
24
+
36
25
bbins = np .bincount (indices )
37
26
arr [: len (bbins )] += bbins
38
27
return arr
@@ -63,24 +52,22 @@ def _symmetrize_correlograms(correlograms):
63
52
return np .dstack ((sym , correlograms ))
64
53
65
54
66
- def xcorr (spike_times , spike_clusters , bin_size = None , window_size = None ):
55
+ def xcorr (
56
+ spike_times : np .ndarray ,
57
+ spike_clusters : np .ndarray ,
58
+ bin_size : float ,
59
+ window_size : int ,
60
+ ) -> np .ndarray :
67
61
"""Compute all pairwise cross-correlograms among the clusters appearing in `spike_clusters`.
68
62
69
- Parameters
70
- ----------
71
-
72
- :param spike_times: Spike times in seconds.
73
- :type spike_times: array-like
74
- :param spike_clusters: Spike-cluster mapping.
75
- :type spike_clusters: array-like
76
- :param bin_size: Size of the bin, in seconds.
77
- :type bin_size: float
78
- :param window_size: Size of the window, in seconds.
79
- :type window_size: float
80
-
81
- Returns an `(n_clusters, n_clusters, winsize_samples)` array with all pairwise
82
- cross-correlograms.
63
+ Args:
64
+ spike_times (np.ndarray): Spike times in seconds.
65
+ spike_clusters (np.ndarray): Spike-cluster mapping.
66
+ bin_size (float): Size of the time bin in seconds.
67
+ window_size (int): Size of the correlogram window in seconds.
83
68
69
+ Returns:
70
+ np.ndarray: cross-correlogram array
84
71
"""
85
72
assert np .all (np .diff (spike_times ) >= 0 ), "The spike times must be increasing."
86
73
assert spike_times .ndim == 1
@@ -140,21 +127,16 @@ def xcorr(spike_times, spike_clusters, bin_size=None, window_size=None):
140
127
return _symmetrize_correlograms (correlograms )
141
128
142
129
143
- def acorr (spike_times , bin_size = None , window_size = None ):
144
- """Compute the auto-correlogram of a neuron.
145
-
146
- Parameters
147
- ----------
148
-
149
- :param spike_times: Spike times in seconds.
150
- :type spike_times: array-like
151
- :param bin_size: Size of the bin, in seconds.
152
- :type bin_size: float
153
- :param window_size: Size of the window, in seconds.
154
- :type window_size: float
130
+ def acorr (spike_times : np .ndarray , bin_size : float , window_size : int ) -> np .ndarray :
131
+ """Compute the auto-correlogram of a unit.
155
132
156
- Returns an `(winsize_samples,)` array with the auto-correlogram.
133
+ Args:
134
+ spike_times (np.ndarray): Spike times in seconds.
135
+ bin_size (float, optional): Size of the time bin in seconds.
136
+ window_size (int, optional): Size of the correlogram window in seconds.
157
137
138
+ Returns:
139
+ np.ndarray: auto-correlogram array (winsize_samples,)
158
140
"""
159
141
xc = xcorr (
160
142
spike_times ,
0 commit comments