"""Placement of whole clusters with no overlap, for the nulls A' and B' (see RULES.txt, DEVIATIONS 6). Real clusters are, by their definition, more than W days apart. A cluster may not be put where its span comes within W days of a cluster already placed. Clusters are placed in random order; a rejected position is drawn again. """ import numpy as np from numba import njit @njit def place_jitter(n, anchor, span, w, lo, hi, seed, r, period, phase, jit): """Uniform shift in [-jit, jit], reflected into [lo, hi]. r > 0 adds the acceptance weight 1 + 2 r cos(2 pi t / period - phase), for the power test. Returns shifts (n, clusters), failures.""" np.random.seed(seed) nc = anchor.size out = np.zeros((n, nc), dtype=np.int64) fails = 0 off = 1000 size = hi + 3000 + off for b in range(n): occ = np.zeros(size, dtype=np.bool_) order = np.random.permutation(nc) for c in order: x = anchor[c] placed = False for _ in range(2000): x = anchor[c] + np.random.randint(-jit, jit + 1) if x < lo: x = 2 * lo - x if x > hi: x = 2 * hi - x if r > 0.0: if np.random.random() > (1.0 + 2.0 * r * np.cos(2.0 * np.pi * x / period - phase)) / (1.0 + 2.0 * r): continue if not occ[max(x - w + off, 0) : x + span[c] + w + off + 1].any(): placed = True break if not placed: fails += 1 occ[x + off : x + span[c] + off + 1] = True out[b, c] = x - anchor[c] return out, fails @njit def place_months(n, anchor, span, w, ccycle, c_off, m_start, m_nd, m_cdf, last, seed): """A cluster's first event goes to a date in its own cycle: month by the cumulative weights m_cdf[c_off[c]:c_off[c+1]], uniform within the month. Returns shifts (n, clusters), failures.""" np.random.seed(seed) nc = anchor.size out = np.zeros((n, nc), dtype=np.int64) fails = 0 off = 1000 size = last + 3000 + off for b in range(n): occ = np.zeros(size, dtype=np.bool_) order = np.random.permutation(nc) for c in order: k = ccycle[c] a0, a1 = c_off[k], c_off[k + 1] x = anchor[c] placed = False for _ in range(2000): i = a0 + np.searchsorted(m_cdf[a0:a1], np.random.random()) if i >= a1: i = a1 - 1 x = m_start[i] + int(np.random.random() * m_nd[i]) if not occ[max(x - w + off, 0) : x + span[c] + w + off + 1].any(): placed = True break if not placed: fails += 1 occ[x + off : x + span[c] + off + 1] = True out[b, c] = x - anchor[c] return out, fails