Exercise: stain separation

Goal

By the end of this exercise you should be able to:

  • compute optical densities from RGB intensities, and verify that stains add in OD and multiply in intensity,
  • separate two stains by hand and build a 3 \times 3 stain matrix with a residual column,
  • predict crosstalk and noise from the stain vectors (the condition number),
  • explain the failure modes (dark pixels, wrong I_0, wrong vectors) and how Macenko estimates the vectors.

Do the pen-and-paper part first, then the demo.

Background

Idea What it means
Beer–Lambert I_c = I_{0,c}\,10^{-\sum_k s_{ck}C_k}: stains multiply the transmitted light
Optical density \mathrm{OD}_c = -\log_{10}(I_c/I_{0,c}) = \sum_k s_{ck}C_k: linear in the concentrations
Stain matrix unit stain vectors (R, G, B) as the columns of S; \mathbf{od} = S\mathbf C, \mathbf C = S^{-1}\mathbf{od}
Published vectors hematoxylin (0.65, 0.70, 0.29), eosin (0.07, 0.99, 0.11), DAB (0.27, 0.57, 0.78) (Ruifrok & Johnston)
Residual column with two stains: their normalised cross product
Macenko the plane of the OD cloud (two principal directions), the two extreme angles in it = the stain vectors

By hand

B1. A pixel has I = (80, 120, 200) and the background is I_0 = 255 in every channel. (a) Compute its optical density. (b) Which channel does the stain absorb most? What colour does the pixel look? (c) Deconvolved with the H, E, residual matrix of the lecture, it gives \mathbf C = (0.75, -0.21, -0.09). What does the negative eosin value tell you about this pixel?

B2. Restrict H&E to the red and green channels: hematoxylin (0.65, 0.70), eosin (0.07, 0.99) (columns of a 2 \times 2 S). (a) A pixel has OD (0.5, 1.0). Solve for the two concentrations. (b) A pixel holds 0.4 units of hematoxylin and 0.3 of eosin. Compute its OD, and the OD of each stain alone. Check that they add. (c) With I_0 = 255, compute the transmitted red and green intensities of the three pixels in (b). Check that the two-stain intensity is I_0 times the product of the two single-stain transmissions.

B3. The unit vectors of hematoxylin and eosin are \hat{\mathbf h} = (0.651, 0.701, 0.291) and \hat{\mathbf e} = (0.070, 0.991, 0.110). (a) Compute \hat{\mathbf h} \times \hat{\mathbf e} and normalise it. Check that it is perpendicular to both. (b) The lecture gives S^{-1} for S = [\hat{\mathbf h}\ \hat{\mathbf e}\ \hat{\mathbf r}]. Check that the first row of S^{-1}, (1.482, -0.162, 0.510), times the first column of S gives 1, and times the second column gives 0. (c) Why must the third row of S^{-1} be \hat{\mathbf r} itself?

B4. The angles between the unit stain vectors are H–E 39°, H–DAB 37°, E–DAB 48°. The condition numbers are \kappa = 2.8 for (H, E, residual), 3.0 for (H, DAB, residual), and 3.8 for (H, E, DAB). (a) Which pair of stains is most similar? Which of the three separations amplifies noise most? (b) Why is separating three stains harder than two, even though H–E and H–DAB are about equally far apart?

B5. A nucleus pixel is completely black in red: I_R = 0. What OD do you assign if you clip at I = 1, with I_0 = 255? Why is its hematoxylin value still unreliable?

B6. The true white level is 242, but you analyse with I_0 = 205. (a) What OD does a clean background pixel (I = 242 in every channel) get? (b) The rows of S^{-1} (H, E, residual) sum to 1.83, -0.24 and 0.53. What concentrations does the background pixel get? Which map shows the problem most?

B7. In an H-DAB slide you want to know whether a tumour expresses a protein more than normal tissue next to it. Is comparing the mean DAB map value of the two regions reasonable? Is comparing the same two regions between two hospitals’ slides?

Math questions

B8. (math) Derive the linear OD model from Beer–Lambert, dI/dz = -\mu(z) I along the light path, with \mu = \ln 10 \sum_k s_k\,c_k(z). What is C_k in terms of c_k(z)?

B9. (math) Show that if \hat{\mathbf r} is a unit vector perpendicular to \mathbf s_1 and \mathbf s_2, then for S = [\mathbf s_1\ \mathbf s_2\ \hat{\mathbf r}] the third row of S^{-1} is \hat{\mathbf r}^\top. Conclude that the residual concentration of a pixel is its OD’s component along \hat{\mathbf r}.

