Polar coordinates: measuring a ring

Week 1 — Polar coordinates

Quantitative biology and integrative omics

About this lecture

Many things in biology grow or spread outwards from a centre: a drop of antibiotic, a colony, a tumour spheroid, a cell nucleus. For those, (x, y) is the wrong way to describe a position. Distance from the centre and direction exposes this symmetry and can make the data easier to analyze

  • Polar coordinates (r, \theta): how far from the centre, and in which direction.
  • A very useful transformation, but a nonlinear one: unlike rotation or shear, it is not a matrix.
  • Unwrapping an image around a centre turns rings into straight lines.

The clear zone around an antibiotic disk shows how well it works

Is this bacterium resistant to an antibiotic? The disk diffusion (Kirby–Bauer) test answers it in one plate:

  • Spread bacteria evenly on agar; place 6 mm paper disks, each soaked in a different antibiotic.
  • The drug diffuses outwards. Where it is strong enough, nothing grows: a clear zone of inhibition.
  • Next day, measure each zone’s diameter in mm. A lab compares it with a breakpoint table (EUCAST or CLSI) for that drug and species: susceptible or resistant.

Diffusion spreads equally in all directions, so the zones are (nearly) circles. Circles are easy in polar coordinates.

Image: CDC / Gilda L. Jones, Public Health Image Library #10785, public domain.

A point can be described by its distance and angle from a centre

A point (x, y), seen from a centre (x_0, y_0):

  • r = distance from the centre
  • \theta = angle, counter-clockwise from the x direction
  • If we want degrees from 0-360, not just 0-90, atan2() is our friend

r = \sqrt{(x - x_0)^2 + (y - y_0)^2}, \qquad \theta = \operatorname{atan2}(y - y_0,\ x - x_0)

and back again:

x = x_0 + r\cos\theta, \qquad y = y_0 + r\sin\theta

dx <- 3; dy <- 4
sqrt(dx^2 + dy^2)          # r = 5
atan2(dy, dx) * 180 / pi   # theta = 53.1 degrees

Image: CDC / Gilda L. Jones, PHIL #10785, public domain.

Polar coordinates turn circles into lines, which no matrix can do

  • Every transformation in Transforming images with matrices kept straight lines straight: that is what a matrix does.
  • Polar coordinates turn circles into straight lines and straight lines into curves. No 2 × 2 matrix can do that.
  • Check with one point: doubling (1, 1) doubles r but keeps \theta. A linear map would double both outputs.

Brightness against distance from the disk shows the zone edge

Load the plate as in Transforming images with matrices, then compute r for every pixel around disk 5:

img <- jpeg::readJPEG("plate.jpg")                # 600 x 600 grey levels, 0..1
img <- t(img[nrow(img):1, ])                      # flip so up is up; now img[x, y]
xy  <- t(expand.grid(1:nrow(img), 1:ncol(img)))   # 2 x N pixel coordinates
x0 <- 353; y0 <- 356                              # centre of disk 5
r  <- sqrt((xy[1, ] - x0)^2 + (xy[2, ] - y0)^2)   # one distance per pixel
near <- r <= 80
plot(r[near], img[near], pch = 16, cex = 0.3)     # grey level against distance

Download plate.jpg. Image: CDC / Gilda L. Jones, PHIL #10785, public domain.

Averaging over the angle gives a profile whose drop is the edge

Group the pixels by distance and average: one number per radius.

prof <- tapply(img[near], round(r[near]), mean)   # mean grey level at r = 0, 1, ..., 80
rr   <- as.numeric(names(prof))
edge <- min(rr[rr >= 16 & prof < 0.55])           # first radius past the disk that is dark
edge                                              # 53 pixels
2 * edge / (26.8 / 6)                             # diameter: 24 mm (a 6 mm disk is 26.8 px)

Image: CDC / Gilda L. Jones, PHIL #10785, public domain.

Unwrapping turns the circular zone edge into a straight line

To turn the ring into a picture in (\theta, r), go backwards: for each (r, \theta) on a grid, compute x, y and look up the nearest pixel.

unwrap <- function(x0, y0, rmax = 80) {
  r     <- 0:rmax
  theta <- (0:359) * pi / 180                     # one column per degree
  x <- x0 + outer(r, cos(theta))                  # (rmax + 1) x 360 grid of positions
  y <- y0 + outer(r, sin(theta))
  matrix(img[cbind(round(as.vector(x)), round(as.vector(y)))], nrow = length(r))
}
u <- unwrap(353, 356)                             # rows = r, columns = theta

Image: CDC / Gilda L. Jones, PHIL #10785, public domain.

Try it: move the centre

For a zone that is not round, find the edge at each angle

Disk 4 sits near the plate edge, between two other zones. Find the edge separately for every angle, one column of the strip at a time:

u4 <- unwrap(478, 413, rmax = 100)
edge_at <- apply(u4, 2, function(v) min(which(0:100 >= 16 & v < 0.55)) - 1)   # row 1 is r = 0
range(edge_at)                                    # 69 to 83 pixels: not a circle
  • The edge r(\theta) is a curve in the strip, not a line. Averaging over \theta would hide that.
  • Lab rule of thumb: an irregular or merging zone is unreliable; repeat the test or read the undisturbed side.

Image: CDC / Gilda L. Jones, PHIL #10785, public domain.

The same steps measure every zone; fuzzy edges are less reliable

The same three lines for every disk: centre → unwrap → profile → edge.

disks <- data.frame(x = c(218, 386, 111, 478, 353, 226, 506, 101, 322, 436, 148, 298),
                    y = c(508, 511, 403, 413, 356, 306, 275, 247, 220, 147, 113,  82))
edge_of <- function(p) min(which(0:80 >= 16 & p < 0.55)) - 1   # row 1 is r = 0
edges <- sapply(1:12, function(i) edge_of(rowMeans(unwrap(disks$x[i], disks$y[i]))))
diam_mm <- 2 * edges / (26.8 / 6)
  • Each diameter is then compared with the breakpoint for its drug and species.
  • Disk 11 has a fuzzy edge: its diameter depends strongly on the threshold (exercise R5).

Image: CDC / Gilda L. Jones, PHIL #10785, public domain.

Many round structures in biology are easier to measure from their centre

The same move, “pick a centre, then use distance and angle”, appears all over biology:

Where What r or \theta tells you
Tumour spheroids, organoids radial profile of a stain: living rim, oxygen-starved necrotic core
Cell nucleus how chromatin or a protein is spread from the centre to the nuclear envelope
Centrosome, mitotic spindle microtubules radiate outwards: in (\theta, r) they become vertical lines
Cell migration \theta alone: do cells move towards a chemoattractant? (circular statistics)
Fourier transform (later) a complex coefficient is stored in polar form: size Mod() and phase Arg()

Summary

Concept Key fact R
Polar coordinates distance and angle from a chosen centre sqrt(dx^2 + dy^2), atan2(dy, dx)
Back to (x, y) x = x_0 + r\cos\theta,\ y = y_0 + r\sin\theta x0 + r * cos(theta)
Not a matrix circles → lines, lines → curves; \theta does not scale —
Radial profile average over \theta: one number per distance tapply(img, round(r), mean)
Unwrapping for each (r, \theta), look up the pixel it came from img[cbind(i, j)]
Shape of a zone edge per angle, r(\theta); flat means round apply(u, 2, ...)
Scale a known object (6 mm disk) converts pixels to mm 26.8 / 6 px per mm

Next (week 2, Eigenvalues & eigenvectors): directions that a matrix only scales.