# Chapter 11, Image data: figures for the lecture notes ch11-image-data.html.
# Follows Holmes and Huber (2019), Modern Statistics for Modern Biology, Chapter 11.
#
#   Rscript ch11_image_data.R             all figures and the demo data
#   Rscript ch11_image_data.R fig_11_28   one figure
#
# Figures reproduced: 11.1 to 11.18, 11.21, 11.22 and 11.24 to 11.32 (29 figures). The image figures
# are raster images (savefig(..., dense = TRUE)); whole photographs are block-averaged by 2 or 3 to
# keep the files small.
# Figures skipped:
#   11.19  photographs of a lymph-node biopsy reproduced from a pathology paper (not computed here)
#   11.20  scan of a stained lymph node (the image itself is not part of the data; only the cell
#          positions segmented from it are, and they are used for Figures 11.21 to 11.30)
#   11.23  photograph of rain drops (illustration)
#
# The package spatstat, which the book uses for Sections 11.15 to 11.17, is not installed here, so
# every point-process estimator (convex-hull window, kernel intensity with edge corrections, relative
# risk, the nearest-neighbour distribution G, the inhomogeneous K and L functions and the pair
# correlation function) is implemented below from its definition. Pair counting uses the kd-tree radius search
# of the package dbscan; the Voronoi propagation of Section 11.12 is a Dijkstra search on the pixel
# graph (package igraph) and reproduces EBImage::propagate pixel for pixel.
#
# Slow steps (the pair sums for K over 103,681 T cells) are cached in code/cache/ch11-*.rds.
# Data for the browser demos are written to courses/biostat/data/ch11-*.json.

.this_dir <- local({
  a <- grep("^--file=", commandArgs(FALSE), value = TRUE)
  if (length(a)) dirname(normalizePath(sub("^--file=", "", a[1]))) else getwd()
})
source(file.path(.this_dir, "common.R"))
suppressPackageStartupMessages({
  library(EBImage)
  library(genefilter)   # half.range.mode
  library(igraph)       # Dijkstra for the Voronoi propagation
  library(dbscan)       # kd-tree neighbour searches for G and K
  library(polyclip)     # inner parallel set of the window (border correction)
  library(patchwork)
  library(GGally)
})

CH <- 11
CACHE <- file.path(CODE_DIR, "cache")
dir.create(CACHE, showWarnings = FALSE)
cached <- function(name, expr) {
  f <- file.path(CACHE, paste0("ch11-", name, ".rds"))
  if (file.exists(f)) return(readRDS(f))
  val <- force(expr)
  saveRDS(val, f)
  val
}
.memo <- new.env()
memo <- function(name, expr) {
  if (!exists(name, envir = .memo, inherits = FALSE)) assign(name, force(expr), envir = .memo)
  get(name, envir = .memo)
}

# =============================================================================================
# Part 1. Images (Sections 11.2 to 11.14)
# =============================================================================================

mosquito <- function() memo("mosq", readImage(msmb_data("mosquito.png")))
hiv <- function() memo("hiv", readImage(msmb_data("hiv.png")))
nuclei_tif <- function() memo("nuc", readImage(msmb_data("nuclei.tif")))

# The HCT116 cells of Section 11.8: three 16-bit channels (DNA, alpha-tubulin, actin), stored by
# readImage as k / 65535. The scanner used 11 or 12 bits, so the book rescales by 32 and 16; these
# are powers of two and the products are exact.
cells_scaled <- function() memo("cells", {
  cells <- readImage(vapply(c("image-DAPI.tif", "image-FITC.tif", "image-Cy3.tif"), msmb_data, ""))
  cells[, , 1] <- 32 * cells[, , 1]
  cells[, , 2:3] <- 16 * cells[, , 2:3]
  cells
})

gauss_brush <- function(sigma, size = 51) makeBrush(size = size, shape = "gaussian", sigma = sigma)
disc_filter <- function(size = 21) { d <- makeBrush(size, "disc"); d / sum(d) }

# Section 11.8.2: smoothing with Gaussian weights, sigma = 1 (DNA) and 3 (tubulin, actin)
cells_smooth <- function() memo("cs", {
  cells <- cells_scaled()
  cs <- Image(dim = dim(cells))
  sg <- c(1, 3, 3)
  for (i in 1:3) cs[, , i] <- filter2(cells[, , i], gauss_brush(sg[i]))
  cs
})

# Background parameters of Section 11.13: on the log scale, the location is the half-range mode
# and the width the root mean square of the deviations to its left (see Filling the gap 11.9).
bg_pars <- function(x) {
  x <- log(x)
  loc <- half.range.mode(x)
  left <- (x - loc)[x < loc]
  wid <- sqrt(mean(left^2))
  c(loc = loc, wid = wid, thr = loc + 6 * wid)
}

# The segmentation pipeline of Sections 11.9 to 11.13 (same steps and settings as the book)
segmentation <- function() memo("seg", {
  cs <- cells_smooth()
  disc <- disc_filter(21)
  offset <- 0.02
  localBg <- filter2(cs[, , 1], disc)
  nucThresh <- cs[, , 1] - localBg > offset
  nucOpened <- EBImage::opening(nucThresh, kern = makeBrush(5, shape = "disc"))
  nucSeed <- bwlabel(nucOpened)
  nucMask <- fillHull(cs[, , 1] - localBg > 0)
  nuclei <- propagate(cs[, , 1], nucSeed, mask = nucMask)
  cellBg <- apply(cs, MARGIN = 3, FUN = bg_pars)
  cytoplasmMask <- (cs[, , 2] > exp(cellBg["thr", 2])) | nuclei | (cs[, , 3] > exp(cellBg["thr", 3]))
  cellbodies <- propagate(x = cs[, , 3], seeds = nuclei, lambda = 1.0e-2, mask = cytoplasmMask)
  list(nucThresh = nucThresh, nucOpened = nucOpened, nucSeed = nucSeed, nucMask = nucMask,
       nuclei = nuclei, cellBg = cellBg, cytoplasmMask = cytoplasmMask, cellbodies = cellbodies)
})

# ==== morphology as set operations (Section 11.10; agrees with EBImage::erode/dilate exactly) ====
# shift_img(A, dx, dy, fill)[p] = A[p + (dx, dy)], with 'fill' outside the image
shift_img <- function(A, dx, dy, fill) {
  n1 <- nrow(A); n2 <- ncol(A)
  out <- matrix(fill, n1, n2)
  i1 <- max(1, 1 + dx):min(n1, n1 + dx); j1 <- max(1, 1 + dy):min(n2, n2 + dy)
  if (length(i1) && length(j1) && min(i1) <= max(i1)) out[i1 - dx, j1 - dy] <- A[i1, j1]
  out
}
brush_offsets <- function(B) {
  c0 <- (nrow(B) + 1) / 2
  w <- which(B > 0, arr.ind = TRUE)
  cbind(w[, 1] - c0, w[, 2] - c0)
}
# erosion: p is kept iff p + b lies in A for every b of B (offsets that leave the image are ignored)
erode_set <- function(A, B) {
  A <- A > 0; o <- brush_offsets(B); R <- matrix(TRUE, nrow(A), ncol(A))
  for (k in seq_len(nrow(o))) R <- R & shift_img(A, o[k, 1], o[k, 2], TRUE)
  R
}
# dilation: p is set iff p - b lies in A for some b of B (the union of the translates A + b)
dilate_set <- function(A, B) {
  A <- A > 0; o <- brush_offsets(B); R <- matrix(FALSE, nrow(A), ncol(A))
  for (k in seq_len(nrow(o))) R <- R | shift_img(A, -o[k, 1], -o[k, 2], FALSE)
  R
}
# the rule as worded in the book: "if any pixel under the mask is background, set ALL these pixels
# to background" (this is not erosion; see the erratum note in Section 11.10 of the notes)
erode_book_wording <- function(A, B) {
  A <- A > 0; o <- brush_offsets(B); n1 <- nrow(A); n2 <- ncol(A)
  hit <- A & !erode_set(A, B)
  bi <- which(hit, arr.ind = TRUE); L <- A
  for (k in seq_len(nrow(o))) {
    ii <- bi[, 1] + o[k, 1]; jj <- bi[, 2] + o[k, 2]
    ok <- ii >= 1 & ii <= n1 & jj >= 1 & jj <= n2
    L[cbind(ii[ok], jj[ok])] <- FALSE
  }
  L
}

