Exercise: bleed-through and compensation
Before this unit: Day 46: Colour.
See also: Day 3: Bit depth and noise; Day 16: Measurements.
Goal
By the end of this exercise you should be able to:
- compute the channel values of a dye from a sampled spectrum and two filters, and write a two-colour pixel as \mathbf y = M\mathbf a,
- estimate a spillover coefficient from a single-stained control,
- compensate with M^{-1} by hand, and recognise under- and over-compensation,
- explain with the determinant why similar dyes amplify noise, and why bleed-through fakes correlation.
Do the pen-and-paper part first, then the two demos.
Background
| Idea | What it means |
|---|---|
| Channel value | spectrum × filter transmission, added over wavelength |
| Mixing matrix M | rows = channels, columns = dyes; column k = one unit of dye k as the channels see it |
| A pixel | \mathbf y = M\mathbf a: the dyes’ contributions add |
| Spillover matrix | M with each column divided by its entry in the dye’s own channel: S = \begin{pmatrix} 1 & s_{12} \\ s_{21} & 1 \end{pmatrix}; s_{21} = dye 1 into channel 2 |
| Single-stained control | only dye 1: channel 2 against channel 1 is a line through the origin with slope s_{21} |
| Compensation | \hat{\mathbf a} = S^{-1}\mathbf y, with \begin{pmatrix} a & b \\ c & d \end{pmatrix}^{-1} = \frac{1}{ad - bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix} |
| Check | a single-stained population should lie flat, at the background level |
By hand
B1. Spectra are sampled in five bins of 25 nm, centred at 500, 525, 550, 575 and 600 nm. Per unit amount, dye 1 emits (0.2, 1.0, 0.6, 0.3, 0.1) and dye 2 emits (0, 0.1, 0.5, 1.0, 0.6). Channel 1’s filter transmits the first two bins fully, channel 2’s filter the last two. (a) Compute the four channel values (multiply, add, times 25 nm). Write M. (b) Scale each column by the dye’s own channel. What are s_{21} and s_{12}, in percent? (c) Channel 2’s filter is widened to transmit half of the 550-nm bin too. Recompute both columns. What happens to the bleed-through of dye 1, and to the signal of dye 2 in its own channel?
Use S = \begin{pmatrix} 1 & 0.1 \\ 0.2 & 1 \end{pmatrix} in B2–B4.
B2. What do the channels record for a pixel with (a_1, a_2) = (2000, 0)? For (0, 1000)? For (2000, 1000)? Which column of S did you use for the first two?
B3. Compute \det S and S^{-1}. Check that S^{-1}S = I (first row is enough).
B4. A pixel records (2100, 1400). Compensate it by hand. Does it contain both dyes?
B5. Two similar dyes give S_2 = \begin{pmatrix} 1 & 0.9 \\ 0.9 & 1 \end{pmatrix}. Compute \det S_2 and S_2^{-1}. A pixel recorded as (1000, 900) compensates to (1000, 0). Now channel 2 reads 10 units too high, (1000, 910): compensate again. How large is the error in each dye? Compare with the same 10-unit error under the S of B2–B4.
B6. A single-stained control (only dye 1), background already subtracted, gives four pixels (channel 1, channel 2): (100, 27), (200, 48), (300, 77), (400, 98). (a) Estimate s_{21} as the slope through the origin, \sum xy / \sum x^2. (b) Compare with the average of the four ratios y/x. Which pixels does each estimate trust most?
B7. True s_{21} = 0.25, s_{12} = 0. A nucleus pixel contains 400 units of dye 1 and no dye 2. (a) What does it record? (b) Compensate it with s_{21} = 0.15, 0.25 and 0.35. Which result is impossible for a real dye, and what does it tell you? (c) The nucleus has pixels from 100 to 400 units of dye 1. Sketch the compensated dye-2 value against dye 1 for the three guesses.
B8. Nucleus (dye 1) and membrane (dye 2) do not overlap. Before compensation, do you expect the correlation between channel 1 and channel 2 over the cell to be positive, zero or negative? And after a correct compensation?
Math questions
B9. (math) Derive the 2 \times 2 inverse formula by solving S\mathbf a = \mathbf y for a_1 and a_2. Show that the entries of S^{-1} grow like 1/\det S, and that |\det S| is the area of the parallelogram spanned by the columns.
B10. (math) Show that \hat s = \sum x_i y_i / \sum x_i^2 minimises \sum_i (y_i - s x_i)^2. If channel 1 has noise of variance \sigma^2 and the true x_i have mean square \overline{x^2}, the slope fitted to noisy x is biased towards zero by a factor of about \overline{x^2}/(\overline{x^2} + \sigma^2): explain in one sentence why bright controls help.
B11. (math) Let the true spillover be s_{12}, s_{21} and the guesses g_{12}, g_{21}. Compute S_g^{-1}S and show that, to first order in the errors \varepsilon_{ij} = s_{ij} - g_{ij}, a dye-1-only population ends up on a line with slope \varepsilon_{21}.
B1. (a) Dye 1: channel 1 = (0.2 + 1.0) \cdot 25 = 30, channel 2 = (0.3 + 0.1) \cdot 25 = 10. Dye 2: channel 1 = (0 + 0.1) \cdot 25 = 2.5, channel 2 = (1.0 + 0.6) \cdot 25 = 40. M = \begin{pmatrix} 30 & 2.5 \\ 10 & 40 \end{pmatrix}. (b) s_{21} = 10/30 = 0.333 (33 %), s_{12} = 2.5/40 = 0.0625 (6 %). The red tail of dye 1 makes the bleed-through one-sided. (c) Channel 2 now adds half of the 550-nm bin: dye 1 gives (0.5 \cdot 0.6 + 0.3 + 0.1) \cdot 25 = 17.5, dye 2 gives (0.5 \cdot 0.5 + 1.0 + 0.6) \cdot 25 = 46.25. Bleed-through of dye 1: 17.5/30 = 58 % (was 33 %); s_{12} = 2.5/46.25 = 5.4 %. Dye 2’s own signal grows by 16 %. A wider filter collects more photons and more bleed-through.
B2. S\binom{2000}{0} = \binom{2000}{400} (column 1 times 2000), S\binom{0}{1000} = \binom{100}{1000} (column 2 times 1000), S\binom{2000}{1000} = \binom{2100}{1400}: the sum of the two.
B3. \det S = 1 - 0.1 \cdot 0.2 = 0.98, S^{-1} = \frac{1}{0.98}\begin{pmatrix} 1 & -0.1 \\ -0.2 & 1 \end{pmatrix} = \begin{pmatrix} 1.020 & -0.102 \\ -0.204 & 1.020 \end{pmatrix}. First row of S^{-1}S: \frac{1}{0.98}(1 - 0.02,\ 0.1 - 0.1) = (1, 0).
B4. S^{-1}\binom{2100}{1400} = \frac{1}{0.98}\binom{2100 - 140}{-420 + 1400} = \frac{1}{0.98}\binom{1960}{980} = \binom{2000}{1000}. Both dyes: it is the third pixel of B2.
B5. \det S_2 = 1 - 0.81 = 0.19, S_2^{-1} = \frac{1}{0.19}\begin{pmatrix} 1 & -0.9 \\ -0.9 & 1 \end{pmatrix} = \begin{pmatrix} 5.26 & -4.74 \\ -4.74 & 5.26 \end{pmatrix}. S_2^{-1}\binom{1000}{910} = \frac{1}{0.19}\binom{1000 - 819}{-900 + 910} = \binom{952.6}{52.6}: errors of -47 and +53 from a 10-unit error. Under S: S^{-1}\binom{0}{10} = \binom{-1.0}{10.2}, the error stays about 10. Nearly parallel columns, a small determinant, a large inverse.
B6. (a) \sum xy = 2700 + 9600 + 23\,100 + 39\,200 = 74\,600, \sum x^2 = 10\,000 + 40\,000 + 90\,000 + 160\,000 = 300\,000, \hat s_{21} = 0.249. (b) The ratios are 0.270, 0.240, 0.257, 0.245, mean 0.253. The slope through the origin weights each pixel by x^2: the bright pixels count most, where noise matters least relative to the signal. The plain mean of ratios trusts the dim pixel (0.270) as much as the bright ones.
B7. (a) (400,\ 0.25 \cdot 400) = (400, 100). (b) \hat a_2 = 100 - g \cdot 400: 40 for g = 0.15, 0 for g = 0.25, -40 for g = 0.35. The negative value is impossible: the spillover was set too high (over-compensation). (c) Lines through the origin with slopes 0.25 - g: +0.10 (tilting up: under), 0 (flat: right), -0.10 (tilting down: over).
B8. Positive: every bright nucleus pixel is also somewhat bright in channel 2 (bleed-through), so the two channels rise together. After a correct compensation the nucleus is at background level in the dye-2 image and the correlation is about zero (in the demo below: 0.33 before, -0.03 after; the small negative value is noise: compensation subtracts channel 1’s noise from channel 2).
B9. From a\,a_1 + b\,a_2 = y_1 and c\,a_1 + d\,a_2 = y_2: multiply the first by d, the second by b and subtract: (ad - bc)a_1 = d y_1 - b y_2. Likewise (ad - bc)a_2 = -c y_1 + a y_2. So S^{-1} = \frac{1}{ad - bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}: every entry is divided by \det S. The parallelogram spanned by (a, c) and (b, d) has area |ad - bc| (base times height, or the 2D cross product).
B10. \frac{d}{ds}\sum (y_i - s x_i)^2 = -2\sum x_i(y_i - s x_i) = 0 gives \hat s = \sum x_i y_i/\sum x_i^2; the second derivative 2\sum x_i^2 > 0, so it is a minimum. With noisy x, the denominator collects the noise variance as well (\overline{x^2} + \sigma^2) while the numerator does not, so the slope shrinks; a bright control makes \overline{x^2} \gg \sigma^2 and the factor close to 1.
B11. S_g^{-1} = \frac{1}{1 - g_{12}g_{21}}\begin{pmatrix} 1 & -g_{12} \\ -g_{21} & 1 \end{pmatrix} and S_g^{-1}S = \frac{1}{1 - g_{12}g_{21}}\begin{pmatrix} 1 - g_{12}s_{21} & s_{12} - g_{12} \\ s_{21} - g_{21} & 1 - g_{21}s_{12} \end{pmatrix}. A dye-1-only pixel a(1, 0)^\top is compensated to a times the first column: \hat a_2/\hat a_1 = \varepsilon_{21}/(1 - g_{12}s_{21}) = \varepsilon_{21}(1 + g_{12}s_{21} + \dots). To first order in the errors (and in the small product g_{12}s_{21}): slope \varepsilon_{21}.
Explore: spectra, filters and the mixing matrix
Emission spectra of 2–4 dyes (synthetic shapes with a red tail, drawn in the colour of their peak) and the channels’ filter bands (grey). Drag a dye’s numbered circle to move its peak; drag a band to move it, its dark edge handles to resize it. spectral detector replaces the bands by 8–32 contiguous bins. The heat map is M with each column divided by the dye’s own channel (or, with more channels than dyes, by its largest entry). The readout gives the bleed-through, \det, the condition number \kappa (with columns scaled to length 1; the next unit explains it) and the share of each dye’s photons that reach its own channel.
S1. Preset green into orange. Read s_{21} and s_{12}. Which is larger, and why (compare with Q1 of the quiz)?
S2. Drag channel 2’s band to the right (towards longer wavelengths), then back. What happens to the bleed-through of dye 1, and to the photons dye 2 delivers to its own channel?
S3. Preset narrow filters. Compare bleed-through and photons with S1. Is narrowing the filters free?
S4. Preset two dyes 20 nm apart. Read the matrix, \det and \kappa. What would compensation do to the noise here, compared with the classic preset?
S5. Preset 8-bin spectral detector. How many bins does each dye reach with at least 30 % of its peak bin? Why is that not a problem when there are more channels than dyes?
S1. s_{21} = 0.204 (20.4 %), s_{12} = 0.005 (0.5 %). Dye 1’s red tail reaches channel 2; dye 2 emits almost nothing in channel 1’s band. \det = 0.999, \kappa = 1.23: compensation is easy here.
S2. Moving channel 2 to longer wavelengths takes it further down dye 1’s tail: the bleed-through falls. But the band also moves away from dye 2’s peak, so dye 2’s own-channel photons fall too. Moving it back towards 550 nm does the opposite.
S3. Bleed-through 15.3 % (from 20.4 %), and dye 2 into channel 1 practically 0. But only 20 % of dye 1’s and 26 % of dye 2’s photons reach their own channels, against 61 % and 74 % with the wide filters: three times less light, so relatively more shot noise. Not free.
S4. The matrix has 1.02 for dye 1 in channel 2 (dye 1 is as bright in channel 2 as in its own channel) and 0.15 for dye 2 in channel 1; \det = 0.85 and \kappa = 3.08, against 0.999 and 1.23 for the classic preset. The columns are closer to parallel, so M^{-1} has larger entries and compensation amplifies the noise more.
S5. Reading the transposed matrix (one row per dye): dye 1 reaches channels 2 and 3 (1.00, 0.48), dye 2 channels 3–5 (0.60, 1.00, 0.31), dye 3 channels 5–6 (1.00, 0.67), dye 4 channels 7–8 (1.00, 0.35). Two or three bins each. With eight channels and four dyes there are more equations than unknowns: every bin carries information about the dyes’ shapes, and least squares (next unit) uses them all. Bleed-through is only a problem when one channel per dye is read as if it were that dye alone.
Explore: compensate a two-colour image
A synthetic cell (seeded): dye 1 in the nucleus, dye 2 in the membrane, a few small vesicles carrying both, with Poisson noise; measured = S \times true with a hidden S. Left: channel 2 (or channel 1, or the merge) before and after compensation. Middle: every pixel as a point, channel 2 against channel 1; colours mark the nucleus pixels (dye 1 only, green), membrane pixels (dye 2 only, orange), vesicles (purple) and background (grey). Right: after compensation with your guess, S^{-1} \times measured. The thick lines are least-squares lines through the nucleus and the membrane pixels; the dashed lines are the medians of the background. The readout gives S, S^{-1}, the slopes with a verdict, the medians, Pearson’s r of the two channels inside the cell, and the slope through the origin of the nucleus pixels before compensation.
E1. Preset no compensation. Which population tilts, and by how much? Look at channel 2 on the left: where is the ghost? What is the nucleus pixels’ slope through the origin, and why is it a little larger than the green line’s slope?
E2. Find s_{21} and s_{12} by making both single-labelled populations flat and level with the background. Then reveal true S.
E3. Set s_{21} = 0.15, then 0.35 (keep s_{12} = 0.05). Read the nucleus pixels’ median dye-2 value and the verdict each time. What does the compensated channel-2 image show in the nucleus for 0.35?
E4. Compare Pearson’s r with no compensation and with the true S. Untick double-labelled vesicles and compare again: why does r go slightly negative after compensation?
E5. Preset similar dyes. The true S is now \begin{pmatrix} 1 & 0.9 \\ 0.9 & 1 \end{pmatrix} and the guess is exact. Describe the scatter after compensation and the background noise. Compare with B5.
E1. The nucleus pixels (green) tilt up with slope 0.245; the membrane pixels lean right slightly (0.047). The ghost is the nucleus, faint in channel 2 (median 200 against a background of 13). The slope through the origin is 0.263: larger, because the background offset (about 10 counts in each channel) is not subtracted and lifts the dim pixels; the least-squares line has an intercept and absorbs the offset, but noise in channel 1 pulls its slope slightly down (B10).
E2. s_{21} = 0.25, s_{12} = 0.05: the nucleus slope is -0.006 and its median 9.9 (background 10.1); the membrane slope -0.003, median 9.6 (background 9.7). Both “flat: compensated”.
E3. 0.15: slope +0.095, median 86 (background 11): under-compensated, the ghost is still there. 0.35: slope -0.107, median -66: over-compensated; the nucleus becomes a dark hole in the compensated channel 2, below the background, which no dye can produce.
E4. r = 0.33 before and -0.03 after compensation (with vesicles). Without vesicles: 0.27 before and -0.11 after. Compensation subtracts 0.25 \times channel 1 from channel 2, and with it channel 1’s noise: the noise of the two compensated channels is negatively correlated. The vesicles (true double labelling) add a little real positive correlation.
E5. Even with the exact S, both populations smear out along a diagonal (slopes about -0.6) and the background noise in dye 2 grows from 4.6 (measured channel) to 31 after compensation. The medians are still right (nucleus 7.9 against a background of 10), but individual pixels are not: S_2^{-1} multiplies the noise by about 5, as in B5.
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/crosstalk/exercise.rs.
C1. channel_value(spectrum, filter, dl) and mixing_matrix(dyes, filters, dl): a channel adds up spectrum × filter; one dye gives one column:
for every channel c and dye k:
M[c][k] = Σ_bins spectrum_k[i] · filter_c[i] · Δλ
The check is B1(a) and (c).
C2. slope_through_origin(xs, ys): \sum xy / \sum x^2. The check is B6(a).
C3. inverse2(m) and compensate(ch1, ch2, s): the 2 \times 2 inverse by its formula, and compensation of every pixel:
S⁻¹ = 1/(ad − bc) · ( d −b ; −c a ) # once
for every pixel:
(a1, a2) = S⁻¹ · (ch1, ch2)
The checks are B3, B4, B5 and B7(b).
C4. pearson(a, b, mask): Pearson’s correlation of two images over a mask (subtract the means, then \sum d_a d_b / \sqrt{\sum d_a^2 \sum d_b^2}).
Then cargo run -p unmixing -- crosstalk and open the pictures in out/crosstalk/. The program makes a synthetic cell with dye 1 in the nucleus, dye 2 in the membrane and a few vesicles with both, and records two channels with the true spillover s_{21} = 0.25, s_{12} = 0.05, an offset of 10 and photon noise (channel1.png, channel2.png). It estimates the spillovers from the nucleus and the membrane pixels (single-stained structures) after subtracting the offset, compensates (dye2-compensated.png), and tries the guesses s_{21} = 0.15, 0.25, 0.35 (dye2-over.png for 0.35). Last, it records the cell with two similar dyes, S = \begin{pmatrix} 1 & 0.9 \\ 0.9 & 1 \end{pmatrix}, and compensates exactly (similar-dye2.png).
C5. Which spillovers does the program estimate, and what does it get for s_{21} without subtracting the offset? Why is that larger?
C6. Read the nucleus’s median in the dye-2 image and its slope for the three guesses. Compare with B7. What does the nucleus look like in dye2-over.png? Compare Pearson’s r before and after compensation, and explain the sign after (demo question E4).
C7. How much does the background noise grow with the similar dyes? Compare with B5.
The model solution is unmixing/src/crosstalk/solution.rs in the download.
C5. s_{21} = 0.249 and s_{12} = 0.050, close to the true 0.25 and 0.05. Without subtracting the offset, s_{21} = 0.271. The offset adds 10 counts to both channels of every pixel: a fake component with a flat spectrum. For the dim nucleus pixels it lifts the ratio channel 2 / channel 1 most, and the line through the origin tilts up.
C6. Guess 0.15: median 30.2, slope +0.100 (under-compensated, the ghost remains). Guess 0.25: median 0.0, slope -0.001 (flat, at the background level of -0.3). Guess 0.35: median -30.3, slope -0.103 (over-compensated). The slopes are s_{21} - g, as in B7(c). In dye2-over.png the nucleus is a dark hole, darker than the background: negative dye, which no stain can give. Pearson’s r inside the cell is 0.13 measured and -0.19 compensated. Compensation subtracts 0.25 \times channel 1 from channel 2, and with it channel 1’s noise. Where both dyes are absent (most of the cytoplasm) the noise of the two compensated images is then negatively correlated. The bleed-through’s positive correlation is gone; a small negative one from the noise remains.
C7. The background noise has a standard deviation of 3.2 counts in channel 2 and 22.4 after compensation, 7 times more. S_2^{-1} has entries of about 5, and adds the noise of both channels with weights 5.26 and -4.74: \sqrt{5.26^2 + 4.74^2} \approx 7. This is B5: a small determinant, a large inverse. In similar-dye2.png the whole image is grainy, the nucleus most.
Where this leads
With two dyes and two channels, compensation is a 2 \times 2 inverse. A spectral detector records many more channels than dyes, and the noise never fits all equations exactly: the next unit, least-squares unmixing, takes the mixture closest to the measured spectrum, uses the residual to find missing components, and measures how much similar dyes amplify noise with the condition number. Compensation returns as the second correction in what the microscope does to colocalisation (day 48), and with cross-excitation added in sensitised emission on the FRET day (day 49).