B10. (math) In the plane spanned by two stain vectors at angles \varphi_1 < \varphi_2, show that any pixel \mathbf{od} = C_1\mathbf s_1 + C_2\mathbf s_2 with C_1, C_2 \ge 0 (not both zero) has an angle between \varphi_1 and \varphi_2 (assume \varphi_2 - \varphi_1 < 180°). Why does Macenko use percentiles instead of the minimum and maximum angle?

B1. (a) \mathrm{OD} = -\log_{10}(80/255,\ 120/255,\ 200/255) = (0.50, 0.33, 0.11). (b) Red (OD 0.50): red light is absorbed, blue passes: a blue-purple pixel, like hematoxylin. (c) The pixel’s OD is not a non-negative mixture of hematoxylin and eosin: it absorbs less green relative to red than hematoxylin does, so the fit needs “negative eosin”. Either a different stain or pigment, or vectors that do not match this slide.

B2. (a) \det S = 0.65 \cdot 0.99 - 0.07 \cdot 0.70 = 0.5945; C_H = (0.99 \cdot 0.5 - 0.07 \cdot 1.0)/0.5945 = 0.715, C_E = (-0.70 \cdot 0.5 + 0.65 \cdot 1.0)/0.5945 = 0.505. (b) H alone: 0.4(0.65, 0.70) = (0.260, 0.280); E alone: 0.3(0.07, 0.99) = (0.021, 0.297); both: (0.281, 0.577), the sum. (c) I = 255 \cdot 10^{-\mathrm{OD}}: H alone (140.1, 133.8), E alone (243.0, 128.7), both (133.5, 67.5). And 255 \cdot (140.1/255)(243.0/255) = 133.5, 255 \cdot (133.8/255)(128.7/255) = 67.5: the transmissions multiply.

B3. (a) \hat{\mathbf h} \times \hat{\mathbf e} = (0.701 \cdot 0.110 - 0.291 \cdot 0.991,\ 0.291 \cdot 0.070 - 0.651 \cdot 0.110,\ 0.651 \cdot 0.991 - 0.701 \cdot 0.070) = (-0.211, -0.051, 0.596), length 0.635, \hat{\mathbf r} = (-0.332, -0.081, 0.940). Dot products with \hat{\mathbf h} and \hat{\mathbf e} are 0 (up to rounding). (b) 1.482 \cdot 0.651 - 0.162 \cdot 0.701 + 0.510 \cdot 0.291 = 0.965 - 0.114 + 0.148 = 1.000; 1.482 \cdot 0.070 - 0.162 \cdot 0.991 + 0.510 \cdot 0.110 = 0.104 - 0.161 + 0.056 = 0.000 (up to rounding). (c) \hat{\mathbf r}^\top S = (\hat{\mathbf r}\cdot\hat{\mathbf h},\ \hat{\mathbf r}\cdot\hat{\mathbf e},\ \hat{\mathbf r}\cdot\hat{\mathbf r}) = (0, 0, 1), which is what the third row of S^{-1} must give (B9).

B4. (a) H–DAB is the most similar pair (37°), H–E almost as similar (39°). The three-stain separation (H, E, DAB) has the largest \kappa (3.8): it amplifies noise most. (b) With three stains, each must be distinguished from the plane spanned by the other two, not just from one other vector, and DAB lies fairly close to the plane of H and E. With two stains the third column is perpendicular by construction, the best possible partner.

B5. \mathrm{OD} = -\log_{10}(1/255) = 2.41. The true OD could be much higher (any value above 2.4 gives I = 0), and only the red channel is clipped, so the pixel’s OD direction is distorted towards green and blue: its hematoxylin is underestimated and some of it leaks into the other maps.

B6. (a) -\log_{10}(242/205) = -0.072 in every channel: negative, “less than no stain”. (b) \mathbf C = S^{-1}(-0.072, -0.072, -0.072)^\top = -0.072 \times (1.83, -0.24, 0.53) = (-0.132, 0.018, -0.038). The hematoxylin map shows it most: the whole background becomes negative. (In the demo, with the slide’s own vectors: -0.13.)

B7. Within one slide, stained in one batch: a reasonable relative comparison (both regions had the same reaction time), with the caveat that DAB saturates in strongly positive regions. Between hospitals: no. Protocols, incubation times and scanners differ, DAB is not stoichiometric, and even the stain vectors differ; compare positive/negative classifications at best, after checking controls.

B8. dI/dz = -\mu I gives I = I_0\exp(-\int\mu\,dz) = I_0\,10^{-\sum_k s_k\int c_k(z)\,dz}, so \mathrm{OD} = \sum_k s_k C_k with C_k = \int c_k(z)\,dz: the amount of stain along the light path (a line integral, as in the Radon day).