# connected components of a binary image with 4- or 8-connectivity (union of pixel adjacencies)
label_components <- function(A, conn = 4) {
  A <- A > 0; n1 <- nrow(A); n2 <- ncol(A)
  id <- matrix(0L, n1, n2); id[A] <- seq_len(sum(A))
  dl <- if (conn == 4) list(c(1, 0), c(0, 1)) else list(c(1, 0), c(0, 1), c(1, 1), c(1, -1))
  e <- do.call(rbind, lapply(dl, function(d) {
    s <- A & shift_img(A, d[1], d[2], FALSE)
    p <- which(s, arr.ind = TRUE)
    cbind(id[p], id[cbind(p[, 1] + d[1], p[, 2] + d[2])])
  }))
  g <- make_graph(t(e), n = sum(A), directed = FALSE)
  m <- components(g)$membership
  # number the components in the order of their first pixel (column-major), as bwlabel does
  first <- tapply(seq_along(m), m, min)
  m <- match(m, order(first))
  out <- matrix(0L, n1, n2); out[A] <- m
  out
}

# fillHull: background components (4-connected) that do not touch the image border are holes
fill_holes <- function(A) {
  A <- A > 0
  L <- label_components(!A, conn = 4)
  border <- unique(c(L[1, ], L[nrow(L), ], L[, 1], L[, ncol(L)]))
  border <- border[border > 0]
  A | (L > 0 & !(L %in% border))
}

# ==== Voronoi propagation on an image manifold (Section 11.12) ====
# Multi-source Dijkstra on the 8-neighbour pixel graph. The cost of a step from p to a neighbour q
# is ds = sqrt((dz^2 + lambda |p - q|^2) / (1 + lambda)), the metric (11.4) up to the constant
# factor 2, where dz is the sum over the 3 x 3 window of |x(p + d) - x(q + d)| (image edges
# replicated). With this dz the labels agree with EBImage::propagate on every pixel;
# zfun = "pixel" uses the plain difference x(q) - x(p) instead. Each pixel takes the label of the
# neighbour on its shortest path (ties: smallest pixel index).
propagate_dijkstra <- function(x, seeds, mask = NULL, lambda = 1e-4, zfun = c("window", "pixel"),
                               return_dist = FALSE) {
  zfun <- match.arg(zfun)
  x <- as.matrix(imageData(x)); S <- as.matrix(imageData(seeds)); n1 <- nrow(x); n2 <- ncol(x)
  M <- if (is.null(mask)) matrix(TRUE, n1, n2) else as.matrix(imageData(mask)) > 0
  M <- M | S > 0
  id <- matrix(0L, n1, n2); id[M] <- seq_len(sum(M)); N <- sum(M)
  if (zfun == "window") {
    pad <- matrix(0, n1 + 2, n2 + 2); pad[2:(n1 + 1), 2:(n2 + 1)] <- x
    pad[1, ] <- pad[2, ]; pad[n1 + 2, ] <- pad[n1 + 1, ]; pad[, 1] <- pad[, 2]; pad[, n2 + 2] <- pad[, n2 + 1]
  }
  ii <- rep(1:n1, n2); jj <- rep(1:n2, each = n1)
  EW <- lapply(list(c(1, 0), c(0, 1), c(1, 1), c(1, -1)), function(d) {
    i2 <- ii + d[1]; j2 <- jj + d[2]
    ok <- i2 >= 1 & i2 <= n1 & j2 >= 1 & j2 <= n2
    i <- ii[ok]; j <- jj[ok]; i2 <- i2[ok]; j2 <- j2[ok]
    ok <- M[cbind(i, j)] & M[cbind(i2, j2)]
    i <- i[ok]; j <- j[ok]; i2 <- i2[ok]; j2 <- j2[ok]
    if (zfun == "pixel") dz <- x[cbind(i2, j2)] - x[cbind(i, j)]
    else {
      dz <- 0
      for (a in -1:1) for (b in -1:1) dz <- dz + abs(pad[cbind(i2 + 1 + a, j2 + 1 + b)] - pad[cbind(i + 1 + a, j + 1 + b)])
    }
    list(e = cbind(id[cbind(i, j)], id[cbind(i2, j2)]), w = sqrt((dz^2 + lambda * sum(d^2)) / (1 + lambda)))
  })
  el <- do.call(rbind, lapply(EW, `[[`, "e")); ww <- unlist(lapply(EW, `[[`, "w"))
  sp <- which(S > 0 & M); src <- N + 1
  g <- make_graph(t(rbind(el, cbind(src, id[sp]))), n = N + 1, directed = FALSE)
  E(g)$weight <- c(ww, rep(0, length(sp)))
  dist <- as.numeric(distances(g, v = src, algorithm = "dijkstra"))[1:N]
  lab <- integer(N); lab[id[sp]] <- as.integer(S[sp])
  a <- c(el[, 1], el[, 2]); b <- c(el[, 2], el[, 1]); wab <- c(ww, ww)
  cand <- dist[a] + wab - dist[b]
  o <- order(b, cand, a); pick <- !duplicated(b[o])
  pred <- integer(N); pred[b[o][pick]] <- a[o][pick]
  for (v in order(dist)) if (lab[v] == 0L && is.finite(dist[v]) && pred[v] > 0L) lab[v] <- lab[pred[v]]
  out <- matrix(0L, n1, n2); out[M] <- lab
  if (return_dist) { dd <- matrix(Inf, n1, n2); dd[M] <- dist; return(list(label = out, dist = dd)) }
  out
}

# ==== drawing helpers for raster figures ====
# block-average an image (2D, or 3D with channels last) by an integer factor f
bin_down <- function(a, f) {
  a <- if (inherits(a, "Image")) imageData(a) else a
  if (f == 1) return(a)
  d <- dim(a); n1 <- (d[1] %/% f) * f; n2 <- (d[2] %/% f) * f
  one <- function(m) {
    m <- m[1:n1, 1:n2]
    m <- rowsum(m, rep(seq_len(n1 / f), each = f), reorder = FALSE)
    t(rowsum(t(m), rep(seq_len(n2 / f), each = f), reorder = FALSE)) / f^2
  }
  if (length(d) == 2) one(a) else simplify2array(lapply(seq_len(d[3]), function(k) one(a[, , k])))
}
as_rast <- function(a) {
  a <- if (inherits(a, "Image")) imageData(a) else a
  a <- pmin(pmax(a, 0), 1); a[is.na(a)] <- 0
  if (length(dim(a)) == 2) return(as.raster(t(a)))
  r <- rgb(a[, , 1], a[, , 2], a[, , 3]); dim(r) <- dim(a)[1:2]
  as.raster(t(r))
}
# a function that draws images side by side (base graphics), each with a panel label
panel_fig <- function(imgs, labels = NULL, ncol = length(imgs), interpolate = FALSE, cex = 0.85) {
  force(imgs)
  function() {
    n <- length(imgs); nr <- ceiling(n / ncol)
    op <- par(mfrow = c(nr, ncol), mar = c(0.25, 0.25, if (is.null(labels)) 0.25 else 1.35, 0.25),
              oma = c(0, 0, 0, 0), bg = "white")
    on.exit(par(op))
    for (k in seq_len(n)) {
      a <- imgs[[k]]; a <- if (inherits(a, "Image")) imageData(a) else a
      w <- dim(a)[1]; h <- dim(a)[2]
      plot.new(); plot.window(c(0, w), c(0, h), asp = 1, xaxs = "i", yaxs = "i")
      rasterImage(as_rast(a), 0, 0, w, h, interpolate = interpolate)
      if (!is.null(labels)) mtext(labels[k], side = 3, line = 0.15, cex = cex, adj = 0)
    }
  }
}
img_dims <- function(a) dim(if (inherits(a, "Image")) imageData(a) else a)[1:2]
# height in inches of a row of panels of the given width
row_height <- function(imgs, width, ncol = length(imgs), label = TRUE) {
  d <- img_dims(imgs[[1]]); nr <- ceiling(length(imgs) / ncol)
  nr * (width / ncol * d[2] / d[1] + if (label) 0.25 else 0.05)
}

