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
32"""
43
54import numpy as np
65
76
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."""
109
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
2310 lookup = np .asarray (lookup , dtype = np .int32 )
2411 m = (lookup .max () if len (lookup ) else 0 ) + 1
2512 tmp = np .zeros (m + 1 , dtype = int )
@@ -32,7 +19,9 @@ def _index_of(arr, lookup):
3219
3320def _increment (arr , indices ):
3421 """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+
3625 bbins = np .bincount (indices )
3726 arr [: len (bbins )] += bbins
3827 return arr
@@ -63,24 +52,22 @@ def _symmetrize_correlograms(correlograms):
6352 return np .dstack ((sym , correlograms ))
6453
6554
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 :
6761 """Compute all pairwise cross-correlograms among the clusters appearing in `spike_clusters`.
6862
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.
8368
69+ Returns:
70+ np.ndarray: cross-correlogram array
8471 """
8572 assert np .all (np .diff (spike_times ) >= 0 ), "The spike times must be increasing."
8673 assert spike_times .ndim == 1
@@ -140,21 +127,16 @@ def xcorr(spike_times, spike_clusters, bin_size=None, window_size=None):
140127 return _symmetrize_correlograms (correlograms )
141128
142129
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.
155132
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.
157137
138+ Returns:
139+ np.ndarray: auto-correlogram array (winsize_samples,)
158140 """
159141 xc = xcorr (
160142 spike_times ,
0 commit comments