B9. \hat{\mathbf r}^\top S = (\hat{\mathbf r}^\top\mathbf s_1, \hat{\mathbf r}^\top\mathbf s_2, \hat{\mathbf r}^\top\hat{\mathbf r}) = (0, 0, 1) = \mathbf e_3^\top. Multiplying by S^{-1} from the right: \hat{\mathbf r}^\top = \mathbf e_3^\top S^{-1}, the third row. So C_3 = \hat{\mathbf r}^\top\mathbf{od}: the component of the OD perpendicular to the plane of the two stains.

B10. Write \mathbf s_k = (\cos\varphi_k, \sin\varphi_k) in the plane. A non-negative combination of two vectors lies in the (convex) cone between them, so its direction lies between \varphi_1 and \varphi_2 (for an angle difference below 180° the cone is the sector between the two rays). The extreme angles are reached only by (nearly) pure pixels. Minimum and maximum would be decided by single noisy or odd pixels (dust, pigment); the 1st and 99th percentiles are robust extremes.

Explore: separate H&E

A synthetic H&E slide (seeded), rendered by Beer–Lambert from known concentration maps with this slide’s own stain vectors (close to, but not equal to, the published ones), plus detector noise and 8-bit rounding. Right of the image: the stain matrix in use. Below: the three estimated concentration maps (or the true ones). The triangle shows every pixel’s OD divided by its sum (corners: pure red, green or blue absorption); the dashed circles are the true stain directions, the filled circles the vectors in use: drag them. The square is the residual direction (cross product). The heat map T = S^{-1}S_\text{true} says how much of each true stain ends up in each estimated map (the identity = perfect separation). The readout gives \kappa(S), the hematoxylin map’s mean and noise in unstained areas, and, when there are any, the share of pixels with clearly negative concentrations and the number of saturated pixels.

T1. Preset Ruifrok defaults. Read the crosstalk matrix. How much eosin ends up in the hematoxylin map, and how much hematoxylin in the eosin map? What does the residual map show?

T2. Preset the true vectors. What happens to the matrix and the residual map? Where do the pixels lie in the triangle?

T3. Preset wrong eosin vector. Describe the H map and the readout. Then drag the eosin circle yourself onto the end of the pixel cloud.

T4. Preset nearly parallel stains. Compare \kappa and the noise of the H map in unstained areas with T2.

T5. Preset wrong I₀ / saturated pixels. Two problems are combined here (the stain density is raised and I_0 lowered). Use the sliders to separate them: what does each one do?

T6. Preset estimate from the image (Macenko). How close are the estimated vectors to the dashed circles? Compare the crosstalk matrix with T1.

T1. The H map contains 0.20 of the true eosin (and 0.92 of the true H); the E map contains 0.10 of the true H (0.81 of the true E). The residual map is not zero: it shows eosin-rich regions (0.20 of the true eosin goes there), because the published eosin vector is 14° away from this slide’s eosin. \kappa = 2.8.

T2. The H and E columns of T become the identity (1.00 and 0.00): perfect separation, and the residual map is zero (up to noise and rounding). In the triangle the pixels lie on the line between the two true directions; the circles sit at its ends. \kappa = 3.7, the H map’s noise in unstained areas has sd 0.007.

T3. The H map contains -0.54 times the true eosin: the cytoplasm becomes negative in the H map (77 % of the pixels have a concentration below -0.05), and the E map overestimates eosin (1.49). \kappa = 5.8. Dragging the eosin circle to the end of the cloud brings the matrix back to near-identity.

T4. \kappa = 10.5 (against 3.7) and the H map’s noise in unstained areas has sd 0.020 (against 0.007): three times noisier. The crosstalk matrix shows large mixed terms (H map -1.48 times the true eosin).

T5. Stain density 2.6 alone: 662 pixels saturate (some channel at I \le 1) in the darkest nuclei; their concentrations are clipped and wrong, the rest is fine. I_0 = 205 alone: the background’s OD becomes negative and the H map’s unstained areas average -0.13 (B6); 86 % of the pixels have a concentration below -0.05.

T6. The Macenko vectors are within 2.0° (H) and 1.1° (E) of the true ones (Ruifrok: 3.7° and 14°). The crosstalk matrix is close to the identity: 1.06 and 0.04 in the H row, -0.07 and 0.96 in the E row. Small deviations remain because no pixel is perfectly pure and the percentiles are robust, not extreme.

Explore: H-DAB, count positive nuclei

The same demo in H-DAB mode: a hematoxylin counterstain, a light eosin, and some nuclei DAB-positive (brown). The maps show H, DAB and E. With count positive nuclei, the DAB map is thresholded with Otsu’s method (the thresholding day) and connected regions of at least 15 pixels are counted.

D1. Preset count positive nuclei. What threshold does Otsu choose, and how many positive nuclei are found? Compare with the truth.