# =============================================================================================
# Figures 11.1 to 11.18
# =============================================================================================

fig_11_1 <- function() {
  m <- bin_down(mosquito(), 2)
  f <- function() {
    panel_fig(list(m), interpolate = TRUE)()
    text(x = 85 / 2, y = (dim(m)[2] - 800 / 2), labels = "A mosquito", adj = 0, col = "orange", cex = 1.3)
  }
  savefig(f, "fig-11-1", ch = CH, width = 6, height = 6 * dim(m)[2] / dim(m)[1], dense = TRUE, res = 120)
}

fig_11_2 <- function() {
  h <- bin_down(hiv(), 3)
  savefig(panel_fig(list(h), interpolate = TRUE), "fig-11-2", ch = CH, width = 6,
          height = 6 * dim(h)[2] / dim(h)[1], dense = TRUE, res = 120)
}

fig_11_3 <- function() {
  nuc <- nuclei_tif()
  imgs <- lapply(1:4, function(k) bin_down(1 - imageData(nuc)[, , k], 2))
  savefig(panel_fig(imgs, labels = paste("frame", 1:4), ncol = 2, interpolate = TRUE), "fig-11-3",
          ch = CH, width = 5, height = row_height(imgs, 5, ncol = 2), dense = TRUE, res = 120)
}

fig_11_4 <- function() {
  v <- as.numeric(imageData(mosquito()))
  p <- ggplot(data.frame(v = v), aes(v)) +
    geom_histogram(breaks = seq(0, 1, length.out = 65), fill = "grey55", colour = "white", linewidth = 0.15) +
    labs(x = "pixel intensity", y = "number of pixels",
         title = sprintf("mosquito: %s pixels, range [%g, %g]", format(length(v), big.mark = ","), min(v), max(v)))
  savefig(p, "fig-11-4", ch = CH, width = 4.6, height = 3.4)
}

fig_11_5 <- function() {
  m <- imageData(mosquito())
  imgs <- list(m, imageData(EBImage::normalize(-mosquito())), m * 3, m^(1 / 3))
  imgs <- lapply(imgs, bin_down, f = 3)
  savefig(panel_fig(imgs, labels = c("(a) original", "(b) normalize(-mosq)", "(c) mosq * 3", "(d) mosq^(1/3)"),
                    ncol = 2, interpolate = TRUE), "fig-11-5", ch = CH, width = 6.4,
          height = row_height(imgs, 6.4, ncol = 2), dense = TRUE, res = 120)
}

fig_11_6 <- function() {
  m <- mosquito()
  imgs <- list(imageData(m[100:438, 112:550]), bin_down(imageData(m > 0.5) * 1, 2), bin_down(transpose(m), 2))
  savefig(panel_fig(imgs, labels = c("(a) crop [100:438, 112:550]", "(b) mosq > 0.5", "(c) transpose"),
                    ncol = 3, interpolate = TRUE), "fig-11-6", ch = CH, width = 7.2, height = 3.2,
          dense = TRUE, res = 120)
}

fig_11_7 <- function() {
  m <- mosquito()
  imgs <- list(EBImage::rotate(m, angle = 30), EBImage::translate(m, v = c(100, 170)), flip(m), flop(m))
  imgs <- lapply(imgs, function(a) bin_down(imageData(a), 3))
  savefig(panel_fig(imgs, labels = c("(a) rotate 30 degrees", "(b) translate (100, 170)", "(c) flip", "(d) flop"),
                    ncol = 2, interpolate = TRUE), "fig-11-7", ch = CH, width = 6.4, height = 5.6,
          dense = TRUE, res = 120)
}

fig_11_8 <- function() {
  cells <- imageData(cells_scaled())
  rgbimg <- abind_rgb(cells[, , 3], cells[, , 2], cells[, , 1])
  imgs <- list(cells[, , 1], cells[, , 2], cells[, , 3], rgbimg)
  savefig(panel_fig(imgs, labels = c("DAPI (DNA)", "FITC (tubulin)", "Cy3 (actin)", "overlay")),
          "fig-11-8", ch = CH, width = 8, height = row_height(imgs, 8), dense = TRUE, res = 130)
}
abind_rgb <- function(r, g, b) { a <- array(0, c(dim(r), 3)); a[, , 1] <- r; a[, , 2] <- g; a[, , 3] <- b; a }

fig_11_9 <- function() {
  nucSmooth <- filter2(getFrame(cells_scaled(), 1), gauss_brush(7))
  imgs <- list(imageData(nucSmooth))
  savefig(panel_fig(imgs, interpolate = TRUE), "fig-11-9", ch = CH, width = 3.2,
          height = row_height(imgs, 3.2, label = FALSE), dense = TRUE, res = 130)
}

fig_11_10 <- function() {
  w <- gauss_brush(7)
  s <- seq_len(ncol(w)) - 26
  d <- data.frame(s = s, w = w[26, ])
  cont <- data.frame(s = seq(-25, 25, by = 0.1))
  cont$w <- exp(-cont$s^2 / (2 * 49)) / (2 * pi * 49)
  p <- ggplot(d, aes(s, w)) +
    geom_line(data = cont, colour = COL[["orange"]], linetype = 2) +
    geom_point(size = 1.4) +
    labs(x = "column offset t (pixels)", y = "w[26, ]",
         title = "middle row of makeBrush(51, 'gaussian', sigma = 7)")
  savefig(p, "fig-11-10", ch = CH, width = 4.6, height = 3.3)
}

illumination <- function() memo("illum", {
  cs <- cells_smooth()
  py <- seq(-1, +1, length.out = dim(cs)[1])
  px <- seq(-1, +1, length.out = dim(cs)[2])
  illuminationGradient <- Image(outer(py, px, function(x, y) exp(-(x^2 + y^2))))
  nucBad <- cs[, , 1] * illuminationGradient
  localBackground <- filter2(nucBad, disc_filter(21))
  list(gradient = illuminationGradient, bad = nucBad, localBackground = localBackground,
       thresh = nucBad - localBackground > 0.02, global = nucBad > 0.2)
})

fig_11_11 <- function() {
  il <- illumination()
  imgs <- lapply(list(il$gradient, il$bad, il$localBackground, il$thresh * 1), imageData)
  savefig(panel_fig(imgs, labels = c("(a) illumination", "(b) badly illuminated", "(c) local background",
                                     "(d) adaptive threshold")),
          "fig-11-11", ch = CH, width = 8, height = row_height(imgs, 8), dense = TRUE, res = 130)
}

