|
| 1 | +"""Code adapted from International Brain Laboratory, T. (2021). ibllib [Computer software]. https://github.com/int-brain-lab/ibllib |
| 2 | +""" |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | + |
| 7 | +def _index_of(arr: np.ndarray, lookup: np.ndarray): |
| 8 | + """Replace scalars in an array by their indices in a lookup table.""" |
| 9 | + |
| 10 | + lookup = np.asarray(lookup, dtype=np.int32) |
| 11 | + m = (lookup.max() if len(lookup) else 0) + 1 |
| 12 | + tmp = np.zeros(m + 1, dtype=int) |
| 13 | + # Ensure that -1 values are kept. |
| 14 | + tmp[-1] = -1 |
| 15 | + if len(lookup): |
| 16 | + tmp[lookup] = np.arange(len(lookup)) |
| 17 | + return tmp[arr] |
| 18 | + |
| 19 | + |
| 20 | +def _increment(arr, indices): |
| 21 | + """Increment some indices in a 1D vector of non-negative integers. |
| 22 | + Repeated indices are taken into account. |
| 23 | + """ |
| 24 | + |
| 25 | + bbins = np.bincount(indices) |
| 26 | + arr[: len(bbins)] += bbins |
| 27 | + return arr |
| 28 | + |
| 29 | + |
| 30 | +def _diff_shifted(arr, steps=1): |
| 31 | + return arr[steps:] - arr[: len(arr) - steps] |
| 32 | + |
| 33 | + |
| 34 | +def _create_correlograms_array(n_clusters, winsize_bins): |
| 35 | + return np.zeros((n_clusters, n_clusters, winsize_bins // 2 + 1), dtype=np.int32) |
| 36 | + |
| 37 | + |
| 38 | +def _symmetrize_correlograms(correlograms): |
| 39 | + """Return the symmetrized version of the CCG arrays.""" |
| 40 | + |
| 41 | + n_clusters, _, n_bins = correlograms.shape |
| 42 | + assert n_clusters == _ |
| 43 | + |
| 44 | + # We symmetrize c[i, j, 0]. |
| 45 | + # This is necessary because the algorithm in correlograms() |
| 46 | + # is sensitive to the order of identical spikes. |
| 47 | + correlograms[..., 0] = np.maximum(correlograms[..., 0], correlograms[..., 0].T) |
| 48 | + |
| 49 | + sym = correlograms[..., 1:][..., ::-1] |
| 50 | + sym = np.transpose(sym, (1, 0, 2)) |
| 51 | + |
| 52 | + return np.dstack((sym, correlograms)) |
| 53 | + |
| 54 | + |
| 55 | +def xcorr( |
| 56 | + spike_times: np.ndarray, |
| 57 | + spike_clusters: np.ndarray, |
| 58 | + bin_size: float, |
| 59 | + window_size: int, |
| 60 | +) -> np.ndarray: |
| 61 | + """Compute all pairwise cross-correlograms among the clusters appearing in `spike_clusters`. |
| 62 | +
|
| 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. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + np.ndarray: cross-correlogram array |
| 71 | + """ |
| 72 | + assert np.all(np.diff(spike_times) >= 0), "The spike times must be increasing." |
| 73 | + assert spike_times.ndim == 1 |
| 74 | + assert spike_times.shape == spike_clusters.shape |
| 75 | + |
| 76 | + # Find `binsize`. |
| 77 | + bin_size = np.clip(bin_size, 1e-5, 1e5) # in seconds |
| 78 | + |
| 79 | + # Find `winsize_bins`. |
| 80 | + window_size = np.clip(window_size, 1e-5, 1e5) # in seconds |
| 81 | + winsize_bins = 2 * int(0.5 * window_size / bin_size) + 1 |
| 82 | + |
| 83 | + # Take the cluster order into account. |
| 84 | + clusters = np.unique(spike_clusters) |
| 85 | + n_clusters = len(clusters) |
| 86 | + |
| 87 | + # Like spike_clusters, but with 0..n_clusters-1 indices. |
| 88 | + spike_clusters_i = _index_of(spike_clusters, clusters) |
| 89 | + |
| 90 | + # Shift between the two copies of the spike trains. |
| 91 | + shift = 1 |
| 92 | + |
| 93 | + # At a given shift, the mask precises which spikes have matching spikes |
| 94 | + # within the correlogram time window. |
| 95 | + mask = np.ones_like(spike_times, dtype=bool) |
| 96 | + |
| 97 | + correlograms = _create_correlograms_array(n_clusters, winsize_bins) |
| 98 | + |
| 99 | + # The loop continues as long as there is at least one spike with |
| 100 | + # a matching spike. |
| 101 | + while mask[:-shift].any(): |
| 102 | + # Interval between spike i and spike i+shift. |
| 103 | + spike_diff = _diff_shifted(spike_times, shift) |
| 104 | + |
| 105 | + # Binarize the delays between spike i and spike i+shift. |
| 106 | + spike_diff_b = np.round(spike_diff / bin_size).astype(np.int64) |
| 107 | + |
| 108 | + # Spikes with no matching spikes are masked. |
| 109 | + mask[:-shift][spike_diff_b > (winsize_bins / 2)] = False |
| 110 | + |
| 111 | + # Cache the masked spike delays. |
| 112 | + m = mask[:-shift].copy() |
| 113 | + d = spike_diff_b[m] |
| 114 | + |
| 115 | + # Find the indices in the raveled correlograms array that need |
| 116 | + # to be incremented, taking into account the spike clusters. |
| 117 | + indices = np.ravel_multi_index( |
| 118 | + (spike_clusters_i[:-shift][m], spike_clusters_i[+shift:][m], d), |
| 119 | + correlograms.shape, |
| 120 | + ) |
| 121 | + |
| 122 | + # Increment the matching spikes in the correlograms array. |
| 123 | + _increment(correlograms.ravel(), indices) |
| 124 | + |
| 125 | + shift += 1 |
| 126 | + |
| 127 | + return _symmetrize_correlograms(correlograms) |
| 128 | + |
| 129 | + |
| 130 | +def acorr(spike_times: np.ndarray, bin_size: float, window_size: int) -> np.ndarray: |
| 131 | + """Compute the auto-correlogram of a unit. |
| 132 | +
|
| 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. |
| 137 | +
|
| 138 | + Returns: |
| 139 | + np.ndarray: auto-correlogram array (winsize_samples,) |
| 140 | + """ |
| 141 | + xc = xcorr( |
| 142 | + spike_times, |
| 143 | + np.zeros_like(spike_times, dtype=np.int32), |
| 144 | + bin_size=bin_size, |
| 145 | + window_size=window_size, |
| 146 | + ) |
| 147 | + return xc[0, 0, :] |
0 commit comments