"""Protocol 018: shared-fluctuation measures that do not depend on how many spikes were detected.

For a spike count with mean m and variance V, the latent-rate excess variance (V - m) / m^2 is unchanged when
spikes are deleted independently at random with a fixed probability: thinning gives m' = p m and
V' = p^2 V + p (1 - p) m, so (V' - m') / m'^2 = (V - m) / m^2. The normalised covariance of two counts,
Cov / (m1 m2), is unchanged even when the two are thinned with different probabilities. Both describe
fluctuations of the underlying rate shared within a bin, not the number of detected spikes.

Counts are taken in bins of width T inside 1 s segments only, so bins never straddle segments. The
within-segment versions measure fluctuations around each segment's own mean, so slow drift and changes in the
set of recorded sites between segments do not count as shared fluctuations.

The primary measure, within_segment_pairwise, keeps only cross-unit terms. The pooled excess variance of a
summed count would also contain each unit's own (private) excess variance with weight about 1/N, which is
large for a dozen multiunit sites and negligible for thousands of model neurons; it is kept as a secondary.
"""
import numpy as np

SEGMENT_S = 1.0
BIN_SIZES_S = (0.01, 0.05, 0.1, 0.25, 1.0)
PRIMARY_BIN_S = 0.1


def bin_edges(segment_starts, bin_s):
    """Edges of the bins inside each 1 s segment, as (segments, bins + 1) and the flat bin count per segment."""
    per = int(round(SEGMENT_S / bin_s))
    if abs(per * bin_s - SEGMENT_S) > 1e-9:
        raise ValueError(f'bin {bin_s} s does not divide the {SEGMENT_S} s segment')
    starts = np.asarray(segment_starts, float)
    return starts[:, None] + np.arange(per + 1) * bin_s, per


def segment_counts(times, segment_starts, bin_s):
    """Counts of one spike train in every bin of every segment: an array (segments, bins per segment)."""
    edges, per = bin_edges(segment_starts, bin_s)
    t = np.sort(np.asarray(times, float))
    idx = np.searchsorted(t, edges, side='left')
    return np.diff(idx, axis=1)


def excess_cv2(counts):
    """(Var - mean) / mean^2 over all bins given (any shape); nan if the mean is 0."""
    c = np.asarray(counts, float).ravel()
    m = c.mean()
    if m <= 0 or c.size < 2:
        return float('nan')
    return float((c.var(ddof=1) - m) / m ** 2)


def within_segment_excess_cv2(seg_counts):
    """Latent-rate excess variance inside segments, robust to slow drift and to changes between segments.
    seg_counts is (segments, bins per segment). With B bins per segment and segment means nbar_j:
        [sum_j sum_b (n_jb - nbar_j)^2 - sum_j (B - 1) nbar_j] / [sum_j (B - 1) (nbar_j^2 - nbar_j / B)]
    The numerator estimates the summed within-segment latent variance (the Poisson part removed); the
    denominator the summed squared means. Thinning with a fixed probability scales both by p^2."""
    c = np.asarray(seg_counts, float)
    if c.ndim != 2 or c.shape[1] < 2:
        return float('nan')
    B = c.shape[1]
    nbar = c.mean(axis=1)
    num = ((c - nbar[:, None]) ** 2).sum() - ((B - 1) * nbar).sum()
    den = ((B - 1) * (nbar ** 2 - nbar / B)).sum()
    return float(num / den) if den > 0 else float('nan')


def within_segment_normalised_covariance(seg_counts_by_unit, min_spikes=0):
    """Mean over unit pairs of the within-segment covariance normalised by the product of segment means:
        sum_j sum_b (n_jb - nbar_j)(m_jb - mbar_j) / sum_j (B - 1) nbar_j mbar_j
    seg_counts_by_unit is (units, segments, bins). Units with fewer than min_spikes spikes are left out.
    Returns (mean, number of pairs)."""
    c = np.asarray(seg_counts_by_unit, float)
    keep = c.sum(axis=(1, 2)) >= max(min_spikes, 1)
    c = c[keep]
    if len(c) < 2:
        return float('nan'), 0
    B = c.shape[2]
    dev = c - c.mean(axis=2, keepdims=True)
    nbar = c.mean(axis=2)
    vals = []
    for i in range(len(c)):
        for k in range(i + 1, len(c)):
            den = ((B - 1) * nbar[i] * nbar[k]).sum()
            if den > 0:
                vals.append((dev[i] * dev[k]).sum() / den)
    return (float(np.mean(vals)) if vals else float('nan')), len(vals)


def within_segment_pairwise(seg_counts_by_unit):
    """Primary measure: the shared latent-rate fluctuation between units, pooled over all unit pairs and
    measured inside segments. seg_counts_by_unit is (units, segments, bins). With dev_ijb = n_ijb - nbar_ij:
        num = sum_j sum_b [ (sum_i dev_ijb)^2 - sum_i dev_ijb^2 ]       (= the sum over pairs i != k)
        den = sum_j (B - 1) [ (sum_i nbar_ij)^2 - sum_i nbar_ij^2 ]
    Only cross-unit terms enter, so each unit's private variability (bursting, private rate wander, Poisson
    noise) does not count, and the value does not depend on the number of units. Each pair term is unchanged
    when its two units are thinned independently; with unequal thinning only the pair weights change."""
    c = np.asarray(seg_counts_by_unit, float)
    if c.ndim != 3 or c.shape[0] < 2 or c.shape[2] < 2:
        return float('nan')
    B = c.shape[2]
    nbar = c.mean(axis=2)                                   # (units, segments)
    dev = c - nbar[:, :, None]
    s = dev.sum(axis=0)                                     # (segments, bins)
    num = (s ** 2).sum() - (dev ** 2).sum()
    den = (B - 1) * ((nbar.sum(axis=0) ** 2) - (nbar ** 2).sum(axis=0)).sum()
    return float(num / den) if den > 0 else float('nan')