fig_11_12 <- function() {
  sg <- segmentation()
  set.seed(11)
  seedCol <- colorLabels(sg$nucSeed)
  set.seed(11)
  nucCol <- colorLabels(sg$nuclei)
  imgs <- list(imageData(sg$nucThresh) * 1, imageData(sg$nucOpened) * 1, imageData(seedCol),
               imageData(sg$nucMask) * 1, imageData(nucCol))
  savefig(panel_fig(imgs, labels = c("(a) nucThresh", "(b) nucOpened", "(c) nucSeed", "(d) nucMask", "(e) nuclei")),
          "fig-11-12", ch = CH, width = 8.5, height = row_height(imgs, 8.5), dense = TRUE, res = 150)
}

fig_11_13 <- function() {
  sg <- segmentation()
  zeros <- Image(dim = dim(sg$nuclei))
  voronoiExamp <- propagate(seeds = sg$nuclei, x = zeros, lambda = 100)
  voronoiPaint <- paintObjects(voronoiExamp, toRGB(1 - sg$nucOpened), col = c("grey45", NA))
  imgs <- list(imageData(voronoiPaint))
  savefig(panel_fig(imgs), "fig-11-13", ch = CH, width = 3.4, height = row_height(imgs, 3.4, label = FALSE),
          dense = TRUE, res = 150)
}

actin_log <- function() as.numeric(log(imageData(cells_smooth())[, , 3]))

fig_11_14 <- function() {
  p <- ggplot(data.frame(v = actin_log()), aes(v)) +
    geom_histogram(bins = 60, fill = "grey55", colour = "white", linewidth = 0.15) +
    labs(x = "log(cellsSmooth[, , 3])", y = "number of pixels", title = "actin channel, log scale")
  savefig(p, "fig-11-14", ch = CH, width = 4.6, height = 3.3)
}

zoom_hist <- function(lines = FALSE) {
  v <- actin_log()
  br <- seq(min(v), max(v), length.out = 301)
  p <- ggplot(data.frame(v = v), aes(v)) +
    geom_histogram(breaks = br, fill = "grey55", colour = NA) +
    coord_cartesian(xlim = c(-3.6, -3.1)) +
    labs(x = "log(cellsSmooth[, , 3])", y = "number of pixels")
  if (lines) {
    bg <- segmentation()$cellBg[, 3]
    n <- length(v); h <- diff(br)[1]
    # a normal background with the robust location and width, scaled to the histogram; its area is
    # twice the number of pixels left of the mode (the mode splits the background in halves)
    nb <- 2 * sum(v < bg[["loc"]])
    cur <- data.frame(v = seq(-3.6, -3.3, by = 0.001))
    cur$y <- nb * h * dnorm(cur$v, bg[["loc"]], bg[["wid"]])
    p <- p + geom_line(data = cur, aes(v, y), colour = COL[["blue"]], linewidth = 0.5) +
      geom_vline(xintercept = bg[["loc"]], colour = "brown", linewidth = 0.6) +
      geom_vline(xintercept = bg[["thr"]], colour = "red", linewidth = 0.6) +
      annotate("text", x = bg[["loc"]], y = Inf, label = "loc", vjust = 1.5, hjust = 1.1, colour = "brown", size = 3.2) +
      annotate("text", x = bg[["thr"]], y = Inf, label = "thr = loc + 6 wid", vjust = 1.5, hjust = -0.05, colour = "red", size = 3.2)
  }
  p
}
fig_11_15 <- function() savefig(zoom_hist(FALSE) + labs(title = "zoom, 300 bins"), "fig-11-15", ch = CH, width = 4.6, height = 3.3)
fig_11_16 <- function() savefig(zoom_hist(TRUE) + labs(title = "background location and threshold"), "fig-11-16",
                                ch = CH, width = 4.6, height = 3.3)

cell_overlays <- function() memo("overlays", {
  cells <- cells_scaled(); sg <- segmentation()
  cellsColor <- rgbImage(red = cells[, , 3], green = cells[, , 2], blue = cells[, , 1])
  nucSegOnNuc <- paintObjects(sg$nuclei, tgt = toRGB(cells[, , 1]), col = "#ffff00")
  nucSegOnAll <- paintObjects(sg$nuclei, tgt = cellsColor, col = "#ffff00")
  cellSegOnAll <- paintObjects(sg$cellbodies, tgt = nucSegOnAll, col = "#ff0080")
  list(nucSegOnNuc = nucSegOnNuc, nucSegOnAll = nucSegOnAll, cellSegOnAll = cellSegOnAll)
})

fig_11_17 <- function() {
  sg <- segmentation(); ov <- cell_overlays()
  set.seed(11)
  cbCol <- colorLabels(sg$cellbodies)
  imgs <- list(imageData(sg$cytoplasmMask) * 1, imageData(cbCol), imageData(ov$nucSegOnNuc),
               imageData(ov$nucSegOnAll), imageData(ov$cellSegOnAll))
  savefig(panel_fig(imgs, labels = c("(a) cytoplasmMask", "(b) cellbodies", "(c) nucSegOnNuc",
                                     "(d) nucSegOnAll", "(e) cellSegOnAll")),
          "fig-11-17", ch = CH, width = 8.5, height = row_height(imgs, 8.5), dense = TRUE, res = 150)
}

cell_features <- function() memo("feat", {
  cells <- cells_scaled(); sg <- segmentation()
  data.frame(meanNucInt = as.numeric(tapply(cells[, , 1], sg$nuclei, mean))[-1],
             meanActIntInNuc = as.numeric(tapply(cells[, , 3], sg$nuclei, mean))[-1],
             meanActIntInCell = as.numeric(tapply(cells[, , 3], sg$cellbodies, mean))[-1])
})

fig_11_18 <- function() {
  p <- ggpairs(cell_features(), progress = FALSE,
               lower = list(continuous = wrap("points", size = 1, alpha = 0.8)),
               diag = list(continuous = wrap("densityDiag", colour = "grey30"))) + theme_course(10)
  savefig(p, "fig-11-18", ch = CH, width = 6, height = 6)
}

# Exercise 11.3: autocorrelation by the Wiener-Khinchin theorem (circular lags), shifted so that
# lag (0, 0) is in the middle
fftshift2 <- function(m) {
  n1 <- nrow(m); n2 <- ncol(m)
  m[c((n1 %/% 2 + 1):n1, seq_len(n1 %/% 2)), c((n2 %/% 2 + 1):n2, seq_len(n2 %/% 2))]
}
autocorr2d <- function(x) {
  y <- fft(x / sum(x))
  Re(fftshift2(fft(y * Conj(y), inverse = TRUE)))
}
autocorr_all <- function() memo("ac", {
  cells <- imageData(cells_scaled())
  ac <- lapply(1:3, function(k) autocorr2d(cells[, , k]))
  names(ac) <- c("DAPI", "FITC", "Cy3")
  n1 <- dim(cells)[1]; n2 <- dim(cells)[2]
  # zero lag sits at (n1/2 + 1, n2/2 + 1) after the shift; the radius is measured from there
  r <- round(sqrt((col(ac[[1]]) - (n2 / 2 + 1))^2 + (row(ac[[1]]) - (n1 / 2 + 1))^2))
  r_book <- round(sqrt((col(ac[[1]]) - n2 / 2)^2 + (row(ac[[1]]) - n1 / 2)^2))
  list(ac = ac, r = r, r_book = r_book)
})
heat <- function(m, title) {
  d <- data.frame(x = rep(seq_len(nrow(m)), ncol(m)), y = rep(seq_len(ncol(m)), each = nrow(m)), v = as.numeric(m))
  ggplot(d, aes(x, y, fill = v)) + geom_raster() + coord_fixed(expand = FALSE) +
    scale_fill_viridis_c(name = NULL) + labs(x = NULL, y = NULL, title = title) +
    theme_course(9) + theme(legend.key.width = unit(0.25, "cm"), legend.key.height = unit(0.6, "cm"))
}
fig_11_31 <- function() {
  A <- autocorr_all()
  p <- heat(A$ac$DAPI, "(a) DAPI") + heat(A$ac$FITC, "(b) FITC") + heat(A$ac$Cy3, "(c) Cy3") +
    heat(A$r, "(d) radius r") + plot_layout(ncol = 4)
  savefig(p, "fig-11-31", ch = CH, width = 9, height = 2.9, dense = TRUE, res = 130)
}
fig_11_32 <- function() {
  A <- autocorr_all()
  d <- do.call(rbind, lapply(names(A$ac), function(w) {
    s <- tapply(as.numeric(A$ac[[w]]), as.numeric(A$r), mean)
    data.frame(channel = w, r = as.numeric(names(s)), value = as.numeric(s))
  }))
  d <- d[d$r <= 50, ]
  p <- ggplot(d, aes(r, value, colour = channel)) + geom_line(linewidth = 0.7) +
    scale_colour_manual(values = c(Cy3 = "red", FITC = "green4", DAPI = "blue")) +
    labs(x = "lag radius r (pixels)", y = "autocorrelation / mean^2", colour = NULL)
  savefig(p, "fig-11-32", ch = CH, width = 5, height = 3.4)
}

