Skip to content

corr.py

Code adapted from International Brain Laboratory, T. (2021). ibllib [Computer software]. https://github.com/int-brain-lab/ibllib

xcorr(spike_times, spike_clusters, bin_size, window_size)

Compute all pairwise cross-correlograms among the clusters appearing in spike_clusters.

Parameters:

Name Type Description Default
spike_times ndarray

Spike times in seconds.

required
spike_clusters ndarray

Spike-cluster mapping.

required
bin_size float

Size of the time bin in seconds.

required
window_size int

Size of the correlogram window in seconds.

required

Returns:

Type Description
ndarray

np.ndarray: cross-correlogram array

Source code in element_array_ephys/plotting/corr.py
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def xcorr(
    spike_times: np.ndarray,
    spike_clusters: np.ndarray,
    bin_size: float,
    window_size: int,
) -> np.ndarray:
    """Compute all pairwise cross-correlograms among the clusters appearing in `spike_clusters`.

    Args:
        spike_times (np.ndarray): Spike times in seconds.
        spike_clusters (np.ndarray): Spike-cluster mapping.
        bin_size (float): Size of the time bin in seconds.
        window_size (int): Size of the correlogram window in seconds.

    Returns:
         np.ndarray: cross-correlogram array
    """
    assert np.all(np.diff(spike_times) >= 0), "The spike times must be increasing."
    assert spike_times.ndim == 1
    assert spike_times.shape == spike_clusters.shape

    # Find `binsize`.
    bin_size = np.clip(bin_size, 1e-5, 1e5)  # in seconds

    # Find `winsize_bins`.
    window_size = np.clip(window_size, 1e-5, 1e5)  # in seconds
    winsize_bins = 2 * int(0.5 * window_size / bin_size) + 1

    # Take the cluster order into account.
    clusters = np.unique(spike_clusters)
    n_clusters = len(clusters)

    # Like spike_clusters, but with 0..n_clusters-1 indices.
    spike_clusters_i = _index_of(spike_clusters, clusters)

    # Shift between the two copies of the spike trains.
    shift = 1

    # At a given shift, the mask precises which spikes have matching spikes
    # within the correlogram time window.
    mask = np.ones_like(spike_times, dtype=bool)

    correlograms = _create_correlograms_array(n_clusters, winsize_bins)

    # The loop continues as long as there is at least one spike with
    # a matching spike.
    while mask[:-shift].any():
        # Interval between spike i and spike i+shift.
        spike_diff = _diff_shifted(spike_times, shift)

        # Binarize the delays between spike i and spike i+shift.
        spike_diff_b = np.round(spike_diff / bin_size).astype(np.int64)

        # Spikes with no matching spikes are masked.
        mask[:-shift][spike_diff_b > (winsize_bins / 2)] = False

        # Cache the masked spike delays.
        m = mask[:-shift].copy()
        d = spike_diff_b[m]

        # Find the indices in the raveled correlograms array that need
        # to be incremented, taking into account the spike clusters.
        indices = np.ravel_multi_index(
            (spike_clusters_i[:-shift][m], spike_clusters_i[+shift:][m], d),
            correlograms.shape,
        )

        # Increment the matching spikes in the correlograms array.
        _increment(correlograms.ravel(), indices)

        shift += 1

    return _symmetrize_correlograms(correlograms)

acorr(spike_times, bin_size, window_size)

Compute the auto-correlogram of a unit.

Parameters:

Name Type Description Default
spike_times ndarray

Spike times in seconds.

required
bin_size float

Size of the time bin in seconds.

required
window_size int

Size of the correlogram window in seconds.

required

Returns:

Type Description
ndarray

np.ndarray: auto-correlogram array (winsize_samples,)

Source code in element_array_ephys/plotting/corr.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def acorr(spike_times: np.ndarray, bin_size: float, window_size: int) -> np.ndarray:
    """Compute the auto-correlogram of a unit.

    Args:
        spike_times (np.ndarray): Spike times in seconds.
        bin_size (float, optional): Size of the time bin in seconds.
        window_size (int, optional): Size of the correlogram window in seconds.

    Returns:
        np.ndarray: auto-correlogram array (winsize_samples,)
    """
    xc = xcorr(
        spike_times,
        np.zeros_like(spike_times, dtype=np.int32),
        bin_size=bin_size,
        window_size=window_size,
    )
    return xc[0, 0, :]