def bootstrap_segments(measure, seg_counts_by_unit, draws, rng):
    """measure() on `draws` resamples of the segments with replacement (axis 1 of (units, segments, bins))."""
    c = np.asarray(seg_counts_by_unit, float)
    k = c.shape[1]
    return np.array([measure(c[:, rng.integers(0, k, k)]) for _ in range(draws)])


def shuffle_within_segments(seg_counts_by_unit, rng):
    """Counting-noise null: each unit's spikes in a segment redistributed uniformly over its bins, which keeps
    every unit's segment counts and removes all within-segment fluctuation."""
    c = np.asarray(seg_counts_by_unit, int)
    B = c.shape[2]
    return rng.multinomial(c.sum(axis=2), np.full(B, 1.0 / B))


def local_draw_indices(segment_starts, n_segments, draws, rng):
    """Index sets of the n_segments segments nearest in time to a randomly chosen segment of the group."""
    starts = np.asarray(segment_starts, float)
    if len(starts) < n_segments:
        return []
    out = []
    for _ in range(draws):
        centre = starts[rng.integers(0, len(starts))]
        out.append(np.argsort(np.abs(starts - centre), kind='stable')[:n_segments])
    return out


def cross_site_coincidences(trains, window_ticks=2, shoulder_ticks=(10, 50), min_sites=3, tick_s=1e-4):
    """Common-mode artifact check for spike trains of several sites, in integer ticks of the data's 0.1 ms
    resolution so that window edges do not depend on float rounding. Returns:
      masks             per train, the spikes of every event in which at least min_sites different sites fire
                        within +-window_ticks of one spike; all copies of an event are marked;
      peak_to_shoulder  the density of cross-site spike pairs at |lag| <= window_ticks divided by their density at
                        shoulder_ticks[0] <= |lag| <= shoulder_ticks[1] (per tick); rate fluctuations on a 100 ms
                        scale are flat at a few ms, so values well above 1 point to near-simultaneous events;
      flagged_fraction, events (connected groups of marked spikes).
    """
    k = [np.rint(np.asarray(x, float) / tick_s).astype(np.int64) for x in trains]
    ticks = np.concatenate(k) if k else np.array([], np.int64)
    site = np.concatenate([np.full(len(x), i) for i, x in enumerate(k)]) if k else np.array([], int)
    order = np.argsort(ticks, kind='stable')
    ts, ss = ticks[order], site[order]
    n = len(ts)
    w, (lo_s, hi_s) = window_ticks, shoulder_ticks
    seed = np.zeros(n, bool)
    peak = shoulder = 0
    lo = 0
    for i in range(n):
        while ts[i] - ts[lo] > hi_s:
            lo += 1
        near = set()
        j = lo
        while j < n and ts[j] - ts[i] <= hi_s:
            if j != i and ss[j] != ss[i]:
                d = abs(int(ts[j] - ts[i]))
                if d <= w:
                    peak += 1
                    near.add(ss[j])
                elif lo_s <= d <= hi_s:
                    shoulder += 1
            j += 1
        if len(near) + 1 >= min_sites:
            seed[i] = True
    flag = seed.copy()                                    # every spike near a seed on another site joins the event
    for i in np.flatnonzero(seed):
        j = i
        while j > 0 and ts[i] - ts[j - 1] <= w:
            j -= 1
        while j < n and ts[j] - ts[i] <= w:
            if ss[j] != ss[i]:
                flag[j] = True
            j += 1
    flags = np.empty(n, bool)
    flags[order] = flag
    masks, pos = [], 0
    for x in k:
        masks.append(flags[pos:pos + len(x)])
        pos += len(x)
    peak_density = peak / (2 * w + 1)
    shoulder_density = shoulder / (2 * (hi_s - lo_s + 1))
    ratio = float(peak_density / shoulder_density) if shoulder else float('nan')
    marked = ts[flag]
    events = int(1 + np.count_nonzero(np.diff(marked) > w)) if len(marked) else 0
    return masks, ratio, float(flag.mean()) if n else 0.0, events


def mean_normalised_covariance(counts_by_unit):
    """Mean over unit pairs of Cov(n_i, n_j) / (m_i m_j); counts_by_unit is (units, bins). Units with no spikes
    are left out. Returns (mean, number of pairs)."""
    c = np.asarray(counts_by_unit, float)
    m = c.mean(axis=1)
    c, m = c[m > 0], m[m > 0]
    if len(m) < 2:
        return float('nan'), 0
    cov = np.cov(c, ddof=1)
    norm = cov / np.outer(m, m)
    iu = np.triu_indices(len(m), k=1)
    return float(norm[iu].mean()), int(len(iu[0]))


def population_counts(trains, segment_starts, bin_s):
    """Pooled counts of many spike trains: (segments, bins per segment)."""
    pooled = np.concatenate([np.asarray(t, float) for t in trains]) if trains else np.array([])
    return segment_counts(pooled, segment_starts, bin_s)


def resampled(measure, seg_counts, n_segments, draws, rng):
    """measure() of `draws` random sets of n_segments distinct segments (rows of seg_counts)."""
    seg_counts = np.asarray(seg_counts, float)
    k = len(seg_counts)
    if k < n_segments:
        return np.array([])
    return np.array([measure(seg_counts[rng.choice(k, n_segments, replace=False)]) for _ in range(draws)])


def resampled_cv2(seg_counts, n_segments, draws, rng):
    return resampled(excess_cv2, seg_counts, n_segments, draws, rng)