# =============================================================================================
# Part 2. Spatial point processes (Sections 11.15 to 11.17)
# =============================================================================================

lymph_node <- function() memo("ln", {
  cl <- c("T_cells", "Tumor", "DCs", "other_cells")
  do.call(rbind, lapply(cl, function(k) {
    d <- utils::read.csv(msmb_data(sprintf("99_4525D-%s.txt", k)))
    data.frame(x = d$globalX, y = d$globalY, class = k)
  }))
})

# convex hull as a counter-clockwise polygon
hull_window <- function() memo("hull", {
  ln <- lymph_node()
  h <- rev(chull(ln$x, ln$y))
  P <- data.frame(x = ln$x[h], y = ln$y[h])
  if (poly_area_signed(P) < 0) P <- P[nrow(P):1, ]
  P
})
poly_area_signed <- function(P) {
  x <- P$x; y <- P$y; x2 <- c(x[-1], x[1]); y2 <- c(y[-1], y[1])
  sum(x * y2 - x2 * y) / 2
}
poly_area <- function(P) abs(poly_area_signed(P))
# inside test for a convex counter-clockwise polygon
in_convex <- function(x, y, P) {
  ok <- rep(TRUE, length(x)); n <- nrow(P)
  for (k in 1:n) {
    a <- P[k, ]; b <- P[k %% n + 1, ]
    ok <- ok & ((b$x - a$x) * (y - a$y) - (b$y - a$y) * (x - a$x) >= 0)
  }
  ok
}
# distance from points to the boundary of a polygon
bdist_poly <- function(x, y, P) {
  d <- rep(Inf, length(x)); n <- nrow(P)
  for (k in 1:n) {
    ax <- P$x[k]; ay <- P$y[k]; bx <- P$x[k %% n + 1]; by <- P$y[k %% n + 1]
    vx <- bx - ax; vy <- by - ay; L2 <- vx^2 + vy^2
    t <- pmin(1, pmax(0, ((x - ax) * vx + (y - ay) * vy) / L2))
    d <- pmin(d, sqrt((x - ax - t * vx)^2 + (y - ay - t * vy)^2))
  }
  d
}
# area of the inner parallel set W (-) b(0, r) of a convex polygon
eroded_area <- function(P, r) {
  vapply(r, function(rr) {
    if (rr == 0) return(poly_area(P))
    o <- polyclip::polyoffset(list(x = P$x, y = P$y), -rr, jointype = "miter", miterlim = 1e6)
    if (!length(o)) return(0)
    sum(vapply(o, function(q) poly_area(data.frame(x = q$x, y = q$y)), 0))
  }, 0)
}

# ==== kernel smoothing on a pixel grid (Section 11.16.2) ====
pixel_grid <- function(P, nx = 256, ny = 256) {
  xr <- range(P$x); yr <- range(P$y)
  dx <- diff(xr) / nx; dy <- diff(yr) / ny
  xc <- xr[1] + (seq_len(nx) - 0.5) * dx; yc <- yr[1] + (seq_len(ny) - 0.5) * dy
  inside <- matrix(in_convex(rep(xc, ny), rep(yc, each = nx), P), nx, ny)
  list(xr = xr, yr = yr, nx = nx, ny = ny, dx = dx, dy = dy, xc = xc, yc = yc, inside = inside)
}
# weighted counts of points in the pixels
bin_points <- function(G, x, y, w = 1) {
  i <- pmin(G$nx, pmax(1, floor((x - G$xr[1]) / G$dx) + 1))
  j <- pmin(G$ny, pmax(1, floor((y - G$yr[1]) / G$dy) + 1))
  w <- rep_len(w, length(x))
  m <- matrix(0, G$nx, G$ny)
  s <- rowsum(w, i + (j - 1) * G$nx)
  m[as.integer(rownames(s))] <- s[, 1]
  m
}
# (m * k)(u) = sum_p m_p k(u - v_p) at the pixel centres u, Gaussian k with sd sigma, zero padded FFT
gauss_smooth_grid <- function(G, m, sigma) {
  nx <- G$nx; ny <- G$ny; Nx <- 2 * nx; Ny <- 2 * ny
  ox <- c(0:(nx - 1), -(nx):-1) * G$dx; oy <- c(0:(ny - 1), -(ny):-1) * G$dy
  k <- outer(dnorm(ox, sd = sigma), dnorm(oy, sd = sigma))
  M <- matrix(0, Nx, Ny); M[1:nx, 1:ny] <- m
  Re(fft(fft(M) * fft(k), inverse = TRUE))[1:nx, 1:ny] / (Nx * Ny)
}
# bilinear interpolation of a pixel image at points
interp_grid <- function(G, Z, x, y) {
  fx <- (x - G$xc[1]) / G$dx + 1; fy <- (y - G$yc[1]) / G$dy + 1
  fx <- pmin(G$nx, pmax(1, fx)); fy <- pmin(G$ny, pmax(1, fy))
  i0 <- pmin(G$nx - 1, floor(fx)); j0 <- pmin(G$ny - 1, floor(fy)); ax <- fx - i0; ay <- fy - j0
  Z[cbind(i0, j0)] * (1 - ax) * (1 - ay) + Z[cbind(i0 + 1, j0)] * ax * (1 - ay) +
    Z[cbind(i0, j0 + 1)] * (1 - ax) * ay + Z[cbind(i0 + 1, j0 + 1)] * ax * ay
}
# edge-correction factor e(u) = integral over W of k(u - v) dv
edge_factor <- function(G, sigma) gauss_smooth_grid(G, G$inside * G$dx * G$dy, sigma)
# kernel intensity: edge = "none", "uniform" (divide by e(u)) or "diggle" (weight 1/e(x_i))
intensity_grid <- function(G, x, y, sigma, edge = c("diggle", "uniform", "none")) {
  edge <- match.arg(edge)
  e <- edge_factor(G, sigma)
  w <- if (edge == "diggle") 1 / interp_grid(G, e, x, y) else 1
  Z <- gauss_smooth_grid(G, bin_points(G, x, y, w), sigma)
  if (edge == "uniform") Z <- Z / e
  Z[!G$inside] <- NA
  Z
}
default_sigma <- function(P) min(diff(range(P$x)), diff(range(P$y))) / 8