D2. Switch to Ruifrok defaults, then wrong DAB vector and nearly parallel stains. Watch the DAB map, the threshold and the count. Why is the count so robust here, and when would it not be?

D1. Threshold 0.27; 14 positive nuclei found; the slide has 14 DAB-positive nuclei among 30.

D2. Ruifrok: threshold 0.29, 14. Wrong DAB vector: 0.39, 14, and the H map gets -0.60 times the DAB (positive nuclei look hematoxylin-poor). Nearly parallel: 0.62, 14, with a noisier DAB map (\kappa = 9.8). The count is robust because the positive nuclei are strongly brown and well separated from the background: crosstalk changes the DAB values (and the threshold) but not which nuclei are above it. Weakly positive nuclei, or touching nuclei, would be miscounted first; the H measurements in DAB-positive nuclei are not robust at all.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in unmixing/src/stain_unmixing/exercise.rs. Images hold linear intensities in [0, 1] (8-bit values divided by 255); a stain matrix has the unit stain vectors (red, green, blue) as its columns.

C1. optical_density(img, i0): \mathrm{OD}_c = -\log_{10}(I_c / I_{0,c}) in every pixel, with I first raised to at least 1/255. The checks are B1(a), B5 and B6(a).

C2. transmit(amounts, s, i0): Beer–Lambert, I_c = I_{0,c}\,10^{-\sum_k s_{ck} C_k}. The check is B2(c): the transmissions of two stains multiply.

C3. stain_matrix(s1, s2) and deconvolve(od, s): colour deconvolution:

h = s1 / |s1|,  e = s2 / |s2|
r = h × e, scaled to length 1            # the residual column
S = [h  e  r];  S⁻¹ once
for every pixel:  (C_1, C_2, C_res) = S⁻¹ · od

The checks are B3(a), B1(c) and B6(b).

C4. macenko(od, beta, alpha): the stain vectors from the image, the four steps of the slide “Estimating the stain vectors: Macenko”:

keep the pixels with |od| > β
v1, v2 = the two leading eigenvectors of Σ od odᵀ        # flip v1 to point into the cloud
φ = atan2(od · v2, od · v1) for every kept pixel
the stains = cos φ · v1 + sin φ · v2 at the α and 100 − α percentiles of φ

The check: on mixtures of two known stains, the pure ones included, it finds both within 0.1°.

Then cargo run -p unmixing -- stain-unmixing and open the pictures in out/stain-unmixing/. The program renders a synthetic H&E slide by Beer–Lambert with this slide’s own stain vectors, a white level of 242, detector noise and 8-bit rounding (slide.png). It separates it with the published vectors, with the true ones and with Macenko’s estimate (\beta = 0.15, \alpha = 1): he-*.png show the H map (left) and the E map (right), stain dark on white. For each it prints how much of each true stain ends up in each map, and the mean of the residual map. Last, it analyses the slide with the white level 205.

C5. How far are the published vectors from this slide’s stains? How much eosin ends up in the H map with the published vectors, and what does the H map look like? Compare with the true vectors and with Macenko’s estimate (demo questions T1, T2 and T6).

C6. With the white level set to 205, what OD does the empty background get, and what does the H map show there? Compare with B6.

The model solution is unmixing/src/stain_unmixing/solution.rs in the download.

C5. This slide’s hematoxylin is 3.1° and its eosin 13.9° from the published vectors. With the published vectors the H map is 0.94\,H + 0.21\,E and the E map 0.07\,H + 0.81\,E: a fifth of the eosin lands in the H map, which shows a grey haze over the whole tissue, darker where the eosin is strong; only the empty holes are white. The residual map is not zero (mean 0.065): the eosin is not in the plane of the two published vectors. With the true vectors the maps are exactly 1.00\,H and 1.00\,E and the residual is 0. The H map is white wherever there are no nuclei. Macenko’s estimate is 2.4° (H) and 1.0° (E) from the truth, and gives 1.07\,H + 0.04\,E and -0.09\,H + 0.97\,E, close to the identity.

C6. The background gets OD -0.070 in every channel (B6(a): -0.072; 8-bit rounding of the white level makes the small difference), and the H map in the empty holes averages -0.125 instead of 0 (B6(b): -0.132). It is “less than no stain”, and the whole background of the H map is negative.

Where this leads

Colour deconvolution is the last piece of the day: a fluorescence image, a lambda stack, a hyperspectral scene and a stained slide are all \mathbf y = M\mathbf a. The concentration maps are ordinary grey images: threshold them (day 12), label and measure the objects (days 14 and 16). Stain normalisation between scanners deconvolves with one slide’s vectors and recombines with a reference slide’s. The fluorescence days that follow use the day’s model: colocalisation (day 48) and FRET (day 49) correct bleed-through before comparing channels, and FLIM (day 50) unmixes decays instead of spectra.