"""What batch integration asks, as a small computed toy (not benchmark data, so the label says schematic). Three cell types are sampled in two batches; the second batch is shifted, as a technical batch effect would shift it. Before: each cell type splits into two islands, one per batch. After: a per-batch location correction (each batch's mean moved to the common mean, the simplest form of what ComBat does) mixes the batches and keeps the types apart. Checks: of each cell's 5 nearest neighbours, the share from the other batch (batch mixing) and from the same cell type (identity kept), before and after. """ import math import random from pathlib import Path import kit rnd = random.Random(7) TYPES = [(-1.3, 0.8, 0.25, 0.16, 0.5), (1.3, 0.9, 0.23, 0.17, -0.4), (-0.05, -1.0, 0.27, 0.15, 0.1)] # centre, spread, tilt SHIFT = (0.8, -0.6) PER = 7 # cells per type per batch cells = [] # (x, y, type, batch) for t, (cx, cy, sx, sy, a) in enumerate(TYPES): for b in (0, 1): for _ in range(PER): u, v = rnd.gauss(0, sx), rnd.gauss(0, sy) x, y = cx + u * math.cos(a) - v * math.sin(a), cy + u * math.sin(a) + v * math.cos(a) cells.append((x + b * SHIFT[0], y + b * SHIFT[1], t, b)) mean = lambda pts: (sum(p[0] for p in pts) / len(pts), sum(p[1] for p in pts) / len(pts)) gx, gy = mean(cells) bm = [mean([c for c in cells if c[3] == b]) for b in (0, 1)] after = [(x - bm[b][0] + gx, y - bm[b][1] + gy, t, b) for x, y, t, b in cells] def knn_shares(pts, k=5): mix = keep = 0 for p in pts: nn = sorted((q for q in pts if q is not p), key=lambda q: math.dist(p[:2], q[:2]))[:k] mix += sum(q[3] != p[3] for q in nn) / k keep += sum(q[2] == p[2] for q in nn) / k return mix / len(pts), keep / len(pts) m0, k0 = knn_shares(cells) m1, k1 = knn_shares(after) assert m0 < 0.1 and m1 > 0.35 and k1 > 0.95, (m0, k0, m1, k1) # two panels side by side, at one scale span = lambda pts, k: max(p[k] for p in pts) - min(p[k] for p in pts) scale = min(min(250 / span(pts, 0), 228 / span(pts, 1)) for pts in (cells, after)) def place(pts, left): xs, ys = [p[0] for p in pts], [p[1] for p in pts] ox = left + 140 - scale * (max(xs) + min(xs)) / 2 oy = 128 + scale * (max(ys) + min(ys)) / 2 return [(ox + scale * x, oy - scale * y, t, b) for x, y, t, b in pts] P0, P1 = place(cells, 18), place(after, 342) def hull(pts): pts, out = sorted(set(pts)), [] for seq in (pts, pts[::-1]): h = [] for p in seq: while len(h) >= 2 and (h[-1][0] - h[-2][0]) * (p[1] - h[-2][1]) - (h[-1][1] - h[-2][1]) * (p[0] - h[-2][0]) <= 0: h.pop() h.append(p) out += h[:-1] return out body = [kit.line(306, 128, 334, 128, 'faint-s', 2.5), kit.polygon([(342, 128), (330, 121), (330, 135)], 'faint')] for P in (P0, P1): # each cell type as one soft region: before it holds two islands, after one body.append('' + ''.join( kit.polygon(hull([(x, y) for x, y, t_, b in P if t_ == t]), 'f', ' stroke="var(--f)" stroke-width="30" stroke-linejoin="round"') for t in range(len(TYPES))) + '') for P in (P0, P1): for x, y, t, b in sorted(P, key=lambda p: (p[3], p[1])): body.append(kit.dot(x, y, 7, 'sand' if b else 'ink')) kit.write(__file__, 'life-health', ''.join(body), label='3 types × 2 batches, before → after · schematic', title='Toy: three cell types from two batches, split by batch before integration and mixed after') print(f'batch mixing {m0:.2f} → {m1:.2f}, same type among neighbours {k0:.2f} → {k1:.2f}')