# ==== nearest-neighbour distance distribution G (Section 11.17) ====
G_estimates <- function(x, y, P, r) {
  n <- length(x); A <- poly_area(P); lam <- n / A
  d <- dbscan::kNN(cbind(x, y), k = 1)$dist[, 1]
  b <- bdist_poly(x, y, P)
  # reduced sample (border): points farther than r from the boundary
  rs <- vapply(r, function(rr) { k <- b >= rr; sum(d[k] <= rr) / sum(k) }, 0)
  # Kaplan-Meier: d censored by b
  tt <- pmin(d, b); ev <- d <= b
  ot <- sort(unique(tt[ev]))
  Y <- n - findInterval(ot, sort(tt), left.open = TRUE)
  dN <- as.numeric(table(factor(tt[ev], levels = ot)))
  S <- cumprod(1 - dN / Y)
  km <- 1 - c(1, S)[findInterval(r, ot) + 1]
  # Hanisch: weights 1/|W (-) d_i| on uncensored points, self-normalised
  ok <- d <= b
  ea <- eroded_area(P, sort(unique(round(d[ok], 6))))
  wts <- 1 / ea[match(round(d[ok], 6), sort(unique(round(d[ok], 6))))]
  dd <- d[ok]; o <- order(dd)
  han <- c(0, cumsum(wts[o]))[findInterval(r, dd[o]) + 1] / sum(wts)
  data.frame(r = r, km = km, rs = rs, han = han, theo = 1 - exp(-lam * pi * r^2))
}

# ==== inhomogeneous K, L and the pair correlation function (Section 11.17.1) ====
# lambda at the data points: Gaussian kernel, leave-one-out, uniform edge correction, then
# renormalised so that sum(1 / lambda_i) = |W| (the Campbell identity, Filling the gap 11.15)
lambda_at_points <- function(x, y, P, sigma, G) {
  e <- edge_factor(G, sigma)
  Z <- gauss_smooth_grid(G, bin_points(G, x, y), sigma)
  lam <- (interp_grid(G, Z, x, y) - dnorm(0, sd = sigma)^2) / interp_grid(G, e, x, y)
  lam * sum(1 / lam) / poly_area(P)
}
# Border-corrected K_inhom on several r grids at once:
#   num(r) = sum_i sum_{j != i} 1{d_ij <= r} 1{b_i >= r} / (lambda_i lambda_j)
#   "border":     num(r) / sum_i 1{b_i >= r} / lambda_i
#   "bord.modif": num(r) / |W (-) b(0, r)|
Kinhom_border <- function(x, y, lam, P, rgrids, chunk = 2000) {
  b <- bdist_poly(x, y, P)
  X <- cbind(x, y); n <- nrow(X); R <- max(unlist(rgrids))
  nums <- lapply(rgrids, function(r) numeric(length(r) + 1))
  for (s in seq(1, n, by = chunk)) {
    idx <- s:min(n, s + chunk - 1)
    fr <- dbscan::frNN(X, eps = R, query = X[idx, , drop = FALSE], sort = FALSE)
    len <- lengths(fr$id)
    i <- rep(idx, len); j <- unlist(fr$id); dij <- unlist(fr$dist)
    keep <- j != i
    i <- i[keep]; j <- j[keep]; dij <- dij[keep]
    w <- 1 / (lam[i] * lam[j])
    for (g in seq_along(rgrids)) {
      r <- rgrids[[g]]
      k0 <- findInterval(dij, r, left.open = TRUE) + 1        # first grid index with r_k >= d_ij
      k1 <- findInterval(b[i], r)                              # last grid index with r_k <= b_i
      ok <- k0 <= k1
      if (!any(ok)) next
      a1 <- rowsum(w[ok], k0[ok]); a2 <- rowsum(w[ok], k1[ok] + 1)
      v <- nums[[g]]
      v[as.integer(rownames(a1))] <- v[as.integer(rownames(a1))] + a1[, 1]
      v[as.integer(rownames(a2))] <- v[as.integer(rownames(a2))] - a2[, 1]
      nums[[g]] <- v
    }
  }
  lapply(seq_along(rgrids), function(g) {
    r <- rgrids[[g]]
    num <- cumsum(nums[[g]])[seq_along(r)]
    den_b <- vapply(r, function(rr) sum((b >= rr) / lam), 0)
    den_m <- eroded_area(P, r)
    data.frame(r = r, border = num / den_b, bord.modif = num / den_m, theo = pi * r^2)
  })
}
# g(r) = K'(r) / (2 pi r). Writing K(r) = pi r^2 J(r) gives g = J + (r / 2) J'(r); J is bounded near
# r = 0, so a smoothing spline through J (rather than through K) gives a stable derivative there.
pcf_from_K <- function(r, K, df = NULL) {
  ok <- r > 0 & is.finite(K)
  J <- K[ok] / (pi * r[ok]^2)
  sp <- if (is.null(df)) smooth.spline(r[ok], J) else smooth.spline(r[ok], J, df = df)
  g <- rep(NA_real_, length(r))
  g[ok] <- predict(sp, r[ok])$y + r[ok] / 2 * predict(sp, r[ok], deriv = 1)$y
  g
}

lymph_results <- function() cached("lymph", {
  ln <- lymph_node(); P <- hull_window(); A <- poly_area(P)
  G <- pixel_grid(P, 256, 256)
  sig <- default_sigma(P)
  Tm <- ln[ln$class == "Tumor", ]
  dens_diggle <- intensity_grid(G, Tm$x, Tm$y, sig, "diggle")
  dens_none <- intensity_grid(G, Tm$x, Tm$y, sig, "none")
  # G for the whole (unmarked) pattern
  Gr <- seq(0, 21, by = 0.05)
  Gest <- G_estimates(ln$x, ln$y, P, Gr)
  # K_inhom, L_inhom and g for the T cells
  Tc <- ln[ln$class == "T_cells", ]
  rmax <- sqrt(1000 / (pi * nrow(Tc) / A))
  lam <- lambda_at_points(Tc$x, Tc$y, P, sig, G)
  r1 <- seq(0, rmax, length.out = 513); r2 <- seq(0, 10, by = 0.2); r3 <- seq(0, 10, by = 0.05)
  K <- Kinhom_border(Tc$x, Tc$y, lam, P, list(r1, r2, r3))
  list(G = G, sigma = sig, area = A, dens_diggle = dens_diggle, dens_none = dens_none,
       Gest = Gest, rmax = rmax, lam_range = range(lam), K1 = K[[1]], K2 = K[[2]], K3 = K[[3]],
       n = table(ln$class))
})

# relative risk with sigma = 250: p_k(u) = lambda_k(u) / sum_j lambda_j(u); the edge factors cancel.
# Far from every cell the smoothed intensities are at the level of the FFT round-off, so the ratio
# is only formed where the total is above 1e-9 of its maximum.
relrisk_grid <- function(sigma = 250) memo("rr", {
  ln <- lymph_node(); G <- lymph_results()$G
  types <- c("DCs", "other_cells", "T_cells", "Tumor")
  lk <- lapply(types, function(k) { s <- ln[ln$class == k, ]; pmax(gauss_smooth_grid(G, bin_points(G, s$x, s$y), sigma), 0) })
  tot <- Reduce(`+`, lk)
  ok <- G$inside & tot > 1e-9 * max(tot)
  rr <- lapply(lk, function(z) { p <- z / tot; p[!ok] <- NA; p })
  names(rr) <- types
  rr
})

raster_df <- function(G, Z) data.frame(x = rep(G$xc, G$ny), y = rep(G$yc, each = G$nx), v = as.numeric(Z))

fig_11_21 <- function() {
  ln <- lymph_node(); d <- ln[ln$class %in% c("T_cells", "Tumor"), ]
  p <- ggplot(d, aes(x, y, colour = class)) + geom_point(shape = ".", alpha = 0.6) +
    facet_grid(. ~ class) + guides(colour = "none") + coord_fixed() +
    scale_colour_manual(values = c(T_cells = COL[["teal"]], Tumor = COL[["red"]]))
  savefig(p, "fig-11-21", ch = CH, width = 8, height = 5.2, dense = TRUE, res = 150)
}

fig_11_22 <- function() {
  P <- hull_window(); ln <- lymph_node()
  set.seed(1)
  bg <- ln[sample(nrow(ln), 4000), ]
  p <- ggplot(P, aes(x, y)) + geom_point(data = bg, colour = "grey80", shape = ".") +
    geom_polygon(fill = NA, colour = "black") + geom_point(colour = COL[["blue"]], size = 1.6) +
    coord_fixed() + labs(title = sprintf("convex hull: %d vertices, area %.4g", nrow(P), poly_area(P)))
  savefig(p, "fig-11-22", ch = CH, width = 4.6, height = 5)
}

intensity_plot <- function(Z, title) {
  L <- lymph_results(); P <- hull_window()
  ggplot(raster_df(L$G, Z * 1e4), aes(x, y, fill = v)) + geom_raster() +
    geom_polygon(data = P, aes(x, y), inherit.aes = FALSE, fill = NA, colour = "grey30") +
    scale_fill_viridis_c(name = expression(paste(hat(lambda), " x ", 10^4)), na.value = "white",
                         limits = c(0, max(L$dens_diggle, L$dens_none, na.rm = TRUE) * 1e4)) +
    coord_fixed(expand = FALSE) + labs(x = "x", y = "y", title = title)
}
fig_11_24 <- function() {
  L <- lymph_results()
  savefig(intensity_plot(L$dens_diggle, sprintf("Tumor cells, sigma = %.0f, edge corrected", L$sigma)),
          "fig-11-24", ch = CH, width = 4.4, height = 4.6, dense = TRUE)
}
fig_11_25 <- function() {
  L <- lymph_results()
  savefig(intensity_plot(L$dens_none, sprintf("Tumor cells, sigma = %.0f, no edge correction", L$sigma)),
          "fig-11-25", ch = CH, width = 4.4, height = 4.6, dense = TRUE)
}
fig_11_26 <- function() {
  L <- lymph_results(); rr <- relrisk_grid()
  d <- do.call(rbind, lapply(names(rr), function(k) cbind(raster_df(L$G, rr[[k]]), class = k)))
  p <- ggplot(d, aes(x, y, fill = v)) + geom_raster() + facet_wrap(~class, ncol = 2) +
    scale_fill_viridis_c(name = "probability", na.value = "white", limits = c(0, 1)) +
    coord_fixed(expand = FALSE) + labs(x = "x", y = "y")
  savefig(p, "fig-11-26", ch = CH, width = 6.4, height = 7, dense = TRUE)
}
fig_11_27 <- function() {
  g <- lymph_results()$Gest
  d <- rbind(data.frame(r = g$r, G = g$km, est = "Kaplan-Meier"), data.frame(r = g$r, G = g$rs, est = "border (reduced sample)"),
             data.frame(r = g$r, G = g$han, est = "Hanisch"), data.frame(r = g$r, G = g$theo, est = "Poisson"))
  d <- d[d$r <= 10, ]
  p <- ggplot(d, aes(r, G, colour = est, linetype = est)) + geom_line(linewidth = 0.6) +
    scale_colour_manual(values = c("Kaplan-Meier" = "#e41a1c", "border (reduced sample)" = "#377eb8",
                                   Hanisch = "#4daf4a", Poisson = "#984ea3")) +
    scale_linetype_manual(values = c("Kaplan-Meier" = 1, "border (reduced sample)" = 2, Hanisch = 3, Poisson = 1)) +
    labs(x = "r", y = "G(r)", colour = NULL, linetype = NULL) +
    theme(legend.position = c(0.02, 0.98), legend.justification = c(0, 1))
  savefig(p, "fig-11-27", ch = CH, width = 4.6, height = 4.4)
}
fig_11_28 <- function() {
  K <- lymph_results()$K1
  d <- rbind(data.frame(r = K$r, L = sqrt(K$bord.modif / pi), est = "modified border"),
             data.frame(r = K$r, L = sqrt(K$border / pi), est = "border"),
             data.frame(r = K$r, L = K$r, est = "Poisson: L(r) = r"))
  p <- ggplot(d, aes(r, L, colour = est)) + geom_line(linewidth = 0.6) +
    scale_colour_manual(values = c("modified border" = "#e41a1c", border = "#377eb8", "Poisson: L(r) = r" = "#4daf4a")) +
    labs(x = "r", y = expression(L[inhom](r)), colour = NULL) +
    theme(legend.position = c(0.02, 0.98), legend.justification = c(0, 1))
  savefig(p, "fig-11-28", ch = CH, width = 4.4, height = 5.2)
}
pcf_plot <- function(K, xlim = NULL, title = NULL) {
  d <- data.frame(r = K$r, g = pcf_from_K(K$r, K$bord.modif))
  p <- ggplot(d[is.finite(d$g), ], aes(r, g)) + geom_hline(yintercept = 1, colour = "#4daf4a") +
    geom_line(colour = "#e41a1c", linewidth = 0.6) + labs(x = "r", y = expression(g[inhom](r)), title = title)
  if (!is.null(xlim)) p <- p + coord_cartesian(xlim = xlim)
  p
}
fig_11_29 <- function() {
  L <- lymph_results()
  p <- pcf_plot(L$K1, title = "(a) 0 <= r <= 695") + pcf_plot(L$K1, xlim = c(0, 10), title = "(b) zoom, 0 <= r <= 10")
  savefig(p, "fig-11-29", ch = CH, width = 7.6, height = 3.6)
}
fig_11_30 <- function() {
  L <- lymph_results()
  savefig(pcf_plot(L$K2, title = "r = 0, 0.2, ..., 10"), "fig-11-30", ch = CH, width = 4.4, height = 3.6)
}

# =============================================================================================
# Demo data
# =============================================================================================

# little-endian unsigned integers, base64 encoded (the demos decode them with atob)
b64_uint <- function(v, bytes = 2) {
  v <- as.integer(round(v))
  stopifnot(all(v >= 0), all(v < 256^bytes))
  raw <- if (bytes == 1) as.raw(v) else as.raw(rbind(v %% 256L, v %/% 256L))
  jsonlite::base64_enc(raw)
}
# the T cells, tumour cells and dendritic cells in a square sub-window of the lymph node
demo_window <- function() list(x0 = 8800, y0 = 15000, side = 1200)
window_points <- function() memo("winpts", {
  ln <- lymph_node(); W <- demo_window()
  s <- ln[ln$x >= W$x0 & ln$x < W$x0 + W$side & ln$y >= W$y0 & ln$y < W$y0 + W$side, ]
  s$x <- s$x - W$x0; s$y <- s$y - W$y0
  s
})

export_demo_data <- function() {
  cells <- imageData(cells_scaled()); cs <- imageData(cells_smooth()); sg <- segmentation()
  n1 <- dim(cells)[1]; n2 <- dim(cells)[2]
  export_json(list(n1 = n1, n2 = n2, scale = 65535,
                   note = "DAPI channel of the HCT116 cells (Laufer et al. 2013), multiplied by 32 as in the book; value = q / 65535, x fastest",
                   dapi = b64_uint(cells[, , 1] * 65535, 2)), "ch11-dapi.json")
  export_json(list(n1 = n1, n2 = n2, scale = 65535,
                   note = "actin channel smoothed with a Gaussian of sd 3 (rounded to q / 65535); nuclei labels (0 = background); cytoplasm mask",
                   actin = b64_uint(cs[, , 3] * 65535, 2), nuclei = b64_uint(imageData(sg$nuclei), 1),
                   mask = b64_uint(imageData(sg$cytoplasmMask) * 1, 1), nlab = max(sg$nuclei)), "ch11-cellbodies.json")
  s <- window_points(); W <- demo_window()
  export_json(list(side = W$side, x0 = W$x0, y0 = W$y0,
                   T_cells = list(x = s$x[s$class == "T_cells"], y = s$y[s$class == "T_cells"]),
                   Tumor = list(x = s$x[s$class == "Tumor"], y = s$y[s$class == "Tumor"]),
                   other_cells = list(x = s$x[s$class == "other_cells"], y = s$y[s$class == "other_cells"])),
              "ch11-lymph-window.json")
}

# ==== K, L, G and g on a rectangle (used by the point-pattern demo and checked against it) ====
# Ripley's isotropic weight for a rectangle [0, a] x [0, b]: 2 pi / (length of the circle of radius d
# about x_i inside W), valid for d below half the shorter side
ripley_weight <- function(x, y, d, a, b) {
  e <- cbind(x, a - x, y, b - y)
  al <- acos(pmin(e / d, 1))                      # half-angle of the arc beyond each side
  out <- 2 * rowSums(al)
  for (cn in list(c(1, 3), c(1, 4), c(2, 3), c(2, 4))) {
    inside <- e[, cn[1]]^2 + e[, cn[2]]^2 < d^2
    out <- out - ifelse(inside, al[, cn[1]] + al[, cn[2]] - pi / 2, 0)
  }
  2 * pi / (2 * pi - out)
}
K_rect <- function(x, y, a, b, r, correction = c("none", "border", "isotropic", "translation")) {
  correction <- match.arg(correction)
  n <- length(x); A <- a * b
  D <- as.matrix(dist(cbind(x, y))); diag(D) <- Inf
  if (correction == "border") {
    bd <- pmin(x, a - x, y, b - y)
    return(vapply(r, function(rr) { k <- bd >= rr; if (!any(k)) return(NA_real_)
      sum(D[k, , drop = FALSE] <= rr) / sum(k) / ((n - 1) / A) }, 0))
  }
  W <- switch(correction,
              none = matrix(1, n, n),
              translation = A / outer(seq_len(n), seq_len(n), function(i, j) (a - abs(x[i] - x[j])) * (b - abs(y[i] - y[j]))),
              isotropic = { Wm <- matrix(1, n, n); for (i in seq_len(n)) Wm[i, ] <- ripley_weight(rep(x[i], n), rep(y[i], n), pmax(D[i, ], 1e-12), a, b); Wm })
  vapply(r, function(rr) A / (n * (n - 1)) * sum(W[D <= rr]), 0)
}


# ==== Section 11.5: entropy of the grey levels and of horizontal differences (bits per pixel) ====
image_entropy <- function(m) {
  q <- round(255 * m)
  H <- function(v) { p <- tabulate(v - min(v) + 1) / length(v); p <- p[p > 0]; -sum(p * log2(p)) }
  c(levels = H(as.integer(q)), differences = H(as.integer(q[-1, ] - q[-nrow(q), ])))
}

# ==== Exercise 11.5: Voronoi tessellation of the Ukrainian cities of Chapter 9 ====
# (a) planar Voronoi tiles of the classical MDS coordinates (deldir, bounding box 10% larger than the
# points); (b) nearest city by great-circle distance on a 0.05 degree grid, compared with the nearest
# city in the MDS plane after a rigid (Procrustes) alignment of the MDS map to a local projection.
ukraine_voronoi <- function() memo("ukr", {
  e <- new.env(); load(msmb_data("ukrainecities.RData"), envir = e)
  co <- as.data.frame(e$ukraine_coords)[, c("city", "lat", "lon")]
  D <- as.matrix(e$ukraine_dists)
  co <- co[match(rownames(D), co$city), ]
  mds <- cmdscale(D, k = 2)
  rg <- apply(mds, 2, range); ext <- 0.1 * apply(rg, 2, diff)
  vt <- deldir::deldir(mds[, 1], mds[, 2], rw = c(rg[, 1] + c(-1, 1) * ext[1], rg[, 2] + c(-1, 1) * ext[2]))
  tiles <- data.frame(city = rownames(D), area_km2 = vt$summary$dir.area)
  hav <- function(lat1, lon1, lat2, lon2) {
    r <- pi / 180; a <- sin((lat2 - lat1) * r / 2)^2 + cos(lat1 * r) * cos(lat2 * r) * sin((lon2 - lon1) * r / 2)^2
    2 * 6371 * asin(pmin(1, sqrt(a)))
  }
  gl <- expand.grid(lon = seq(22, 40.5, by = 0.05), lat = seq(44, 52.5, by = 0.05))
  near_sph <- apply(sapply(seq_len(nrow(co)), function(k) hav(gl$lat, gl$lon, co$lat[k], co$lon[k])), 1, which.min)
  lat0 <- mean(co$lat); R <- 6371 * pi / 180
  proj <- function(lat, lon) cbind(R * lon * cos(lat0 * pi / 180), R * lat)
  Y <- proj(co$lat, co$lon); Yc <- scale(Y, scale = FALSE); Xc <- scale(mds, scale = FALSE)
  sv <- svd(t(Xc) %*% Yc); Q <- sv$u %*% t(sv$v)             # rotation or reflection taking MDS to the map
  Xa <- Xc %*% Q
  G <- sweep(proj(gl$lat, gl$lon), 2, attr(Yc, "scaled:center"))
  near_mds <- dbscan::kNN(Xa, k = 1, query = G)$id[, 1]
  w <- cos(gl$lat * pi / 180)
  list(tiles = tiles[order(-tiles$area_km2), ], agree = sum(w * (near_sph == near_mds)) / sum(w),
       rms_align = sqrt(mean(rowSums((Xa - Yc)^2))), n = nrow(co))
})


# ==== Exercise 11.4 (substitute): the nuclear pipeline on the four frames of nuclei.tif ====
nuclei_tif_counts <- function(offset = 0.05) memo("nuctif", {
  nuc <- nuclei_tif()
  t(vapply(1:4, function(k) {
    f <- filter2(getFrame(nuc, k), gauss_brush(1))
    ad <- EBImage::opening(f - filter2(f, disc_filter(21)) > offset, makeBrush(5, "disc"))
    gl <- EBImage::opening(f > otsu(f), makeBrush(5, "disc"))
    c(adaptive = max(bwlabel(ad)), otsu = max(bwlabel(gl)), otsu_threshold = otsu(f))
  }, numeric(3)))
})

run_figures(list(
  fig_11_1 = fig_11_1, fig_11_2 = fig_11_2, fig_11_3 = fig_11_3, fig_11_4 = fig_11_4, fig_11_5 = fig_11_5,
  fig_11_6 = fig_11_6, fig_11_7 = fig_11_7, fig_11_8 = fig_11_8, fig_11_9 = fig_11_9, fig_11_10 = fig_11_10,
  fig_11_11 = fig_11_11, fig_11_12 = fig_11_12, fig_11_13 = fig_11_13, fig_11_14 = fig_11_14,
  fig_11_15 = fig_11_15, fig_11_16 = fig_11_16, fig_11_17 = fig_11_17, fig_11_18 = fig_11_18,
  fig_11_21 = fig_11_21, fig_11_22 = fig_11_22, fig_11_24 = fig_11_24, fig_11_25 = fig_11_25,
  fig_11_26 = fig_11_26, fig_11_27 = fig_11_27, fig_11_28 = fig_11_28, fig_11_29 = fig_11_29,
  fig_11_30 = fig_11_30, fig_11_31 = fig_11_31, fig_11_32 = fig_11_32, data = export_demo_data
))
